Home > fvcom_prepro > read_admesh_mesh.m

read_admesh_mesh

PURPOSE ^

Read admesh mesh files (version 2.2) into MATLAB mesh object.

SYNOPSIS ^

function [Mobj] = read_admesh_mesh(varargin)

DESCRIPTION ^

 Read admesh mesh files (version 2.2) into MATLAB mesh object.

 Mobj = function read_fvcom_mesh(varargin)

 DESCRIPTION:
    Read admesh 14 file and bathymetry file. Store in a matlab mesh object.

 INPUT [keyword pairs]:
   'msh'                   = gmsh msh file [e.g. casename.msh]
   [optional] 'bath'       = gmsh bathymetry file [e.g. tst_dep.dat]
   [optional] 'coordinate' = coordinate system [spherical; cartesian (default)]
   [optional] 'addCoriolis' = calculate Coriolis param (f), requires [lon, lat]

 OUTPUT:
    Mobj = MATLAB structure containing mesh data

 EXAMPLE USAGE
    Mobj = read_admesh_mesh('msh', 'casename.14', 'coordinate', 'spherical')

 Author(s):
    Ricardo Torres (Plymouth Marine Laboratory) based on read_gmsh_mesh

 Revision history

   2016-06-22 First version.

==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Mobj] = read_admesh_mesh(varargin)
0002 % Read admesh mesh files (version 2.2) into MATLAB mesh object.
0003 %
0004 % Mobj = function read_fvcom_mesh(varargin)
0005 %
0006 % DESCRIPTION:
0007 %    Read admesh 14 file and bathymetry file. Store in a matlab mesh object.
0008 %
0009 % INPUT [keyword pairs]:
0010 %   'msh'                   = gmsh msh file [e.g. casename.msh]
0011 %   [optional] 'bath'       = gmsh bathymetry file [e.g. tst_dep.dat]
0012 %   [optional] 'coordinate' = coordinate system [spherical; cartesian (default)]
0013 %   [optional] 'addCoriolis' = calculate Coriolis param (f), requires [lon, lat]
0014 %
0015 % OUTPUT:
0016 %    Mobj = MATLAB structure containing mesh data
0017 %
0018 % EXAMPLE USAGE
0019 %    Mobj = read_admesh_mesh('msh', 'casename.14', 'coordinate', 'spherical')
0020 %
0021 % Author(s):
0022 %    Ricardo Torres (Plymouth Marine Laboratory) based on read_gmsh_mesh
0023 %
0024 % Revision history
0025 %
0026 %   2016-06-22 First version.
0027 %
0028 %==============================================================================
0029 
0030 subname = 'read_admesh_mesh';
0031 global ftbverbose
0032 if ftbverbose
0033     fprintf('\nbegin : %s \n', subname)
0034 end
0035 
0036 have_bath = false;
0037 have_strings = false;
0038 
0039 %--------------------------------------------------------------------------
0040 % Create a blank mesh object
0041 %--------------------------------------------------------------------------
0042 Mobj = make_blank_mesh;
0043 coordinate = 'cartesian';
0044 
0045 %--------------------------------------------------------------------------
0046 % Parse input arguments
0047 %--------------------------------------------------------------------------
0048 
0049 assert(mod(length(varargin), 2) == 0, 'incorrect usage of read_admesh_mesh, use keyword pairs')
0050 
0051 % Assume we have nothing sensible.
0052 have_msh = false;
0053 have_bath = false;
0054 have_lonlat = false;
0055 have_xy = false;
0056 
0057 for i = 1:2:length(varargin) - 1
0058     keyword = lower(varargin{i});
0059 
0060     assert(ischar(keyword), 'incorrect usage of read_gmsh_mesh')
0061 
0062     switch keyword
0063         case 'msh'
0064             gmsh_msh = varargin{i + 1};
0065             have_msh = true;
0066         case 'bath'
0067             gmsh_bath = varargin{i + 1};
0068             have_bath = true;
0069         case 'coordinate'
0070             coord = varargin{i + 1};
0071             if strcmpi(coord, 'spherical')
0072                 coordinate = 'spherical';
0073                 have_lonlat = true;
0074             elseif strcmpi(coord, 'cartesian')
0075                 coordinate = 'cartesian';
0076                 have_xy = true;
0077             else
0078                 warning('Unrecognised coordinate system (%s). Valid values are ''spherical'' and ''cartesian''.', coordinate)
0079             end
0080         case 'addcoriolis'
0081             val = varargin{i + 1};
0082             if val
0083                 addCoriolis = true;
0084             else
0085                 addCoriolis = false;
0086             end
0087         otherwise
0088             disp(varargin{i + 1})
0089             error('Can''t understand property: %s', varargin{i + 1});
0090 
0091     end
0092 end
0093 
0094 %--------------------------------------------------------------------------
0095 % Read the mesh from the msh file
0096 %--------------------------------------------------------------------------
0097 
0098 fid = fopen(gmsh_msh, 'rt');
0099 assert(fid >= 0, sprintf('file: %s does not exist\n',  gmsh_msh));
0100 
0101 % Count number of elements and vertices
0102 if ftbverbose
0103     fprintf('Reading from: %s\n', gmsh_msh)
0104 end
0105 
0106 % Read mesh type, written from srcatch:
0107 %
0108 % http://www.geuz.org/pipermail/gmsh/attachments/20071002/642cb6c3/attachment.m
0109 %
0110 
0111 lin = fgetl(fid);
0112 % first line is title of mesh
0113 title_str = lin;
0114 lin = fgetl(fid);
0115 % next line is mesh dimensions
0116 form = sscanf(lin, '%u %u');
0117 nElems = form(1);
0118 nVerts = form(2);
0119 
0120 %
0121 % if strcmp(lin, '$NOD')
0122 %     fileformat = 1;
0123 % elseif strcmp(lin, '$MeshFormat')
0124 %     fileformat = 2;
0125 %     lin = fgetl(fid);
0126 %     if feof(fid)
0127 %         fprintf(sprintf('Syntax error (no version) in: %s\n', filename));
0128 %         fileformat = 0;
0129 %     end
0130 %     form = sscanf(lin, '%f %d %d');
0131 %     if form(1) < 2
0132 %         fprintf(sprintf('Unknown mesh format: %s\n', lin));
0133 %         fileformat = 0;
0134 %     end
0135 %     if form(2) ~= 0
0136 %         fprintf('Sorry, this program can only read ASCII format\n');
0137 %         fileformat = 0;
0138 %     end
0139 %     fgetl(fid); % this should be $EndMeshFormat
0140 %     if feof(fid)
0141 %         fprintf('Syntax error (no $EndMeshFormat) in: %s\n', filename);
0142 %         fileformat = 0;
0143 %     end
0144 %     lin = fgetl(fid); % this should be $Nodes
0145 %     if feof(fid)
0146 %         fprintf('Syntax error (no $Nodes) in: %s\n', filename);
0147 %         fileformat = 0;
0148 %     end
0149 % end
0150 %
0151 % assert(logical(fileformat), 'Unrecognised mesh.')
0152 % Read in the number of nodes
0153 % nVerts = str2double(fgetl(fid));
0154 
0155 % Read the node positions and depths.
0156 C = textscan(fid, '%d %f %f %f', nVerts);
0157 nid = C{1};
0158 x = C{2};
0159 y = C{3};
0160 h = C{4};
0161 
0162 if have_lonlat
0163     lon = x;
0164     lat = y;
0165 else
0166     lon = zeros(size(x));
0167     lat = zeros(size(y));
0168 end
0169 
0170 % Now we should be at the end of the nodes and about to read the elements.
0171 
0172 % Read the element triangulation table. Format is:
0173 %   ID, dim, n1, n2, n3
0174 % Read the Element positions.
0175 C = textscan(fid, '%d %d %d %d %d', nElems);
0176 
0177 tri1 = [C{3},C{4},C{5}];
0178 
0179 have_lonlat = false;
0180 have_xy     = false;
0181 if strcmpi(coordinate, 'spherical')
0182     lon = x;
0183     lat = y;
0184     % Why reset everything to zero here? Because we don't have cartesian
0185     % ... what we read before were lat and lon
0186     x = x * 0.0;
0187     y = y * 0.0;
0188     have_lonlat = true;
0189     % Just do a double check on the coordinates to make sure we don't
0190     % actually have cartesian
0191     if max(lon) > 360
0192         warning('You''ve specified spherical coordinates, but your upper longitude value exceeds 360 degrees. Are you sure you have spherical data?')
0193     end
0194 elseif strcmpi(coordinate, 'cartesian')
0195     have_xy = true;
0196 else
0197     warning('Unrecognised coordinate system (%s). Valid values are ''spherical'' and ''cartesian''.', coordinate)
0198 end
0199 
0200 
0201 %--------------------------------------------------------------------------
0202 % check the topography from the mesh file... No separate file for
0203 % bathymetry here
0204 %--------------------------------------------------------------------------
0205 
0206     bath_range = max(h) - min(h);
0207     if have_bath || bath_range == 0
0208     elseif bath_range ~= 0
0209         have_bath = true;
0210     end
0211     % Make sure we have positive depths
0212     if sum(h > 0) < sum(h < 0)
0213         h = -h;
0214     end
0215 %--------------------------------------------------------------------------
0216 % Read the nodestrings
0217 %--------------------------------------------------------------------------
0218 lin = fgetl(fid); % need to skip one line for some reason...
0219 lin = fgetl(fid);
0220 if feof(fid)
0221     fprintf(sprintf('No Open Boundary strings found in: %s\n', filename));
0222     have_strings=0;
0223 else
0224     ind0=strfind(lin, '=');
0225     str_case = strtrim(lin(ind0+1:end));
0226     if strcmp(str_case, 'Number of open boundaries');
0227         nBoundary = sscanf(lin(1:ind0-1), '%d');
0228         have_strings=1;
0229     else
0230         fprintf(sprintf('Unknown mesh file format: %s\n', lin));
0231     end
0232 end
0233 
0234 if have_strings
0235     % read total number of open boundary nodes
0236      lin = fgetl(fid);
0237     ind0=strfind(lin, '=');
0238     str_case = strtrim(lin(ind0+1:end));
0239     if strcmp(str_case, 'Total number of open boundary nodes');
0240         nObc = sscanf(lin(1:ind0-1), '%d');
0241     else
0242         fprintf(sprintf('Unknown mesh file format: %s\n', lin));
0243     end
0244     % read number of open boundary nodes for each boundary
0245     for nn=1:nBoundary
0246     lin = fgetl(fid);
0247     ind0=strfind(lin, '=');
0248     str_case = strtrim(lin(ind0+1:end));
0249     display([' Reading  ',str_case ]);
0250         tmpnObc = sscanf(lin(1:ind0-1), '%u');
0251         boundary_names{nn} = num2str(nn);
0252         read_obc_nodes(nn)= textscan(fid,'%d',tmpnObc);
0253         fprintf(sprintf('Read Nodes in boundary: %s\n', boundary_names{nn}));
0254         lin = fgetl(fid); % no idea why we need this additional one...
0255 
0256     end
0257 end
0258 fclose(fid);
0259 
0260 
0261 %--------------------------------------------------------------------------
0262 % Transfer to Mesh structure
0263 %--------------------------------------------------------------------------
0264 
0265 Mobj.nVerts  = nVerts;
0266 Mobj.nElems  = nElems;
0267 Mobj.nativeCoords = coordinate;
0268 
0269 if have_lonlat
0270     Mobj.have_lonlat    = have_lonlat;
0271 Mobj.lon          = lon;
0272 Mobj.lat          = lat;
0273 end
0274 if have_xy
0275     Mobj.have_xy        = have_xy;
0276 Mobj.x            = x;
0277 Mobj.y            = y;
0278 end
0279 if have_bath
0280     Mobj.have_bath      = have_bath;
0281     Mobj.h              = h;
0282 end
0283 if have_strings
0284     Mobj.have_strings   = have_strings;
0285     Mobj.read_obc_nodes = read_obc_nodes;
0286 end
0287 if exist('addCoriolis', 'var') && addCoriolis
0288     Mobj.have_cor       = true;
0289 end
0290 
0291 Mobj.tri          = tri1;
0292 
0293 if ftbverbose
0294   fprintf('end   : %s\n', subname)
0295 end

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005