source: trunk/src/proj_field.m @ 878

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

civ improved at mask edge

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