JISAO data

Mexcdf interface for reading netCDF format files with MATLAB


MATLAB now provides low- and high-level software for reading netCDF files. Mexcdf predates this, and you should try using the MATLAB-supplied scripts before installing mexcdf.

"Mexcdf" software is available from SourceForge.Net which you can use to read and write netCDF-format files. The following are quick instructions to read files and two examples. With luck, you can actually be reading data in 15 minutes. Rene Garreaud provided the original script.

1) Let filename.nc be the netCDF file that you want to read. Type ncdump -h filename.nc in UNIX (not MATLAB) to see the header of the netCDF file. This will tell you the variable names and indicate whether or not the variables need to be unpacked when you read them into MATLAB. The outputs of this call will be the dimensions and variables.

  • Dimensions tells you the variable names and the dimensions of each variable. For example, lat (short for "latitude") could be a vector of latitude grid points, and there could be 180 of them for 2-degree latitude resolution global data.
  • Variables provides information on the variables. A variable will be introduced as
    variable_type variable_name, for example, float lat. The variable name is then lat. The scale_factor and add_offset for the variable, if they are defined, are used to unpack the data:
    unpacked_data = ( packed_data * scale_factor ) + add_offset;

    2) Type nc = netcdf('filename.nc'); in MATLAB to open the netCDF file.

    3) The basic command you need in MATLAB is variable=nc{'variable_name'}(:,:,:,:);
    where variable_name is the variable that you want to read in. The (:,:,:,:) at the end specifies the dimensions (for example, time,vertical level,lat,lon) that you want to read in. (:,:,:,:) will read in all of a 4-dimensional data volume. The order of the arguments is given in the ncdump output. For NCEP-NCAR reanalysis data provided by NOAA ESRL, the vertical level ranges from 1,2,3, ... (don't type in 500 hoping to get 500mb data).

    Often the data has been scaled to make for a physically smaller file. The best example is sea-level pressure (SLP), where a typical value of 1025.35mb will be written as 2525 in the file. The scaling information will be included as "add_offset" and "scale_factor" in the attributes for a variable. The add_offset, scale_factor, and _FillValue (used to be called missingvalue) can be obtained with
    nc{ 'variable_name' }.scale_factor(:)
    nc{ 'variable_name' }.add_offset(:)
    nc{ 'variable_name' }.FillValue_(:)
    (For some reason Matlab cannot parse text beginning with an underscore.)
    In some versions Matlab will rescale the data if you add a "1" to the above command: variable=nc{'variable_name',1}(:,:,:,:);

    4) Time is specified as so many units since some reference time. This information is obtained with
    nc{ 'time' }.units(:)
    nc{ 'time'}(:)

    Two examples:

    Example 1:

    ncdump -h sstoi8299.nc
    netcdf sstoi8299 {
    dimensions:
            lat = 180 ;
            lon = 360 ;
            time = UNLIMITED ; // (216 currently)
    variables:
            float lat(lat) ;
                    lat:title = "Latitude" ;
                    lat:units = "degrees_north" ;
                    lat:scale_factor = 1.f ;
                    lat:add_offset = 0.f ;
            float lon(lon) ;
                    lon:title = "Longitude" ;
                    lon:units = "degrees_east" ;
                    lon:scale_factor = 1.f ;
                    lon:add_offset = 0.f ;
            double time(time) ;
                    time:title = "Time" ;
                    time:units = "days    since 1982- 1- 1  0: 0: 0" ;
                    time:scale_factor = 1.f ;
                    time:add_offset = 0.f ;
            short data(time, lat, lon) ;
                    data:long_name = "deg.C" ;
                    data:add_offset = 20.f ;
                    data:scale_factor = 0.01f ;
                    data:missing_value = 32767s ;
                    data:units = "deg.C" ;
    
    nc = netcdf('sstoi8299.nc','nowrite');
    ygrid=nc{'lat'}(:);  
    xgrid=nc{'lon'}(:);  
    sst=nc{'data',1}(1,:,:);    
    
    Reads the latitude and longitude grid points, and the first month of SST data into MATLAB. The ,1 argument specifies that the SST data is unpacked using the add_offset and scale_factor information (see the ncdump output).

    Example 2:

    ncdump -h air.mon.mean.nc 
    netcdf air.mon.mean {
    dimensions:
            lon = 144 ;
            lat = 73 ;
            level = 17 ;
            time = UNLIMITED ; // (484 currently)
    variables:
     ...
            short air(time, level, lat, lon) ;
    
    
    That is, we have a global grid of air temperature, with 17 levels and 484 time samples (monthly from 1958-1997). The variable air have four dimensions: time, vertical level, lat and lon.
    We want to read the global fields at the 3rd vertical level at time=21 Then, try:
     nc = netcdf('air.mon.mean.nc','nowrite');
     T=nc{'air'}(21,3,:,:);
     whos
      Name      Size         Bytes  Class
    
      T        73x144        84096  double array
      nc        4-D           1690  netcdf object
    
    The result is in the matlab variable T
    

    March 2011
    Todd Mitchell ( mitchell@atmos.washington.edu )
    JISAO data