Home > atmlab > geoplots > gmt > save_nc_gmt_input.m

save_nc_gmt_input

PURPOSE ^

SYNOPSIS ^

function save_nc_gmt_input (filename, data, lat, lon)

DESCRIPTION ^

 SAVE_NC_GMT_INPUT 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 ^

save_nc_gmt_input.m

SOURCE CODE ^

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

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