Changeset 140 for trunk/src/nc2struct.m
- Timestamp:
- Nov 29, 2010, 9:55:54 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/nc2struct.m
r127 r140 7 7 % OUTPUT: 8 8 % Data: structure containing all the information of the netcdf file (or netcdf object) 9 % with fields:10 % (optional).ListGlobalAttribute: cell listing the names of the global attributes9 % with (optional)fields: 10 % .ListGlobalAttribute: cell listing the names of the global attributes 11 11 % .Att_1,Att_2... : values of the global attributes 12 12 % .ListVarName: list of variable names to select (cell array of char strings {'VarName1', 'VarName2',...} ) … … 15 15 % .ListDimName=list of dimension (added information, not requested for field description) 16 16 % .DimValue= vlalues of dimensions (added information, not requested for field description) 17 % .VarDimIndex= list of dimension indices (added information, 18 % 17 % var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else. 18 % var_detect=[] in the absence of input cell array 19 % ichoice: index of the selected line in the case of multiple choice 20 % (cell array of varible names with multiple lines) , =[] by default 19 21 %INPUT: 20 % nc:name of a netcdf file (char string) or netcdf object21 % 22 % -in the absence of other arguments, all the fields are read23 % -a cell array, ListVarName,of char strings {'VarName1', 'VarName2',...} )22 % nc: name of a netcdf file (char string) or netcdf object 23 % additional arguments: 24 % -no additional arguments: all the variables of the netcdf fiel are read. 25 % -a cell array, ListVarName, made of char strings {'VarName1', 'VarName2',...} ) 24 26 % if ListVarName=[] or {}, no variables is read (only global attributes) 25 % if ListVarName is absent, or = '*', ALL the variables are read.26 % if ListVarName is a cell array with n lines, the set of variables27 % will be sought by order of priorityin the list, while output names will be set by the first line27 % if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read. 28 % if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority 29 % in the list, while output names will be set by the first line 28 30 % - the string 'ListGlobalAttribute' followed by a list of attribute names: reads only these attributes (fast reading) 29 31 % … … 52 54 53 55 if ~isequal(hhh,'') 54 % default output55 Data=[]; 56 var_detect=[]; 56 %% default output 57 Data=[];%default 58 var_detect=[];%default 57 59 ichoice=[];%default 58 %open the netcdf file for reading 60 61 %% open the netcdf file for reading 59 62 if ischar(nc) 60 63 if exist(nc,'file') … … 68 71 testfile=0; 69 72 end 70 % short reading of global attributes 73 74 %% short reading opion for global attributes only, if the first argument is 'ListGlobalAttribute' 71 75 if isequal(varargin{1},'ListGlobalAttribute') 72 76 for ilist=2:numel(varargin) … … 82 86 end 83 87 84 % reading of variables, includingattributes85 ListVarName=varargin{1}; 86 [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, attributes87 88 % -------- readglobal attributes (constants)-----------88 %% full reading: get the nbre of dimensions, variables, global attributes 89 ListVarName=varargin{1}; 90 [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the netcdf file 91 92 %% -------- read all global attributes (constants)----------- 89 93 att_key={};%default 90 94 iatt_g=0; … … 92 96 for iatt=1:ngatts 93 97 keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1); 94 indstr1=regexp(keystr,'\\' );%detect '\\'95 indstr2=regexp(keystr,'\.' );%detect '\.'98 indstr1=regexp(keystr,'\\','once');%detect '\\' 99 indstr2=regexp(keystr,'\.','once');%detect '\.' 96 100 if isempty(indstr1) && isempty(indstr2) 97 101 valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr); … … 117 121 Data.ListGlobalAttribute=att_key; 118 122 119 % -------- read dimensions-----------120 ListDimName ={};123 %% -------- read dimension names----------- 124 ListDimNameNetcdf={}; 121 125 dim_value=[]; 122 for idim=1:ndims%length(dim_read); 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; 127 % DimIndices=[1:ndims]; %index of the dimension in the netcdf file 128 flag_used=zeros(1,ndims);%initialize test of used dimensions 126 for idim=1:ndims %loop on the dimensions of the netcdf file 127 [ListDimNameNetcdf{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);%get name and value of each dimension 128 end 129 if ~isempty(ListDimNameNetcdf) 130 flag_used=zeros(1,ndims);%initialize the flag indicating the selected dimensions in the list (0=unused) 129 131 end 130 132 131 % -------- readvariables -----------132 var_read={}; %default133 %% -------- read names of variables ----------- 134 ListVarNameNetcdf={}; %default 133 135 dimids={}; 134 136 nbatt=[]; 135 for ivar=1:nvars 136 [var_read{ivar},xtype,dimids{ivar},nbatt(ivar)] = netcdf.inqVar(nc,ivar-1); 137 for ncvar=1:nvars %loop on the variables of the netcdf file 138 %get name, type, dimensions and attribute numbers of each variable 139 [ListVarNameNetcdf{ncvar},xtype,dimids{ncvar},nbatt(ncvar)] = netcdf.inqVar(nc,ncvar-1); 137 140 end 138 var_index=1:nvars; %default set of variable indices in the netcdf file139 141 testmulti=0; 140 OutputList=[]; 141 %select input variables, if requested by the input ListVarName 142 if ~(isequal(ListVarName,'*')||isempty(ListVarName)) 142 if isequal(ListVarName,'*')||isempty(ListVarName) 143 var_index=1:nvars; %all the variables are selected in the netcdf file 144 Data.ListVarName=ListVarNameNetcdf; 145 else %select input variables, if requested by the input ListVarName 143 146 sizvar=size(ListVarName); 144 testmulti=(sizvar(1)>1); 147 testmulti=(sizvar(1)>1);%test for multiple choice of variable ranked by order of priority 145 148 var_index=zeros(1,sizvar(2));%default 146 if testmulti 147 OutputList=ListVarName(1,:); 148 testend=0; 149 if testmulti %multiple choice of variable ranked by order of priority 149 150 for iline=1:sizvar(1) 150 if testend 151 break 151 search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of netcdf variables 152 if ~isempty(search_index) 153 break % go to the next line 152 154 end 153 % var_index=zeros(size(ListVarName));%default 154 for ivar=1:sizvar(2) 155 if ~isempty(ListVarName{iline,ivar}) 156 for ilist=1:nvars 157 if isequal(var_read{ilist},ListVarName{iline,ivar}) 158 var_index(ivar)=ilist; 159 % var_detect(ivar)=1; 160 break 161 end 162 end 163 if ivar==1 164 if var_index(ivar)==0 165 break%go to next line if the first nc variable is not found 166 else 167 testend=1; %this line will be read 168 ichoice=iline-1; %selectedline number in the list of input names of variables 169 end 170 end 171 end 172 end 173 end 174 else %single list of input variables 175 for ivar=1:sizvar(2) 176 for ilist=1:nvars 177 if isequal(var_read{ilist},ListVarName{ivar}) 178 var_index(ivar)=ilist; 179 var_detect(ivar)=1; 180 break 181 end 182 end 183 end 184 end 185 list_index=find(var_index); 186 if ~isempty(list_index) 187 if testmulti 188 OutputList=OutputList(list_index); 189 end 190 var_index=var_index(list_index); 191 var_detect=(var_index~=0); 192 var_read=var_read(var_index); 193 end 194 end 195 196 %select variable attributes and associate dimensions 197 % var_dim_index=[]; %default 198 Data.ListVarName={};%default 199 VarDimIndex={};%default 200 for ivar=1:length(var_read) 201 if testmulti 202 Data.ListVarName{ivar}=OutputList{ivar};%new name given by ListVarName(1,:) 155 end 156 ichoice=iline-1;%selected line number in the list of input names of variables 203 157 else 204 Data.ListVarName{ivar}=var_read{ivar};%name of the variable 205 end 206 var_dim=dimids{var_index(ivar)}+1; %dimension indices used by the variable 207 flag_used(var_dim)=ones(size(var_dim));%flag_used =1 for the indices of used dimensions 208 VarDimIndex{ivar}=var_dim; 209 210 %variable attributes 211 if ivar==1 212 Data.VarAttribute={};%initialisation of the list of variable attributes 213 end 214 %variable attributes 158 iline=1; 159 end 160 for ivar=1:sizvar(2) 161 search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of netcdf file 162 if ~isempty(search_index) 163 var_index(ivar)=search_index;%index of the netcdf list corresponding to the input list index ivar 164 end 165 end 166 var_detect=(var_index~=0);%=1 for detected variables 167 list_index=find(var_index);% indices in the input list corresponding to a detected variable 168 var_index=var_index(list_index);% netcdf variable indices corresponding to the output list of read variable 169 Data.ListVarName=ListVarName(1,list_index);%the first line of ListVarName sets the output names of the variables 170 end 171 172 %% get the dimensions and attributes associated to variables 173 for ivar=1:length(var_index) 174 var_dim{ivar}=dimids{var_index(ivar)}+1; %netcdf dimension indices used by the variable #ivar 175 Data.VarDimName{ivar}=ListDimNameNetcdf(var_dim{ivar}); 176 flag_used(var_dim{ivar})=ones(size(var_dim{ivar}));%flag_used =1 for the indices of used dimensions 215 177 for iatt=1:nbatt(var_index(ivar)) 216 178 attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1); … … 226 188 end 227 189 228 %select the used dimensions 229 if ~isempty(var_read) 230 dim_index=find(flag_used); 231 old2new=cumsum(flag_used); 232 ListDimName=ListDimName(dim_index); 233 dim_value=dim_value(dim_index); 234 end 235 for ivar=1:length(var_read) 236 Data.VarDimName{ivar}=ListDimName(old2new(VarDimIndex{ivar})); 237 end 238 Data.ListDimName=ListDimName; 239 Data.DimValue=dim_value; 240 Data.VarDimIndex= VarDimIndex; 241 %variable values 190 %% select the dimensions used for the set of input variables 191 if ~isempty(var_index) 192 dim_index=find(flag_used);%list of netcdf dimensions indices corresponding to used dimensions 193 Data.ListDimName=ListDimNameNetcdf(dim_index); 194 Data.DimValue=dim_value(dim_index); 195 end 196 197 %% get the values of the input variables 242 198 if ~isempty(ListVarName) 243 for ivar=1:length( Data.ListVarName)199 for ivar=1:length(var_index) 244 200 VarName=Data.ListVarName{ivar}; 245 indstr=regexp(VarName,'-');%detect '-' 246 if ~isempty(indstr) 247 VarName(indstr)=[]; 248 end 201 VarName=regexprep(VarName,'-',''); %suppress '-' if it exists in the netcdf variable name 249 202 eval(['Data.' VarName '=double(netcdf.getVar(nc,var_index(ivar)-1));'])%read the variable data 250 203 end 251 204 end 252 % -------- close fle----------- 205 206 %% -------- close fle----------- 253 207 if testfile==1 254 208 netcdf.close(nc) 255 209 end 210 211 %% old netcdf library 256 212 else 257 213 [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin);
Note: See TracChangeset
for help on using the changeset viewer.