# Composite picture netCDF?

**URL:** <https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545>\
**Category:** Q&A\
**Created:** [April 7, 2021, 7:23pm UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545 "2021-04-07T19:23:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 7, 2021, 7:23pm UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/1 "2021-04-07T19:23:07Z")

</div>

Hi there,

Given several layers in a 3D NetCDF file (lon x lat x time), is there a way to compute a mean state of these layers … so a 2D ( lon x lat ) … without having to resort to loop?

Something like

```auto
grdmath FILE?var[a:b] MEAN 
- OR -
grdmath FILE?var[list_indices] MEAN

```

Thanks

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [April 7, 2021, 11:57pm UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/2 "2021-04-07T23:57:23Z")

</div>

No.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 8, 2021, 12:08am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/3 "2021-04-08T00:08:00Z")

</div>

Thanks for confirmation 🙂

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [April 8, 2021, 12:52am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/4 "2021-04-08T00:52:21Z")

</div>

Loops are painful but only in shell(s)  
A real case with Julia

```auto
julia> G = gmtread("MAOB/T2001_2019_SST_CLIMA.nc?z[0]");

julia> for k = 2:12 G += gmtread("MAOB/T2001_2019_SST_CLIMA.nc?z[@(k)]"); end

julia> G /= 12;
# Here some bug still be found-and-killed forced me to save grid in disk to visualize it

julia> gmtwrite("SST_avg.grd", G)

julia> imshow("SST_avg.grd", title="Average SST from 2001 to 2019", fmt=:png, colorbar=true)

```

 ![GMTjl_tmp](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/d/de1d10f9475fc0ea472e170f66f8e69e874f7f2e.png)

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [April 8, 2021, 1:08am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/5 "2021-04-08T01:08:23Z")

</div>

Don’t forget grdmath’s stack operators; see **grdmath -S** for details on who they are.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 8, 2021, 3:50am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/6 "2021-04-08T03:50:34Z")

</div>

So that would be something like

```auto
grdmath $file?var[a] $file?var[b] $file?var[c] ... -S MEAN = output.nc

```

can we pipe the list via `cat list.txt | ` ?

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [April 8, 2021, 4:06am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/7 "2021-04-08T04:06:47Z")

</div>

sure, but like this: gmt grdmath $(cat list-of-grids) -S MEAN = output.nc

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 8, 2021, 4:20am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/8 "2021-04-08T04:20:05Z")

</div>

So …

```auto
cat << EOF > list.txt
grid?var[idt1]
grid?var[idt2]
grid?var[idtN]
EOF

grdmath $(cat list.txt) -S MEAN = output.nc

```

As simple as that ?

---

<div class="post-metadata">

**Author:** ![pwessel](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/pwessel/32/30_2.png) [@pwessel](https://forum.generic-mapping-tools.org/u/pwessel)\
**Post date:** [April 8, 2021, 5:31am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/9 "2021-04-08T05:31:36Z")

</div>

Sure, should be, caveat on keeping all those Unix special characters as they are, but otherwise should work.

---

<div class="post-metadata">

**Author:** ![lucalelli](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/lucalelli/32/2485_2.png) [@lucalelli](https://forum.generic-mapping-tools.org/u/lucalelli)\
**Post date:** [April 14, 2021, 9:26am UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/10 "2021-04-14T09:26:00Z")

</div>

There’s a third solution I routinely use for this, which is CDO’s operator timmean.  
Given a stack of geographical regular latlon grids, one can

`cdo timmean INFILE OUTFILE`

GMT works pretty good in tandem with CDO

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [April 14, 2021, 4:32pm UTC](https://forum.generic-mapping-tools.org/t/composite-picture-netcdf/1545/11 "2021-04-14T16:32:31Z")

</div>

Hum, good to know, it can be useful in the case of continuous time-series.
