1 |
enderton |
1.3 |
function [S] = rdmnc_mod2(varargin) |
2 |
|
|
|
3 |
|
|
% Usage: |
4 |
|
|
% S=RDMNC(FILE1,FILE2,...) |
5 |
|
|
% S=RDMNC(FILE1,...,ITER) |
6 |
|
|
% S=RDMNC(FILE1,...,'VAR1','VAR2',...) |
7 |
|
|
% S=RDMNC(FILE1,...,'VAR1','VAR2',...,ITER) |
8 |
|
|
% |
9 |
|
|
% Input: |
10 |
|
|
% FILE1 Either a single file name (e.g. 'state.nc') or a wild-card |
11 |
|
|
% strings expanding to a group of file names (e.g. 'state.*.nc'). |
12 |
|
|
% There are no assumptions about suffices (e.g. 'state.*' works). |
13 |
|
|
% VAR1 Model variable names as written in the MNC/netcdf file. |
14 |
|
|
% ITER Vector of iterations in the MNC/netcdf files, not model time. |
15 |
adcroft |
1.1 |
% |
16 |
enderton |
1.3 |
% Output: |
17 |
|
|
% S Structure with fields corresponding to 'VAR1', 'VAR2', ... |
18 |
adcroft |
1.1 |
% |
19 |
enderton |
1.3 |
% Description: |
20 |
|
|
% This function is a rudimentary wrapper for joining and reading netcdf |
21 |
|
|
% files produced by MITgcm. It does not give the same flexibility as |
22 |
|
|
% opening the netcdf files directly using netcdf(), but is useful for |
23 |
|
|
% quick loading of entire model fields which are distributed in multiple |
24 |
|
|
% netcdf files. |
25 |
adcroft |
1.1 |
% |
26 |
enderton |
1.3 |
% Example: |
27 |
|
|
% >> S=rdmnd('state.*','XC','YC','T'); |
28 |
|
|
% >> imagesc( S.XC, S.YC, S.T(:,:,1)' ); |
29 |
adcroft |
1.1 |
% |
30 |
enderton |
1.4 |
% Author: Alistair Adcroft |
31 |
enderton |
1.3 |
% Modifications: Daniel Enderton |
32 |
adcroft |
1.1 |
|
33 |
enderton |
1.3 |
% Initializations |
34 |
adcroft |
1.1 |
file={}; |
35 |
enderton |
1.3 |
filepaths={}; |
36 |
adcroft |
1.1 |
files={}; |
37 |
|
|
varlist={}; |
38 |
enderton |
1.3 |
iters=[]; |
39 |
adcroft |
1.1 |
|
40 |
|
|
% Process function arguments |
41 |
|
|
for iarg=1:nargin; |
42 |
enderton |
1.3 |
arg=varargin{iarg}; |
43 |
|
|
if ischar(arg) |
44 |
|
|
if isempty(dir(char(arg))) |
45 |
|
|
varlist{end+1}=arg; |
46 |
|
|
else |
47 |
|
|
file{end+1}=arg; |
48 |
|
|
end |
49 |
|
|
else |
50 |
|
|
if isempty(iters) |
51 |
|
|
iters=arg; |
52 |
|
|
else |
53 |
|
|
error(['The only allowed numeric argument is iterations',... |
54 |
|
|
' to read in as a vector for the last arguement']); |
55 |
|
|
end |
56 |
|
|
end |
57 |
adcroft |
1.1 |
end |
58 |
|
|
|
59 |
|
|
% Create list of filenames |
60 |
|
|
for eachfile=file |
61 |
enderton |
1.3 |
filepathtemp=eachfile{:}; |
62 |
|
|
indecies = find(filepathtemp=='/'); |
63 |
|
|
if ~isempty(indecies) |
64 |
|
|
filepathtemp = filepathtemp(1:indecies(end)); |
65 |
|
|
else |
66 |
|
|
filepathtemp = ''; |
67 |
|
|
end |
68 |
|
|
expandedEachFile=dir(char(eachfile{1})); |
69 |
|
|
for i=1:size(expandedEachFile,1); |
70 |
|
|
if expandedEachFile(i).isdir==0 |
71 |
|
|
files{end+1}=expandedEachFile(i).name; |
72 |
|
|
filepaths{end+1}=filepathtemp; |
73 |
|
|
end |
74 |
|
|
end |
75 |
adcroft |
1.1 |
end |
76 |
|
|
|
77 |
|
|
|
78 |
enderton |
1.3 |
% If iterations unspecified, open all the files and make list of all the |
79 |
|
|
% iterations that appear, use this iterations list for data extraction. |
80 |
|
|
if isempty(iters) |
81 |
|
|
iters = []; |
82 |
|
|
for ieachfile=1:length(files) |
83 |
|
|
eachfile = [filepaths{ieachfile},files{ieachfile}]; |
84 |
|
|
nc=netcdf(char(eachfile),'read'); |
85 |
enderton |
1.4 |
nciters = nc{'iter'}(:); |
86 |
|
|
if isempty(nciters), nciters = nc{'T'}(:); end |
87 |
|
|
iters = [iters,nciters]; |
88 |
enderton |
1.3 |
close(nc); |
89 |
|
|
end |
90 |
|
|
iters = unique(iters); |
91 |
adcroft |
1.1 |
end |
92 |
|
|
|
93 |
enderton |
1.3 |
% Cycle through files for data extraction. |
94 |
|
|
S.attributes=[]; |
95 |
|
|
for ieachfile=1:length(files) |
96 |
|
|
eachfile = [filepaths{ieachfile},files{ieachfile}]; |
97 |
|
|
nc=netcdf(char(eachfile),'read'); |
98 |
|
|
S=rdmnc_local(nc,varlist,iters,S); |
99 |
|
|
close(nc); |
100 |
adcroft |
1.1 |
end |
101 |
|
|
|
102 |
enderton |
1.3 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
103 |
|
|
% Local functions % |
104 |
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
105 |
adcroft |
1.1 |
|
106 |
enderton |
1.3 |
function [A] = read_att(nc); |
107 |
|
|
allatt=ncnames(att(nc)); |
108 |
|
|
if ~isempty(allatt) |
109 |
|
|
for attr=allatt; |
110 |
|
|
A.(char(attr))=nc.(char(attr))(:); |
111 |
|
|
end |
112 |
|
|
else |
113 |
|
|
A = 'none'; |
114 |
|
|
end |
115 |
adcroft |
1.1 |
|
116 |
enderton |
1.3 |
function [i0,j0,fn] = findTileOffset(S); |
117 |
|
|
fn=0; |
118 |
|
|
if isfield(S,'attributes') & isfield(S.attributes,'global') |
119 |
|
|
G=S.attributes.global; |
120 |
|
|
tn=G.tile_number; |
121 |
|
|
snx=G.sNx; sny=G.sNy; nsx=G.nSx; nsy=G.nSy; npx=G.nPx; npy=G.nPy; |
122 |
|
|
ntx=nsx*npx;nty=nsy*npy; |
123 |
|
|
gbi=mod(tn-1,ntx); gbj=(tn-gbi-1)/ntx; |
124 |
|
|
i0=snx*gbi; j0=sny*gbj; |
125 |
|
|
if isfield(S.attributes.global,'exch2_myFace') |
126 |
|
|
fn=G.exch2_myFace; |
127 |
|
|
end |
128 |
|
|
else |
129 |
|
|
i0=0;j0=0; |
130 |
|
|
end |
131 |
|
|
%[snx,sny,nsx,nsy,npx,npy,ntx,nty,i0,j0,fn]; |
132 |
|
|
|
133 |
|
|
function [S] = rdmnc_local(nc,varlist,iters,S) |
134 |
|
|
|
135 |
|
|
fiter = nc{'iter'}(:); % File iterations present |
136 |
enderton |
1.4 |
if isempty(fiter), fiter = nc{'T'}(:); end |
137 |
enderton |
1.3 |
[fii,dii] = ismember(fiter,iters); fii = find(fii); % File iteration index |
138 |
|
|
dii = dii(find(dii ~= 0)); % Data interation index |
139 |
enderton |
1.4 |
|
140 |
enderton |
1.3 |
% No variables specified? Default to all |
141 |
|
|
if isempty(varlist), varlist=ncnames(var(nc)); end |
142 |
enderton |
1.4 |
|
143 |
enderton |
1.3 |
% Attributes for structure |
144 |
|
|
if iters>0; S.iters_read_from_file=iters; end |
145 |
|
|
S.attributes.global=read_att(nc); |
146 |
enderton |
1.4 |
|
147 |
enderton |
1.3 |
% Read variable data |
148 |
enderton |
1.4 |
for ivar=1:size(varlist,2) |
149 |
|
|
|
150 |
enderton |
1.3 |
cvar=char(varlist{ivar}); |
151 |
|
|
if isempty(nc{cvar}) |
152 |
|
|
disp(['No such variable ''',cvar,''' in MNC file ',name(nc)]); |
153 |
|
|
continue |
154 |
|
|
end |
155 |
|
|
|
156 |
|
|
dims=ncnames(dim(nc{cvar})); % Dimensions |
157 |
|
|
|
158 |
|
|
if dims{1}=='T' |
159 |
|
|
if isempty(find(fii)), return, end |
160 |
|
|
tmpdata=nc{cvar}(find(fii),:); |
161 |
|
|
it = length(dims); |
162 |
|
|
else |
163 |
|
|
tmpdata=nc{cvar}(:); |
164 |
|
|
it = 0; |
165 |
|
|
end |
166 |
enderton |
1.4 |
|
167 |
enderton |
1.3 |
tmpdata=squeeze(permute(tmpdata,[9:-1:1])); |
168 |
|
|
[ni nj nk nm nn no np]=size(tmpdata); |
169 |
|
|
[ni nj nk nm nn no np]; |
170 |
|
|
|
171 |
|
|
[i0,j0,fn]=findTileOffset(S); |
172 |
|
|
cdim=dims{end}; if cdim(1)~='X'; i0=0; end |
173 |
|
|
cdim=dims{end}; if cdim(1)=='Y'; i0=j0; j0=0; end |
174 |
|
|
if length(dims)>1; |
175 |
|
|
cdim=dims{end-1}; if cdim(1)~='Y'; j0=0; end |
176 |
|
|
else |
177 |
|
|
j0=0; |
178 |
|
|
end |
179 |
|
|
|
180 |
|
|
Sstr = ''; |
181 |
|
|
for istr = 1:7 |
182 |
|
|
if istr == it, Sstr = [Sstr,'dii,']; |
183 |
|
|
elseif istr == 1, Sstr = [Sstr,'i0+(1:ni),']; |
184 |
|
|
elseif istr == 2, Sstr = [Sstr,'j0+(1:nj),']; |
185 |
|
|
elseif istr == 3, Sstr = [Sstr,'(1:nk),']; |
186 |
|
|
elseif istr == 4, Sstr = [Sstr,'(1:nm),']; |
187 |
|
|
elseif istr == 5, Sstr = [Sstr,'(1:nn),']; |
188 |
|
|
elseif istr == 6, Sstr = [Sstr,'(1:no),']; |
189 |
|
|
elseif istr == 7, Sstr = [Sstr,'(1:np),']; |
190 |
|
|
else, error('Can''t handle this many dimensions!'); |
191 |
|
|
end |
192 |
|
|
end |
193 |
|
|
eval(['S.(cvar)(',Sstr(1:end-1),')=tmpdata;']) |
194 |
|
|
%S.(cvar)(i0+(1:ni),j0+(1:nj),(1:nk),(1:nm),(1:nn),(1:no),(1:np))=tmpdata; |
195 |
|
|
S.attributes.(cvar)=read_att(nc{cvar}); |
196 |
|
|
end |
197 |
adcroft |
1.1 |
|
198 |
|
|
if isempty(S) |
199 |
enderton |
1.3 |
error('Something didn''t work!!!'); |
200 |
adcroft |
1.1 |
end |