source: trunk/src/proj_field.m @ 871

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

histo improved

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