# VMT_GridData2MeanXS

## PURPOSE

This routine generates a uniformly spaced grid for the mean cross section and

## SYNOPSIS

function [A,V] = VMT_GridData2MeanXS(z,A,V,unitQcorrection)

## DESCRIPTION

     This routine generates a uniformly spaced grid for the mean cross section and
     maps (interpolates) individual transects to this grid.

     (adapted from code by J. Czuba)

     P.R. Jackson, USGS, 12-9-08
     Last modified: F.L. Engel, USGS 2/20/2013

## CROSS-REFERENCE INFORMATION

This function calls:

- VMT_unitQcont VMT_UNITQCONT applies unit discharge continuity correction MCS velocities

This function is called by:

- VMT_ProcessTransects Driver program to process multiple transects at a single cross-section

## SUBFUNCTIONS

- function mypostcallback_zoom(obj,evd)
- function mypostcallback_pan(obj,evd)

## SOURCE CODE

    0001 function [A,V] = VMT_GridData2MeanXS(z,A,V,unitQcorrection)
    0002 % This routine generates a uniformly spaced grid for the mean cross section and
    0003 % maps (interpolates) individual transects to this grid.
    0004 %
    0005 % (adapted from code by J. Czuba)
    0006 %
    0007 % P.R. Jackson, USGS, 12-9-08
    0008 % Last modified: F.L. Engel, USGS 2/20/2013
    0009
    0010 %% User Input
    0011
    0012 xgdspc      = A(1).hgns; %Horizontal Grid node spacing in meters
    0013 %ygdspc      = A(1).vgns; %double(A(1).Sup.binSize_cm)/100; %Vertical Grid node spacing in meters
    0014 ygdspc      = double(A(1).Sup.binSize_cm)/100; %Vertical Grid node spacing in meters
    0015 if 0
    0016     xgdspc  = V.meddens + V.stddens;  %Auto method should include 67% of the values
    0017     %disp(['X Grid Node Auto Spacing = ' num2str(xgdspc) ' m'])
    0018     log_text = ['X Grid Node Auto Spacing = ' num2str(xgdspc) ' m'];
    0019 end
    0020
    0021
    0022 %% Determine uniform mean c-s grid for vector interpolating
    0023
    0024 % Determine mean cross-section velocity vector grid
    0025 % NEW: Allowed for explicit specification of vertical grid node spacing
    0026 V.mcsDist               = linspace(0,V.dl,floor(V.dl/xgdspc));
    0027 V.mcsDepth              = ...
    0028     min(A(1).Wat.binDepth(:)):ygdspc:max(A(1).Wat.binDepth(:));
    0029 % V.mcsDepth              = A(1).Wat.binDepth(:,1);
    0030 [V.mcsDist, V.mcsDepth] = meshgrid(V.mcsDist,V.mcsDepth');
    0031
    0032
    0033 % Define the MCS XY points. (REVISED PRJ, 10-18-12)
    0034 % Coordinate assignments depend on the starting
    0035 % point and the slope of the cross section. Theta is limited to 0 to 180
    0036 % (geographic) and 90 to 270 (arithmetic).  For COS, arithmetic angles
    0037 % between 90 and 270 are always negative so no need to add additional IF
    0038 % statement based on the slope.  However, SIN theta (aritmetic) is positive
    0039 % in MFD quadrants 2 and 4 and negative in 1 and 3. Therefore, we use the slope
    0040 % (positive in MFD quadrants 1 and 3, negative in 2 and 4) to determine whether to add or
    0041 % subtract the incremental distances from the start point.  (MFD = mean
    0042 % flow direction, used to define quadrants above and below)
    0043
    0044 if V.xLeftBank == V.xe % MFD Quadrants 2 and 3 (east start)
    0045     V.mcsX = V.xLeftBank - V.mcsDist(1,:).*cosd(geo2arideg(V.theta));
    0046 else % MFD Quadrants 1 and 4 (west start)
    0047     V.mcsX = V.xLeftBank + V.mcsDist(1,:).*cosd(geo2arideg(V.theta));
    0048 end
    0049
    0050 if V.yLeftBank == V.yn % MFD Quadrants 1 and 2 (north start)
    0051     if V.m >= 0 %MFD Quadrant 2
    0052         V.mcsY = V.yLeftBank - V.mcsDist(1,:).*sind(geo2arideg(V.theta));
    0053     else %MFD Quadrant 1
    0054         V.mcsY = V.yLeftBank + V.mcsDist(1,:).*sind(geo2arideg(V.theta));
    0055     end
    0056 else % MFD Quadrants 3 and 4 (south start)
    0057     if V.m >= 0 %MFD Quadrant 4
    0058         V.mcsY = V.yLeftBank + V.mcsDist(1,:).*sind(geo2arideg(V.theta));
    0059     else %MFD Quadrant 3
    0060         V.mcsY = V.yLeftBank - V.mcsDist(1,:).*sind(geo2arideg(V.theta));
    0061     end
    0062 end
    0063
    0064 V.mcsX = meshgrid(V.mcsX,V.mcsDepth(:,1));
    0065 V.mcsY = meshgrid(V.mcsY,V.mcsDepth(:,1));
    0066
    0067
    0068 % %Plot the MCS on figure 1
    0069 % figure(1); hold on
    0070 % plot(V.xLeftBank,V.yLeftBank,'gs','MarkerFaceColor','g'); hold on  %Green left bank start point
    0071 % plot(V.xRightBank,V.yRightBank,'rs','MarkerFaceColor','r'); hold on %Red right bank end point
    0072 % plot(V.mcsX(1,:),V.mcsY(1,:),'k+'); hold on
    0073 % figure(1); set(gca,'DataAspectRatio',[1 1 1],'PlotBoxAspectRatio',[1 1 1])
    0074 % clear zi
    0075 %
    0076 % % Format the ticks for UTM and allow zooming and panning
    0077 % figure(1);
    0078 % ticks_format('%6.0f','%8.0f'); %formats the ticks for UTM
    0079 % hdlzm_fig1 = zoom;
    0080 % set(hdlzm_fig1,'ActionPostCallback',@mypostcallback_zoom);
    0081 % set(hdlzm_fig1,'Enable','on');
    0082 % hdlpn_fig1 = pan;
    0083 % set(hdlpn_fig1,'ActionPostCallback',@mypostcallback_pan);
    0084 % set(hdlpn_fig1,'Enable','on');
    0085
    0086
    0087 %% If specified, correct the streamwise velocity by enforcing mass
    0088 % flux (capacitor) continuity
    0089 if unitQcorrection
    0090     A = VMT_unitQcont(A,V,z);
    0091 end
    0092
    0093 %% Interpolate individual transects onto uniform mean c-s grid
    0094 % Fill in uniform grid based on individual transects mapped onto the mean
    0095 % cross-section by interpolating between adjacent points
    0096
    0097 %ZI = interp2(X,Y,Z,XI,YI)
    0098 for zi = 1 : z
    0099
    0100     A(zi).Comp.mcsBack = interp2(A(zi).Comp.itDist, A(zi).Comp.itDepth, ...
    0101         A(zi).Clean.bs(:,A(zi).Comp.vecmap),V.mcsDist, V.mcsDepth);
    0102     A(zi).Comp.mcsBack(A(zi).Comp.mcsBack>=255) = NaN;
    0103     A(zi).Comp.mcsEast = interp2(A(zi).Comp.itDist, A(zi).Comp.itDepth, ...
    0104         A(zi).Clean.vEast(:,A(zi).Comp.vecmap), V.mcsDist, V.mcsDepth);
    0105     A(zi).Comp.mcsNorth = interp2(A(zi).Comp.itDist, A(zi).Comp.itDepth, ...
    0106         A(zi).Clean.vNorth(:,A(zi).Comp.vecmap), V.mcsDist, V.mcsDepth);
    0107     A(zi).Comp.mcsVert = interp2(A(zi).Comp.itDist, A(zi).Comp.itDepth, ...
    0108         A(zi).Clean.vVert(:,A(zi).Comp.vecmap), V.mcsDist, V.mcsDepth);
    0109
    0110     %Compute magnitude
    0111     A(zi).Comp.mcsMag = sqrt(A(zi).Comp.mcsEast.^2 + A(zi).Comp.mcsNorth.^2);
    0112
    0113
    0114     %For direction, compute from the velocity components
    0115     A(zi).Comp.mcsDir = ari2geodeg((atan2(A(zi).Comp.mcsNorth,A(zi).Comp.mcsEast))*180/pi);
    0116
    0117     A(zi).Comp.mcsBed  = interp1(A(zi).Comp.itDist(1,:),...
    0118         nanmean(A(zi).Nav.depth(A(zi).Comp.vecmap,:),2),V.mcsDist(1,:));
    0119
    0120 end
    0121
    0122 % clear zi
    0123
    0124 %%%%%%%%%%%%%%%%
    0125 % SUBFUNCTIONS %
    0126 %%%%%%%%%%%%%%%%
    0127 function mypostcallback_zoom(obj,evd)
    0128 ticks_format('%6.0f','%8.0f'); %formats the ticks for UTM (when zooming)
    0129
    0130 function mypostcallback_pan(obj,evd)
    0131 ticks_format('%6.0f','%8.0f'); %formats the ticks for UTM (when panning)