escor and write .nc

Why

相关分析我之前也写过,为什么还要有这篇文章?因为我希望将来用模块化的思维使用NCL。计算是一部分,画图是另一部分。这样每次一个routine需要做什么就调用之前写好的template就可以。思路上共3部分:计算;输出;leadlag以及合并。

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"

begin
lead_lag_num=12
target_month=13

target_index_name="tp_sh_stat"
target_index_raw   = asciiread("~/project/iobm_plus/sst_eof/iobm_lsf/IOBM_monthly_index_"+target_month+".txt", -1, "float")
year               = ispan(1979,2014,1)
year_start         = 1981
year_end           = 2008
year_num           = year_end-year_start+1
target_index       = target_index_raw
target_index!0     = "year"
target_index&year  = year
;index              = dim_standardize_Wrap(target_index({year_start:year_end},{target_month}),0)
index              = dim_standardize_Wrap(dtrend(target_index({year_start:year_end}),False),0)
copy_VarCoords(target_index({year_start:year_end}),index)
printVarSummary(index)

file_list=systemfunc("ls /uwnd.mon.mean.1958-2014.nc")
a=addfile(file_list,"r")
sst_summer=short2flt(a->uwnd((year_start-1958)*12+target_month-lead_lag_num-1:(year_end-1958)*12+target_month-lead_lag_num-1:12,:,:,:))
printVarSummary(sst_summer)
sst_summer_dtrend=dtrend_n(sst_summer,True,0)
copy_VarCoords(sst_summer,sst_summer_dtrend)
coe=escorc(index,sst_summer_dtrend(lev|:,lat|:,lon|:,time|:))
copy_VarCoords(sst_summer_dtrend(0,:,:,:),coe)
printVarSummary(coe)
printMinMax(coe,False)

coe_ttest=coe
coe_ttest=coe@_FillValue
dim_coe_ttest=dimsizes(coe_ttest)
do i=0,dim_coe_ttest(0)-1
    do j=0,dim_coe_ttest(1)-1
      do k=0,dim_coe_ttest(2)-1
        n    = dimsizes(index) ; n=11
        df   = n-2         ; degree of freedom
        t    = coe(i,j,k)*sqrt((n-2)/(1-coe(i,j,k)^2))
        p    = student_t(t, df)
        coe_ttest(i,j,k)=p
      end do 
    end do 
end do 
copy_VarCoords(coe,coe_ttest)
printMinMax(coe_ttest,False)
printVarSummary(coe_ttest)

output_filename=get_script_prefix_name+"_"+target_month+"_"+lead_lag_num+".nc"
system("rm -f "+output_filename)
PDataOut=addfile("./"+get_script_prefix_name+"/"+output_filename,"c")
;***************************************************
; define file options [Version a033]
;***********************************************
setfileoption(PDataOut, "prefill", False)
setfileoption(PDataOut, "suppressclose", True)
setfileoption(PDataOut, "definemode", True)
;***********************************************
; assign file attributes
;***********************************************   
;*****************************************************                 
fAtt               = True                       
fAtt@title         = "correlation between GODAS ts and "+target_index_name+"monthly data"         
fAtt@source        = "GODAS station obs"
fAtt@Conventions   = "None"
fAtt@creation_date = systemfunc("date")                                         
fileattdef( PDataOut, fAtt )
;***********************************************
; predefine coordinate information               
;***********************************************
dimNames = (/"lev", "lat", "lon" /)                       
dimSizes = (/dimsizes(coe&lev),dimsizes(coe&lat),dimsizes(coe&lon)/)                         
dimUnlim = (/False,False,False/)                         
filedimdef(PDataOut, dimNames,dimSizes,dimUnlim)

filevardef(PDataOut, "lat", typeof(coe&lat), getvardims(coe&lat))
filevarattdef(PDataOut, "lat", coe&lat)

filevardef(PDataOut, "lon", typeof(coe&lon), getvardims(coe&lon) )
filevarattdef(PDataOut, "lon", coe&lon)     

filevardef(PDataOut,"lev",typeof(coe&lev), getvardims(coe&lev) )
filevarattdef(PDataOut,"lev", coe&lev)

filevardef(PDataOut,"coe",typeof(coe), getvardims(coe) )
filevarattdef(PDataOut,"coe", coe)

filevardef(PDataOut,"coe_ttest",typeof(coe_ttest), getvardims(coe_ttest) )
filevarattdef(PDataOut,"coe_ttest", coe_ttest)
;***********************************************
; terminate define mode: not necessary but for clarity
;***********************************************
setfileoption(PDataOut, "definemode", False)
;***********************************************
; write data values to predefined locations 
; (/ .../) operator transfer values only                     
;***********************************************   
PDataOut->lat    = (/ coe&lat /)                                             
PDataOut->lon    = (/ coe&lon /)
PDataOut->lev   = (/ coe&lev /)
PDataOut->coe   = (/ coe /)
PDataOut->coe_ttest   = (/ coe_ttest /)
end

接下来用nco的命令ncecat将之前算的文件合并即可。之所以不直接都写在ncl里,一是考虑到高内聚低冗杂的模块化思想,二是考虑到可以降低内存方面的负载。至于为什么不在bash里优雅地使用循环。。好吧,长时间不用已经不想写bash了,觉得bash这种语言可读性不高,简单地句子分分钟明白(写几层循环过几天看整个人都不好了),而且本人Vim玩的很熟,上面地leadlag也就是录个宏搞定地,写下面这个东西也是几个正则表达式就搞定了。

ncecat -u lead_lag "${0%.*}"_1_-12.nc "${0%.*}"_1_-11.nc "${0%.*}"_1_-10.nc "${0%.*}"_1_-9.nc "${0%.*}"_1_-8.nc "${0%.*}"_1_-7.nc "${0%.*}"_1_-6.nc "${0%.*}"_1_-5.nc "${0%.*}"_1_-4.nc "${0%.*}"_1_-3.nc "${0%.*}"_1_-2.nc "${0%.*}"_1_-1.nc "${0%.*}"_1_0.nc "${0%.*}"_1_1.nc "${0%.*}"_1_2.nc "${0%.*}"_1_3.nc "${0%.*}"_1_4.nc "${0%.*}"_1_5.nc "${0%.*}"_1_6.nc "${0%.*}"_1_7.nc "${0%.*}"_1_8.nc "${0%.*}"_1_9.nc "${0%.*}"_1_10.nc "${0%.*}"_1_11.nc "${0%.*}"_1_12.nc                           "${0%.*}"_1.nc
ncecat -u lead_lag "${0%.*}"_2_-12.nc "${0%.*}"_2_-11.nc "${0%.*}"_2_-10.nc "${0%.*}"_2_-9.nc "${0%.*}"_2_-8.nc "${0%.*}"_2_-7.nc "${0%.*}"_2_-6.nc "${0%.*}"_2_-5.nc "${0%.*}"_2_-4.nc "${0%.*}"_2_-3.nc "${0%.*}"_2_-2.nc "${0%.*}"_2_-1.nc "${0%.*}"_2_0.nc "${0%.*}"_2_1.nc "${0%.*}"_2_2.nc "${0%.*}"_2_3.nc "${0%.*}"_2_4.nc "${0%.*}"_2_5.nc "${0%.*}"_2_6.nc "${0%.*}"_2_7.nc "${0%.*}"_2_8.nc "${0%.*}"_2_9.nc "${0%.*}"_2_10.nc "${0%.*}"_2_11.nc "${0%.*}"_2_12.nc                           "${0%.*}"_2.nc
ncecat -u lead_lag "${0%.*}"_3_-12.nc "${0%.*}"_3_-11.nc "${0%.*}"_3_-10.nc "${0%.*}"_3_-9.nc "${0%.*}"_3_-8.nc "${0%.*}"_3_-7.nc "${0%.*}"_3_-6.nc "${0%.*}"_3_-5.nc "${0%.*}"_3_-4.nc "${0%.*}"_3_-3.nc "${0%.*}"_3_-2.nc "${0%.*}"_3_-1.nc "${0%.*}"_3_0.nc "${0%.*}"_3_1.nc "${0%.*}"_3_2.nc "${0%.*}"_3_3.nc "${0%.*}"_3_4.nc "${0%.*}"_3_5.nc "${0%.*}"_3_6.nc "${0%.*}"_3_7.nc "${0%.*}"_3_8.nc "${0%.*}"_3_9.nc "${0%.*}"_3_10.nc "${0%.*}"_3_11.nc "${0%.*}"_3_12.nc                           "${0%.*}"_3.nc
ncecat -u lead_lag "${0%.*}"_4_-12.nc "${0%.*}"_4_-11.nc "${0%.*}"_4_-10.nc "${0%.*}"_4_-9.nc "${0%.*}"_4_-8.nc "${0%.*}"_4_-7.nc "${0%.*}"_4_-6.nc "${0%.*}"_4_-5.nc "${0%.*}"_4_-4.nc "${0%.*}"_4_-3.nc "${0%.*}"_4_-2.nc "${0%.*}"_4_-1.nc "${0%.*}"_4_0.nc "${0%.*}"_4_1.nc "${0%.*}"_4_2.nc "${0%.*}"_4_3.nc "${0%.*}"_4_4.nc "${0%.*}"_4_5.nc "${0%.*}"_4_6.nc "${0%.*}"_4_7.nc "${0%.*}"_4_8.nc "${0%.*}"_4_9.nc "${0%.*}"_4_10.nc "${0%.*}"_4_11.nc "${0%.*}"_4_12.nc                           "${0%.*}"_4.nc
ncecat -u lead_lag "${0%.*}"_5_-12.nc "${0%.*}"_5_-11.nc "${0%.*}"_5_-10.nc "${0%.*}"_5_-9.nc "${0%.*}"_5_-8.nc "${0%.*}"_5_-7.nc "${0%.*}"_5_-6.nc "${0%.*}"_5_-5.nc "${0%.*}"_5_-4.nc "${0%.*}"_5_-3.nc "${0%.*}"_5_-2.nc "${0%.*}"_5_-1.nc "${0%.*}"_5_0.nc "${0%.*}"_5_1.nc "${0%.*}"_5_2.nc "${0%.*}"_5_3.nc "${0%.*}"_5_4.nc "${0%.*}"_5_5.nc "${0%.*}"_5_6.nc "${0%.*}"_5_7.nc "${0%.*}"_5_8.nc "${0%.*}"_5_9.nc "${0%.*}"_5_10.nc "${0%.*}"_5_11.nc "${0%.*}"_5_12.nc                           "${0%.*}"_5.nc
ncecat -u lead_lag "${0%.*}"_6_-12.nc "${0%.*}"_6_-11.nc "${0%.*}"_6_-10.nc "${0%.*}"_6_-9.nc "${0%.*}"_6_-8.nc "${0%.*}"_6_-7.nc "${0%.*}"_6_-6.nc "${0%.*}"_6_-5.nc "${0%.*}"_6_-4.nc "${0%.*}"_6_-3.nc "${0%.*}"_6_-2.nc "${0%.*}"_6_-1.nc "${0%.*}"_6_0.nc "${0%.*}"_6_1.nc "${0%.*}"_6_2.nc "${0%.*}"_6_3.nc "${0%.*}"_6_4.nc "${0%.*}"_6_5.nc "${0%.*}"_6_6.nc "${0%.*}"_6_7.nc "${0%.*}"_6_8.nc "${0%.*}"_6_9.nc "${0%.*}"_6_10.nc "${0%.*}"_6_11.nc "${0%.*}"_6_12.nc                           "${0%.*}"_6.nc
ncecat -u lead_lag "${0%.*}"_7_-12.nc "${0%.*}"_7_-11.nc "${0%.*}"_7_-10.nc "${0%.*}"_7_-9.nc "${0%.*}"_7_-8.nc "${0%.*}"_7_-7.nc "${0%.*}"_7_-6.nc "${0%.*}"_7_-5.nc "${0%.*}"_7_-4.nc "${0%.*}"_7_-3.nc "${0%.*}"_7_-2.nc "${0%.*}"_7_-1.nc "${0%.*}"_7_0.nc "${0%.*}"_7_1.nc "${0%.*}"_7_2.nc "${0%.*}"_7_3.nc "${0%.*}"_7_4.nc "${0%.*}"_7_5.nc "${0%.*}"_7_6.nc "${0%.*}"_7_7.nc "${0%.*}"_7_8.nc "${0%.*}"_7_9.nc "${0%.*}"_7_10.nc "${0%.*}"_7_11.nc "${0%.*}"_7_12.nc                           "${0%.*}"_7.nc
ncecat -u lead_lag "${0%.*}"_8_-12.nc "${0%.*}"_8_-11.nc "${0%.*}"_8_-10.nc "${0%.*}"_8_-9.nc "${0%.*}"_8_-8.nc "${0%.*}"_8_-7.nc "${0%.*}"_8_-6.nc "${0%.*}"_8_-5.nc "${0%.*}"_8_-4.nc "${0%.*}"_8_-3.nc "${0%.*}"_8_-2.nc "${0%.*}"_8_-1.nc "${0%.*}"_8_0.nc "${0%.*}"_8_1.nc "${0%.*}"_8_2.nc "${0%.*}"_8_3.nc "${0%.*}"_8_4.nc "${0%.*}"_8_5.nc "${0%.*}"_8_6.nc "${0%.*}"_8_7.nc "${0%.*}"_8_8.nc "${0%.*}"_8_9.nc "${0%.*}"_8_10.nc "${0%.*}"_8_11.nc "${0%.*}"_8_12.nc                           "${0%.*}"_8.nc
ncecat -u lead_lag "${0%.*}"_9_-12.nc "${0%.*}"_9_-11.nc "${0%.*}"_9_-10.nc "${0%.*}"_9_-9.nc "${0%.*}"_9_-8.nc "${0%.*}"_9_-7.nc "${0%.*}"_9_-6.nc "${0%.*}"_9_-5.nc "${0%.*}"_9_-4.nc "${0%.*}"_9_-3.nc "${0%.*}"_9_-2.nc "${0%.*}"_9_-1.nc "${0%.*}"_9_0.nc "${0%.*}"_9_1.nc "${0%.*}"_9_2.nc "${0%.*}"_9_3.nc "${0%.*}"_9_4.nc "${0%.*}"_9_5.nc "${0%.*}"_9_6.nc "${0%.*}"_9_7.nc "${0%.*}"_9_8.nc "${0%.*}"_9_9.nc "${0%.*}"_9_10.nc "${0%.*}"_9_11.nc "${0%.*}"_9_12.nc                           "${0%.*}"_9.nc
ncecat -u lead_lag "${0%.*}"_10_-12.nc "${0%.*}"_10_-11.nc "${0%.*}"_10_-10.nc "${0%.*}"_10_-9.nc "${0%.*}"_10_-8.nc "${0%.*}"_10_-7.nc "${0%.*}"_10_-6.nc "${0%.*}"_10_-5.nc "${0%.*}"_10_-4.nc "${0%.*}"_10_-3.nc "${0%.*}"_10_-2.nc "${0%.*}"_10_-1.nc "${0%.*}"_10_0.nc "${0%.*}"_10_1.nc "${0%.*}"_10_2.nc "${0%.*}"_10_3.nc "${0%.*}"_10_4.nc "${0%.*}"_10_5.nc "${0%.*}"_10_6.nc "${0%.*}"_10_7.nc "${0%.*}"_10_8.nc "${0%.*}"_10_9.nc "${0%.*}"_10_10.nc "${0%.*}"_10_11.nc "${0%.*}"_10_12.nc  "${0%.*}"_10.nc
ncecat -u lead_lag "${0%.*}"_11_-12.nc "${0%.*}"_11_-11.nc "${0%.*}"_11_-10.nc "${0%.*}"_11_-9.nc "${0%.*}"_11_-8.nc "${0%.*}"_11_-7.nc "${0%.*}"_11_-6.nc "${0%.*}"_11_-5.nc "${0%.*}"_11_-4.nc "${0%.*}"_11_-3.nc "${0%.*}"_11_-2.nc "${0%.*}"_11_-1.nc "${0%.*}"_11_0.nc "${0%.*}"_11_1.nc "${0%.*}"_11_2.nc "${0%.*}"_11_3.nc "${0%.*}"_11_4.nc "${0%.*}"_11_5.nc "${0%.*}"_11_6.nc "${0%.*}"_11_7.nc "${0%.*}"_11_8.nc "${0%.*}"_11_9.nc "${0%.*}"_11_10.nc "${0%.*}"_11_11.nc "${0%.*}"_11_12.nc  "${0%.*}"_11.nc
ncecat -u lead_lag "${0%.*}"_12_-12.nc "${0%.*}"_12_-11.nc "${0%.*}"_12_-10.nc "${0%.*}"_12_-9.nc "${0%.*}"_12_-8.nc "${0%.*}"_12_-7.nc "${0%.*}"_12_-6.nc "${0%.*}"_12_-5.nc "${0%.*}"_12_-4.nc "${0%.*}"_12_-3.nc "${0%.*}"_12_-2.nc "${0%.*}"_12_-1.nc "${0%.*}"_12_0.nc "${0%.*}"_12_1.nc "${0%.*}"_12_2.nc "${0%.*}"_12_3.nc "${0%.*}"_12_4.nc "${0%.*}"_12_5.nc "${0%.*}"_12_6.nc "${0%.*}"_12_7.nc "${0%.*}"_12_8.nc "${0%.*}"_12_9.nc "${0%.*}"_12_10.nc "${0%.*}"_12_11.nc "${0%.*}"_12_12.nc  "${0%.*}"_12.nc
ncecat -u month "${0%.*}"_1.nc "${0%.*}"_2.nc "${0%.*}"_3.nc "${0%.*}"_4.nc "${0%.*}"_5.nc "${0%.*}"_6.nc "${0%.*}"_7.nc "${0%.*}"_8.nc "${0%.*}"_9.nc "${0%.*}"_10.nc "${0%.*}"_11.nc "${0%.*}"_12.nc "${0%.*}".nc 

最后生成地文件大概就是这样:

Variable: f
Type: file
filename:   coe_uwnd_iobm
path:   coe_uwnd_iobm.nc
   file global attributes:
      creation_date : Wed Feb 22 20:00:53 CST 2017
      Conventions : None
      source : GODAS station obs
      title : correlation between GODAS ts and tp_sh_statmonthly data
      history : Fri Feb 24 07:28:23 2017: ncecat -u month coe_uwnd_iobm_1.nc coe_uwnd_iobm_2.nc coe_uwnd_iobm_3.c
Fri Feb 24 07:27:39 2017: ncecat -u lead_lag coe_uwnd_iobm_1_-12.nc coe_uwnd_iobm_1_-11.nc coe_uwnd_iobm_1_-10.nc
      nco_openmp_thread_number : 1
   dimensions:
      month = 12  // unlimited
      lat = 145
      lon = 288
      lev = 37
      lead_lag = 25
   variables:
      float lat ( lat )
         long_name :    latitude
         GridType : Cylindrical Equidistant Projection Grid
         units :    degrees_north
         Dj :   1.25
         Di :   1.25
         Lo2 :  -1.25
         La2 :  -90
         Lo1 :   0
         La1 :  90

      float lon ( lon )
         long_name :    longitude
         GridType : Cylindrical Equidistant Projection Grid
         units :    degrees_east
         Dj :   1.25
         Di :   1.25
         Lo2 :  -1.25
         La2 :  -90
         Lo1 :   0
         La1 :  90

      integer lev ( lev )
         long_name :    isobaric level
         units :    hPa

      float coe ( month, lead_lag, lev, lat, lon )
         _FillValue :   9.96921e+36

      float coe_ttest ( month, lead_lag, lev, lat, lon )
         _FillValue :   9.96921e+36
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容