'nc2struct': transform a netcdf file in a corresponding matlab structure ---------------------------------------------------------------------- function [Data,var_detect,ichoice]=nc2struct(nc,ListVarName) OUTPUT: Data: structure containing all the information of the netcdf file (or netcdf object) with fields: .ListGlobalAttribute: cell listing the names of the global attributes .Att_1,Att_2... : values of the global attributes .ListDimName: cell listing the names of the array dimensions .DimValue: array dimension values (Matlab vector with the same length as .ListDimName .ListVarName: cell listing the names of the variables .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else. ichoice: = line INPUT: nc: name of a netcdf file (char string) or netcdf object ListVarName: optional list of variable names to select (cell array of char strings {'VarName1', 'VarName2',...} ) if ListVarName=[] or {}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes) if ListVarName is absent, or = '*', ALL the variables are read. if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority in the list, while output names will be set by the first line AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA This file is part of the toolbox UVMAT. UVMAT is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. UVMAT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License (file UVMAT/COPYING.txt) for more details. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0001 %'nc2struct': transform a netcdf file in a corresponding matlab structure 0002 %---------------------------------------------------------------------- 0003 % function [Data,var_detect,ichoice]=nc2struct(nc,ListVarName) 0004 % 0005 % OUTPUT: 0006 % Data: structure containing all the information of the netcdf file (or netcdf object) 0007 % with fields: 0008 % .ListGlobalAttribute: cell listing the names of the global attributes 0009 % .Att_1,Att_2... : values of the global attributes 0010 % .ListDimName: cell listing the names of the array dimensions 0011 % .DimValue: array dimension values (Matlab vector with the same length as .ListDimName 0012 % .ListVarName: cell listing the names of the variables 0013 % .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName 0014 % .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName 0015 % .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName 0016 % .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName 0017 % var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else. 0018 % ichoice: = line 0019 % 0020 %INPUT: 0021 % nc: name of a netcdf file (char string) or netcdf object 0022 % ListVarName: optional list of variable names to select (cell array of char strings {'VarName1', 'VarName2',...} ) 0023 % if ListVarName=[] or {}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes) 0024 % if ListVarName is absent, or = '*', ALL the variables are read. 0025 % if ListVarName is a cell array with n lines, the set of variables 0026 % will be sought by order of priority in the list, while output names will be set by the first line 0027 % 0028 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 0029 % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. 0030 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 0031 % This file is part of the toolbox UVMAT. 0032 % 0033 % UVMAT is free software; you can redistribute it and/or modify 0034 % it under the terms of the GNU General Public License as published by 0035 % the Free Software Foundation; either version 2 of the License, or 0036 % (at your option) any later version. 0037 % 0038 % UVMAT is distributed in the hope that it will be useful, 0039 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0040 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0041 % GNU General Public License (file UVMAT/COPYING.txt) for more details. 0042 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 0043 0044 function [Data,var_detect,ichoice]=nc2struct(nc,ListVarName) 0045 0046 if ~exist('ListVarName','var') 0047 ListVarName='*'; 0048 end 0049 hhh=which('netcdf.open');% look for built-in matlab netcdf library 0050 0051 if ~isequal(hhh,'') 0052 %default output 0053 Data=[]; 0054 % ListIndex=[]; 0055 var_detect=[]; 0056 ichoice=[];%default 0057 %open the netcdf file for reading 0058 if ischar(nc) 0059 if exist(nc,'file') 0060 nc=netcdf.open(nc,'NC_NOWRITE'); 0061 testfile=1; 0062 else 0063 Data.Txt=['ERROR:file ' nc ' does not exist']; 0064 return 0065 end 0066 else 0067 testfile=0; 0068 end 0069 [ndims,nvars,ngatts] = netcdf.inq(nc);%nbre of dimensions, variables, attributes 0070 0071 % -------- read global attributes (constants)----------- 0072 att_key={};%default 0073 iatt_g=0; 0074 Data.ListGlobalAttribute={};%default 0075 for iatt=1:ngatts 0076 keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1); 0077 indstr1=regexp(keystr,'\\');%replace dots' 0078 indstr2=regexp(keystr,'\.');%replace dots' 0079 if isempty(indstr1) && isempty(indstr2) 0080 valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr); 0081 if ischar(valuestr) && length(valuestr)<200 0082 iatt_g=iatt_g+1; 0083 indstr1=regexp(keystr,'\\');%replace dots' 0084 indstr2=regexp(keystr,'\.');%replace dots' 0085 if isempty(indstr1) && isempty(indstr2) 0086 eval(['Data.' keystr '=''' valuestr ''';']) 0087 att_key{iatt_g}=keystr; 0088 end 0089 elseif isempty(valuestr) 0090 iatt_g=iatt_g+1; 0091 eval(['Data.' keystr '=[];']) 0092 att_key{iatt_g}=keystr; 0093 elseif isnumeric(valuestr) 0094 iatt_g=iatt_g+1; 0095 eval(['Data.' keystr '=valuestr;']) 0096 att_key{iatt_g}=keystr; 0097 end 0098 end 0099 end 0100 Data.ListGlobalAttribute=att_key; 0101 0102 % -------- read dimensions ----------- 0103 dim_name={}; 0104 dim_value=[]; 0105 for idim=1:ndims%length(dim_read); 0106 [dim_name{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1); 0107 end 0108 if ~isempty(dim_name) && ~isempty(dim_value) 0109 Data.ListDimName=dim_name; 0110 Data.DimValue=dim_value; 0111 % DimIndices=[1:ndims]; %index of the dimension in the netcdf file 0112 dim_used=zeros(1,ndims);%initialize test of used dimensions 0113 end 0114 0115 % -------- read variables ----------- 0116 var_read={}; %default 0117 dimids={}; 0118 nbatt=[]; 0119 for ivar=1:nvars 0120 [var_read{ivar},xtype,dimids{ivar},nbatt(ivar)] = netcdf.inqVar(nc,ivar-1); 0121 end 0122 var_index=1:nvars; %default set of variable indices in the netcdf file 0123 testmulti=0; 0124 OutputList=[]; 0125 0126 %select input variables, if requested by the input ListVarName 0127 if isequal(ListVarName,'*')||isemptyl(ListVarName) 0128 sizvar=size(ListVarName); 0129 testmulti=(sizvar(1)>1); 0130 var_index=zeros(1,sizvar(2));%default 0131 if testmulti 0132 OutputList=ListVarName(1,:); 0133 testend=0; 0134 for iline=1:sizvar(1) 0135 if testend 0136 break 0137 end 0138 % var_index=zeros(size(ListVarName));%default 0139 for ivar=1:sizvar(2) 0140 if ~isempty(ListVarName{iline,ivar}) 0141 for ilist=1:nvars 0142 if isequal(var_read{ilist},ListVarName{iline,ivar}) 0143 var_index(ivar)=ilist; 0144 % var_detect(ivar)=1; 0145 break 0146 end 0147 end 0148 if ivar==1 0149 if var_index(ivar)==0 0150 break%go to next line if the first nc variable is not found 0151 else 0152 testend=1; %this line will be read 0153 ichoice=iline-1; %selectedline number in the list of input names of variables 0154 end 0155 end 0156 end 0157 end 0158 end 0159 else %single list of input variables 0160 for ivar=1:sizvar(2) 0161 for ilist=1:nvars 0162 if isequal(var_read{ilist},ListVarName{ivar}) 0163 var_index(ivar)=ilist; 0164 var_detect(ivar)=1; 0165 break 0166 end 0167 end 0168 end 0169 end 0170 list_index=find(var_index); 0171 if ~isempty(list_index) 0172 if testmulti 0173 OutputList=OutputList(list_index); 0174 end 0175 var_index=var_index(list_index); 0176 var_detect=(var_index~=0); 0177 var_read=var_read(var_index); 0178 end 0179 end 0180 0181 0182 %select variable attributes and associate dimensions 0183 % var_dim_index=[]; %default 0184 Data.ListVarName={};%default 0185 VarDimIndex={};%default 0186 for ivar=1:length(var_read) 0187 if testmulti 0188 Data.ListVarName{ivar}=OutputList{ivar};%new name given by ListVarName(1,:) 0189 else 0190 Data.ListVarName{ivar}=var_read{ivar};%name of the variable 0191 end 0192 var_dim=dimids{var_index(ivar)}+1; %dimension indices used by the variable 0193 dim_used(var_dim)=ones(size(var_dim)); 0194 VarDimIndex{ivar}=var_dim; 0195 0196 %variable attributes 0197 if ivar==1 0198 Data.VarAttribute={};%initialisation of the list of variable attributes 0199 end 0200 %variable attributes 0201 for iatt=1:nbatt(var_index(ivar)) 0202 attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1); 0203 valuestr= netcdf.getAtt(nc,var_index(ivar)-1,attname); 0204 if ischar(valuestr) 0205 eval(['Data.VarAttribute{ivar}.' attname '=''' valuestr ''';']) 0206 elseif isempty(valuestr) 0207 eval(['Data.VarAttribute{ivar}.' attname '=[];']) 0208 elseif isnumeric(valuestr) 0209 eval(['Data.VarAttribute{ivar}.' attname '=valuestr;']) 0210 end 0211 end 0212 end 0213 0214 %select the used dimensions 0215 if isempty(var_read) 0216 if isfield(Data,'ListDimName') && isfield(Data,'DimValue') 0217 Data=rmfield(Data,'ListDimName'); 0218 Data=rmfield(Data,'DimValue'); 0219 end 0220 else 0221 % list_dim=1:ndims; 0222 dim_index=find(dim_used); 0223 % list_dim=list_dim(dim_index); 0224 old2new=cumsum(dim_used); 0225 Data.ListDimName=Data.ListDimName(dim_index); 0226 Data.DimValue=Data.DimValue(dim_index); 0227 end 0228 for ivar=1:length(var_read) 0229 Data.VarDimIndex{ivar}=old2new(VarDimIndex{ivar});% ENLEVER Data.VarDimIndex ulterieurement 0230 Data.VarDimName{ivar}=Data.ListDimName(Data.VarDimIndex{ivar}); 0231 end 0232 %variable values 0233 if ~isempty(ListVarName) 0234 for ivar=1:length(Data.ListVarName) 0235 eval(['Data.' Data.ListVarName{ivar} '=netcdf.getVar(nc,var_index(ivar)-1);'])%read the variable data 0236 eval(['Data.' Data.ListVarName{ivar} '=Data.' Data.ListVarName{ivar} ''';'])%read the variable data 0237 end 0238 end 0239 % -------- close fle----------- 0240 if testfile==1 0241 netcdf.close(nc) 0242 end 0243 else 0244 [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName); 0245 end