Commit bce2b576 authored by Pierre Cazenave's avatar Pierre Cazenave

Add script to create the files needed by FVCOM when outputting a set of time...

Add script to create the files needed by FVCOM when outputting a set of time series at given locations
parent c374ec80
function [Mobj] = add_stations_list(Mobj,Positions,Names,Dist)
% Add a set of stations at which FVCOM will output time series.
%
% [Mobj] = add_stations_list(Mobj,Positions,Names,Dist)
%
% DESCRIPTION:
% Supply a list of positions (in the same coordinate system as the
% native coordinates of the grid) and a cell array of names. Nearest
% grid node to those supplied will be used in the output file.
%
% INPUT
% Mobj = Matlab mesh object
% Positions = Array of the positions of the stations
% Names = Cell array of the names of the stations defined in Positions
% Dist = Maximum distance away for a node to be included
%
% OUTPUT:
% Mobj = Matlab mesh object with an additional cell array containing id,
% x, y, nodelist, depth and station name.
%
% EXAMPLE USAGE
% Mobj = add_stations_list(Mobj, [-5.54, 50.103; -3.0865, 58.441], ...
% {'Newlyn', 'Wick'}, 0.25)
%
% Author(s):
% Pierre Cazenave (Plymouth Marine Laboratory)
%
%
% Revision history
% 2012-11-30 First version.
%
%==========================================================================
subname = 'add_stations_list';
global ftbverbose
if(ftbverbose)
fprintf('\n')
fprintf(['begin : ' subname '\n'])
end;
%--------------------------------------------------------------------------
% Check the inputs
%--------------------------------------------------------------------------
nPos = size(Positions, 1);
nNames = size(Names, 1);
if nPos ~= nNames
error('The number of the supplied station positions and names do not match (%i and %i respectively)', nPos, nNames)
end
%--------------------------------------------------------------------------
% For each site in the supplied positions, find the nearest node ID
%--------------------------------------------------------------------------
% Check for whether the input has both spherical and cartesian.
if size(Positions, 2) > 2
% Now check for which is the native coordinate system, and output the
% station positions in that coordinate system.
if strcmpi(Mobj.nativeCoords, 'cartesian')
cols = [3, 4];
elseif strcmpi(Mobj.nativeCoords, 'spherical')
cols = [1, 2];
else
error('Unknown native coordinate system string: %s', Mobj.nativeCoords)
end
else
% We have to assume the positions are in the grid's native coordinate
% system.
cols = [1, 2];
end
inc = 1;
% out = cell(nPos, 1);
for s=1:nPos
[node, dist] = find_nearest_pt(Positions(s, cols(1)), Positions(s, cols(2)), Mobj);
if dist >= Dist
% Skip out for this station
if(ftbverbose)
fprintf('Skipping station %s (%g, %g). Nodal distance from station position = %f\n', Names{s}, Positions(s, 1), Positions(s, 2), dist)
end
continue
end
out{inc} = {inc, Positions(s, cols(1)), Positions(s, cols(2)), node, Mobj.h(node), Names{s}};
inc = inc + 1;
end
Mobj.stations = out;
\ No newline at end of file
function [Mobj] = write_FVCOM_stations(Mobj,filename)
% Add a set of stations at which FVCOM will output time series.
%
% function add_stations_list(Mobj,filename)
%
% DESCRIPTION:
% Given a mesh object with time series positions and names
% (Mobj.Position and Mobj.Names from add_stations_list.m), write out to
% ASCII file filename.
%
% INPUT
% Mobj = Matlab mesh object
% filename = FVCOM stations file name
%
% OUTPUT:
% FVCOM stations file: filename
%
% EXAMPLE USAGE
% write_FVCOM_stations(Mobj, 'tst_stations.dat')
%
% Author(s):
% Pierre Cazenave (Plymouth Marine Laboratory)
%
% Revision history
% 2012-11-30 First version.
%
%==========================================================================
subname = 'write_FVCOM_stations';
global ftbverbose
if(ftbverbose)
fprintf('\n'); fprintf(['begin : ' subname '\n']);
end
%--------------------------------------------------------------------------
% Parse input arguments
%--------------------------------------------------------------------------
if exist('Mobj', 'var') ~= 1 || exist('filename', 'var') ~= 1
error('arguments to write_FVCOM_grid are incorrect')
end
%--------------------------------------------------------------------------
% Dump the file
%--------------------------------------------------------------------------
if strcmpi(Mobj.nativeCoords, 'cartesian')
x = Mobj.Positions(:,3);
y = Mobj.Positions(:,4);
elseif strcmpi(Mobj.nativeCoords, 'spherical')
x = Mobj.Positions(:,1);
y = Mobj.Positions(:,2);
else
error('Unknown native coordinate system string: %s', Mobj.nativeCoords)
end
if(ftbverbose)
fprintf('writing FVCOM gridfile %s\n',filename);
end
fid = fopen(filename,'w');
fprintf(fid, ' No X Y Node (Cell) Station Name\n');
for s=1:length(Mobj.stations)
fprintf(fid, '%i %f %f %i %f %s\n', cell2mat(Mobj.stations{s}(1:5)), char(Mobj.stations{s}(6)));
end
fclose(fid);
if(ftbverbose)
fprintf(['end : ' subname '\n'])
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