source: trunk/src/nc2struct_toolbox.m @ 131

Last change on this file since 131 was 56, checked in by sommeria, 15 years ago

uvmat: edit vector button activated
debugging and cleaning after modifications made with the windows version

File size: 9.8 KB
Line 
1%'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY
2%----------------------------------------------------------------------
3% function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListField)
4%
5% OUTPUT:
6%  Data: structure containing all the information of the netcdf file (or netcdf object)
7%           with fields:
8%    .ListGlobalAttribute: cell listing the names of the global attributes
9%        .Att_1,Att_2... : values of the global attributes
10%            .ListDimName: cell listing the names of the array dimensions
11%               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
12%            .ListVarName: cell listing the names of the variables
13%            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
14%            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
15%           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
16%        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
17%  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.
18%  ichoice: = line
19%
20%INPUT:
21%     nc:      name of a netcdf file (char string) or netcdf object   
22% ListField: optional list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
23%         if ListField is absent or ='*', ALL the attributes and variables are read.  %     
24%        if  ListField='ListGlobalAttribute', followed by the arguments 'name1', name2'..., only thes global attributes will be read (short option)
25%        if  ListField=[] or{}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes)
26%        if ListField 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%
29%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
30%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
31%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
32%     This file is part of the toolbox UVMAT.
33%
34%     UVMAT is free software; you can redistribute it and/or modify
35%     it under the terms of the GNU General Public License as published by
36%     the Free Software Foundation; either version 2 of the License, or
37%     (at your option) any later version.
38%
39%     UVMAT is distributed in the hope that it will be useful,
40%     but WITHOUT ANY WARRANTY; without even the implied warranty of
41%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
43%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
44
45function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin)
46
47List=varargin{1};
48%default output
49Data=[];
50var_detect=[];
51ichoice=[];%default
52
53%open the netcdf file for reading
54if ischar(nc)
55    if exist(nc,'file') % rmq: time for exist search = 0.5 ms CPU
56        nc=netcdf(nc,'nowrite'); % rmq: time needed for opening = 2 ms CPU
57        testfile=1;
58    else
59       Data.Txt=['ERROR:file ' nc ' does not exist'];
60       return
61    end
62else
63    testfile=0;
64end
65
66% short reading of global attributes
67if ~isempty(List) && isequal(List{1},'ListGlobalAttribute')
68    for ilist=2:numel(List)
69        att_str=List{ilist};
70        eval(['Data.' att_str '=nc.' att_str '(:);'])
71    end
72    close(nc)
73        %total time from beginning : 15 ms for a single attribute
74   return
75end
76
77% reading of variables, including attributes
78if isempty(List)
79   ListVarName='*';
80else
81%     if isempty(List{1})
82%         ListVarName='*';
83%     else
84        ListVarName=List{1}; 
85%     end
86end
87%  -------- read global attributes -----------             
88att_read=att(nc);%cell of 'global attributes' (nc objects), CPU time 30 ms   
89att_key={};%default
90iatt_g=0;
91for iatt=1:length(att_read)
92    aa=att_read{iatt};
93    keystr=name(aa);
94    indstr1=regexp(keystr,'\\');%replace dots'
95    indstr2=regexp(keystr,'\.');%replace dots'
96    if ~isequal(keystr,'title') % PROBLEM WITH civx files do not read 'title'
97        if  isempty(indstr1) && isempty(indstr2) %
98           eval(['valuestr=nc.' keystr '(:);'])
99            if ischar(valuestr) && length(valuestr)<200
100                iatt_g=iatt_g+1;
101                indstr1=regexp(keystr,'\\');%replace dots'
102                indstr2=regexp(keystr,'\.');%replace dots'
103                if isempty(indstr1) && isempty(indstr2)
104                    eval(['Data.' keystr '=''' valuestr ''';'])
105                    att_key{iatt_g}=keystr;
106                end
107            elseif isempty(valuestr)
108                iatt_g=iatt_g+1;
109                eval(['Data.' keystr '=[];'])
110                att_key{iatt_g}=keystr;
111            elseif isnumeric(valuestr)
112                iatt_g=iatt_g+1;
113                eval(['Data.' keystr '=valuestr;'])
114                att_key{iatt_g}=keystr;
115            end
116        end
117    end
118end
119Data.ListGlobalAttribute=att_key;
120nbattr=length(att_key);
121neworder=[nbattr+1 (1:nbattr)];
122Data=orderfields(Data,neworder);
123
124%  -------- read dimensions -----------
125dim_read=dim(nc);%cell of variable dimension names (nc objects): CPU time 0.0013
126dim_name={};
127dim_value=[];
128for idim=1:length(dim_read);
129    aa=dim_read{idim};
130    if ~isempty(aa)
131    dim_name{idim}=name(aa);
132    dim_value(idim)=length(aa);
133    end
134end
135if ~isempty(dim_name) && ~isempty(dim_value)
136    Data.ListDimName=dim_name;
137    Data.DimValue=dim_value;
138    used=zeros(1,length(dim_value));%initialize test of used dimensions
139end
140
141%  -------- read variables -----------
142var_read={}; %default
143testmulti=0;
144OutputList=[];
145if isequal(ListVarName,'*')%|| isempty(ListVarName)
146     var_read=var(nc);%cell of all variables
147elseif ~isempty(ListVarName)
148    sizvar=size(ListVarName);
149    testmulti=(sizvar(1)>1);
150    if testmulti
151        OutputList=ListVarName(1,:);
152        testend=0;
153        for iline=1:sizvar(1)
154            if testend
155                break
156            end
157            for ivar=1:sizvar(2)
158                var_read{ivar}=[];%default
159                var_detect(ivar)=0;%default
160                VarName=ListVarName{iline,ivar};
161                if ~isempty(VarName)
162                     var_read{ivar}=nc{VarName};%select the input variable names
163                     if ivar==1
164                        if isempty (var_read{ivar})
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                            var_detect(ivar)=1;
170                        end
171                     else
172                          var_detect(ivar)=~isempty (var_read{ivar});
173                     end
174                end
175            end
176        end
177        if ~isempty(find(var_detect,1))
178            OutputList=OutputList(find(var_detect)); 
179        end
180    else   %single list of input variables
181        var_detect=ones(size(ListVarName));
182        for ivar=1:sizvar(2)
183            var_read{ivar}=nc{ListVarName{ivar}};%select the input variable names
184            var_detect(ivar)=~isempty(var_read{ivar});
185        end
186    end
187    var_read=var_read(find(var_detect));
188end
189
190% var_dim_index=[]; %default
191Data.ListVarName={};%default
192for ivar=1:length(var_read)
193    vv=var_read{ivar};
194    Data.ListVarName{ivar}=name(vv);%name of the variable
195    if testmulti
196        Data.ListVarName{ivar}=OutputList{ivar};
197    else
198        Data.ListVarName{ivar}=name(vv);%name of the variable
199    end
200    var_dim=dim(vv);%dimension netcdf object of the variable
201    for ivardim=1:length(var_dim)
202        var_dim_name=name(var_dim{ivardim});%name of the dimension
203        for idim=1:length(dim_name)% find the index of the current dimension in the list of dimensions
204            if isequal(dim_name{idim},var_dim_name)
205                Data.VarDimIndex{ivar}(ivardim)=idim;
206                used(idim)=1;
207                break
208            end
209        end
210    end
211    Data.VarDimName{ivar}={};
212    %variable attributes
213    Data.VarAttribute{ivar}=[];%initialisation of the list of variable attributes
214    %variable attributes
215    att_read=att(vv);
216    for iatt=1:length(att_read)
217        aa=att_read{iatt};
218        eval(['valuestr=vv.' name(aa) '(:);'])
219        if ischar(valuestr)
220            eval(['Data.VarAttribute{ivar}.' name(aa) '=''' valuestr ''';'])
221        elseif isempty(valuestr)
222            eval(['Data.VarAttribute{ivar}.' name(aa) '=[];'])
223        elseif isnumeric(valuestr)
224            eval(['Data.VarAttribute{ivar}.' name(aa) '=valuestr;'])
225        end
226    end
227end
228
229%select the used dimensions
230if isempty(var_read)
231    if isfield(Data,'ListDimName') && isfield(Data,'DimValue')
232    Data=rmfield(Data,'ListDimName');
233    Data=rmfield(Data,'DimValue');
234    end
235else
236    old_dim_index=find(used); %dimension indices which are used by the selected variables
237    old2new=cumsum(used);
238    Data.ListDimName=Data.ListDimName(old_dim_index);
239    Data.DimValue=Data.DimValue(old_dim_index);
240end
241for ivar=1:length(var_read)
242    Data.VarDimIndex{ivar}=(old2new(Data.VarDimIndex{ivar}));
243    Data.VarDimName{ivar}=(Data.ListDimName(Data.VarDimIndex{ivar}));
244end
245%variable values
246
247if  ~isempty(ListVarName)
248    for ivar=1:length(Data.ListVarName)
249        vv=var_read{ivar};
250        vdata=vv(:);%data array of the field variable
251        eval(['Data.' Data.ListVarName{ivar} '=vdata;'])%read the variable data
252    end
253end
254%  -------- close fle-----------
255if testfile==1
256    close(nc)
257end
258
259%total time from beginning : 150 ms for a full civ2 field, 65 ms for four fields
260
Note: See TracBrowser for help on using the repository browser.