source: trunk/src/proj_field.m @ 644

Last change on this file since 644 was 644, checked in by sommeria, 11 years ago

various improvements: resize GUI uvmat, projection on lines

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