1 function h=read_wrfout_sel(files,vars,nsamples)
2 % h=read_wrfout_sel(files,vars,nsamples)
3 % read selected time levels from wrfouts
5 % files cell array of file names
6 % files cell array of variable names; if followed by 'sparse' will be read as such
7 % nsamples number of timestep samples to read, if missing read all
9 % h structure with the selected variables
12 [s,dims]=nc2struct(files{i},{'Times'},{});
21 error('sparse flag must follow a variable name');
28 varname{nvar}=vars{i};
29 spfield{nvar}=[lower(varname{nvar}),'_sparse'];
35 if ~exist('nsamples','var') | nsamples == 0,
38 step = floor((ntot+nsamples-1)/nsamples)
41 idx=mod(ntot-1,step)+1:step:ntot;
45 k=find(j>=start);k=k(end);
47 fprintf('reading step %i as %i from file %i into %i\n',j,loc,k,jx)
48 if loc <=0 | k > length(files), error('bad index'), end
49 [f,dims]=nc2struct(files{k},varname,{},loc);
51 for jj=1:length(varname)
52 field=lower(varname{jj});
55 h.(spfield{jj})=cell(1,nidx);
57 h.(field)=zeros([d(1:end-1),nidx]);
61 for jj=1:length(varname)
63 h.(spfield{jj}){jx}=sparse(f.(field));
65 field=lower(varname{jj});
66 n=length(dims.(field));
69 h.(field)(:,jx)=f.(field);
71 h.(field)(:,:,jx)=f.(field);
73 h.(field)(:,:,:,jx)=f.(field);
75 error(['unsupported number of dimensions ',num2str(n)]);