NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

Re: [netcdfgroup] easy/quick way to get attribute value from shell?

Tom Roche May 18, 2012 2:25 PM
>>> Is there an easy and quick way to *get* the value of [any
>>> attribute, by name] (whether global or variable) from a (bash)
>>> shell commandline or script?
...
>>> unless I need to run R for other reasons, I'm writing bash like

>>>  if [[ "$(ncdump -h ${F} | fgrep -e ${OUTPUT_VAR_NAME}:units)" =~ 
>>> "${OUTPUT_VAR_UNITS}" ]] ; then
>>>    echo -e 'output!'
>>>  fi

Roy Mendelssohn Fri, 18 May 2012 18:23:39 -0700
>> [ncks]
>> '-m'
>> Print variable metadata to screen (similar to ncdump -h).
...
>> '-P'
>> Print data, metadata, and units to screen.

These, like `ncdump`, generate composite output, from which the
attribute value must be extracted. Hence `ncks` provides no advantage
over `ncdump`: is that not readily apparent?

Henry Butowsky recommends a somewhat cleaner approach for getting an
attribute value

me@it:/tmp$ ncap2 -v -O -s 'print(N2O@var_desc)' 
./emis_mole_onlyN2O_windowed.ncf
> N2O@var_desc, size = 80 NC_CHAR, value = Denitrification_N2     
me@it:/tmp$ FOO="$(ncap2 -v -O -s 'print(N2O@var_desc)' 
./emis_mole_onlyN2O_windowed.ncf)"
me@it:/tmp$ echo -e "FOO='${FOO##*= }'"
FOO='Denitrification_N2     '

but still some processing (and prior knowledge of the output format) is
required. Ideally there would be a modifier for NCO print statements
with the semantics 'just give me the value'; for that matter, the API
could be more readily understandable--is the intent of something
called `ncap2` readily apparent? Ideally we could combine the
clarities of R package=ncdf4 and the NCO print statement, e.g.

ncatt_get 'N2O@var_desc' /path/to/file
or, for global attributes
ncatt_get '@NVARS' /path/to/file

that could be used like

if [[ "$(ncatt_get 'N2O@units' /path/to/file)" == 'kg/ha' ]] ; then
  # convert kg to mol
  # convert ha to map-scaled gridcell area
fi

FWIW, Tom Roche <Tom_Roche@xxxxxxxxx>



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