# VMT_CompMeanXS

## PURPOSE

Computes the mean cross section data from individual transects

## SYNOPSIS

function [A,V,log_text] = VMT_CompMeanXS(z,A,V)

## DESCRIPTION

     Computes the mean cross section data from individual transects
     that have been previously mapped to a common grid.

     (adapted from code by J. Czuba)

     P.R. Jackson, USGS, 12-9-08

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

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

## SOURCE CODE

    0001 function [A,V,log_text] = VMT_CompMeanXS(z,A,V)
    0002 % Computes the mean cross section data from individual transects
    0003 % that have been previously mapped to a common grid.
    0004 %
    0005 % (adapted from code by J. Czuba)
    0006 %
    0007 % P.R. Jackson, USGS, 12-9-08
    0008
    0009
    0010
    0011 %% Average mapped mean cross-sections from individual transects together
    0012
    0013 % Averaging for backscatter is only computed for Rio Grande probes
    0014 % Procedure for SonTek probes is different
    0015
    0016 switch V.probeType
    0017     % Assign mapped uniform grid vectors to the same array for averaging
    0018     case 'RG'
    0019         for zi = 1 : z
    0020
    0021             Back(:,:,zi) = A(zi).Comp.mcsBack(:,:);
    0022             Dir(:,:,zi) = A(zi).Comp.mcsDir(:,:);
    0023             Mag(:,:,zi) = A(zi).Comp.mcsMag(:,:);
    0024             East(:,:,zi) = A(zi).Comp.mcsEast(:,:);
    0025             North(:,:,zi) = A(zi).Comp.mcsNorth(:,:);
    0026             Vert(:,:,zi) = A(zi).Comp.mcsVert(:,:);
    0027             Bed(:,:,zi) = A(zi).Comp.mcsBed(:,:);
    0028
    0029         end
    0030
    0031         numavg = nansum(~isnan(Mag),3);
    0032         numavg(numavg==0) = NaN;
    0033         enscnt = nanmean(numavg,1);
    0034         [I,J] = ind2sub(size(enscnt),find(enscnt>=1));  %Changed to >= 1 PRJ 12-10-08  (uses data even if only one measurement)
    0035
    0036
    0037         Backone= Back;
    0038         Backone(~isnan(Back))=1;
    0039         V.countBack = nansum(Backone,3);
    0040         V.countBack(V.countBack==0)=NaN;
    0041         V.mcsBack = nanmean(Back,3);
    0042
    0043
    0044         Magone = Mag;
    0045         Vertone = Vert;
    0046         Bedone = Bed;
    0047
    0048
    0049         Magone(~isnan(Mag))=1;
    0050         Vertone(~isnan(Vert))=1;
    0051         Bedone(~isnan(Bed))=1;
    0052
    0053
    0054         V.countMag = nansum(Magone,3);
    0055         V.countVert = nansum(Vertone,3);
    0056         V.countBed = nansum(Bedone,3);
    0057
    0058         V.countMag(V.countMag==0)=NaN;
    0059         V.countVert(V.countVert==0)=NaN;
    0060         V.countBed(V.countBed==0)=NaN;
    0061
    0062         % Average mapped mean cross-sections from individual transects together
    0063
    0064         %V.mcsDir = nanmean(Dir,3);  % Will not average correctly in all cases due to 0-360
    0065         %wrapping (PRJ, 9-29-10)
    0066         %V.mcsMag = nanmean(Mag,3);  %Mag recomputed from north, east, up components(PRJ, 3-21-11)
    0067         V.mcsEast = nanmean(East,3);
    0068         V.mcsNorth = nanmean(North,3);
    0069         V.mcsVert = nanmean(Vert,3);
    0070
    0071     % Put all of the Sontek data in one place, then interpolate values at
    0072     % the MCS grid
    0073     case 'M9'
    0074
    0075         x       = [];
    0076         y       = [];
    0077         East    = [];
    0078         North   = [];
    0079         Vert    = [];
    0080         for zi = 1: z
    0081
    0082             Dir(:,:,zi) = A(zi).Comp.mcsDir(:,:);
    0083             Bed(:,:,zi) = A(zi).Comp.mcsBed(:,:);
    0084
    0085             xx    = meshgrid(A(zi).Comp.dl,A(zi).Wat.binDepth(:,1));
    0086             x     = [x; xx(:)];
    0087             y     = [y; A(zi).Wat.binDepth(:)];
    0088             East  = [East;  A(zi).Wat.vEast(:)];
    0089             North = [North; A(zi).Wat.vNorth(:)];
    0090             Vert  = [Vert;  A(zi).Wat.vVert(:)];
    0091         end
    0092
    0093         % FIXME: I call griddate 3 times. Need to rewrite to create 1
    0094         % delauney tri, and replace the V data.
    0095         V.mcsEast  = griddata(x,y,East,V.mcsDist,V.mcsDepth);
    0096         V.mcsNorth = griddata(x,y,North,V.mcsDist,V.mcsDepth);
    0097         V.mcsVert  = griddata(x,y,Vert,V.mcsDist,V.mcsDepth);
    0098
    0099 end % switch Probe type
    0100
    0101         %Average Magnitude
    0102         V.mcsMag = sqrt(V.mcsEast.^2 + V.mcsNorth.^2 + V.mcsVert.^2);
    0103
    0104         %Average the flow direction
    0105         V.mcsDir = ari2geodeg((atan2(V.mcsNorth, V.mcsEast))*180/pi);
    0106         % V.mcsDir = 90 - (atan2(V.mcsNorth, V.mcsEast))*180/pi; %Compute the atan from the velocity componentes, convert to radians, and rotate to north axis
    0107         % qindx = find(V.mcsDir < 0);
    0108         %     if ~isempty(qindx)
    0109         %         V.mcsDir(qindx) = V.mcsDir(qindx) + 360;  %Must add 360 deg to Quadrant 4 values as they are negative angles from the +y axis
    0110         %     end
    0111
    0112         V.mcsBed = nanmean(Bed,3);
    0113
    0114         %Compute the Bed Elevation in meters (Takes the mean value of the entered
    0115         %WSE timeseries if file loaded)
    0116         %disp(['Assigned Water Surface Elevation (WSE; in meters) = ' num2str(mean(A(1).wse))])
    0117         log_text = ['      WSE in meters) = ' num2str(mean(A(1).wse))];
    0118         V.mcsBedElev = mean(A(1).wse) - V.mcsBed;
    0119
    0120
    0121
    0122 return
    0123
    0124 % Remove values (Omitted 11/23/10, PRJ)
    0125 % Clean up
    0126 % switch A(1).probeType
    0127 %     case 'RG'
    0128 %         V.mcsBack(:,1:J(1)-1)=NaN;
    0129 %         V.mcsBack(:,J(end)+1:end)=NaN;
    0130 %         V.countBack(:,1:J(1)-1)=NaN;
    0131 %         V.countBack(:,J(end)+1:end)=NaN;
    0132 % end
    0133 %
    0134 % V.mcsDir(:,1:J(1)-1)=NaN;
    0135 % V.mcsDir(:,J(end)+1:end)=NaN;
    0136 % V.mcsMag(:,1:J(1)-1)=NaN;
    0137 % V.mcsMag(:,J(end)+1:end)=NaN;
    0138 % V.mcsEast(:,1:J(1)-1)=NaN;
    0139 % V.mcsEast(:,J(end)+1:end)=NaN;
    0140 % V.mcsNorth(:,1:J(1)-1)=NaN;
    0141 % V.mcsNorth(:,J(end)+1:end)=NaN;
    0142 % V.mcsVert(:,1:J(1)-1)=NaN;
    0143 % V.mcsVert(:,J(end)+1:end)=NaN;
    0144 % V.mcsBed(:,1:J(1)-1)=NaN;
    0145 % V.mcsBed(:,J(end)+1:end)=NaN;
    0146 % V.countMag(:,1:J(1)-1)=NaN;
    0147 % V.countVert(:,1:J(1)-1)=NaN;
    0148 % V.countBed(:,1:J(1)-1)=NaN;
    0149 % V.countMag(:,J(end)+1:end)=NaN;
    0150 % V.countVert(:,J(end)+1:end)=NaN;
    0151 % V.countBed(:,J(end)+1:end)=NaN;
    0152
    0153