/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_calc/calc_zonmean_T.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_calc/calc_zonmean_T.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.9 - (hide annotations) (download)
Mon Feb 8 16:42:33 2016 UTC (9 years, 5 months ago) by gforget
Branch: MAIN
CVS Tags: checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u
Changes since 1.8: +88 -39 lines
- calc_zonmean_T.m: add alternative methods, treat case of structure input.
- calc_mskmean_T.m (new): computes average over a region (mask).
- removed: calc_budget_mean_mask.m, calc_budget_mean_zonal.m

1 gforget 1.9 function [fldOut,X,Y,weightOut]=calc_zonmean_T(fldIn,method,fldType);
2     % CALC_ZONMEAN_T(budgIn,method,fldType)
3     % computes zonal average of fldIn (or its fields recursively).
4 gforget 1.4 %
5 gforget 1.9 % If method is 1 (default) then mskCedge (from mygrid.LATS_MASKS)
6     % and volume elements used; if method is 2 then mskCedge and surface
7     % elements are used; if method is -1 or -2 then mskC is used (from mygrid)
8     % instead of mskCedge to define the averaging footpring.
9     %
10     % If fldType is 'intensive' (default) then fldIn is mutliplied by
11     % RAC (method=2 or -2) or RAC.*hFacC*DRF (method=1 or -1).
12 gforget 1.1
13     global mygrid;
14    
15 gforget 1.9 if isempty(who('fldType')); fldType='intensive'; end;
16     if isempty(who('method')); method=1; end;
17    
18     if isa(fldIn,'struct');
19     list0=fieldnames(fldIn);
20     fldOut=[];
21     for vv=1:length(list0);
22     tmp1=getfield(fldIn,list0{vv});
23     if isa(tmp1,'gcmfaces');
24     [tmp2,X,Y,weightOut]=calc_zonmean_T(tmp1,method,fldType);
25     fldOut=setfield(fldOut,list0{vv},tmp2);
26     end;
27     end;
28     return;
29     end;
30    
31 gforget 1.1 %initialize output:
32 gforget 1.9 n3=max(size(fldIn.f1,3),1); n4=max(size(fldIn.f1,4),1);
33     fldOut=NaN*squeeze(zeros(length(mygrid.LATS_MASKS),n3,n4));
34     weightOut=NaN*squeeze(zeros(length(mygrid.LATS_MASKS),n3,n4));
35 gforget 1.1
36     %use array format to speed up computation below:
37 gforget 1.9 fldIn=convert2gcmfaces(fldIn);
38     n1=size(fldIn,1); n2=size(fldIn,2);
39     fldIn=reshape(fldIn,n1*n2,n3*n4);
40    
41     %set rac and hFacC according to method
42     if abs(method)==1;
43     rac=reshape(convert2gcmfaces(mygrid.RAC),n1*n2,1)*ones(1,n3*n4);
44     if n3==length(mygrid.RC);
45     hFacC=reshape(convert2gcmfaces(mygrid.hFacC),n1*n2,n3);
46     hFacC=repmat(hFacC,[1 n4]);
47     DRF=repmat(mygrid.DRF',[n1*n2 n4]);
48     else;
49     hFacC=reshape(convert2gcmfaces(mygrid.mskC(:,:,1)),n1*n2,1)*ones(1,n3*n4);
50     hFacC(isnan(hFacC))=0;
51     DRF=repmat(mygrid.DRF(1),[n1*n2 n3*n4]);
52     end;
53     weight=rac.*hFacC.*DRF;
54 gforget 1.7 else;
55 gforget 1.9 weight=mygrid.mskC(:,:,1).*mygrid.RAC;
56     weight=reshape(convert2gcmfaces(weight),n1*n2,1)*ones(1,n3*n4);
57 gforget 1.1 end;
58 gforget 1.9
59 gforget 1.6 %masked area only:
60 gforget 1.9 weight(isnan(fldIn))=0;
61     weight(isnan(weight))=0;
62     mask=weight; mask(weight~=0)=1;
63     fldIn(isnan(fldIn))=0;
64    
65     ny=length(mygrid.LATS_MASKS);
66     for iy=1:ny;
67    
68     if method>0;
69     %get list of points that form a zonal band:
70     mm=convert2gcmfaces(mygrid.LATS_MASKS(iy).mskCedge);
71     mm=find(~isnan(mm)&mm~=0);
72     else;
73     if iy>1&iy<ny;
74     tmpMin=0.5*(mygrid.LATS(iy-1)+mygrid.LATS(iy));
75     tmpMax=0.5*(mygrid.LATS(iy)+mygrid.LATS(iy+1));
76     elseif iy==1;
77     tmpMin=-Inf;
78     tmpMax=0.5*(mygrid.LATS(iy)+mygrid.LATS(iy+1));
79     elseif iy==ny;
80     tmpMin=0.5*(mygrid.LATS(iy-1)+mygrid.LATS(iy));
81     tmpMax=+Inf;
82     end;
83     mm=convert2gcmfaces(mygrid.YC>=tmpMin&mygrid.YC<tmpMax);
84     mm=find(~isnan(mm)&mm~=0);
85     end;
86 gforget 1.6
87 gforget 1.9 if strcmp(fldType,'intensive');
88     tmp1=nansum(fldIn(mm,:).*weight(mm,:),1)./nansum(weight(mm,:),1);
89     tmp2=nansum(weight(mm,:),1);
90     else;
91     tmp1=nansum(fldIn(mm,:).*mask(mm,:),1)./nansum(weight(mm,:),1);
92     tmp2=nansum(weight(mm,:),1);
93     end;
94    
95     %store:
96     fldOut(iy,:,:)=reshape(tmp1,n3,n4);
97     weightOut(iy,:,:)=reshape(tmp2,n3,n4);
98 gforget 1.7
99     end;
100 gforget 1.1
101 gforget 1.7 X=[]; Y=[];
102 gforget 1.9 if size(fldOut,2)==length(mygrid.RC);
103 gforget 1.7 X=mygrid.LATS*ones(1,length(mygrid.RC));
104     Y=ones(length(mygrid.LATS),1)*(mygrid.RC');
105 gforget 1.9 elseif size(fldOut,2)==1;
106 gforget 1.7 X=mygrid.LATS;
107     Y=ones(length(mygrid.LATS),1);
108     end;
109 gforget 1.1

  ViewVC Help
Powered by ViewVC 1.1.22