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,CoordType,VarRole,errormsg]=find_field_cells(Data)=find_field_cells(Data) |
---|
5 | % |
---|
6 | % OUTPUT: |
---|
7 | % CellVaxIndex: cell whose elements are arrays of the variable indices in the list Data.ListVarName for each field cell |
---|
8 | % CellvarIndex{i} represents a set of variables with the same dimensions |
---|
9 | % NbDim: array with the length of CellVarIndex, giving the space dimension of each field cell |
---|
10 | % CoordType: cell array with elements 'scattered','grid','tps'; type of coordinates for each field cell |
---|
11 | % VarRole: cell array of structures with fields |
---|
12 | % .coord_x, y, z: indices (in .ListVarname) of variables representing scattered (unstructured) coordinates x, y, z |
---|
13 | % .vector_x,_y,_z: indices of variables giving the vector components x, y, z |
---|
14 | % .warnflag: index of warnflag |
---|
15 | % .errorflag: index of error flag |
---|
16 | % .ancillary: indices of ancillary variables |
---|
17 | % .image : B/W image, (behaves like scalar) |
---|
18 | % .color : color image, the last index, which is not a coordinate variable, represent the 3 color components rgb |
---|
19 | % .discrete: like scalar, but set of data points without continuity, represented as dots in a usual plot, instead of continuous lines otherwise |
---|
20 | % .scalar: scalar field (default) |
---|
21 | % .coord: vector of indices of coordinate variables corresponding to matrix dimensions |
---|
22 | % |
---|
23 | % .FieldRequest= 'interp_lin', 'interp_tps' indicate whether lin interpolation or derivatives (tps) is needed to calculate the requested field |
---|
24 | % .Operation = operation to be performed to finalise the field cell after projection |
---|
25 | % .SubCheck=0 /1 indicate that the field must be substracted (second entry in uvmat) |
---|
26 | % errormsg: error message |
---|
27 | % |
---|
28 | % INPUT: |
---|
29 | % Data: structure representing fields, output of check_field_structure |
---|
30 | % .ListVarName: cell array listing the names (cahr strings) of the variables |
---|
31 | % .VarDimName: cell array of cells containing the set of dimension names for each variable of .ListVarName |
---|
32 | % .VarAttribute: cell array of structures containing the variable attributes: |
---|
33 | % .VarAttribute{ilist}.key=value, where ilist is the variable |
---|
34 | % index, key is the name of the attribute, value its value (char string or number) |
---|
35 | % |
---|
36 | % HELP: |
---|
37 | % to get the dimensions of arrays common to the field #icell |
---|
38 | % VarIndex=CellVarIndex{icell}; % list of variable indices |
---|
39 | % DimIndex=Data.VarDimIndex{VarIndex(1)} % list of dimensions for each variable in the cell #icell |
---|
40 | % |
---|
41 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
42 | % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. |
---|
43 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
44 | % This file is part of the toolbox UVMAT. |
---|
45 | % |
---|
46 | % UVMAT is free software; you can redistribute it and/or modify |
---|
47 | % it under the terms of the GNU General Public License as published by |
---|
48 | % the Free Software Foundation; either version 2 of the License, or |
---|
49 | % (at your option) any later version. |
---|
50 | % |
---|
51 | % UVMAT is distributed in the hope that it will be useful, |
---|
52 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
53 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
54 | % GNU General Public License (file UVMAT/COPYING.txt) for more details.for input in proj_field and plot_field |
---|
55 | % group the variables into 'fields' with common dimensions |
---|
56 | |
---|
57 | function [CellVarIndex,NbDim,CoordType,VarRole,errormsg]=find_field_cells(Data) |
---|
58 | CellVarIndex={}; |
---|
59 | |
---|
60 | CellVarType=[]; |
---|
61 | errormsg=[]; |
---|
62 | if ~isfield(Data,'ListVarName'), erromsg='the list of variables .ListVarName is missing';return;end |
---|
63 | if ~isfield(Data,'VarDimName'), erromsg='the list of dimensions .VarDimName is missing';return;end |
---|
64 | nbvar=numel(Data.ListVarName);%number of field variables |
---|
65 | if ~isequal(numel(Data.VarDimName),nbvar), erromsg='.ListVarName and .VarDimName have unequal length';return;end |
---|
66 | if isfield(Data,'ListDimName')&& isfield(Data,'DimValue')&&isequal(numel(Data.ListDimName),numel(Data.DimValue)) |
---|
67 | check_dim=1;% dimensions of data defined, data not needed for this function |
---|
68 | else |
---|
69 | check_dim=0; |
---|
70 | for ilist=1:numel(ListVarName) |
---|
71 | if ~isfield(Data,Data.ListVarName{ilist}) |
---|
72 | errormsg=['missing variable ' Data.ListVarName{ilist}]; |
---|
73 | return |
---|
74 | end |
---|
75 | end |
---|
76 | end |
---|
77 | icell=0; |
---|
78 | |
---|
79 | NbDim=[]; |
---|
80 | VarDimIndex=[]; |
---|
81 | VarDimName={}; |
---|
82 | |
---|
83 | %% role of variables and list of requested operations |
---|
84 | %ListRole={'coord_x','coord_y','coord_z','vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',... |
---|
85 | % 'ancillary','image','color','discrete','scalar','coord_tps'};% rmq vector_x_tps and vector_y_tps to be replaced by vector_x and vector_y |
---|
86 | Role=num2cell(blanks(nbvar));%initialize a cell array of nbvar blanks |
---|
87 | FieldRequest=regexprep(Role,' ',''); % fieldRequest set to '' by default |
---|
88 | Operation=cell(size(Role)); % fieldRequest set to {} by default |
---|
89 | CheckSub=zeros(size(Role));% =1 for fields to substract |
---|
90 | Role=regexprep(Role,' ','scalar'); % Role set to 'scalar' by default |
---|
91 | if isfield(Data,'VarAttribute') |
---|
92 | for ivar=1:numel(Data.VarAttribute) |
---|
93 | if isfield(Data.VarAttribute{ivar},'Role') |
---|
94 | Role{ivar}=Data.VarAttribute{ivar}.Role; |
---|
95 | end |
---|
96 | if isfield(Data.VarAttribute{ivar},'FieldRequest') |
---|
97 | FieldRequest{ivar}=Data.VarAttribute{ivar}.FieldRequest; |
---|
98 | end |
---|
99 | if isfield(Data.VarAttribute{ivar},'Operation') |
---|
100 | Operation{ivar}=Data.VarAttribute{ivar}.Operation; |
---|
101 | end |
---|
102 | if isfield(Data.VarAttribute{ivar},'CheckSub') |
---|
103 | CheckSub(ivar)=Data.VarAttribute{ivar}.CheckSub; |
---|
104 | end |
---|
105 | end |
---|
106 | end |
---|
107 | |
---|
108 | %% find scattered coordinates |
---|
109 | ivar_coord_x=find(strcmp('coord_x',Role); |
---|
110 | VarDimCell=cell(numel(ivar_coord_x)); |
---|
111 | check_select=zeros(1,nbvar); |
---|
112 | CellVarIndex=cell(1,numel(ivar_coord_x)); |
---|
113 | CoordType=cell(1,numel(ivar_coord_x)); |
---|
114 | VarRole=cell(1,numel(ivar_coord_x)); |
---|
115 | for icell=1:numel(ivar_coord_x) |
---|
116 | DimCell=Data.VarDimName{ivar_coord_x(icell)}; |
---|
117 | if ischar(DimCell),DimCell={DimCell};end |
---|
118 | check_cell=zeros(numel(DimCell),nbvar); |
---|
119 | for idim=1:numel(DimCell) |
---|
120 | for ivar=1:nbvar |
---|
121 | check_cell(idim,ivar)=strcmp(DimCell{idim},Data.VarDimName{ivar}); |
---|
122 | end |
---|
123 | end |
---|
124 | check_cell=sum(check_cell,1)==numel(DimCell);%logical array=1 for variables belonging to the current cell |
---|
125 | VarIndex=find(check_cell); |
---|
126 | if ~(numel(VarIndex)==1 && numel(DimCell)==1)% exclude case of isolated coord_x variable (treated later) |
---|
127 | if numel(VarIndex)==1 && numel(DimCell)>1% no variable associated to coordinate |
---|
128 | NbDim(icell)=0; |
---|
129 | else |
---|
130 | VarRole{icell}.Coord=ivar_coord_x(icell); |
---|
131 | ind_y=find(strcmp('coord_y',Role(CellVarIndex{icell}))); |
---|
132 | if numel(VarIndex)==2||isempty(ind_y)% no variable, except possibly y |
---|
133 | NbDim(icell)=1; |
---|
134 | else |
---|
135 | CoordType{icell}='scattered'; |
---|
136 | ind_z=find(strcmp('coord_z',Role(CellVarIndex{icell}))); |
---|
137 | if numel(VarIndex)==3||isempty(ind_z)% no z variable, except possibly as a fct z(x,y) |
---|
138 | VarRole{icell}.Coord=[VarIndex(ind_y) VarRole{icell}.Coord]; |
---|
139 | NbDim(icell)=2; |
---|
140 | else |
---|
141 | VarRole{icell}.Coord=[VarIndex(ind_z) VarRole{icell}.Coord]; |
---|
142 | NbDim(icell)=3; |
---|
143 | end |
---|
144 | end |
---|
145 | end |
---|
146 | CellVarIndex{icell}=VarIndex; |
---|
147 | check_select=check_select|check_cell; |
---|
148 | end |
---|
149 | end |
---|
150 | |
---|
151 | %% look for tps coordinates |
---|
152 | ivar_remain=find(~check_select); |
---|
153 | check_coord_tps= strcmp('coord_tps',Role(~check_select)); |
---|
154 | ivar_tps=ivar_remain(check_coord_tps); |
---|
155 | for icell_tps=1:numel(ivar_tps) |
---|
156 | DimCell=Data.VarDimName{ivar_tps(icell_tps)}; |
---|
157 | icell=numel(CellVarIndex)+icell_tps; |
---|
158 | VarRole{icell}.Coord=ivar_tps(icell); |
---|
159 | VarRole{icell}.subrange_tps=[]; |
---|
160 | VarRole{icell}.nbsites_tps=[]; |
---|
161 | if numel(DimCell)==3 |
---|
162 | VarDimName=Data.VarDimName(~check_select); |
---|
163 | for ivardim=1:numel(VarDimName) |
---|
164 | if strcmp(VarDimName{ivardim},DimCell{3}) |
---|
165 | VarRole{icell}.nbsites_tps= ivar_remain(ivardim); |
---|
166 | check_cell(ivar_remain(ivardim))=1;% nbre of sites for each tps subdomain |
---|
167 | elseif strcmp(VarDimName{ivardim}{1},DimCell{2}) && strcmp(VarDimName{ivardim}{3},DimCell{3}) |
---|
168 | VarRole{icell}.subrange_tps=ivar_remain(ivardim); |
---|
169 | check_cell(ivar_remain(ivardim))=1;% subrange definiton for tps |
---|
170 | elseif strcmp(VarDimName{ivardim}{1},DimCell{1}) && strcmp(VarDimName{ivardim}{2},DimCell{3}) |
---|
171 | check_cell(ivar_remain(ivardim))=1;% variable |
---|
172 | end |
---|
173 | end |
---|
174 | end |
---|
175 | if check_dim |
---|
176 | check_index= strcmp(DimCell{2},Data.ListDimName); |
---|
177 | NbDim(icell)=Data.DimValue(check_index); |
---|
178 | else |
---|
179 | NbDim(icell)=size(Data.(Data.ListVarName{VarRole{icell}.Coord}),2); |
---|
180 | end |
---|
181 | CoordType{icell}='tps'; |
---|
182 | CellVarIndex{icell}=find(check_cell); |
---|
183 | check_select=check_select|check_cell; |
---|
184 | end |
---|
185 | |
---|
186 | %% look for dimension variables and corresponding gridded data |
---|
187 | ivar_remain=find(~check_select); |
---|
188 | check_coord= cellfun(@numel,VarDimName)==1|cellfun(@ischar,VarDimName)==1;% find variables with a single dimension |
---|
189 | ListCoordIndex=ivar_remain(check_coord); |
---|
190 | ListCoordName=Data.ListVarName(ListCoordIndex); |
---|
191 | ListDimName=Data.VarDimName(ListCoordIndex); |
---|
192 | for ilist=1:numel(ListCoordIndex) |
---|
193 | if ischar(ListDimName{ilist}) |
---|
194 | ListDimName{ilist}=ListDimName(ilist);%transform string to cell |
---|
195 | end |
---|
196 | if check_dim |
---|
197 | check_index= strcmp(ListDimName{ilist}{1},Data.ListDimName); |
---|
198 | DimValue=Data.DimValue(check_index); |
---|
199 | else |
---|
200 | DimValue=numel(Data.(ListCoordName{ilist}); |
---|
201 | end |
---|
202 | if DimValue==2% case of uniform grid coordinate defined by lower and upper bounds only |
---|
203 | ListDimName{ilist}=ListCoordName{ilist};% look for dimensions with name equal to coordinate for |
---|
204 | end |
---|
205 | end |
---|
206 | NewCellVarIndex={}; |
---|
207 | NewCellDimIndex={]; |
---|
208 | NewNbDim=[]; |
---|
209 | NewCoordType={}; |
---|
210 | NewVarRole={}; |
---|
211 | VarDimName=Data.VarDimName(~check_select);%dimensions of remaining variables |
---|
212 | for ivardim=1:numel(VarDimName) % loop on the list of remaining variables |
---|
213 | DimCell=VarDimName{ivardim};% dimension names of the current variable |
---|
214 | DimIndices=[]; |
---|
215 | for idim=1:numel(DimCell) |
---|
216 | ind_dim=find(strcmp(DimCell{idim},ListDimName));%find the dim index in the list of coord dim |
---|
217 | if ~isempty(ind_dim) |
---|
218 | DimIndices=[DimIndices ind_dim]; %update the list of coord dims included in DimCell |
---|
219 | end |
---|
220 | end |
---|
221 | check_previous=0; |
---|
222 | for iprev=1:numel(NewCellVarIndex) |
---|
223 | if isequal(DimIndices,NewCellDimIndex{iprev}) |
---|
224 | NewCellVarIndex{iprev}=[NewCellVarIndex{iprev} ivar_remain(ivardim)];%append the current variable index to the found field cell |
---|
225 | check_previous=1; |
---|
226 | break |
---|
227 | end |
---|
228 | end |
---|
229 | if ~check_previous |
---|
230 | nbcell=numel(NewCellVarIndex)+1; |
---|
231 | NewCellVarIndex{nbcell}=[ivar_remain(ivardim)];% create a new field cell with the current variable index |
---|
232 | NewCellDimIndex{nbcell}=DimIndices; |
---|
233 | NewNbDim(nbcell)=numel(DimIndices); |
---|
234 | NewCoordType{nbcell}='grid'; |
---|
235 | NewVarRole{nbcell}.Coord=ListCoordIndex(DimIndices); |
---|
236 | end |
---|
237 | end |
---|
238 | CellVarIndex=[CellVarIndex NewCellVarIndex]; |
---|
239 | NbDim=[NbDim NewNbDim]; |
---|
240 | CoordType=[CoordType NewCoordType]; |
---|
241 | VarRole=[VarRole NewVarRole]; |
---|
242 | |
---|
243 | %% suppress empty cells |
---|
244 | check_empty=cellfun(@isempty,CellVarIndex); |
---|
245 | CellVarIndex(check_empty)=[]; |
---|
246 | NbDim(check_empty)=[]; |
---|
247 | CoordType(check_empty)=[]; |
---|
248 | VarRole(check_empty)=[]; |
---|
249 | |
---|
250 | %% document roles of non-coordinate variables |
---|
251 | ListRole={'vector_x','vector_y','vector_z','vector_x_tps','vector_y_tps','warnflag','errorflag',... |
---|
252 | 'ancillary','image','color','discrete','scalar'};% except coord,coord_x,_y,_z,Coord_tps already taken, into account |
---|
253 | for icell=1:numel(CellVarIndex) |
---|
254 | VarIndex=CellVarIndex{icell}; |
---|
255 | for ivar=VarIndex |
---|
256 | if isfield(VarRole{icell},Role{ivar}) |
---|
257 | VarRole{icell}.(Role{ivar})=[VarRole{icell}.(Role{ivar}) ivar]; |
---|
258 | else |
---|
259 | VarRole{icell}.(Role{ivar})= ivar; |
---|
260 | end |
---|
261 | if ~isempty(FieldRequest{ivar}) |
---|
262 | VarRole{icell}.FieldRequest=FieldRequest{ivar}; |
---|
263 | end |
---|
264 | if ~isempty(Operation{ivar}) |
---|
265 | VarRole{icell}.Operation=Operation{ivar}; |
---|
266 | end |
---|
267 | if CheckSub{ivar}==1 |
---|
268 | VarRole{icell}.CheckSub=1; |
---|
269 | end |
---|
270 | end |
---|
271 | end |
---|