1 |
function [data,xax,yax,zax,months,time,dim] = ... |
2 |
DiagLoadGradsData(Grads,dad,fln) |
3 |
|
4 |
% Read table file. |
5 |
tablfile = [dad,'/',Grads,'.tabl']; |
6 |
datafile = [dad,'/',Grads,'.data']; |
7 |
file = textread(tablfile,'%s','delimiter','\n','whitespace',''); |
8 |
for iline = 1:length(file) |
9 |
rem = file{iline}; tokens = {}; itoken = 0; |
10 |
while isstr(rem) |
11 |
[token,rem] = strtok(rem); itoken = itoken + 1; |
12 |
if ~isempty(token), tokens{itoken} = token; end |
13 |
end |
14 |
|
15 |
if isequal(tokens{1},'XDEF') |
16 |
if ~isequal(tokens{3},'LINEAR') |
17 |
error('Unable to deal with nonlinear grads x-axis data!'); |
18 |
end |
19 |
num = str2num(tokens{2}); |
20 |
ini = str2num(tokens{4}); |
21 |
inc = str2num(tokens{5}); |
22 |
xax = [ini:inc:ini+(num-1)*inc]; |
23 |
if min(xax) >= 0 && max(xax) > 180 |
24 |
xax = xax - 180; |
25 |
end |
26 |
nx = length(xax); |
27 |
end |
28 |
|
29 |
if isequal(tokens{1},'YDEF') |
30 |
if ~isequal(tokens{3},'LINEAR') |
31 |
error('Unable to deal with nonlinear grads y-axis data!'); |
32 |
end |
33 |
num = str2num(tokens{2}); |
34 |
ini = str2num(tokens{4}); |
35 |
inc = str2num(tokens{5}); |
36 |
yax = [ini:inc:ini+(num-1)*inc]; ny = length(yax); |
37 |
end |
38 |
|
39 |
if isequal(tokens{1},'ZDEF') |
40 |
if isequal(tokens{2},'1') |
41 |
zax = [0]; zax_found = 1; dim = 2; |
42 |
else |
43 |
if ~isequal(tokens{3},'LINEAR') |
44 |
error('Unable to deal with nonlinear grads z-axis data!'); |
45 |
end |
46 |
num = str2num(tokens{2}); |
47 |
ini = str2num(tokens{4}); |
48 |
inc = str2num(tokens{5}); |
49 |
zax = [ini:inc:ini+(num-1)*inc]; dim = 3; |
50 |
end |
51 |
nz = length(zax); |
52 |
end |
53 |
|
54 |
if isequal(tokens{1},'TDEF'), ini=tokens{4}; |
55 |
if ~isequal(tokens{3},'LINEAR') |
56 |
error('Currently unable to deal with nonlinear grads z-axis data!'); |
57 |
end |
58 |
if ~isequal(tokens{5},'1mo') |
59 |
error('Currently unable to deal with non monthly mean grads data!'); |
60 |
end |
61 |
if length(ini) == 13 |
62 |
monchar=ini(9:11); |
63 |
end |
64 |
if isequal(monchar,'JAN'), inimonth = 1; |
65 |
elseif isequal(monchar,'FEB'), inimonth = 2; |
66 |
elseif isequal(monchar,'MAR'), inimonth = 3; |
67 |
elseif isequal(monchar,'APR'), inimonth = 4; |
68 |
elseif isequal(monchar,'MAY'), inimonth = 5; |
69 |
elseif isequal(monchar,'JUN'), inimonth = 6; |
70 |
elseif isequal(monchar,'JUL'), inimonth = 7; |
71 |
elseif isequal(monchar,'AUG'), inimonth = 8; |
72 |
elseif isequal(monchar,'SEP'), inimonth = 9; |
73 |
elseif isequal(monchar,'OCT'), inimonth = 10; |
74 |
elseif isequal(monchar,'NOV'), inimonth = 11; |
75 |
elseif isequal(monchar,'DEC'), inimonth = 12; end |
76 |
num = str2num(tokens{2}); |
77 |
months=[inimonth:num]; time=months/12; nt = length(months); |
78 |
|
79 |
end |
80 |
|
81 |
if isequal(tokens{1},'VARS') |
82 |
nv = str2num(tokens{2}); |
83 |
VARSline = iline; |
84 |
end |
85 |
|
86 |
if isequal(tokens{1},fln) |
87 |
FIELDline = iline; |
88 |
index = iline - VARSline; |
89 |
end |
90 |
|
91 |
if isequal(tokens{1},'UNDEF') |
92 |
undef = str2num(tokens{2}); |
93 |
end |
94 |
|
95 |
end |
96 |
|
97 |
% Verify that things are there. |
98 |
parameters = {'xax' ,'yax' ,'zax' ,'time','nv' ,'undef','index'}; |
99 |
GRADSnames = {'XDEF','YDEF','ZDEF','TDEF','VARS','UNDEF',fln }; |
100 |
for ii = 1:length(parameters) |
101 |
try |
102 |
eval([parameters{ii},';']); |
103 |
catch |
104 |
error(['GRADS information not found: ',GRADSnames{ii}]); |
105 |
end |
106 |
end |
107 |
|
108 |
% Read in grads data using axis infomation. |
109 |
tol = 1e-5; |
110 |
fid=fopen(datafile,'r','b'); |
111 |
data = fread(fid,'real*4'); |
112 |
fclose(fid); |
113 |
data = reshape(data,[nx,ny,nz,nv,nt]); |
114 |
data = squeeze(data(:,:,:,index,:)); |
115 |
data( abs((data-undef)/undef) < tol ) = NaN; |