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.

```
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 ) )' ;

threetotwo.m -- the MATLAB script.

Filling partial years of data in arrays and time series

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.

Saving data in ascii format

It is sometimes useful to save data in ascii format, and this is especially true for climate indices where you might want to look at values for selected periods without having to ingest data into software.

As of version R2012b, and possibly a little before, this is accomplished in Matlab with

`save filename.ascii -ascii variable_name(s)`

where "variable_name(s)" have to be in double precision. The error message for an incorrect variable type is cryptic.

September 2013 Todd Mitchell ( mitchell@atmos.washington.edu )

JISAO data