do_residual.html 6.22 KB
 Geoffrey Cowles committed Jul 10, 2013 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 `````` Description of do_residual
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.
`````` Pierre Cazenave committed Aug 10, 2016 117 ``````
Generated on Wed 10-Aug-2016 16:44:39 by m2html © 2005
`````` Geoffrey Cowles committed Jul 10, 2013 118 119 `````` ``````