source: trunk/src/find_field_indices.m @ 501

Last change on this file since 501 was 501, checked in by sommeria, 9 years ago

various improvements: read input parameters for civ. Order of panels rationalised.
rationalisation in find_field_indices
simplification of default PARAM.xml, suppress unneeded information.

File size: 14.0 KB
Line 
1%'find_file_indices': test field structure for input in proj_field and plot_field
2%    group the variables  into 'fields' with common dimensions
3%------------------------------------------------------------------------
4% function  [CellVarIndex,NbDim,CellVarType,errormsg]=find_field_indices(Data)
5%
6% OUTPUT:
7% CellVaxIndex: cell whose elements are arrays of indices in the list data.ListVarName 
8%              CellvarIndex{i} represents a set of variables with the same dimensions
9% NbDim: array with the length of CellVarIndex, giving its  space dimension
10% CellVarType: cell array of structures with fields
11%      .coord_x, y, z: indices (in .ListVarname) of variables representing  unstructured coordinates x, y, z
12%      .vector_x,_y,_z: indices of variables giving the vector components x, y, z
13%      .warnflag: index of warnflag
14%      .errorflag: index of error flag
15%      .ancillary: indices of ancillary variables
16%      .image   : B/W image, (behaves like scalar)
17%      .color : color image, the last index, which is not a coordinate variable, represent the 3 color components rgb
18%      .discrete: like scalar, but set of data points without continuity, represented as dots in a usual plot, instead of continuous lines otherwise
19%      .scalar: scalar field (default)
20%      .coord: vector of indices of coordinate variables corresponding to matrix dimensions
21% errormsg: error message
22%   
23% INPUT:
24% Data: structure representing fields, output of check_field_structure
25%            .ListVarName: cell array listing the names (cahr strings) of the variables
26%            .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName
27%            .VarAttribute: cell array of structures containing the variable attributes:
28%                     .VarAttribute{ilist}.key=value, where ilist is the variable
29%                     index, key is the name of the attribute, value its value (char string or number)
30%
31% HELP:
32% to get the dimensions of arrays common to the field #icell
33%         VarIndex=CellVarIndex{icell}; % list of variable indices
34%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
35%
36%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
37%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
38%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
39%     This file is part of the toolbox UVMAT.
40%
41%     UVMAT is free software; you can redistribute it and/or modify
42%     it under the terms of the GNU General Public License as published by
43%     the Free Software Foundation; either version 2 of the License, or
44%     (at your option) any later version.
45%
46%     UVMAT is distributed in the hope that it will be useful,
47%     but WITHOUT ANY WARRANTY; without even the implied warranty of
48%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49%     GNU General Public License (file UVMAT/COPYING.txt) for more details.for input in proj_field and plot_field
50%    group the variables  into 'fields' with common dimensions
51%------------------------------------------------------------------------
52% function  [CellVarIndex,NbDim,CellVarType,errormsg]=find_field_indices(Data)
53%
54% OUTPUT:
55% CellVaxIndex: cell whose elements are arrays of indices in the list data.ListVarName 
56%              CellvarIndex{i} represents a set of variables with the same dimensions
57% NbDim: array with the length of CellVarIndex, giving its  space dimension
58% CellVarType: cell array of structures with fields
59%      .coord_x, y, z: indices (in .ListVarname) of variables representing  unstructured coordinates x, y, z
60%      .vector_x,_y,_z: indices of variables giving the vector components x, y, z
61%      .warnflag: index of warnflag
62%      .errorflag: index of error flag
63%      .ancillary: indices of ancillary variables
64%      .image   : B/W image, (behaves like scalar)
65%      .color : color image, the last index, which is not a coordinate variable, represent the 3 color components rgb
66%      .discrete: like scalar, but set of data points without continuity, represented as dots in a usual plot, instead of continuous lines otherwise
67%      .scalar: scalar field (default)
68%      .coord: vector of indices of coordinate variables corresponding to matrix dimensions
69% errormsg: error message
70%   
71% INPUT:
72% Data: structure representing fields, output of check_field_structure
73%            .ListVarName: cell array listing the names (cahr strings) of the variables
74%            .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName
75%            .VarAttribute: cell array of structures containing the variable attributes:
76%                     .VarAttribute{ilist}.key=value, where ilist is the variable
77%                     index, key is the name of the attribute, value its value (char string or number)
78%
79% HELP:
80% to get the dimensions of arrays common to the field #icell
81%         VarIndex=CellVarIndex{icell}; % list of variable indices
82%         DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell
83%
84%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
85%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
86%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
87%     This file is part of the toolbox UVMAT.
88%
89%     UVMAT is free software; you can redistribute it and/or modify
90%     it under the terms of the GNU General Public License as published by
91%     the Free Software Foundation; either version 2 of the License, or
92%     (at your option) any later version.
93%
94%     UVMAT is distributed in the hope that it will be useful,
95%     but WITHOUT ANY WARRANTY; without even the implied warranty of
96%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
97%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
98%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
99
100function [CellVarIndex,NbDim,CellVarType,errormsg]=find_field_indices(Data)
101CellVarIndex={};
102
103NbDim=[];
104CellVarType=[];
105errormsg=[];
106nbvar=numel(Data.ListVarName);%number of field variables
107icell=0;
108
109NbDim=[];
110ivardim=0;
111VarDimIndex=[];
112VarDimName={};
113if ~isfield(Data,'VarDimName')
114    errormsg='missing .VarDimName';
115    return
116end
117
118%% role of variables
119Role=num2cell(blanks(nbvar));%initialize a cell array of nbvar blanks
120Role=regexprep(Role,' ','scalar'); % Role set to 'scalar' by default
121if isfield(Data,'VarAttribute')
122    for ivar=1:numel(Data.VarAttribute)
123        if isfield(Data.VarAttribute{ivar},'Role')
124            Role{ivar}=Data.VarAttribute{ivar}.Role;
125        end
126    end
127end
128
129%% loop on the list of variables, group them by common dimensions
130for ivar=1:nbvar
131    if ischar(Data.VarDimName{ivar})
132        Data.VarDimName{ivar}=Data.VarDimName(ivar);%transform char chain into cell
133    end
134    DimCell=Data.VarDimName{ivar}; %dimensions associated with the variable #ivar
135    testnewcell=1;
136    for icell_prev=1:numel(CellVarIndex)%detect whether the dimensions of ivar fit with an existing cell
137        PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev
138        PrevDimCell=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names
139        if isequal(PrevDimCell,DimCell)
140            CellVarIndex{icell_prev}=[CellVarIndex{icell_prev} ivar];% add variable index #ivar to the cell #icell_prev
141            testnewcell=0; %existing cell detected
142            break
143        end
144    end
145    if testnewcell
146        icell=icell+1;
147        CellVarIndex{icell}=ivar;%put the current variable index in the new cell
148        NbDim(icell)=numel(DimCell);%default
149    end
150   
151    %look for dimension variables
152%     if numel(DimCell)==1% if the variable has a single dimension
153%         if strcmp(DimCell{1},Data.ListVarName{ivar}) %|| strcmp(Role{ivar},'dimvar')
154%             ivardim=ivardim+1;
155%             VarDimIndex(ivardim)=ivar;%index of the variable
156%             VarDimName{ivardim}=DimCell{1};%name of the dimension
157%         end
158%     end
159end
160
161%% find dimension variables
162checksinglecell=cellfun(@numel,CellVarIndex)==1 & NbDim==1;% find isolated cells with a single dimension
163ind_dim_var_cell=find(checksinglecell);
164%CoordType(ind_dim_var_cell)='dim_var';% to be used in output
165for icoord=1:numel(ind_dim_var_cell)
166VarDimIndex(icoord)=CellVarIndex{ind_dim_var_cell(icoord)};
167VarDimName{icoord}=Data.VarDimName{VarDimIndex(icoord)}{1};
168end
169
170%% find the spatial dimensions and vector components
171ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
172    'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y
173% NbDim=zeros(size(CellVarIndex));%default
174
175for ilist=1:numel(ListRole)
176    VarType.(ListRole{ilist})=find(strcmp(ListRole{ilist},Role));
177end
178%look for tps coordinates
179if ~isempty(VarType.coord_tps)
180    VarType.subrange_tps=[];
181    VarType.nbsites_tps=[];
182    select=zeros(1,numel(VarType.coord_tps));
183    for ifield=1:numel(VarType.coord_tps)
184        DimCell=Data.VarDimName{VarType.coord_tps(ifield)};
185        if numel(DimCell)==3
186            for ivardim=1:numel(Data.VarDimName)
187                if strcmp(Data.VarDimName{ivardim},DimCell{3})
188                    VarType.nbsites_tps=[VarType.nbsites_tps ivardim];
189                    select(ifield)=select(ifield)+1;
190                elseif strcmp(Data.VarDimName{ivardim}{1},DimCell{2}) && strcmp(Data.VarDimName{ivardim}{3},DimCell{3})
191                    VarType.subrange_tps=[VarType.subrange_tps ivardim];
192                    select(ifield)=select(ifield)+1;
193                end
194            end
195        end
196    end
197    VarType.coord_tps=VarType.coord_tps(select==2);
198    VarType.subrange_tps=VarType.subrange_tps(select==2);
199    VarType.nbsites_tps=VarType.nbsites_tps(select==2);
200end
201
202index_remove=[];
203CellVarType=cell(1,length(CellVarIndex));
204for icell=1:length(CellVarIndex)
205    if checksinglecell(icell)
206        continue
207    end
208    VarIndex=CellVarIndex{icell};%set of variable indices with the same dim
209    check_remove=0;
210    for ifield=1:numel(VarType.coord_tps)
211        if isequal(VarIndex,VarType.coord_tps(ifield))||isequal(VarIndex,VarType.subrange_tps(ifield))||isequal(VarIndex,VarType.nbsites_tps(ifield))
212            index_remove=[index_remove icell];% removes Coord_tps as field cell
213            check_remove=1;
214        end
215    end
216   
217    if ~check_remove
218        for ilist=1:numel(ListRole)
219            CellVarType{icell}.(ListRole{ilist})=VarIndex(find(strcmp(ListRole{ilist},Role(VarIndex))));
220        end
221        DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell
222        if numel(CellVarType{icell}.coord_x)>1 || numel(CellVarType{icell}.coord_y)>1 || numel(CellVarType{icell}.coord_z)>1
223            errormsg='multiply defined coordinates  in the same cell';
224            return
225        end
226        % case of x cordinate marked as a dimension variable (var name=dimension name)
227        if isempty(CellVarType{icell}.coord_x)
228            var_dim_index=find(strcmp(DimCell{1},Data.ListVarName(VarIndex)));
229            if ~isempty(var_dim_index)
230                CellVarType{icell}.coord_x=VarIndex(var_dim_index);
231            end
232        end         
233        if numel(CellVarType{icell}.errorflag)>1
234            errormsg='multiply defined error flag in the same cell';
235            return
236        end
237        if numel(CellVarType{icell}.warnflag)>1
238            errormsg='multiply defined warning flag in the same cell';
239            return
240        end
241        test_coord=0;
242        % look for unstructured coordinates
243        if numel(VarIndex)>1
244            if ~isempty(CellVarType{icell}.coord_z)
245                NbDim(icell)=3;
246                test_coord=1;
247            elseif ~isempty(CellVarType{icell}.coord_y)
248                NbDim(icell)=2;
249                test_coord=1;
250            elseif ~isempty(CellVarType{icell}.coord_x)
251                NbDim(icell)=1;
252                test_coord=1;
253            end
254        end
255        % look for coordinates variables
256        coord=zeros(1,numel(DimCell));%default
257        %     if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
258        if  ~test_coord && ~isempty(VarDimName)
259            for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
260                ind_coord=find(strcmp(DimCell{idim},VarDimName));
261                if ~isempty(ind_coord)
262                    coord(idim)=VarDimIndex(ind_coord);
263                end
264%                 for ivardim=1:numel(VarDimName)
265%                     if strcmp(VarDimName{ivardim},DimCell{idim})
266%                         coord(idim)=VarDimIndex(ivardim);
267%                         break
268%                     end
269%                 end
270            end
271            NbDim(icell)=numel(find(coord));
272        end
273        CellVarType{icell}.coord=coord;
274        %look for tps data
275        if ~isempty(VarType.coord_tps)
276            for ilist=1:numel(VarType.coord_tps)
277            tps_dimnames=Data.VarDimName{VarType.coord_tps(ilist)};
278            if length(tps_dimnames)==3 && strcmp(tps_dimnames{1},DimCell{1}) && strcmp(tps_dimnames{3},DimCell{2})
279                CellVarIndex{icell}=[CellVarIndex{icell} VarType.coord_tps(ilist) VarType.nbsites_tps(ilist) VarType.subrange_tps(ilist)];
280                CellVarType{icell}.coord_tps=VarType.coord_tps(ilist);
281                CellVarType{icell}.nbsites_tps=VarType.nbsites_tps(ilist);
282                CellVarType{icell}.subrange_tps=VarType.subrange_tps(ilist);
283                if isfield(Data,'ListDimName')
284                    dim_index=find(strcmp(tps_dimnames{2},Data.ListDimName));
285                    NbDim(icell)=Data.DimValue(dim_index);
286                else
287                NbDim(icell)=size(Data.(Data.ListVarName{VarType.coord_tps(ilist)}),2);
288                end
289            end
290            end
291        end
292    end
293end
294if ~isempty(index_remove)
295    CellVarIndex(index_remove)=[];
296    CellVarType(index_remove)=[];
297    NbDim(index_remove)=[];
298end
Note: See TracBrowser for help on using the repository browser.