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 length(myTimes)>1; |
49 |
if nDim(end)~=length(myTimes); |
50 |
error('last dimension should match the length of myTimes'); |
51 |
end; |
52 |
nDim=length(nDim); tt=''; for jj=1:nDim-1; tt=[tt ':,']; end; |
53 |
else; |
54 |
nDim=length(nDim)+1; tt=''; for jj=1:nDim-1; tt=[tt ':,']; end; |
55 |
end; |
56 |
|
57 |
%compute time mean: |
58 |
eval(['myMean=mean(myFld(' tt 'recInAve),nDim);']); |
59 |
|
60 |
%compute anomaly: |
61 |
if nargout>1; |
62 |
myAnom=myFld-repmat(myMean,[ones(1:nDim-1) length(myTimes)]); |
63 |
end; |
64 |
|