[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 20030125: netCDF: question about global attributes?



>To: russ
>From: Naveenta Anand <address@hidden>
>Subject: netCDF: question about global attributes?
>Organization: Fisheries and Oceans Canada
>Keywords: 200301242005.h0OK55x03331 netCDF global attributes

Naveenta,

> Thanks for your response. I think you almost answered my question. But let me 
> confirm that. My question was that "Can global attributes be read (say string 
> of text) through an external file by some command like FILE READ instead of 
> writing fixed length record in the code by the user?" In other words, Can I 
> read Global attributes at run time?

I'm still having trouble understanding the question.  The way I'm
parsing it is something like "can you read global attributes at run
time, instead of writing them?", to which I would answer yes, but what
do you mean by "instead of", since you can do both?  Maybe I'm just
being dense ...

If you want to read a global attribute named "history" out of a netCDF
file named "data.nc" without writing a program, on a Unix system you
could just use the "ncdump" command and the "grep" command to get the
value, using something like:

  ncdump -h "data.nc" | grep ":history"

or put a tab before the ":" in case there might be variable attributes
with the same name, and the output would be something like:

                :history = "created 2003-01-28 by R. Rew"

If this global attribute continued over multiple lines (each ending
with an escaped newline ("\n"), printing it with a single command
would not be so easy, so, for example, if you have a global attribute
named "description", the CDL might be:

                :description = "DMSP SSM/I Geophysical\n",
    "Retrievals of 19.5m Surface Wind Speed,\n",
    "Water Vapor, and Columnar Liquid Water of Clouds.\n",
    "These data sets have been spatially filled." ;

then the Unix command

  ncdump -h hist.nc | sed -n '/:description/,/" ;/p'

would output

                :description = "DMSP SSM/I Geophysical\n",
    "Retrievals of 19.5m Surface Wind Speed,\n",
    "Water Vapor, and Columnar Liquid Water of Clouds.\n",
    "These data sets have been spatially filled." ;

If you need to get the whole multiline attribute into a single shell
variable, you could probably do it in a perl script or with a fancier
awk command, but I'm not sure this is what you are asking.
Specifically, I'm not sure if the "FILE READ" command you are using
for an example is meant to be a shell command, or a command for some
interpreter that is in a context not provided, such as an IDL command
or a MATLAB command.

If you're asking how you read the value of a global attribute into a
character string in a program (in C, C++, Fortran, Fortan90, Java?),
then you'll have to be more specific.  In C, for example, you read it
at run time just like you would a variable attribute, using

    char buf[200]; // or use malloc to get enough space ...
    nc_get_att_text(ncid, NC_GLOBAL, "history", buf);

to read its value into the buf character string.

I hope this helps ...

--Russ

_____________________________________________________________________

Russ Rew                                         UCAR Unidata Program
address@hidden                     http://www.unidata.ucar.edu