Home > atmlab > geoplots > gmt > gmt_nc_save_gridded.m

gmt_nc_save_gridded

PURPOSE ^

% GMT_NC_SAVE_GRIDDED write a gridded netcdf file for GMT.

SYNOPSIS ^

function gmt_nc_save_gridded (x, y, z, cdf)

DESCRIPTION ^

% GMT_NC_SAVE_GRIDDED   write a gridded netcdf file for GMT.

 This function writes a netcdf file that can be used as input for
 GMT plotting routines.
 
 FORMAT  write_gmtgrid(x,y,z,cdf_file)

 IN    x    Longitude grid vector
       y    Latitude grid vector
       z    Data matrix

 Example:  x=[ 1 2 ];
           y=[ 1 2 3 ];
           z=[ 1 2; 3 4; 5 6];
           contour(x,y,z);
           write_gmtgrid(x,y,z,grdfile);

 x and y can be either row or column vectors

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

gmt_nc_save_gridded.m

SOURCE CODE ^

0001 function gmt_nc_save_gridded (x, y, z, cdf)
0002 %% GMT_NC_SAVE_GRIDDED   write a gridded netcdf file for GMT.
0003 %
0004 % This function writes a netcdf file that can be used as input for
0005 % GMT plotting routines.
0006 %
0007 % FORMAT  write_gmtgrid(x,y,z,cdf_file)
0008 %
0009 % IN    x    Longitude grid vector
0010 %       y    Latitude grid vector
0011 %       z    Data matrix
0012 %
0013 % Example:  x=[ 1 2 ];
0014 %           y=[ 1 2 3 ];
0015 %           z=[ 1 2; 3 4; 5 6];
0016 %           contour(x,y,z);
0017 %           write_gmtgrid(x,y,z,grdfile);
0018 %
0019 % x and y can be either row or column vectors
0020 
0021 % 2010-01-20 Created by Oliver Lemke.
0022 
0023 if exist('OCTAVE_VERSION','builtin')
0024   write_gmtgrid_octave( x, y, z, cdf);
0025   return;
0026 end;
0027 
0028 [ny,nx] = size(z);
0029 
0030 % Create the file
0031 ncid = netcdf.create ( cdf, 'NC_CLOBBER' );
0032 
0033 %
0034 % Define the dimensions.
0035 lon_dimid = netcdf.defDim ( ncid, 'x', nx );
0036 lat_dimid = netcdf.defDim ( ncid, 'y', ny );
0037 
0038 %
0039 % Define the coordinate variables. We will only define coordinate
0040 % variables for lat and lon.
0041 lon_varid = netcdf.defVar ( ncid, 'x', 'double', lon_dimid );
0042 lat_varid = netcdf.defVar ( ncid, 'y', 'double', lat_dimid );
0043 
0044 %
0045 % The dimids array is used to pass the dimids of the dimensions of
0046 % the netCDF variables.
0047 dimids = [ lon_dimid lat_dimid ];
0048 
0049 %
0050 % Define the netCDF data variable.
0051 mean_varid = netcdf.defVar (ncid, 'z', 'float', dimids);
0052 
0053 netcdf.putAtt ( ncid, lon_varid, 'long_name', 'x' );
0054 netcdf.putAtt ( ncid, lat_varid, 'long_name', 'y' );
0055 netcdf.putAtt ( ncid, mean_varid, 'long_name', 'z' );
0056 netcdf.putAtt ( ncid, netcdf.getConstant ('NC_GLOBAL'), 'node_offset', 1 );
0057 
0058 netcdf.endDef (ncid);
0059 
0060 netcdf.putVar ( ncid, lon_varid, x );
0061 netcdf.putVar ( ncid, lat_varid, y );
0062 netcdf.putVar ( ncid, mean_varid, single(z)' );
0063 
0064 netcdf.close (ncid);
0065 
0066 
0067 %% WRITE_GMTGRID_OCTAVE
0068 
0069 function write_gmtgrid_octave (x, y, z, cdf)
0070 
0071 [ny,nx] = size(z);
0072 
0073 nc = netcdf ( cdf, 'c' );
0074 
0075 %
0076 % Define the dimensions.
0077 nc('x') = nx;
0078 nc('y') = ny;
0079 
0080 %
0081 % Write the variable dimensions.
0082 nc{'x'} = ncdouble ( 'x' );
0083 nc{'x'}.long_name = 'x';
0084 
0085 nc{'y'} = ncdouble ( 'y' );
0086 nc{'y'}.long_name = 'y';
0087 
0088 nc{'z'} = ncdouble ( 'y', 'x' );
0089 nc{'z'}.long_name = 'z';
0090 
0091 %
0092 % Write the variables.
0093 nc{'x'}(:) = x;
0094 nc{'y'}(:) = y;
0095 nc{'z'}(:) = z;
0096 
0097 nc.node_offset = '1';
0098 
0099 close (nc);
0100

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005