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 |
% Take monthly average of data % |
37 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
38 |
|
39 |
% Take monthly average of data. The result has the same dimensions of the |
40 |
% time-independent field (2D or 3D), and the last dimension is the time, |
41 |
% which has a length of 12, running from 1 to 12, representing January |
42 |
% through December respectively. If there is no data for a months, that |
43 |
% months values are set to NaN. |
44 |
if isequal(avg,'Non') |
45 |
temp = data; |
46 |
elseif isequal(avg,'Avg') |
47 |
if isequal(Dim,2), temp = meanovernan(data,3); % Dimension option overrides. |
48 |
elseif isequal(Dim,3), temp = meanovernan(data,4); |
49 |
elseif ismember(fln,fields2D), temp = meanovernan(data,3); |
50 |
elseif ismember(fln,fields3D), temp = meanovernan(data,4); |
51 |
else |
52 |
error('Field not accounted for in ''fields2D'' of ''fields3D'' and dimension not specified!'); |
53 |
end |
54 |
else |
55 |
for imon = 1:12 |
56 |
if ismember(fln,fields2D) || isequal(Dim,2) |
57 |
if isempty(find(months == imon)) |
58 |
dataMonAvg(:,:,imon) = NaN * zeros(size(data(:,:,1))); |
59 |
else |
60 |
inds=find(months==imon); |
61 |
dataMonAvg(:,:,imon) = meanovernan(data(:,:,absmonths(inds)),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 |
inds=find(months==imon); |
68 |
dataMonAvg(:,:,:,imon) = meanovernan(data(:,:,:,absmonths(inds)),4); |
69 |
end |
70 |
else |
71 |
error('Field not accounted for in ''fields2D'' of ''fields3D'''); |
72 |
end |
73 |
end |
74 |
end |
75 |
|
76 |
|
77 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
78 |
% Average data % |
79 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
80 |
|
81 |
% If 'avg' (the averaging scheme) is time-sequential, merely return the |
82 |
% data, if it is time-year, merely return the monthly averaged data. Note |
83 |
% that these options are for a contour plot of a property which is only a |
84 |
% function of latitude, that is you can only use this for a zonally |
85 |
% averaged to i=# slice of a 2D field. |
86 |
if ismember(avg,{'Non','Avg'}) |
87 |
dummy = 1; |
88 |
elseif isequal(avg,'Tse') |
89 |
temp = data; |
90 |
elseif isequal(avg,'Tyr') |
91 |
temp = dataMonAvg; |
92 |
temp(:,:,13) = temp(:,:,1); |
93 |
|
94 |
|
95 |
|
96 |
% Average along certain month combinations, or just select a month itself. |
97 |
% Here we pick the month index number(s) based on the averaging parameter, |
98 |
% and then select those indecies of the monthly averaged data and take a |
99 |
% mean over the time axis. |
100 |
else |
101 |
if isequal(avg,'Jan'), index = [1]; |
102 |
elseif isequal(avg,'Feb'), index = [2]; |
103 |
elseif isequal(avg,'Mar'), index = [3]; |
104 |
elseif isequal(avg,'Apr'), index = [4]; |
105 |
elseif isequal(avg,'May'), index = [5]; |
106 |
elseif isequal(avg,'Jun'), index = [6]; |
107 |
elseif isequal(avg,'Jul'), index = [7]; |
108 |
elseif isequal(avg,'Aug'), index = [8]; |
109 |
elseif isequal(avg,'Sep'), index = [9]; |
110 |
elseif isequal(avg,'Oct'), index = [10]; |
111 |
elseif isequal(avg,'Nov'), index = [11]; |
112 |
elseif isequal(avg,'Dec'), index = [12]; |
113 |
elseif isequal(avg,'DJF'), index = [12,1,2]; |
114 |
elseif isequal(avg,'MAM'), index = [3,4,5]; |
115 |
elseif isequal(avg,'JJA'), index = [6,7,8]; |
116 |
elseif isequal(avg,'SON'), index = [9,10,11]; |
117 |
elseif isequal(avg,'Ann'), index = [1:12]; |
118 |
else |
119 |
error(['Unaccounted for averaging scheme: ',avg]); |
120 |
end |
121 |
|
122 |
% Make an array 'newindex' which has all the values as in 'index', but |
123 |
% with the emission those months with no data (all NaNs). Note that |
124 |
% sometimes the edges of the U and V velocity fields can be NaN from |
125 |
% the interpolation, so check for NaN months a few horizontal indecies |
126 |
% into the array. |
127 |
umonths = unique(months); |
128 |
newindex = umonths(ismember(umonths,index)); |
129 |
|
130 |
% Cast a warning or error for all or some missing data, respectively. |
131 |
if isempty(newindex) |
132 |
error(['No data for the averaging sceheme: ',avg,]); |
133 |
elseif length(newindex) ~= length(index) |
134 |
disp(['***Warning*** Missing data for averaging scheme: ',avg]); |
135 |
disp([' Month(s) with data: ',mat2str(newindex)]); |
136 |
disp([' Month(s) for averaging: ',mat2str(index)]); |
137 |
end |
138 |
|
139 |
% Perform time averaging. |
140 |
if ismember(fln,fields2D) || isequal(Dim,2) |
141 |
temp = meanovernan(dataMonAvg(:,:,newindex),3); |
142 |
elseif ismember(fln,fields3D) || isequal(Dim,3) |
143 |
temp = meanovernan(dataMonAvg(:,:,:,newindex),4); |
144 |
end |
145 |
|
146 |
end |
147 |
|
148 |
|
149 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
150 |
% Prepare for output % |
151 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
152 |
|
153 |
data = temp; |