1 |
function data = DiagAverage(data,fln,avg,absmonths,ddf,Dim); |
2 |
|
3 |
% Function: DiagAverage |
4 |
% Author: Daniel Enderton |
5 |
% |
6 |
% Input Fields: |
7 |
% |
8 |
% Field Type (Brief) Description |
9 |
% ----------------------------------------------------------------------- |
10 |
% data array Data |
11 |
% fln string Field name |
12 |
% avg string Averaging scheme ('Ann','DJF', 'JJA',...) |
13 |
% absmonths array Absolute months (time axis of data) |
14 |
% |
15 |
% Output Fields: |
16 |
% |
17 |
% Field Type (Brief) Description |
18 |
% ----------------------------------------------------------------------- |
19 |
% data array Averaged data. |
20 |
% |
21 |
% Descripton: |
22 |
% This function averages the data. The field to average is defined by |
23 |
% 'fln', and the loaded, pre-averaged data is 'data'. The field |
24 |
% 'absmonths' is the time axis given in months (1,13,25,... are assumed |
25 |
% to be Janurary), and is returned in 'datatime' so that there can be a |
26 |
% time axis for every experiment / configuration setting. It is |
27 |
% important to remember that this function assumed monthly data! |
28 |
|
29 |
% Load parameters (here only general parameters). |
30 |
DiagGenParam; |
31 |
|
32 |
% Calcuate months of year. |
33 |
months = mod(absmonths-1,12)+1; |
34 |
|
35 |
|
36 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
37 |
% Take monthly average of data % |
38 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
39 |
|
40 |
% Take monthly average of data. The result has the same dimensions of the |
41 |
% time-independent field (2D or 3D), and the last dimension is the time, |
42 |
% which has a length of 12, running from 1 to 12, representing January |
43 |
% through December respectively. If there is no data for a months, that |
44 |
% months values are set to NaN. |
45 |
if isequal(avg,'Non') |
46 |
temp = data; |
47 |
elseif isequal(avg,'Avg') |
48 |
if isequal(Dim,2), temp = meanovernan(data,3); % Dimension option overrides. |
49 |
elseif isequal(Dim,3), temp = meanovernan(data,4); |
50 |
elseif ismember(fln,fields2D), temp = meanovernan(data,3); |
51 |
elseif ismember(fln,fields3D), temp = meanovernan(data,4); |
52 |
else |
53 |
error('Field not accounted for in ''fields2D'' of ''fields3D'' and dimension not specified!'); |
54 |
end |
55 |
else |
56 |
for imon = 1:12 |
57 |
if ismember(fln,fields2D) || isequal(Dim,2) |
58 |
if isempty(find(months == imon)) |
59 |
dataMonAvg(:,:,imon) = NaN * zeros(size(data(:,:,1))); |
60 |
else |
61 |
dataMonAvg(:,:,imon) = meanovernan(data(:,:,months == imon),3); |
62 |
end |
63 |
elseif ismember(fln,fields3D) || isequal(Dim,3) |
64 |
if isempty(find(months == imon)) |
65 |
dataMonAvg(:,:,:,imon) = NaN * zeros(size(data(:,:,:,1))); |
66 |
else |
67 |
dataMonAvg(:,:,:,imon) = meanovernan(data(:,:,:,months == imon),4); |
68 |
end |
69 |
else |
70 |
error('Field not accounted for in ''fields2D'' of ''fields3D'''); |
71 |
end |
72 |
end |
73 |
end |
74 |
|
75 |
|
76 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
77 |
% Average data % |
78 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
79 |
|
80 |
% If 'avg' (the averaging scheme) is time-sequential, merely return the |
81 |
% data, if it is time-year, merely return the monthly averaged data. Note |
82 |
% that these options are for a contour plot of a property which is only a |
83 |
% function of latitude, that is you can only use this for a zonally |
84 |
% averaged to i=# slice of a 2D field. |
85 |
if ismember(avg,{'Non','Avg'}) |
86 |
dummy = 1; |
87 |
elseif isequal(avg,'Tse') |
88 |
temp = data; |
89 |
elseif isequal(avg,'Tyr') |
90 |
temp = dataMonAvg; |
91 |
temp(:,:,13) = temp(:,:,1); |
92 |
|
93 |
|
94 |
|
95 |
% Average along certain month combinations, or just select a month itself. |
96 |
% Here we pick the month index number(s) based on the averaging parameter, |
97 |
% and then select those indecies of the monthly averaged data and take a |
98 |
% mean over the time axis. |
99 |
else |
100 |
if isequal(avg,'Jan'), index = [1]; |
101 |
elseif isequal(avg,'Feb'), index = [2]; |
102 |
elseif isequal(avg,'Mar'), index = [3]; |
103 |
elseif isequal(avg,'Apr'), index = [4]; |
104 |
elseif isequal(avg,'May'), index = [5]; |
105 |
elseif isequal(avg,'Jun'), index = [6]; |
106 |
elseif isequal(avg,'Jul'), index = [7]; |
107 |
elseif isequal(avg,'Aug'), index = [8]; |
108 |
elseif isequal(avg,'Sep'), index = [9]; |
109 |
elseif isequal(avg,'Oct'), index = [10]; |
110 |
elseif isequal(avg,'Nov'), index = [11]; |
111 |
elseif isequal(avg,'Dec'), index = [12]; |
112 |
elseif isequal(avg,'DJF'), index = [12,1,2]; |
113 |
elseif isequal(avg,'MAM'), index = [3,4,5]; |
114 |
elseif isequal(avg,'JJA'), index = [6,7,8]; |
115 |
elseif isequal(avg,'SON'), index = [9,10,11]; |
116 |
elseif isequal(avg,'Ann'), index = [1:12]; |
117 |
else |
118 |
error(['Unaccounted for averaging scheme: ',avg]); |
119 |
end |
120 |
|
121 |
% Make an array 'newindex' which has all the values as in 'index', but |
122 |
% with the emission those months with no data (all NaNs). Note that |
123 |
% sometimes the edges of the U and V velocity fields can be NaN from |
124 |
% the interpolation, so check for NaN months a few horizontal indecies |
125 |
% into the array. |
126 |
umonths = unique(months); |
127 |
newindex = umonths(ismember(umonths,index)); |
128 |
|
129 |
% Cast a warning or error for all or some missing data, respectively. |
130 |
if isempty(newindex) |
131 |
error(['No data for the averaging sceheme: ',avg,]); |
132 |
elseif length(newindex) ~= length(index) |
133 |
disp(['***Warning*** Missing data for averaging scheme: ',avg]); |
134 |
disp([' Month(s) with data: ',mat2str(newindex)]); |
135 |
disp([' Month(s) for averaging: ',mat2str(index)]); |
136 |
end |
137 |
|
138 |
% Perform time averaging. |
139 |
if ismember(fln,fields2D) || isequal(Dim,2) |
140 |
temp = meanovernan(dataMonAvg(:,:,newindex),3); |
141 |
elseif ismember(fln,fields3D) || isequal(Dim,3) |
142 |
temp = meanovernan(dataMonAvg(:,:,:,newindex),4); |
143 |
end |
144 |
|
145 |
end |
146 |
|
147 |
|
148 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
149 |
% Prepare for output % |
150 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
151 |
|
152 |
data = temp; |