source: trunk/src/proj_field.m @ 646

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

various update, bugs to be expected

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