1 | %'proj_field': projects the field on a projection object |
---|
2 | %-------------------------------------------------------------------------- |
---|
3 | % function [ProjData,errormsg]=proj_field(FieldData,ObjectData,VarMesh) |
---|
4 | % |
---|
5 | % OUTPUT: |
---|
6 | % ProjData structure containing the fields of the input field FieldData, |
---|
7 | % transmitted or projected on the object, plus the additional fields |
---|
8 | % .UMax, .UMin, .VMax, .VMin: min and max of velocity components in a domain |
---|
9 | % .UMean,VMean: mean of the velocity components in a domain |
---|
10 | % .AMin, AMax: min and max of a scalar |
---|
11 | % .AMean: mean of a scalar in a domain |
---|
12 | % .NbPix; |
---|
13 | % .DimName= names of the matrix dimensions (matlab cell) |
---|
14 | % .VarName= names of the variables [ProjData.VarName {'A','AMean','AMin','AMax'}]; |
---|
15 | % .VarDimNameIndex= dimensions of the variables, indicated by indices in the list .DimName; |
---|
16 | % |
---|
17 | %INPUT |
---|
18 | % ObjectData: structure characterizing the projection object |
---|
19 | % .Type : type of projection object |
---|
20 | % .ProjMode=mode of projection ; |
---|
21 | % .CoordUnit: 'px', 'cm' units for the coordinates defining the object |
---|
22 | % .Angle ( angles of rotation (=[0 0 0] by default) |
---|
23 | % .ProjAngle=angle of projection; |
---|
24 | % .DX,.DY,.DZ=increments along each coordinate |
---|
25 | % .Coord(nbpoints,3): set of coordinates defining the object position; |
---|
26 | |
---|
27 | %FieldData: data of the field to be projected on the projection object, with optional fields |
---|
28 | % .Txt: error message, transmitted to the projection |
---|
29 | % .FieldList: cell array of strings representing the fields to calculate |
---|
30 | % .CoordMesh: typical distance between data points (used for mouse action or display), transmitted |
---|
31 | % .CoordUnit, .TimeUnit, .dt: transmitted |
---|
32 | % standardised description of fields, nc-formated Matlab structure with fields: |
---|
33 | % .ListGlobalAttribute: cell listing the names of the global attributes |
---|
34 | % .Att_1,Att_2... : values of the global attributes |
---|
35 | % .ListVarName: cell listing the names of the variables |
---|
36 | % .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName |
---|
37 | % .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName |
---|
38 | % The variables are grouped in 'fields', made of a set of variables with common dimensions (using the function find_field_cells) |
---|
39 | % The variable attribute 'Role' is used to define the role for plotting: |
---|
40 | % Role = 'scalar': (default) represents a scalar field |
---|
41 | % = 'coord': represents a set of unstructured coordinates, whose |
---|
42 | % space dimension is given by the last array dimension (called 'NbDim'). |
---|
43 | % = 'coord_x', 'coord_y', 'coord_z': represents a separate set of |
---|
44 | % unstructured coordinate x, y or z |
---|
45 | % = 'vector': represents a vector field whose number of components |
---|
46 | % is given by the last dimension (called 'NbDim') |
---|
47 | % = 'vector_x', 'vector_y', 'vector_z' :represents the x, y or z component of a vector |
---|
48 | % = 'warnflag' : provides a warning flag about the quality of data in a 'Field', default=0, no warning |
---|
49 | % = 'errorflag': provides an error flag marking false data, |
---|
50 | % default=0, no error. Different non zero values can represent different criteria of elimination. |
---|
51 | % |
---|
52 | % Default role of variables (by name) |
---|
53 | % vector field: |
---|
54 | % .X,.Y: position of the velocity vectors, projected on the object |
---|
55 | % .U, .V, .W: velocity components, projected on the object |
---|
56 | % .C, .CName: scalar associated to the vector |
---|
57 | % .F : equivalent to 'warnflag' |
---|
58 | % .FF: equivalent to 'errorflag' |
---|
59 | % scalar field or image: |
---|
60 | % .AName: name of a scalar (to be calculated from velocity fields after projection), transmitted |
---|
61 | % .A: scalar, projected on the object |
---|
62 | % .AX, .AY: positions for the scalar |
---|
63 | % case of a structured grid: A is a dim 2 matrix and .AX=[first last] (length 2 vector) represents the first and last abscissa of the grid |
---|
64 | % case of an unstructured scalar: A is a vector, AX and AY the corresponding coordinates |
---|
65 | |
---|
66 | %======================================================================= |
---|
67 | % Copyright 2008-2015, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France |
---|
68 | % http://www.legi.grenoble-inp.fr |
---|
69 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
70 | % |
---|
71 | % This file is part of the toolbox UVMAT. |
---|
72 | % |
---|
73 | % UVMAT is free software; you can redistribute it and/or modify |
---|
74 | % it under the terms of the GNU General Public License as published |
---|
75 | % by the Free Software Foundation; either version 2 of the license, |
---|
76 | % or (at your option) any later version. |
---|
77 | % |
---|
78 | % UVMAT is distributed in the hope that it will be useful, |
---|
79 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
80 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
81 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
82 | %======================================================================= |
---|
83 | |
---|
84 | function [ProjData,errormsg]=proj_field(FieldData,ObjectData,VarMesh) |
---|
85 | errormsg='';%default |
---|
86 | ProjData=[]; |
---|
87 | |
---|
88 | %% check input projection object: type, projection mode and Coord: |
---|
89 | if ~isfield(ObjectData,'Type')||~isfield(ObjectData,'ProjMode') |
---|
90 | return |
---|
91 | end |
---|
92 | ListProjMode={'projection','interp_lin','interp_tps','inside','outside'};%list of effective projection modes |
---|
93 | if isempty(find(strcmp(ObjectData.ProjMode,ListProjMode), 1))% no projection in case |
---|
94 | return |
---|
95 | end |
---|
96 | if ~isfield(ObjectData,'Coord')||isempty(ObjectData.Coord) |
---|
97 | if strcmp(ObjectData.Type,'plane') |
---|
98 | ObjectData.Coord=[0 0];%default |
---|
99 | else |
---|
100 | return |
---|
101 | end |
---|
102 | end |
---|
103 | |
---|
104 | %% apply projection depending on the object type |
---|
105 | switch ObjectData.Type |
---|
106 | case 'points' |
---|
107 | [ProjData,errormsg]=proj_points(FieldData,ObjectData); |
---|
108 | case {'line','polyline'} |
---|
109 | [ProjData,errormsg] = proj_line(FieldData,ObjectData); |
---|
110 | case {'polygon','rectangle','ellipse'} |
---|
111 | if isequal(ObjectData.ProjMode,'inside')||isequal(ObjectData.ProjMode,'outside') |
---|
112 | if ~exist('VarMesh','var') |
---|
113 | VarMesh=[]; |
---|
114 | end |
---|
115 | [ProjData,errormsg] = proj_patch(FieldData,ObjectData,VarMesh); |
---|
116 | else |
---|
117 | [ProjData,errormsg] = proj_line(FieldData,ObjectData); |
---|
118 | end |
---|
119 | case 'plane' |
---|
120 | [ProjData,errormsg] = proj_plane(FieldData,ObjectData); |
---|
121 | case 'volume' |
---|
122 | [ProjData,errormsg] = proj_volume(FieldData,ObjectData); |
---|
123 | end |
---|
124 | |
---|
125 | %----------------------------------------------------------------- |
---|
126 | %project on a set of points |
---|
127 | function [ProjData,errormsg]=proj_points(FieldData,ObjectData)%% |
---|
128 | %------------------------------------------------------------------- |
---|
129 | |
---|
130 | siz=size(ObjectData.Coord); |
---|
131 | width=0; |
---|
132 | if isfield(ObjectData,'Range') |
---|
133 | width=ObjectData.Range(1,2); |
---|
134 | end |
---|
135 | if isfield(ObjectData,'RangeX')&&~isempty(ObjectData.RangeX) |
---|
136 | width=max(ObjectData.RangeX); |
---|
137 | end |
---|
138 | if isfield(ObjectData,'RangeY')&&~isempty(ObjectData.RangeY) |
---|
139 | width=max(width,max(ObjectData.RangeY)); |
---|
140 | end |
---|
141 | if isfield(ObjectData,'RangeZ')&&~isempty(ObjectData.RangeZ) |
---|
142 | width=max(width,max(ObjectData.RangeZ)); |
---|
143 | end |
---|
144 | if isequal(ObjectData.ProjMode,'projection') |
---|
145 | if width==0 |
---|
146 | errormsg='projection range around points needed'; |
---|
147 | return |
---|
148 | end |
---|
149 | elseif ~isequal(ObjectData.ProjMode,'interp_lin') |
---|
150 | errormsg=(['ProjMode option ' ObjectData.ProjMode ' not available in proj_field']); |
---|
151 | return |
---|
152 | end |
---|
153 | [ProjData,errormsg]=proj_heading(FieldData,ObjectData); |
---|
154 | if ~isempty(errormsg) |
---|
155 | return |
---|
156 | end |
---|
157 | ProjData.NbDim=0; |
---|
158 | [CellInfo,NbDimArray,errormsg]=find_field_cells(FieldData); |
---|
159 | if ~isempty(errormsg) |
---|
160 | errormsg=['error in proj_field/proj_points:' errormsg]; |
---|
161 | return |
---|
162 | end |
---|
163 | %LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
164 | for icell=1:length(CellInfo) |
---|
165 | if NbDimArray(icell)<=1 |
---|
166 | continue %projection only for multidimensional fields |
---|
167 | end |
---|
168 | VarIndex=CellInfo{icell}.VarIndex;% indices of the selected variables in the list FieldData.ListVarName |
---|
169 | ivar_X=CellInfo{icell}.CoordIndex(end); |
---|
170 | ivar_Y=CellInfo{icell}.CoordIndex(end-1); |
---|
171 | ivar_Z=[]; |
---|
172 | if NbDimArray(icell)==3 |
---|
173 | ivar_Z=CellInfo{icell}.CoordIndex(1); |
---|
174 | end |
---|
175 | ivar_FF=[]; |
---|
176 | if isfield(CellInfo{icell},'VarIndex_errorflag') |
---|
177 | ivar_FF=CellInfo{icell}.VarIndex_errorflag; |
---|
178 | if numel(ivar_FF)>1 |
---|
179 | errormsg='multiple error flag input'; |
---|
180 | return |
---|
181 | end |
---|
182 | end |
---|
183 | % select types of variables to be projected |
---|
184 | ListProj={'VarIndex_scalar','VarIndex_image','VarIndex_color','VarIndex_vector_x','VarIndex_vector_y'}; |
---|
185 | check_proj=false(size(FieldData.ListVarName)); |
---|
186 | for ilist=1:numel(ListProj) |
---|
187 | if isfield(CellInfo{icell},ListProj{ilist}) |
---|
188 | check_proj(CellInfo{icell}.(ListProj{ilist}))=1; |
---|
189 | end |
---|
190 | end |
---|
191 | VarIndex=find(check_proj); |
---|
192 | ProjData.ListVarName={'Y','X','NbVal'}; |
---|
193 | ProjData.VarDimName={'nb_points','nb_points','nb_points'}; |
---|
194 | ProjData.VarAttribute{1}.Role='ancillary'; |
---|
195 | ProjData.VarAttribute{2}.Role='ancillary'; |
---|
196 | ProjData.VarAttribute{3}.Role='ancillary'; |
---|
197 | for ivar=VarIndex |
---|
198 | VarName=FieldData.ListVarName{ivar}; |
---|
199 | ProjData.ListVarName=[ProjData.ListVarName {VarName}];% add the current variable to the list of projected variables |
---|
200 | ProjData.VarDimName=[ProjData.VarDimName {'nb_points'}]; % projected VarName has a single dimension called 'nb_points' (set of projection points) |
---|
201 | |
---|
202 | end |
---|
203 | if strcmp( CellInfo{icell}.CoordType,'scattered') |
---|
204 | coord_x=FieldData.(FieldData.ListVarName{ivar_X}); |
---|
205 | coord_y=FieldData.(FieldData.ListVarName{ivar_Y}); |
---|
206 | test3D=0;% TEST 3D CASE : NOT COMPLETED , 3D CASE : NOT COMPLETED |
---|
207 | if length(ivar_Z)==1 |
---|
208 | coord_z=FieldData.(FieldData.ListVarName{ivar_Z}); |
---|
209 | test3D=1; |
---|
210 | end |
---|
211 | |
---|
212 | for ipoint=1:siz(1) |
---|
213 | Xpoint=ObjectData.Coord(ipoint,:); |
---|
214 | distX=coord_x-Xpoint(1); |
---|
215 | distY=coord_y-Xpoint(2); |
---|
216 | dist=distX.*distX+distY.*distY; |
---|
217 | indsel=find(dist<width*width); |
---|
218 | ProjData.X(ipoint,1)=Xpoint(1); |
---|
219 | ProjData.Y(ipoint,1)=Xpoint(2); |
---|
220 | if isequal(length(ivar_FF),1) |
---|
221 | FFName=FieldData.ListVarName{ivar_FF}; |
---|
222 | FF=FieldData.(FFName)(indsel); |
---|
223 | indsel=indsel(~FF); |
---|
224 | end |
---|
225 | ProjData.NbVal(ipoint,1)=length(indsel); |
---|
226 | for ivar=VarIndex |
---|
227 | VarName=FieldData.ListVarName{ivar}; |
---|
228 | if isempty(indsel) |
---|
229 | ProjData.(VarName)(ipoint,1)=NaN; |
---|
230 | else |
---|
231 | Var=FieldData.(VarName)(indsel); |
---|
232 | ProjData.(VarName)(ipoint,1)=mean(Var); |
---|
233 | if isequal(ObjectData.ProjMode,'interp_lin') |
---|
234 | ProjData.(VarName)(ipoint,1)=griddata_uvmat(coord_x(indsel),coord_y(indsel),Var,Xpoint(1),Xpoint(2)); |
---|
235 | end |
---|
236 | end |
---|
237 | end |
---|
238 | end |
---|
239 | else %case of structured coordinates |
---|
240 | if strcmp( CellInfo{icell}.CoordType,'grid') |
---|
241 | AYName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(end-1)}; |
---|
242 | AXName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)}; |
---|
243 | eval(['AX=FieldData.' AXName ';']);% set of x positions |
---|
244 | eval(['AY=FieldData.' AYName ';']);% set of y positions |
---|
245 | AName=FieldData.ListVarName{VarIndex(1)};% a single variable assumed in the current cell |
---|
246 | eval(['A=FieldData.' AName ';']);% scalar |
---|
247 | npxy=size(A); |
---|
248 | %update VarDimName in case of components (non coordinate dimensions e;g. color components) |
---|
249 | if numel(npxy)>NbDimArray(icell) |
---|
250 | ProjData.VarDimName{end}={'nb_points','component'}; |
---|
251 | end |
---|
252 | for idim=1:NbDimArray(icell) %loop on space dimensions |
---|
253 | test_interp(idim)=0;%test for coordiate interpolation (non regular grid), =0 by default |
---|
254 | test_coord(idim)=0;%test for defined coordinates, =0 by default |
---|
255 | ivar=CellInfo{icell}.CoordIndex(idim); |
---|
256 | Coord{idim}=FieldData.(FieldData.ListVarName{ivar}); % position for the first index |
---|
257 | if numel(Coord{idim})==2 |
---|
258 | DCoord_min(idim)= (Coord{idim}(2)-Coord{idim}(1))/(npxy(idim)-1); |
---|
259 | test_direct(idim)=DCoord_min(idim)>0;% =1 for increasing values, 0 otherwise |
---|
260 | else |
---|
261 | DCoord=diff(Coord{idim}); |
---|
262 | DCoord_min(idim)=min(DCoord); |
---|
263 | DCoord_max=max(DCoord); |
---|
264 | test_direct(idim)=DCoord_max>0;% =1 for increasing values, 0 otherwise |
---|
265 | test_direct_min=DCoord_min(idim)>0;% =1 for increasing values, 0 otherwise |
---|
266 | if ~isequal(test_direct(idim),test_direct_min) |
---|
267 | errormsg=['non monotonic dimension variable # ' num2str(idim) ' in proj_field.m']; |
---|
268 | return |
---|
269 | end |
---|
270 | test_interp(idim)=(DCoord_max-DCoord_min(idim))> 0.0001*abs(DCoord_max);% test grid regularity |
---|
271 | test_coord(idim)=1; |
---|
272 | end |
---|
273 | end |
---|
274 | DX=DCoord_min(2); |
---|
275 | DY=DCoord_min(1); |
---|
276 | for ipoint=1:siz(1) |
---|
277 | xwidth=width/(abs(DX)); |
---|
278 | ywidth=width/(abs(DY)); |
---|
279 | i_min=round((ObjectData.Coord(ipoint,1)-Coord{2}(1))/DX+0.5-xwidth); %minimum index of the selected region |
---|
280 | i_min=max(1,i_min);%restrict to field limit |
---|
281 | i_plus=round((ObjectData.Coord(ipoint,1)-Coord{2}(1))/DX+0.5+xwidth); |
---|
282 | i_plus=min(npxy(2),i_plus); %restrict to field limit |
---|
283 | j_min=round((ObjectData.Coord(ipoint,2)-Coord{1}(1))/DY-ywidth+0.5); |
---|
284 | j_min=max(1,j_min); |
---|
285 | j_plus=round((ObjectData.Coord(ipoint,2)-Coord{1}(1))/DY+ywidth+0.5); |
---|
286 | j_plus=min(npxy(1),j_plus); |
---|
287 | ProjData.X(ipoint,1)=ObjectData.Coord(ipoint,1); |
---|
288 | ProjData.Y(ipoint,1)=ObjectData.Coord(ipoint,2); |
---|
289 | i_int=(i_min:i_plus); |
---|
290 | j_int=(j_min:j_plus); |
---|
291 | ProjData.NbVal(ipoint,1)=length(j_int)*length(i_int); |
---|
292 | if isempty(i_int) || isempty(j_int) |
---|
293 | for ivar=VarIndex |
---|
294 | eval(['ProjData.' FieldData.ListVarName{ivar} '(ipoint,:)=NaN;']); |
---|
295 | end |
---|
296 | errormsg=['no data points in the selected projection range ' num2str(width) ]; |
---|
297 | else |
---|
298 | %TODO: introduce circle in the selected subregion |
---|
299 | %[I,J]=meshgrid([1:j_int],[1:i_int]); |
---|
300 | for ivar=VarIndex |
---|
301 | Avalue=FieldData.(FieldData.ListVarName{ivar})(j_int,i_int,:); |
---|
302 | ProjData.(FieldData.ListVarName{ivar})(ipoint,:)=mean(mean(Avalue)); |
---|
303 | end |
---|
304 | end |
---|
305 | end |
---|
306 | end |
---|
307 | end |
---|
308 | end |
---|
309 | |
---|
310 | %----------------------------------------------------------------- |
---|
311 | %project in a patch |
---|
312 | function [ProjData,errormsg]=proj_patch(FieldData,ObjectData,VarMesh)%% |
---|
313 | %------------------------------------------------------------------- |
---|
314 | [ProjData,errormsg]=proj_heading(FieldData,ObjectData); |
---|
315 | if ~isempty(errormsg) |
---|
316 | return |
---|
317 | end |
---|
318 | %objectfield=fieldnames(ObjectData); |
---|
319 | widthx=0; |
---|
320 | widthy=0; |
---|
321 | if isfield(ObjectData,'RangeX') && ~isempty(ObjectData.RangeX) |
---|
322 | widthx=max(ObjectData.RangeX); |
---|
323 | end |
---|
324 | if isfield(ObjectData,'RangeY') && ~isempty(ObjectData.RangeY) |
---|
325 | widthy=max(ObjectData.RangeY); |
---|
326 | end |
---|
327 | |
---|
328 | %A REVOIR, GENERALISER: UTILISER proj_line |
---|
329 | ProjData.NbDim=1; |
---|
330 | ProjData.ListVarName={}; |
---|
331 | ProjData.VarDimName={}; |
---|
332 | ProjData.VarAttribute={}; |
---|
333 | |
---|
334 | CoordMesh=zeros(1,numel(FieldData.ListVarName)); |
---|
335 | if isfield (FieldData,'VarAttribute') |
---|
336 | for iattr=1:length(FieldData.VarAttribute)%initialization of variable attribute values |
---|
337 | if isfield(FieldData.VarAttribute{iattr},'Unit') |
---|
338 | unit{iattr}=FieldData.VarAttribute{iattr}.Unit; |
---|
339 | end |
---|
340 | if isfield(FieldData.VarAttribute{iattr},'CoordMesh') |
---|
341 | CoordMesh(iattr)=FieldData.VarAttribute{iattr}.CoordMesh; |
---|
342 | end |
---|
343 | end |
---|
344 | end |
---|
345 | |
---|
346 | %group the variables (fields of 'FieldData') in cells of variables with the same dimensions |
---|
347 | [CellInfo,NbDim,errormsg]=find_field_cells(FieldData); |
---|
348 | if ~isempty(errormsg) |
---|
349 | errormsg=['error in proj_field/proj_patch:' errormsg]; |
---|
350 | return |
---|
351 | end |
---|
352 | |
---|
353 | %LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
354 | for icell=1:length(CellInfo) |
---|
355 | CoordType=CellInfo{icell}.CoordType; |
---|
356 | test_Amat=0; |
---|
357 | if NbDim(icell)~=2% proj_patch acts only on fields of space dimension 2 |
---|
358 | continue |
---|
359 | end |
---|
360 | ivar_FF=[]; |
---|
361 | testfalse=isfield(CellInfo{icell},'VarIndex_errorflag'); |
---|
362 | if testfalse |
---|
363 | ivar_FF=CellInfo{icell}.VarIndex_errorflag; |
---|
364 | FFName=FieldData.ListVarName{ivar_FF}; |
---|
365 | errorflag=FieldData.(FFName); |
---|
366 | end |
---|
367 | % select types of variables to be projected |
---|
368 | ListProj={'VarIndex_scalar','VarIndex_image','VarIndex_color','VarIndex_vector_x','VarIndex_vector_y'}; |
---|
369 | check_proj=false(size(FieldData.ListVarName)); |
---|
370 | for ilist=1:numel(ListProj) |
---|
371 | if isfield(CellInfo{icell},ListProj{ilist}) |
---|
372 | check_proj(CellInfo{icell}.(ListProj{ilist}))=1; |
---|
373 | end |
---|
374 | end |
---|
375 | VarIndex=find(check_proj); |
---|
376 | |
---|
377 | ivar_X=CellInfo{icell}.CoordIndex(end); |
---|
378 | ivar_Y=CellInfo{icell}.CoordIndex(end-1); |
---|
379 | ivar_Z=[]; |
---|
380 | if NbDim(icell)==3 |
---|
381 | ivar_Z=CellInfo{icell}.CoordIndex(1); |
---|
382 | end |
---|
383 | switch CellInfo{icell}.CoordType |
---|
384 | case 'scattered' %case of unstructured coordinates |
---|
385 | for ivar=[VarIndex ivar_X ivar_Y ivar_FF] |
---|
386 | VarName=FieldData.ListVarName{ivar}; |
---|
387 | FieldData.(VarName)=reshape(FieldData.(VarName),[],1); |
---|
388 | end |
---|
389 | XName=FieldData.ListVarName{ivar_X}; |
---|
390 | YName=FieldData.ListVarName{ivar_Y}; |
---|
391 | coord_x=FieldData.(FieldData.ListVarName{ivar_X}); |
---|
392 | coord_y=FieldData.(FieldData.ListVarName{ivar_Y}); |
---|
393 | % image or 2D matrix |
---|
394 | case 'grid' %case of structured coordinates |
---|
395 | test_Amat=1;% test for image or 2D matrix |
---|
396 | AYName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(end-1)}; |
---|
397 | AXName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)}; |
---|
398 | AX=FieldData.(AXName);% x coordinate |
---|
399 | AY=FieldData.(AYName);% y coordinate |
---|
400 | VarName=FieldData.ListVarName{VarIndex(1)}; |
---|
401 | DimValue=size(FieldData.(VarName)); |
---|
402 | if length(AX)==2 |
---|
403 | AX=linspace(AX(1),AX(end),DimValue(2)); |
---|
404 | end |
---|
405 | if length(AY)==2 |
---|
406 | AY=linspace(AY(1),AY(end),DimValue(1)); |
---|
407 | end |
---|
408 | if length(DimValue)==3 |
---|
409 | testcolor=1; |
---|
410 | npxy(3)=3; |
---|
411 | else |
---|
412 | testcolor=0; |
---|
413 | npxy(3)=1; |
---|
414 | end |
---|
415 | [Xi,Yi]=meshgrid(AX,AY); |
---|
416 | npxy(1)=length(AY); |
---|
417 | npxy(2)=length(AX); |
---|
418 | Xi=reshape(Xi,npxy(1)*npxy(2),1); |
---|
419 | Yi=reshape(Yi,npxy(1)*npxy(2),1); |
---|
420 | for ivar=1:length(VarIndex) |
---|
421 | VarName=FieldData.ListVarName{VarIndex(ivar)}; |
---|
422 | FieldData.(VarName)=reshape(FieldData.(VarName),npxy(1)*npxy(2),npxy(3)); % keep only non false vectors |
---|
423 | end |
---|
424 | end |
---|
425 | %select the indices in the range of action |
---|
426 | testin=[];%default |
---|
427 | if isequal(ObjectData.Type,'rectangle') |
---|
428 | if strcmp(CellInfo{icell}.CoordType,'scattered') |
---|
429 | distX=abs(coord_x-ObjectData.Coord(1,1)); |
---|
430 | distY=abs(coord_y-ObjectData.Coord(1,2)); |
---|
431 | testin=distX<widthx & distY<widthy; |
---|
432 | elseif test_Amat |
---|
433 | distX=abs(Xi-ObjectData.Coord(1,1)); |
---|
434 | distY=abs(Yi-ObjectData.Coord(1,2)); |
---|
435 | testin=distX<widthx & distY<widthy; |
---|
436 | end |
---|
437 | elseif isequal(ObjectData.Type,'polygon') |
---|
438 | if strcmp(CoordType,'scattered') |
---|
439 | testin=inpolygon(coord_x,coord_y,ObjectData.Coord(:,1),ObjectData.Coord(:,2)); |
---|
440 | elseif strcmp(CoordType,'grid') |
---|
441 | testin=inpolygon(Xi,Yi,ObjectData.Coord(:,1),ObjectData.Coord(:,2)); |
---|
442 | else%calculate the scalar |
---|
443 | testin=[]; %A REVOIR |
---|
444 | end |
---|
445 | elseif isequal(ObjectData.Type,'ellipse') |
---|
446 | X2Max=widthx*widthx; |
---|
447 | Y2Max=(widthy)*(widthy); |
---|
448 | if strcmp(CoordType,'scattered') |
---|
449 | distX=(coord_x-ObjectData.Coord(1,1)); |
---|
450 | distY=(coord_y-ObjectData.Coord(1,2)); |
---|
451 | testin=(distX.*distX/X2Max+distY.*distY/Y2Max)<1; |
---|
452 | elseif strcmp(CoordType,'grid') %case of usual 2x2 matrix |
---|
453 | distX=(Xi-ObjectData.Coord(1,1)); |
---|
454 | distY=(Yi-ObjectData.Coord(1,2)); |
---|
455 | testin=(distX.*distX/X2Max+distY.*distY/Y2Max)<1; |
---|
456 | end |
---|
457 | end |
---|
458 | %selected indices |
---|
459 | if isequal(ObjectData.ProjMode,'outside') |
---|
460 | testin=~testin; |
---|
461 | end |
---|
462 | if testfalse |
---|
463 | testin=testin & (errorflag==0); % keep only non false vectors |
---|
464 | end |
---|
465 | indsel=find(testin); |
---|
466 | nbvar=0; |
---|
467 | VarSize=zeros(size(VarIndex)); |
---|
468 | for ivar=VarIndex |
---|
469 | VarName=FieldData.ListVarName{ivar}; |
---|
470 | ProjData.([VarName 'Mean'])=mean(double(FieldData.(VarName)(indsel,:))); % take the mean in the selected region, for each co |
---|
471 | ProjData.([VarName 'Min'])=min(double(FieldData.(VarName)(indsel,:))); % take the min in the selected region , for each color component |
---|
472 | ProjData.([VarName 'Max'])=max(double(FieldData.(VarName)(indsel,:))); % take the max in the selected region , for each color co |
---|
473 | nbvar=nbvar+1; |
---|
474 | VarSize(nbvar)=mean((ProjData.([VarName 'Max'])-ProjData.([VarName 'Min']))/100); |
---|
475 | end |
---|
476 | if isempty(VarMesh)% || isnan(VarMesh) % mesh not specified as input, estimate from the bounds |
---|
477 | VarMesh=mean(VarSize); |
---|
478 | ord=10^(floor(log10(VarMesh)));%order of magnitude |
---|
479 | if VarMesh/ord >=5 |
---|
480 | VarMesh=5*ord; |
---|
481 | elseif VarMesh/ord >=2 |
---|
482 | VarMesh=2*ord; |
---|
483 | else |
---|
484 | VarMesh=ord; |
---|
485 | end |
---|
486 | end |
---|
487 | for ivar=VarIndex |
---|
488 | VarName=FieldData.ListVarName{ivar}; |
---|
489 | LowBound=VarMesh*ceil(ProjData.([VarName 'Min'])/VarMesh); |
---|
490 | UpperBound=VarMesh*floor(ProjData.([VarName 'Max'])/VarMesh); |
---|
491 | if numel(indsel)<=1 |
---|
492 | errormsg='only one data point or less for histogram'; |
---|
493 | return |
---|
494 | elseif isequal(LowBound,UpperBound) |
---|
495 | errormsg='attempt histogram of uniform field: low bound = high bound'; |
---|
496 | return |
---|
497 | end |
---|
498 | ProjData.(VarName)=LowBound:VarMesh:UpperBound; % list of bin values |
---|
499 | ProjData.([VarName 'Histo'])=hist(double(FieldData.(VarName)(indsel,:)),ProjData.(VarName)); % histogram at predefined bin positions |
---|
500 | ProjData.ListVarName=[ProjData.ListVarName {VarName} {[VarName 'Histo']} {[VarName 'Mean']} {[VarName 'Min']} {[VarName 'Max']}]; |
---|
501 | if test_Amat && testcolor |
---|
502 | ProjData.VarDimName=[ProjData.VarDimName {VarName} {{VarName,'rgb'}} {'rgb'} {'rgb'} {'rgb'}];%{{'nb_point','rgb'}}; |
---|
503 | else |
---|
504 | ProjData.VarDimName=[ProjData.VarDimName {VarName} {VarName} {'one'} {'one'} {'one'}]; |
---|
505 | end |
---|
506 | VarAttribute_var=[]; |
---|
507 | if isfield(FieldData,'VarAttribute')&& numel(FieldData.VarAttribute)>=ivar |
---|
508 | VarAttribute_var=FieldData.VarAttribute{ivar}; |
---|
509 | end |
---|
510 | % VarAttribute_var.Role='coord_x';% the variable is now used as an absissa |
---|
511 | VarAttribute_histo.Role='histo'; |
---|
512 | ProjData.VarAttribute=[ProjData.VarAttribute {VarAttribute_var} {VarAttribute_histo} {[]} {[]} {[]}]; |
---|
513 | end |
---|
514 | end |
---|
515 | |
---|
516 | %----------------------------------------------------------------- |
---|
517 | %project on a line |
---|
518 | % AJOUTER flux,circul,error |
---|
519 | % OUTPUT: |
---|
520 | % ProjData: projected field |
---|
521 | % |
---|
522 | function [ProjData,errormsg] = proj_line(FieldData, ObjectData) |
---|
523 | %----------------------------------------------------------------- |
---|
524 | |
---|
525 | %% prepare heading for the projected field |
---|
526 | [ProjData,errormsg]=proj_heading(FieldData,ObjectData);%transfer global attributes |
---|
527 | if ~isempty(errormsg) |
---|
528 | return |
---|
529 | end |
---|
530 | ProjData.NbDim=1; |
---|
531 | %initialisation of the input parameters and defaultoutput |
---|
532 | ProjMode=ObjectData.ProjMode; %rmq: ProjMode always defined from input={'projection','interp_lin','interp_tps'} |
---|
533 | % ProjAngle=90; %90 degrees projection by default |
---|
534 | width=0; |
---|
535 | if isfield(ObjectData,'RangeY') |
---|
536 | width=max(ObjectData.RangeY);%Rangey needed bfor mode 'projection' |
---|
537 | end |
---|
538 | % default output |
---|
539 | errormsg='';%default |
---|
540 | Xline=[]; |
---|
541 | flux=0; |
---|
542 | circul=0; |
---|
543 | liny=ObjectData.Coord(:,2); |
---|
544 | NbPoints=size(ObjectData.Coord,1); |
---|
545 | testfalse=0; |
---|
546 | ListIndex={}; |
---|
547 | |
---|
548 | %% group the variables (fields of 'FieldData') in cells of variables with the same dimensions |
---|
549 | [CellInfo,NbDim,errormsg]=find_field_cells(FieldData); |
---|
550 | if ~isempty(errormsg) |
---|
551 | errormsg=['error in proj_field/proj_line:' errormsg]; |
---|
552 | return |
---|
553 | end |
---|
554 | CellInfo=CellInfo(NbDim==2); %keep only the 2D cells |
---|
555 | %%%%%% TODO: treat 1D fields: project as identity so that P o P=P for projection operation |
---|
556 | cell_select=true(size(CellInfo)); |
---|
557 | |
---|
558 | for icell=1:length(CellInfo) |
---|
559 | if isfield(CellInfo{icell},'ProjModeRequest') |
---|
560 | if ~strcmp(CellInfo{icell}.ProjModeRequest, ProjMode) |
---|
561 | cell_select(icell)=0; |
---|
562 | end |
---|
563 | if strcmp(ProjMode,'interp_tps')&& ~strcmp(CellInfo{icell}.CoordType,'tps') |
---|
564 | cell_select(icell)=0; |
---|
565 | end |
---|
566 | end |
---|
567 | end |
---|
568 | if isempty(find(cell_select)) |
---|
569 | errormsg=[' invalid projection mode ''' ProjMode ''': use ''interp_tps'' to interpolate spatial derivatives']; |
---|
570 | return |
---|
571 | end |
---|
572 | CellInfo=CellInfo(cell_select); |
---|
573 | |
---|
574 | %% projection line: object types selected from proj_field='line','polyline','polygon','rectangle','ellipse': |
---|
575 | LineCoord=ObjectData.Coord; |
---|
576 | switch ObjectData.Type |
---|
577 | case 'ellipse' |
---|
578 | LineLength=2*pi*ObjectData.RangeX*ObjectData.RangeY; |
---|
579 | NbSegment=0; |
---|
580 | case 'rectangle' |
---|
581 | LineCoord([1 4],1)=ObjectData.Coord(1,1)-ObjectData.RangeX; |
---|
582 | LineCoord([1 2],2)=ObjectData.Coord(1,2)-ObjectData.RangeY; |
---|
583 | LineCoord([2 3],1)=ObjectData.Coord(1,1)+ObjectData.RangeX; |
---|
584 | LineCoord([4 1],2)=ObjectData.Coord(1,2)+ObjectData.RangeY; |
---|
585 | case 'polygon' |
---|
586 | LineCoord(NbPoints+1)=LineCoord(1); |
---|
587 | end |
---|
588 | if ~strcmp(ObjectData.Type,'ellipse') |
---|
589 | if ~strcmp(ObjectData.Type,'rectangle') && NbPoints<2 |
---|
590 | return% line needs at least 2 points to be defined |
---|
591 | end |
---|
592 | dlinx=diff(LineCoord(:,1)); |
---|
593 | dliny=diff(LineCoord(:,2)); |
---|
594 | [theta,dlength]=cart2pol(dlinx,dliny);%angle and length of each segment |
---|
595 | LineLength=sum(dlength); |
---|
596 | NbSegment=numel(LineLength); |
---|
597 | end |
---|
598 | CheckClosedLine=~isempty(find(strcmp(ObjectData.Type,{'rectangle','ellipse','polygon'}))); |
---|
599 | |
---|
600 | % x = a \ \cosh \mu \ \cos \nu |
---|
601 | % |
---|
602 | % y = a \ \sinh \mu \ \sin \nu |
---|
603 | |
---|
604 | %% angles of the polyline and boundaries of action for mode 'projection' |
---|
605 | |
---|
606 | % determine a rectangles at +-width from the line (only used for the ProjMode='projection or 'interp_tps') |
---|
607 | xsup=zeros(1,NbPoints); xinf=zeros(1,NbPoints); ysup=zeros(1,NbPoints); yinf=zeros(1,NbPoints); |
---|
608 | if isequal(ProjMode,'projection') |
---|
609 | if strcmp(ObjectData.Type,'line') |
---|
610 | xsup=ObjectData.Coord(:,1)-width*sin(theta); |
---|
611 | xinf=ObjectData.Coord(:,1)+width*sin(theta); |
---|
612 | ysup=ObjectData.Coord(:,2)+width*cos(theta); |
---|
613 | yinf=ObjectData.Coord(:,2)-width*cos(theta); |
---|
614 | else |
---|
615 | errormsg='mode projection only available for simple line, use interpolation otherwise'; |
---|
616 | return |
---|
617 | end |
---|
618 | else % need to define the set of interpolation points |
---|
619 | if isfield(ObjectData,'DX') && ~isempty(ObjectData.DX) |
---|
620 | DX=abs(ObjectData.DX);%mesh of interpolation points along the line |
---|
621 | if CheckClosedLine |
---|
622 | NbPoint=ceil(LineLength/DX); |
---|
623 | DX=LineLength/NbPoint;%adjust DX to get an integer nbre of intervals in a closed line |
---|
624 | DX_edge=DX/2; |
---|
625 | else |
---|
626 | DX_edge=(LineLength-DX*floor(LineLength/DX))/2;%margin from the first point and first interpolation point, the same for the end point |
---|
627 | end |
---|
628 | XI=[]; |
---|
629 | YI=[]; |
---|
630 | ThetaI=[]; |
---|
631 | dlengthI=[]; |
---|
632 | if strcmp(ObjectData.Type,'ellipse') |
---|
633 | phi=(DX_edge:DX:LineLength)*2*pi/LineLength; |
---|
634 | XI=ObjectData.RangeX*cos(phi); |
---|
635 | YI=ObjectData.RangeY*sin(phi); |
---|
636 | dphi=2*pi*DX/LineLength; |
---|
637 | [ThetaI,dlengthI]=cart2pol(-ObjectData.RangeX*sin(phi)*dphi,ObjectData.RangeY*cos(phi)*dphi); |
---|
638 | else |
---|
639 | for isegment=1:NbSegment |
---|
640 | costheta=cos(theta(isegment)); |
---|
641 | sintheta=sin(theta(isegment)); |
---|
642 | % XIsegment=LineCoord(isegment,1)+DX_edge*costheta:DX*costheta:LineCoord(isegment+1,1)); |
---|
643 | % YIsegment=(LineCoord(isegment,2)+DX_edge*sintheta:DX*sintheta:LineCoord(isegment+1,2)); |
---|
644 | NbInterval=floor((dlength(isegment)-DX_edge)/DX); |
---|
645 | LastX=DX_edge+DX*NbInterval; |
---|
646 | NbPoint=NbInterval+1; |
---|
647 | XIsegment=linspace(LineCoord(isegment,1)+DX_edge*costheta,LineCoord(isegment,1)+LastX*costheta,NbPoint); |
---|
648 | YIsegment=linspace(LineCoord(isegment,2)+DX_edge*sintheta,LineCoord(isegment,2)+LastX*sintheta,NbPoint); |
---|
649 | XI=[XI XIsegment]; |
---|
650 | YI=[YI YIsegment]; |
---|
651 | ThetaI=[ThetaI theta(isegment)*ones(1,numel(XIsegment))]; |
---|
652 | dlengthI=[dlengthI DX*ones(1,numel(XIsegment))]; |
---|
653 | DX_edge=DX-(dlength(isegment)-LastX);%edge for the next segment set to keep DX=DX_end+DX_edge between two segments |
---|
654 | end |
---|
655 | end |
---|
656 | Xproj=cumsum(dlengthI); |
---|
657 | else |
---|
658 | errormsg='abscissa mesh along line DX needed for interpolation'; |
---|
659 | return |
---|
660 | end |
---|
661 | end |
---|
662 | |
---|
663 | %% loop on variable cells with the same space dimension 2 |
---|
664 | ProjData.ListVarName={}; |
---|
665 | ProjData.VarDimName={}; |
---|
666 | check_abscissa=0; |
---|
667 | for icell=1:length(CellInfo) |
---|
668 | % list of variable types to be projected |
---|
669 | ListProj={'VarIndex_scalar','VarIndex_image','VarIndex_color','VarIndex_vector_x','VarIndex_vector_y'}; |
---|
670 | check_proj=false(size(FieldData.ListVarName)); |
---|
671 | for ilist=1:numel(ListProj) |
---|
672 | if isfield(CellInfo{icell},ListProj{ilist}) |
---|
673 | check_proj(CellInfo{icell}.(ListProj{ilist}))=1; |
---|
674 | end |
---|
675 | end |
---|
676 | VarIndex=find(check_proj);% indices of the variables to be projected |
---|
677 | |
---|
678 | %% identify vector components |
---|
679 | %testU=isfield(CellInfo{icell},'VarIndex_vector_x') &&isfield(CellInfo{icell},'VarIndex_vector_y') ;% test for vectors |
---|
680 | % if testU |
---|
681 | % UName=FieldData.ListVarName{CellInfo{icell}.VarIndex_vector_x}; |
---|
682 | % VName=FieldData.ListVarName{CellInfo{icell}.VarIndex_vector_y}; |
---|
683 | % vector_x=FieldData.(UName); |
---|
684 | % vector_y=FieldData.(VName); |
---|
685 | % end |
---|
686 | %identify error flag |
---|
687 | errorflag=0; %default, no error flag |
---|
688 | if isfield(CellInfo{icell},'VarIndex_errorflag');% test for error flag |
---|
689 | FFName=FieldData.ListVarName{CellInfo{icell}.VarIndex_errorflag}; |
---|
690 | errorflag=FieldData.(FFName); |
---|
691 | end |
---|
692 | VarName=FieldData.ListVarName(VarIndex);% cell array of the names of variables to pje |
---|
693 | ivar_U=[]; |
---|
694 | ivar_V=[]; |
---|
695 | %% check needed object properties for unstructured positions (position given by the variables with role coord_x, coord_y |
---|
696 | |
---|
697 | % circul=0; |
---|
698 | % flux=0; |
---|
699 | %%%%%%% % A FAIRE CALCULER MEAN DES QUANTITES %%%%%% |
---|
700 | switch CellInfo{icell}.CoordType |
---|
701 | %case of unstructured coordinates |
---|
702 | case 'scattered' |
---|
703 | % XName= FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)}; |
---|
704 | % YName= FieldData.ListVarName{CellInfo{icell}.CoordIndex(end-1)}; |
---|
705 | coord_x=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)}); |
---|
706 | coord_y=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(end-1)}); |
---|
707 | |
---|
708 | if isequal(ProjMode,'projection') |
---|
709 | if width==0 |
---|
710 | errormsg='range of the projection object is missing'; |
---|
711 | return |
---|
712 | end |
---|
713 | ProjData.ListVarName=[ProjData.ListVarName FieldData.ListVarName(CellInfo{icell}.CoordIndex(end))]; |
---|
714 | ProjData.VarDimName=[ProjData.VarDimName FieldData.ListVarName(CellInfo{icell}.CoordIndex(end))]; |
---|
715 | nbvar=numel(ProjData.ListVarName); |
---|
716 | ProjData.VarAttribute{nbvar}.long_name='abscissa along line'; |
---|
717 | % select the (non false) input data located in the band of projection |
---|
718 | flagsel=(errorflag==0) & ((coord_y -yinf(1))*(xinf(2)-xinf(1))>(coord_x-xinf(1))*(yinf(2)-yinf(1))) ... |
---|
719 | & ((coord_y -ysup(1))*(xsup(2)-xsup(1))<(coord_x-xsup(1))*(ysup(2)-ysup(1))) ... |
---|
720 | & ((coord_y -yinf(2))*(xsup(2)-xinf(2))>(coord_x-xinf(2))*(ysup(2)-yinf(2))) ... |
---|
721 | & ((coord_y -yinf(1))*(xsup(1)-xinf(1))<(coord_x-xinf(1))*(ysup(1)-yinf(1))); |
---|
722 | coord_x=coord_x(flagsel); |
---|
723 | coord_y=coord_y(flagsel); |
---|
724 | costheta=cos(theta); |
---|
725 | sintheta=sin(theta); |
---|
726 | Xproj=(coord_x-ObjectData.Coord(1,1))*costheta + (coord_y-ObjectData.Coord(1,2))*sintheta; %projection on the line |
---|
727 | [Xproj,indsort]=sort(Xproj);% sort points by increasing absissa along the projection line |
---|
728 | ProjData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)})=Xproj; |
---|
729 | for ivar=1:numel(VarIndex) |
---|
730 | ProjData.(VarName{ivar})=FieldData.(VarName{ivar})(flagsel);% restrict variables to the projection band |
---|
731 | ProjData.(VarName{ivar})=ProjData.(VarName{ivar})(indsort);% sort by absissa |
---|
732 | ProjData.ListVarName=[ProjData.ListVarName VarName{ivar}]; |
---|
733 | ProjData.VarDimName=[ProjData.VarDimName FieldData.ListVarName(CellInfo{icell}.CoordIndex(end))]; |
---|
734 | ProjData.VarAttribute{nbvar+ivar}=FieldData.VarAttribute{VarIndex(ivar)};%reproduce var attribute |
---|
735 | if isfield(ProjData.VarAttribute{nbvar+ivar},'Role') |
---|
736 | if strcmp(ProjData.VarAttribute{nbvar+ivar}.Role,'vector_x'); |
---|
737 | ivar_U=nbvar+ivar; |
---|
738 | elseif strcmp(ProjData.VarAttribute{nbvar+ivar}.Role,'vector_y'); |
---|
739 | ivar_V=nbvar+ivar; |
---|
740 | end |
---|
741 | end |
---|
742 | ProjData.VarAttribute{ivar+nbvar}.Role='discrete';% will promote plots of the profiles with continuous lines |
---|
743 | end |
---|
744 | elseif isequal(ProjMode,'interp_lin') %filtering %linear interpolation: |
---|
745 | if ~check_abscissa |
---|
746 | XName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)}; |
---|
747 | ProjData.ListVarName=[ProjData.ListVarName {XName}]; |
---|
748 | ProjData.VarDimName=[ProjData.VarDimName {XName}]; |
---|
749 | nbvar=numel(ProjData.ListVarName); |
---|
750 | ProjData.VarAttribute{nbvar}.long_name='abscissa along line'; |
---|
751 | check_abscissa=1; % define abcissa only once |
---|
752 | end |
---|
753 | if ~isequal(errorflag,0) |
---|
754 | VarName_FF=FieldData.ListVarName{CellInfo{icell}.VarIndex_errorflag}; |
---|
755 | indsel=find(FieldData.(VarName_FF)==0); |
---|
756 | coord_x=coord_x(indsel); |
---|
757 | coord_y=coord_y(indsel); |
---|
758 | for ivar=1:numel(CellInfo{icell}.VarIndex) |
---|
759 | VarName=FieldData.ListVarName{CellInfo{icell}.VarIndex(ivar)}; |
---|
760 | FieldData.(VarName)=FieldData.(VarName)(indsel); |
---|
761 | end |
---|
762 | end |
---|
763 | [ProjVar,ListFieldProj,VarAttribute,errormsg]=calc_field_interp([coord_x coord_y],FieldData,CellInfo{icell}.FieldName,XI,YI); |
---|
764 | ProjData.X=Xproj; |
---|
765 | nbvar=numel(ProjData.ListVarName); |
---|
766 | ProjData.ListVarName=[ProjData.ListVarName ListFieldProj]; |
---|
767 | ProjData.VarAttribute=[ProjData.VarAttribute VarAttribute]; |
---|
768 | for ivar=1:numel(VarAttribute) |
---|
769 | ProjData.VarDimName=[ProjData.VarDimName {XName}]; |
---|
770 | if isfield(VarAttribute{ivar},'Role') |
---|
771 | if strcmp(VarAttribute{ivar}.Role,'vector_x'); |
---|
772 | ivar_U=ivar+nbvar; |
---|
773 | elseif strcmp(VarAttribute{ivar}.Role,'vector_y'); |
---|
774 | ivar_V=ivar+nbvar; |
---|
775 | end |
---|
776 | end |
---|
777 | ProjData.VarAttribute{ivar+nbvar}.Role='continuous';% will promote plots of the profiles with continuous lines |
---|
778 | ProjData.(ListFieldProj{ivar})=ProjVar{ivar}; |
---|
779 | end |
---|
780 | end |
---|
781 | case 'tps'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
782 | if strcmp(ProjMode,'interp_tps') |
---|
783 | Coord=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex}); |
---|
784 | NbCentres=FieldData.(FieldData.ListVarName{CellInfo{icell}.NbCentres_tps}); |
---|
785 | SubRange=FieldData.(FieldData.ListVarName{CellInfo{icell}.SubRange_tps}); |
---|
786 | if isfield(CellInfo{icell},'VarIndex_vector_x_tps')&&isfield(CellInfo{icell},'VarIndex_vector_y_tps') |
---|
787 | FieldVar=cat(3,FieldData.(FieldData.ListVarName{CellInfo{icell}.VarIndex_vector_x_tps}),FieldData.(FieldData.ListVarName{CellInfo{icell}.VarIndex_vector_y_tps})); |
---|
788 | end |
---|
789 | [DataOut,VarAttribute,errormsg]=calc_field_tps(Coord,NbCentres,SubRange,FieldVar,CellInfo{icell}.FieldName,cat(3,XI,YI)); |
---|
790 | ProjData.ListVarName=[ProjData.ListVarName {'X'}]; |
---|
791 | ProjData.VarDimName=[ProjData.VarDimName {'X'}]; |
---|
792 | ProjData.X=Xproj; |
---|
793 | nbvar=numel(ProjData.ListVarName); |
---|
794 | ProjData.VarAttribute{nbvar}.long_name='abscissa along line'; |
---|
795 | ProjVarName=(fieldnames(DataOut))'; |
---|
796 | ProjData.ListVarName=[ProjData.ListVarName ProjVarName]; |
---|
797 | ProjData.VarAttribute=[ProjData.VarAttribute VarAttribute]; |
---|
798 | for ivar=1:numel(VarAttribute) |
---|
799 | ProjData.VarDimName=[ProjData.VarDimName {'X'}]; |
---|
800 | if isfield(VarAttribute{ivar},'Role') |
---|
801 | if strcmp(VarAttribute{ivar}.Role,'vector_x'); |
---|
802 | ivar_U=ivar+nbvar; |
---|
803 | elseif strcmp(VarAttribute{ivar}.Role,'vector_y'); |
---|
804 | ivar_V=ivar+nbvar; |
---|
805 | end |
---|
806 | end |
---|
807 | ProjData.VarAttribute{ivar+nbvar}.Role='continuous';% will promote plots of the profiles with continuous lines |
---|
808 | ProjData.(ProjVarName{ivar})=DataOut.(ProjVarName{ivar}); |
---|
809 | end |
---|
810 | end |
---|
811 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
812 | |
---|
813 | case 'grid' %case of structured coordinates |
---|
814 | if ~isequal(ObjectData.Type,'line')% exclude polyline |
---|
815 | errormsg=['no projection available on ' ObjectData.Type 'for structured coordinates']; % |
---|
816 | else |
---|
817 | test_Amat=1;%image or 2D matrix |
---|
818 | test_interp2=0;%default |
---|
819 | AYName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(end-1)}; |
---|
820 | AXName=FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)}; |
---|
821 | eval(['AX=FieldData.' AXName ';']);% set of x positions |
---|
822 | eval(['AY=FieldData.' AYName ';']);% set of y positions |
---|
823 | AName=FieldData.ListVarName{VarIndex(1)}; |
---|
824 | eval(['A=FieldData.' AName ';']);% scalar |
---|
825 | npxy=size(A); |
---|
826 | npx=npxy(2); |
---|
827 | npy=npxy(1); |
---|
828 | if numel(AX)==2 |
---|
829 | DX=(AX(2)-AX(1))/(npx-1); |
---|
830 | else |
---|
831 | DX_vec=diff(AX); |
---|
832 | DX=max(DX_vec); |
---|
833 | DX_min=min(DX_vec); |
---|
834 | if (DX-DX_min)>0.0001*abs(DX) |
---|
835 | test_interp2=1; |
---|
836 | DX=DX_min; |
---|
837 | end |
---|
838 | end |
---|
839 | if numel(AY)==2 |
---|
840 | DY=(AY(2)-AY(1))/(npy-1); |
---|
841 | else |
---|
842 | DY_vec=diff(AY); |
---|
843 | DY=max(DY_vec); |
---|
844 | DY_min=min(DY_vec); |
---|
845 | if (DY-DY_min)>0.0001*abs(DY) |
---|
846 | test_interp2=1; |
---|
847 | DY=DY_min; |
---|
848 | end |
---|
849 | end |
---|
850 | AXI=linspace(AX(1),AX(end), npx);%set of x positions for the interpolated input data |
---|
851 | AYI=linspace(AY(1),AY(end), npy);%set of x positions for the interpolated input data |
---|
852 | if isfield(ObjectData,'DX') |
---|
853 | DXY_line=ObjectData.DX;%mesh on the projection line |
---|
854 | else |
---|
855 | DXY_line=sqrt(abs(DX*DY));% mesh on the projection line |
---|
856 | end |
---|
857 | dlinx=ObjectData.Coord(2,1)-ObjectData.Coord(1,1); |
---|
858 | dliny=ObjectData.Coord(2,2)-ObjectData.Coord(1,2); |
---|
859 | linelength=sqrt(dlinx*dlinx+dliny*dliny); |
---|
860 | theta=angle(dlinx+i*dliny);%angle of the line |
---|
861 | if isfield(FieldData,'RangeX') |
---|
862 | XMin=min(FieldData.RangeX);%shift of the origin on the line |
---|
863 | else |
---|
864 | XMin=0; |
---|
865 | end |
---|
866 | eval(['ProjData.' AXName '=linspace(XMin,XMin+linelength,linelength/DXY_line+1);'])%abscissa of the new pixels along the line |
---|
867 | y=linspace(-width,width,2*width/DXY_line+1);%ordintes of the new pixels (coordinate across the line) |
---|
868 | eval(['npX=length(ProjData.' AXName ');']) |
---|
869 | npY=length(y); %TODO: utiliser proj_grid |
---|
870 | eval(['[X,Y]=meshgrid(ProjData.' AXName ',y);'])%grid in the line coordinates |
---|
871 | XIMA=ObjectData.Coord(1,1)+(X-XMin)*cos(theta)-Y*sin(theta); |
---|
872 | YIMA=ObjectData.Coord(1,2)+(X-XMin)*sin(theta)+Y*cos(theta); |
---|
873 | XIMA=(XIMA-AX(1))/DX+1;% index of the original image along x |
---|
874 | YIMA=(YIMA-AY(1))/DY+1;% index of the original image along y |
---|
875 | XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line' |
---|
876 | YIMA=reshape(round(YIMA),1,npX*npY); |
---|
877 | flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image |
---|
878 | ind_in=find(flagin); |
---|
879 | ind_out=find(~flagin); |
---|
880 | ICOMB=(XIMA-1)*npy+YIMA; |
---|
881 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
882 | nbcolor=1; %color images |
---|
883 | if numel(npxy)==2 |
---|
884 | nbcolor=1; |
---|
885 | elseif length(npxy)==3 |
---|
886 | nbcolor=npxy(3); |
---|
887 | else |
---|
888 | errormsg='multicomponent field not projected'; |
---|
889 | display(errormsg) |
---|
890 | return |
---|
891 | end |
---|
892 | nbvar=length(ProjData.ListVarName);% number of var from previous cells |
---|
893 | ProjData.ListVarName=[ProjData.ListVarName {AXName}]; |
---|
894 | ProjData.VarDimName=[ProjData.VarDimName {AXName}]; |
---|
895 | for ivar=VarIndex |
---|
896 | %VarName{ivar}=FieldData.ListVarName{ivar}; |
---|
897 | if test_interp2% interpolate on new grid |
---|
898 | FieldData.(FieldData.ListVarName{ivar})=interp2(FieldData.(AXName),FieldData.(AYName),FieldData.(FieldData.ListVarName{ivar}),AXI,AYI);%TO TEST |
---|
899 | end |
---|
900 | vec_A=reshape(squeeze(FieldData.(FieldData.ListVarName{ivar})),npx*npy,nbcolor); %put the original image in colum |
---|
901 | if nbcolor==1 |
---|
902 | vec_B(ind_in)=vec_A(ICOMB); |
---|
903 | vec_B(ind_out)=zeros(size(ind_out)); |
---|
904 | A_out=reshape(vec_B,npY,npX); |
---|
905 | ProjData.(FieldData.ListVarName{ivar}) =sum(A_out,1)/npY; |
---|
906 | elseif nbcolor==3 |
---|
907 | vec_B(ind_in,1:3)=vec_A(ICOMB,:); |
---|
908 | vec_B(ind_out,1)=zeros(size(ind_out)); |
---|
909 | vec_B(ind_out,2)=zeros(size(ind_out)); |
---|
910 | vec_B(ind_out,3)=zeros(size(ind_out)); |
---|
911 | A_out=reshape(vec_B,npY,npX,nbcolor); |
---|
912 | ProjData.(FieldData.ListVarName{ivar})=squeeze(sum(A_out,1)/npY); |
---|
913 | end |
---|
914 | ProjData.ListVarName=[ProjData.ListVarName FieldData.ListVarName{ivar}]; |
---|
915 | ProjData.VarDimName=[ProjData.VarDimName {AXName}];%to generalize with the initial name of the x coordinate |
---|
916 | ProjData.VarAttribute{ivar}.Role='continuous';% for plot with continuous line |
---|
917 | end |
---|
918 | if nbcolor==3 |
---|
919 | ProjData.VarDimName{end}={AXName,'rgb'}; |
---|
920 | end |
---|
921 | end |
---|
922 | end |
---|
923 | if ~isempty(ivar_U) && ~isempty(ivar_V) |
---|
924 | vector_x =ProjData.(ProjData.ListVarName{ivar_U}); |
---|
925 | ProjData.(ProjData.ListVarName{ivar_U}) =cos(theta)*vector_x+sin(theta)*ProjData.(ProjData.ListVarName{ivar_V}); |
---|
926 | ProjData.(ProjData.ListVarName{ivar_V}) =-sin(theta)*vector_x+cos(theta)*ProjData.(ProjData.ListVarName{ivar_V}); |
---|
927 | end |
---|
928 | end |
---|
929 | |
---|
930 | % %shotarter case for horizontal or vertical line (A FAIRE |
---|
931 | % % Rangx=[0.5 npx-0.5];%image coordiantes of corners |
---|
932 | % % Rangy=[npy-0.5 0.5]; |
---|
933 | % % if isfield(Calib,'Pxcmx')&isfield(Calib,'Pxcmy')%old calib |
---|
934 | % % Rangx=Rangx/Calib.Pxcmx; |
---|
935 | % % Rangy=Rangy/Calib.Pxcmy; |
---|
936 | % % else |
---|
937 | % % [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],[0 0]);%case of translations without rotation and quadratic deformation |
---|
938 | % % [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,[0 0]); |
---|
939 | % % end |
---|
940 | % |
---|
941 | % % test_scal=0;%default% 3- 'UserData':(get(handles.Tag,'UserData') |
---|
942 | |
---|
943 | |
---|
944 | %----------------------------------------------------------------- |
---|
945 | %project on a plane |
---|
946 | % AJOUTER flux,circul,error |
---|
947 | function [ProjData,errormsg] = proj_plane(FieldData, ObjectData) |
---|
948 | %----------------------------------------------------------------- |
---|
949 | |
---|
950 | %% rotation angles |
---|
951 | PlaneAngle=[0 0 0]; |
---|
952 | norm_plane=[0 0 1]; |
---|
953 | cos_om=1; |
---|
954 | sin_om=0; |
---|
955 | test90x=0;%=1 for 90 degree rotation alround x axis |
---|
956 | test90y=0;%=1 for 90 degree rotation alround y axis |
---|
957 | if isfield(ObjectData,'Angle')&& isequal(size(ObjectData.Angle),[1 3])&& ~isequal(ObjectData.Angle,[0 0 0]) |
---|
958 | test90y=isequal(ObjectData.Angle,[0 90 0]); |
---|
959 | PlaneAngle=(pi/180)*ObjectData.Angle; |
---|
960 | om=norm(PlaneAngle);%norm of rotation angle in radians |
---|
961 | OmAxis=PlaneAngle/om; %unit vector marking the rotation axis |
---|
962 | cos_om=cos(om); |
---|
963 | sin_om=sin(om); |
---|
964 | coeff=OmAxis(3)*(1-cos_om); |
---|
965 | %components of the unity vector norm_plane normal to the projection plane |
---|
966 | norm_plane(1)=OmAxis(1)*coeff+OmAxis(2)*sin_om; |
---|
967 | norm_plane(2)=OmAxis(2)*coeff-OmAxis(1)*sin_om; |
---|
968 | norm_plane(3)=OmAxis(3)*coeff+cos_om; |
---|
969 | end |
---|
970 | testangle=~isequal(PlaneAngle,[0 0 0])||~isequal(ObjectData.Coord(1:2),[0 0 ]) ;% && ~test90y && ~test90x;%=1 for slanted plane |
---|
971 | |
---|
972 | %% mesh sizes DX and DY |
---|
973 | DX=[]; |
---|
974 | DY=[];%default |
---|
975 | if isfield(ObjectData,'DX') && ~isempty(ObjectData.DX) |
---|
976 | DX=abs(ObjectData.DX);%mesh of interpolation points |
---|
977 | elseif isfield(FieldData,'CoordMesh') |
---|
978 | DX=FieldData.CoordMesh; |
---|
979 | end |
---|
980 | if isfield(ObjectData,'DY') && ~isempty(ObjectData.DY) |
---|
981 | DY=abs(ObjectData.DY);%mesh of interpolation points |
---|
982 | elseif isfield(FieldData,'CoordMesh') |
---|
983 | DY=FieldData.CoordMesh; |
---|
984 | end |
---|
985 | if ~strcmp(ObjectData.ProjMode,'projection') && (isempty(DX)||isempty(DY)) |
---|
986 | errormsg='DX or DY not defined'; |
---|
987 | return |
---|
988 | end |
---|
989 | |
---|
990 | %% extrema along each axis |
---|
991 | testXMin=0;% test if min of X coordinates defined on the projection object, =0 by default |
---|
992 | testXMax=0;% test if max of X coordinates defined on the projection object, =0 by default |
---|
993 | testYMin=0;% test if min of Y coordinates defined on the projection object, =0 by default |
---|
994 | testYMax=0;% test if max of Y coordinates defined on the projection object, =0 by default |
---|
995 | if isfield(ObjectData,'RangeX') % rangeX defined by the projection object |
---|
996 | XMin=min(ObjectData.RangeX); |
---|
997 | XMax=max(ObjectData.RangeX); |
---|
998 | testXMin=XMax>XMin;%=1 if XMin defined (i.e. RangeY has two distinct elements) |
---|
999 | testXMax=1;% max of X coordinates defined on the projection object |
---|
1000 | end |
---|
1001 | if isfield(ObjectData,'RangeY') % rangeY defined by the projection object |
---|
1002 | YMin=min(ObjectData.RangeY); |
---|
1003 | YMax=max(ObjectData.RangeY); |
---|
1004 | testYMin=YMax>YMin;%=1 if YMin defined (i.e. RangeY has tow distinct elements) |
---|
1005 | testYMax=1;% max of Y coordinates defined on the projection object |
---|
1006 | end |
---|
1007 | width=0;%default width of the projection band |
---|
1008 | if isfield(ObjectData,'RangeZ') |
---|
1009 | width=max(ObjectData.RangeZ); |
---|
1010 | end |
---|
1011 | |
---|
1012 | %% interpolation range |
---|
1013 | thresh2=[]; |
---|
1014 | if isfield(ObjectData,'RangeInterp') |
---|
1015 | thresh2=ObjectData.RangeInterp*ObjectData.RangeInterp;%square of interpolation range (do not interpolate beyond this range) |
---|
1016 | end |
---|
1017 | |
---|
1018 | %% initiate Matlab structure for physical field |
---|
1019 | [ProjData,errormsg]=proj_heading(FieldData,ObjectData); |
---|
1020 | if ~isempty(errormsg) |
---|
1021 | return |
---|
1022 | end |
---|
1023 | |
---|
1024 | %% reproduce initial plane position and angle |
---|
1025 | if isfield(FieldData,'PlaneCoord')&&length(FieldData.PlaneCoord)==3&& isfield(ProjData,'ProjObjectCoord') |
---|
1026 | if length(ProjData.ProjObjectCoord)==3% if the projection plane has a z coordinate |
---|
1027 | if isfield(ProjData,'.PlaneCoord') && ~isequal(ProjData.PlaneCoord(3),ProjData.ProjObjectCoord) %check the consistency with the z coordinate of the field plane (set by calibration) |
---|
1028 | errormsg='inconsistent z position for field and projection plane'; |
---|
1029 | return |
---|
1030 | end |
---|
1031 | else % the z coordinate is set only by the field plane (by calibration) |
---|
1032 | ProjData.ProjObjectCoord(3)=FieldData.PlaneCoord(3); |
---|
1033 | end |
---|
1034 | if isfield(FieldData,'PlaneAngle') |
---|
1035 | if isfield(ProjData,'ProjObjectAngle') |
---|
1036 | if ~isequal(FieldData.PlaneAngle,ProjData.ProjObjectAngle) %check the consistency with the z coordinate of the field plane (set by calibration) |
---|
1037 | errormsg='inconsistent plane angle for field and projection plane'; |
---|
1038 | return |
---|
1039 | end |
---|
1040 | else |
---|
1041 | ProjData.ProjObjectAngle=FieldData.PlaneAngle; |
---|
1042 | end |
---|
1043 | end |
---|
1044 | end |
---|
1045 | ProjData.NbDim=2; |
---|
1046 | ProjData.ListVarName={}; |
---|
1047 | ProjData.VarDimName={}; |
---|
1048 | ProjData.VarAttribute={}; |
---|
1049 | if ~isempty(DX) && ~isempty(DY) |
---|
1050 | ProjData.CoordMesh=sqrt(DX*DY);%define typical data mesh, useful for mouse selection in plots |
---|
1051 | elseif isfield(FieldData,'CoordMesh') |
---|
1052 | ProjData.CoordMesh=FieldData.CoordMesh; |
---|
1053 | end |
---|
1054 | error=0;%default |
---|
1055 | flux=0; |
---|
1056 | testfalse=0; |
---|
1057 | ListIndex={}; |
---|
1058 | |
---|
1059 | %% group the variables (fields of 'FieldData') in cells of variables with the same dimensions |
---|
1060 | [CellInfo,NbDimArray,errormsg]=find_field_cells(FieldData); |
---|
1061 | |
---|
1062 | if ~isempty(errormsg) |
---|
1063 | errormsg=['error in proj_field/proj_plane:' errormsg]; |
---|
1064 | return |
---|
1065 | end |
---|
1066 | check_grid=zeros(size(CellInfo));% =1 if a grid is needed , =0 otherwise, for each field cell |
---|
1067 | |
---|
1068 | ProjMode=cell(size(CellInfo)); |
---|
1069 | for icell=1:numel(CellInfo) |
---|
1070 | ProjMode{icell}=ObjectData.ProjMode;% projection mode of the plane object |
---|
1071 | end |
---|
1072 | icell_grid=[];% field cell index which defines the grid |
---|
1073 | if ~strcmp(ObjectData.ProjMode,'projection') |
---|
1074 | %% define the new coordinates in case of interpolation on a imposed grid |
---|
1075 | if ~testYMin |
---|
1076 | errormsg='min Y value not defined for the projection grid';return |
---|
1077 | end |
---|
1078 | if ~testYMax |
---|
1079 | errormsg='max Y value not defined for the projection grid';return |
---|
1080 | end |
---|
1081 | if ~testXMin |
---|
1082 | errormsg='min X value not defined for the projection grid';return |
---|
1083 | end |
---|
1084 | if ~testXMax |
---|
1085 | errormsg='max X value not defined for the projection grid';return |
---|
1086 | end |
---|
1087 | else |
---|
1088 | %% case of a grid requested by the input field |
---|
1089 | for icell=1:numel(CellInfo)% TODO: recalculate coordinates here to get the bounds in the rotated coordinates |
---|
1090 | if isfield(CellInfo{icell},'ProjModeRequest') |
---|
1091 | switch CellInfo{icell}.ProjModeRequest |
---|
1092 | case 'interp_lin' |
---|
1093 | ProjMode{icell}='interp_lin'; |
---|
1094 | case 'interp_tps' |
---|
1095 | ProjMode{icell}='interp_tps'; |
---|
1096 | end |
---|
1097 | end |
---|
1098 | if strcmp(ProjMode{icell},'interp_lin')||strcmp(ProjMode{icell},'interp_tps') |
---|
1099 | check_grid(icell)=1; |
---|
1100 | end |
---|
1101 | if strcmp(CellInfo{icell}.CoordType,'grid')&&NbDimArray(icell)>=2 |
---|
1102 | if ~testangle && isempty(icell_grid)% if the input gridded data is not modified, choose the first one in case of multiple gridded field cells |
---|
1103 | icell_grid=icell; |
---|
1104 | ProjMode{icell}='projection'; |
---|
1105 | end |
---|
1106 | check_grid(icell)=1; |
---|
1107 | end |
---|
1108 | end |
---|
1109 | if ~isempty(find(check_grid))% if a grid is requested by the input field |
---|
1110 | if isempty(icell_grid)% if the grid is not given by cell #icell_grid |
---|
1111 | if ~isfield(FieldData,'XMax') |
---|
1112 | FieldData=find_field_bounds(FieldData); |
---|
1113 | end |
---|
1114 | end |
---|
1115 | end |
---|
1116 | end |
---|
1117 | if ~isempty(find(check_grid))||~strcmp(ObjectData.ProjMode,'projection')%no existing gridded data used |
---|
1118 | if isempty(icell_grid)||~strcmp(ObjectData.ProjMode,'projection')%no existing gridded data used |
---|
1119 | AYName='coord_y'; |
---|
1120 | AXName='coord_x'; |
---|
1121 | if strcmp(ObjectData.ProjMode,'projection') |
---|
1122 | ProjData.coord_y=[FieldData.YMin FieldData.YMax];%note that if projection is done on a grid, the Min and Max along each direction must have been defined |
---|
1123 | ProjData.coord_x=[FieldData.XMin FieldData.XMax]; |
---|
1124 | coord_x_proj=FieldData.XMin:FieldData.CoordMesh:FieldData.XMax; |
---|
1125 | coord_y_proj=FieldData.YMin:FieldData.CoordMesh:FieldData.YMax; |
---|
1126 | else |
---|
1127 | ProjData.coord_y=[ObjectData.RangeY(1) ObjectData.RangeY(2)];%note that if projection is done on a grid, the Min and Max along each direction must have been defined |
---|
1128 | ProjData.coord_x=[ObjectData.RangeX(1) ObjectData.RangeX(2)]; |
---|
1129 | coord_x_proj=ObjectData.RangeX(1):ObjectData.DX:ObjectData.RangeX(2); |
---|
1130 | coord_y_proj=ObjectData.RangeY(1):ObjectData.DY:ObjectData.RangeY(2); |
---|
1131 | end |
---|
1132 | [XI,YI]=meshgrid(coord_x_proj,coord_y_proj);%grid in the new coordinates |
---|
1133 | ProjData.VarDimName={AYName,AXName}; |
---|
1134 | % XI=ObjectData.Coord(1,1)+(X)*cos(PlaneAngle(3))-YI*sin(PlaneAngle(3));%corresponding coordinates in the original system |
---|
1135 | % YI=ObjectData.Coord(1,2)+(X)*sin(PlaneAngle(3))+YI*cos(PlaneAngle(3)); |
---|
1136 | else% we use the existing grid from field cell #icell_grid |
---|
1137 | NbDim=NbDimArray(icell_grid); |
---|
1138 | AYName=FieldData.ListVarName{CellInfo{icell_grid}.CoordIndex(NbDim-1)};%name of input x coordinate (name preserved on projection) |
---|
1139 | AXName=FieldData.ListVarName{CellInfo{icell_grid}.CoordIndex(NbDim)};%name of input y coordinate (name preserved on projection) |
---|
1140 | AYDimName=FieldData.VarDimName{CellInfo{icell_grid}.CoordIndex(NbDim-1)};% |
---|
1141 | AXDimName=FieldData.VarDimName{CellInfo{icell_grid}.CoordIndex(NbDim)};% |
---|
1142 | ProjData.VarDimName={AYDimName,AXDimName}; |
---|
1143 | ProjData.(AYName)=FieldData.(AYName); % new (projected ) y coordinates |
---|
1144 | ProjData.(AXName)=FieldData.(AXName); % new (projected ) y coordinates |
---|
1145 | end |
---|
1146 | ProjData.ListVarName={AYName,AXName}; |
---|
1147 | |
---|
1148 | ProjData.VarAttribute={[],[]}; |
---|
1149 | end |
---|
1150 | |
---|
1151 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
1152 | % LOOP ON FIELD CELLS, PROJECT VARIABLES |
---|
1153 | % CellVarIndex=cells of variable index arrays |
---|
1154 | %ivar_new=0; % index of the current variable in the projected field |
---|
1155 | % icoord=0; |
---|
1156 | nbcoord=0;%number of added coordinate variables brought by projection |
---|
1157 | %nbvar=0; |
---|
1158 | vector_x_proj=[]; |
---|
1159 | vector_y_proj=[]; |
---|
1160 | for icell=1:length(CellInfo) |
---|
1161 | NbDim=NbDimArray(icell); |
---|
1162 | if NbDim<2 |
---|
1163 | continue % only cells represnting 2D or 3D fields are involved |
---|
1164 | end |
---|
1165 | VarIndex=CellInfo{icell}.VarIndex;% indices of the selected variables in the list FieldData.ListVarName |
---|
1166 | %dimensions |
---|
1167 | DimCell=FieldData.VarDimName{VarIndex(1)}; |
---|
1168 | if ischar(DimCell) |
---|
1169 | DimCell={DimCell};%name of dimensions |
---|
1170 | end |
---|
1171 | coord_z=0;%default |
---|
1172 | ListVarName={};% initiate list of projected variables for cell # icell |
---|
1173 | VarDimName={};% initiate coresponding list of dimensions for cell # icell |
---|
1174 | VarAttribute={};% initiate coresponding list of var attributes for cell # icell |
---|
1175 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
1176 | switch CellInfo{icell}.CoordType |
---|
1177 | |
---|
1178 | case 'scattered' |
---|
1179 | %% case of input fields with unstructured coordinates (applies for projMode ='projection' or 'interp_lin') |
---|
1180 | if strcmp(ProjMode{icell},'interp_tps') |
---|
1181 | continue %skip for next cell (needs tps field cell) |
---|
1182 | end |
---|
1183 | coord_x=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(end)});% initial x coordinates |
---|
1184 | coord_y=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(end-1)});% initial y coordinates |
---|
1185 | check3D=(numel(CellInfo{icell}.CoordIndex)==3); |
---|
1186 | if check3D |
---|
1187 | coord_z=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(1)}); |
---|
1188 | end |
---|
1189 | |
---|
1190 | % translate initial coordinates to account for the new origin |
---|
1191 | coord_x=coord_x-ObjectData.Coord(1,1); |
---|
1192 | coord_y=coord_y-ObjectData.Coord(1,2); |
---|
1193 | if check3D |
---|
1194 | coord_z=coord_z-ObjectData.Coord(1,3); |
---|
1195 | end |
---|
1196 | |
---|
1197 | % selection of the vectors in the projection range (3D case) |
---|
1198 | if check3D && width > 0 |
---|
1199 | %components of the unitiy vector normal to the projection plane |
---|
1200 | fieldZ=norm_plane(1)*coord_x + norm_plane(2)*coord_y+ norm_plane(3)*coord_z;% distance to the plane |
---|
1201 | indcut=find(abs(fieldZ) <= width); |
---|
1202 | for ivar=VarIndex |
---|
1203 | VarName=FieldData.ListVarName{ivar}; |
---|
1204 | FieldData.(VarName)=FieldData.(VarName)(indcut); |
---|
1205 | end |
---|
1206 | coord_x=coord_x(indcut); |
---|
1207 | coord_y=coord_y(indcut); |
---|
1208 | coord_z=coord_z(indcut); |
---|
1209 | end |
---|
1210 | |
---|
1211 | %rotate coordinates if needed: coord_X,coord_Y= = coordinates in the new plane |
---|
1212 | Psi=PlaneAngle(1); |
---|
1213 | Theta=PlaneAngle(2); |
---|
1214 | Phi=PlaneAngle(3); |
---|
1215 | if testangle && ~test90y && ~test90x;%=1 for slanted plane |
---|
1216 | coord_X=(coord_x *cos(Phi) + coord_y* sin(Phi)); |
---|
1217 | coord_Y=(-coord_x *sin(Phi) + coord_y *cos(Phi))*cos(Theta); |
---|
1218 | coord_Y=coord_Y+coord_z *sin(Theta); |
---|
1219 | coord_X=(coord_X *cos(Psi) - coord_Y* sin(Psi));%A VERIFIER |
---|
1220 | coord_Y=(coord_X *sin(Psi) + coord_Y* cos(Psi)); |
---|
1221 | else |
---|
1222 | coord_X=coord_x; |
---|
1223 | coord_Y=coord_y; |
---|
1224 | end |
---|
1225 | |
---|
1226 | %restriction to the range of X and Y if imposed by the projection object |
---|
1227 | testin=ones(size(coord_X)); %default |
---|
1228 | testbound=0; |
---|
1229 | if testXMin |
---|
1230 | testin=testin & (coord_X >= XMin); |
---|
1231 | testbound=1; |
---|
1232 | end |
---|
1233 | if testXMax |
---|
1234 | testin=testin & (coord_X <= XMax); |
---|
1235 | testbound=1; |
---|
1236 | end |
---|
1237 | if testYMin |
---|
1238 | testin=testin & (coord_Y >= YMin); |
---|
1239 | testbound=1; |
---|
1240 | end |
---|
1241 | if testYMin |
---|
1242 | testin=testin & (coord_Y <= YMax); |
---|
1243 | testbound=1; |
---|
1244 | end |
---|
1245 | if testbound |
---|
1246 | indcut=find(testin); |
---|
1247 | if isempty(indcut) |
---|
1248 | errormsg='data outside the bounds of the projection object'; |
---|
1249 | return |
---|
1250 | end |
---|
1251 | for ivar=VarIndex |
---|
1252 | VarName=FieldData.ListVarName{ivar}; |
---|
1253 | FieldData.(VarName)=FieldData.(VarName)(indcut); |
---|
1254 | end |
---|
1255 | coord_X=coord_X(indcut); |
---|
1256 | coord_Y=coord_Y(indcut); |
---|
1257 | if check3D |
---|
1258 | coord_Z=coord_Z(indcut); |
---|
1259 | end |
---|
1260 | end |
---|
1261 | |
---|
1262 | % two cases of projection for scattered coordinates |
---|
1263 | switch ProjMode{icell} |
---|
1264 | case 'projection' |
---|
1265 | nbvar=0; |
---|
1266 | %nbvar=numel(ProjData.ListVarName); |
---|
1267 | for ivar=VarIndex %transfer variables to the projection plane |
---|
1268 | VarName=FieldData.ListVarName{ivar}; |
---|
1269 | if ivar==CellInfo{icell}.CoordIndex(end) |
---|
1270 | ProjData.(VarName)=coord_X; |
---|
1271 | elseif ivar==CellInfo{icell}.CoordIndex(end-1) % y coordinate |
---|
1272 | ProjData.(VarName)=coord_Y; |
---|
1273 | elseif ~(check3D && ivar==CellInfo{icell}.CoordIndex(1)) % other variables (except Z coordinate wyhich is not reproduced) |
---|
1274 | ProjData.(VarName)=FieldData.(VarName); |
---|
1275 | end |
---|
1276 | if ~(check3D && ivar==CellInfo{icell}.CoordIndex(1)) |
---|
1277 | ListVarName=[ListVarName VarName]; |
---|
1278 | VarDimName=[VarDimName DimCell]; |
---|
1279 | nbvar=nbvar+1; |
---|
1280 | if isfield(FieldData,'VarAttribute') && length(FieldData.VarAttribute) >=ivar |
---|
1281 | VarAttribute{nbvar}=FieldData.VarAttribute{ivar}; |
---|
1282 | end |
---|
1283 | end |
---|
1284 | end |
---|
1285 | case 'interp_lin'%interpolate data on a regular grid |
---|
1286 | if isfield(CellInfo{icell},'VarIndex_errorflag') |
---|
1287 | VarName_FF=FieldData.ListVarName{CellInfo{icell}.VarIndex_errorflag}; |
---|
1288 | indsel=find(FieldData.(VarName_FF)==0); |
---|
1289 | coord_X=coord_X(indsel); |
---|
1290 | coord_Y=coord_Y(indsel); |
---|
1291 | for ivar=1:numel(CellInfo{icell}.VarIndex) |
---|
1292 | VarName=FieldData.ListVarName{CellInfo{icell}.VarIndex(ivar)}; |
---|
1293 | FieldData.(VarName)=FieldData.(VarName)(indsel); |
---|
1294 | end |
---|
1295 | end |
---|
1296 | % interpolate and calculate field on the grid |
---|
1297 | |
---|
1298 | [VarVal,ListVarName,VarAttribute,errormsg]=calc_field_interp([coord_X coord_Y],FieldData,CellInfo{icell}.FieldName,XI,YI); |
---|
1299 | |
---|
1300 | % set to NaN interpolation points which are too far from any initial data (more than 2 CoordMesh) |
---|
1301 | if exist('scatteredInterpolant','file')%recent Matlab versions |
---|
1302 | F=scatteredInterpolant(coord_X, coord_Y,coord_X,'nearest'); |
---|
1303 | G=scatteredInterpolant(coord_X, coord_Y,coord_Y,'nearest'); |
---|
1304 | else |
---|
1305 | F=TriScatteredInterp([coord_X coord_Y],coord_X,'nearest'); |
---|
1306 | G=TriScatteredInterp([coord_X coord_Y],coord_Y,'nearest'); |
---|
1307 | end |
---|
1308 | Distx=F(XI,YI)-XI;% diff of x coordinates with the nearest measurement point |
---|
1309 | Disty=G(XI,YI)-YI;% diff of y coordinates with the nearest measurement point |
---|
1310 | Dist=Distx.*Distx+Disty.*Disty; |
---|
1311 | if ~isempty(thresh2) |
---|
1312 | for ivar=1:numel(VarVal) |
---|
1313 | VarVal{ivar}(Dist>thresh2)=NaN;% % put to NaN interpolated positions further than 4 meshes from initial data |
---|
1314 | end |
---|
1315 | end |
---|
1316 | if isfield(CellInfo{icell},'CheckSub') && CellInfo{icell}.CheckSub && ~isempty(vector_x_proj) |
---|
1317 | ProjData.(FieldData.ListVarName{vector_x_proj})=ProjData.(FieldData.ListVarName{vector_x_proj})-VarVal{1}; |
---|
1318 | ProjData.(FieldData.ListVarName{vector_y_proj})=ProjData.(FieldData.ListVarName{vector_y_proj})-VarVal{2}; |
---|
1319 | ListVarName={};% no new variable |
---|
1320 | VarAttribute={}; |
---|
1321 | else |
---|
1322 | VarDimName=cell(size(ListVarName)); |
---|
1323 | for ilist=1:numel(ListVarName)% reshape data, excluding coordinates (ilist=1-2), TODO: rationalise |
---|
1324 | ListVarName{ilist}=regexprep(ListVarName{ilist},'(.+',''); |
---|
1325 | if ~isempty(find(strcmp(ListVarName{ilist},ProjData.ListVarName))) |
---|
1326 | ListVarName{ilist}=[ListVarName{ilist} '_1']; |
---|
1327 | end |
---|
1328 | ProjData.(ListVarName{ilist})=VarVal{ilist}; |
---|
1329 | VarDimName{ilist}={'coord_y','coord_x'}; |
---|
1330 | end |
---|
1331 | end |
---|
1332 | if isfield (CellInfo{icell},'VarIndex_vector_x')&& isfield (CellInfo{icell},'VarIndex_vector_y') |
---|
1333 | vector_x_proj=CellInfo{icell}.VarIndex_vector_x; %preserve for next cell |
---|
1334 | vector_y_proj=CellInfo{icell}.VarIndex_vector_y; %preserve for next cell |
---|
1335 | end |
---|
1336 | end |
---|
1337 | |
---|
1338 | case 'tps' |
---|
1339 | %% case of tps data (applies only in interp_tps mode) |
---|
1340 | if strcmp(ProjMode{icell},'interp_tps') |
---|
1341 | Coord=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex}); |
---|
1342 | NbCentres=FieldData.(FieldData.ListVarName{CellInfo{icell}.NbCentres_tps}); |
---|
1343 | SubRange=FieldData.(FieldData.ListVarName{CellInfo{icell}.SubRange_tps}); |
---|
1344 | if isfield(CellInfo{icell},'VarIndex_vector_x_tps')&&isfield(CellInfo{icell},'VarIndex_vector_y_tps') |
---|
1345 | FieldVar=cat(3,FieldData.(FieldData.ListVarName{CellInfo{icell}.VarIndex_vector_x_tps}),FieldData.(FieldData.ListVarName{CellInfo{icell}.VarIndex_vector_y_tps})); |
---|
1346 | end |
---|
1347 | % interpolate data using thin plate spline |
---|
1348 | [DataOut,VarAttribute,errormsg]=calc_field_tps(Coord,NbCentres,SubRange,FieldVar,CellInfo{icell}.FieldName,cat(3,XI,YI)); |
---|
1349 | |
---|
1350 | % set to NaN interpolation points which are too far from any initial data (more than 2 CoordMesh) |
---|
1351 | Coord=permute(Coord,[1 3 2]); |
---|
1352 | Coord=reshape(Coord,size(Coord,1)*size(Coord,2),2); |
---|
1353 | if exist('scatteredInterpolant','file')%recent Matlab versions |
---|
1354 | F=scatteredInterpolant(Coord,Coord(:,1),'nearest'); |
---|
1355 | G=scatteredInterpolant(Coord,Coord(:,2),'nearest'); |
---|
1356 | else |
---|
1357 | F=TriScatteredInterp(Coord,Coord(:,1),'nearest'); |
---|
1358 | G=TriScatteredInterp(Coord,Coord(:,2),'nearest'); |
---|
1359 | end |
---|
1360 | Distx=F(XI,YI)-XI;% diff of x coordinates with the nearest measurement point |
---|
1361 | Disty=G(XI,YI)-YI;% diff of y coordinates with the nearest measurement point |
---|
1362 | Dist=Distx.*Distx+Disty.*Disty; |
---|
1363 | ListVarName=(fieldnames(DataOut))'; |
---|
1364 | VarDimName=cell(size(ListVarName)); |
---|
1365 | for ilist=1:numel(ListVarName)% reshape data, excluding coordinates (ilist=1-2), TODO: rationalise |
---|
1366 | VarName=ListVarName{ilist}; |
---|
1367 | VarDimName{ilist}={'coord_y','coord_x'}; |
---|
1368 | ProjData.(VarName)=DataOut.(VarName); |
---|
1369 | if ~isempty(thresh2) |
---|
1370 | ProjData.(VarName)(Dist>thresh2)=NaN;% put to NaN interpolated positions further than RangeInterp from initial data |
---|
1371 | end |
---|
1372 | end |
---|
1373 | end |
---|
1374 | |
---|
1375 | case 'grid' |
---|
1376 | %% case of input fields defined on a structured grid |
---|
1377 | VarName=FieldData.ListVarName{VarIndex(1)};%get the first variable of the cell to get the input matrix dimensions |
---|
1378 | DimValue=size(FieldData.(VarName));%input matrix dimensions |
---|
1379 | DimValue(DimValue==1)=[];%remove singleton dimensions |
---|
1380 | NbDim=numel(DimValue);%update number of space dimensions |
---|
1381 | nbcolor=1; %default number of 'color' components: third matrix index without corresponding coordinate |
---|
1382 | if NbDim>=3 |
---|
1383 | if NbDim>3 |
---|
1384 | errormsg='matrices with more than 3 dimensions not handled'; |
---|
1385 | return |
---|
1386 | else |
---|
1387 | if numel(CellInfo{icell}.CoordIndex)==2% the third matrix dimension does not correspond to a space coordinate |
---|
1388 | nbcolor=DimValue(3); |
---|
1389 | DimValue(3)=[]; %number of 'color' components updated |
---|
1390 | NbDim=2;% space dimension set to 2 |
---|
1391 | end |
---|
1392 | end |
---|
1393 | end |
---|
1394 | Coord_z=[]; |
---|
1395 | Coord_y=[]; |
---|
1396 | Coord_x=[]; |
---|
1397 | |
---|
1398 | if testangle |
---|
1399 | ProjMode{icell}='interp_lin'; %request linear interpolation for projection on a tilted plane |
---|
1400 | end |
---|
1401 | |
---|
1402 | if isequal(ProjMode{icell},'projection')% && (~testangle || test90y || test90x) |
---|
1403 | if NbDim==2 && ~testXMin && ~testXMax && ~testYMin && ~testYMax% no range restriction |
---|
1404 | ListVarName=[ListVarName FieldData.ListVarName(VarIndex)]; |
---|
1405 | VarDimName=[VarDimName FieldData.VarDimName(VarIndex)]; |
---|
1406 | if isfield(FieldData,'VarAttribute') |
---|
1407 | VarAttribute=[VarAttribute FieldData.VarAttribute(VarIndex)]; |
---|
1408 | end |
---|
1409 | ProjData.(AYName)=FieldData.(AYName); |
---|
1410 | ProjData.(AXName)=FieldData.(AXName); |
---|
1411 | for ivar=VarIndex |
---|
1412 | VarName=FieldData.ListVarName{ivar}; |
---|
1413 | ProjData.(VarName)=FieldData.(VarName);% no change by projection |
---|
1414 | end |
---|
1415 | else |
---|
1416 | Coord{1}=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(1)}); |
---|
1417 | Coord{2}=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(2)}); |
---|
1418 | if NbDim==3 |
---|
1419 | Coord{3}=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(3)}); |
---|
1420 | end |
---|
1421 | if numel(Coord{NbDim-1})==2 |
---|
1422 | DY=(Coord{NbDim-1}(2)-Coord{NbDim-1}(1))/(DimValue(1)-1); |
---|
1423 | end |
---|
1424 | if numel(Coord{NbDim})==2 |
---|
1425 | DX=(Coord{NbDim}(2)-Coord{NbDim}(1))/(DimValue(2)-1); |
---|
1426 | end |
---|
1427 | if testYMax |
---|
1428 | YIndexMax=(YMax-Coord{NbDim-1}(1))/DY+1;% matrix index corresponding to the max y value for the new field |
---|
1429 | if testYMin%test_direct(indY) |
---|
1430 | YIndexMin=(YMin-Coord{NbDim-1}(1))/DY+1;% matrix index corresponding to the min x value for the new field |
---|
1431 | else |
---|
1432 | YIndexMin=1; |
---|
1433 | end |
---|
1434 | else |
---|
1435 | YIndexMax=Coord{NbDim-1}(end)/DY; |
---|
1436 | YIndexMin=1; |
---|
1437 | end |
---|
1438 | if testXMax |
---|
1439 | XIndexMax=(XMax-Coord{NbDim}(1))/DY+1;% matrix index corresponding to the max y value for the new field |
---|
1440 | if testYMin%test_direct(indY) |
---|
1441 | XIndexMin=(XMin-Coord{NbDim}(1))/DX+1;% matrix index corresponding to the min x value for the new field |
---|
1442 | else |
---|
1443 | XIndexMin=1; |
---|
1444 | end |
---|
1445 | else |
---|
1446 | XIndexMax=Coord{NbDim}(end)/DX; |
---|
1447 | XIndexMin=1; |
---|
1448 | end |
---|
1449 | YIndexRange(1)=ceil(min(YIndexMin,YIndexMax));%first y index to select from the previous field |
---|
1450 | YIndexRange(1)=max(YIndexRange(1),1);% avoid bound lower than the first index |
---|
1451 | YIndexRange(2)=floor(max(YIndexMin,YIndexMax));%last y index to select from the previous field |
---|
1452 | YIndexRange(2)=min(YIndexRange(2),DimValue(NbDim-1));% limit to the last available index |
---|
1453 | XIndexRange(1)=ceil(min(XIndexMin,XIndexMax));%first x index to select from the previous field |
---|
1454 | XIndexRange(1)=max(XIndexRange(1),1);% avoid bound lower than the first index |
---|
1455 | XIndexRange(2)=floor(max(XIndexMin,XIndexMax));%last x index to select from the previous field |
---|
1456 | XIndexRange(2)=min(XIndexRange(2),DimValue(NbDim));% limit to the last available index |
---|
1457 | if test90y |
---|
1458 | ind_new=[3 2 1]; |
---|
1459 | DimCell={AYProjName,AXProjName}; |
---|
1460 | iz=ceil((ObjectData.Coord(1,1)-Coord{3}(1))/DX)+1; |
---|
1461 | for ivar=VarIndex |
---|
1462 | VarName=FieldData.ListVarName{ivar}; |
---|
1463 | ListVarName=[ListVarName VarName]; |
---|
1464 | VarDimName=[VarDimName {DimCell}]; |
---|
1465 | VarAttribute{length(ListVarName)}=FieldData.VarAttribute{ivar}; %reproduce the variable attributes |
---|
1466 | ProjData.(VarName)=permute(FieldData.(VarName),ind_new);% permute x and z indices for 90 degree rotation |
---|
1467 | ProjData.(VarName)=squeeze(ProjData.(VarName)(iz,:,:));% select the z index iz |
---|
1468 | end |
---|
1469 | ProjData.(AYName)=[Ybound(1) Ybound(2)]; %record the new (projected ) y coordinates |
---|
1470 | ProjData.(AXName)=[Coord{1}(end),Coord{1}(1)]; %record the new (projected ) x coordinates |
---|
1471 | else |
---|
1472 | if NbDim==3 |
---|
1473 | DZ=(Coord{1}(end)-Coord{1}(1))/(numel(Coord{1})-1); |
---|
1474 | DimCell(1)=[]; %suppress z variable |
---|
1475 | DimValue(1)=[]; |
---|
1476 | test_direct=1;%TOdo; GENERALIZE, SEE CASE OF points |
---|
1477 | if test_direct(1) |
---|
1478 | iz=ceil((ObjectData.Coord(1,3)-Coord{1}(1))/DZ)+1; |
---|
1479 | else |
---|
1480 | iz=ceil((Coord{1}(1)-ObjectData.Coord(1,3))/DZ)+1; |
---|
1481 | end |
---|
1482 | end |
---|
1483 | for ivar=VarIndex% loop on non coordinate variables |
---|
1484 | VarName=FieldData.ListVarName{ivar}; |
---|
1485 | ListVarName=[ListVarName VarName]; |
---|
1486 | VarDimName=[VarDimName {DimCell}]; |
---|
1487 | if isfield(FieldData,'VarAttribute') && length(FieldData.VarAttribute)>=ivar |
---|
1488 | VarAttribute{length(ListVarName)}=FieldData.VarAttribute{ivar}; |
---|
1489 | end |
---|
1490 | if NbDim==3 |
---|
1491 | ProjData.(VarName)=squeeze(FieldData.(VarName)(iz,YIndexRange(1):YIndexRange(end),XIndexRange(1):XIndexRange(end))); |
---|
1492 | else |
---|
1493 | ProjData.(VarName)=FieldData.(VarName)(YIndexRange(1):YIndexRange(end),XIndexRange(1):XIndexRange(end),:); |
---|
1494 | end |
---|
1495 | end |
---|
1496 | if testXMax |
---|
1497 | ProjData.(AXName)=Coord{NbDim}(1)+DX*(XIndexRange-1); %record the new (projected ) x coordinates |
---|
1498 | else |
---|
1499 | ProjData.(AXName)=FieldData.(AXName); |
---|
1500 | end |
---|
1501 | if testYMax |
---|
1502 | ProjData.(AYName)=Coord{NbDim-1}(1)+DY*(YIndexRange-1); %record the new (projected ) x coordinates |
---|
1503 | else |
---|
1504 | ProjData.(AYName)=FieldData.(AYName); |
---|
1505 | end |
---|
1506 | end |
---|
1507 | end |
---|
1508 | else % case with interpolation on a grid |
---|
1509 | if NbDim==2 %2D case |
---|
1510 | if isequal(ProjMode{icell},'interp_tps') |
---|
1511 | npx_interp_tps=ceil(abs(DX/DAX)); |
---|
1512 | npy_interp_tps=ceil(abs(DY/DAY)); |
---|
1513 | Minterp_tps=ones(npy_interp_tps,npx_interp_tps)/(npx_interp_tps*npy_interp_tps); |
---|
1514 | test_interp_tps=1; |
---|
1515 | else |
---|
1516 | test_interp_tps=0; |
---|
1517 | end |
---|
1518 | Coord{1}=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(1)}); |
---|
1519 | Coord{2}=FieldData.(FieldData.ListVarName{CellInfo{icell}.CoordIndex(2)}); |
---|
1520 | if ~(testXMin && testYMin)% % if the range of the projected coordinates is not fully defined by the projection object, find the extrema of the projected field |
---|
1521 | xcorner=[min(Coord{NbDim}) max(Coord{NbDim}) max(Coord{NbDim}) min(Coord{NbDim})]-ObjectData.Coord(1,1);% corner absissa of the original grid with respect to the new origin |
---|
1522 | ycorner=[min(Coord{NbDim-1}) min(Coord{NbDim-1}) max(Coord{NbDim-1}) max(Coord{NbDim-1})]-ObjectData.Coord(1,2);% corner ordinates of the original grid |
---|
1523 | xcor_new=xcorner*cos(PlaneAngle(3))+ycorner*sin(PlaneAngle(3));%coordinates of the corners in new frame |
---|
1524 | ycor_new=-xcorner*sin(PlaneAngle(3))+ycorner*cos(PlaneAngle(3)); |
---|
1525 | if ~testXMin |
---|
1526 | XMin=min(xcor_new); |
---|
1527 | end |
---|
1528 | if ~testXMax |
---|
1529 | XMax=max(xcor_new); |
---|
1530 | end |
---|
1531 | if ~testYMin |
---|
1532 | YMin=min(ycor_new); |
---|
1533 | end |
---|
1534 | if ~testYMax |
---|
1535 | YMax=max(ycor_new); |
---|
1536 | end |
---|
1537 | end |
---|
1538 | coord_x_proj=XMin:DX:XMax; |
---|
1539 | coord_y_proj=YMin:DY:YMax; |
---|
1540 | ProjData.(AYName)=[coord_y_proj(1) coord_y_proj(end)]; %record the new (projected ) y coordinates |
---|
1541 | ProjData.(AXName)=[coord_x_proj(1) coord_x_proj(end)]; %record the new (projected ) x coordinates |
---|
1542 | [X,YI]=meshgrid(coord_x_proj,coord_y_proj);%grid in the new coordinates |
---|
1543 | XI=ObjectData.Coord(1,1)+(X)*cos(PlaneAngle(3))-YI*sin(PlaneAngle(3));%corresponding coordinates in the original system |
---|
1544 | YI=ObjectData.Coord(1,2)+(X)*sin(PlaneAngle(3))+YI*cos(PlaneAngle(3)); |
---|
1545 | if numel(Coord{1})==2% x coordiante defiend by its bounds, get the whole set |
---|
1546 | Coord{1}=linspace(Coord{1}(1),Coord{1}(2),CellInfo{icell}.CoordSize(1)); |
---|
1547 | end |
---|
1548 | if numel(Coord{2})==2% y coordiante defiend by its bounds, get the whole set |
---|
1549 | Coord{2}=linspace(Coord{2}(1),Coord{2}(2),CellInfo{icell}.CoordSize(2)); |
---|
1550 | end |
---|
1551 | [X,Y]=meshgrid(Coord{2},Coord{1});%initial coordinates |
---|
1552 | %name of error flag variable |
---|
1553 | FFName='FF';%default name (if not already used) |
---|
1554 | if isfield(ProjData,'FF') |
---|
1555 | ind=1; |
---|
1556 | while isfield(ProjData,['FF_' num2str(ind)]) |
---|
1557 | ind=ind+1; |
---|
1558 | end |
---|
1559 | FFName=['FF_' num2str(ind)];% append an index to the name of error flag, FF_1,FF_2... |
---|
1560 | end |
---|
1561 | % project all variables in the cell |
---|
1562 | for ivar=VarIndex |
---|
1563 | VarName=FieldData.ListVarName{ivar}; |
---|
1564 | if size(FieldData.(VarName),3)==1 |
---|
1565 | ProjData.(VarName)=interp2(X,Y,double(FieldData.(VarName)),XI,YI,'*linear');%interpolation fct |
---|
1566 | else |
---|
1567 | ProjData.(VarName)=interp2(X,Y,double(FieldData.(VarName)(:,:,1)),XI,YI,'*linear'); |
---|
1568 | for icolor=2:size(FieldData.(VarName),3)% project 'color' components |
---|
1569 | ProjData.(VarName)=cat(3,ProjData.(VarName),interp2(X,Y,double(FieldData.(VarName)(:,:,icolor)),XI,YI,'*linear')); |
---|
1570 | end |
---|
1571 | end |
---|
1572 | if isa(FieldData.(VarName),'uint8') |
---|
1573 | ProjData.(VarName)=uint8(ProjData.(VarName));%put result to integer 8 bits if the initial field is integer (image) |
---|
1574 | elseif isa(FieldData.(VarName),'uint16') |
---|
1575 | ProjData.(VarName)=uint16(ProjData.(VarName));%put result to integer 16 bits if the initial field is integer (image) |
---|
1576 | end |
---|
1577 | ListVarName=[ListVarName VarName]; |
---|
1578 | DimCell(1:2)={AYName,AXName}; |
---|
1579 | VarDimName=[VarDimName {DimCell}]; |
---|
1580 | if isfield(FieldData,'VarAttribute')&&length(FieldData.VarAttribute)>=ivar |
---|
1581 | VarAttribute{length(ListVarName)+nbcoord}=FieldData.VarAttribute{ivar}; |
---|
1582 | end; |
---|
1583 | ProjData.(FFName)=isnan(ProjData.(VarName));%detact NaN (points outside the interpolation range) |
---|
1584 | ProjData.(VarName)(ProjData.(FFName))=0; %set to 0 the NaN data |
---|
1585 | end |
---|
1586 | %update list of variables with error flag |
---|
1587 | ListVarName=[ListVarName FFName]; |
---|
1588 | VarDimName=[VarDimName {DimCell}]; |
---|
1589 | VarAttribute{numel(ListVarName)}.Role='errorflag'; |
---|
1590 | elseif ~testangle |
---|
1591 | % unstructured z coordinate |
---|
1592 | test_sup=(Coord{1}>=ObjectData.Coord(1,3)); |
---|
1593 | iz_sup=find(test_sup); |
---|
1594 | iz=iz_sup(1); |
---|
1595 | if iz>=1 & iz<=npz |
---|
1596 | %ProjData.ListDimName=[ProjData.ListDimName ListDimName(2:end)]; |
---|
1597 | %ProjData.DimValue=[ProjData.DimValue npY npX]; |
---|
1598 | for ivar=VarIndex |
---|
1599 | VarName=FieldData.ListVarName{ivar}; |
---|
1600 | ListVarName=[ListVarName VarName]; |
---|
1601 | VarAttribute{length(ListVarName)}=FieldData.VarAttribute{ivar}; %reproduce the variable attributes |
---|
1602 | ProjData.(VarName)=squeeze(FieldData.(VarName)(iz,:,:));% select the z index iz |
---|
1603 | %TODO : do a vertical average for a thick plane |
---|
1604 | if test_interp(2) || test_interp(3) |
---|
1605 | ProjData.(VarName)=interp2(Coord{3},Coord{2},ProjData.(VarName),Coord_x,Coord_y); |
---|
1606 | end |
---|
1607 | end |
---|
1608 | end |
---|
1609 | else |
---|
1610 | errormsg='projection of structured coordinates on oblique plane not yet implemented'; |
---|
1611 | %TODO: use interp_lin3 |
---|
1612 | return |
---|
1613 | end |
---|
1614 | end |
---|
1615 | end |
---|
1616 | % update the global list of projected variables: |
---|
1617 | ProjData.ListVarName=[ProjData.ListVarName ListVarName]; |
---|
1618 | ProjData.VarDimName=[ProjData.VarDimName VarDimName]; |
---|
1619 | ProjData.VarAttribute=[ProjData.VarAttribute VarAttribute]; |
---|
1620 | |
---|
1621 | %% projection of velocity components in the rotated coordinates |
---|
1622 | if testangle |
---|
1623 | ivar_U=[];ivar_V=[];ivar_W=[]; |
---|
1624 | for ivar=1:numel(VarAttribute) |
---|
1625 | if isfield(VarAttribute{ivar},'Role') |
---|
1626 | if strcmp(VarAttribute{ivar}.Role,'vector_x') |
---|
1627 | ivar_U=ivar; |
---|
1628 | elseif strcmp(VarAttribute{ivar}.Role,'vector_y') |
---|
1629 | ivar_V=ivar; |
---|
1630 | elseif strcmp(VarAttribute{ivar}.Role,'vector_z') |
---|
1631 | ivar_W=ivar; |
---|
1632 | end |
---|
1633 | end |
---|
1634 | end |
---|
1635 | if ~isempty(ivar_U) |
---|
1636 | if isempty(ivar_V) |
---|
1637 | msgbox_uvmat('ERROR','v velocity component missing in proj_field.m') |
---|
1638 | return |
---|
1639 | else |
---|
1640 | UName=ListVarName{ivar_U}; |
---|
1641 | VName=ListVarName{ivar_V}; |
---|
1642 | ProjData.(UName)=cos(PlaneAngle(3))*ProjData.(UName)+ sin(PlaneAngle(3))*ProjData.(VName); |
---|
1643 | ProjData.(VName)=(-sin(PlaneAngle(3))*ProjData.(UName)+ cos(PlaneAngle(3))*ProjData.(VName)); |
---|
1644 | if ~isempty(ivar_W) |
---|
1645 | WName=FieldData.ListVarName{ivar_W}; |
---|
1646 | ProjData.(VName)=ProjData.(VName)+ ProjData.(WName)*sin(Theta);% |
---|
1647 | ProjData.(WName)=NormVec_X*ProjData.(UName)+ NormVec_Y*ProjData.(VName)+ NormVec_Z* ProjData.(WName); |
---|
1648 | end |
---|
1649 | end |
---|
1650 | end |
---|
1651 | end |
---|
1652 | end |
---|
1653 | % %prepare substraction in case of two input fields |
---|
1654 | % SubData.ListVarName={}; |
---|
1655 | % SubData.VarDimName={}; |
---|
1656 | % SubData.VarAttribute={}; |
---|
1657 | % check_remove=zeros(size(ProjData.ListVarName)); |
---|
1658 | % for iproj=1:numel(ProjData.VarAttribute) |
---|
1659 | % if isfield(ProjData.VarAttribute{iproj},'CheckSub')&&isequal(ProjData.VarAttribute{iproj}.CheckSub,1) |
---|
1660 | % VarName=ProjData.ListVarName{iproj}; |
---|
1661 | % SubData.ListVarName=[SubData.ListVarName {VarName}]; |
---|
1662 | % SubData.VarDimName=[SubData.VarDimName ProjData.VarDimName{iproj}]; |
---|
1663 | % SubData.VarAttribute=[SubData.VarAttribute ProjData.VarAttribute{iproj}]; |
---|
1664 | % SubData.(VarName)=ProjData.(VarName); |
---|
1665 | % check_remove(iproj)=1; |
---|
1666 | % end |
---|
1667 | % end |
---|
1668 | % if ~isempty(find(check_remove)) |
---|
1669 | % ind_remove=find(check_remove); |
---|
1670 | % ProjData.ListVarName(ind_remove)=[]; |
---|
1671 | % ProjData.VarDimName(ind_remove)=[]; |
---|
1672 | % ProjData.VarAttribute(ind_remove)=[]; |
---|
1673 | % ProjData=sub_field(ProjData,[],SubData); |
---|
1674 | % end |
---|
1675 | |
---|
1676 | %----------------------------------------------------------------- |
---|
1677 | %projection in a volume |
---|
1678 | function [ProjData,errormsg] = proj_volume(FieldData, ObjectData) |
---|
1679 | |
---|
1680 | %----------------------------------------------------------------- |
---|
1681 | ProjData=FieldData;%default output |
---|
1682 | |
---|
1683 | %% axis origin |
---|
1684 | if isempty(ObjectData.Coord) |
---|
1685 | ObjectData.Coord(1,1)=0;%origin of the plane set to [0 0] by default |
---|
1686 | ObjectData.Coord(1,2)=0; |
---|
1687 | ObjectData.Coord(1,3)=0; |
---|
1688 | end |
---|
1689 | |
---|
1690 | %% rotation angles |
---|
1691 | VolumeAngle=[0 0 0]; |
---|
1692 | norm_plane=[0 0 1]; |
---|
1693 | if isfield(ObjectData,'Angle')&& isequal(size(ObjectData.Angle),[1 3])&& ~isequal(ObjectData.Angle,[0 0 0]) |
---|
1694 | PlaneAngle=ObjectData.Angle; |
---|
1695 | VolumeAngle=ObjectData.Angle; |
---|
1696 | om=norm(VolumeAngle);%norm of rotation angle in radians |
---|
1697 | OmAxis=VolumeAngle/om; %unit vector marking the rotation axis |
---|
1698 | cos_om=cos(pi*om/180); |
---|
1699 | sin_om=sin(pi*om/180); |
---|
1700 | coeff=OmAxis(3)*(1-cos_om); |
---|
1701 | %components of the unity vector norm_plane normal to the projection plane |
---|
1702 | norm_plane(1)=OmAxis(1)*coeff+OmAxis(2)*sin_om; |
---|
1703 | norm_plane(2)=OmAxis(2)*coeff-OmAxis(1)*sin_om; |
---|
1704 | norm_plane(3)=OmAxis(3)*coeff+cos_om; |
---|
1705 | end |
---|
1706 | testangle=~isequal(VolumeAngle,[0 0 0]); |
---|
1707 | |
---|
1708 | %% mesh sizes DX, DY, DZ |
---|
1709 | DX=0; |
---|
1710 | DY=0; %default |
---|
1711 | DZ=0; |
---|
1712 | if isfield(ObjectData,'DX')&~isempty(ObjectData.DX) |
---|
1713 | DX=abs(ObjectData.DX);%mesh of interpolation points |
---|
1714 | end |
---|
1715 | if isfield(ObjectData,'DY')&~isempty(ObjectData.DY) |
---|
1716 | DY=abs(ObjectData.DY);%mesh of interpolation points |
---|
1717 | end |
---|
1718 | if isfield(ObjectData,'DZ')&~isempty(ObjectData.DZ) |
---|
1719 | DZ=abs(ObjectData.DZ);%mesh of interpolation points |
---|
1720 | end |
---|
1721 | if ~strcmp(ProjMode,'projection') && (DX==0||DY==0||DZ==0) |
---|
1722 | errormsg='grid mesh DX , DY or DZ is missing'; |
---|
1723 | return |
---|
1724 | end |
---|
1725 | |
---|
1726 | %% extrema along each axis |
---|
1727 | testXMin=0; |
---|
1728 | testXMax=0; |
---|
1729 | testYMin=0; |
---|
1730 | testYMax=0; |
---|
1731 | if isfield(ObjectData,'RangeX') |
---|
1732 | XMin=min(ObjectData.RangeX); |
---|
1733 | XMax=max(ObjectData.RangeX); |
---|
1734 | testXMin=XMax>XMin; |
---|
1735 | testXMax=1; |
---|
1736 | end |
---|
1737 | if isfield(ObjectData,'RangeY') |
---|
1738 | YMin=min(ObjectData.RangeY); |
---|
1739 | YMax=max(ObjectData.RangeY); |
---|
1740 | testYMin=YMax>YMin; |
---|
1741 | testYMax=1; |
---|
1742 | end |
---|
1743 | width=0;%default width of the projection band |
---|
1744 | if isfield(ObjectData,'RangeZ') |
---|
1745 | ZMin=min(ObjectData.RangeZ); |
---|
1746 | ZMax=max(ObjectData.RangeZ); |
---|
1747 | testZMin=ZMax>ZMin; |
---|
1748 | testZMax=1; |
---|
1749 | end |
---|
1750 | |
---|
1751 | %% initiate Matlab structure for physical field |
---|
1752 | [ProjData,errormsg]=proj_heading(FieldData,ObjectData); |
---|
1753 | if ~isempty(errormsg) |
---|
1754 | return |
---|
1755 | end |
---|
1756 | |
---|
1757 | ProjData.NbDim=3; |
---|
1758 | ProjData.ListVarName={}; |
---|
1759 | ProjData.VarDimName={}; |
---|
1760 | if ~isequal(DX,0)&& ~isequal(DY,0) |
---|
1761 | ProjData.CoordMesh=sqrt(DX*DY);%define typical data mesh, useful for mouse selection in plots |
---|
1762 | elseif isfield(FieldData,'CoordMesh') |
---|
1763 | ProjData.CoordMesh=FieldData.CoordMesh; |
---|
1764 | end |
---|
1765 | |
---|
1766 | error=0;%default |
---|
1767 | flux=0; |
---|
1768 | testfalse=0; |
---|
1769 | ListIndex={}; |
---|
1770 | |
---|
1771 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
1772 | %% group the variables (fields of 'FieldData') in cells of variables with the same dimensions |
---|
1773 | %----------------------------------------------------------------- |
---|
1774 | idimvar=0; |
---|
1775 | % LOOP ON GROUPS OF VARIABLES SHARING THE SAME DIMENSIONS |
---|
1776 | % CellVarIndex=cells of variable index arrays |
---|
1777 | ivar_new=0; % index of the current variable in the projected field |
---|
1778 | icoord=0; |
---|
1779 | nbcoord=0;%number of added coordinate variables brought by projection |
---|
1780 | nbvar=0; |
---|
1781 | for icell=1:length(CellVarIndex) |
---|
1782 | NbDim=NbDimVec(icell); |
---|
1783 | if NbDim<3 |
---|
1784 | continue |
---|
1785 | end |
---|
1786 | VarIndex=CellVarIndex{icell};% indices of the selected variables in the list FieldData.ListVarName |
---|
1787 | VarType=VarTypeCell{icell}; |
---|
1788 | ivar_X=VarType.coord_x; |
---|
1789 | ivar_Y=VarType.coord_y; |
---|
1790 | ivar_Z=VarType.coord_z; |
---|
1791 | ivar_U=VarType.vector_x; |
---|
1792 | ivar_V=VarType.vector_y; |
---|
1793 | ivar_W=VarType.vector_z; |
---|
1794 | ivar_C=VarType.scalar ; |
---|
1795 | ivar_Anc=VarType.ancillary; |
---|
1796 | test_anc=zeros(size(VarIndex)); |
---|
1797 | test_anc(ivar_Anc)=ones(size(ivar_Anc)); |
---|
1798 | ivar_F=VarType.warnflag; |
---|
1799 | ivar_FF=VarType.errorflag; |
---|
1800 | check_unstructured_coord=~isempty(ivar_X) && ~isempty(ivar_Y); |
---|
1801 | DimCell=FieldData.VarDimName{VarIndex(1)}; |
---|
1802 | if ischar(DimCell) |
---|
1803 | DimCell={DimCell};%name of dimensions |
---|
1804 | end |
---|
1805 | |
---|
1806 | %% case of input fields with unstructured coordinates |
---|
1807 | if check_unstructured_coord |
---|
1808 | XName=FieldData.ListVarName{ivar_X}; |
---|
1809 | YName=FieldData.ListVarName{ivar_Y}; |
---|
1810 | eval(['coord_x=FieldData.' XName ';']) |
---|
1811 | eval(['coord_y=FieldData.' YName ';']) |
---|
1812 | if length(ivar_Z)==1 |
---|
1813 | ZName=FieldData.ListVarName{ivar_Z}; |
---|
1814 | eval(['coord_z=FieldData.' ZName ';']) |
---|
1815 | end |
---|
1816 | |
---|
1817 | % translate initial coordinates |
---|
1818 | coord_x=coord_x-ObjectData.Coord(1,1); |
---|
1819 | coord_y=coord_y-ObjectData.Coord(1,2); |
---|
1820 | if ~isempty(ivar_Z) |
---|
1821 | coord_z=coord_z-ObjectData.Coord(1,3); |
---|
1822 | end |
---|
1823 | |
---|
1824 | % selection of the vectors in the projection range |
---|
1825 | % if length(ivar_Z)==1 && width > 0 |
---|
1826 | % %components of the unitiy vector normal to the projection plane |
---|
1827 | % fieldZ=NormVec_X*coord_x + NormVec_Y*coord_y+ NormVec_Z*coord_z;% distance to the plane |
---|
1828 | % indcut=find(abs(fieldZ) <= width); |
---|
1829 | % for ivar=VarIndex |
---|
1830 | % VarName=FieldData.ListVarName{ivar}; |
---|
1831 | % eval(['FieldData.' VarName '=FieldData.' VarName '(indcut);']) |
---|
1832 | % % A VOIR : CAS DE VAR STRUCTUREE MAIS PAS GRILLE REGULIERE : INTERPOLER SUR GRILLE REGULIERE |
---|
1833 | % end |
---|
1834 | % coord_x=coord_x(indcut); |
---|
1835 | % coord_y=coord_y(indcut); |
---|
1836 | % coord_z=coord_z(indcut); |
---|
1837 | % end |
---|
1838 | |
---|
1839 | %rotate coordinates if needed: TODO modify |
---|
1840 | if testangle |
---|
1841 | coord_X=(coord_x *cos(Phi) + coord_y* sin(Phi)); |
---|
1842 | coord_Y=(-coord_x *sin(Phi) + coord_y *cos(Phi))*cos(Theta); |
---|
1843 | if ~isempty(ivar_Z) |
---|
1844 | coord_Y=coord_Y+coord_z *sin(Theta); |
---|
1845 | end |
---|
1846 | |
---|
1847 | coord_X=(coord_X *cos(Psi) - coord_Y* sin(Psi));%A VERIFIER |
---|
1848 | coord_Y=(coord_X *sin(Psi) + coord_Y* cos(Psi)); |
---|
1849 | |
---|
1850 | else |
---|
1851 | coord_X=coord_x; |
---|
1852 | coord_Y=coord_y; |
---|
1853 | coord_Z=coord_z; |
---|
1854 | end |
---|
1855 | %restriction to the range of x and y if imposed |
---|
1856 | testin=ones(size(coord_X)); %default |
---|
1857 | testbound=0; |
---|
1858 | if testXMin |
---|
1859 | testin=testin & (coord_X >= XMin); |
---|
1860 | testbound=1; |
---|
1861 | end |
---|
1862 | if testXMax |
---|
1863 | testin=testin & (coord_X <= XMax); |
---|
1864 | testbound=1; |
---|
1865 | end |
---|
1866 | if testYMin |
---|
1867 | testin=testin & (coord_Y >= YMin); |
---|
1868 | testbound=1; |
---|
1869 | end |
---|
1870 | if testYMax |
---|
1871 | testin=testin & (coord_Y <= YMax); |
---|
1872 | testbound=1; |
---|
1873 | end |
---|
1874 | if testbound |
---|
1875 | indcut=find(testin); |
---|
1876 | for ivar=VarIndex |
---|
1877 | VarName=FieldData.ListVarName{ivar}; |
---|
1878 | eval(['FieldData.' VarName '=FieldData.' VarName '(indcut);']) |
---|
1879 | end |
---|
1880 | coord_X=coord_X(indcut); |
---|
1881 | coord_Y=coord_Y(indcut); |
---|
1882 | if length(ivar_Z)==1 |
---|
1883 | coord_Z=coord_Z(indcut); |
---|
1884 | end |
---|
1885 | end |
---|
1886 | % different cases of projection |
---|
1887 | if isequal(ObjectData.ProjMode,'projection')%%%%%%% NOT USED %%%%%%%%%% |
---|
1888 | for ivar=VarIndex %transfer variables to the projection plane |
---|
1889 | VarName=FieldData.ListVarName{ivar}; |
---|
1890 | if ivar==ivar_X %x coordinate |
---|
1891 | eval(['ProjData.' VarName '=coord_X;']) |
---|
1892 | elseif ivar==ivar_Y % y coordinate |
---|
1893 | eval(['ProjData.' VarName '=coord_Y;']) |
---|
1894 | elseif isempty(ivar_Z) || ivar~=ivar_Z % other variables (except Z coordinate wyhich is not reproduced) |
---|
1895 | eval(['ProjData.' VarName '=FieldData.' VarName ';']) |
---|
1896 | end |
---|
1897 | if isempty(ivar_Z) || ivar~=ivar_Z |
---|
1898 | ProjData.ListVarName=[ProjData.ListVarName VarName]; |
---|
1899 | ProjData.VarDimName=[ProjData.VarDimName DimCell]; |
---|
1900 | nbvar=nbvar+1; |
---|
1901 | if isfield(FieldData,'VarAttribute') && length(FieldData.VarAttribute) >=ivar |
---|
1902 | ProjData.VarAttribute{nbvar}=FieldData.VarAttribute{ivar}; |
---|
1903 | end |
---|
1904 | end |
---|
1905 | end |
---|
1906 | elseif isequal(ObjectData.ProjMode,'interp_lin')||isequal(ObjectData.ProjMode,'interp_tps')%interpolate data on a regular grid |
---|
1907 | coord_x_proj=XMin:DX:XMax; |
---|
1908 | coord_y_proj=YMin:DY:YMax; |
---|
1909 | coord_z_proj=ZMin:DZ:ZMax; |
---|
1910 | DimCell={'coord_z','coord_y','coord_x'}; |
---|
1911 | ProjData.ListVarName={'coord_z','coord_y','coord_x'}; |
---|
1912 | ProjData.VarDimName={'coord_z','coord_y','coord_x'}; |
---|
1913 | nbcoord=2; |
---|
1914 | ProjData.coord_z=[ZMin ZMax]; |
---|
1915 | ProjData.coord_y=[YMin YMax]; |
---|
1916 | ProjData.coord_x=[XMin XMax]; |
---|
1917 | if isempty(ivar_X), ivar_X=0; end; |
---|
1918 | if isempty(ivar_Y), ivar_Y=0; end; |
---|
1919 | if isempty(ivar_Z), ivar_Z=0; end; |
---|
1920 | if isempty(ivar_U), ivar_U=0; end; |
---|
1921 | if isempty(ivar_V), ivar_V=0; end; |
---|
1922 | if isempty(ivar_W), ivar_W=0; end; |
---|
1923 | if isempty(ivar_F), ivar_F=0; end; |
---|
1924 | if isempty(ivar_FF), ivar_FF=0; end; |
---|
1925 | if ~isequal(ivar_FF,0) |
---|
1926 | VarName_FF=FieldData.ListVarName{ivar_FF}; |
---|
1927 | eval(['indsel=find(FieldData.' VarName_FF '==0);']) |
---|
1928 | coord_X=coord_X(indsel); |
---|
1929 | coord_Y=coord_Y(indsel); |
---|
1930 | end |
---|
1931 | FF=zeros(1,length(coord_y_proj)*length(coord_x_proj)); |
---|
1932 | testFF=0; |
---|
1933 | [X,Y,Z]=meshgrid(coord_y_proj,coord_z_proj,coord_x_proj);%grid in the new coordinates |
---|
1934 | for ivar=VarIndex |
---|
1935 | VarName=FieldData.ListVarName{ivar}; |
---|
1936 | if ~( ivar==ivar_X || ivar==ivar_Y || ivar==ivar_Z || ivar==ivar_F || ivar==ivar_FF || test_anc(ivar)==1) |
---|
1937 | ivar_new=ivar_new+1; |
---|
1938 | ProjData.ListVarName=[ProjData.ListVarName {VarName}]; |
---|
1939 | ProjData.VarDimName=[ProjData.VarDimName {DimCell}]; |
---|
1940 | if isfield(FieldData,'VarAttribute') && length(FieldData.VarAttribute) >=ivar |
---|
1941 | ProjData.VarAttribute{ivar_new+nbcoord}=FieldData.VarAttribute{ivar}; |
---|
1942 | end |
---|
1943 | if ~isequal(ivar_FF,0) |
---|
1944 | eval(['FieldData.' VarName '=FieldData.' VarName '(indsel);']) |
---|
1945 | end |
---|
1946 | % linear interpolation |
---|
1947 | InterpFct=TriScatteredInterp(double(coord_X),double(coord_Y),double(coord_Z),double(FieldData.(VarName))); |
---|
1948 | ProjData.(VarName)=InterpFct(X,Y,Z); |
---|
1949 | % eval(['varline=reshape(ProjData.' VarName ',1,length(coord_y_proj)*length(coord_x_proj));']) |
---|
1950 | % FFlag= isnan(varline); %detect undefined values NaN |
---|
1951 | % indnan=find(FFlag); |
---|
1952 | % if ~isempty(indnan) |
---|
1953 | % varline(indnan)=zeros(size(indnan)); |
---|
1954 | % eval(['ProjData.' VarName '=reshape(varline,length(coord_y_proj),length(coord_x_proj));']) |
---|
1955 | % FF(indnan)=ones(size(indnan)); |
---|
1956 | % testFF=1; |
---|
1957 | % end |
---|
1958 | if ivar==ivar_U |
---|
1959 | ivar_U=ivar_new; |
---|
1960 | end |
---|
1961 | if ivar==ivar_V |
---|
1962 | ivar_V=ivar_new; |
---|
1963 | end |
---|
1964 | if ivar==ivar_W |
---|
1965 | ivar_W=ivar_new; |
---|
1966 | end |
---|
1967 | end |
---|
1968 | end |
---|
1969 | if testFF |
---|
1970 | ProjData.FF=reshape(FF,length(coord_y_proj),length(coord_x_proj)); |
---|
1971 | ProjData.ListVarName=[ProjData.ListVarName {'FF'}]; |
---|
1972 | ProjData.VarDimName=[ProjData.VarDimName {DimCell}]; |
---|
1973 | ProjData.VarAttribute{ivar_new+1+nbcoord}.Role='errorflag'; |
---|
1974 | end |
---|
1975 | end |
---|
1976 | |
---|
1977 | %% case of input fields defined on a structured grid |
---|
1978 | else |
---|
1979 | VarName=FieldData.ListVarName{VarIndex(1)};%get the first variable of the cell to get the input matrix dimensions |
---|
1980 | eval(['DimValue=size(FieldData.' VarName ');'])%input matrix dimensions |
---|
1981 | DimValue(DimValue==1)=[];%remove singleton dimensions |
---|
1982 | NbDim=numel(DimValue);%update number of space dimensions |
---|
1983 | nbcolor=1; %default number of 'color' components: third matrix index without corresponding coordinate |
---|
1984 | if NbDim>=3 |
---|
1985 | if NbDim>3 |
---|
1986 | errormsg='matrices with more than 3 dimensions not handled'; |
---|
1987 | return |
---|
1988 | else |
---|
1989 | if numel(find(VarType.coord))==2% the third matrix dimension does not correspond to a space coordinate |
---|
1990 | nbcolor=DimValue(3); |
---|
1991 | DimValue(3)=[]; %number of 'color' components updated |
---|
1992 | NbDim=2;% space dimension set to 2 |
---|
1993 | end |
---|
1994 | end |
---|
1995 | end |
---|
1996 | AYName=FieldData.ListVarName{VarType.coord(NbDim-1)};%name of input x coordinate (name preserved on projection) |
---|
1997 | AXName=FieldData.ListVarName{VarType.coord(NbDim)};%name of input y coordinate (name preserved on projection) |
---|
1998 | eval(['AX=FieldData.' AXName ';']) |
---|
1999 | eval(['AY=FieldData.' AYName ';']) |
---|
2000 | ListDimName=FieldData.VarDimName{VarIndex(1)}; |
---|
2001 | ProjData.ListVarName=[ProjData.ListVarName {AYName} {AXName}]; %TODO: check if it already exists in Projdata (several cells) |
---|
2002 | ProjData.VarDimName=[ProjData.VarDimName {AYName} {AXName}]; |
---|
2003 | |
---|
2004 | % for idim=1:length(ListDimName) |
---|
2005 | % DimName=ListDimName{idim}; |
---|
2006 | % if strcmp(DimName,'rgb')||strcmp(DimName,'nb_coord')||strcmp(DimName,'nb_coord_i') |
---|
2007 | % nbcolor=DimValue(idim); |
---|
2008 | % DimValue(idim)=[]; |
---|
2009 | % end |
---|
2010 | % if isequal(DimName,'nb_coord_j')% NOTE: CASE OF TENSOR NOT TREATED |
---|
2011 | % DimValue(idim)=[]; |
---|
2012 | % end |
---|
2013 | % end |
---|
2014 | Coord_z=[]; |
---|
2015 | Coord_y=[]; |
---|
2016 | Coord_x=[]; |
---|
2017 | |
---|
2018 | for idim=1:NbDim %loop on space dimensions |
---|
2019 | test_interp(idim)=0;%test for coordiate interpolation (non regular grid), =0 by default |
---|
2020 | ivar=VarType.coord(idim);% index of the variable corresponding to the current dimension |
---|
2021 | if ~isequal(ivar,0)% a variable corresponds to the dimension #idim |
---|
2022 | eval(['Coord{idim}=FieldData.' FieldData.ListVarName{ivar} ';']) ;% coord values for the input field |
---|
2023 | if numel(Coord{idim})==2 %input array defined on a regular grid |
---|
2024 | DCoord_min(idim)=(Coord{idim}(2)-Coord{idim}(1))/DimValue(idim); |
---|
2025 | else |
---|
2026 | DCoord=diff(Coord{idim});%array of coordinate derivatives for the input field |
---|
2027 | DCoord_min(idim)=min(DCoord); |
---|
2028 | DCoord_max=max(DCoord); |
---|
2029 | % test_direct(idim)=DCoord_max>0;% =1 for increasing values, 0 otherwise |
---|
2030 | if abs(DCoord_max-DCoord_min(idim))>abs(DCoord_max/1000) |
---|
2031 | msgbox_uvmat('ERROR',['non monotonic dimension variable # ' num2str(idim) ' in proj_field.m']) |
---|
2032 | return |
---|
2033 | end |
---|
2034 | test_interp(idim)=(DCoord_max-DCoord_min(idim))> 0.0001*abs(DCoord_max);% test grid regularity |
---|
2035 | end |
---|
2036 | test_direct(idim)=(DCoord_min(idim)>0); |
---|
2037 | else % no variable associated with the dimension #idim, the coordinate value is set equal to the matrix index by default |
---|
2038 | Coord_i_str=['Coord_' num2str(idim)]; |
---|
2039 | DCoord_min(idim)=1;%default |
---|
2040 | Coord{idim}=[0.5 DimValue(idim)-0.5]; |
---|
2041 | test_direct(idim)=1; |
---|
2042 | end |
---|
2043 | end |
---|
2044 | if DY==0 |
---|
2045 | DY=abs(DCoord_min(NbDim-1)); |
---|
2046 | end |
---|
2047 | npY=1+round(abs(Coord{NbDim-1}(end)-Coord{NbDim-1}(1))/DY);%nbre of points after interpol |
---|
2048 | if DX==0 |
---|
2049 | DX=abs(DCoord_min(NbDim)); |
---|
2050 | end |
---|
2051 | npX=1+round(abs(Coord{NbDim}(end)-Coord{NbDim}(1))/DX);%nbre of points after interpol |
---|
2052 | for idim=1:NbDim |
---|
2053 | if test_interp(idim) |
---|
2054 | DimValue(idim)=1+round(abs(Coord{idim}(end)-Coord{idim}(1))/abs(DCoord_min(idim)));%nbre of points after possible interpolation on a regular gri |
---|
2055 | end |
---|
2056 | end |
---|
2057 | Coord_y=linspace(Coord{NbDim-1}(1),Coord{NbDim-1}(end),npY); |
---|
2058 | test_direct_y=test_direct(NbDim-1); |
---|
2059 | Coord_x=linspace(Coord{NbDim}(1),Coord{NbDim}(end),npX); |
---|
2060 | test_direct_x=test_direct(NbDim); |
---|
2061 | DAX=DCoord_min(NbDim); |
---|
2062 | DAY=DCoord_min(NbDim-1); |
---|
2063 | minAX=min(Coord_x); |
---|
2064 | maxAX=max(Coord_x); |
---|
2065 | minAY=min(Coord_y); |
---|
2066 | maxAY=max(Coord_y); |
---|
2067 | xcorner=[minAX maxAX minAX maxAX]-ObjectData.Coord(1,1); |
---|
2068 | ycorner=[maxAY maxAY minAY minAY]-ObjectData.Coord(1,2); |
---|
2069 | xcor_new=xcorner*cos(Phi)+ycorner*sin(Phi);%coord new frame |
---|
2070 | ycor_new=-xcorner*sin(Phi)+ycorner*cos(Phi); |
---|
2071 | if ~testXMax |
---|
2072 | XMax=max(xcor_new); |
---|
2073 | end |
---|
2074 | if ~testXMin |
---|
2075 | XMin=min(xcor_new); |
---|
2076 | end |
---|
2077 | if ~testYMax |
---|
2078 | YMax=max(ycor_new); |
---|
2079 | end |
---|
2080 | if ~testYMin |
---|
2081 | YMin=min(ycor_new); |
---|
2082 | end |
---|
2083 | DXinit=(maxAX-minAX)/(DimValue(NbDim)-1); |
---|
2084 | DYinit=(maxAY-minAY)/(DimValue(NbDim-1)-1); |
---|
2085 | if DX==0 |
---|
2086 | DX=DXinit; |
---|
2087 | end |
---|
2088 | if DY==0 |
---|
2089 | DY=DYinit; |
---|
2090 | end |
---|
2091 | if NbDim==3 |
---|
2092 | DZ=(Coord{1}(end)-Coord{1}(1))/(DimValue(1)-1); |
---|
2093 | if ~test_direct(1) |
---|
2094 | DZ=-DZ; |
---|
2095 | end |
---|
2096 | Coord_z=linspace(Coord{1}(1),Coord{1}(end),DimValue(1)); |
---|
2097 | test_direct_z=test_direct(1); |
---|
2098 | end |
---|
2099 | npX=floor((XMax-XMin)/DX+1); |
---|
2100 | npY=floor((YMax-YMin)/DY+1); |
---|
2101 | if test_direct_y |
---|
2102 | coord_y_proj=linspace(YMin,YMax,npY);%abscissa of the new pixels along the line |
---|
2103 | else |
---|
2104 | coord_y_proj=linspace(YMax,YMin,npY);%abscissa of the new pixels along the line |
---|
2105 | end |
---|
2106 | if test_direct_x |
---|
2107 | coord_x_proj=linspace(XMin,XMax,npX);%abscissa of the new pixels along the line |
---|
2108 | else |
---|
2109 | coord_x_proj=linspace(XMax,XMin,npX);%abscissa of the new pixels along the line |
---|
2110 | end |
---|
2111 | |
---|
2112 | % case with no rotation and interpolation |
---|
2113 | if isequal(ProjMode,'projection') && isequal(Phi,0) && isequal(Theta,0) && isequal(Psi,0) |
---|
2114 | if ~testXMin && ~testXMax && ~testYMin && ~testYMax && NbDim==2 |
---|
2115 | ProjData=FieldData; |
---|
2116 | else |
---|
2117 | indY=NbDim-1; |
---|
2118 | if test_direct(indY) |
---|
2119 | min_indy=ceil((YMin-Coord{indY}(1))/DYinit)+1; |
---|
2120 | YIndexFirst=floor((YMax-Coord{indY}(1))/DYinit)+1; |
---|
2121 | Ybound(1)=Coord{indY}(1)+DYinit*(min_indy-1); |
---|
2122 | Ybound(2)=Coord{indY}(1)+DYinit*(YIndexFirst-1); |
---|
2123 | else |
---|
2124 | min_indy=ceil((Coord{indY}(1)-YMax)/DYinit)+1; |
---|
2125 | max_indy=floor((Coord{indY}(1)-YMin)/DYinit)+1; |
---|
2126 | Ybound(2)=Coord{indY}(1)-DYinit*(max_indy-1); |
---|
2127 | Ybound(1)=Coord{indY}(1)-DYinit*(min_indy-1); |
---|
2128 | end |
---|
2129 | if test_direct(NbDim)==1 |
---|
2130 | min_indx=ceil((XMin-Coord{NbDim}(1))/DXinit)+1; |
---|
2131 | max_indx=floor((XMax-Coord{NbDim}(1))/DXinit)+1; |
---|
2132 | Xbound(1)=Coord{NbDim}(1)+DXinit*(min_indx-1); |
---|
2133 | Xbound(2)=Coord{NbDim}(1)+DXinit*(max_indx-1); |
---|
2134 | else |
---|
2135 | min_indx=ceil((Coord{NbDim}(1)-XMax)/DXinit)+1; |
---|
2136 | max_indx=floor((Coord{NbDim}(1)-XMin)/DXinit)+1; |
---|
2137 | Xbound(2)=Coord{NbDim}(1)+DXinit*(max_indx-1); |
---|
2138 | Xbound(1)=Coord{NbDim}(1)+DXinit*(min_indx-1); |
---|
2139 | end |
---|
2140 | if NbDim==3 |
---|
2141 | DimCell(1)=[]; %suppress z variable |
---|
2142 | DimValue(1)=[]; |
---|
2143 | %structured coordinates |
---|
2144 | if test_direct(1) |
---|
2145 | iz=ceil((ObjectData.Coord(1,3)-Coord{1}(1))/DZ)+1; |
---|
2146 | else |
---|
2147 | iz=ceil((Coord{1}(1)-ObjectData.Coord(1,3))/DZ)+1; |
---|
2148 | end |
---|
2149 | end |
---|
2150 | min_indy=max(min_indy,1);% deals with margin (bound lower than the first index) |
---|
2151 | min_indx=max(min_indx,1); |
---|
2152 | max_indy=min(max_indy,DimValue(1)); |
---|
2153 | max_indx=min(max_indx,DimValue(2)); |
---|
2154 | for ivar=VarIndex% loop on non coordinate variables |
---|
2155 | VarName=FieldData.ListVarName{ivar}; |
---|
2156 | ProjData.ListVarName=[ProjData.ListVarName VarName]; |
---|
2157 | ProjData.VarDimName=[ProjData.VarDimName {DimCell}]; |
---|
2158 | if isfield(FieldData,'VarAttribute') && length(FieldData.VarAttribute)>=ivar |
---|
2159 | ProjData.VarAttribute{length(ProjData.ListVarName)}=FieldData.VarAttribute{ivar}; |
---|
2160 | end |
---|
2161 | if NbDim==3 |
---|
2162 | eval(['ProjData.' VarName '=squeeze(FieldData.' VarName '(iz,min_indy:max_indy,min_indx:max_indx));']); |
---|
2163 | else |
---|
2164 | eval(['ProjData.' VarName '=FieldData.' VarName '(min_indy:max_indy,min_indx:max_indx,:);']); |
---|
2165 | end |
---|
2166 | end |
---|
2167 | eval(['ProjData.' AYName '=[Ybound(1) Ybound(2)];']) %record the new (projected ) y coordinates |
---|
2168 | eval(['ProjData.' AXName '=[Xbound(1) Xbound(2)];']) %record the new (projected ) x coordinates |
---|
2169 | end |
---|
2170 | elseif isfield(FieldData,'A') %TO GENERALISE % case with rotation and/or interpolation |
---|
2171 | if NbDim==2 %2D case |
---|
2172 | [X,Y]=meshgrid(coord_x_proj,coord_y_proj);%grid in the new coordinates |
---|
2173 | XIMA=ObjectData.Coord(1,1)+(X)*cos(Phi)-Y*sin(Phi);%corresponding coordinates in the original image |
---|
2174 | YIMA=ObjectData.Coord(1,2)+(X)*sin(Phi)+Y*cos(Phi); |
---|
2175 | XIMA=(XIMA-minAX)/DXinit+1;% image index along x |
---|
2176 | YIMA=(-YIMA+maxAY)/DYinit+1;% image index along y |
---|
2177 | XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line' |
---|
2178 | YIMA=reshape(round(YIMA),1,npX*npY); |
---|
2179 | flagin=XIMA>=1 & XIMA<=DimValue(2) & YIMA >=1 & YIMA<=DimValue(1);%flagin=1 inside the original image |
---|
2180 | if isequal(ObjectData.ProjMode,'interp_tps') |
---|
2181 | npx_interp_tps=ceil(abs(DX/DAX)); |
---|
2182 | npy_interp_tps=ceil(abs(DY/DAY)); |
---|
2183 | Minterp_tps=ones(npy_interp_tps,npx_interp_tps)/(npx_interp_tps*npy_interp_tps); |
---|
2184 | test_interp_tps=1; |
---|
2185 | else |
---|
2186 | test_interp_tps=0; |
---|
2187 | end |
---|
2188 | eval(['ProjData.' AYName '=[coord_y_proj(1) coord_y_proj(end)];']) %record the new (projected ) y coordinates |
---|
2189 | eval(['ProjData.' AXName '=[coord_x_proj(1) coord_x_proj(end)];']) %record the new (projected ) x coordinates |
---|
2190 | for ivar=VarIndex |
---|
2191 | VarName=FieldData.ListVarName{ivar}; |
---|
2192 | if test_interp(1) || test_interp(2)%interpolate on a regular grid |
---|
2193 | eval(['ProjData.' VarName '=interp2(Coord{2},Coord{1},FieldData.' VarName ',Coord_x,Coord_y'');']) %TO TEST |
---|
2194 | end |
---|
2195 | %filter the field (image) if option 'interp_tps' is used |
---|
2196 | if test_interp_tps |
---|
2197 | Aclass=class(FieldData.A); |
---|
2198 | ProjData.(VarName)=interp_tps2(Minterp_tps,FieldData.(VarName),'valid'); |
---|
2199 | if ~isequal(Aclass,'double') |
---|
2200 | ProjData.(VarName)=Aclass(FieldData.(VarName));%revert to integer values |
---|
2201 | end |
---|
2202 | end |
---|
2203 | eval(['vec_A=reshape(FieldData.' VarName ',[],nbcolor);'])%put the original image in line |
---|
2204 | %ind_in=find(flagin); |
---|
2205 | ind_out=find(~flagin); |
---|
2206 | ICOMB=(XIMA-1)*DimValue(1)+YIMA; |
---|
2207 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
2208 | vec_B(flagin,1:nbcolor)=vec_A(ICOMB,:); |
---|
2209 | for icolor=1:nbcolor |
---|
2210 | vec_B(ind_out,icolor)=zeros(size(ind_out)); |
---|
2211 | end |
---|
2212 | ProjData.ListVarName=[ProjData.ListVarName VarName]; |
---|
2213 | ProjData.VarDimName=[ProjData.VarDimName {DimCell}]; |
---|
2214 | if isfield(FieldData,'VarAttribute')&&length(FieldData.VarAttribute)>=ivar |
---|
2215 | ProjData.VarAttribute{length(ProjData.ListVarName)+nbcoord}=FieldData.VarAttribute{ivar}; |
---|
2216 | end |
---|
2217 | eval(['ProjData.' VarName '=reshape(vec_B,npY,npX,nbcolor);']); |
---|
2218 | end |
---|
2219 | ProjData.FF=reshape(~flagin,npY,npX);%false flag A FAIRE: tenir compte d'un flga antï¿œrieur |
---|
2220 | ProjData.ListVarName=[ProjData.ListVarName 'FF']; |
---|
2221 | ProjData.VarDimName=[ProjData.VarDimName {DimCell}]; |
---|
2222 | ProjData.VarAttribute{length(ProjData.ListVarName)}.Role='errorflag'; |
---|
2223 | else %3D case |
---|
2224 | if ~testangle |
---|
2225 | % unstructured z coordinate |
---|
2226 | test_sup=(Coord{1}>=ObjectData.Coord(1,3)); |
---|
2227 | iz_sup=find(test_sup); |
---|
2228 | iz=iz_sup(1); |
---|
2229 | if iz>=1 & iz<=npz |
---|
2230 | %ProjData.ListDimName=[ProjData.ListDimName ListDimName(2:end)]; |
---|
2231 | %ProjData.DimValue=[ProjData.DimValue npY npX]; |
---|
2232 | for ivar=VarIndex |
---|
2233 | VarName=FieldData.ListVarName{ivar}; |
---|
2234 | ProjData.ListVarName=[ProjData.ListVarName VarName]; |
---|
2235 | ProjData.VarAttribute{length(ProjData.ListVarName)}=FieldData.VarAttribute{ivar}; %reproduce the variable attributes |
---|
2236 | eval(['ProjData.' VarName '=squeeze(FieldData.' VarName '(iz,:,:));'])% select the z index iz |
---|
2237 | %TODO : do a vertical average for a thick plane |
---|
2238 | if test_interp(2) || test_interp(3) |
---|
2239 | eval(['ProjData.' VarName '=interp2(Coord{3},Coord{2},ProjData.' VarName ',Coord_x,Coord_y'');']) |
---|
2240 | end |
---|
2241 | end |
---|
2242 | end |
---|
2243 | else |
---|
2244 | errormsg='projection of structured coordinates on oblique plane not yet implemented'; |
---|
2245 | %TODO: use interp3 |
---|
2246 | return |
---|
2247 | end |
---|
2248 | end |
---|
2249 | end |
---|
2250 | end |
---|
2251 | |
---|
2252 | %% projection of velocity components in the rotated coordinates |
---|
2253 | if testangle |
---|
2254 | if isempty(ivar_V) |
---|
2255 | msgbox_uvmat('ERROR','v velocity component missing in proj_field.m') |
---|
2256 | return |
---|
2257 | end |
---|
2258 | UName=FieldData.ListVarName{ivar_U}; |
---|
2259 | VName=FieldData.ListVarName{ivar_V}; |
---|
2260 | eval(['ProjData.' UName '=cos(Phi)*ProjData.' UName '+ sin(Phi)*ProjData.' VName ';']) |
---|
2261 | eval(['ProjData.' VName '=cos(Theta)*(-sin(Phi)*ProjData.' UName '+ cos(Phi)*ProjData.' VName ');']) |
---|
2262 | if ~isempty(ivar_W) |
---|
2263 | WName=FieldData.ListVarName{ivar_W}; |
---|
2264 | eval(['ProjData.' VName '=ProjData.' VName '+ ProjData.' WName '*sin(Theta);'])% |
---|
2265 | eval(['ProjData.' WName '=NormVec_X*ProjData.' UName '+ NormVec_Y*ProjData.' VName '+ NormVec_Z* ProjData.' WName ';']); |
---|
2266 | end |
---|
2267 | if ~isequal(Psi,0) |
---|
2268 | eval(['ProjData.' UName '=cos(Psi)* ProjData.' UName '- sin(Psi)*ProjData.' VName ';']); |
---|
2269 | eval(['ProjData.' VName '=sin(Psi)* ProjData.' UName '+ cos(Psi)*ProjData.' VName ';']); |
---|
2270 | end |
---|
2271 | end |
---|
2272 | end |
---|
2273 | |
---|
2274 | %------------------------------------------------------------------------ |
---|
2275 | %--- transfer the global attributes |
---|
2276 | function [ProjData,errormsg]=proj_heading(FieldData,ObjectData) |
---|
2277 | %------------------------------------------------------------------------ |
---|
2278 | ProjData=[];%default |
---|
2279 | errormsg='';%default |
---|
2280 | |
---|
2281 | %% transfer error |
---|
2282 | if isfield(FieldData,'Txt') |
---|
2283 | errormsg=FieldData.Txt; %transmit erreur message |
---|
2284 | return; |
---|
2285 | end |
---|
2286 | |
---|
2287 | %% transfer global attributes |
---|
2288 | if ~isfield(FieldData,'ListGlobalAttribute') |
---|
2289 | ProjData.ListGlobalAttribute={}; |
---|
2290 | else |
---|
2291 | ProjData.ListGlobalAttribute=FieldData.ListGlobalAttribute; |
---|
2292 | end |
---|
2293 | for iattr=1:length(ProjData.ListGlobalAttribute) |
---|
2294 | AttrName=ProjData.ListGlobalAttribute{iattr}; |
---|
2295 | if isfield(FieldData,AttrName) |
---|
2296 | ProjData.(AttrName)=FieldData.(AttrName); |
---|
2297 | end |
---|
2298 | end |
---|
2299 | |
---|
2300 | %% transfer coordinate unit |
---|
2301 | if isfield(ProjData,'CoordUnit') |
---|
2302 | ProjData=rmfield(ProjData,'CoordUnit');% do not transfer by default (to avoid x/y=1 for profiles) |
---|
2303 | end |
---|
2304 | if isfield(FieldData,'CoordUnit') |
---|
2305 | if isfield(ObjectData,'CoordUnit') && ~strcmp(FieldData.CoordUnit,ObjectData.CoordUnit) |
---|
2306 | errormsg=[ObjectData.Type ' in ' ObjectData.CoordUnit ' coordinates, while field in ' FieldData.CoordUnit ]; |
---|
2307 | return |
---|
2308 | elseif strcmp(ObjectData.Type,'plane')|| strcmp(ObjectData.Type,'volume') |
---|
2309 | ProjData.CoordUnit=FieldData.CoordUnit; |
---|
2310 | end |
---|
2311 | end |
---|
2312 | |
---|
2313 | %% store the properties of the projection object |
---|
2314 | ListObject={'Name','Type','ProjMode','angle','RangeX','RangeY','RangeZ','DX','DY','DZ','Coord'}; |
---|
2315 | for ilist=1:length(ListObject) |
---|
2316 | if isfield(ObjectData,ListObject{ilist}) |
---|
2317 | val=ObjectData.(ListObject{ilist}); |
---|
2318 | if ~isempty(val) |
---|
2319 | ProjData.(['ProjObject' ListObject{ilist}])=val; |
---|
2320 | ProjData.ListGlobalAttribute=[ProjData.ListGlobalAttribute {['ProjObject' ListObject{ilist}]}]; |
---|
2321 | end |
---|
2322 | end |
---|
2323 | end |
---|
2324 | |
---|