/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_misc/annualmean.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_misc/annualmean.m

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


Revision 1.1 - (hide annotations) (download)
Wed Apr 18 01:58:42 2012 UTC (13 years, 2 months ago) by gforget
Branch: MAIN
- function that computes annual mean (and anomalies from annual mean).

1 gforget 1.1 function [myMean,myAnom]=annualmean(myTimes,myFld,myYear);
2     %object: compute an annual mean for myYear (= 'first','last',n (int) or 'all')
3     % of a gcmfaces or array object (myFld) provided at myTimes (in years)
4     %input: myTimes,myFld,myYear
5     %output: myMean if the corresponding time mean
6     % (optional: myAnom is myFld-myMean)
7     %
8     %algorithm :
9     % - determine the number of records in one year (lYear)
10     % - determine the number of full years (nYears)
11     % - prepare list of records to average (depending on myYear, lYear and nYears)
12     % - in case when lYear<2 we use records as years
13     %
14     %by assumption:
15     % - the time dimension is last in myFld, and matches the length of myTimes
16    
17     %determine the number of records in one year (lYear)
18     tmp1=mean(myTimes(2:end)-myTimes(1:end-1));
19     lYear=round(1/tmp1);
20    
21     %in case when lYear<2 we use records as years
22     if ~(lYear>=2); lYear=1; myTimes=[1:length(myTimes)]; end;
23    
24     %determine the number of full years (nYears)
25     nYears=floor(length(myTimes)/lYear);
26    
27     %determine records that correspond to myYear, which
28     % may be 'first','last',n (an integer) or 'all'
29     if ischar(myYear);
30     if strcmp(myYear,'first');
31     recInAve=[1:lYear];
32     elseif strcmp(myYear,'last');
33     recInAve=[1:lYear]+(nYears-1)*lYear;
34     elseif strcmp(myYear,'all');
35     recInAve=[1:nYears*lYear];
36     else;
37     error('inconsistent specification of myYear');
38     end;
39     elseif (myYear>=1)&(myYear<=nYears);
40     recInAve=[1:lYear]+(myYear-1)*lYear;
41     else;
42     error('inconsistent specification of myYear');
43     end;
44     nRecs=length(recInAve);
45    
46     %determine last dimension, which need to match the length myTimes
47     if strcmp(class(myFld),'gcmfaces'); nDim=size(myFld{1}); else; nDim=size(myFld); end;
48     if nDim(end)~=length(myTimes); error('last dimensopn should match the length of myTimes'); end;
49     nDim=length(nDim); tt=''; for jj=1:nDim-1; tt=[tt ':,']; end;
50    
51     %compute time mean:
52     eval(['myMean=mean(myFld(' tt 'recInAve),nDim);']);
53    
54     %compute anomaly:
55     if nargout>1;
56     myAnom=myFld-repmat(myMean,[ones(1:nDim-1) length(myTimes)]);
57     end;
58    

  ViewVC Help
Powered by ViewVC 1.1.22