Home > utilities > do_residual.m

# do_residual

## PURPOSE

DO_RESIDUAL Takes the u and v vectors of a model output and calculates

## SYNOPSIS

function [rDir,rMag,uRes,vRes]=do_residual(u,v,dt)

## DESCRIPTION

``` DO_RESIDUAL Takes the u and v vectors of a model output and calculates
the long-term direction and magnitude for that data.

[RDIR,RMAG,URES,VRES]=DO_RESIDUAL(U,V,DT) takes the residual direction
(RDIR) and magnitude RMAG) of the data in U and V sampled at interval
DT. URES and UDIR are the summed U and V positions (the raw data for a
progresive vector diagram). Direction output is in degrees, vector
magnitude in units/s.

Pierre Cazenave PML 20/03/2012.```

## CROSS-REFERENCE INFORMATION

This function calls:
This function is called by:
• do_residual_plot Take the output of do_residual and plot as a vector figure. Summarises a

## SOURCE CODE

```0001 function [rDir,rMag,uRes,vRes]=do_residual(u,v,dt)
0002 % DO_RESIDUAL Takes the u and v vectors of a model output and calculates
0003 % the long-term direction and magnitude for that data.
0004 %
0005 %   [RDIR,RMAG,URES,VRES]=DO_RESIDUAL(U,V,DT) takes the residual direction
0006 %   (RDIR) and magnitude RMAG) of the data in U and V sampled at interval
0007 %   DT. URES and UDIR are the summed U and V positions (the raw data for a
0008 %   progresive vector diagram). Direction output is in degrees, vector
0009 %   magnitude in units/s.
0010 %
0011 % Pierre Cazenave PML 20/03/2012.
0012 %
0013
0014 % Loosely based on my original dfsuResidual.m and processResidual function
0015 % for DHI's MIKE21 software, which in turn were based on Dave Lambkin's
0016 % residual analysis scripts.
0017 %
0018 % TODO: Make it possible to specify the average for all layers (i.e. NZ is
0019 % all layers).
0020
0021 % Let's do it...
0022
0023 toSecFactor=24*60*60;
0024
0025 nElements=size(u,1);
0026 nLayers=size(u,2);
0027 nTimeSteps=size(u,3);
0028
0029 % Some tidal assumptions. This will need to change in areas in which the
0030 % diurnal tide dominates over the semidiurnal.
0031 tideCycle=(12+(25/60))/24;
0032 tideWindow=ceil(tideCycle/dt);
0033 tideDuration=(mean((dt*nTimeSteps)-tideCycle)-mean(tideCycle))*toSecFactor;
0034
0035 % Preallocate outputs.
0036 uRes=zeros(nElements,nLayers,nTimeSteps);
0037 vRes=zeros(nElements,nLayers,nTimeSteps);
0038 uSum=nan(nElements,nTimeSteps,nLayers);
0039 vSum=nan(nElements,nTimeSteps,nLayers);
0040 uStart=nan(nElements,nLayers);
0041 vStart=nan(nElements,nLayers);
0042 uEnd=nan(nElements,nLayers);
0043 vEnd=nan(nElements,nLayers);
0044
0045 for hh=1:nLayers
0046     uSum(:,:,hh)=cumsum(squeeze(u(:,hh,:)),2);
0047     vSum(:,:,hh)=cumsum(squeeze(v(:,hh,:)),2);
0048     for ii=1:nTimeSteps;
0049         uRes(:,hh,ii)=uRes(:,hh,ii)+(uSum(:,ii,hh).*(dt*toSecFactor));
0050         vRes(:,hh,ii)=vRes(:,hh,ii)+(vSum(:,ii,hh).*(dt*toSecFactor));
0051     end
0052     uStart(:,hh)=mean(squeeze(uRes(:,hh,1:tideWindow)),2);
0053     vStart(:,hh)=mean(squeeze(vRes(:,hh,1:tideWindow)),2);
0054     uEnd(:,hh)=mean(squeeze(uRes(:,hh,end-tideWindow:end)),2);
0055     vEnd(:,hh)=mean(squeeze(vRes(:,hh,end-tideWindow:end)),2);
0056 end
0057
0058 uDiff=uEnd-uStart;
0059 vDiff=vEnd-vStart;
0060
0061 % Calculate direction and magnitude.
0062 rDir=atan2(uDiff,vDiff)*(180/pi); % in degrees.
0063 rMag=sqrt(uDiff.^2+vDiff.^2)/tideDuration; % in units/s.```

Generated on Wed 10-Aug-2016 16:44:39 by m2html © 2005