source: trunk/src/nc2struct.m @ 1009

Last change on this file since 1009 was 1001, checked in by sommeria, 7 years ago

tubcorrelation added, compilation improved to accept tranform fct

File size: 14.4 KB
Line 
1
2%'nc2struct': transform a NetCDF file in a corresponding matlab structure
3% it reads all the global attributes and all variables, or a selected list.
4% The corresponding dimensions and variable attributes are then extracted
5%----------------------------------------------------------------------
6% function [Data,var_detect,ichoice,errormsg]=nc2struct(nc,varargin)
7%
8% OUTPUT:
9%  Data: structure containing all the information of the NetCDF file (or NetCDF object)
10%           with (optional)fields:
11%                    .ListGlobalAttribute: cell listing the names of the global attributes
12%                    .Att_1,Att_2... : values of the global attributes
13%                    .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
14%                    .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)
15%                    .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
16%                  .ListDimName=list of dimension (added information, not requested for field description)
17%                  .DimValue= vlalues of dimensions (added information, not requested for field description)
18%                  .VarType= integers giving the type of variable as coded by netcdf =2 for char, =4 for single,=( for double
19%  var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else.
20%            var_detect=[] in the absence of input cell array
21%  ichoice: index of the selected line in the case of multiple choice
22%        (cell array of varible names with multiple lines) , =[] by default
23%
24% INPUT:
25%  nc:  name of a NetCDF file (char string) or NetCDF object
26%  additional arguments:
27%       -no additional arguments: all the variables of the NetCDF file are read.
28%       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} )
29%         if ListVarName=[] or {}, no variable value is read (only global attributes and list of variables and dimensions)
30%         if ListVarName is absent, or = '*', ALL the variables of the NetCDF file are read.
31%         if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority in the list,
32%            while output names will be set by the first line
33%       - the string 'ListGlobalAttribute' followed by a list of attribute  names: reads only these attributes (fast reading)
34%       - the string 'TimeVarName', a string (the name of the variable considered as time), an integer or vector with integer values
35%            representing time indices to select for each variable, the cell of other input variable names.
36%       - the string 'TimeDimName', a string (the name of the dimension considered as time), an integer or vector with integer values
37%            representing time indices to select for each variable, the cell of other input variable names.
38
39%=======================================================================
40% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
41%   http://www.legi.grenoble-inp.fr
42%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
43%
44%     This file is part of the toolbox UVMAT.
45%
46%     UVMAT is free software; you can redistribute it and/or modify
47%     it under the terms of the GNU General Public License as published
48%     by the Free Software Foundation; either version 2 of the license,
49%     or (at your option) any later version.
50%
51%     UVMAT is distributed in the hope that it will be useful,
52%     but WITHOUT ANY WARRANTY; without even the implied warranty of
53%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54%     GNU General Public License (see LICENSE.txt) for more details.
55%=======================================================================
56
57function [Data,var_detect,ichoice,errormsg]=nc2struct(nc,varargin)
58errormsg='';%default error message
59if isempty(varargin)
60    varargin{1}='*';
61end
62hhh=which('netcdf.open');% look for built-in matlab NetCDF library
63
64if ~isequal(hhh,'')
65    %% default output
66    Data=[];%default
67    var_detect=[];%default
68    ichoice=[];%default
69
70    %% open the NetCDF file for reading
71    if ischar(nc)
72        if exist(nc,'file')
73            try
74                nc=netcdf.open(nc,'NC_NOWRITE');
75                testfile=1;
76            catch ME
77                errormsg=['ERROR opening ' nc ': ' ME.message];
78                return
79            end
80        else
81            errormsg=['ERROR:file ' nc ' does not exist'];
82            return
83        end
84    else
85        testfile=0;
86    end
87
88    %% short reading option for global attributes only, if the first argument is 'ListGlobalAttribute'
89    if isequal(varargin{1},'ListGlobalAttribute')
90        for ilist=2:numel(varargin)
91            valuestr=[];%default
92            try
93                valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),varargin{ilist});
94            catch ME
95            end
96            eval(['Data.' varargin{ilist} '=valuestr;'])
97        end
98        netcdf.close(nc)
99        return
100    end
101
102    %% time variable or dimension
103    input_index=1;
104    CheckTimeVar=0;
105    TimeVarName='';
106    if isequal(varargin{1},'TimeVarName')
107        TimeVarName=varargin{2};
108        CheckTimeVar=1;
109        TimeIndex=varargin{3};
110        input_index=4;% list of varibles to read is at fourth argument
111    elseif isequal(varargin{1},'TimeDimName')
112        TimeDimName=varargin{2};
113        TimeIndex=varargin{3};
114        input_index=4;
115    end
116
117    %% full reading: get the nbre of dimensions, variables, global attributes
118    ListVarName=varargin{input_index};
119    [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the NetCDF file
120
121    %%  -------- read all global attributes (constants)-----------
122    Data.ListGlobalAttribute={};%default
123    att_key=cell(1,ngatts);%default
124    for iatt=1:ngatts
125        keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1);
126        valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr);
127        keystr=regexprep(keystr,{'\','/','\.','-',' '},{'','','','',''});%remove  '\','.' or '-' if exists
128        if strcmp(keystr(1),'_')
129            keystr(1)=[];
130        end
131        try
132            if ischar(valuestr) %& length(valuestr)<200 & double(valuestr)<=122 & double(valuestr)>=48 %usual characters
133                eval(['Data.' keystr '=''' valuestr ''';'])
134            elseif isnumeric(valuestr)
135                eval(['Data.' keystr '=valuestr;'])
136            else
137                eval(['Data.' keystr '='';'])
138            end
139            att_key{iatt}=keystr;
140        catch ME
141            att_key{iatt}=['attr_' num2str(iatt)];
142            Data.(att_key{iatt})=[];
143        end
144    end
145    Data.ListGlobalAttribute=att_key;
146
147    %%  -------- read dimension names-----------
148    ListDimNameNetcdf=cell(1,ndims);
149    dim_value=zeros(1,ndims);
150    for idim=1:ndims %loop on the dimensions of the NetCDF file
151        [ListDimNameNetcdf{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);%get name and value of each dimension
152    end
153    if ~isempty(ListDimNameNetcdf)
154        flag_used=zeros(1,ndims);%initialize the flag indicating the selected dimensions in the list (0=unused)
155    end
156    if isequal(varargin{1},'TimeDimName')% time dimension introduced
157        TimeDimIndex=find(strcmp(TimeDimName,ListDimNameNetcdf));
158        if isempty(TimeDimIndex)
159            errormsg=['requested time dimension ' varargin{2} ' not found'];
160            return
161        end
162        if dim_value(TimeDimIndex)<varargin{3}
163           errormsg=['requested time index ' num2str(varargin{3}) ' exceeds matrix dimension'];
164            return
165        end
166    end
167
168    %%  -------- read names of variables -----------
169    ListVarNameNetcdf=cell(1,nvars); %default
170    dimids=cell(1,nvars);
171    nbatt=zeros(1,nvars);
172    for ncvar=1:nvars %loop on the variables of the NetCDF file
173        %get name, type, dimensions and attribute numbers of each variable
174        [ListVarNameNetcdf{ncvar},xtype(ncvar),dimids{ncvar},nbatt(ncvar)] = netcdf.inqVar(nc,ncvar-1);
175    end
176%     testmulti=0;
177    if isequal(ListVarName,'*')||isempty(ListVarName)
178        var_index=1:nvars; %all the variables are selected in the NetCDF file
179        Data.ListVarName=ListVarNameNetcdf;
180    else   %select input variables, if requested by the input ListVarName
181        check_keep=ones(1,size(ListVarName,2));
182        for ivar=1:size(ListVarName,2) % check redondancy of variable names
183            if ~isempty(find(strcmp(ListVarName{1,ivar},ListVarName(1:ivar-1)), 1))
184                check_keep(ivar)=0;% the variable #ivar is already in the list
185            end
186        end
187        ListVarName=ListVarName(:,logical(check_keep));
188        if size(ListVarName,1)>1 %multiple choice of variable ranked by order of priority
189            for iline=1:size(ListVarName,1)
190                search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of NetCDF variables
191                if ~isempty(search_index)
192                    break % go to the next line
193                end
194            end
195            ichoice=iline-1;%selected line number in the list of input names of variables
196        else
197            iline=1;
198        end
199        %ListVarName=ListVarName(iline,:);% select the appropriate option for input variable (lin ein the input name matrix)
200        if CheckTimeVar
201            TimeVarIndex=find(strcmp(TimeVarName,ListVarNameNetcdf),1); %look for the index of the time variable in the netcdf list
202            if isempty(TimeVarIndex)
203                errormsg='requested variable for time is missing';
204                return
205            end
206            TimeDimIndex=dimids{TimeVarIndex}(1)+1;
207            ListVarName=[ListVarName {TimeVarName}];
208        end
209        var_index=zeros(1,size(ListVarName,2));%default list of variable indices
210        for ivar=1:size(ListVarName,2)
211            search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of NetCDF file
212            if ~isempty(search_index)
213                var_index(ivar)=search_index;%index of the netcdf list corresponding to the input list index ivar
214            end
215        end
216        var_detect=(var_index~=0);%=1 for detected variables
217        list_index=find(var_index);% indices in the input list corresponding to a detected variable
218        var_index=var_index(list_index);% NetCDF variable indices corresponding to the output list of read variable
219        Data.ListVarName=ListVarName(1,list_index);%the first line of ListVarName sets the output names of the variables
220    end
221
222    %% get the dimensions and attributes associated to  variables
223    var_dim=cell(size(var_index));% initiate list of dimensions for variables
224    for ivar=1:length(var_index)
225        var_dim{ivar}=dimids{var_index(ivar)}+1; %netcdf dimension indices used by the variable #ivar
226        Data.VarDimName{ivar}=ListDimNameNetcdf(var_dim{ivar});
227        flag_used(var_dim{ivar})=ones(size(var_dim{ivar}));%flag_used =1 for the indices of used dimensions
228        for iatt=1:nbatt(var_index(ivar))
229            attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1);
230            valuestr= netcdf.getAtt(nc,var_index(ivar)-1,attname);
231            attname=regexprep(attname,{'\','/','\.','-',' '},{'','','','',''});%remove  '\','.' or '-' if exists
232            if strcmp(attname(1),'_')
233                attname(1)=[];
234            end
235            try
236                if ~isempty(valuestr)
237                    Data.VarAttribute{ivar}.(attname)=valuestr;
238                end
239            catch ME
240                display(attname)
241                display(valuestr)
242                display(ME.message)
243                Data.VarAttribute{ivar}.(['atrr_' num2str(iatt)])='not read';
244            end
245        end
246    end
247
248    %% select the dimensions used for the set of input variables
249    if ~isempty(var_index)
250        dim_index=find(flag_used);%list of netcdf dimensions indices corresponding to used dimensions
251        Data.ListDimName=ListDimNameNetcdf(dim_index);
252        Data.DimValue=dim_value(dim_index);
253        if input_index==4% if a dimension is selected as time
254            Data.DimValue(TimeDimIndex)=numel(TimeIndex);
255        end
256    end
257
258    %% get the values of the input variables
259    if  ~isempty(ListVarName)
260        for ivar=1:length(var_index)
261            VarName=Data.ListVarName{ivar};
262            VarName=regexprep(VarName,'-','_'); %suppress '-' if it exists in the NetCDF variable name (leads to errors in matlab)
263%             CheckSub=0;
264            if input_index==4% if a dimension is selected as time
265                ind_vec=zeros(1,numel(var_dim{ivar}));% vector with zeros corresponding to al the dimensions of the variable VarName
266                ind_size=dim_value(var_dim{ivar});% vector giving the size (for each dimension) of the variable VarName
267                index_time=find(var_dim{ivar}==TimeDimIndex);
268                if ~isempty(index_time)
269                    if ind_size(index_time)<max(TimeIndex)
270                        errormsg=['requested index ' num2str(TimeIndex) ' exceeds matrix dimension'];
271                        return
272                    end
273                    ind_vec(index_time)=TimeIndex-1;% selected index(or indices) to read
274                    ind_size(index_time)=numel(TimeIndex);%length of the selected set of time indices
275                    if numel(TimeIndex)==1 && ~strcmp(VarName,TimeVarName)
276                        Data.VarDimName{ivar}(index_time)=[];% for a single selected time remove the time in the list of dimensions (except for tTime itself)
277                    end
278                end
279                Data.(VarName)=netcdf.getVar(nc,var_index(ivar)-1,ind_vec,ind_size); %read the variable data
280                Data.(VarName)=squeeze(Data.(VarName));%remove singeton dimension
281            else
282                Data.(VarName)=netcdf.getVar(nc,var_index(ivar)-1); %read the whole variable data
283            end
284            if xtype(var_index(ivar))==5
285                Data.(VarName)=double(Data.(VarName)); %transform to double for single pecision
286            end
287        end
288    end
289    Data.VarType=xtype(var_index);
290
291    %%  -------- close fle-----------
292    if testfile==1
293        netcdf.close(nc)
294    end
295
296    %% old NetCDF library
297else
298    [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin);
299end
Note: See TracBrowser for help on using the repository browser.