-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmon2ann.m
83 lines (67 loc) · 2.09 KB
/
mon2ann.m
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
function [Xann] = mon2ann(Xmon,smon,emon)
%MON2ANN Compute the annual average given monthly inputs.
% [Xann] = mon2ann(Xmon,smon,emon)
% 'Xmon' is the monthly input data, assumed to start in Jan and end
% in Dec, 'smon' is the starting month (e.g. Jan=1), 'emon' is the
% ending month (e.g. Dec=12). 'Xmon' can have dimensions
% (lat,lon,time) or (space,time), or (time) only.
%
% If the ending month is before the starting month (the average is
% taken beginning in one year and carrying over into the next, e.g.,
% Apr--Mar next year), one year's worth of data will be dropped to
% accomodate this choice. If 'smon' and 'emon' are empty, no
% averaging is performed.
%
% Nathan Steiger, October 2015
if isempty(smon) || isempty(emon)
% No averaging
Xann=Xmon;
elseif isvector(Xmon)
% Determine number of years of data
numMonths=length(Xmon);
yrs=numMonths/12;
% Correct number of yrs if end < start (e.g. avg over Jul-->Jun)
if smon > emon
emon=emon+12;
yrs=yrs-1;
end
Xann=zeros(yrs,1);
for q=1:yrs
Xann(q)=nansum(Xmon(smon:emon))/length(smon:emon);
smon=smon+12;
emon=emon+12;
end
elseif length(size(Xmon))==2
% Determine number of years of data
[rows,numMonths]=size(Xmon);
yrs=numMonths/12;
% Correct number of yrs if end < start (e.g. avg over Jul-->Jun)
if smon > emon
emon=emon+12;
yrs=yrs-1;
end
Xann=zeros(rows,yrs);
for q=1:yrs
Xann(:,q)=nansum(Xmon(:,smon:emon),2)/length(smon:emon);
smon=smon+12;
emon=emon+12;
end
elseif length(size(Xmon))==3
% Determine number of years of data
[rows,cols,numMonths]=size(Xmon);
yrs=numMonths/12;
% Correct number of yrs if end < start (e.g. avg over Jul-->Jun)
if smon > emon
emon=emon+12;
yrs=yrs-1;
end
Xann=zeros(rows,cols,yrs);
for q=1:yrs
Xann(:,:,q)=nansum(Xmon(:,:,smon:emon),3)/length(smon:emon);
smon=smon+12;
emon=emon+12;
end
else
error('Incorrect input for Xmon...')
end
end