/[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.12 - (hide annotations) (download)
Sat Feb 17 23:49:43 2007 UTC (17 years, 3 months ago) by jmc
Branch: MAIN
Changes since 1.11: +3 -0 lines
add $Header:  $ and $Name:  & (for CVS)

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

  ViewVC Help
Powered by ViewVC 1.1.22