read_fvcom_mesh.m 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
function [Mobj] = read_fvcom_mesh(gridfile) 

% 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)
%
% Revision history
%   
%==============================================================================

subname = 'read_fvcom_mesh';
fprintf('\n')
fprintf(['begin : ' subname '\n'])


%------------------------------------------------------------------------------
% Create a blank mesh object
%------------------------------------------------------------------------------
Mobj = make_blank_mesh();
coordinate = 'cartesian';
have_bath = false;
have_xy = true;
have_lonlat = false;


%------------------------------------------------------------------------------
% Read the mesh from the fvcom grid file
%------------------------------------------------------------------------------


fid = fopen(gridfile,'r');
if(fid  < 0)
	error(['file: ' gridfile ' does not exist']);
end;

%----------------------------------------------------
% read in the fvcom connectivity and vertices 
%----------------------------------------------------
C = textscan(fid, '%s %s %s %d', 1); nVerts = C{4};
C = textscan(fid, '%s %s %s %d', 1); nElems = C{4};
tri = zeros(nElems,3); 
x   = zeros(nVerts,1);
y   = zeros(nVerts,1);
h   = zeros(nVerts,1);
lon = zeros(nVerts,1);
lat = zeros(nVerts,1);
ts  = zeros(nVerts,1);

fprintf('reading mesh file\n');
fprintf('# nodes %d\n',nVerts);
fprintf('# elems %d\n',nElems);
for i=1:nElems
  C = textscan(fid,' %d %d %d %d %d\n',1);
  tri(i,1) = C{2};  tri(i,2) = C{3}; tri(i,3) = C{4};
end;
for i=1:nVerts 
  C = textscan(fid, '%d %f %f %f', 1);
  x(i) = C{2};
  y(i) = C{3};
end;
fprintf('mesh read in\n');
fclose(fid);

%------------------------------------------------------------------------------
% Transfer to Mesh structure
%------------------------------------------------------------------------------

Mobj.nVerts  = nVerts;
Mobj.nElems  = nElems;
Mobj.nativeCoords = coordinate;

if(have_lonlat)
	Mobj.have_lonlat  = have_lonlat;
end;
if(have_xy)
	Mobj.have_xy      = have_xy;
end;
if(have_bath)
	Mobj.have_bath    = have_bath;
end;
Mobj.x            = x;
Mobj.y            = y;
Mobj.ts           = ts;
Mobj.lon          = lon;
Mobj.lat          = lat;
Mobj.h            = h;
Mobj.tri          = tri;


fprintf(['end   : ' subname '\n'])