Home > fvcom_prepro > read_grid_mesh.m

read_grid_mesh

PURPOSE ^

Read .grid mesh files into Matlab mesh object

SYNOPSIS ^

function [Mobj] = read_grid_mesh(varargin)

DESCRIPTION ^

 Read .grid mesh files into Matlab mesh object  

 [Mobj] = function read_grid_mesh(varargin)

 DESCRIPTION:
    Read NOCL .grid file 
    Store in a matlab mesh object 

 INPUT [keyword pairs]:  
   'grid'                  = NOCL .grid file (e.g. UK_grid.grid)
   [optional] 'coordinate' = coordinate system for output data [spherical; cartesian (default)]
   [optional] 'input_coord' = coordinate system for input data [spherical; cartesian (default)]
   [optional] 'project'    = generate (x,y) coordinates if input is (lon,lat) 
                             generate (lon,lat) coordinates if input is (x,y)
                            ['true' ; 'false' (default)], see myproject.m
   [optional] 'zone'    = specify UTM zone for projection
   [optional] 'addCoriolis' = calculate Coriolis param (f), requires [lon,lat]

 OUTPUT:
    Mobj = matlab structure containing mesh data

 EXAMPLE USAGE
    Mobj = read_grid_mesh('grid','UK_grid.grid','coordinate','spherical','addCoriolis','true')

 Author(s):  
    Geoff Cowles (University of Massachusetts Dartmouth)
    Pierre Cazenave (Plymouth Marine Laboratory)
    Karen Thurston (National Oceanography Centre Liverpool)

 Revision history (KJT)
   2012-11-13 Adapted 'read_sms_mesh.m' to read NOCL .grid files
   2012-11-19 Added input/output coordinate functionality
   
==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Mobj] = read_grid_mesh(varargin) 
0002 
0003 % Read .grid mesh files into Matlab mesh object
0004 %
0005 % [Mobj] = function read_grid_mesh(varargin)
0006 %
0007 % DESCRIPTION:
0008 %    Read NOCL .grid file
0009 %    Store in a matlab mesh object
0010 %
0011 % INPUT [keyword pairs]:
0012 %   'grid'                  = NOCL .grid file (e.g. UK_grid.grid)
0013 %   [optional] 'coordinate' = coordinate system for output data [spherical; cartesian (default)]
0014 %   [optional] 'input_coord' = coordinate system for input data [spherical; cartesian (default)]
0015 %   [optional] 'project'    = generate (x,y) coordinates if input is (lon,lat)
0016 %                             generate (lon,lat) coordinates if input is (x,y)
0017 %                            ['true' ; 'false' (default)], see myproject.m
0018 %   [optional] 'zone'    = specify UTM zone for projection
0019 %   [optional] 'addCoriolis' = calculate Coriolis param (f), requires [lon,lat]
0020 %
0021 % OUTPUT:
0022 %    Mobj = matlab structure containing mesh data
0023 %
0024 % EXAMPLE USAGE
0025 %    Mobj = read_grid_mesh('grid','UK_grid.grid','coordinate','spherical','addCoriolis','true')
0026 %
0027 % Author(s):
0028 %    Geoff Cowles (University of Massachusetts Dartmouth)
0029 %    Pierre Cazenave (Plymouth Marine Laboratory)
0030 %    Karen Thurston (National Oceanography Centre Liverpool)
0031 %
0032 % Revision history (KJT)
0033 %   2012-11-13 Adapted 'read_sms_mesh.m' to read NOCL .grid files
0034 %   2012-11-19 Added input/output coordinate functionality
0035 %
0036 %==============================================================================
0037 
0038 subname = 'read_grid_mesh';
0039 global ftbverbose;
0040 if(ftbverbose);
0041   fprintf('\n')
0042   fprintf(['begin : ' subname '\n'])
0043 end;
0044 
0045 have_grid = false;
0046 have_bath = false;
0047 have_lonlat = false;
0048 have_xy = false;
0049 userproject = false;
0050 haveUTM = false;
0051 addCoriolis = false;
0052 
0053 %------------------------------------------------------------------------------
0054 % Create a blank mesh object
0055 %------------------------------------------------------------------------------
0056 Mobj = make_blank_mesh();
0057 
0058 %------------------------------------------------------------------------------
0059 % Parse input arguments
0060 %------------------------------------------------------------------------------
0061 
0062 if(mod(length(varargin),2) ~= 0)
0063     error(['incorrect usage of ',subname,', use keyword pairs'])
0064 end;
0065 
0066 
0067 for i=1:2:length(varargin)-1
0068     keyword  = lower(varargin{i});
0069     if( ~ischar(keyword) )
0070         error(['incorrect usage of ',subname,', check keywords'])
0071     end;
0072     
0073     switch(keyword(1:3))
0074     
0075     case 'gri'
0076         grid_fname = varargin{i+1};
0077         have_grid = true;
0078     case 'coo'
0079         coord = varargin{i+1};
0080         if(coord(1:3)=='sph')
0081             coordinate = 'spherical';
0082         else
0083             coordinate = 'cartesian';
0084         end;
0085     case 'in_'
0086         in_coord = varargin{i+1};
0087         if(in_coord(1:3)=='sph')
0088             in_coordinate = 'spherical';
0089             have_lonlat = true;
0090         else
0091             in_coordinate = 'cartesian';
0092             have_xy = true;
0093         end;
0094     case 'pro'
0095         val = varargin{i+1};
0096         if( val )
0097             userproject = true;
0098         else
0099             userproject = false;
0100         end;
0101     case 'zon'
0102         fullzone = varargin{i+1};
0103         UTMzone = regexpi(fullzone,'\ ','split');
0104         UTMzone=str2double(char(UTMzone{1}(1)));
0105         haveUTM = true;
0106     case 'add'
0107         val = varargin{i+1};
0108         if( val )
0109             addCoriolis = true;
0110         else
0111             addCoriolis = false;
0112         end;
0113     otherwise
0114         error(['Can''t understand property:' char(varargin{i+1})]);
0115     end; %switch(keyword)
0116     
0117 end;
0118         
0119 %------------------------------------------------------------------------------
0120 % Read the mesh from the .grid file
0121 %------------------------------------------------------------------------------
0122 
0123 fid = fopen(grid_fname,'r');
0124 if(fid  < 0)
0125     error(['file: ' grid_fname ' does not exist']);
0126 end;
0127 
0128 % Count number of elements and vertices
0129 if(ftbverbose);
0130   fprintf(['reading from: ' grid_fname '\n'])
0131 end;
0132 lin = fgetl(fid);   % ignore first line, it's the mesh name
0133 c=textscan(fid,'%u %u',1);  % get nElems and nVerts
0134 nElems = c{1};
0135 nVerts = c{2};
0136 clear c
0137 
0138 if(ftbverbose); 
0139   fprintf('nVerts: %d\n',nVerts); 
0140   fprintf('nElems: %d\n',nElems); 
0141   fprintf('reading in connectivity and grid points\n')
0142 end;
0143 
0144 % allocate memory to hold mesh and connectivity
0145 tri = zeros(nElems,3);
0146 x   = zeros(nVerts,1);
0147 y   = zeros(nVerts,1);
0148 h   = zeros(nVerts,1);
0149 lon = zeros(nVerts,1);
0150 lat = zeros(nVerts,1);
0151 ts  = zeros(nVerts,1);
0152 
0153 c = textscan(fid, '%u %f %f %f ', nVerts);
0154 x = c{2};
0155 y = c{3};
0156 h = c{4};
0157 clear c
0158 
0159 c = textscan(fid, '%u %u %u %u %u', nElems);
0160 tri(:,1) = c{3};
0161 tri(:,2) = c{4};
0162 tri(:,3) = c{5};
0163 clear c
0164 
0165 % Make sure we have bathymetry
0166 if sum(h)==0
0167     have_bath=false;
0168 else
0169     have_bath=true;
0170 end
0171 
0172 % Make sure we have positive depths
0173 if sum(h>0) < sum(h<0)
0174     h = -h;
0175 end
0176 
0177 % Build array of all the nodes in the open boundaries
0178 c = textscan(fid, '%u %*[^\n]',1);
0179 nOpenSeg = c{1};    % number of open boundary segments
0180 clear c
0181 
0182 lin=fgetl(fid); % skip the next line
0183 
0184 c = textscan(fid, '%u %*[^\n]',1);
0185 nOpenNodes = c{1};    % number of open boundary nodes
0186 clear c
0187 
0188 % Initialise SegCount variable
0189 SegCount = [0,0];
0190 
0191 for i=1:nOpenSeg    % for each open boundary segment
0192     c = textscan(fid, '%u %*[^\n]',1); % how many nodes in this segment?
0193     SegCount(1) = 1+SegCount(2);
0194     SegCount(2) = SegCount(1)+c{1}-1;
0195     clear c
0196     c = textscan(fid,'%u %*[^\n]',(SegCount(2)-SegCount(1)+1));    % get all the nodes in this segment
0197     allNodes{i} = c{1};
0198     clear c
0199 end
0200 
0201 if have_lonlat == true
0202     lon = x;
0203     lat = y;
0204     x = x*0.0;
0205     y = y*0.0;
0206     % Just do a double check on the coordinates to make sure we don't
0207     % actually have cartesian
0208     if max(lon)>360
0209         warning('You''ve specified spherical coordinates, but your upper longitude value exceeds 360 degrees. Are you sure you have spherical data?')
0210     end
0211 else
0212     have_xy = true;
0213 end;
0214 
0215 %------------------------------------------------------------------------------
0216 % Project if desired by user
0217 %------------------------------------------------------------------------------
0218 
0219 if(userproject)
0220     if (in_coordinate(1:3)=='car')
0221         fprintf('inverse projecting to get (lon,lat)\n')
0222         utmZones=cellfun(@(x) repmat(x,length(x),1),fullzone,'uni',false);
0223         [lon,lat] = utm2deg(x,y,utmZones{1});
0224         Mobj.have_lonlat = true;
0225     elseif (in_coordinate(1:3)=='sph')
0226         fprintf('forward projecting to get (x,y)\n')
0227         [x,y] = wgs2utm(lat,lon,UTMzone,'N');
0228         have_xy = true;
0229     end
0230 end
0231 
0232 %------------------------------------------------------------------------------
0233 % Transfer to Mesh structure
0234 %------------------------------------------------------------------------------
0235 
0236 Mobj.nVerts  = nVerts;
0237 Mobj.nElems  = nElems;
0238 Mobj.nativeCoords = coordinate;
0239 
0240 if have_lonlat==true
0241     Mobj.have_lonlat    = have_lonlat;
0242 end;
0243 if have_xy==true
0244     Mobj.have_xy        = have_xy;
0245 end;
0246 
0247 Mobj.have_bath      = have_bath;
0248 
0249 Mobj.read_obc_nodes = allNodes;
0250 
0251 Mobj.x            = x;
0252 Mobj.y            = y;
0253 Mobj.ts           = ts;
0254 Mobj.lon          = lon;
0255 Mobj.lat          = lat;
0256 Mobj.h            = h;
0257 Mobj.tri          = tri;
0258 
0259 if addCoriolis==true
0260     Mobj.have_cor = true;
0261     Mobj = add_coriolis(Mobj,'uselatitude');
0262 end
0263 
0264 if(ftbverbose);
0265   fprintf(['end   : ' subname '\n'])
0266 end;
0267 fclose(fid);
0268 
0269

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