3.0 Using Matlab to read netCDF files

There are two methods for reading netCDF files within Matlab. The first method described below is perhaps the simplest and most powerful, but is available only for Matlab 5 and 6. The second method, also described here, is available for those who have access only to Matlab 4.

3.1 Using the netcdf command

The netcdf command is a toolbox that needs to be installed with your matlab toolboxes and was developed by Chuck Denham. It makes use of structures and objects, so it is important that the user have some familiarity with these facilities in matlab.

Click here to see a simple example of the netcdf command.

The above example shows just how easy it is to read an arbitrary netcdf file into Matlab. It took just one line of code to specify the information needed to read the entire file into matlab. The other lines of code are there to illustrate how one might examine the file, how to load specific variables (such as latitude, longitude, and sea surface temperature), and how to create a preliminary quick look plot of the data.

Because the WOCE Global Data V3 have a common structure there are always 'time', 'depth', 'latitude', and 'longitude' variables. All of the data fields follow the COARDS naming convention. In principle the user doesn't need to know anything about the structure and size of the data (i.e. format and dimensions). This makes programming very easy.

To do more sophisticated manipulations of the netCDF files with the netcdf command the reader should read the help file from the matlab command line.

>> netcdf()

for help, and

>> netcdf('version')

to get the version number.

The netcdf command also allows the writing of output files, which means that intermediate analysis files can also be in the netCDF format for further analysis.

3.2 Sample commands of CSIRO Matlab/netCDF interface

Commonwealth Scientific Industrial Research Organisation (CSIRO) distributes a Matlab interface for reading netCDF files (www.marine.csiro.au/sw/matlab-netcdf.html). This is a powerful tool for those who are interested only in reading netCDF files. Those who want to create netCDF files from within Matlab should use Chuck Denham's interface mentioned above.

Of the 6 main commands, the two most important are getnc (getcdf) and inqnc (inqcdf). Detailed instructions on their use can be found at the website referenced above; only a bare bones description is given here. The use of inqnc allows interactive interrogation of the netCDF file. Here we present an example in which inqnc examines a current meter record named rcm01037.nc:

>> inqnc('rcm01037')

--- Global attributes ---

WOCE_Version: 3.0
conventions: COARDS/WOCE
EXPOCODE: 32MW9305, 32MW9508
experiment_name: Deep Currents (PCM6)
mooring_name: WHOI 947
pi_name: B.Owens/B.Warren
instrument_type: VACM
latitude: 40.6000
longitude: 147.3492
instrument_depth: 1999.0 m
seafloor_depth: 5280 m
sampling_interval: 30 min
earliest_start_time: 12-jun-1993 12:15:00
latest_stop_time: 01-jul-1995 12:45:00
null_value: -999.9
The 4 dimensions are 1) time = 35954   2) depth = 1
3) latitude = 1   4) longitude = 1

----- Get further information about the following variables -----

-1) None of them (no further information)
 0) All of the variables
 1) time   2) depth   3) latitude
 4) longitude   5) woce_date   6) woce_time
 7) speed   8) direction   9) u
10) v   11) temperature   12) pressure


The command has listed the global attributes of the current record. The global attributes consist of general information about the data, and in this example include the principal investigator, the instrument name, experiment name, etc. In addition inqnc gives a list of the variables that are contained within the file. In this case there are 12 different variables (time, speed, direction, etc.) that are organised as vectors with each element corresponding to a particular time (i.e., these are time series). Because inqnc is interactive it is possible to determine the attributes of each of the variables. To retrieve one of the variables within the above data set, such as speed, the getnc command is used:

>> speed=getnc('rcm01037','speed');

This is the simplest use of the getnc command. Because only two arguments are provided getnc assumes that the data are to be automatically scaled, that flagged data are to be changed to NaN (not a number), and that the entire speed series is required. The Matlab script in Appendix 2 shows how all of the Matlab data in the above test file can be read with just a few lines of code. This is considerably less than in the Fortran example shown in Appendix 1. However the development time for the Fortran and Matlab examples is similar and quick.

The getnc command also has arguments that allow changing missing values, changing the stride length (i.e. allow sub-sampling of the variable), etc. In this way the user can sub-sample, scale the data and read only the variables that are required from the netCDF file. In very large files this represents an important advantage to the user. A nice example of this capability is shown on CSIRO website. The website also shows an example of the Matlab command

>> help inqnc

in which all the arguments are listed.

All of the netCDF files that appear in the WOCE Global Data Set can be read by scripts based on just these two Matlab commands. By using the simplest form of getnc you will be able to read most of the netCDF files from a particular WOCE Data Assembly Centre with a single script. Thus it is easy and quick to create macros that can read the different netCDF files from a WOCE Data Assembly Centre, leading to significant savings in time and effort. Other important commands are whatnc (gives a list of netCDF files in local directory) and attnc (gets the attributes of particular variables, such as scale factor).

If necessary the scale factor applied to data can be obtained from the netCDF file by using the attnc comand. For example,

>> Latitude=getnc(file,'Latitude')*attnc(file,'Latitude','Scale');

would force the Latitude to be correctly scaled by the scale factor from within the netcCDF file.

Return to the top of the primer.