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

Re: 20010628: NetCDF help



>To: address@hidden
>From: "Vincent Cerimele" <address@hidden>
>Subject: help
>Organization: UCAR/Unidata
>Keywords: 200106281745.f5SHjm107997

Hi Vincent,

> I am trying to take an ascii file that has 30 varriables and convert it to 
> netCDF, but I really have no idea how to do it.  Can you help?  Thanks.

There are several ways to do this, and the best approach depends on
whether you want a general solution that will work for lots of other
similar files or you just need to do it once for a particular file.

For a general solution:

 - Pick a programming language you can use to read in the values from
   the ASCII file and that also has a netCDF interface.  Possibilities
   include C, C++, Fortran77, Fortran90, Java, Perl, Python, MATLAB,
   or IDL.

 - Create a new netCDF file with the appropriate netCDF function call.

 - Define netCDF dimensions used to specify the shapes of the
   variables, using the appropriate netCDF calls.

 - Create corresponding netCDF variables with the appropriate netCDF
   function calls.

 - Give the variables appropriate attributes (for example, units) with
   the appropriate netCDF calls.

 - Read the values of all the variables from the ASCII file and store
   them in variable arrays.

 - Write the values out to the netCDF variables using the appropriate
   netCDF calls.

 - Close the netCDF file.

For a more detailed explanation, see section 4.1 of the netCDF User's
Guide.

Many of these steps can be done for you by the "ncgen" utility, which
reads an ASCII representation of a netCDF file in a notation named
"CDL" and either generates the corresponding netCDF file or generates
C or Fortran code for a program that will create the corresponding
netCDF file.  So if you can get your ASCII data into CDL form somehow,
for example with a text editor or text tools like sed, it's easy to
use ncgen to convert the CDL file into a binary netCDF file.  For an
example and explanation of the ASCII CDL notation, see section 2.1 of
the netCDF User's Guide, at

  http://www.unidata.ucar.edu/packages/netcdf/guidec/guidec-7.html#HEADING7-0
or
  http://www.unidata.ucar.edu/packages/netcdf/guidef/guidef-7.html#HEADING7-0

for the C or Fortran77 versions, for example.

An alternative is to use "tbl2cdf", a contributed program that
converts a flat ASCII table to a netCDF file. The program scans a flat
ASCII table for the name and most general data type of each column. It
then creates and populates a netCDF file with the data.  The source
for tbl2cdf is available here:

  ftp://ftp.unidata.ucar.edu/pub/netcdf/contrib/tbl2cdf.c

Finally, you could use some other format X for which converters from
ASCII to X and for X to netCDF are available.  (If X is CDL, ncgen is
the converter from CDL to netCDF.)  There are several other
possibilities for X listed in the "Software for Manipulating or
Displaying NetCDF Data" available from 

  http://www.unidata.ucar.edu/packages/netcdf/software.html

--Russ

_____________________________________________________________________

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