1 |
function m = meanslices(fpat, ilist, ns, nps) |
2 |
|
3 |
% Function m = meanslices(ilist, ns, nps) |
4 |
% |
5 |
% |
6 |
% INPUTS |
7 |
% fpat string containing file pattern |
8 |
% ilist list of iteration values |
9 |
% ns number of slices to process at at time |
10 |
% nps number of values per slice |
11 |
% |
12 |
% OUTPUTS |
13 |
% m output array of dimension ns*nps |
14 |
|
15 |
|
16 |
if nargin ~= 4 |
17 |
disp('Error: wrong number of arguments') |
18 |
end |
19 |
|
20 |
deltatT = 1200; % model time step size (s) |
21 |
tavefreq = 259200; % averaging period (s) |
22 |
startDate = datenum(1992,1,1); % model integration starting date |
23 |
|
24 |
oldyear = -1; |
25 |
newyear = oldyear; |
26 |
|
27 |
is = 1; |
28 |
for is = 1:ns |
29 |
|
30 |
disp(['Processing slice: ' sprintf('%d',is)]); |
31 |
nm = 0; |
32 |
m = zeros(nps,1); |
33 |
|
34 |
i = 1; |
35 |
for i = 1:length(ilist) |
36 |
|
37 |
iter = ilist(i); |
38 |
str = datestr(startDate + (iter*deltatT-tavefreq/2)/60/60/24); |
39 |
dv = datevec(startDate + (iter*deltatT-tavefreq/2)/60/60/24); |
40 |
newyear = dv(1); |
41 |
|
42 |
fname = sprintf(fpat, ilist(i)); |
43 |
disp([' ' fname ' ' str]); |
44 |
fid = fopen(fname, 'r', 'ieee-be'); |
45 |
offset = (is - 1)*nps*4; |
46 |
fseek(fid, offset, 'bof'); |
47 |
tmp = fread(fid, nps, 'real*4'); |
48 |
fclose(fid); |
49 |
|
50 |
if (newyear ~= oldyear) && (nm > 0) |
51 |
fnout = sprintf('ave-%d', oldyear); |
52 |
disp([' ==> Writing file "' fnout '"']); |
53 |
fido = fopen(fnout, 'a', 'ieee-be'); |
54 |
% fseek(fido, offset, 'bof'); |
55 |
m = m / nm; |
56 |
fwrite(fido, m, 'real*4'); |
57 |
fclose(fido); |
58 |
m = zeros(nps,1); |
59 |
nm = 0; |
60 |
end |
61 |
m = m + tmp; |
62 |
nm = nm + 1; |
63 |
|
64 |
oldyear = newyear; |
65 |
|
66 |
end |
67 |
end |