| 1 | function [PsiB,ylat]=calcBolusPsiCube(varargin); | 
| 2 |  | 
| 3 | % [PsiB,ylat]=calcBolusPsiCube(d,g,GMform,blkFile); | 
| 4 | % | 
| 5 | % Compute eddy-induced streamfunction from Gent and McWilliams scheme | 
| 6 | % | 
| 7 | % Input arguments: | 
| 8 | %   The incoming field data (d) and grid data (g) must be in a structured | 
| 9 | %   array format (which is the format that comes from rdmnc): | 
| 10 | %       d       [Field data]  Kwx, Kwy (Skew flux form) or GM_PsiX, GM_PsiY (advective form) | 
| 11 | %       g       [Grid data ]  drF,rA,dxC,dyC,dxG,dyG,HFacW,HFacS | 
| 12 | %       GMform  [string]      GM form: 'Skew' or 'Advc' | 
| 13 | %       blkFile [file name]   Broken line file | 
| 14 | %       mask    [structure]   Optional: Mask field for computation per basin, it assumes that | 
| 15 | %                             maskW and maskS are provided in a structure | 
| 16 | % Output arguments: | 
| 17 | %       PsiB : bolus streamfunction at interface level (in Sv) | 
| 18 | %       ylat : meridional coordinate of PsiB | 
| 19 | % | 
| 20 | % Comments: | 
| 21 | %   -For Skew-flux form: | 
| 22 | %        PsiB computed from Kwx & Kwy divided by 2. | 
| 23 | %        first average Kwx and Kwy at u- and v-points: | 
| 24 | %        psiX=(rAc*Kwx)_i / (dXc*dYg) ; psiY=(rAc*Kwy)_j / dYc ; | 
| 25 | %        and then "zonally" average along broken lines | 
| 26 | %   -For Advective form: | 
| 27 | %        PsiB computed from PsiX and PsiY | 
| 28 | %        just need to "zonally" average along broken lines | 
| 29 | % | 
| 30 | %--------------------------------------------------------------------- | 
| 31 |  | 
| 32 | masking=0; | 
| 33 |  | 
| 34 | % Read input parameters. | 
| 35 | d       = varargin{1}; | 
| 36 | g       = varargin{2}; | 
| 37 | GMform  = varargin{3}; | 
| 38 | blkFile = varargin{4}; | 
| 39 | if length(varargin) == 5 | 
| 40 | mask = varargin{5}; | 
| 41 | masking = 1; | 
| 42 | end | 
| 43 |  | 
| 44 |  | 
| 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 46 | %                     Prepare grid stuff                                  % | 
| 47 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 48 |  | 
| 49 | nc = size(g.XC,2); | 
| 50 | nr = length(g.drF); | 
| 51 |  | 
| 52 | switch GMform | 
| 53 | case 'Skew' | 
| 54 | nt = size(d.GM_Kwx,4); | 
| 55 | case 'Advc' | 
| 56 | nt = size(d.GM_PsiX,4); | 
| 57 | end | 
| 58 |  | 
| 59 | %--- areas : | 
| 60 | ra  = g.rA; | 
| 61 | dxc = reshape(g.dxC(1:6*nc,1:nc),[6*nc*nc,1]); | 
| 62 | dyc = reshape(g.dyC(1:6*nc,1:nc),[6*nc*nc,1]); | 
| 63 | dxg = reshape(g.dxG(1:6*nc,1:nc),[6*nc*nc,1]); | 
| 64 | dyg = reshape(g.dyG(1:6*nc,1:nc),[6*nc*nc,1]); | 
| 65 |  | 
| 66 | rAu=dxc.*dyg; | 
| 67 | rAv=dyc.*dxg; | 
| 68 |  | 
| 69 | %--- masks : | 
| 70 | hw = reshape(g.HFacW(1:6*nc,1:nc,1:nr),[6*nc*nc,nr]); | 
| 71 | hs = reshape(g.HFacS(1:6*nc,1:nc,1:nr),[6*nc*nc,nr]); | 
| 72 | mskw=ceil(hw); mskw=min(1,mskw); | 
| 73 | msks=ceil(hs); msks=min(1,msks); | 
| 74 |  | 
| 75 | mskWloc = ones(6*nc*nc,1); | 
| 76 | mskSloc = ones(6*nc*nc,1); | 
| 77 | if masking == 1 | 
| 78 | mskWloc=reshape(mask.maskW(:,:,1),6*nc*nc,1); | 
| 79 | mskSloc=reshape(mask.maskS(:,:,1),6*nc*nc,1); | 
| 80 | end | 
| 81 |  | 
| 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 83 | %                     Read/Prepare GM fields                              % | 
| 84 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 85 | psiX_all = zeros(6*nc*nc,nr,nt); | 
| 86 | psiY_all = zeros(6*nc*nc,nr,nt); | 
| 87 |  | 
| 88 | switch GMform | 
| 89 | case 'Skew' | 
| 90 |  | 
| 91 | kwx_all = 0.5*d.GM_Kwx; | 
| 92 | kwy_all = 0.5*d.GM_Kwy; | 
| 93 |  | 
| 94 | for it = 1:nt | 
| 95 | kwx = kwx_all(:,:,:,it); | 
| 96 | kwy = kwy_all(:,:,:,it); | 
| 97 |  | 
| 98 | %-- K*ra + add 1 overlap : | 
| 99 | kwx = repmat(ra,[1 1 nr]).*kwx; | 
| 100 | kwy = repmat(ra,[1 1 nr]).*kwy; | 
| 101 | v6X = split_C_cub(kwx,1); | 
| 102 | v6Y = split_C_cub(kwy,1); | 
| 103 | k6x = v6X(:,[2:nc+1],:,:); | 
| 104 | k6y = v6Y([2:nc+1],:,:,:); | 
| 105 |  | 
| 106 | %----------------- | 
| 107 | clear v6X v6Y | 
| 108 | v6X = (k6x([2:nc+1],:,:,:) + k6x([1:nc],:,:,:))/2; | 
| 109 | v6Y = (k6y(:,[2:nc+1],:,:) + k6y(:,[1:nc],:,:))/2; | 
| 110 |  | 
| 111 | psiX = zeros(6*nc,nc,nr); | 
| 112 | psiY = zeros(6*nc,nc,nr); | 
| 113 |  | 
| 114 | for n = 1:6 | 
| 115 | is = 1+nc*(n-1);ie=nc*n; | 
| 116 | psiX([is:ie],[1:nc],[1:nr]) = v6X(:,:,:,n); | 
| 117 | psiY([is:ie],[1:nc],[1:nr]) = v6Y(:,:,:,n); | 
| 118 | end | 
| 119 |  | 
| 120 | psiX = reshape(psiX,6*nc*nc,nr); | 
| 121 | psiY = reshape(psiY,6*nc*nc,nr); | 
| 122 |  | 
| 123 | psiX_all(:,:,it) = mskw .* psiX ./ repmat(rAu,[1,nr]); | 
| 124 | psiY_all(:,:,it) = msks .* psiY ./ repmat(rAv,[1,nr]); | 
| 125 |  | 
| 126 | end | 
| 127 |  | 
| 128 | case 'Advc' | 
| 129 |  | 
| 130 | psiX_all = reshape(d.GM_PsiX(1:6*nc,:,:,1:nt),6*nc*nc,nr,nt); | 
| 131 | psiY_all = reshape(d.GM_PsiY(:,1:nc,:,1:nt)  ,6*nc*nc,nr,nt); | 
| 132 |  | 
| 133 | %if masking == 1 | 
| 134 | %   psiX_all = repmat(reshape(mask.maskW,6*nc*nc,1),[1 nr nt]) .* psiX_all; | 
| 135 | %   psiY_all = repmat(reshape(mask.maskS,6*nc*nc,1),[1 nr nt]) .* psiY_all; | 
| 136 | %end | 
| 137 |  | 
| 138 | otherwise | 
| 139 | disp(['C est Portnawak: GMform should be Skew or Advc: ',GMform]) | 
| 140 | end | 
| 141 |  | 
| 142 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 143 | %                     Zonally integrate along broken lines                % | 
| 144 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
| 145 |  | 
| 146 | load(blkFile); | 
| 147 | ydim = length(bkl_Ylat); | 
| 148 | ylat = [-90,bkl_Ylat,90]; | 
| 149 | ufac= rem(bkl_Flg,2); | 
| 150 | vfac= fix(bkl_Flg/2); | 
| 151 |  | 
| 152 | PsiB= zeros(ydim+2,nr+1,nt); | 
| 153 |  | 
| 154 | for it = 1:nt | 
| 155 | for k = 1:nr | 
| 156 | psixt=dyg.*psiX_all(:,k,it).*mskWloc; | 
| 157 | psiyt=dxg.*psiY_all(:,k,it).*mskSloc; | 
| 158 | for jl = 1:ydim | 
| 159 | ie = bkl_Npts(jl); | 
| 160 | PsiB(jl+1,k,it) = sum( ufac(1:ie,jl).*psixt(bkl_IJuv(1:ie,jl)) ... | 
| 161 | + vfac(1:ie,jl).*psiyt(bkl_IJuv(1:ie,jl)) ); | 
| 162 | end | 
| 163 | end | 
| 164 | end | 
| 165 | PsiB = 1e-6*PsiB; |