Reading and writing netCDF-format files
Matlab provides both low- and high-level scripts for reading and
writing netCDF files. One of the high-level scripts is "ncinfo" which
will
read the header of the file, and give you the variable
names,
dimensions, and attributes for the file. Alternatively you can download the
command line utility "ncdump" from
http://www.unidata.ucar.edu/downloads/netcdf/index.jsp
"ncdump" will dump the header, a single variable, or the entire
file. If you are new to reading netCDF or other self-describing
format files, the ability to find out the variable names, dimensions,
and attributes in a file before you try the provided script will make
your work easier and less stressful!
The Matlab provided high-level scripts are "nccreate", "ncdisp",
"ncinfo", "ncread", "ncreadatt", "ncwrite", "ncwriteatt", and "ncwriteschema".
Type "help command_name" to get information on how to use the script.
I have found the Matlab WWW documentation to be very good.
A second method for reading and writing netCDF files with Matlab is
called "Mexcdf," which stands for matlab extension for reading common
data format files. This library
predates the Matlab effort. You should download the "ncdump" utility
described above. Examples of how to read files using the low-level
mexcdf functions is linked here. High-level mexcdf scripts for
reading netCDF files are called "snctools", and they are included in
the mexcdf library. You should try them
too.
Converting 3-dimensional arrays to 2-dimensions
Matlab now handles arrays with greater than 2 dimensions, which is
useful for calculating means along latitude or
longitude circles or spatial differences. The great majority of
calculations, however, treat each grid point as an independent
timeseries, and it is simpler to manipulate the data as a
two-dimensional matrix, A(t,x). The following 2 lines will reduce an input
3-dimensional array to 2-dimensions.
Consider A = A(nt,ny,nx) where nt, ny, nx are the number of
time, latitude, and longitude points, respectively.
threetotwo.m -- the MATLAB script.
For some reason, datasets and time series are still written for partial
years of data. In a world of
gigabytes and terabytes, people are still worried about bytes and
kilobytes. The following script will append missing values ("NaN"s)
to time series or data arrays to make a complete year of data. The
script can handle monthly or other time resolution data (but not Leap
Days for now --- it would be easy to do).
fill_year.m -- the MATLAB script.
A = permute( A, [ 3 2 1 ] );
"A" will be dimensioned A(nt,nx*ny), and the second index will first
span the first latitude circle and then the second latitude circle,
and so forth.
A = squeeze( reshape( A, nx*ny,nt,1 ) )' ;
Filling partial years of data in arrays and time series
January 2010: I am at present figuring out how to get matlab and
netcdf-input/output to work on my Apple computer. The Regional Ocean
Modeling System (ROMS) WWW page seems very good so far. I have been
able to get "snctools", a set of scripts written on top of low order
input/output routines, to work. Yay!
March 2011
Todd Mitchell ( mitchell@atmos.washington.edu
)
JISAO data