Changeset 816
- Timestamp:
- Aug 13, 2014, 10:33:12 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/nc2struct.m
r809 r816 1 %'nc2struct': transform a netcdffile in a corresponding matlab structure1 %'nc2struct': transform a NetCDF file in a corresponding matlab structure 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 … … 6 6 % 7 7 % OUTPUT: 8 % Data: structure containing all the information of the netcdf file (or netcdfobject)8 % Data: structure containing all the information of the NetCDF file (or NetCDF object) 9 9 % with (optional)fields: 10 10 % .ListGlobalAttribute: cell listing the names of the global attributes 11 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) 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 14 % .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName 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 % .VarType= integers giving the type of variable as coded by netcdf =2 for char, =4 for single,=( for double17 % .VarType= integers giving the type of variable as coded by netcdf =2 for char, =4 for single,=( for double 18 18 % var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else. 19 % var_detect=[] in the absence of input cell array 20 % ichoice: index of the selected line in the case of multiple choice 21 % (cell array of varible names with multiple lines) , =[] by default 22 %INPUT: 23 % nc: name of a netcdf file (char string) or netcdf object 19 % var_detect=[] in the absence of input cell array 20 % ichoice: index of the selected line in the case of multiple choice 21 % (cell array of varible names with multiple lines) , =[] by default 22 % 23 % INPUT: 24 % nc: name of a NetCDF file (char string) or NetCDF object 24 25 % additional arguments: 25 % -no additional arguments: all the variables of the netcdffile are read.26 % -a cell array, ListVarName, made of char strings {'VarName1', 'VarName2',...} ) 26 % -no additional arguments: all the variables of the NetCDF file are read. 27 % -a cell array, ListVarName, made of char strings {'VarName1', 'VarName2',...} ) 27 28 % if ListVarName=[] or {}, no variable value is read (only global attributes and list of variables and dimensions) 28 % if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read.29 % if ListVarName is absent, or = '*', ALL the variables of the NetCDF file are read. 29 30 % if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority in the list, 30 31 % while output names will be set by the first line 31 32 % - the string 'ListGlobalAttribute' followed by a list of attribute names: reads only these attributes (fast reading) 32 % - the string 'TimeVarName', a string (the name of the variable considered as time), an integer or vector with integer values 33 % - the string 'TimeVarName', a string (the name of the variable considered as time), an integer or vector with integer values 33 34 % representing time indices to select for each variable, the cell of other input variable names. 34 % - the string 'TimeDimName', a string (the name of the dimension considered as time), an integer or vector with integer values 35 % - the string 'TimeDimName', a string (the name of the dimension considered as time), an integer or vector with integer values 35 36 % representing time indices to select for each variable, the cell of other input variable names. 36 37 … … 52 53 % GNU General Public License (see LICENSE.txt) for more details. 53 54 %======================================================================= 54 55 55 56 function [Data,var_detect,ichoice,errormsg]=nc2struct(nc,varargin) 56 57 errormsg='';%default error message … … 58 59 varargin{1}='*'; 59 60 end 60 hhh=which('netcdf.open');% look for built-in matlab netcdflibrary61 hhh=which('netcdf.open');% look for built-in matlab NetCDF library 61 62 62 63 if ~isequal(hhh,'') … … 65 66 var_detect=[];%default 66 67 ichoice=[];%default 67 68 %% open the netcdffile for reading68 69 %% open the NetCDF file for reading 69 70 if ischar(nc) 70 71 if exist(nc,'file') … … 83 84 testfile=0; 84 85 end 85 86 86 87 %% short reading option for global attributes only, if the first argument is 'ListGlobalAttribute' 87 88 if isequal(varargin{1},'ListGlobalAttribute') … … 97 98 return 98 99 end 99 100 100 101 %% time variable or dimension 101 102 input_index=1; … … 112 113 input_index=4; 113 114 end 114 115 115 116 %% full reading: get the nbre of dimensions, variables, global attributes 116 117 ListVarName=varargin{input_index}; 117 [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the netcdffile118 118 [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the NetCDF file 119 119 120 %% -------- read all global attributes (constants)----------- 120 121 Data.ListGlobalAttribute={};%default … … 142 143 end 143 144 Data.ListGlobalAttribute=att_key; 144 145 145 146 %% -------- read dimension names----------- 146 147 ListDimNameNetcdf=cell(1,ndims); 147 148 dim_value=zeros(1,ndims); 148 for idim=1:ndims %loop on the dimensions of the netcdffile149 for idim=1:ndims %loop on the dimensions of the NetCDF file 149 150 [ListDimNameNetcdf{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);%get name and value of each dimension 150 151 end … … 162 163 return 163 164 end 164 end 165 165 end 166 166 167 %% -------- read names of variables ----------- 167 168 ListVarNameNetcdf=cell(1,nvars); %default 168 169 dimids=cell(1,nvars); 169 170 nbatt=zeros(1,nvars); 170 for ncvar=1:nvars %loop on the variables of the netcdffile171 for ncvar=1:nvars %loop on the variables of the NetCDF file 171 172 %get name, type, dimensions and attribute numbers of each variable 172 173 [ListVarNameNetcdf{ncvar},xtype(ncvar),dimids{ncvar},nbatt(ncvar)] = netcdf.inqVar(nc,ncvar-1); … … 174 175 % testmulti=0; 175 176 if isequal(ListVarName,'*')||isempty(ListVarName) 176 var_index=1:nvars; %all the variables are selected in the netcdffile177 var_index=1:nvars; %all the variables are selected in the NetCDF file 177 178 Data.ListVarName=ListVarNameNetcdf; 178 179 else %select input variables, if requested by the input ListVarName … … 186 187 if size(ListVarName,1)>1 %multiple choice of variable ranked by order of priority 187 188 for iline=1:size(ListVarName,1) 188 search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of netcdfvariables189 search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of NetCDF variables 189 190 if ~isempty(search_index) 190 191 break % go to the next line … … 207 208 var_index=zeros(1,size(ListVarName,2));%default list of variable indices 208 209 for ivar=1:size(ListVarName,2) 209 search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of netcdffile210 search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of NetCDF file 210 211 if ~isempty(search_index) 211 212 var_index(ivar)=search_index;%index of the netcdf list corresponding to the input list index ivar … … 214 215 var_detect=(var_index~=0);%=1 for detected variables 215 216 list_index=find(var_index);% indices in the input list corresponding to a detected variable 216 var_index=var_index(list_index);% netcdfvariable indices corresponding to the output list of read variable217 var_index=var_index(list_index);% NetCDF variable indices corresponding to the output list of read variable 217 218 Data.ListVarName=ListVarName(1,list_index);%the first line of ListVarName sets the output names of the variables 218 219 end 219 220 220 221 %% get the dimensions and attributes associated to variables 221 222 var_dim=cell(size(var_index));% initiate list of dimensions for variables … … 243 244 end 244 245 end 245 246 246 247 %% select the dimensions used for the set of input variables 247 248 if ~isempty(var_index) … … 253 254 end 254 255 end 255 256 256 257 %% get the values of the input variables 257 258 if ~isempty(ListVarName) 258 259 for ivar=1:length(var_index) 259 260 VarName=Data.ListVarName{ivar}; 260 VarName=regexprep(VarName,'-','_'); %suppress '-' if it exists in the netcdfvariable name (leads to errors in matlab)261 VarName=regexprep(VarName,'-','_'); %suppress '-' if it exists in the NetCDF variable name (leads to errors in matlab) 261 262 % CheckSub=0; 262 263 if input_index==4% if a dimension is selected as time … … 272 273 ind_size(index_time)=numel(TimeIndex);%length of the selected set of time indices 273 274 if numel(TimeIndex)==1 && ~strcmp(VarName,TimeVarName) 274 Data.VarDimName{ivar}(index_time)=[];% for a single selected time remove the time in the list of dimensions (except for tTime itself) 275 Data.VarDimName{ivar}(index_time)=[];% for a single selected time remove the time in the list of dimensions (except for tTime itself) 275 276 end 276 end 277 end 277 278 Data.(VarName)=double(netcdf.getVar(nc,var_index(ivar)-1,ind_vec,ind_size)); %read the variable data 278 279 Data.(VarName)=squeeze(Data.(VarName));%remove singeton dimension … … 286 287 end 287 288 Data.VarType=xtype(var_index); 288 289 289 290 %% -------- close fle----------- 290 291 if testfile==1 291 292 netcdf.close(nc) 292 293 end 293 294 %% old netcdflibrary294 295 %% old NetCDF library 295 296 else 296 297 [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin);
Note: See TracChangeset
for help on using the changeset viewer.