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,VarType,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 | % VarType: 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 | % .ListDimName: cell listing the names of the array dimensions |
---|
26 | % .ListVarName: cell listing the names of the variables |
---|
27 | % .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName |
---|
28 | % |
---|
29 | % HELP: |
---|
30 | % to get the dimensions of arrays common to the field #icell |
---|
31 | % VarIndex=CellVarIndex{icell}; % list of variable indices |
---|
32 | % DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell |
---|
33 | % |
---|
34 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
35 | % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. |
---|
36 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
37 | % This file is part of the toolbox UVMAT. |
---|
38 | % |
---|
39 | % UVMAT is free software; you can redistribute it and/or modify |
---|
40 | % it under the terms of the GNU General Public License as published by |
---|
41 | % the Free Software Foundation; either version 2 of the License, or |
---|
42 | % (at your option) any later version. |
---|
43 | % |
---|
44 | % UVMAT is distributed in the hope that it will be useful, |
---|
45 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
46 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
47 | % GNU General Public License (file UVMAT/COPYING.txt) for more details. |
---|
48 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
49 | |
---|
50 | function [CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(Data) |
---|
51 | CellVarIndex={}; |
---|
52 | NbDim=[]; |
---|
53 | VarType=[]; |
---|
54 | errormsg=[]; |
---|
55 | nbvar=numel(Data.ListVarName);%number of field variables |
---|
56 | icell=0; |
---|
57 | ivardim=0; |
---|
58 | VarDimIndex=[]; |
---|
59 | VarDimName={}; |
---|
60 | |
---|
61 | if ~isfield(Data,'VarDimName') |
---|
62 | errormsg='missing .VarDimName'; |
---|
63 | return |
---|
64 | end |
---|
65 | |
---|
66 | %loop on the list of variables |
---|
67 | for ivar=1:nbvar |
---|
68 | DimCell=Data.VarDimName{ivar}; %dimensions associated with the variable #ivar |
---|
69 | if ischar(DimCell) |
---|
70 | DimCell={DimCell}; |
---|
71 | Data.VarDimName{ivar}={Data.VarDimName{ivar}};%transform char chain into cell |
---|
72 | end |
---|
73 | testnewcell=1; |
---|
74 | for icell_prev=1:numel(CellVarIndex)%detect whether the dimensions of ivar fit with an existing cell |
---|
75 | PrevVarIndex=CellVarIndex{icell_prev};%list of variable indices in cell # icell_prev |
---|
76 | PrevDimName=Data.VarDimName{PrevVarIndex(1)};%list of corresponding variable names |
---|
77 | if isequal(PrevDimName,DimCell) |
---|
78 | CellVarIndex{icell_prev}=[CellVarIndex{icell_prev} ivar];% add variable index #ivar to the cell #icell_prev |
---|
79 | testnewcell=0; %existing cell detected |
---|
80 | break |
---|
81 | end |
---|
82 | end |
---|
83 | if testnewcell |
---|
84 | icell=icell+1; |
---|
85 | CellVarIndex{icell}=ivar;%put the current variabl index in the new cell |
---|
86 | end |
---|
87 | |
---|
88 | %look for dimension variables |
---|
89 | if numel(DimCell)==1% if the variable has a single dimension |
---|
90 | Role=''; |
---|
91 | if isfield(Data,'VarAttribute') && length(Data.VarAttribute)>=ivar && isfield(Data.VarAttribute{ivar},'Role') |
---|
92 | Role=Data.VarAttribute{ivar}.Role; |
---|
93 | end |
---|
94 | if strcmp(DimCell{1},Data.ListVarName{ivar}) || strcmp(Role,'dimvar') |
---|
95 | ivardim=ivardim+1; |
---|
96 | VarDimIndex(ivardim)=ivar;%index of the variable |
---|
97 | VarDimName{ivardim}=DimCell{1};%name of the dimension |
---|
98 | end |
---|
99 | end |
---|
100 | end |
---|
101 | |
---|
102 | % find the spatial dimensions and vector components |
---|
103 | ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','warnflag','errorflag',... |
---|
104 | 'ancillary','image','color','discrete','scalar'}; |
---|
105 | for icell=1:length(CellVarIndex) |
---|
106 | for ilist=1:numel(ListRole) |
---|
107 | eval(['ivar_' ListRole{ilist} '=[];']) |
---|
108 | end |
---|
109 | VarIndex=CellVarIndex{icell};%set of variable indices with the same dim |
---|
110 | DimCell=Data.VarDimName{VarIndex(1)};% list of dimensions for each variable in the cell #icell |
---|
111 | if isfield(Data,'VarAttribute'); |
---|
112 | VarAttribute=Data.VarAttribute; |
---|
113 | else |
---|
114 | VarAttribute={}; |
---|
115 | end |
---|
116 | test_2D=0; |
---|
117 | for ivar=VarIndex |
---|
118 | if length(VarAttribute)>=ivar |
---|
119 | if isfield(VarAttribute{ivar},'Role') |
---|
120 | role=VarAttribute{ivar}.Role; |
---|
121 | switch role |
---|
122 | case ListRole |
---|
123 | eval(['ivar_' role '=[ivar_' role ' ivar];']) |
---|
124 | otherwise |
---|
125 | ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role') |
---|
126 | end |
---|
127 | else |
---|
128 | ivar_scalar=[ivar_scalar ivar];% variable considered as scalar in the absence of Role attribute |
---|
129 | end |
---|
130 | if isfield(VarAttribute{ivar},'Coord_2') |
---|
131 | test_2D=1; %obsolete convention |
---|
132 | end |
---|
133 | else |
---|
134 | ivar_scalar=[ivar_scalar ivar];%variables are consiered as 'scalar' by default (in the absence of attribute 'Role') |
---|
135 | end |
---|
136 | end |
---|
137 | for ilist=1:numel(ListRole) |
---|
138 | eval(['VarType{icell}.' ListRole{ilist} '=ivar_' ListRole{ilist} ';']) |
---|
139 | end |
---|
140 | if numel(ivar_coord_x)>1 || numel(ivar_coord_y)>1 || numel(ivar_coord_z)>1 |
---|
141 | errormsg='multiply defined coordinates in the same cell'; |
---|
142 | return |
---|
143 | end |
---|
144 | % if ivar_vector_x>1 |
---|
145 | % ivar_vector_x=ivar_vector_x(1); |
---|
146 | % end |
---|
147 | % if ivar_vector_y>1 |
---|
148 | % ivar_vector_y=ivar_vector_y(1); |
---|
149 | % end |
---|
150 | % if ivar_vector_z>1 |
---|
151 | % ivar_vector_z=ivar_vector_z(1); |
---|
152 | % end |
---|
153 | if numel(ivar_errorflag)>1 |
---|
154 | errormsg='multiply defined error flag in the same cell'; |
---|
155 | return |
---|
156 | end |
---|
157 | if numel(ivar_warnflag)>1 |
---|
158 | errormsg='multiply defined warning flag in the same cell'; |
---|
159 | return |
---|
160 | end |
---|
161 | %NbDim(icell)=0;% nbre of space dimensions |
---|
162 | % NbDim(icell)=numel(DimCell); |
---|
163 | NbDim(icell)=0; |
---|
164 | test_coord=0; |
---|
165 | if numel(VarIndex)>1 |
---|
166 | if ~isempty(ivar_coord_z) |
---|
167 | NbDim(icell)=3; |
---|
168 | test_coord=1; |
---|
169 | elseif ~isempty(ivar_coord_y) |
---|
170 | NbDim(icell)=2; |
---|
171 | test_coord=1; |
---|
172 | elseif ~isempty(ivar_coord_x) |
---|
173 | NbDim(icell)=1; |
---|
174 | test_coord=1; |
---|
175 | end |
---|
176 | end |
---|
177 | % look at coordinates variables |
---|
178 | coord=zeros(1,numel(DimCell));%default |
---|
179 | % if NbDim(icell)==0 && ~isempty(VarDimName)% no unstructured coordinate found |
---|
180 | if ~test_coord && ~isempty(VarDimName) |
---|
181 | for idim=1:numel(DimCell) %loop on the dimensions of the variables in cell #icell |
---|
182 | for ivardim=1:numel(VarDimName) |
---|
183 | if strcmp(VarDimName{ivardim},DimCell{idim}) |
---|
184 | coord(idim)=VarDimIndex(ivardim); |
---|
185 | break |
---|
186 | end |
---|
187 | end |
---|
188 | end |
---|
189 | NbDim(icell)=numel(find(coord)); |
---|
190 | end |
---|
191 | VarType{icell}.coord=coord; |
---|
192 | if NbDim(icell)==0 && test_2D %look at attributes Coord_1, coord_2 (obsolete convention) |
---|
193 | NbDim(icell)=2; |
---|
194 | end |
---|
195 | end |
---|