Changeset 55 for trunk/src/nc2struct.m
- Timestamp:
- Mar 17, 2010, 10:26:41 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/nc2struct.m
r45 r55 2 2 % it reads all the global attributes and all variables, or a selected list. 3 3 % The corresponding dimensions and variable attributes are then extracted 4 %%%%%% TODO: add the possibility to read only attributes, see nc2struct_toolbox %%%5 4 %---------------------------------------------------------------------- 6 5 % function [Data,var_detect,ichoice]=nc2struct(nc,ListVarName) … … 9 8 % Data: structure containing all the information of the netcdf file (or netcdf object) 10 9 % with fields: 11 % .ListGlobalAttribute: cell listing the names of the global attributes 12 % .Att_1,Att_2... : values of the global attributes 13 % .ListDimName: cell listing the names of the array dimensions 14 % .DimValue: array dimension values (Matlab vector with the same length as .ListDimName 15 % .ListVarName: cell listing the names of the variables 16 % .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName 17 % .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName 18 % .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName 19 % .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName 20 % var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else. 21 % ichoice: = line 22 % 10 % (optional) .ListGlobalAttribute: cell listing the names of the global attributes 11 % .Att_1,Att_2... : values of the global attributes 12 % .ListVarName: list of variable names to select (cell array of char strings {'VarName1', 'VarName2',...} ) 13 % .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells) 14 % .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName 15 % .ListDimName=list of dimension (added information, not requested for field description) 16 % .DimValue= vlalues of dimensions (added information, not requested for field description) 17 % .VarDimIndex= list of dimension indices (added information, 18 23 19 %INPUT: 24 20 % nc: name of a netcdf file (char string) or netcdf object … … 47 43 function [Data,var_detect,ichoice]=nc2struct(nc,varargin) 48 44 List=varargin; 49 if nargin==045 if isempty(varargin) 50 46 List{1}='*'; 51 47 end … … 122 118 123 119 % -------- read dimensions ----------- 124 dim_name={};120 ListDimName={}; 125 121 dim_value=[]; 126 122 for idim=1:ndims%length(dim_read); 127 [dim_name{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1); 128 end 129 if ~isempty(dim_name) && ~isempty(dim_value) 130 Data.ListDimName=dim_name; 131 Data.DimValue=dim_value; 123 [ListDimName{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1); 124 end 125 if ~isempty(ListDimName) %&& ~isempty(dim_value) 126 % Data.DimValue=dim_value; 132 127 % DimIndices=[1:ndims]; %index of the dimension in the netcdf file 133 dim_used=zeros(1,ndims);%initialize test of used dimensions128 flag_used=zeros(1,ndims);%initialize test of used dimensions 134 129 end 135 130 … … 198 193 end 199 194 end 200 201 195 202 196 %select variable attributes and associate dimensions 203 197 % var_dim_index=[]; %default … … 211 205 end 212 206 var_dim=dimids{var_index(ivar)}+1; %dimension indices used by the variable 213 dim_used(var_dim)=ones(size(var_dim));207 flag_used(var_dim)=ones(size(var_dim));%flag_used =1 for the indices of used dimensions 214 208 VarDimIndex{ivar}=var_dim; 215 209 … … 233 227 234 228 %select the used dimensions 235 if isempty(var_read)236 if isfield(Data,'ListDimName')&& isfield(Data,'DimValue')237 Data=rmfield(Data,'ListDimName');238 239 end240 else229 if ~isempty(var_read) 230 % if isfield(Data,'ListDimName') %&& isfield(Data,'DimValue') 231 % Data=rmfield(Data,'ListDimName'); 232 % %Data=rmfield(Data,'DimValue'); 233 % end 234 % else 241 235 % list_dim=1:ndims; 242 dim_index=find( dim_used);236 dim_index=find(flag_used); 243 237 % list_dim=list_dim(dim_index); 244 old2new=cumsum( dim_used);245 Data.ListDimName=Data.ListDimName(dim_index);246 Data.DimValue=Data.DimValue(dim_index);238 old2new=cumsum(flag_used); 239 ListDimName=ListDimName(dim_index); 240 dim_value=dim_value(dim_index); 247 241 end 248 242 for ivar=1:length(var_read) 249 Data.VarDimIndex{ivar}=old2new(VarDimIndex{ivar});% ENLEVER Data.VarDimIndex ulterieurement 250 Data.VarDimName{ivar}=Data.ListDimName(Data.VarDimIndex{ivar}); 251 end 243 %Data.VarDimIndex{ivar}=old2new(VarDimIndex{ivar});% ENLEVER Data.VarDimIndex ulterieurement 244 %Data.VarDimName{ivar}=Data.ListDimName(Data.VarDimIndex{ivar}); 245 Data.VarDimName{ivar}=ListDimName(old2new(VarDimIndex{ivar})); 246 end 247 Data.ListDimName=ListDimName; 248 Data.DimValue=dim_value; 249 Data.VarDimIndex= VarDimIndex; 252 250 %variable values 253 251 if ~isempty(ListVarName) … … 259 257 end 260 258 eval(['Data.' VarName '=netcdf.getVar(nc,var_index(ivar)-1);'])%read the variable data 261 eval(['siz=size(Data.' VarName ');'])262 if numel(siz)<=2263 eval(['Data.' VarName '=Data.' VarName ''';'])%read the variable data264 end259 %eval(['siz=size(Data.' VarName ');']) 260 % if numel(siz)<=2 261 %eval(['Data.' VarName '=Data.' VarName ''';'])%read the variable data 262 %end 265 263 end 266 264 end
Note: See TracChangeset
for help on using the changeset viewer.