Re: do loop problem

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Thu Nov 10 2011 - 11:08:12 MST

This is not clear to me. You may have left out something.

Actually, I think you should use 'addfiles'
   http://www.ncl.ucar.edu/Document/Functions/Built-in/addfiles.shtml
to read the variable across all files. PLEASE read the documentation
(see the Examples)

Then use: dim_avg_n_Wrap. PLEASE read the documentation
http://www.ncl.ucar.edu/Document/Functions/Contributed/dim_avg_n_Wrap.shtml

The following is modified from a script I have.
If it does not work you should use 'print' statements
to find any errors.

Also you may wish to create a netCDF of the result. See Method 1
   http://www.ncl.ucar.edu/Applications/o-netcdf.shtml

;*****************************************************
; Miscellaneous initializations
;*****************************************************

    yrStrt = 2010
    yrLast = 2010
    nmoStrt = 8 ; 1
    nmoLast = 8 ; 12

    var = "CPRAT_P8_L1_GGA0_avg"

    dRoot = "/data14/cluo/WRFV3.2.1/WRFDATA/CFSR/" ; GRIB
    fRoot = "flxf01.gdas." ; file root

;*****************************************************
; Read 4x-daily data; Force addfiles to read 'time'
;*****************************************************

   setfileoption("grb","SingleElementDimensions",(/"Initial_time"/))

   do year=yrStrt,yrLast
     do nmo=nmoStrt,nmoLast
        yyyymm = year*100+nmo
        print("=====> Calculating Monthly Mean: "+yyyymm+" <=====")

        diri = dRoot + yyyymm +"/"
        fili = systemfunc("cd "+diri+" ; ls "+fRoot+yyyymm+"*")
        print(fili)
        nfili = dimsizes(fili)

        f = addfiles( diri+fili+".grb2", "r")
        x = f[:]->$var$
        printVarSummary(x)

        xMon = dim_avg_n_Wrap(x, 0)
        printVarSummary(xMon)

       delete([/ fili, x, xMon /] )
     end do ; nmo
   end do ; year

On 11/09/2011 09:53 PM, Chao Luo wrote:
> Hi Dave,
>
> Thanks! This works for my do loop. I need to sum two dim array in loop. But how to average the two dimenison arry which has Fill values in the arry. My script is:
>
> ----------------
> do id = 1, 30
> if (id .lt. 10) then
> id_str = "0" + id
> else
> id_str = "" + id
> end if
>
> do it = 0, 18, 6
> if (it .lt. 10) then
> it_str = "0" + it
> else
> it_str = "" + it
> end if
>
> fname = id_str + it_str
>
> f = addfile ("/data14/cluo/WRFV3.2.1/WRFDATA/CFSR/201008/flxf01.gdas.201008"+fname+".grb2","r")
> avgprecip = avgprecip + f->CPRAT_P8_L1_GGA0_avg
> end do
> end do
> avgprecip = avgprecip/30./4.
> -------------------
>
> In above script, CPRAT_P8_L1_GGA0_avg is two dim array, which has Fill values in the array.
>
> Thanks,
>
> Chao
>
>
> ----- Original Message -----
> From: "David Brown"<dbrown@ucar.edu>
> To: "Chao Luo"<chao.luo@eas.gatech.edu>
> Cc: ncl-talk@ucar.edu
> Sent: Wednesday, November 9, 2011 4:51:34 PM
> Subject: Re: do loop problem
>
> Hi Chao,
> I see a number of things wrong.
> In the do loop you spelled "precip" as "prcip" on the addfile line. That means that avgprecip always gets set to the first value of precip that you initialize before the do loop.
> You define id and it as integers for the do loops but then you try to make them into strings. You need separate string variables and you certainly do not want to modify the loop counter variables within the loop.
> The expression "id" is not going to give you the integer value of the variable id as a string, it is going to give you the string "id"
> Also if you are getting the complete variable, it is more efficient not to use '(:,:)'
> You should indent properly as well
>
> NCL does not have a single line "if" statement. Try this:
>
> do id = 1, 30
> if (id .lt. 10) then
> id_str = "0" + id
> else
> id_str = "" + id
> end if
> do it = ....
>
> etc. Hope this helps.
> -dave
>
> On Nov 9, 2011, at 4:55 PM, Chao Luo wrote:
>
>> Hi,
>>
>> I want to average one parameter in CFSR files. The file name like:
>> gdas.2010080100.grb2, gads.2010080106.grbs, gads.2010080112.grb, gads.2010080118.grb2
>> gdas.2010080200.grb2, gads.2010080206.grbs, gads.2010080212.grb, gads.2010080218.grb2
>> ...
>>
>> The NCL script I wrote as follows, but it dosn't work. Any help is very apprciated!
>>
>> Thanks much!
>>
>> Chao
>>
>> ------------------------
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>>
>> begin
>> f = addfile ("/data14/cluo/WRFV3.2.1/WRFDATA/CFSR/201008/flxf01.gdas.2010080100.grb2", "r")
>> precip = f-> CPRAT_P8_L1_GGA0_avg(:,:)
>> avgprecip = precip
>> avgprecip(:,:) = 0.
>>
>> ;;;do loop for month files
>>
>> do id = 1, 30
>> if ( id.lt.10 ) then id = 0 + "id"
>> do it = 0, 18, 6
>> if (it.lt.10) then it = 0 + "it"
>> fname = "id" + "it"
>> f = addfile ("/data14/cluo/WRFV3.2.1/WRFDATA/CFSR/201008/flxf01.gdas.201008+"fname","r")
>> prcip = f->CPRAT_P8_L1_GGA0_avg(:,:) ; total precipitation
>> avgprecip(:,:) = avgprecip(:,:) + precip(:,:)
>> end do
>> end do
>> avgprecip(:,:) = avgprecip(:,:)/30./4.
>> end
>> ------------------------
>> _______________________________________________
>> 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

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Nov 10 11:08:18 2011

This archive was generated by hypermail 2.1.8 : Mon Nov 14 2011 - 10:41:55 MST