Re: Computing Daily Averages and Outputting a netCDF file.

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Wed, 01 Jul 2009 08:56:56 -0600

Looks to me like you are prgramming in fortran or C :-)
I suggest taking advantage of NCL's array syntax.

[1]
When reading an entire array,

don't use

    y = f->uas(:,:,:)

rather
    y = f->uas

It is more efficient. Actually, if 'knowing' it is 3D
is important, I suggest

    y = f->uas ; (:,:,:)

[2]
Not sure why you did the following:
    m = new(1,float)
then
    m = 0

delete the 'm = new(1,float)' and just set m = 0.0
Really, you do not need to do this. See [3]

[3]
The following is untested. Since you have model output
I assume there are no missing values. (If there were, we
would have to count the number of valid values and if there
were not 8 values we would set yo _FillValue.)
Also, the following uses named dimensions time,lat,lon.
Change to appropriate names

       yday = y(::8,:,:) ; trick: allocate space & preserve meta data
       printVarSummary(yday)

NCL version up to 5.0.0
       do nt=0,ntim-1,8
          yday(nt/8,:,:) = (/ dim_avg( y(lat|:,lon|:,time|nt:nt+7) ) /)
       end do

NCL new version 5.1.0
       do nt=0,ntim-1,8
          yday(nt/8,:,:) = (/ dim_avg_n( y(nt:nt+7,:,:), 0 ) /)
       end do

Good luck

DJ Rasmussen wrote:
> Hello,
>
> I am trying to be able to read in a netCDF file with 3 hour data for an
> entire year, and then export a netCDF file with daily averages computed
> from the 3 hour data. So far I have created a variety of loops to run,
> but I get an error:
>
> fatal: Subscript out of range, error in subscript #0
> fatal:An error occured reading y
> fatal:An error occured reading y
> fatal:Execute: Error occurred at or near line 43 in file
> DMR_avgDaily_WSP10.ncl
>
> Regards,
>
> DJ Rasmussen
>
> ;*********************************************************************
> ; Calculate the Daily Average
> ;*********************************************************************
> 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
>
> ;************************************************************
> ; open files and read in desired variables
> ;************************************************************
>
> CtmDir = "/home/rasmussen/NARCCAP/"
> appl = "uas_CRCM_2038010103"
> f = addfile(CtmDir+appl+".nc","r")
> y = f->uas(:,:,:)
>
> dimy = dimsizes(y)
> time = dimy(0)
> xc = dimy(1)
> yc = dimy(2)
> tstep = (time)
>
> store = new((/tstep,xc,yc/), "float")
> m = new(1,float)
>
> ;************************************************************
> ; location loop
> ;*************************************************************
>
> do i=0,(xc-1)
>
> do j=0,(yc-1)
>
> ;************************************************************
> ; day loop
> ;*************************************************************
>
> a = 0
> b = 7
>
> do l=0,(tstep-1)
> z = y(a:b,i,j)
> m = 0
>
> ;************************************************************
> ; calculate average and average hours
> ;*************************************************************
>
> do k=0,7
> n = z(k)
> m = m + n
> end do ; calculate average
> mean = m/8
>
> store(l,i,j) = mean
> m = 0
> a = a+8
> b = b+8
>
> end do ; day loop
>
> end do ; col loop
>
> end do ; row loop
>
> ;************************************************************
> ; output
> ;*************************************************************
>
> store!0 = "TSTEP"
> store!1 = "ROW"
> store!2 = "COL"
>
> store_at_long_name = "Daily Avg Wind Speed"
> store_at_units = "m/s"
>
> ncdf = addfile("dailyAvguas_CRCM_2038010103.nc","c")
> filedimdef(ncdf,"TSTEP",-1,True)
> fileattdef (ncdf, f)
>
> ncdf->uas = store
>
> end
>
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Jul 01 2009 - 08:56:56 MDT

This archive was generated by hypermail 2.2.0 : Thu Jul 02 2009 - 11:39:37 MDT