


Write grid and connectivity to FVCOM format grid file
function write_FVCOM_grid(Mobj,filename)
DESCRIPTION:
Generate an ascii FVCOM 3.x format gridfile from Mesh object
INPUT
Mobj = Mesh object with fields:
- nativeCoords - string of the native coordinates (cartesian or
spherical).
- x, y, lon, lat - node coordinates for either cartesian or
spherical.
- nVerts - number of nodes.
- nElems - number of elements.
- tri - grid connectivity table.
- h - water depth at the nodes.
filename = FVCOM grid file name
OUTPUT:
FVCOM grid file: filename
EXAMPLE USAGE
write_FVCOM_grid(Mobj, 'tst_grd.dat')
Author(s):
Geoff Cowles (University of Massachusetts Dartmouth)
Rory O'Hara Murray (Marine Scotland Science)
Pierre Cazenave (Plymouth Marine Laboratory)
Revision history
2014-10-07 Removed loops to speed up writing the file
2016-06-14 Minor code tweaks to fix MATLAB warnings. Add check that we
successfully opened the output file.
==========================================================================

0001 function write_FVCOM_grid(Mobj,filename) 0002 % Write grid and connectivity to FVCOM format grid file 0003 % 0004 % function write_FVCOM_grid(Mobj,filename) 0005 % 0006 % DESCRIPTION: 0007 % Generate an ascii FVCOM 3.x format gridfile from Mesh object 0008 % 0009 % INPUT 0010 % Mobj = Mesh object with fields: 0011 % - nativeCoords - string of the native coordinates (cartesian or 0012 % spherical). 0013 % - x, y, lon, lat - node coordinates for either cartesian or 0014 % spherical. 0015 % - nVerts - number of nodes. 0016 % - nElems - number of elements. 0017 % - tri - grid connectivity table. 0018 % - h - water depth at the nodes. 0019 % filename = FVCOM grid file name 0020 % 0021 % OUTPUT: 0022 % FVCOM grid file: filename 0023 % 0024 % EXAMPLE USAGE 0025 % write_FVCOM_grid(Mobj, 'tst_grd.dat') 0026 % 0027 % Author(s): 0028 % Geoff Cowles (University of Massachusetts Dartmouth) 0029 % Rory O'Hara Murray (Marine Scotland Science) 0030 % Pierre Cazenave (Plymouth Marine Laboratory) 0031 % 0032 % Revision history 0033 % 2014-10-07 Removed loops to speed up writing the file 0034 % 2016-06-14 Minor code tweaks to fix MATLAB warnings. Add check that we 0035 % successfully opened the output file. 0036 %========================================================================== 0037 0038 [~, subname] = fileparts(mfilename('fullpath')); 0039 0040 global ftbverbose 0041 if ftbverbose 0042 fprintf('\nbegin : %s\n', subname); 0043 end 0044 0045 %-------------------------------------------------------------------------- 0046 % Parse input arguments 0047 %-------------------------------------------------------------------------- 0048 if ~exist('Mobj', 'var') || ~exist('filename', 'var') 0049 error('arguments to write_FVCOM_grid are incorrect') 0050 end 0051 0052 %-------------------------------------------------------------------------- 0053 % Dump the file 0054 %------------------------------------------------------------------------------ 0055 if strcmpi(Mobj.nativeCoords, 'cartesian') 0056 x = Mobj.x; 0057 y = Mobj.y; 0058 else 0059 x = Mobj.lon; 0060 y = Mobj.lat; 0061 end 0062 if ftbverbose 0063 fprintf('writing FVCOM gridfile %s\n', filename) 0064 end 0065 fid = fopen(filename,'w'); 0066 assert(fid > 0, 'Error opening output file %s', filename) 0067 fprintf(fid, 'Node Number = %d\n', Mobj.nVerts); 0068 fprintf(fid, 'Cell Number = %d\n', Mobj.nElems); 0069 fprintf(fid, '%d %d %d %d %d\n', [(1:Mobj.nElems)', Mobj.tri, (1:Mobj.nElems)']'); 0070 fprintf(fid, '%d %f %f %f\n', [(1:Mobj.nVerts)', x, y, Mobj.h]'); 0071 fclose(fid); 0072 0073 if ftbverbose 0074 fprintf('end : %s\n', subname) 0075 end 0076