source: trunk/src/proj_field.m @ 886

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

various bug fixes

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