source: trunk/src/proj_field.m @ 866

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

stereo_civ_updated

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