# VMT_SmoothVar

## PURPOSE

This routine smooths all processed variables in V struct. By default is

## SYNOPSIS

function [V] = VMT_SmoothVar(V,hwin,vwin)

## DESCRIPTION

     This routine smooths all processed variables in V struct. By default is
     uses smooth2a (FEX), but can also use nanmoving_average2 (FEX).

     Updated 9-30-10 to include the smooth2a routine with user selection.
     Updated 12-21-2012 to smooth all variables. This is necessary to separate
     plotting from computations. FLE

     P.R. Jackson, USGS, 8/31/09
     Last modified: F.L. Engel, USGS, 12/21/2012

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- VMT --- THE VELOCITY MAPPING TOOLBOX ---

## SOURCE CODE

    0001 function [V] = VMT_SmoothVar(V,hwin,vwin)
    0002 % This routine smooths all processed variables in V struct. By default is
    0003 % uses smooth2a (FEX), but can also use nanmoving_average2 (FEX).
    0004 %
    0005 % Updated 9-30-10 to include the smooth2a routine with user selection.
    0006 % Updated 12-21-2012 to smooth all variables. This is necessary to separate
    0007 % plotting from computations. FLE
    0008 %
    0009 % P.R. Jackson, USGS, 8/31/09
    0010 % Last modified: F.L. Engel, USGS, 12/21/2012
    0011
    0012 %disp(['Smoothing Data '])
    0013 warning off
    0014 %% Smooth
    0015 use_smooth2a = 1; %Set to 1 to use smooth2a.m for smoothing else set to 0 to use nanmoving_average2.m
    0016 % Set default to smooth2a 8-7-12 to overcome issues
    0017 % with corner values not getting averaged in
    0018 % nanmoving_average2 (really affected backscatter)
    0019 if V.probeType == 'RG'
    0020     var ={...
    0021         'streamwise';...
    0022         'transverse';...
    0023         'mag';...
    0024         'primary_zsd';...
    0025         'secondary_zsd';...
    0026         'primary_roz';...
    0027         'secondary_roz';...
    0028         'primary_roz_x';...
    0029         'primary_roz_y';...
    0030         'secondary_roz_x';...
    0031         'secondary_roz_y';...
    0032         'backscatter';...
    0033         'flowangle';...
    0034         };
    0035 elseif V.probeType == 'M9'
    0036     var ={...
    0037         'streamwise';...
    0038         'transverse';...
    0039         'mag';...
    0040         'primary_zsd';...
    0041         'secondary_zsd';...
    0042         'primary_roz';...
    0043         'secondary_roz';...
    0044         'primary_roz_x';...
    0045         'primary_roz_y';...
    0046         'secondary_roz_x';...
    0047         'secondary_roz_y';...
    0048         ...'backscatter';...
    0049         'flowangle';...
    0050         };
    0051 end
    0052 % Fr  - Window semi-length in the rows.
    0053 Fr = vwin; %
    0054 % Fc  - Window semi-length in the columns.
    0055 Fc = hwin; %
    0056
    0057 if Fr == 0 & Fc ~= 0
    0058     errordlg('Both Vertical Smoothing Window and Horizontal Smoothing Window must be set to zero to turn off smoothing. Smoothing cannot be turned off in one direction only.' );
    0059 elseif Fr ~= 0 & Fc == 0
    0060     errordlg('Both Vertical Smoothing Window and Horizontal Smoothing Window must be set to zero to turn off smoothing. Smoothing cannot be turned off in one direction only.');
    0061 end
    0062
    0063 for i = 1:numel(var)
    0064     switch var{i}
    0065         case{'streamwise'}  %Smooths the streamwise velocity
    0066             if Fr == 0 & Fc == 0
    0067                 V.uSmooth = V.u;
    0068             else
    0069                 if use_smooth2a
    0070                     [V.uSmooth] = smooth2a(V.u,Fr,Fc);
    0071                 else
    0072                     [V.uSmooth] = nanmoving_average2(V.u,Fr,Fc);
    0073                 end
    0074             end
    0075
    0076         case{'transverse'} %Smooths the transverse velocity
    0077             if Fr == 0 & Fc == 0
    0078                 V.vSmooth = V.v;
    0079             else
    0080                 if use_smooth2a
    0081                     [V.vSmooth] = smooth2a(V.v,Fr,Fc);
    0082                 else
    0083                     [V.vSmooth] = nanmoving_average2(V.v,Fr,Fc);
    0084                 end
    0085             end
    0086         case{'mag'} %Smooths the velocity magnitude
    0087             if Fr == 0 & Fc == 0
    0088                 V.mcsMagSmooth = V.mcsMag;
    0089             else
    0090                 if use_smooth2a
    0091                     %[V.mcsMagSmooth] = smooth2a(V.mcsMag,Fr,Fc);  %Changed to
    0092                     %use the components to smooths and then recompute. (PRJ,
    0093                     %3-21-11)
    0094                     V.mcsEastSmooth  = smooth2a(V.mcsEast,Fr,Fc);
    0095                     V.mcsNorthSmooth = smooth2a(V.mcsNorth,Fr,Fc);
    0096                 else
    0097                     %[V.mcsMagSmooth] = nanmoving_average2(V.mcsMag,Fr,Fc);
    0098                     V.mcsEastSmooth  = nanmoving_average2(V.mcsEast,Fr,Fc);
    0099                     V.mcsNorthSmooth = nanmoving_average2(V.mcsNorth,Fr,Fc);
    0100                 end
    0101                 [V.mcsMagSmooth] = sqrt(V.mcsEastSmooth .^2 + V.mcsNorthSmooth.^2);
    0102             end
    0103         case{'primary_zsd'}  %Smooths the primary velocity with zero secondary discharge definition
    0104             if Fr == 0 & Fc == 0
    0105                 V.vpSmooth = V.vp;
    0106             else
    0107                 if use_smooth2a
    0108                     [V.vpSmooth] = smooth2a(V.vp,Fr,Fc);
    0109                 else
    0110                     [V.vpSmooth] = nanmoving_average2(V.vp,Fr,Fc);
    0111                 end
    0112             end
    0113         case{'secondary_zsd'} %Smooths the secondary velocity with zero secondary discharge definition
    0114             if Fr == 0 & Fc == 0
    0115                 V.vsSmooth = V.vs;
    0116             else
    0117                 if use_smooth2a
    0118                     [V.vsSmooth] = smooth2a(V.vs,Fr,Fc);
    0119                 else
    0120                     [V.vsSmooth] = nanmoving_average2(V.vs,Fr,Fc);
    0121                 end
    0122             end
    0123         case{'primary_roz'}  %Smooths the primary velocity with Rozovskii definition
    0124             if Fr == 0 & Fc == 0
    0125                 V.Roz.upSmooth = V.Roz.up;
    0126             else
    0127                 if use_smooth2a
    0128                     [V.Roz.upSmooth] = smooth2a(V.Roz.up,Fr,Fc);
    0129                 else
    0130                     [V.Roz.upSmooth] = nanmoving_average2(V.Roz.up,Fr,Fc);
    0131                 end
    0132             end
    0133         case{'secondary_roz'} %Smooths the secondary velocity with Rozovskii definition
    0134             if Fr == 0 & Fc == 0
    0135                 V.Roz.usSmooth = V.Roz.us;
    0136             else
    0137                 if use_smooth2a
    0138                     [V.Roz.usSmooth] = smooth2a(V.Roz.us,Fr,Fc);
    0139                 else
    0140                     [V.Roz.usSmooth] = nanmoving_average2(V.Roz.us,Fr,Fc);
    0141                 end
    0142             end
    0143         case{'primary_roz_x'}  %Smooths the primary velocity with Rozovskii definition (downstream component)
    0144             if Fr == 0 & Fc == 0
    0145                 V.Roz.upxSmooth = V.Roz.upx;
    0146             else
    0147                 if use_smooth2a
    0148                     [V.Roz.upxSmooth] = smooth2a(V.Roz.upx,Fr,Fc);
    0149                 else
    0150                     [V.Roz.upxSmooth] = nanmoving_average2(V.Roz.upx,Fr,Fc);
    0151                 end
    0152             end
    0153         case{'primary_roz_y'}  %Smooths the primary velocity with Rozovskii definition (cross-stream component)
    0154             if Fr == 0 & Fc == 0
    0155                 V.Roz.upySmooth = V.Roz.upy;
    0156             else
    0157                 if use_smooth2a
    0158                     [V.Roz.upySmooth] = smooth2a(V.Roz.upy,Fr,Fc);
    0159                 else
    0160                     [V.Roz.upySmooth] = nanmoving_average2(V.Roz.upy,Fr,Fc);
    0161                 end
    0162             end
    0163         case{'secondary_roz_x'} %Smooths the secondary velocity with Rozovskii definition (downstream component)
    0164             if Fr == 0 & Fc == 0
    0165                 V.Roz.usxSmooth = V.Roz.usx;
    0166             else
    0167                 if use_smooth2a
    0168                     [V.Roz.usxSmooth] = smooth2a(V.Roz.usx,Fr,Fc);
    0169                 else
    0170                     [V.Roz.usxSmooth] = nanmoving_average2(V.Roz.usx,Fr,Fc);
    0171                 end
    0172             end
    0173         case{'secondary_roz_y'} %Smooths the secondary velocity with Rozovskii definition (cross-stream component)
    0174             if Fr == 0 & Fc == 0
    0175                 V.Roz.usySmooth = V.Roz.usy;
    0176             else
    0177                 if use_smooth2a
    0178                     [V.Roz.usySmooth] = smooth2a(V.Roz.usy,Fr,Fc);
    0179                 else
    0180                     [V.Roz.usySmooth] = nanmoving_average2(V.Roz.usy,Fr,Fc);
    0181                 end
    0182             end
    0183         case{'backscatter'} %Smooths the backscatter
    0184             if Fr == 0 & Fc == 0
    0185                 V.mcsBackSmooth = V.mcsBack;
    0186             else
    0187                 if use_smooth2a
    0188                     [V.mcsBackSmooth] = smooth2a(V.mcsBack,Fr,Fc);
    0189                 else
    0190                     [V.mcsBackSmooth] = nanmoving_average2(V.mcsBack,Fr,Fc);
    0191                 end
    0192
    0193             end
    0194         case{'flowangle'} %Smooths the flow direction
    0195             if Fr == 0 & Fc == 0
    0196                 V.mcsDirSmooth = V.mcsDir;
    0197             else
    0198                 %Must smooth velocity components and then compute flow direction
    0199                 if use_smooth2a
    0200                     V.mcsNorthSmooth  = smooth2a(V.mcsNorth,Fr,Fc);
    0201                     V.mcsEastSmooth   = smooth2a(V.mcsEast,Fr,Fc);
    0202                 else
    0203                     V.mcsNorthSmooth  = nanmoving_average2(V.mcsNorth,Fr,Fc);
    0204                     V.mcsEastSmooth   = nanmoving_average2(V.mcsEast,Fr,Fc);
    0205                 end
    0206                 V.mcsDirSmooth    = 90 - (atan2(V.mcsNorthSmooth, V.mcsEastSmooth))*180/pi; %Compute the atan from the velocity componentes, convert to radians, and rotate to north axis
    0207                 qindx = find(V.mcsDirSmooth < 0);
    0208                 if ~isempty(qindx)
    0209                     V.mcsDirSmooth(qindx) = V.mcsDirSmooth(qindx) + 360;  %Must add 360 deg to Quadrant 4 values as they are negative angles from the +y axis
    0210                 end
    0211             end
    0212             %     case{'vorticity_vw'} %Smooths the vorticity using U,V
    0213             %         if Fr == 0 & Fc == 0
    0214             %             V.vort_uv_smooth = V.vorticity_vw;
    0215             %         else
    0216             %             if use_smooth2a
    0217             %                 [V.vort_uv_smooth] = smooth2a(V.vorticity_vw,Fr,Fc);
    0218             %             else
    0219             %                 [V.vort_uv_smooth] = nanmoving_average2(V.vorticity_vw,Fr,Fc);
    0220             %             end
    0221             %         end
    0222             %     case{'vorticity_zsd'} %Smooths the vorticity using Zero Secondary Discharge definition
    0223             %         if Fr == 0 & Fc == 0
    0224             %             V.vort_zsd_smooth = V.vorticity_zsd;
    0225             %         else
    0226             %             if use_smooth2a
    0227             %                 [V.vort_zsd_smooth] = smooth2a(V.vorticity_zsd,Fr,Fc);
    0228             %             else
    0229             %                 [V.vort_zsd_smooth] = nanmoving_average2(V.vorticity_zsd,Fr,Fc);
    0230             %             end
    0231             %         end
    0232             %     case{'vorticity_roz'} %Smooths the vorticity using Zero Secondary Discharge definition
    0233             %         if Fr == 0 & Fc == 0
    0234             %             V.vort_roz_smooth = V.vorticity_roz;
    0235             %         else
    0236             %             if use_smooth2a
    0237             %                 [V.vort_roz_smooth] = smooth2a(V.vorticity_roz,Fr,Fc);
    0238             %             else
    0239             %                 [V.vort_roz_smooth] = nanmoving_average2(V.vorticity_roz,Fr,Fc);
    0240             %             end
    0241             %         end
    0242     end
    0243 end
    0244
    0245 %Smooths the vertical velocity (Must always do it for inclusion into
    0246 %secondary vectors)
    0247 if Fr == 0 & Fc == 0
    0248     V.wSmooth = V.w;
    0249 else
    0250     if use_smooth2a
    0251         [V.wSmooth] = smooth2a(V.w,Fr,Fc);
    0252     else
    0253         [V.wSmooth] = nanmoving_average2(V.w,Fr,Fc);
    0254     end
    0255 end
    0256
    0257 %% Close
    0258 warning on
    0259 %disp('Smoothing Completed')