source: trunk/src/proj_field.m @ 896

Last change on this file since 896 was 896, checked in by sommeria, 9 years ago

bug corrected in civ_series/patch

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