/[MITgcm]/MITgcm/utils/matlab/rdmnc.m
ViewVC logotype

Annotation of /MITgcm/utils/matlab/rdmnc.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.15 - (hide annotations) (download)
Sat Mar 3 21:40:45 2007 UTC (17 years, 3 months ago) by jmc
Branch: MAIN
Changes since 1.14: +3 -3 lines
fix reading multiple files with different number of time reccords.

1 enderton 1.6 function [S] = rdmnc(varargin)
2 enderton 1.3
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 jmc 1.15 % $Header: /u/gcmpack/MITgcm/utils/matlab/rdmnc.m,v 1.14 2007/02/19 22:38:52 jmc Exp $
34 jmc 1.12 % $Name: $
35    
36 enderton 1.3 % Initializations
37 jmc 1.13 dBug=0;
38 adcroft 1.1 file={};
39 enderton 1.3 filepaths={};
40 adcroft 1.1 files={};
41     varlist={};
42 enderton 1.3 iters=[];
43 adcroft 1.1
44     % Process function arguments
45     for iarg=1:nargin;
46 enderton 1.3 arg=varargin{iarg};
47     if ischar(arg)
48     if isempty(dir(char(arg)))
49     varlist{end+1}=arg;
50     else
51     file{end+1}=arg;
52     end
53     else
54     if isempty(iters)
55     iters=arg;
56     else
57     error(['The only allowed numeric argument is iterations',...
58 jmc 1.14 ' to read in as a vector for the last argument']);
59 enderton 1.3 end
60     end
61 adcroft 1.1 end
62 jmc 1.14 if isempty(file)
63     if isempty(varlist),
64     fprintf( 'No file name in argument list\n');
65     else
66     fprintf(['No file in argument list:\n ==> ',char(varlist(1))]);
67     for i=2:size(varlist,2), fprintf([' , ',char(varlist(i))]); end
68     fprintf(' <==\n');
69     end
70     error(' check argument list !!!');
71     end
72 adcroft 1.1
73     % Create list of filenames
74     for eachfile=file
75 enderton 1.3 filepathtemp=eachfile{:};
76     indecies = find(filepathtemp=='/');
77     if ~isempty(indecies)
78     filepathtemp = filepathtemp(1:indecies(end));
79     else
80     filepathtemp = '';
81     end
82     expandedEachFile=dir(char(eachfile{1}));
83     for i=1:size(expandedEachFile,1);
84     if expandedEachFile(i).isdir==0
85     files{end+1}=expandedEachFile(i).name;
86     filepaths{end+1}=filepathtemp;
87     end
88     end
89 adcroft 1.1 end
90    
91    
92 enderton 1.3 % If iterations unspecified, open all the files and make list of all the
93     % iterations that appear, use this iterations list for data extraction.
94     if isempty(iters)
95     iters = [];
96     for ieachfile=1:length(files)
97     eachfile = [filepaths{ieachfile},files{ieachfile}];
98     nc=netcdf(char(eachfile),'read');
99 enderton 1.4 nciters = nc{'iter'}(:);
100     if isempty(nciters), nciters = nc{'T'}(:); end
101 jmc 1.15 iters = [iters,nciters'];
102 enderton 1.3 close(nc);
103     end
104 jmc 1.15 iters = unique(iters');
105 adcroft 1.1 end
106    
107 enderton 1.3 % Cycle through files for data extraction.
108     S.attributes=[];
109     for ieachfile=1:length(files)
110     eachfile = [filepaths{ieachfile},files{ieachfile}];
111 jmc 1.13 if dBug > 0, fprintf([' open: ',eachfile]); end
112 enderton 1.3 nc=netcdf(char(eachfile),'read');
113 jmc 1.13 S=rdmnc_local(nc,varlist,iters,S,dBug);
114 enderton 1.3 close(nc);
115 adcroft 1.1 end
116    
117 enderton 1.5
118 enderton 1.3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119     % Local functions %
120     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 adcroft 1.1
122 enderton 1.3 function [A] = read_att(nc);
123     allatt=ncnames(att(nc));
124     if ~isempty(allatt)
125     for attr=allatt;
126     A.(char(attr))=nc.(char(attr))(:);
127     end
128     else
129     A = 'none';
130     end
131 adcroft 1.1
132 enderton 1.3 function [i0,j0,fn] = findTileOffset(S);
133     fn=0;
134     if isfield(S,'attributes') & isfield(S.attributes,'global')
135     G=S.attributes.global;
136     tn=G.tile_number;
137     snx=G.sNx; sny=G.sNy; nsx=G.nSx; nsy=G.nSy; npx=G.nPx; npy=G.nPy;
138     ntx=nsx*npx;nty=nsy*npy;
139     gbi=mod(tn-1,ntx); gbj=(tn-gbi-1)/ntx;
140     i0=snx*gbi; j0=sny*gbj;
141     if isfield(S.attributes.global,'exch2_myFace')
142     fn=G.exch2_myFace;
143     end
144     else
145     i0=0;j0=0;
146     end
147     %[snx,sny,nsx,nsy,npx,npy,ntx,nty,i0,j0,fn];
148    
149 jmc 1.13 function [S] = rdmnc_local(nc,varlist,iters,S,dBug)
150 enderton 1.3
151     fiter = nc{'iter'}(:); % File iterations present
152 enderton 1.4 if isempty(fiter), fiter = nc{'T'}(:); end
153 mlosch 1.10 if isinf(iters); iters = fiter(end); end
154     if isnan(iters); iters = fiter; end
155 enderton 1.3 [fii,dii] = ismember(fiter,iters); fii = find(fii); % File iteration index
156     dii = dii(find(dii ~= 0)); % Data interation index
157 jmc 1.13 if dBug > 0,
158     fprintf(' ; fii='); fprintf(' %i',fii);
159     fprintf(' ; dii='); fprintf(' %i',dii); fprintf(' \n');
160     end
161 enderton 1.4
162 enderton 1.3 % No variables specified? Default to all
163     if isempty(varlist), varlist=ncnames(var(nc)); end
164 enderton 1.4
165 enderton 1.3 % Attributes for structure
166 jmc 1.14 if iters>0; S.iters_from_file=iters; end
167 enderton 1.3 S.attributes.global=read_att(nc);
168 enderton 1.4
169 enderton 1.3 % Read variable data
170 enderton 1.4 for ivar=1:size(varlist,2)
171    
172 enderton 1.3 cvar=char(varlist{ivar});
173     if isempty(nc{cvar})
174     disp(['No such variable ''',cvar,''' in MNC file ',name(nc)]);
175     continue
176     end
177    
178 enderton 1.6 dims = ncnames(dim(nc{cvar})); % Dimensions
179 jmc 1.14 sizVar = size(nc{cvar}); nDims=length(sizVar);
180 enderton 1.6 if dims{1} == 'T'
181 jmc 1.14 if isempty(find(fii)), error('Iters not found'); end
182     it = length(dims);
183 enderton 1.7 tmpdata = nc{cvar}(fii,:);
184 jmc 1.14 %- leading unity dimensions get lost; add them back:
185     tmpdata=reshape(tmpdata,[length(fii) sizVar(2:end)]);
186 enderton 1.3 else
187 jmc 1.14 it = 0;
188 enderton 1.6 tmpdata = nc{cvar}(:);
189 jmc 1.14 %- leading unity dimensions get lost; add them back:
190     tmpdata=reshape(tmpdata,sizVar);
191 enderton 1.3 end
192 enderton 1.4
193 jmc 1.13 if dBug > 1,
194     fprintf([' var:',cvar,': nDims=%i ('],nDims);fprintf(' %i',size(nc{cvar}));
195     fprintf('):%i,nD=%i,it=%i ;',length(size(tmpdata)),length(dims),it);
196     end
197     if length(dims) > 1,
198     tmpdata=permute(tmpdata,[nDims:-1:1]);
199     end
200     if dBug > 1,
201     % fprintf('(tmpdata:');fprintf(' %i',size(tmpdata)); fprintf(')');
202     end
203 enderton 1.3 [ni nj nk nm nn no np]=size(tmpdata);
204    
205     [i0,j0,fn]=findTileOffset(S);
206     cdim=dims{end}; if cdim(1)~='X'; i0=0; end
207     cdim=dims{end}; if cdim(1)=='Y'; i0=j0; j0=0; end
208     if length(dims)>1;
209     cdim=dims{end-1}; if cdim(1)~='Y'; j0=0; end
210     else
211     j0=0;
212     end
213 jmc 1.13 if dBug > 1,
214     fprintf(' i0,ni= %i %i; j,nj= %i %i; nk=%i :',i0,ni,j0,nj,nk);
215     end
216 enderton 1.3
217     Sstr = '';
218 jmc 1.13 for istr = 1:max(nDims,length(dims)),
219 enderton 1.3 if istr == it, Sstr = [Sstr,'dii,'];
220     elseif istr == 1, Sstr = [Sstr,'i0+(1:ni),'];
221     elseif istr == 2, Sstr = [Sstr,'j0+(1:nj),'];
222     elseif istr == 3, Sstr = [Sstr,'(1:nk),'];
223     elseif istr == 4, Sstr = [Sstr,'(1:nm),'];
224     elseif istr == 5, Sstr = [Sstr,'(1:nn),'];
225     elseif istr == 6, Sstr = [Sstr,'(1:no),'];
226     elseif istr == 7, Sstr = [Sstr,'(1:np),'];
227     else, error('Can''t handle this many dimensions!');
228     end
229     end
230     eval(['S.(cvar)(',Sstr(1:end-1),')=tmpdata;'])
231     %S.(cvar)(i0+(1:ni),j0+(1:nj),(1:nk),(1:nm),(1:nn),(1:no),(1:np))=tmpdata;
232 jmc 1.13 if dBug > 1, fprintf(' %i',size(S.(cvar))); fprintf('\n'); end
233    
234 enderton 1.3 S.attributes.(cvar)=read_att(nc{cvar});
235     end
236 adcroft 1.1
237     if isempty(S)
238 enderton 1.3 error('Something didn''t work!!!');
239 adcroft 1.1 end

  ViewVC Help
Powered by ViewVC 1.1.22