/[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.1 - (hide annotations) (download)
Mon Mar 8 16:45:56 2004 UTC (20 years, 2 months ago) by adcroft
Branch: MAIN
CVS Tags: checkpoint52l_pre, checkpoint52n_post, checkpoint53d_post, checkpoint54a_pre, checkpoint55c_post, checkpoint54e_post, checkpoint54a_post, checkpoint53c_post, checkpoint55d_pre, checkpoint57b_post, checkpoint57c_pre, checkpoint55j_post, checkpoint56b_post, checkpoint52l_post, checkpoint55h_post, checkpoint54b_post, checkpoint53b_pre, checkpoint55b_post, checkpoint54d_post, checkpoint56c_post, checkpoint52m_post, checkpoint55, checkpoint53a_post, checkpoint57a_post, checkpoint54, checkpoint54f_post, checkpoint53b_post, checkpoint55g_post, checkpoint55f_post, checkpoint57a_pre, checkpoint55i_post, checkpoint57, checkpoint56, checkpoint53, checkpoint53g_post, hrcube5, checkpoint57c_post, checkpoint55e_post, checkpoint53f_post, checkpoint55a_post, checkpoint53d_pre, checkpoint54c_post, checkpoint56a_post, checkpoint55d_post
Rudimentary script for reading distributed netcdf files.

1 adcroft 1.1 function [S] = rdmnc(varargin)
2     % S=RDMNC(FILE1,FILE2,...)
3     % S=RDMNC(FILE1,...,ITER)
4     % S=RDMNC(FILE1,...,'VAR1','VAR2',...)
5     % S=RDMNC(FILE1,...,'VAR1','VAR2',...,ITER)
6     %
7     % FILE1 ... is either a single file name (e.g. 'state.nc') or a wild-card
8     % strings expanding to a group of file names (e.g. 'state.*.nc').
9     % There are no assumptions about suffices (e.g. 'state.*' works)
10     % VAR1 ... are model variable names as written in the MNC/netcdf file
11     % ITER is a vector of time levels (consecutive indices referring to snap-shot
12     % in the MNC/netcdf file i.e. 1,2,3,... and not model time)
13     % S is a structure with mutliple fields
14     %
15     % rdmnc() is a rudimentary wrapper for joining and reading netcdf files
16     % produced by MITgcm. It does not give the same flexibility as opening the
17     % netcdf files directly using netcdf(). It is useful for quick loading of
18     % entire model fields which are distributed in multiple netcdf files.
19     % rdmnc() also reads non-MITgcm generated netcdf files.
20     %
21     % e.g. To plot the surface temperature in last time-level written to file
22     % >> S=rdmnd('state.*','XC','YC','RC','T',Inf);
23     % >> imagesc( S.XC, S.YC, S.T(:,:,1)' );
24     %
25     % $Header: $
26    
27     %Defaults
28     global verbose
29     file={};
30     files={};
31     verbose=0;
32     varlist={};
33     timelevels=[];
34    
35     % Process function arguments
36     for iarg=1:nargin;
37     arg=varargin{iarg};
38     if ischar(arg)
39     switch char(arg)
40     case 'verbose',
41     verbose=1;
42     otherwise,
43     if isempty( dir(char(arg)) )
44     varlist{end+1}={arg};
45     else
46     file{end+1}={arg};
47     end
48     end
49     else
50     if isempty(timelevels)
51     timelevels=arg;
52     else
53     error('Specify the time levels in a vector and not as multiple numeric arguments')
54     end
55     end
56     end
57    
58     if verbose; disp('Verbose mode enabled'); end
59    
60     % Create list of filenames
61     for eachfile=file
62     expandedEachFile=dir(char(eachfile{1}));
63     for i=1:size(expandedEachFile,1);
64     if expandedEachFile(i).isdir==0; files{end+1}=expandedEachFile(i).name; end
65     end
66     end
67    
68     % Open each file
69     S.attributes=[];
70     for eachfile={files{1:end}}
71     nc=netcdf(char(eachfile),'read');
72     if ismncfile(nc)
73     S=rdmnc_local(nc,varlist,timelevels,S);
74     else
75     S=rdnetcdf_local(nc,varlist,S);
76     end
77     end
78    
79     % -----------------------------------------------------------------------------
80     function [result] = ismncfile(nc);
81     result=~isempty(nc.('MITgcm_mnc_ver'));
82     % -----------------------------------------------------------------------------
83     function [A] = read_att(nc);
84     global verbose
85     allatt=ncnames(att(nc)); if verbose; allatt, end
86     A='none';
87     for attr=allatt;
88     A.(char(attr))=nc.(char(attr))(:);
89     end
90     % -----------------------------------------------------------------------------
91     function [i0,j0,fn] = findTileOffset(S);
92     global verbose
93     fn=0;
94     if isfield(S,'attributes') & isfield(S.attributes,'global')
95     G=S.attributes.global;
96     tn=G.tile_number;
97     snx=G.sNx; sny=G.sNy; nsx=G.nSx; nsy=G.nSy; npx=G.nPx; npy=G.nPy;
98     ntx=nsx*npx;nty=nsy*npy;
99     gbi=mod(tn-1,ntx); gbj=(tn-gbi-1)/ntx;
100     i0=snx*gbi; j0=sny*gbj;
101     if isfield(S.attributes.global,'exch2_myFace')
102     fn=G.exch2_myFace;
103     end
104     else
105     i0=0;j0=0;
106     end
107     % -----------------------------------------------------------------------------
108     function [S] = rdnetcdf_local(nc,varlist,S)
109     % Read all attributes and variable data from a netcdf file
110     % with special operations for MNC style data
111     global verbose
112    
113     % No variables specified? Default to all
114     if isempty(varlist)
115     varlist=ncnames(var(nc)); if verbose; varlist, end
116     end
117    
118     % Attributes for structure
119     S.attributes=read_att(nc);
120    
121     % Read variable data
122     for ivar=1:size(varlist,2);
123     cvar=char(varlist{ivar});
124     tmpdata=nc{cvar}(:);
125     if isempty(tmpdata)
126     disp(['No such variable ''' cvar ''' in netcdf file' name(nc)])
127     else
128     tmpdata=squeeze(permute(tmpdata,[9:-1:1]));
129     S.(cvar)=tmpdata;
130     S.attributes.(cvar)=read_att(nc{cvar});
131     end
132     end
133     % -----------------------------------------------------------------------------
134     function [S] = rdmnc_local(nc,varlist,timelevels,S)
135     % Read all attributes and variable data from a netcdf file
136     % with special operations for MNC style data
137     global verbose
138    
139     nt=size( nc('T'), 1); if verbose; nt, end
140    
141     % No variables specified? Default to all
142     if isempty(varlist)
143     varlist=ncnames(var(nc)); if verbose; varlist, end
144     end
145    
146     % No iterations specified? Default to all
147     if isempty(timelevels) | isnan(timelevels)
148     timelevels=1:nt;
149     elseif timelevels == Inf
150     timelevels=nt;
151     end
152    
153     % Sanity checks
154     if max( find(timelevels > nt) )
155     error('Requested time level is beyond time dimension in netcdf file')
156     end
157    
158     % Attributes for structure
159     if timelevels>0; S.timelevels_read_from_file=timelevels; end
160     S.attributes.global=read_att(nc);
161    
162     % Read variable data
163     for ivar=1:size(varlist,2);
164     cvar=char(varlist{ivar}); if verbose; cvar, end
165     if isempty(nc{cvar})
166     disp(['No such variable ''' cvar ''' in MNC file ' name(nc)])
167     continue
168     end
169     dims=ncnames(dim(nc{cvar}));
170     if dims{1}=='T'
171     if verbose; disp(['Reading variable ''' cvar ''' with record dimension']); end
172     tmpdata=nc{cvar}(timelevels,:);
173     else
174     if verbose; disp(['Reading variable ''' cvar '''']); end
175     tmpdata=nc{cvar}(:);
176     end
177     if isempty(tmpdata)
178     error(['No such variable ''' cvar ''' in MNC file ' name(nc)])
179     else
180     tmpdata=squeeze(permute(tmpdata,[9:-1:1]));
181     [ni nj nk nm nn no np]=size(tmpdata);
182     if np~=1; error('Wow! This is a very high dimension variable...'); end
183     [i0,j0,fn]=findTileOffset(S);
184     cdim=dims{end}; if cdim(1)~='X'; i0=0; end
185     cdim=dims{end}; if cdim(1)=='Y'; i0=j0; j0=0; end
186     if length(dims)>1;
187     cdim=dims{end-1}; if cdim(1)~='Y'; j0=0; end
188     else
189     j0=0;
190     end
191     S.(cvar)(i0+(1:ni),j0+(1:nj),(1:nk),(1:nm),(1:nn),(1:no),(1:np))=tmpdata;
192     S.attributes.(cvar)=read_att(nc{cvar});
193     end
194     end
195    
196     if isempty(S)
197     error('Something didn''t work!!!')
198     end
199     % -----------------------------------------------------------------------------

  ViewVC Help
Powered by ViewVC 1.1.22