Home > atmlab > geoplots > gmt > gmt_nc_save_ungridded.m

gmt_nc_save_ungridded

PURPOSE ^

% GMT_NC_SAVE_UNGRIDDED Write ungridded data for GMT input.

SYNOPSIS ^

function gmt_nc_save_ungridded (filename, data, lat, lon)

DESCRIPTION ^

% GMT_NC_SAVE_UNGRIDDED Write ungridded data for GMT input.

 Write ungridded data for GMT input to a netcdf file. Used for nearneighbor and
 others as a replacement for ascii input.

 IN    filename Name of NetCDF file.
 IN    data          vector variable.
 IN    lat           vector variable.
 IN    lon           vector variable.

 2010-03-04   Created by Oliver Lemke

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

gmt_nc_save_ungridded.m

SOURCE CODE ^

0001 function gmt_nc_save_ungridded (filename, data, lat, lon)
0002 %% GMT_NC_SAVE_UNGRIDDED Write ungridded data for GMT input.
0003 %
0004 % Write ungridded data for GMT input to a netcdf file. Used for nearneighbor and
0005 % others as a replacement for ascii input.
0006 %
0007 % IN    filename Name of NetCDF file.
0008 % IN    data          vector variable.
0009 % IN    lat           vector variable.
0010 % IN    lon           vector variable.
0011 %
0012 % 2010-03-04   Created by Oliver Lemke
0013 
0014 if ~isequal(numel(data),numel(lon),numel(lat))
0015     error('gmtlab:input','This data is expected to be ungridded')
0016 end
0017 
0018 if islogical(data)
0019     data = int8(data);
0020 end
0021 
0022 if exist('OCTAVE_VERSION','builtin')
0023   save_nc_gmt_input_octave (filename, data, lat, lon);
0024   return;
0025 end;
0026 
0027 ncid = netcdf.create (strrep(filename,'~',getenv('HOME')), 'NC_CLOBBER');
0028 
0029 len = netcdf.defDim (ncid, 'numel', length(data));
0030 
0031 
0032 varid1 = netcdf.defVar (ncid, 'x', 'double', len);
0033 varid2 = netcdf.defVar (ncid, 'y', 'double', len);
0034 varid3 = netcdf.defVar (ncid, 'z', gmt_get_nctype3(data), len);
0035 
0036 netcdf.endDef (ncid);
0037 
0038 netcdf.putVar (ncid, varid1, lon);
0039 netcdf.putVar (ncid, varid2, lat);
0040 netcdf.putVar (ncid, varid3, data);
0041 
0042 netcdf.close (ncid);
0043 
0044 function save_nc_gmt_input_octave (filename, data, lat, lon)
0045 %% save_nc_gmt_input_octave
0046 % Octave workaround to save_nc_gmt_input.
0047 %
0048 % Octave uses other netcdflibraries
0049 %
0050 
0051 nc = netcdf (strrep(filename,'~',getenv('HOME')), 'c');
0052 
0053 nc('numel') = length(data);
0054 
0055 nc{'x'} = ncdouble ('numel');
0056 nc{'y'} = ncdouble ('numel');
0057 if strcmpi(gmt_get_nctype(data),{'SHORT','INT','UBYTE','USHORT','UINT','INT64','UINT64'})
0058     nc{'z'} = ncint ('numel');
0059 elseif strcmpi(gmt_get_nctype(data),{'FLOAT'})
0060     nc{'z'} = ncfloat ('numel');
0061 elseif strcmpi(gmt_get_nctype(data),{'DOUBLE'})
0062     nc{'z'} = ncdouble ('numel');
0063 elseif strcmpi(gmt_get_nctype(data),{'BYTE'})
0064     nc{'z'} = ncbyte ('numel');
0065 end
0066 nc{'x'}(:) = lon;
0067 nc{'y'}(:) = lat;
0068 nc{'z'}(:) = data;
0069 
0070 close (nc);

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