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