


Read fvcom mesh file into Matlab mesh object
[Mobj] = function read_fvcom_mesh(gridfile)
DESCRIPTION:
Read FVCOM Grid file (connectivity + nodes)
Store in a matlab mesh object
INPUT [keyword pairs]:
'gridfile' = fvcom mesh file
OUTPUT:
Mobj = matlab structure containing mesh data
EXAMPLE USAGE
Mobj = read_fvcom_mesh('tst_grd.dat')
Author(s):
Geoff Cowles (University of Massachusetts Dartmouth)
Pierre Cazenave (Plymouth Marine Laboratory)
Rory O'Hara Murray (Marien Scotland Science)
Revision history
2014-04-23 Minor formatting fixes and add the global ftbverbose flag.
2014-08-19 Remove loops to speed up reading in the file.
==============================================================================

0001 function [Mobj] = read_fvcom_mesh(gridfile) 0002 0003 % Read fvcom mesh file into Matlab mesh object 0004 % 0005 % [Mobj] = function read_fvcom_mesh(gridfile) 0006 % 0007 % DESCRIPTION: 0008 % Read FVCOM Grid file (connectivity + nodes) 0009 % Store in a matlab mesh object 0010 % 0011 % INPUT [keyword pairs]: 0012 % 'gridfile' = fvcom mesh file 0013 % 0014 % OUTPUT: 0015 % Mobj = matlab structure containing mesh data 0016 % 0017 % EXAMPLE USAGE 0018 % Mobj = read_fvcom_mesh('tst_grd.dat') 0019 % 0020 % Author(s): 0021 % Geoff Cowles (University of Massachusetts Dartmouth) 0022 % Pierre Cazenave (Plymouth Marine Laboratory) 0023 % Rory O'Hara Murray (Marien Scotland Science) 0024 % 0025 % Revision history 0026 % 2014-04-23 Minor formatting fixes and add the global ftbverbose flag. 0027 % 2014-08-19 Remove loops to speed up reading in the file. 0028 %============================================================================== 0029 0030 subname = 'read_fvcom_mesh'; 0031 global ftbverbose; 0032 if ftbverbose 0033 fprintf('\nbegin : %s \n', subname) 0034 end 0035 0036 0037 %------------------------------------------------------------------------------ 0038 % Create a blank mesh object 0039 %------------------------------------------------------------------------------ 0040 Mobj = make_blank_mesh(); 0041 coordinate = 'cartesian'; 0042 have_bath = false; 0043 have_xy = true; 0044 have_lonlat = false; 0045 0046 0047 %------------------------------------------------------------------------------ 0048 % Read the mesh from the fvcom grid file 0049 %------------------------------------------------------------------------------ 0050 0051 0052 fid = fopen(gridfile,'r'); 0053 if(fid < 0) 0054 error(['file: ' gridfile ' does not exist']); 0055 end; 0056 0057 %---------------------------------------------------- 0058 % read in the fvcom connectivity and vertices 0059 %---------------------------------------------------- 0060 C = textscan(fid, '%s %s %s %d', 1); nVerts = C{4}; 0061 C = textscan(fid, '%s %s %s %d', 1); nElems = C{4}; 0062 tri = zeros(nElems,3); 0063 x = zeros(nVerts,1); 0064 y = zeros(nVerts,1); 0065 h = zeros(nVerts,1); 0066 lon = zeros(nVerts,1); 0067 lat = zeros(nVerts,1); 0068 ts = zeros(nVerts,1); 0069 0070 if ftbverbose 0071 fprintf('reading mesh file\n'); 0072 fprintf('# nodes %d\n',nVerts); 0073 fprintf('# elems %d\n',nElems); 0074 end 0075 C = textscan(fid,' %d %d %d %d %d',nElems); 0076 tri(:,1) = C{2}; tri(:,2) = C{3}; tri(:,3) = C{4}; 0077 0078 C = textscan(fid, '%d %f %f %f', nVerts); 0079 x(:) = C{2}; 0080 y(:) = C{3}; 0081 0082 if ftbverbose; fprintf('mesh read in\n'); end 0083 fclose(fid); 0084 0085 %------------------------------------------------------------------------------ 0086 % Transfer to Mesh structure 0087 %------------------------------------------------------------------------------ 0088 0089 Mobj.nVerts = nVerts; 0090 Mobj.nElems = nElems; 0091 Mobj.nativeCoords = coordinate; 0092 0093 if(have_lonlat) 0094 Mobj.have_lonlat = have_lonlat; 0095 end; 0096 if(have_xy) 0097 Mobj.have_xy = have_xy; 0098 end; 0099 if(have_bath) 0100 Mobj.have_bath = have_bath; 0101 end; 0102 Mobj.x = x; 0103 Mobj.y = y; 0104 Mobj.ts = ts; 0105 Mobj.lon = lon; 0106 Mobj.lat = lat; 0107 Mobj.h = h; 0108 Mobj.tri = tri; 0109 0110 0111 if ftbverbose 0112 fprintf('end : %s \n', subname) 0113 end 0114 0115