Commit eddfbae6 authored by Pierre Cazenave's avatar Pierre Cazenave

Minor code tweaks to fix MATLAB warnings. Add check that we successfully...

Minor code tweaks to fix MATLAB warnings. Add check that we successfully opened the output file. Also update the help to be more useful.
parent ad6b98d2
Pipeline #397 skipped
function write_FVCOM_grid(Mobj,filename)
function write_FVCOM_grid(Mobj,filename)
% Write grid and connectivity to FVCOM format grid file
%
% function write_FVCOM_grid(Mobj,filename)
......@@ -7,61 +6,71 @@ function write_FVCOM_grid(Mobj,filename)
% DESCRIPTION:
% Generate an ascii FVCOM 3.x format gridfile from Mesh object
%
% INPUT
% Mobj = 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')
% write_FVCOM_grid(Mobj, 'tst_grd.dat')
%
% Author(s):
% 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
%==============================================================================
subname = 'write_FVCOM_grid';
% 2016-06-14 Minor code tweaks to fix MATLAB warnings. Add check that we
% successfully opened the output file.
%==========================================================================
[~, subname] = fileparts(mfilename('fullpath'));
global ftbverbose
if(ftbverbose);
fprintf('\n'); fprintf(['begin : ' subname '\n']);
end;
if ftbverbose
fprintf('\nbegin : %s\n', subname);
end
%------------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Parse input arguments
%------------------------------------------------------------------------------
if(exist('Mobj')*exist('filename')==0)
error('arguments to write_FVCOM_grid are incorrect')
end;
%--------------------------------------------------------------------------
if ~exist('Mobj', 'var') || ~exist('filename', 'var')
error('arguments to write_FVCOM_grid are incorrect')
end
%------------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Dump the file
%------------------------------------------------------------------------------
if(lower(Mobj.nativeCoords(1:3)) == 'car')
x = Mobj.x;
y = Mobj.y;
if strcmpi(Mobj.nativeCoords, 'cartesian')
x = Mobj.x;
y = Mobj.y;
else
x = Mobj.lon;
y = Mobj.lat;
end;
if(ftbverbose); fprintf('writing FVCOM gridfile %s\n',filename); end;
x = Mobj.lon;
y = Mobj.lat;
end
if ftbverbose
fprintf('writing FVCOM gridfile %s\n', filename)
end
fid = fopen(filename,'w');
fprintf(fid,'Node Number = %d\n',Mobj.nVerts);
fprintf(fid,'Cell Number = %d\n',Mobj.nElems);
% for i=1:Mobj.nElems
% fprintf(fid,'%d %d %d %d %d\n',i,Mobj.tri(i,1:3),i);
% end;
% for i=1:Mobj.nVerts
% fprintf(fid,'%d %f %f %f\n',i,x(i),y(i),Mobj.h(i));
% end;
fprintf(fid, '%d %d %d %d %d\n', [[1:Mobj.nElems]' Mobj.tri [1:Mobj.nElems]']');
fprintf(fid, '%d %f %f %f\n', [[1:Mobj.nVerts]' x y Mobj.h]');
assert(fid > 0, 'Error opening output file %s', filename)
fprintf(fid, 'Node Number = %d\n', Mobj.nVerts);
fprintf(fid, 'Cell Number = %d\n', Mobj.nElems);
fprintf(fid, '%d %d %d %d %d\n', [(1:Mobj.nElems)', Mobj.tri, (1:Mobj.nElems)']');
fprintf(fid, '%d %f %f %f\n', [(1:Mobj.nVerts)', x, y, Mobj.h]');
fclose(fid);
if(ftbverbose);
fprintf(['end : ' subname '\n'])
end;
if ftbverbose
fprintf('end : %s\n', subname)
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment