Warning, /utils/matlab/rdmnc.m is written in an unsupported language. File is not indexed.
view on githubraw file Latest commit add29e06 on 2018-01-31 20:35:05 UTC
cbb456033f Jean*0001 function [S] = rdmnc(varargin)
0002
0003 % Usage:
0004 % S=RDMNC(FILE1,FILE2,...)
0005 % S=RDMNC(FILE1,...,ITER)
0006 % S=RDMNC(FILE1,...,'VAR1','VAR2',...)
0007 % S=RDMNC(FILE1,...,'VAR1','VAR2',...,ITER)
0008 %
0009 % Input:
0010 % FILE1 Either a single file name (e.g. 'state.nc') or a wild-card
0011 % strings expanding to a group of file names (e.g. 'state.*.nc').
0012 % There are no assumptions about suffices (e.g. 'state.*' works).
0013 % VAR1 Model variable names as written in the MNC/netcdf file.
0014 % ITER Vector of iterations in the MNC/netcdf files, not model time.
0015 %
0016 % Output:
0017 % S Structure with fields corresponding to 'VAR1', 'VAR2', ...
0018 %
0019 % Description:
0020 % This function is a rudimentary wrapper for joining and reading netcdf
a0bbca3f8c Jean*0021 % files produced by MITgcm. It does not give the same flexibility as
cbb456033f Jean*0022 % opening the netcdf files directly using netcdf(), but is useful for
0023 % quick loading of entire model fields which are distributed in multiple
0024 % netcdf files.
0025 %
0026 % Example:
0027 % >> S=rdmnd('state.*','XC','YC','T');
0028 % >> imagesc( S.XC, S.YC, S.T(:,:,1)' );
0029 %
0030 % Author: Alistair Adcroft
0031 % Modifications: Daniel Enderton
0032
0033 % Initializations
c69c38dfce Jean*0034 dBug=0;
cbb456033f Jean*0035 file={};
0036 filepaths={};
0037 files={};
0038 varlist={};
0039 iters=[];
0040
43a87a8afa Mart*0041 % find out if matlab's generic netcdf API is available
0042 % if not assume that Chuck Denham's netcdf toolbox is installed
0043 usemexcdf = isempty(which('netcdf.open'));
0044
cbb456033f Jean*0045 % Process function arguments
0046 for iarg=1:nargin;
0047 arg=varargin{iarg};
0048 if ischar(arg)
0049 if isempty(dir(char(arg)))
0050 varlist{end+1}=arg;
0051 else
0052 file{end+1}=arg;
0053 end
0054 else
0055 if isempty(iters)
0056 iters=arg;
0057 else
0058 error(['The only allowed numeric argument is iterations',...
83c8fc8fc3 Jean*0059 ' to read in as a vector for the last argument']);
cbb456033f Jean*0060 end
0061 end
0062 end
83c8fc8fc3 Jean*0063 if isempty(file)
0064 if isempty(varlist),
0065 fprintf( 'No file name in argument list\n');
0066 else
0067 fprintf(['No file in argument list:\n ==> ',char(varlist(1))]);
0068 for i=2:size(varlist,2), fprintf([' , ',char(varlist(i))]); end
a0bbca3f8c Jean*0069 fprintf(' <==\n');
83c8fc8fc3 Jean*0070 end
0071 error(' check argument list !!!');
0072 end
cbb456033f Jean*0073
0074 % Create list of filenames
0075 for eachfile=file
0076 filepathtemp=eachfile{:};
5dc640ab2a Mart*0077 indecies = find(filepathtemp==filesep);
cbb456033f Jean*0078 if ~isempty(indecies)
0079 filepathtemp = filepathtemp(1:indecies(end));
0080 else
0081 filepathtemp = '';
0082 end
0083 expandedEachFile=dir(char(eachfile{1}));
0084 for i=1:size(expandedEachFile,1);
0085 if expandedEachFile(i).isdir==0
0086 files{end+1}=expandedEachFile(i).name;
0087 filepaths{end+1}=filepathtemp;
0088 end
0089 end
0090 end
0091
0092
0093 % If iterations unspecified, open all the files and make list of all the
0094 % iterations that appear, use this iterations list for data extraction.
0095 if isempty(iters)
0096 iters = [];
0097 for ieachfile=1:length(files)
0098 eachfile = [filepaths{ieachfile},files{ieachfile}];
43a87a8afa Mart*0099 if usemexcdf
0100 nc=netcdf(char(eachfile),'read');
0101 nciters = nc{'iter'}(:);
0102 if isempty(nciters), nciters = nc{'T'}(:); end
0103 close(nc);
0104 else
ce7961b627 Mart*0105 % the parser complains about "netcdf.open" when the matlab netcdf
0106 % API is not available, even when it is not used so we have to
0107 % avoid the use of "netcdf.open", etc in this function
0108 nciters = ncgetvar(char(eachfile),'iter');
0109 if isempty(nciters), nciters = ncgetvar(char(eachfile),'T'); end
43a87a8afa Mart*0110 end
ce7961b627 Mart*0111 iters = [iters,nciters'];
cbb456033f Jean*0112 end
8f664175a3 Jean*0113 iters = unique(iters');
cbb456033f Jean*0114 end
0115
0116 % Cycle through files for data extraction.
0117 S.attributes=[];
0118 for ieachfile=1:length(files)
0119 eachfile = [filepaths{ieachfile},files{ieachfile}];
c69c38dfce Jean*0120 if dBug > 0, fprintf([' open: ',eachfile]); end
43a87a8afa Mart*0121 if usemexcdf
0122 nc=netcdf(char(eachfile),'read');
0123 S=rdmnc_local(nc,varlist,iters,S,dBug);
0124 close(nc);
0125 else
ce7961b627 Mart*0126 % the parser complains about "netcdf.open" when the matlab netcdf
0127 % API is not available, even when it is not used so we have to
0128 % avoid the use of "netcdf.open", etc in this function
0129 S=rdmnc_local_matlabAPI(char(eachfile),varlist,iters,S,dBug);
43a87a8afa Mart*0130 end
cbb456033f Jean*0131 end
0132
43a87a8afa Mart*0133 return
cbb456033f Jean*0134
0135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0136 % Local functions %
0137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0138
0139 function [A] = read_att(nc);
0140 allatt=ncnames(att(nc));
0141 if ~isempty(allatt)
0142 for attr=allatt;
0143 A.(char(attr))=nc.(char(attr))(:);
0144 end
0145 else
0146 A = 'none';
0147 end
0148
0149 function [i0,j0,fn] = findTileOffset(S);
0150 fn=0;
0151 if isfield(S,'attributes') & isfield(S.attributes,'global')
0152 G=S.attributes.global;
0153 tn=G.tile_number;
0154 snx=G.sNx; sny=G.sNy; nsx=G.nSx; nsy=G.nSy; npx=G.nPx; npy=G.nPy;
0155 ntx=nsx*npx;nty=nsy*npy;
0156 gbi=mod(tn-1,ntx); gbj=(tn-gbi-1)/ntx;
0157 i0=snx*gbi; j0=sny*gbj;
0158 if isfield(S.attributes.global,'exch2_myFace')
0159 fn=G.exch2_myFace;
a0bbca3f8c Jean*0160 i0=G.exch2_txGlobalo -1; j0=G.exch2_tyGlobalo -1;
cbb456033f Jean*0161 end
0162 else
0163 i0=0;j0=0;
0164 end
0165 %[snx,sny,nsx,nsy,npx,npy,ntx,nty,i0,j0,fn];
0166
c69c38dfce Jean*0167 function [S] = rdmnc_local(nc,varlist,iters,S,dBug)
cbb456033f Jean*0168
966dd2ad04 Mart*0169 fiter = nc{'iter'}(:); % File iterations present
0170 if isempty(fiter), fiter = nc{'T'}(:); end
0171 if isinf(iters); iters = fiter(end); end
0172 if isnan(iters); iters = fiter; end
0173 [fii,dii] = ismember(fiter,iters); fii = find(fii); % File iteration index
0174 dii = dii(find(dii ~= 0)); % Data interation index
0175 if dBug > 0,
a0bbca3f8c Jean*0176 fprintf(' ; fii='); fprintf(' %i',fii);
0177 fprintf(' ; dii='); fprintf(' %i',dii); fprintf(' \n');
966dd2ad04 Mart*0178 end
a0bbca3f8c Jean*0179
966dd2ad04 Mart*0180 % No variables specified? Default to all
0181 if isempty(varlist), varlist=ncnames(var(nc)); end
a0bbca3f8c Jean*0182
966dd2ad04 Mart*0183 % Attributes for structure
0184 if iters>0; S.iters_from_file=iters; end
0185 S.attributes.global=read_att(nc);
0186 [pstr,netcdf_fname,ext] = fileparts(name(nc));
0187 if strcmp(netcdf_fname(end-3:end),'glob')
0188 % assume it is a global file produced by gluemnc and change some
a0bbca3f8c Jean*0189 % attributes
966dd2ad04 Mart*0190 S.attributes.global.sNx = S.attributes.global.Nx;
0191 S.attributes.global.sNy = S.attributes.global.Ny;
0192 S.attributes.global.nPx = 1;
0193 S.attributes.global.nSx = 1;
0194 S.attributes.global.nPy = 1;
0195 S.attributes.global.nSy = 1;
0196 S.attributes.global.tile_number = 1;
0197 S.attributes.global.nco_openmp_thread_number = 1;
0198 end
a0bbca3f8c Jean*0199
966dd2ad04 Mart*0200 % Read variable data
0201 for ivar=1:size(varlist,2)
a0bbca3f8c Jean*0202
966dd2ad04 Mart*0203 cvar=char(varlist{ivar});
0204 if isempty(nc{cvar})
0205 disp(['No such variable ''',cvar,''' in MNC file ',name(nc)]);
0206 continue
5f6d00ca1a Mart*0207 end
d92a965dd1 Mart*0208 % code by Bruno Deremble: if you do not want to read all tiles these
0209 % modifications make the output field smaller, let us see, if it is
a0bbca3f8c Jean*0210 % robust
d92a965dd1 Mart*0211 if (isfield(S,cvar) == 0); firstiter = 1; else firstiter = 0; end
0212 % end code by Bruno Deremble
0213
966dd2ad04 Mart*0214 dims = ncnames(dim(nc{cvar})); % Dimensions
0215 sizVar = size(nc{cvar}); nDims=length(sizVar);
0216 if dims{1} == 'T'
0217 if isempty(find(fii)), error('Iters not found'); end
0218 it = length(dims);
0219 tmpdata = nc{cvar}(fii,:);
0220 % leading unity dimensions get lost; add them back:
0221 tmpdata=reshape(tmpdata,[length(fii) sizVar(2:end)]);
0222 else
0223 it = 0;
0224 tmpdata = nc{cvar}(:);
0225 % leading unity dimensions get lost; add them back:
0226 tmpdata=reshape(tmpdata,sizVar);
0227 end
a0bbca3f8c Jean*0228
966dd2ad04 Mart*0229 if dBug > 1,
0230 fprintf([' var:',cvar,': nDims=%i ('],nDims);fprintf(' %i',size(nc{cvar}));
a0bbca3f8c Jean*0231 fprintf('):%i,nD=%i,it=%i ;',length(size(tmpdata)),length(dims),it);
966dd2ad04 Mart*0232 end
0233 if length(dims) > 1,
0234 tmpdata=permute(tmpdata,[nDims:-1:1]);
0235 end
0236 if dBug > 1,
c69c38dfce Jean*0237 % fprintf('(tmpdata:');fprintf(' %i',size(tmpdata)); fprintf(')');
966dd2ad04 Mart*0238 end
0239 [ni nj nk nm nn no np]=size(tmpdata);
a0bbca3f8c Jean*0240
966dd2ad04 Mart*0241 [i0,j0,fn]=findTileOffset(S);
0242 cdim=dims{end}; if cdim(1)~='X'; i0=0; end
0243 cdim=dims{end}; if cdim(1)=='Y'; i0=j0; j0=0; end
0244 if length(dims)>1;
0245 cdim=dims{end-1}; if cdim(1)~='Y'; j0=0; end
0246 else
0247 j0=0;
0248 end
0249 if dBug > 1,
0250 fprintf(' i0,ni= %i %i; j,nj= %i %i; nk=%i :',i0,ni,j0,nj,nk);
0251 end
d92a965dd1 Mart*0252 % code by Bruno Deremble: if you do not want to read all tiles these
0253 % modifications make the output field smaller, let us see, if it is
a0bbca3f8c Jean*0254 % robust
d92a965dd1 Mart*0255 if (firstiter)
a08098fbe8 Mart*0256 S.attributes.i_first.(cvar) = i0;
0257 S.attributes.j_first.(cvar) = j0;
a0bbca3f8c Jean*0258 end
a08098fbe8 Mart*0259 i0 = i0 - S.attributes.i_first.(cvar);
0260 j0 = j0 - S.attributes.j_first.(cvar);
d92a965dd1 Mart*0261 % end code by Bruno Deremble
a0bbca3f8c Jean*0262
966dd2ad04 Mart*0263 Sstr = '';
0264 for istr = 1:max(nDims,length(dims)),
0265 if istr == it, Sstr = [Sstr,'dii,'];
0266 elseif istr == 1, Sstr = [Sstr,'i0+(1:ni),'];
0267 elseif istr == 2, Sstr = [Sstr,'j0+(1:nj),'];
0268 elseif istr == 3, Sstr = [Sstr,'(1:nk),'];
0269 elseif istr == 4, Sstr = [Sstr,'(1:nm),'];
0270 elseif istr == 5, Sstr = [Sstr,'(1:nn),'];
0271 elseif istr == 6, Sstr = [Sstr,'(1:no),'];
0272 elseif istr == 7, Sstr = [Sstr,'(1:np),'];
0273 else, error('Can''t handle this many dimensions!');
0274 end
0275 end
0276 eval(['S.(cvar)(',Sstr(1:end-1),')=tmpdata;'])
0277 %S.(cvar)(i0+(1:ni),j0+(1:nj),(1:nk),(1:nm),(1:nn),(1:no),(1:np))=tmpdata;
0278 if dBug > 1, fprintf(' %i',size(S.(cvar))); fprintf('\n'); end
a0bbca3f8c Jean*0279
966dd2ad04 Mart*0280 S.attributes.(cvar)=read_att(nc{cvar});
0281 % replace missing or FillValues with NaN
0282 if isfield(S.attributes.(cvar),'missing_value');
0283 misval = S.attributes.(cvar).missing_value;
0284 S.(cvar)(S.(cvar) == misval) = NaN;
0285 end
0286 if isfield(S.attributes.(cvar),'FillValue_');
0287 misval = S.attributes.(cvar).FillValue_;
0288 S.(cvar)(S.(cvar) == misval) = NaN;
0289 end
a0bbca3f8c Jean*0290
966dd2ad04 Mart*0291 end % for ivar
a0bbca3f8c Jean*0292
966dd2ad04 Mart*0293 if isempty(S)
cbb456033f Jean*0294 error('Something didn''t work!!!');
966dd2ad04 Mart*0295 end
a0bbca3f8c Jean*0296
966dd2ad04 Mart*0297 return
43a87a8afa Mart*0298
ce7961b627 Mart*0299 function [S] = rdmnc_local_matlabAPI(fname,varlist,iters,S,dBug)
43a87a8afa Mart*0300
ce7961b627 Mart*0301 fiter = ncgetvar(fname,'iter'); % File iterations present
0302 if isempty(fiter), fiter = ncgetvar(fname,'T'); end
43a87a8afa Mart*0303 if isinf(iters); iters = fiter(end); end
0304 if isnan(iters); iters = fiter; end
0305 [fii,dii] = ismember(fiter,iters); fii = find(fii); % File iteration index
0306 dii = dii(find(dii ~= 0)); % Data interation index
0307 if dBug > 0,
a0bbca3f8c Jean*0308 fprintf(' ; fii='); fprintf(' %i',fii);
0309 fprintf(' ; dii='); fprintf(' %i',dii); fprintf(' \n');
43a87a8afa Mart*0310 end
0311
ce7961b627 Mart*0312 % now open the file for reading
0313 nc = netcdf.open(fname,'NC_NOWRITE');
43a87a8afa Mart*0314 % get basic information about netcdf file
3500d68d4a Mart*0315 [ndims nvars natts recdim] = netcdf.inq(nc);
43a87a8afa Mart*0316
0317 % No variables specified? Default to all
a0bbca3f8c Jean*0318 if isempty(varlist),
43a87a8afa Mart*0319 for k=0:nvars-1
0320 varlist{k+1} = netcdf.inqVar(nc,k);
0321 end
0322 end
a0bbca3f8c Jean*0323
43a87a8afa Mart*0324 % Attributes for structure
0325 if iters>0; S.iters_from_file=iters; end
0326 S.attributes.global=ncgetatt(nc,'global');
0327 [pstr,netcdf_fname,ext] = fileparts(fname);
0328 if strcmp(netcdf_fname(end-3:end),'glob')
0329 % assume it is a global file produced by gluemnc and change some
a0bbca3f8c Jean*0330 % attributes
43a87a8afa Mart*0331 S.attributes.global.sNx = S.attributes.global.Nx;
0332 S.attributes.global.sNy = S.attributes.global.Ny;
0333 S.attributes.global.nPx = 1;
0334 S.attributes.global.nSx = 1;
0335 S.attributes.global.nPy = 1;
0336 S.attributes.global.nSy = 1;
0337 S.attributes.global.tile_number = 1;
0338 S.attributes.global.nco_openmp_thread_number = 1;
0339 end
a0bbca3f8c Jean*0340
43a87a8afa Mart*0341 % Read variable data
0342 for ivar=1:size(varlist,2)
a0bbca3f8c Jean*0343
43a87a8afa Mart*0344 cvar=char(varlist{ivar});
0345 varid=ncfindvarid(nc,cvar);
0346 if isempty(varid)
0347 disp(['No such variable ''',cvar,''' in MNC file ',fname]);
0348 continue
0349 end
d92a965dd1 Mart*0350 % code by Bruno Deremble: if you do not want to read all tiles these
0351 % modifications make the output field smaller, let us see, if it is
a0bbca3f8c Jean*0352 % robust
d92a965dd1 Mart*0353 if (isfield(S,cvar) == 0); firstiter = 1; else firstiter = 0; end
0354 % end code by Bruno Deremble
a0bbca3f8c Jean*0355
43a87a8afa Mart*0356 [varname,xtype,dimids,natts] = netcdf.inqVar(nc,varid);
3500d68d4a Mart*0357 % does this variable contain a record (unlimited) dimension?
0358 [isrecvar,recpos] = ismember(recdim,dimids);
a0bbca3f8c Jean*0359
43a87a8afa Mart*0360 % Dimensions
0361 clear sizVar dims
0362 for k=1:length(dimids)
0363 [dims{k}, sizVar(k)] = netcdf.inqDim(nc,dimids(k));
0364 end
0365 nDims=length(sizVar);
3500d68d4a Mart*0366 if isrecvar
43a87a8afa Mart*0367 if isempty(find(fii)), error('Iters not found'); end
0368 it = length(dims);
3500d68d4a Mart*0369 if length(dimids) == 1
0370 % just a time or iteration variable, this will result in a vector
0371 % and requires special treatment
0372 icount=1;
0373 tmpdata = zeros(length(fii),1);
0374 for k=1:length(fii)
0375 istart = fii(k)-1;
0376 tmpdata(k) = netcdf.getVar(nc,varid,istart,icount,'double');
0377 end
0378 else
0379 % from now on we assume that the record dimension is always the
0380 % last dimension. This may not always be the case
0381 if recpos ~= nDims
0382 error(sprintf('%s\n%s\n%s%s%i%s%i', ...
0383 ['The current code assumes that the record ' ...
0384 'dimension is the last dimension,'], ...
0385 'this is not the case for variable', cvar, ...
0386 ': nDims = ', nDims, ...
0387 ', position of recDim = ', recpos))
0388 end
0389 istart = zeros(1,it); % indexing starts a 0
0390 icount = sizVar;
a0bbca3f8c Jean*0391 % we always want to get only on time slice at a time
0392 icount(recpos) = 1;
3500d68d4a Mart*0393 % make your life simpler by putting the time dimension first
0394 tmpdata = zeros([length(fii) sizVar(1:end-1)]);
0395 for k=1:length(fii)
0396 istart(recpos) = fii(k)-1; % indexing starts at 0
0397 tmp = netcdf.getVar(nc,varid,istart,icount,'double');
0398 tmpdata(k,:) = tmp(:);
0399 end
0400 % move time dimension to the end ...
0401 tmpdata = shiftdim(tmpdata,1);
0402 % ... and restore original shape
0403 tmpdata = reshape(tmpdata,[sizVar(1:end-1) length(fii)]);
0404 end
43a87a8afa Mart*0405 else
0406 it = 0;
0407 tmpdata = netcdf.getVar(nc,varid,'double');
0408 end
3500d68d4a Mart*0409 %
43a87a8afa Mart*0410 if dBug > 1,
0411 fprintf([' var:',cvar,': nDims=%i ('],nDims);fprintf(' %i',sizVar);
a0bbca3f8c Jean*0412 fprintf('):%i,nD=%i,it=%i ;',length(size(tmpdata)),length(dims),it);
43a87a8afa Mart*0413 end
0414 [ni nj nk nm nn no np]=size(tmpdata);
3500d68d4a Mart*0415 %
43a87a8afa Mart*0416 [i0,j0,fn]=findTileOffset(S);
0417 cdim=dims{1}; if cdim(1)~='X'; i0=0; end
0418 cdim=dims{1}; if cdim(1)=='Y'; i0=j0; j0=0; end
0419 if length(dims)>1;
0420 cdim=dims{2}; if cdim(1)~='Y'; j0=0; end
0421 else
0422 j0=0;
0423 end
0424 if dBug > 1,
0425 fprintf(' i0,ni= %i %i; j,nj= %i %i; nk=%i :',i0,ni,j0,nj,nk);
0426 end
d92a965dd1 Mart*0427 % code by Bruno Deremble: if you do not want to read all tiles these
0428 % modifications make the output field smaller, let us see, if it is
a0bbca3f8c Jean*0429 % robust
d92a965dd1 Mart*0430 if (firstiter)
a08098fbe8 Mart*0431 S.attributes.i_first.(cvar) = i0;
0432 S.attributes.j_first.(cvar) = j0;
a0bbca3f8c Jean*0433 end
a08098fbe8 Mart*0434 i0 = i0 - S.attributes.i_first.(cvar);
0435 j0 = j0 - S.attributes.j_first.(cvar);
d92a965dd1 Mart*0436 % end code by Bruno Deremble
0437
43a87a8afa Mart*0438 Sstr = '';
0439 for istr = 1:max(nDims,length(dims)),
0440 if istr == it, Sstr = [Sstr,'dii,'];
0441 elseif istr == 1, Sstr = [Sstr,'i0+(1:ni),'];
0442 elseif istr == 2, Sstr = [Sstr,'j0+(1:nj),'];
0443 elseif istr == 3, Sstr = [Sstr,'(1:nk),'];
0444 elseif istr == 4, Sstr = [Sstr,'(1:nm),'];
0445 elseif istr == 5, Sstr = [Sstr,'(1:nn),'];
0446 elseif istr == 6, Sstr = [Sstr,'(1:no),'];
0447 elseif istr == 7, Sstr = [Sstr,'(1:np),'];
0448 else, error('Can''t handle this many dimensions!');
0449 end
0450 end
0451 eval(['S.(cvar)(',Sstr(1:end-1),')=tmpdata;'])
0452 %S.(cvar)(i0+(1:ni),j0+(1:nj),(1:nk),(1:nm),(1:nn),(1:no),(1:np))=tmpdata;
0453 if dBug > 1, fprintf(' %i',size(S.(cvar))); fprintf('\n'); end
3500d68d4a Mart*0454 %
43a87a8afa Mart*0455 S.attributes.(cvar)=ncgetatt(nc,cvar);
0456 % replace missing or FillValues with NaN
0457 if isfield(S.attributes.(cvar),'missing_value');
0458 misval = S.attributes.(cvar).missing_value;
0459 S.(cvar)(S.(cvar) == misval) = NaN;
0460 end
0461 if isfield(S.attributes.(cvar),'FillValue_');
0462 misval = S.attributes.(cvar).FillValue_;
0463 S.(cvar)(S.(cvar) == misval) = NaN;
0464 end
a0bbca3f8c Jean*0465
43a87a8afa Mart*0466 end % for ivar
ce7961b627 Mart*0467
0468 % close the file
0469 netcdf.close(nc);
a0bbca3f8c Jean*0470
43a87a8afa Mart*0471 if isempty(S)
0472 error('Something didn''t work!!!');
0473 end
a0bbca3f8c Jean*0474
43a87a8afa Mart*0475 return
0476
ce7961b627 Mart*0477 function vf = ncgetvar(fname,varname)
43a87a8afa Mart*0478 % read a netcdf variable
a0bbca3f8c Jean*0479
ce7961b627 Mart*0480 nc=netcdf.open(fname,'NC_NOWRITE');
0481 % find out basics about the files
43a87a8afa Mart*0482 [ndims nvars natts dimm] = netcdf.inq(nc);
0483 vf = [];
0484 varid = [];
0485 for k=0:nvars-1
0486 if strcmp(netcdf.inqVar(nc,k),varname)
3e47429d0a Mart*0487 varid = netcdf.inqVarID(nc,varname);
43a87a8afa Mart*0488 end
0489 end
a0bbca3f8c Jean*0490 if ~isempty(varid);
43a87a8afa Mart*0491 [varn,xtype,dimids,natts] = netcdf.inqVar(nc,varid);
0492 % get data
0493 vf = double(netcdf.getVar(nc,varid));
0494 else
0495 % do nothing
0496 end
ce7961b627 Mart*0497 netcdf.close(nc);
a0bbca3f8c Jean*0498
43a87a8afa Mart*0499 return
0500
0501 function misval = ncgetmisval(nc,varid)
0502
0503 [varname,xtype,dimids,natts] = netcdf.inqVar(nc,varid);
0504 misval = [];
0505 for k=0:natts-1
0506 attn = netcdf.inqAttName(nc,varid,k);
0507 if strcmp(attn,'missing_value') | strcmp(attn,'_FillValue')
0508 misval = double(netcdf.getAtt(nc,varid,attname));
0509 end
0510 end
a0bbca3f8c Jean*0511
43a87a8afa Mart*0512 function A = ncgetatt(nc,varname)
0513 % get all attributes and put them into a struct
a0bbca3f8c Jean*0514
43a87a8afa Mart*0515 % 1. get global properties of file
0516 [ndims nvars natts dimm] = netcdf.inq(nc);
0517
0518 % get variable ID and properties
0519 if strcmp('global',varname)
0520 % "variable" is global
0521 varid = netcdf.getConstant('NC_GLOBAL');
0522 else
0523 % find variable ID and properties
0524 varid = ncfindvarid(nc,varname);
0525 if ~isempty(varid)
0526 [varn,xtype,dimids,natts] = netcdf.inqVar(nc,varid);
0527 else
0528 warning(sprintf('variable %s not found',varname))
0529 end
0530 end
0531
0532 if natts > 1
0533 for k=0:natts-1
0534 attn = netcdf.inqAttName(nc,varid,k);
0535 [xtype attlen]=netcdf.inqAtt(nc,varid,attn);
0536 attval = netcdf.getAtt(nc,varid,attn);
0537 if ~ischar(attval)
0538 attval = double(attval);
0539 end
0746c5fab0 Mart*0540 if strcmp(attn,'_FillValue')
0541 % matlab does not allow variable names to begin with an
0542 % underscore ("_"), so we have to do change the name of this
0543 % obsolete attribute.
0544 A.FillValue_=attval;
0545 else
0546 A.(char(attn))=attval;
0547 end
43a87a8afa Mart*0548 end
0549 else
0550 A = 'none';
0551 end
a0bbca3f8c Jean*0552
43a87a8afa Mart*0553 return
0554
a0bbca3f8c Jean*0555
43a87a8afa Mart*0556 function varid = ncfindvarid(nc,varname)
0557
0558 [ndims nvars natts dimm] = netcdf.inq(nc);
0559 varid=[];
0560 for k=0:nvars-1
0561 if strcmp(netcdf.inqVar(nc,k),varname);
3e47429d0a Mart*0562 varid = netcdf.inqVarID(nc,varname);
43a87a8afa Mart*0563 break
0564 end
0565 end
0566
0567 return