/[MITgcm]/MITgcm_contrib/enderton/Diagnostics/DiagUtility/rdmnc_mod2.m
ViewVC logotype

Contents of /MITgcm_contrib/enderton/Diagnostics/DiagUtility/rdmnc_mod2.m

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


Revision 1.1 - (show annotations) (download)
Wed Aug 10 20:02:28 2005 UTC (20 years ago) by enderton
Branch: MAIN
Script to read MNC files over iterations spanning multiple files and new colormap.

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

  ViewVC Help
Powered by ViewVC 1.1.22