1 | %'nc2struct': transform a netcdf file in a corresponding matlab structure
|
---|
2 | % it reads all the global attributes and all variables, or a selected list.
|
---|
3 | % The corresponding dimensions and variable attributes are then extracted
|
---|
4 | %----------------------------------------------------------------------
|
---|
5 | % function [Data,var_detect,ichoice]=nc2struct(nc,varargin)
|
---|
6 | %
|
---|
7 | % OUTPUT:
|
---|
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 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 | %
|
---|
19 | %INPUT:
|
---|
20 | % nc: name of a netcdf file (char string) or netcdf object
|
---|
21 | % additional arguments:
|
---|
22 | % -in the absence of other arguments, all the fields are read
|
---|
23 | % -a cell array, ListVarName, of char strings {'VarName1', 'VarName2',...} )
|
---|
24 | % 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 variables
|
---|
27 | % will be sought by order of priority in the list, while output names will be set by the first line
|
---|
28 | % - the string 'ListGlobalAttribute' followed by a list of attribute names: reads only these attributes (fast reading)
|
---|
29 | %
|
---|
30 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
---|
31 | % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
|
---|
32 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
---|
33 | % This file is part of the toolbox UVMAT.
|
---|
34 | %
|
---|
35 | % UVMAT is free software; you can redistribute it and/or modify
|
---|
36 | % it under the terms of the GNU General Public License as published by
|
---|
37 | % the Free Software Foundation; either version 2 of the License, or
|
---|
38 | % (at your option) any later version.
|
---|
39 | %
|
---|
40 | % UVMAT is distributed in the hope that it will be useful,
|
---|
41 | % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
42 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
43 | % GNU General Public License (file UVMAT/COPYING.txt) for more details.
|
---|
44 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
---|
45 |
|
---|
46 | function [Data,var_detect,ichoice]=nc2struct(nc,varargin)
|
---|
47 |
|
---|
48 | if isempty(varargin)
|
---|
49 | varargin{1}='*';
|
---|
50 | end
|
---|
51 | hhh=which('netcdf.open');% look for built-in matlab netcdf library
|
---|
52 |
|
---|
53 | if ~isequal(hhh,'')
|
---|
54 | %default output
|
---|
55 | Data=[];
|
---|
56 | var_detect=[];
|
---|
57 | ichoice=[];%default
|
---|
58 | %open the netcdf file for reading
|
---|
59 | if ischar(nc)
|
---|
60 | if exist(nc,'file')
|
---|
61 | nc=netcdf.open(nc,'NC_NOWRITE');
|
---|
62 | testfile=1;
|
---|
63 | else
|
---|
64 | Data.Txt=['ERROR:file ' nc ' does not exist'];
|
---|
65 | return
|
---|
66 | end
|
---|
67 | else
|
---|
68 | testfile=0;
|
---|
69 | end
|
---|
70 | % short reading of global attributes
|
---|
71 | if isequal(varargin{1},'ListGlobalAttribute')
|
---|
72 | for ilist=2:numel(varargin)
|
---|
73 | try
|
---|
74 | valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),varargin{ilist});
|
---|
75 | catch
|
---|
76 | valuestr=[];
|
---|
77 | end
|
---|
78 | eval(['Data.' varargin{ilist} '=valuestr;'])
|
---|
79 | end
|
---|
80 | netcdf.close(nc)
|
---|
81 | return
|
---|
82 | end
|
---|
83 |
|
---|
84 | % reading of variables, including attributes
|
---|
85 | ListVarName=varargin{1};
|
---|
86 | [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, attributes
|
---|
87 |
|
---|
88 | % -------- read global attributes (constants)-----------
|
---|
89 | att_key={};%default
|
---|
90 | iatt_g=0;
|
---|
91 | Data.ListGlobalAttribute={};%default
|
---|
92 | for iatt=1:ngatts
|
---|
93 | keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1);
|
---|
94 | indstr1=regexp(keystr,'\\');%detect '\\'
|
---|
95 | indstr2=regexp(keystr,'\.');%detect '\.'
|
---|
96 | if isempty(indstr1) && isempty(indstr2)
|
---|
97 | valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr);
|
---|
98 | if ischar(valuestr) && length(valuestr)<200
|
---|
99 | iatt_g=iatt_g+1;
|
---|
100 | indstr1=regexp(keystr,'\\');%detect '\\'
|
---|
101 | indstr2=regexp(keystr,'\.');%detect '\.'
|
---|
102 | if isempty(indstr1) && isempty(indstr2)
|
---|
103 | eval(['Data.' keystr '=''' valuestr ''';'])
|
---|
104 | att_key{iatt_g}=keystr;
|
---|
105 | end
|
---|
106 | elseif isempty(valuestr)
|
---|
107 | iatt_g=iatt_g+1;
|
---|
108 | eval(['Data.' keystr '=[];'])
|
---|
109 | att_key{iatt_g}=keystr;
|
---|
110 | elseif isnumeric(valuestr)
|
---|
111 | iatt_g=iatt_g+1;
|
---|
112 | eval(['Data.' keystr '=valuestr;'])
|
---|
113 | att_key{iatt_g}=keystr;
|
---|
114 | end
|
---|
115 | end
|
---|
116 | end
|
---|
117 | Data.ListGlobalAttribute=att_key;
|
---|
118 |
|
---|
119 | % -------- read dimensions -----------
|
---|
120 | ListDimName={};
|
---|
121 | 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
|
---|
129 | end
|
---|
130 |
|
---|
131 | % -------- read variables -----------
|
---|
132 | var_read={}; %default
|
---|
133 | dimids={};
|
---|
134 | nbatt=[];
|
---|
135 | for ivar=1:nvars
|
---|
136 | [var_read{ivar},xtype,dimids{ivar},nbatt(ivar)] = netcdf.inqVar(nc,ivar-1);
|
---|
137 | end
|
---|
138 | var_index=1:nvars; %default set of variable indices in the netcdf file
|
---|
139 | testmulti=0;
|
---|
140 | OutputList=[];
|
---|
141 | %select input variables, if requested by the input ListVarName
|
---|
142 | if ~(isequal(ListVarName,'*')||isempty(ListVarName))
|
---|
143 | sizvar=size(ListVarName);
|
---|
144 | testmulti=(sizvar(1)>1);
|
---|
145 | var_index=zeros(1,sizvar(2));%default
|
---|
146 | if testmulti
|
---|
147 | OutputList=ListVarName(1,:);
|
---|
148 | testend=0;
|
---|
149 | for iline=1:sizvar(1)
|
---|
150 | if testend
|
---|
151 | break
|
---|
152 | 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,:)
|
---|
203 | 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
|
---|
215 | for iatt=1:nbatt(var_index(ivar))
|
---|
216 | attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1);
|
---|
217 | valuestr= netcdf.getAtt(nc,var_index(ivar)-1,attname);
|
---|
218 | if ischar(valuestr)
|
---|
219 | eval(['Data.VarAttribute{ivar}.' attname '=''' valuestr ''';'])
|
---|
220 | elseif isempty(valuestr)
|
---|
221 | eval(['Data.VarAttribute{ivar}.' attname '=[];'])
|
---|
222 | elseif isnumeric(valuestr)
|
---|
223 | eval(['Data.VarAttribute{ivar}.' attname '=valuestr;'])
|
---|
224 | end
|
---|
225 | end
|
---|
226 | end
|
---|
227 |
|
---|
228 | %select the used dimensions
|
---|
229 | 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
|
---|
235 | % list_dim=1:ndims;
|
---|
236 | dim_index=find(flag_used);
|
---|
237 | % list_dim=list_dim(dim_index);
|
---|
238 | old2new=cumsum(flag_used);
|
---|
239 | ListDimName=ListDimName(dim_index);
|
---|
240 | dim_value=dim_value(dim_index);
|
---|
241 | end
|
---|
242 | for ivar=1:length(var_read)
|
---|
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;
|
---|
250 | %variable values
|
---|
251 | if ~isempty(ListVarName)
|
---|
252 | for ivar=1:length(Data.ListVarName)
|
---|
253 | VarName=Data.ListVarName{ivar};
|
---|
254 | indstr=regexp(VarName,'-');%detect '-'
|
---|
255 | if ~isempty(indstr)
|
---|
256 | VarName(indstr)=[];
|
---|
257 | end
|
---|
258 | eval(['Data.' VarName '=double(netcdf.getVar(nc,var_index(ivar)-1));'])%read the variable data
|
---|
259 | %eval(['siz=size(Data.' VarName ');'])
|
---|
260 | % if numel(siz)<=2
|
---|
261 | %eval(['Data.' VarName '=Data.' VarName ''';'])%read the variable data
|
---|
262 | %end
|
---|
263 | end
|
---|
264 | end
|
---|
265 | % -------- close fle-----------
|
---|
266 | if testfile==1
|
---|
267 | netcdf.close(nc)
|
---|
268 | end
|
---|
269 | else
|
---|
270 | [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin);
|
---|
271 | end |
---|