Home > utilities > show_sigma.m

show_sigma

PURPOSE ^

plot a sigma distribution along a user-selected line

SYNOPSIS ^

function show_sigma(meshfile,bathfile,sigmafile,varargin)

DESCRIPTION ^

 plot a sigma distribution along a user-selected line

 show_sigma(meshfile,bathfile,sigmafile) 

 DESCRIPTION:
    plot a sigma distribution along a user-selected line

 INPUT:
    meshfile:   fvcom grid file
    bathfile:   fvcom bathymetry file
    sigmafile:  fvcom sigma distribution file
    npts:       number of points in the user-selected line (optional,
                defaults to 25).

 OUTPUT:

 EXAMPLE USAGE
    show_sigma('tst_grd.dat', 'tst_dep.dat', 'sigma.dat', 50)

 Author(s):
    Geoff Cowles (University of Massachusetts Dartmouth)
    Pierre Cazenave (Plymouth Marine Laboratory)

 Revision history
   2013-01-08 Added more resilient reading of the sigma coordinates file
   (can now handle comments and blank lines). Also add extra (optional)
   argument to increase the profile sampling frequency.
   
==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function show_sigma(meshfile,bathfile,sigmafile,varargin)
0002 % plot a sigma distribution along a user-selected line
0003 %
0004 % show_sigma(meshfile,bathfile,sigmafile)
0005 %
0006 % DESCRIPTION:
0007 %    plot a sigma distribution along a user-selected line
0008 %
0009 % INPUT:
0010 %    meshfile:   fvcom grid file
0011 %    bathfile:   fvcom bathymetry file
0012 %    sigmafile:  fvcom sigma distribution file
0013 %    npts:       number of points in the user-selected line (optional,
0014 %                defaults to 25).
0015 %
0016 % OUTPUT:
0017 %
0018 % EXAMPLE USAGE
0019 %    show_sigma('tst_grd.dat', 'tst_dep.dat', 'sigma.dat', 50)
0020 %
0021 % Author(s):
0022 %    Geoff Cowles (University of Massachusetts Dartmouth)
0023 %    Pierre Cazenave (Plymouth Marine Laboratory)
0024 %
0025 % Revision history
0026 %   2013-01-08 Added more resilient reading of the sigma coordinates file
0027 %   (can now handle comments and blank lines). Also add extra (optional)
0028 %   argument to increase the profile sampling frequency.
0029 %
0030 %==============================================================================
0031 
0032 close all
0033 
0034 global ftbverbose
0035 
0036 if nargin == 4
0037     npts = varargin{1};
0038 else
0039     npts = 25;
0040 end
0041 
0042 % read the mesh
0043 fid = fopen(meshfile,'r');
0044 if(fid  < 0)
0045     error(['file: ' meshfile ' does not exist']);
0046 end
0047 C = textscan(fid, '%s %s %s %d', 1);
0048 Nverts = C{4};
0049 C = textscan(fid, '%s %s %s %d', 1);
0050 Nelems = C{4};
0051 x = zeros(Nverts,3);
0052 tri = zeros(Nelems,3);
0053 if ftbverbose
0054     fprintf('Nverts: %d\n',Nverts)
0055     fprintf('Nelems: %d\n',Nelems)
0056 end
0057 
0058 for i=1:Nelems
0059     C = textscan(fid, '%d %d %d %d %d', 1);
0060     tri(i,1) = C{2};
0061     tri(i,2) = C{3};
0062     tri(i,3) = C{4};
0063 end
0064 for i=1:Nverts
0065     C = textscan(fid, '%d %f %f %f ', 1);
0066     x(i,1) = C{2};
0067     x(i,2) = C{3};
0068 end
0069 
0070 % read the bathy
0071 fid = fopen(bathfile,'r');
0072 if(fid  < 0)
0073     error(['file: ' bathfile ' does not exist']);
0074 end
0075 C = textscan(fid, '%s %s %s %d', 1);
0076 for i=1:Nverts
0077     C = textscan(fid, '%f %f %f ', 1);
0078     x(i,3) = C{3};
0079 end
0080 
0081 if ftbverbose
0082     fprintf('min topography %f\n',min(x(:,3)));
0083     fprintf('max topography %f\n',max(x(:,3)));
0084 end
0085 
0086 % read the sigma file
0087 fid = fopen(sigmafile,'r');
0088 if(fid  < 0)
0089     error(['file: ' sigmafile ' does not exist']);
0090 end
0091 
0092 while ~feof(fid)
0093     line = fgetl(fid);
0094     if isempty(line) || strncmp(line, '!', 1) || ~ischar(line)
0095         continue
0096     end
0097     key = lower(line(1:3));
0098     C = strtrim(regexpi(line, '=', 'split'));
0099     switch key
0100         case 'num'
0101             nlev = str2double(C{2});
0102         case 'sig'
0103             sigtype = C{2};
0104         case 'du '
0105             du = str2double(C{2});
0106         case 'dl '
0107             dl = str2double(C{2});
0108         case 'min'
0109             min_constant_depth = str2double(C{2});
0110         case 'ku '
0111             ku = str2double(C{2});
0112         case 'kl '
0113             kl = str2double(C{2});
0114         case 'zku'
0115             s = str2double(regexp(C{2}, ' ', 'split'));
0116             zku = zeros(ku, 1);
0117             for i = 1:ku
0118                 zku(i) = s(i);
0119             end
0120         case 'zkl'
0121             s = str2double(regexp(C{2}, ' ', 'split'));
0122             zkl = zeros(kl, 1);
0123             for i = 1:kl
0124                 zkl(i) = s(i);
0125             end
0126     end
0127 end
0128 
0129 if ftbverbose
0130     fprintf('nlev %d\n',nlev)
0131     fprintf('sigtype %s\n',sigtype)
0132     fprintf('du %d\n',du)
0133     fprintf('dl %d\n',dl)
0134     fprintf('min_constant_depth %f\n',min_constant_depth)
0135     fprintf('ku %d\n',ku)
0136     fprintf('kl %d\n',kl)
0137     fprintf('zku %d\n',zku)
0138     fprintf('zkl %d\n',zkl)
0139 end
0140 
0141 % generate the sigma coordinates
0142 
0143 % fix "java.lang.IllegalArgumentException: adding a container to a container
0144 % on a different GraphicsDevice" error. See
0145 % http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/169024.
0146 set(0,'DefaultFigureRenderer','OpenGL')
0147 
0148 figure(1)
0149 patch('Vertices',[x(:,1),x(:,2)],'Faces',tri,...
0150     'Cdata',x(:,3),'edgecolor','interp','facecolor','interp');
0151 axis equal
0152 
0153 % plot to get a line
0154 fprintf('select two end points of a transect with your mouse... ');
0155 [xt,yt] = ginput(2);
0156 hold on
0157 fprintf('done.\n')
0158 
0159 ds = (xt(2)-xt(1))/(npts-1);
0160 xline = xt(1):ds:xt(2);
0161 ds = (yt(2)-yt(1))/(npts-1);
0162 yline = yt(1):ds:yt(2);
0163 plot(xline, yline, 'w+')
0164 sline = zeros(1, npts);
0165 for i=2:npts
0166     sline(i) = sline(i-1) + sqrt((xline(i)-xline(i-1))^2 + (yline(i)-yline(i-1))^2);
0167 end
0168 
0169 % interpolate the bathymetry along the line
0170 zline = griddata(x(:,1), x(:,2), x(:,3), xline, yline);
0171 
0172 figure(2)
0173 plot(sline, -zline)
0174 
0175 % generate the sigma coordinates along the line
0176 xslice = zeros(npts,nlev);
0177 yslice = zeros(npts,nlev);
0178 z = nan(npts, nlev);
0179 
0180 % calculate the sigma distributions along the transect
0181 switch lower(sigtype)
0182     case 'generalized'
0183         for i=1:npts
0184             z(i,1:nlev) = sigma_gen(nlev,dl,du,kl,ku,zkl,zku,zline(i),min_constant_depth);
0185         end
0186     case 'uniform'
0187         for i=1:npts
0188             z(i,1:nlev) = 0:-1/double(nlev-1):-1;
0189         end
0190     otherwise
0191         error('Can''t do that sigtype')
0192 end
0193 
0194 for i=1:npts
0195     xslice(i,1:nlev) = sline(i);
0196     yslice(i,1:nlev) = z(i,1:nlev) * zline(i);
0197 end
0198 
0199 
0200 % plot the mesh along the transect
0201 for k=1:nlev
0202     plot(xslice(:,k),yslice(:,k),'k'); hold on;
0203 end
0204 for k=1:npts
0205     plot(xslice(k,:),yslice(k,:),'k'); hold on;
0206 end
0207 axis([xslice(1,1),xslice(end,1),min(yslice(:,end)),5])
0208 title('sigma distribution along the transect');

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