source: trunk/src/nc2struct.m @ 693

Last change on this file since 693 was 693, checked in by sommeria, 11 years ago

bug repaired geometry_calib/Detect grid, + corrections in get_field

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