# [ncl-talk] Calculating annual averages from year/lat/lon grid

Dennis Shea shea at ucar.edu
Tue Jan 22 12:17:30 MST 2019

```Maybe:
===============
let:  x(time,lat,lon) where lat[*] and lon [*]

clat   = *cos*
xAve   = *wgt_areaave_Wrap*
<http://www.ncl.ucar.edu/Document/Functions/Contributed/wgt_areaave_Wrap.shtml>(x,
clat, 1.0, 1)

*printVarSummary*(xAve)

=== For a non-rectilinear grid

q      = f->Q                            ; (ntim,nlat,mlon)
lat2d  = f->LAT                          ; (nlat,mlon)
clat2d = *cos*
<http://www.ncl.ucar.edu/Document/Functions/Built-in/cos.shtml>(lat2d*0.01745329)
; (nlat,mlon)
qAve   = *wgt_areaave2*
<http://www.ncl.ucar.edu/Document/Functions/Built-in/wgt_areaave2.shtml>(q,
clat2d, opt)    ; opt = 0 or 1; (ntim)

copy_VarMeta(q(:,0,0), qAve)

printVarSummary(qAve)

other functions:

*http://www.ncl.ucar.edu/Document/Functions/Contributed/month_to_annual.shtml*
<http://www.ncl.ucar.edu/Document/Functions/Contributed/month_to_annual.shtml>

*D*

On Tue, Jan 22, 2019 at 9:32 AM Toni Klemm <toni-klemm at tamu.edu> wrote:

> All,
>
> I think I solved my own problem (see below). I used the avg function
> instead of dim_avg_n and looped through each year (because avg creates
> averages regardless of dimension, but I only wanted it to consider data
> from a single year), which produced the same averages I got from Excel.
> This is the code I used:
>
> do z=0,84
>   NPP_G_annual_avg(z) = avg(NPP_G(z,:,:))
> end do
>
>
> Please reply if this might still be wrong and/or if you have a different
> solution.
>
> Thanks,
> Toni
>
>
>
> *Toni Klemm, Ph.D.*Postdoctoral Research Associate
> Department of Ecosystem Science and Management
> College of Agriculture and Life Sciences
> Texas A&M University, College Station, TX
> Contributor to the Early Career Climate Forum <http://www.eccforum.org>
>
>
>
>
>
>
>
>
> On Jan 22, 2019, at 9:58 AM, Toni Klemm <toni-klemm at tamu.edu> wrote:
>
> Hello NCL community,
>
> I have a three dimensional grid dataset (year/latitude/longitude) that
> want to calculate the annual average of by averaging the second and third
> dimension, but I don’t think my approach is correct.
>
> The data are projections of net primary productivity (NPP) of grassland in
> the US Great Plains from 2015 to 2099. Most of the grid cells are missing
> values because most grid cells are classified as shrubland, forest, etc. …
> but not grassland and therefore have no grassland NPP.
>
> My approach so far was to average the third dimension first and then the
> second dimension using this command:
> NPP_G_annual_avg = dim_avg_n(dim_avg_n(NPP_G,2),1)
>
> However, I think this produces incorrect results. Comparing NCL with the
> average produced in Microsoft Excel, I get 394.8 from NCL and 417.6 from
> Excel for the year 2015 (the unit is grams of Carbon per square meter per
> year). I think Excel is correct, because it averages all values at once
> while NCL averages one dimension first and then the other, which I think
> creates a bias. Does anyone know how to average two dimensions at once in
> NCL? I attached the 2015 data for the Northern Great Plains as .csv file.
> Missing values are listed as “NaN”.
>
> Thank you very much for your help! I really appreciate it.
>
> Best,
> Toni
>
>
>
> *Toni Klemm, Ph.D.*Postdoctoral Research Associate
> Department of Ecosystem Science and Management
> College of Agriculture and Life Sciences
> Texas A&M University, College Station, TX
> Contributor to the Early Career Climate Forum <http://www.eccforum.org/>
>
>
>
>
>
>
>
> <NPP_G_annual_avg_NP_2015.csv>
>
>
> _______________________________________________
> 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/20190122/31c64ca5/attachment.html>
```