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

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Fri Oct 19 2012 - 14:27:30 MDT

To loop over files ...

[1] fils_r = systemfunc("ls rhum.*.nc")
      fils_u =
      fils_v =

      nfils = dimsizes(filsr)

      f1 = addfiles("rhum.*.nc","r")
      f2 = addfiles("uwnd.*.nc","r")
      f3 = addfiles("vwnd.*.nc","r")

      do nf=0,nfils-1 ; loop over each file

         r = short2flt( f[nf]->rhum )
         u = short2flt( f[nf]->u(:,0:7,:,:) )
         v

         delete([/ r, u, v /]) ; may change size
      end if

[2] Replace
         qint = dim_sum_Wrap(q(time|:,lat|:,lon|:,level|:))
     with
         qint = dim_sum_Wrap(q,1) ; less memory intensive

     Replace
         Qint = dim_sum_Wrap(qint,0)
     With

On 10/19/12 11:55 AM, Soumik Basu wrote:
> 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
>
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Oct 19 14:27:44 2012

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