JISAO data

Select MATLAB scripts


  • Reading and writing netCDF-format files
  • Converting 3-dimensional arrays to 2-dimensions
  • Filling partial years of data in arrays and time series
  • Saving data in ascii format
  • Strings and structures
    
    

    
    
    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" in the NetCDF library available here. The utility "ncdump" will print 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 a script or utility is invaluable.

    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.

    Example: The following MATLAB commands were used to write a netCDF file for a 4-dimensional variable with MATLAB version 2012b. These lines should give you a good idea of what MATLAB wants.

    I start with an existing netCDF-format file, and copy the header information to a new file. In the absence of a netCDF file to copy the header from, writing the header information (variable and attribute names and dimensions) is accomplished with nccreate and ncwriteatt I believe.

    fnin.nc  is the input filename
    fnout.nc is the output filename
    
    finfo = ncinfo( fnin.nc );
    ncwriteschema( fnout.nc, finfo);
    "ncwriteshema" writes the header information, but no data to fnout.nc.
    
    My four-dimensional variable is fdat where fdat = fdat(time,level,lat,lon) from ncdump of fnin.nc.  
    
    The following lines write the longitude, latitude, vertical dimension,
    and time
    ncwrite( fnout.nc, 'lon', longitudes_variable )
    ncwrite( fnout.nc, 'lat', latitudes_variable )
    ncwrite( fnout.nc, 'level', vertical_variable )
    ncwrite( fnout.nc, 'time', time_variable )
    A source of error here is that the variable type: double, integer, 
    short, ... has to match what the fnout.nc file header expects.
    
    nx = number of longitude points
    ny = number of latitude points
    nz = number of vertical points
    nt = number of time points
    
    fdat = ncread( fnin.nc, variablename, [ 1 1 1 icnt ], [ nx ny nz 1 ] );
    reads a data volume for the icnt'th time
    Note that inside the MATLAB session, fdat is dimensioned fdat(lon,lat,level,time).
    
    ncwrite( fnout.nc, variablename, fdat );
    writes all of fdat 
    
    ncwrite( fnout.nc, variablename, fdat(icnt,:,:,:), [ 1 1 1 icnt ] );
    writes the icnt'th data volume of fdat to fnout.nc
    Inside the MATLAB session, fdat is dimensioned fdat(lon,lat,level,time).
    
    
    
    
    
    
    
    
    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 = squeeze( reshape( A, nx*ny,nt,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.

    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