Re: how to use loop to read in multiple nc files

From: Soumik Basu <sbasu_at_nyahnyahspammersnyahnyah>
Date: Fri Oct 19 2012 - 11:55:24 MDT

You can try something like this :

***********************************************
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"
;***********************************************
;begin
;***********************************************
;read in netCDF file
;***********************************************

do yy = 1948, 2007 ; Start the year loop

f1 = addfiles("rhum."+yy+".nc","r")
f2 = addfiles("uwnd."+yy+".nc","r")
f3 = addfiles("vwnd."+yy+".nc","r")
rhum = f1->rhum(:,:,:,:)
q = rhum/100+302.66
copy_VarCoords(rhum,q)
uwnd = f2->uwnd(:,0:7,:,:)
u = uwnd/100+202.66
copy_VarCoords(uwnd,u)
vwnd = f3->vwnd(:,0:7,:,:)
v = vwnd/100+202.66
copy_VarCoords(vwnd,v)

qu=q*u
qv=q*v
copy_VarCoords(q,qu)
copy_VarCoords(q,qv)

g = 9.81
q = qu+qv
copy_VarCoords(qu,q)

qint = dim_sum_Wrap(q(time|:,lat|:,lon|:,level|:))
;*integration*******
Qint = dim_sum_Wrap(qint(lat|:,lon|:,time|:))
Qint = (Qint/g) ;***********weight by
gravity****************
Qint@units = "kg/ms"

end do ; end year loop

;************************************
; create the plot1
;************************************
wks=gsn_open_wks("ps","q")

res = True ; plot mods desired
res@tiMainString = "vertical integrated moisture transport"
; main title
res@cnFillOn = True ; turn on color fill
plot = gsn_csm_contour_map(wks,Qint(:,:),res)

end

On 2012-10-19 09:25, Ke Deng wrote:
> Dear all,
> I am computing the vertical integrated moisture transport (this is
> not the point) and get the right code for that. But now I meet with
> one problem. The data I use now are rhum.****.nc uwnd.****.nc
> vwnd.****.nc yearly data.(60 nc files for each one so 180 nc files
> totally) The below code is useful for one year data so the code is
> absolutely right. But now I want to compute the moisture transport
> over 60 years (1948-2007). At first I used the ncrcat command in
> linux
> to integrate the 60 nc files into one file but when I run the code it
> displayed the segmentation fault. And then I used the command
> systemfunc and addfiles in ncl to process that and also show some
> errors. So the only reason is there are too many files and the
> software can not see them as one big file to process. Now I am stuck
> here and don't know how to write one loop to solve that. Please your
> response will be appreciated! Thank u!
>
> ;***********************************************
> 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"
> ;***********************************************
> ;begin
> ;***********************************************
> ;read in netCDF file
> ;***********************************************
> f1 = addfiles("rhum.1977.nc [2]","r")
> f2 = addfiles("uwnd.1977.nc [3]","r")
> f3 = addfiles("vwnd.1977.nc [4]","r")
> rhum = f1->rhum(:,:,:,:)
> q = rhum/100+302.66
> copy_VarCoords(rhum,q)
> uwnd = f2->uwnd(:,0:7,:,:)
> u = uwnd/100+202.66
> copy_VarCoords(uwnd,u)
> vwnd = f3->vwnd(:,0:7,:,:)
> v = vwnd/100+202.66
> copy_VarCoords(vwnd,v)
>
> qu=q*u
> qv=q*v
> copy_VarCoords(q,qu)
> copy_VarCoords(q,qv)
>
> g = 9.81
> q = qu+qv
> copy_VarCoords(qu,q)
>
> qint = dim_sum_Wrap(q(time|:,lat|:,lon|:,level|:)) 
> ;*integration*******
> Qint = dim_sum_Wrap(qint(lat|:,lon|:,time|:))
> Qint = (Qint/g)            ;***********weight by
> gravity****************
> Qint@units = "kg/ms"
> ;************************************
> ;  create the plot1
> ;************************************
> wks=gsn_open_wks("ps","q")
>
> res                      = True               ; plot mods desired
> res@tiMainString         = "vertical integrated moisture
> transport"    ; main title
> res@cnFillOn             = True               ; turn on color fill
> plot = gsn_csm_contour_map(wks,Qint(:,:),res)
>
> end
>
>
>
> Links:
> ------
> [1] http://rhum.1977.nc
> [2] http://uwnd.1977.nc
> [3] http://vwnd.1977.nc
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

-- 
"Numbing the pain for a while will make it worse when you finally feel 
it.
  ~J.K. Rowling"
*****************************************
Soumik Basu
Graduate Student, Research Assistant
International Arctic Research Center
University of Alaska Fairbanks
Fairbanks,Alaska,USA
*****************************************
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Oct 19 11:55:35 2012

This archive was generated by hypermail 2.1.8 : Tue Oct 23 2012 - 11:10:04 MDT