[ncl-talk] Array operation to compute average week anomalies of sst mistake

Mary Haley haley at ucar.edu
Thu Aug 6 15:07:19 MDT 2015


Sebastian,

First, to get the dimension sizes of an array, this is slightly cleaner
code:

 dims  = dimsizes(sst)
 times = dims(0)
 row   = dims(1)   ; perhaps call it "nlat" instead of "row"?
 col   = dims(2)   ; "nlon" instead of "ncol" ?

Second, when dealing with whole arrays, you don't need to use "(:,:,:)"
type of syntax.

sst = a->anom(:,:,:)

or

sst_avg(:,:)=sst_avg(:,:)/times

If you want the whole array, then:

sst = a->anom

and

sst_avg = sst_avg/times

work fine.


Third, you want to avoid looping in NCL to do calculations, because it's an
unnecessarily expensive operation, and NCL allows for direct array
computations.

Fourth, in this case, there's a function for doing averages across a
specified dimension. Please see "dim_avg" and "dim_avg_n":

http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_avg.shtml
http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_avg_n.shtml

"dim_avg_n_Wrap" will further preserve metadata:

http://www.ncl.ucar.edu/Document/Functions/Contributed/dim_avg_n_Wrap.shtml

I don't know what format your daily temperature values are in, but let's
say you have one value per day.  This means that the first 7 values of the
time dimension will be associated with the first week, the second 7 values
with the second week, etc.

In this case, then, you can reshape the array to be nweeks x 7 x nlat x
nlon, and then take the average across the second dimension (dimension
index 1, because dimension indexing starts at 0 and goes from left to
right).

This will only work if you actually have a multiple of 7 time values. So,
for example, let's say you have 52 weeks of daily values, hence time is 52
* 7 = 364. Here's how you calculate a weekly average:

 dims  = dimsizes(sst)
 ntime = dims(0)
 nlat  = dims(1)
 nlon  = dims(2)

 days_per_week = 7

 nweeks = ntime / days_per_week

; Reshape 3D array to be a 4D array
 sst4d  = reshape(sst,(/nweeks,days_per_week,nlat,nlon/))

; average across the "1" dimension
 sst_weekly_avg = dim_avg_n(sst4d,1)

​--Mary

​
On Wed, Aug 5, 2015 at 12:02 PM, Sebastian Otarola-Bustos <
Sebastian.F.Otarola-Bustos.1 at nd.edu> wrote:

> Hi everybody, I'm doing very simple array operations, but it seems that
> NCL doesn't agree with me, and I'm getting very wrong numbers.
>
> Here is the code I'm refering too:
>
> I'm trying to make a weekly average of daily sst temperatures, so sst is(
> times, lat,lon), then I define a 2D array(lat,lon) , sst_avg, and
> initializes with 0 , up to now everythings goes ok. But in this case, after
> the orange do sst_average is still full with zeros. Finally, I'm just
> trying to divide each element of sst_avg by number of times(days), so I'll
> get the average. But this not work. And it's seems to be very basic error.
> Any help would be really appreciated.
>
>
>  sst = a->anom(:,:,:)
>  printVarSummary(sst)
>  times=dimsizes(sst(:,0,0))
>  row  = dimsizes(sst(0,:,0))
>  col  = dimsizes(sst(0,0,:))
>  sst_avg = new((/row,col/),"float",0)
>          do it =0,times-1
>              sst_avg(:,:)=sst_avg(:,:)+sst(it,:,:)
>          end do
>  print(sst_avg)
>  sst_avg(:,:)=sst_avg(:,:)/times    ; Calculo anomalia semanal promedio
>
>
> All the best,
> Sebastián.
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150806/141c0737/attachment.html 


More information about the ncl-talk mailing list