Home > swan_scripts > swan2netcdf.m

swan2netcdf

PURPOSE ^

matfile = 'c4.mat';

SYNOPSIS ^

function swan2netcdf(matfile,ncfile,basename,first_time,last_time,increment,isforcing,kn)

DESCRIPTION ^

matfile = 'c4.mat';
ncfile = 'case_s0004.nc';
basename = 'skg4.3';
first_time = '20090618_000000';
last_time = '20090621_000000';
increment = 3600;
isforcing = true;
kn = .003;
 Convert a SWAN output file (Matlab) into a NetCDF file

 function swan2netcdf(matfile,ncfile,basename,first_time,last_time,increment);

 DESCRIPTION:
    read output from unstructured SWAN model (currently 40.82) and
    dump to a NetCDF file which is far more useful than a Matlab file.

 INPUT
   matfile  = Unstructured SWAN Matlab file
   ncfile   = NetCDF file for output
   basename = prefix for SWAN mesh, bathymetry, connectivity files
   first_time:  first time frame in Matlab object frame time
   last_time:   last time frame in Matlab object frame time
   increment:   increment in seconds
   isforcing:   converts NaNs from .mat file to 1.0's or 0.0's
   kn:          Nikuradse roughness in meters  [OPTIONAL, default = .01]
 OUTPUT:
    NetCDF file containing:
      a.) time in modified Julian day
      b.) significant wave height (hs)
      c.) wave direction
      d.) mesh
      e.) peak period
      f.) U10
      g.) V10
      h.) bottom orbital velocity
      i.) bottom period
      j.) H/h ratio
      k.) wave-induced bed stress

 EXAMPLE USAGE
   swan2netcdf('gom1.mat','gom1.nc','gom1','20070101_000000','20070131_000000',3600,true,.045)
     this converts gom1.mat to gom1.nc using SWAN grid files gom1.ele, gom1.bot
     and gom1.node from Jan 1, 2007 00:00:00 to Jan 31, 2007 00:00:00 in increments
     of 1 hour.

 NOTE
    routine is not refined, e.g. will not check if files exist and will
    probably crash if you do not have the variables above in the SWAN
    output file.
    You will need approximately the following BLOCK command in your SWAN runfile

    BLOCK 'COMPGRID' NOHEAD 'gom1.mat' LAY 3 XP YP DEP HS RTP TPS DIR WLEN &
                WINDV OUTPUT 20070101_000000 3600 SEC

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

 Revision history
    09-29-2010 -Now warns you if variable was not found in SWAN output
    .mat file and added two variables from SWAN output Ubot and TmBot.
    12-01-2010 Added 'isforcing' option
==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function swan2netcdf(matfile,ncfile,basename,first_time,last_time,increment,isforcing,kn)
0002 %matfile = 'c4.mat';
0003 %ncfile = 'case_s0004.nc';
0004 %basename = 'skg4.3';
0005 %first_time = '20090618_000000';
0006 %last_time = '20090621_000000';
0007 %increment = 3600;
0008 %isforcing = true;
0009 %kn = .003;
0010 % Convert a SWAN output file (Matlab) into a NetCDF file
0011 %
0012 % function swan2netcdf(matfile,ncfile,basename,first_time,last_time,increment);
0013 %
0014 % DESCRIPTION:
0015 %    read output from unstructured SWAN model (currently 40.82) and
0016 %    dump to a NetCDF file which is far more useful than a Matlab file.
0017 %
0018 % INPUT
0019 %   matfile  = Unstructured SWAN Matlab file
0020 %   ncfile   = NetCDF file for output
0021 %   basename = prefix for SWAN mesh, bathymetry, connectivity files
0022 %   first_time:  first time frame in Matlab object frame time
0023 %   last_time:   last time frame in Matlab object frame time
0024 %   increment:   increment in seconds
0025 %   isforcing:   converts NaNs from .mat file to 1.0's or 0.0's
0026 %   kn:          Nikuradse roughness in meters  [OPTIONAL, default = .01]
0027 % OUTPUT:
0028 %    NetCDF file containing:
0029 %      a.) time in modified Julian day
0030 %      b.) significant wave height (hs)
0031 %      c.) wave direction
0032 %      d.) mesh
0033 %      e.) peak period
0034 %      f.) U10
0035 %      g.) V10
0036 %      h.) bottom orbital velocity
0037 %      i.) bottom period
0038 %      j.) H/h ratio
0039 %      k.) wave-induced bed stress
0040 %
0041 % EXAMPLE USAGE
0042 %   swan2netcdf('gom1.mat','gom1.nc','gom1','20070101_000000','20070131_000000',3600,true,.045)
0043 %     this converts gom1.mat to gom1.nc using SWAN grid files gom1.ele, gom1.bot
0044 %     and gom1.node from Jan 1, 2007 00:00:00 to Jan 31, 2007 00:00:00 in increments
0045 %     of 1 hour.
0046 %
0047 % NOTE
0048 %    routine is not refined, e.g. will not check if files exist and will
0049 %    probably crash if you do not have the variables above in the SWAN
0050 %    output file.
0051 %    You will need approximately the following BLOCK command in your SWAN runfile
0052 %
0053 %    BLOCK 'COMPGRID' NOHEAD 'gom1.mat' LAY 3 XP YP DEP HS RTP TPS DIR WLEN &
0054 %                WINDV OUTPUT 20070101_000000 3600 SEC
0055 %
0056 % Author(s):
0057 %    Geoff Cowles (University of Massachusetts Dartmouth)
0058 %    Eric Holmes (University of Massachusetts Dartmouth)
0059 %
0060 % Revision history
0061 %    09-29-2010 -Now warns you if variable was not found in SWAN output
0062 %    .mat file and added two variables from SWAN output Ubot and TmBot.
0063 %    12-01-2010 Added 'isforcing' option
0064 %==============================================================================
0065 
0066 % set Nikaradse roughness to 1 cm if user does not specify
0067 if(~exist('kn'))
0068   kn = .01;
0069 end;
0070 z0 = kn/30.;  %hydraulic roughness
0071 
0072 eval(['load ' matfile]);              % load binary file containing SWAN results
0073 % obtained using BLOCK command with COMPGRID-set
0074 % load the connectivity
0075 elefile=[basename '.ele'];
0076 fid = fopen(elefile);                 % load TRIANGLE element based connectivity file
0077 [nele] = fscanf(fid,'%i',[1 3]);     % get number of triangles
0078 jnk = fscanf(fid,'%i',[4 nele(1)])'; % get connectivity table
0079 tri = jnk(:,2:4);
0080 
0081 % load the bathymetry
0082 bathfile=[basename '.bot'];
0083 h = textread(bathfile,'%f\n');
0084 node = prod(size(h));
0085 
0086 % check if variable range exists
0087 f = first_time;
0088 l = last_time;
0089 tbeg = datenum(str2num(f(1:4)),str2num(f(5:6)),str2num(f(7:8)),str2num(f(10:11)),str2num(f(12:13)),str2num(f(14:15)));
0090 tend = datenum(str2num(l(1:4)),str2num(l(5:6)),str2num(l(7:8)),str2num(l(10:11)),str2num(l(12:13)),str2num(l(14:15)));
0091 tinc  = increment/(24*3600);
0092 times = tbeg:tinc:tend;
0093 ntimes = prod(size(times));
0094 for i=1:ntimes;
0095     %  datestr(times(i))
0096     date = datestr(times(i),30);
0097     vname = ['Hsig_',date(1:8),'_',date(10:15)];
0098     if(exist(vname) == 0)
0099         error('variable frame %s\n does not exist',vname)
0100     end;
0101 end;
0102 
0103 
0104 % dump the netcdf file
0105 nc = netcdf(ncfile, 'clobber');
0106 nc.info = 'created from SWAN unstructured grid output file';
0107 nc.source = 'fvcom grid (unstructured) surface forcing';
0108 
0109 % dimensions
0110 nc('nele') = nele;
0111 nc('node') = node;
0112 nc('three') = 3;
0113 nc('time') = 0;
0114 
0115 % variables
0116 date = datestr(times(1),30);
0117 
0118 
0119 
0120 nc{'x'} = ncfloat('node');
0121 nc{'x'}.long_name = 'nodal x-coordinate';
0122 nc{'x'}.units     = 'm';
0123 
0124 nc{'y'} = ncfloat('node');
0125 nc{'y'}.long_name = 'nodal y-coordinate';
0126 nc{'y'}.units     = 'm';
0127 
0128 nc{'h'} = ncfloat('node');
0129 nc{'h'}.long_name = 'Bathymetry';
0130 nc{'h'}.units     = 'm';
0131 
0132 nc{'d'} = ncfloat('time','node');
0133 nc{'d'}.long_name = 'Depth';
0134 nc{'d'}.units     = 'm';
0135 
0136 nc{'nv'} = ncint('three','nele');
0137 nc{'nv'}.long_name = 'nodes surrounding element';
0138 
0139 
0140 nc{'time'} = ncfloat('time');
0141 nc{'time'}.long_name = 'time';
0142 nc{'time'}.units = 'days since 1858-11-17 00:00:00';
0143 nc{'time'}.format = 'modified julian day (MJD)';
0144 nc{'time'}.time_zone = 'UTC';
0145 
0146 nc{'Itime'} = ncint('time');
0147 nc{'Itime'}.units = 'days since 1858-11-17 00:00:00';
0148 nc{'Itime'}.format = 'modified julian day (MJD)';
0149 nc{'Itime'}.time_zone = 'UTC';
0150 
0151 nc{'Itime2'} = ncint('time');
0152 nc{'Itime2'}.units = 'msec since 00:00:00';
0153 nc{'Itime2'}.time_zone = 'UTC';
0154 
0155 vname = ['Hsig_',date(1:8),'_',date(10:15)];
0156 if(exist(vname) == 0)
0157     warning('Variable Hsig does not exist not being added to netCDF output')
0158 else
0159     nc{'hs'} = ncfloat('time','node');
0160     nc{'hs'}.long_name = 'Significant Wave Height';
0161     nc{'hs'}.units     = 'm';
0162 end;
0163 
0164 vname = ['Dir_',date(1:8),'_',date(10:15)];
0165 if(exist(vname) == 0)
0166     warning('Variable Dir does not exist not being added to netCDF output')
0167 else
0168     nc{'wdir'} = ncfloat('time','node');
0169     nc{'wdir'}.long_name = 'Wave  Direction';
0170     nc{'wdir'}.units     = 'degree';
0171 end;
0172 
0173 vname = ['RTpeak_',date(1:8),'_',date(10:15)];
0174 if(exist(vname) == 0)
0175     warning('Variable RTpeak does not exist not being added to netCDF output')
0176 else
0177     nc{'tpeak'} = ncfloat('time','node');
0178     nc{'tpeak'}.long_name = 'Relative Peak Period';
0179     nc{'tpeak'}.units     = 's';
0180 end;
0181 
0182 vname = ['Windv_x_',date(1:8),'_',date(10:15)];
0183 if(exist(vname) == 0)
0184     warning('Variable WindV_x_ does not exist not being added to netCDF output')
0185 else
0186     nc{'U10'} = ncfloat('time','node');
0187     nc{'U10'}.long_name = 'Wind Velocity x-direction';
0188     nc{'U10'}.units     = 'm/s';
0189 end;
0190 
0191 vname = ['Windv_y_',date(1:8),'_',date(10:15)];
0192 if(exist(vname) == 0)
0193     warning('Variable WindV_y_ does not exist not being added to netCDF output')
0194 else
0195     nc{'V10'} = ncfloat('time','node');
0196     nc{'V10'}.long_name = 'Wind Velocity y-direction';
0197     nc{'V10'}.units     = 'm/s';
0198 end;
0199 
0200 vname = ['Wlen_',date(1:8),'_',date(10:15)];
0201 if(exist(vname) == 0)
0202     warning('Variable Wlen does not exist not being added to netCDF output')
0203 else
0204     nc{'wlen'} = ncfloat('time','node');
0205     nc{'wlen'}.long_name = 'wavelength';
0206     nc{'wlen'}.units     = 'm';
0207 end;
0208 
0209 vname = ['Ubot_',date(1:8),'_',date(10:15)];
0210 if(exist(vname) == 0)
0211     warning('Variable Ubot does not exist not being added to netCDF output')
0212 else
0213     nc{'Ubot'} = ncfloat('time','node');
0214     nc{'Ubot'}.long_name = 'Bottom Orbital Velocity';
0215     nc{'Ubot'}.units     = 'm/s';
0216 end;
0217 
0218 
0219 vname = ['TmBot_',date(1:8),'_',date(10:15)];
0220 if(exist(vname) == 0)
0221     warning('Variable TmBot does not exist not being added to netCDF output')
0222 else
0223     nc{'TmBot'} = ncfloat('time','node');
0224     nc{'TmBot'}.long_name = 'Bottom Wave Period';
0225     nc{'TmBot'}.units     = 's';
0226 end;
0227 
0228 vname1 = ['Hsig_',date(1:8),'_',date(10:15)];
0229 vname2 = ['Depth_',date(1:8),'_',date(10:15)];
0230 if(exist(vname1) == 0 | exist(vname2)==0)
0231     warning('Bot Hsig and Depth do not exist so will not dump H/h')   
0232 else
0233     nc{'H_over_h'} = ncfloat('time','node');
0234     nc{'H_over_h'}.long_name = 'Ratio of Hsig to depth';
0235     nc{'H_over_h'}.units     = '-';
0236 end;
0237 
0238 vname1 = ['TmBot_',date(1:8),'_',date(10:15)];
0239 vname2 = ['Ubot_',date(1:8),'_',date(10:15)];
0240 if(exist(vname1) == 0 | exist(vname2)==0)
0241     warning('Bot TmBot and Ubot do not exist so will not dump bed stress')
0242 else
0243     nc{'tau_w'} = ncfloat('time','node');
0244     nc{'tau_w'}.long_name = 'wave-induced bed stress'; 
0245     nc{'tau_w'}.units     = 'm^2/s^2';
0246 end;
0247 
0248 % static vars
0249 nc{'x'}(:) = Xp;
0250 nc{'y'}(:) = Yp;
0251 nc{'h'}(:) = h;
0252 nc{'nv'}(:,:) = tri';
0253 
0254 badpts = [];
0255 % dump dynamic vars
0256 for i=1:ntimes;
0257     
0258     fprintf('processing time %s\n',datestr(times(i)));
0259     
0260     if(isforcing==1)
0261         date = datestr(times(i),30);
0262         vname = ['Hsig_',date(1:8),'_',date(10:15)]; var1 = eval(vname)';
0263         vname = ['RTpeak_',date(1:8),'_',date(10:15)];var2 = eval(vname)';
0264         var3  = var1+var2;
0265         badpts = find(isnan(var3));
0266         clear var1;
0267         clear var2;
0268         clear var3;
0269     end; 
0270     
0271     %time
0272     shift = 678942.;  % datenum(2010,1,1,0,0,0)-greg2mjulian(2010,1,1,0,0,0);
0273     time  = times(i) - shift;
0274     nc{'time'}(i) = time;
0275     nc{'Itime'}(i) = floor(time);
0276     nc{'Itime2'}(i) = mod(time,1)*24*3600*1000.;
0277     
0278     
0279     %hs
0280     date = datestr(times(i),30);
0281     vname = ['Hsig_',date(1:8),'_',date(10:15)];
0282     if(exist(vname) == 0)
0283         %fprintf('variable frame %s\n does not exist',vname)
0284     else
0285         var = eval(vname)';
0286         if (isforcing == 1); var(badpts) = 0.; end;
0287         nc{'hs'}(i,:) = var;
0288     end;
0289     
0290     %tp
0291     date = datestr(times(i),30);
0292     vname = ['RTpeak_',date(1:8),'_',date(10:15)];
0293     if(exist(vname) == 0)
0294         %fprintf('variable frame %s\n does not exist',vname)
0295     else
0296         var = eval(vname)';
0297         if (isforcing == 1);  var(badpts) = 1.; end;
0298         nc{'tpeak'}(i,:) = var;
0299     end;
0300     
0301     
0302     %depth
0303     date = datestr(times(i),30);
0304     vname = ['Depth_',date(1:8),'_',date(10:15)];
0305     if(exist(vname) == 0)
0306         %fprintf('variable frame %s\n does not exist',vname)
0307     else
0308         nc{'d'}(i,:) = eval(vname)';
0309     end;
0310     
0311     % wave dir
0312     date = datestr(times(i),30);
0313     vname = ['Dir_',date(1:8),'_',date(10:15)];
0314     if(exist(vname) == 0)
0315         %fprintf('variable frame %s\n does not exist',vname)
0316     else
0317         var = eval(vname)';
0318         if (isforcing == 1); var(badpts) = 0.; end;
0319         nc{'wdir'}(i,:) = var;
0320     end;
0321     
0322     % U10
0323     date = datestr(times(i),30);
0324     vname = ['Windv_x_',date(1:8),'_',date(10:15)];
0325     if(exist(vname) == 0)
0326         %fprintf('variable frame %s\n does not exist',vname)
0327     else
0328         var = eval(vname)';
0329         nc{'U10'}(i,:) = var;
0330     end;
0331     
0332     % V10
0333     date = datestr(times(i),30);
0334     vname = ['Windv_y_',date(1:8),'_',date(10:15)];
0335     if(exist(vname) == 0)
0336         %fprintf('variable frame %s\n does not exist',vname)
0337     else
0338         var = eval(vname)';
0339         if (isforcing == 1)
0340             var(isnan(var)) = 0.0;
0341         end
0342         nc{'V10'}(i,:) = var';
0343     end;
0344     
0345     % orbital velocity
0346     date = datestr(times(i),30);
0347     vname = ['Ubot_',date(1:8),'_',date(10:15)];
0348     if(exist(vname) == 0)
0349         %fprintf('variable frame %s\n does not exist',vname)
0350     else
0351         var = eval(vname)';
0352         if (isforcing == 1); var(badpts) = 0.; end;
0353         nc{'Ubot'}(i,:) = var';
0354     end;
0355     
0356     % wavelength
0357     date = datestr(times(i),30);
0358     vname = ['Wlen_',date(1:8),'_',date(10:15)];
0359     if(exist(vname) == 0)
0360         %fprintf('variable frame %s\n does not exist',vname)
0361     else
0362         var = eval(vname)';
0363         if (isforcing == 1); var(badpts) = 1.; end;
0364         nc{'wlen'}(i,:) = var';
0365     end;
0366     
0367     % bottom wave period
0368     date = datestr(times(i),30);
0369     vname = ['TmBot_',date(1:8),'_',date(10:15)];
0370     if(exist(vname) == 0)
0371         %fprintf('variable frame %s\n does not exist',vname)
0372     else
0373         var = eval(vname)';
0374         if (isforcing == 1); var(badpts) = 1.; end;
0375         nc{'TmBot'}(i,:) = var';
0376     end;
0377 
0378     % hsig/depth
0379     date = datestr(times(i),30);
0380     vname1 = ['Hsig_',date(1:8),'_',date(10:15)];
0381     vname2 = ['Depth_',date(1:8),'_',date(10:15)];
0382     if(exist(vname1) == 0 | exist(vname2)==0)
0383         %fprintf('variable frame %s\n does not exist',vname)
0384     else
0385         var1 = eval(vname1)';
0386         var2 = eval(vname2)';
0387         var  = var1./var2;
0388         if (isforcing == 1); var(badpts) = 0.; end;
0389         nc{'H_over_h'}(i,:) = var';
0390     end;
0391     
0392     % wave-induced bed stress
0393     date = datestr(times(i),30);
0394     vname1 = ['TmBot_',date(1:8),'_',date(10:15)];
0395     vname2 = ['Ubot_',date(1:8),'_',date(10:15)];
0396     if(exist(vname1) == 0 | exist(vname2)==0)
0397         %fprintf('variable frame %s\n does not exist',vname)
0398     else
0399         %compute wave induced bed stress using Soulsby formulas
0400         %friction factor (fw) is eq 62A
0401         %tau_w = 0.5*fw*Ubot^2
0402         var1 = eval(vname1)';
0403         var2 = eval(vname2)';
0404         omega_wave = (2.0*pi./max(var1,.05))'; %wave-orbital frequency
0405         var=0.5*1.39*((omega_wave*z0).^.52)'.*(var2.^(2.0-.52));
0406         if (isforcing == 1); var(badpts) = 0.; end;
0407         nc{'tau_w'}(i,:) = var';
0408     end;
0409     %error('hog')
0410 
0411 end;
0412 
0413 
0414 nc = close(nc);

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