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

Re: NetCDF and Matlab



> Organization: .
> Keywords: 199411100201.AA05786

Hi Geoff,

> We'd like to be able to read (and then manipulate and plot) NetCDF
> files using Matlab. Hence the following two questions:
> 
> (i) Do you know if anyone has already done this? Essentially this would
> involve wrting a Matlab M-file which read the data. If so, could you 
> point us in their direction. 

Yes, I've appended a description of a MATLAB interface for netCDF developed
and used by the USGS in Woods Hole.  You can also view this with a WWW
browser such as Mosaic, Netscape, or Lynx, by looking at our Web document on
software for manipulating or displaying netCDF data:

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

> (ii) If not, we will have to do it ourselves. One way might just be 
> to call the NetCDF C or FORTRAN routines from Matlab. But this seems
> a little clumsy, as Matlab should be able to do this itself. 
> But to do this we would need precise information about the file itself,
> the header etc and the way the data is stored (byte ordering etc.) We
> can't find this in the NetCDF manual. Perhaps we are looking in the
> wrong place. Could you tell us where we can find this information, 
> please?

As for the existence of a document describing the exact file structure,
here's an excerpt from a recent answer to another user about this:

    We don't have such a document for several reasons.  First, there is a
    chapter in the netCDF User's Guide on "NetCDF File Structure and
    Performance" that explains the physical structure of netCDF data at a
    high enough level to make clear the performance implications of
    different data organizations.

    Second, we don't want netCDF users to write programs that depend on the
    physical representation of netCDF data.  If they did that, we would not
    be free in the future to change the physical representation.  If users
    only go through the documented interfaces to access the data, any
    changes we make in the future physical representation will be
    transparent to current users.

    Finally, the file structure is completely specified by the source code,
    and by the description that it is the XDR-encoding of the NC structures
    defined in netcdf/libsrc/local_nc.h.  Since XDR is specified elsewhere
    in a separate document, we didn't want to copy that specification but
    instead just refer to it.  That specification is available via a WWW
    browser such as Mosaic or via gopher at

            gopher://ds.internic.net/00/rfc/rfc1014.txt

Anyway, I hope this helps explain why I can't point at a single specific
document.


--
Russ Rew                                                UCAR Unidata Program
address@hidden                                          P.O. Box 3000
http://www.unidata.ucar.edu/                          Boulder, CO 80307-3000


MATLAB Interface

An interface between Matlab and NetCDF called MEXCDF has been developed by
Chuck Denham at the US Geological Survey in Woods Hole.

MATLAB is a software package that integrates numerical analysis, matrix
computation, signal processing and graphical display.

MEXCDF is a complete interface between netCDF and MATLAB that uses the
MATLAB mexfile utility to allow MATLAB users to read, write, and manipulate
netCDF data files in an efficient manner, following the C language interface
syntax found in the netCDF 2.3 users manual. In addition, the interface has
been enhanced in several ways:

     Dimensions and variables accessible by number or name. 
     Attributes accessible by number or name. 
     Parameters accessible by number or name. 
     Prepended "nc" not necessary for operation names. 
     Prepended "NC_" not necessary for specifying parameters. 
     Parameter names not case-sensitive. 
     Required lengths default to actual lengths via -1. 
     AutoScaling via "scale_factor" and "add_offset" attributes. 

As an example, to read the following 2D array 'elev' in file 'foo.cdf', 

              short elev(lat, lon) 
                 elev:scale_factor=100.
                 elev:add_offset=0.,

the required MATLAB commands using mexcdf are simply: 

    mexcdf('open','foo.cdf','nowrite');                    
    elev=mexcdf('varget',cdfid,'elev',[0 0],[-1 -1]);       
    mexcdf('close');                                         

The edges values "-1" means get all the values in this dimension, and
scale_factor and add_offset are handled automagically by varget.

For more information regarding this software, get the file
/pub/mexcdf/README via anonymous ftp from crusty.er.usgs.gov (128.128.19.19)
or contact Rich Signell at address@hidden.