0001 function [Mobj] = read_admesh_mesh(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
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
0041
0042 Mobj = make_blank_mesh;
0043 coordinate = 'cartesian';
0044
0045
0046
0047
0048
0049 assert(mod(length(varargin), 2) == 0, 'incorrect usage of read_admesh_mesh, use keyword pairs')
0050
0051
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
0096
0097
0098 fid = fopen(gmsh_msh, 'rt');
0099 assert(fid >= 0, sprintf('file: %s does not exist\n', gmsh_msh));
0100
0101
0102 if ftbverbose
0103 fprintf('Reading from: %s\n', gmsh_msh)
0104 end
0105
0106
0107
0108
0109
0110
0111 lin = fgetl(fid);
0112
0113 title_str = lin;
0114 lin = fgetl(fid);
0115
0116 form = sscanf(lin, '%u %u');
0117 nElems = form(1);
0118 nVerts = form(2);
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
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
0171
0172
0173
0174
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
0185
0186 x = x * 0.0;
0187 y = y * 0.0;
0188 have_lonlat = true;
0189
0190
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
0203
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
0212 if sum(h > 0) < sum(h < 0)
0213 h = -h;
0214 end
0215
0216
0217
0218 lin = fgetl(fid);
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
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
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);
0255
0256 end
0257 end
0258 fclose(fid);
0259
0260
0261
0262
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