modify sst; read and write as it is

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/cnmap/cnmap.ncl"

begin
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;input
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PDataSST=addfile("~/data/HadISST_sst.nc","r")
sst=PDataSST->sst
target_month=8
sst!1="lat"
sst!2="lon"

latmin=-30
latmax=40
lonmin=30
lonmax=135

sst_for_eof_1=sst(lat|:,lon|:,time|1307+target_month:1739:12)
sst_for_eof_1_clim=clmMonLLT(sst_for_eof_1)
sst_for_eof_2=calcMonAnomLLT(sst_for_eof_1,sst_for_eof_1_clim)
sst_for_eof_dtrend=dtrend_n(sst_for_eof_2,False,2)
copy_VarCoords(sst_for_eof_1,sst_for_eof_dtrend)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;process
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;pre-process
; =================================================================
; create weights:  sqrt(cos(lat))   [or sqrt(gw) ]
;                                     =                                                                       =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =
  rad                                 = 4.*atan(1.)/180.
  clat                                = PDataSST->latitude
  clat                                = sqrt( cos(rad*clat) )                 ; gw for gaussian grid
  clat!0                              = "lat"
sst_for_eof_dtrend_weighted           = sst_for_eof_dtrend                                   ; copy meta data
sst_for_eof_dtrend_weighted           = sst_for_eof_dtrend*conform(sst_for_eof_dtrend, clat, 0)
;sst_for_eof_dtrend_weighted@long_name = "Wgt: "+sst_for_eof_dtrend_weighted@long_name
sst_for_eof=sst_for_eof_dtrend_weighted({lat|latmin:latmax},{lon|lonmin:lonmax},time|:)


neof=3
optEOF=True
;optEOF@jopt=1
sst_eof_v=eofunc_Wrap(sst_for_eof,neof,optEOF)
sst_eof_ts=eofunc_ts_Wrap(sst_for_eof,sst_eof_v,False)
printVarSummary(sst_eof_v)
printVarSummary(sst_eof_ts)
asciiwrite("eof_ts_1_"+target_month+".txt",sst_eof_ts(0,:))

stddev_eof_ts_1=dim_stddev_Wrap(sst_eof_ts(0,:))
print(stddev_eof_ts_1)
sst_rebuild_1=sst_eof_v*stddev_eof_ts_1*2.5
copy_VarCoords(sst_eof_v,sst_rebuild_1)
sst_rebuild=sst_rebuild_1(0,:,:)
printVarSummary(sst_rebuild)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;resourse
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
res=True
res@gsnFrame=False
res@gsnDraw=False
res@gsnAddCyclic         = False        ; data not cyclic
res@gsnSpreadColors      = True
;res@gsnSpreadColorStart=-1
;res@gsnSpreadColorEnd=2
res@cnFillOn=True
res@cnLinesOn=False
res@cnLineLabelsOn=False
res@cnLevelSelectionMode="ExplicitLevels"
res@cnLevels=fspan(-0.8,0.8,17)
res@mpMinLatF             = latmin
res@mpMaxLatF             = latmax
res@mpMinLonF             = lonmin
res@mpMaxLonF             = lonmax
res@mpDataSetName         = "Earth..4"   ; This new database contains
res@mpDataBaseVersion     = "MediumRes"  ; Medium resolution database
res@mpOutlineOn           = True         ; Turn on map outlines
res@mpOutlineSpecifiers   = (/"China:states","Taiwan"/)       ;China:states
res@gsnLeftString=""
res@lbLabelBarOn=True

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;plot
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
wks=gsn_open_wks("png",get_script_prefix_name+"_"+target_month)
;gsn_define_colormap(wks,"matlab_jet")
res@gsnRightString = sprintf("%5.1f", sst_eof_v@pcvar(0)) +"%"
;plot=gsn_csm_contour_map(wks,sst_eof_v(0,:,:),res)
plot=gsn_csm_contour_map(wks,sst_rebuild,res)

draw(plot)
frame(wks)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;add to amip clm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PInputData=addfile("./amip2_sst.data.climo.nc","r")
;system ("rm -f "+"~/FAMIL/exp/iobm/input/ocn/sst/amip2_sst.data.climo.nc")
;POutputData=addfile("~/FAMIL/exp/iobm/input/ocn/sst/amip2_sst.data.climo.nc","c")
system ("rm -f "+"./amip2_sst.data.climo.nc")
POutputData=addfile("./amip2_sst.data.climo.nc","c")

;weights matrix
weight_dim=dimsizes(sst_rebuild)
weight=new(weight_dim,float)
weight=0
bond_wid=5
do i=1,bond_wid
weight(i:weight_dim(0)-i,i:weight_dim(1)-i)=weight(i:weight_dim(0)-i,i:weight_dim(1)-i)+1./bond_wid
end do
copy_VarCoords(sst_rebuild,weight)
sst_rebuild=sst_rebuild*weight


sst_mod=short2flt(PInputData->sst)
printVarSummary(sst_mod(time|:,{lat|latmin:latmax},{lon|lonmin:lonmax}))
sst_mod(time|target_month-1,{lat|latmin:latmax},{lon|lonmin:lonmax})=sst_mod(time|target_month-1,{lat|latmin:latmax},{lon|lonmin:lonmax})+sst_rebuild

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;output
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dim_names = (/"time","lat","lon"/)
;time dimension is unlimited, but currently set to 1.
dim_sizes = dimsizes(sst)
dim_unlimited = (/True,False,False/)

print("Defining dimension sizes to be " + dim_sizes)
filedimdef(POutputData,dim_names,dim_sizes,dim_unlimited)


  att_names = getvaratts(PInputData)           ; get CCM file's global attributes
  if(.not.all(ismissing(att_names))) then
    do i = 0,dimsizes(att_names)-1
      print("copy_fileatts: global attributes->" + att_names(i))
      POutputData@$att_names(i)$ = PInputData@$att_names(i)$     ; copy CCM file's global att ributes
    end do
  end if

  names = getfilevarnames(PInputData)       ; get CCM file's variable names and print them
  print (names)
;
; loop over variables and copy to netCDF file
;
  do i=0, 2
    print("writing POutputData: i,name="+ i+ "   "+ names(i))
    POutputData->$names(i)$ = PInputData->$names(i)$
  end do
    POutputData->sst= sst_mod
  do i=4, dimsizes(names)-1
    print("writing POutputData: i,name="+ i+ "   "+ names(i))
    POutputData->$names(i)$ = PInputData->$names(i)$
  end do

  print (POutputData)      ; print overview of contents of netCDF file
end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末花枫,一起剝皮案震驚了整個(gè)濱河市型酥,隨后出現(xiàn)的幾起案子昧旨,更是在濱河造成了極大的恐慌,老刑警劉巖抽减,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件允青,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡卵沉,警方通過(guò)查閱死者的電腦和手機(jī)颠锉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)史汗,“玉大人琼掠,你說(shuō)我怎么就攤上這事⊥W玻” “怎么了瓷蛙?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我速挑,道長(zhǎng),這世上最難降的妖魔是什么副硅? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任姥宝,我火速辦了婚禮,結(jié)果婚禮上恐疲,老公的妹妹穿的比我還像新娘腊满。我一直安慰自己,他們只是感情好培己,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布碳蛋。 她就那樣靜靜地躺著,像睡著了一般省咨。 火紅的嫁衣襯著肌膚如雪肃弟。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,482評(píng)論 1 302
  • 那天零蓉,我揣著相機(jī)與錄音笤受,去河邊找鬼。 笑死敌蜂,一個(gè)胖子當(dāng)著我的面吹牛箩兽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播章喉,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼汗贫,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了秸脱?” 一聲冷哼從身側(cè)響起落包,我...
    開(kāi)封第一講書(shū)人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎撞反,沒(méi)想到半個(gè)月后妥色,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡遏片,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年嘹害,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吮便。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡笔呀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出髓需,到底是詐尸還是另有隱情许师,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站微渠,受9級(jí)特大地震影響搭幻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜逞盆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一檀蹋、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧云芦,春花似錦俯逾、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至琉历,卻和暖如春坠七,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背善已。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工灼捂, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人换团。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓悉稠,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親艘包。 傳聞我的和親對(duì)象是個(gè)殘疾皇子的猛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容