Changeset 648 for trunk/src/nc2struct.m
- Timestamp:
- Jun 9, 2013, 10:31:58 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/nc2struct.m
r534 r648 23 23 % nc: name of a netcdf file (char string) or netcdf object 24 24 % additional arguments: 25 % -no additional arguments: all the variables of the netcdf fi elare read.25 % -no additional arguments: all the variables of the netcdf file are read. 26 26 % -a cell array, ListVarName, made of char strings {'VarName1', 'VarName2',...} ) 27 % if ListVarName=[] or {}, no variable s is read (only global attributes)27 % if ListVarName=[] or {}, no variable value is read (only global attributes and list of variables and dimensions) 28 28 % if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read. 29 29 % if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority 30 30 % in the list, while output names will be set by the first line 31 31 % - the string 'ListGlobalAttribute' followed by a list of attribute names: reads only these attributes (fast reading) 32 % 32 % - the string 'TimeVarName', a string (the variable considered as 33 % time), an integer (the selected time index), the cell of other 34 % input variables limited to the input time index (considered as the last index of arrays) 35 % - the string 'TimeDimName', a string (the dimension considered as time), an integer (the selected time index), the cell of other 36 % input variables limited to the input time index (considered as the last index of arrays) 37 33 38 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 34 39 % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. … … 91 96 return 92 97 end 98 99 %% time variable or dimension 100 input_index=1; 101 if isequal(varargin{1},'TimeVarName') 102 TimeVarName=varargin{2}; 103 TimeVarIndex=varargin{3}; 104 input_index=4; 105 elseif isequal(varargin{1},'TimeDimName') 106 TimeDimName=varargin{2}; 107 TimeVarIndex=varargin{3}; 108 input_index=4; 109 end 93 110 94 111 %% full reading: get the nbre of dimensions, variables, global attributes 95 ListVarName=varargin{ 1};112 ListVarName=varargin{input_index}; 96 113 [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the netcdf file 97 114 … … 130 147 if ~isempty(ListDimNameNetcdf) 131 148 flag_used=zeros(1,ndims);%initialize the flag indicating the selected dimensions in the list (0=unused) 149 end 150 if isequal(varargin{1},'TimeDimName')% time dimension introduced 151 TimeDimIndex=find(strcmp(varargin{2},ListDimNameNetcdf)); 152 if isempty(TimeDimIndex) 153 Data.Txt=['requested time dimension ' varargin{2} ' not found']; 154 return 155 end 156 if dim_value(TimeDimIndex)<varargin{3} 157 Data.Txt=['requested time index ' num2str(varargin{3}) ' exceeds matrix dimension']; 158 return 159 end 132 160 end 133 161 … … 180 208 181 209 %% get the dimensions and attributes associated to variables 210 var_dim=cell(size(var_index));% initiate list of dimensions for variables 182 211 for ivar=1:length(var_index) 183 212 var_dim{ivar}=dimids{var_index(ivar)}+1; %netcdf dimension indices used by the variable #ivar … … 208 237 end 209 238 end 239 210 240 211 241 %% select the dimensions used for the set of input variables … … 221 251 VarName=Data.ListVarName{ivar}; 222 252 VarName=regexprep(VarName,'-',''); %suppress '-' if it exists in the netcdf variable name 223 eval(['Data.' VarName '=double(netcdf.getVar(nc,var_index(ivar)-1));'])%read the variable data 253 CheckSub=0; 254 if input_index==4% if a dimension is selected as time 255 if var_dim{ivar}(end)==TimeDimIndex% if the last dim of the variable is the time 256 slice_length=prod(var_dim{ivar}(1:end-1)); 257 Data.(VarName)=double(netcdf.getVar(nc,var_index(ivar)-1),TimeIndex*slice_length,slice_length); %read the variable data 258 CheckSub=1; 259 end 260 end 261 if ~CheckSub 262 Data.(VarName)=double(netcdf.getVar(nc,var_index(ivar)-1)); %read the whole variable data 263 end 224 264 end 225 265 end
Note: See TracChangeset
for help on using the changeset viewer.