source: trunk/src/nc2struct.m @ 861

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