Re: [netcdfgroup] simple commandline query of data-variable statistics to stdout?

Tom,

GMT prefers 2D, but can work with higher dimensional data.  This is taken from 
the description for gridinfo doc.

When reading a netCDF file that contains multiple grids, GMT will read, by 
default, the first 2-dimensional grid that can find in that file. To coax GMT 
into reading another multi-dimensional variable in the grid file, append 
?varname to the file name, where varname is the name of the variable. Note that 
you may need to escape the special meaning of ? in your shell program by 
putting a backslash in front of it, or by placing the filename and suffix 
between quotes or double quotes. See grdreformat(1) and Section 4.18 of the GMT 
Technical Reference and Cookbook for more information, particularly on how to 
read splices of 3-, 4-, or 5-dimensional grids.

And this from the grdreformat doc

Multi-dimensional grids
To extract one layer or level from a 3-dimensional grid stored in a 
COARDS-compliant netCDF file, append both the name of the variable and the 
index associated with the layer (starting at zero) in the form: 
ingrdfile?varname[layer]. Alternatively, specify the value associated with that 
layer using parentheses in stead of brackets: ingridfile?varname(level).
In a similar way layers can be extracted from 4- or even 5-dimensional grids. 
For example, if a grid has the dimensions (parameter, time, depth, latitude, 
longitude), a map can be selected by using: 
ingridfile?varname(parameter,time,depth).
Since question marks, brackets and parentheses have special meanings on the 
command line, escape these meanings by placing the full filename and suffix 
between quotes.

So this should pull stats from the first time for your file.
> grdinfo -L ./GEIA_N2O_oceanic.nc\?emi_n2o[0]

Daniel Kokron
NASA Ames (ARC-TN)
SciCon group
301-286-3959

________________________________________
From: netcdfgroup-bounces@xxxxxxxxxxxxxxxx 
[netcdfgroup-bounces@xxxxxxxxxxxxxxxx] On Behalf Of Tom Roche 
[Tom_Roche@xxxxxxxxx]
Sent: Sunday, September 09, 2012 8:46 PM
To: netcdfgroup@xxxxxxxxxxxxxxxx
Subject: Re: [netcdfgroup] simple commandline query of data-variable statistics 
to stdout?

Kokron, Daniel S. Thu, 6 Sep 2012 20:15:31 -0500
>> Have a look at the 'grdinfo' utility which is part of GMT5.

>> http://www.soest.hawaii.edu/gmt5/

GMT looks interesting (and is available as a debian package) but
`grdinfo` seems to only work with 2d data (Am I missing something?)
E.g., for

$ ncdump -h ./GEIA_N2O_oceanic.nc
> netcdf GEIA_N2O_oceanic {
> dimensions:
>       lon = 360 ;
>       lat = 180 ;
>       time = UNLIMITED ; // (1 currently)
> variables:
...
>       double emi_n2o(time, lat, lon) ;
>              emi_n2o:units = "ton N2O-N/yr" ;
>              emi_n2o:missing_value = -999. ;
>              emi_n2o:long_name = "N2O emissions" ;
>              emi_n2o:_FillValue = -999.f ;
...
$ GMT grdinfo ./GEIA_N2O_oceanic.nc
> grdinfo: No 2-D variable in file [./GEIA_N2O_oceanic.nc]

Alas.

Charlie Zender Fri, 07 Sep 2012 10:16:19 -0700
> Your query matches a category of commands which I call filters[,
> e.g.] http://nco.sf.net/nco.html#filters [. Since] ncap2 [is]
> better for arbitrarily complex and longer filters

... I went with that, even though the bigot in me rails against the
requirement to write a file as a side effect. (Fortunately he was
appeased by use of tempfiles :-) `source`able bash follows my .sig.

your assistance is appreciated, Tom Roche <Tom_Roche@xxxxxxxxx>
---------------------code follows to end of post---------------------

# bash functions for NCO "filters", as suggested by Charlie Zender @
# 
http://www.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/2012/msg00286.html
# http://nco.sourceforge.net/nco.html#filters
# All use `ncap2` and take input like $1=datavar name, $2=filename

# Prints maximum
function ncmax {
  TMPFILE="$(mktemp)"
  ncap2 -O -C -v -s "foo=${1}.max();print(foo)" ${2} ${TMPFILE} | cut -f 3- -d 
' '
}

function ncmin {
  TMPFILE="$(mktemp)"
  ncap2 -O -C -v -s "foo=${1}.min();print(foo)" ${2} ${TMPFILE} | cut -f 3- -d 
' '
}

# Prints mean
function ncmean {
  TMPFILE="$(mktemp)"
  ncap2 -O -C -v -s "foo=${1}.avg();print(foo)" ${2} ${TMPFILE} | cut -f 3- -d 
' '
}

# median not currently available?

# Prints RMS (root-mean-square, normalized by N)
function ncRMS {
  TMPFILE="$(mktemp)"
  ncap2 -O -C -v -s "foo=${1}.rms();print(foo)" ${2} ${TMPFILE} | cut -f 3- -d 
' '
}

# Prints range (of values in datavar)
function ncrange {
  TMPFILE="$(mktemp)"
  ncap2 -O -C -v -s \
    "foo=${1}.min();bar=${1}.max();print(foo,\"%f\");print(\" to 
\");print(bar,\"%f\")"\
     ${2} ${TMPFILE}
}

# Prints number of elements in datavar (including NA)
function ncsize {
  TMPFILE="$(mktemp)"
  ncap2 -O -C -v -s "foo=${1}.size();print(foo)" ${2} ${TMPFILE} | cut -f 3- -d 
' '
}

# Prints total
function nctotal {
  TMPFILE="$(mktemp)"
  ncap2 -O -C -v -s "foo=${1}.total();print(foo)" ${2} ${TMPFILE} | cut -f 3- 
-d ' '
}

_______________________________________________
netcdfgroup mailing list
netcdfgroup@xxxxxxxxxxxxxxxx
For list information or to unsubscribe,  visit: 
http://www.unidata.ucar.edu/mailing_lists/

--
This message has been scanned for viruses and dangerous
content by GMAO's MailScanner, and is believed to be clean.



  • 2012 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: