source: trunk/src/find_field_indices.m @ 494

Last change on this file since 494 was 494, checked in by sommeria, 12 years ago

various bugs corrected after testing in Windows OS. Introduction
of filter tps

File size: 12.9 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=mat2cell(blanks(nbvar),1,ones(1,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    DimCell=Data.VarDimName{ivar}; %dimensions associated with the variable #ivar
132    if ischar(DimCell)
133        DimCell={DimCell};
134        Data.VarDimName{ivar}={Data.VarDimName{ivar}};%transform char chain into cell
135    end
136    testnewcell=1;
137    for icell_prev=1:numel(CellVarIndex)%detect whether the dimensions of ivar fit with an existing cell
138        PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev
139        PrevDimName=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names
140        if isequal(PrevDimName,DimCell)
141            CellVarIndex{icell_prev}=[CellVarIndex{icell_prev} ivar];% add variable index #ivar to the cell #icell_prev
142            testnewcell=0; %existing cell detected
143            break
144        end
145    end
146    if testnewcell
147        icell=icell+1;
148        CellVarIndex{icell}=ivar;%put the current variabl index in the new cell
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 the spatial dimensions and vector components
162ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',...
163    'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y
164NbDim=zeros(size(CellVarIndex));%default
165
166for ilist=1:numel(ListRole)
167    VarType.(ListRole{ilist})=find(strcmp(ListRole{ilist},Role));
168end
169%look for tps coordinates
170if ~isempty(VarType.coord_tps)
171    VarType.subrange_tps=[];
172    VarType.nbsites_tps=[];
173    for ifield=1:numel(VarType.coord_tps)
174        select(ifield)=0;
175        DimCell=Data.VarDimName{VarType.coord_tps(ifield)};
176        if numel(DimCell)==3
177            for ivardim=1:numel(Data.VarDimName)
178                if strcmp(Data.VarDimName{ivardim},DimCell{3})
179                    VarType.nbsites_tps=[VarType.nbsites_tps ivardim];
180                    select(ifield)=select(ifield)+1;
181                elseif strcmp(Data.VarDimName{ivardim}{1},DimCell{2}) && strcmp(Data.VarDimName{ivardim}{3},DimCell{3})
182                    VarType.subrange_tps=[VarType.subrange_tps ivardim];
183                    select(ifield)=select(ifield)+1;
184                end
185            end
186        end
187    end
188    VarType.coord_tps=VarType.coord_tps(select==2);
189    VarType.subrange_tps=VarType.subrange_tps(select==2);
190    VarType.nbsites_tps=VarType.nbsites_tps(select==2);
191end
192
193index_remove=[];
194CellVarType=cell(1,length(CellVarIndex));
195for icell=1:length(CellVarIndex)
196    VarIndex=CellVarIndex{icell};%set of variable indices with the same dim
197    check_remove=0;
198    for ifield=1:numel(VarType.coord_tps)
199        if isequal(VarIndex,VarType.coord_tps(ifield))||isequal(VarIndex,VarType.subrange_tps(ifield))||isequal(VarIndex,VarType.nbsites_tps(ifield))
200            index_remove=[index_remove icell];% removes Coord_tps as field cell
201            check_remove=1;
202        end
203    end
204   
205    if ~check_remove
206        for ilist=1:numel(ListRole)
207            CellVarType{icell}.(ListRole{ilist})=VarIndex(find(strcmp(ListRole{ilist},Role(VarIndex))));
208        end
209        DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell
210        if numel(CellVarType{icell}.coord_x)>1 || numel(CellVarType{icell}.coord_y)>1 || numel(CellVarType{icell}.coord_z)>1
211            errormsg='multiply defined coordinates  in the same cell';
212            return
213        end
214        if numel(CellVarType{icell}.errorflag)>1
215            errormsg='multiply defined error flag in the same cell';
216            return
217        end
218        if numel(CellVarType{icell}.warnflag)>1
219            errormsg='multiply defined warning flag in the same cell';
220            return
221        end
222        test_coord=0;
223        % look for unstructured coordinates
224        if numel(VarIndex)>1
225            if ~isempty(CellVarType{icell}.coord_z)
226                NbDim(icell)=3;
227                test_coord=1;
228            elseif ~isempty(CellVarType{icell}.coord_y)
229                NbDim(icell)=2;
230                test_coord=1;
231            elseif ~isempty(CellVarType{icell}.coord_x)
232                NbDim(icell)=1;
233                test_coord=1;
234            end
235        end
236        % look for coordinates variables
237        coord=zeros(1,numel(DimCell));%default
238        %     if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found
239        if  ~test_coord && ~isempty(VarDimName)
240            for idim=1:numel(DimCell)   %loop on the dimensions of the variables in cell #icell
241                for ivardim=1:numel(VarDimName)
242                    if strcmp(VarDimName{ivardim},DimCell{idim})
243                        coord(idim)=VarDimIndex(ivardim);
244                        break
245                    end
246                end
247            end
248            NbDim(icell)=numel(find(coord));
249        end
250        CellVarType{icell}.coord=coord;
251        %look for tps data
252        if ~isempty(VarType.coord_tps)
253            for ilist=1:numel(VarType.coord_tps)
254            tps_dimnames=Data.VarDimName{VarType.coord_tps(ilist)};
255            if length(tps_dimnames)==3 && strcmp(tps_dimnames{1},DimCell{1}) && strcmp(tps_dimnames{3},DimCell{2})
256                CellVarIndex{icell}=[CellVarIndex{icell} VarType.coord_tps(ilist) VarType.nbsites_tps(ilist) VarType.subrange_tps(ilist)];
257                CellVarType{icell}.coord_tps=VarType.coord_tps(ilist);
258                CellVarType{icell}.nbsites_tps=VarType.nbsites_tps(ilist);
259                CellVarType{icell}.subrange_tps=VarType.subrange_tps(ilist);
260                if isfield(Data,'ListDimName')
261                    dim_index=find(strcmp(tps_dimnames{2},Data.ListDimName));
262                    NbDim(icell)=Data.DimValue(dim_index);
263                else
264                NbDim(icell)=size(Data.(Data.ListVarName{VarType.coord_tps(ilist)}),2);
265                end
266            end
267            end
268        end
269    end
270end
271if ~isempty(index_remove)
272    CellVarIndex(index_remove)=[];
273    CellVarType(index_remove)=[];
274    NbDim(index_remove)=[];
275end
Note: See TracBrowser for help on using the repository browser.