Home > swan_scripts > fvcom2swan.m

fvcom2swan

PURPOSE ^

Convert fvcom grid and bathymetry file to unstructured SWAN format

SYNOPSIS ^

function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix,PlotMesh)

DESCRIPTION ^

 Convert fvcom grid and bathymetry file to unstructured SWAN format 

 function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix)

 DESCRIPTION:
    convert FVCOM mesh and bathymetry to SWAN format mesh and bathymetry

 INPUT 
   fvcom_mesh  = FVCOM 3.x grid file 
   fvcom_bathy = FVCOM 3.x bathymetry file
   fvcom_obc   = FVCOM 3.x open boundary file
   prefix      = prefix for naming SWAN grid and depth files   
   PlotMesh    = [true,false] plot the resulting mesh          

 OUTPUT:
    prefix.bot  = swan bathymetry file
    prefix.node = swan vertex file
    prefix.ele  = swan connectivity file

 EXAMPLE USAGE
    fvcom2swan('tst_grd.dat','tst_dep.dat','tst_obc.dat','tst',true) 

 Author(s):  
    Geoff Cowles (University of Massachusetts Dartmouth)

 Revision history
   
==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix,PlotMesh)
0002 % Convert fvcom grid and bathymetry file to unstructured SWAN format
0003 %
0004 % function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix)
0005 %
0006 % DESCRIPTION:
0007 %    convert FVCOM mesh and bathymetry to SWAN format mesh and bathymetry
0008 %
0009 % INPUT
0010 %   fvcom_mesh  = FVCOM 3.x grid file
0011 %   fvcom_bathy = FVCOM 3.x bathymetry file
0012 %   fvcom_obc   = FVCOM 3.x open boundary file
0013 %   prefix      = prefix for naming SWAN grid and depth files
0014 %   PlotMesh    = [true,false] plot the resulting mesh
0015 %
0016 % OUTPUT:
0017 %    prefix.bot  = swan bathymetry file
0018 %    prefix.node = swan vertex file
0019 %    prefix.ele  = swan connectivity file
0020 %
0021 % EXAMPLE USAGE
0022 %    fvcom2swan('tst_grd.dat','tst_dep.dat','tst_obc.dat','tst',true)
0023 %
0024 % Author(s):
0025 %    Geoff Cowles (University of Massachusetts Dartmouth)
0026 %
0027 % Revision history
0028 %
0029 %==============================================================================
0030 
0031 subname = 'fvcom2swan';
0032 fprintf('\n')
0033 fprintf(['begin : ' subname '\n'])
0034 
0035 % setup SWAN output files
0036 swan_bathy  = [prefix,'.bot'];
0037 swan_node   = [prefix,'.node'];
0038 swan_ele    = [prefix,'.ele'];
0039 
0040 %------------------------------------------------------------------------------
0041 % read in the FVCOM bathymetry data
0042 %------------------------------------------------------------------------------
0043 fid = fopen(fvcom_bathy,'r');
0044 if(fid  < 0)
0045   error(['file: ' fvcom_bathy ' does not exist']);
0046 end;
0047 C = textscan(fid, '%s %s %s %d', 1);
0048 Nverts = C{4};
0049 h = zeros(Nverts,1);
0050 fprintf('reading bathymetry file\n');
0051 fprintf('# nodes %d\n',Nverts);
0052 for i=1:Nverts
0053   C = textscan(fid, '%f %f %f', 1);
0054   h(i) = C{3};
0055 end;
0056 fprintf('min depth %f max depth %f\n',min(h),max(h));
0057 fprintf('bathymetry reading complete\n');
0058 fclose(fid);
0059 
0060 %------------------------------------------------------------------------------
0061 % read in the FVCOM open boundary node data
0062 %------------------------------------------------------------------------------
0063 fid = fopen(fvcom_obc,'r');
0064 if(fid  < 0)
0065   error(['file: ' fvcom_obc ' does not exist']);
0066 end;
0067 C = textscan(fid, '%s %s %s %s %d', 1);
0068 Nobcs = C{5};
0069 obc_nodes = zeros(Nobcs,1);
0070 fprintf('reading obc file\n');
0071 fprintf('# nodes %d\n',Nobcs);
0072 for i=1:Nobcs
0073   C = textscan(fid, '%d %d %d', 1);
0074   obc_nodes(i) = C{2};
0075 end;
0076 
0077 fprintf('obc reading complete\n');
0078 fclose(fid);
0079 
0080 %----------------------------------------------------
0081 % read in the fvcom connectivity and vertices
0082 %----------------------------------------------------
0083 fid = fopen(fvcom_mesh,'r');
0084 if(fid  < 0)
0085   error(['file: ' fvcom_mesh ' does not exist']);
0086 end;
0087 C = textscan(fid, '%s %s %s %d', 1); Nverts = C{4};
0088 C = textscan(fid, '%s %s %s %d', 1); Nelems = C{4};
0089 tri = zeros(Nelems,3); 
0090 x   = zeros(Nverts,2);
0091 fprintf('reading mesh file\n');
0092 fprintf('# nodes %d\n',Nverts);
0093 fprintf('# elems %d\n',Nelems);
0094 for i=1:Nelems
0095   C = textscan(fid,' %d %d %d %d %d\n',1);
0096   tri(i,1) = C{2};  tri(i,2) = C{3}; tri(i,3) = C{4};
0097 end;
0098 for i=1:Nverts 
0099   C = textscan(fid, '%d %f %f %f', 1);
0100   x(i,1) = C{2};
0101   x(i,2) = C{3};
0102 end;
0103 fprintf('mesh read in\n');
0104 fclose(fid);
0105 
0106 %----------------------------------------------------
0107 % mark nodes on the boundary
0108 %----------------------------------------------------
0109 bnodes = zeros(Nverts,1);
0110 cells = zeros(Nverts,10);
0111 cellcnt = zeros(Nverts,1);
0112 nbe = zeros(Nelems,3);
0113 
0114 for i = 1:Nelems
0115     n1 = tri(i,1) ; cellcnt(n1) = cellcnt(n1) + 1;
0116     n2 = tri(i,2) ; cellcnt(n2) = cellcnt(n2) + 1;
0117     n3 = tri(i,3) ; cellcnt(n3) = cellcnt(n3) + 1;
0118     cells(tri(i,1),cellcnt(n1)) = i;
0119     cells(tri(i,2),cellcnt(n2)) = i;
0120     cells(tri(i,3),cellcnt(n3)) = i;
0121 end;
0122 
0123 if(max(cellcnt) > 10)
0124   error('increase cells array')
0125 end;
0126 
0127 for i = 1:Nelems
0128     n1 = tri(i,1); n2 = tri(i,2); n3 = tri(i,3);
0129     for j1 = 1:cellcnt(n1)
0130         for j2 = 1:cellcnt(n2)
0131             if((cells(n1,j1) == cells(n2,j2)) & cells(n1,j1) ~= i); nbe(i,3) = cells(n1,j1); end;
0132         end;
0133     end;
0134     for j2 = 1:cellcnt(n2)
0135         for j3 = 1:cellcnt(n3)
0136             if((cells(n2,j2) == cells(n3,j3)) & cells(n2,j2) ~= i); nbe(i,1) = cells(n2,j2); end;
0137         end;
0138     end;
0139     for j1 = 1:cellcnt(n1)
0140         for j3 = 1:cellcnt(n3)
0141              if((cells(n1,j1) == cells(n3,j3)) & cells(n1,j1) ~= i); nbe(i,2) = cells(n3,j3); end;
0142         end;
0143     end;
0144 end;
0145 
0146 
0147 check = Nelems/10;
0148 for i=1:Nelems
0149    n1 = tri(i,1); n2 = tri(i,2); n3 = tri(i,3);
0150    if(nbe(i,1) == 0)
0151      bnodes(n2) = 1; bnodes(n3) = 1;
0152    elseif(nbe(i,2) == 0)
0153      bnodes(n3) = 1; bnodes(n1) = 1; 
0154    elseif(nbe(i,3) == 0)
0155      bnodes(n1) = 1; bnodes(n2) = 1; 
0156    end;
0157    if(mod(i,check)==0); fprintf('bnodes: completed %f percent \n',100*i/Nelems); end;
0158 end;
0159 
0160 %----------------------------------------------------
0161 % Mark open boundary with value of 2
0162 %----------------------------------------------------
0163 if(Nobcs > 1)
0164    bnodes(obc_nodes) = 2;
0165 end;
0166 %----------------------------------------------------
0167 % dump swan bathymetry file
0168 %----------------------------------------------------
0169 fid = fopen(swan_bathy,'w');
0170 for i=1:Nverts
0171     fprintf(fid,'%f\n',h(i));
0172 end;
0173 fclose(fid);
0174 
0175 
0176 %----------------------------------------------------
0177 % dump swan node file
0178 %----------------------------------------------------
0179 fid = fopen(swan_node,'w');
0180 fprintf(fid,'%d 2 0 1\n',Nverts);
0181 for i=1:Nverts
0182     fprintf(fid,'%d %f %f %d\n',i,x(i,1),x(i,2),bnodes(i)); 
0183 end;
0184 fclose(fid);
0185 
0186 
0187 %----------------------------------------------------
0188 % dump swan connectivty file
0189 %----------------------------------------------------
0190 fid = fopen(swan_ele,'w');
0191 fprintf(fid,'%d 3 0\n',Nelems);
0192 for i=1:Nelems 
0193     fprintf(fid,'%d %d %d %d\n',i,tri(i,1:3)); 
0194 end;
0195 fclose(fid);
0196 
0197 %----------------------------------------------------
0198 % plot mesh from swan files to check
0199 %----------------------------------------------------
0200 if(PlotMesh)
0201     plot_swan_mesh(swan_bathy,swan_node,swan_ele)
0202 end;
0203 
0204 fprintf(['end   : ' subname '\n'])
0205

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