source: trunk/src/uvmat.m @ 84

Last change on this file since 84 was 84, checked in by sommeria, 14 years ago

-update_imadoc: copy the timing information of the movie file avi in the new xml file when a geometry_calibration is performed
-set_grid: modified to produce grids in px coordiantes and to display the produced grid
-geometry_calib: display the point with max error
-civ: bug corrected for copying avi movies to png files
-uvmat: small bug fixes
-px_XYZ: introduce default values for Calib parameters

File size: 208.6 KB
Line 
1%'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization
2%------------------------------------------------------------------------
3% function huvmat=uvmat(input)
4%
5%OUTPUT
6% huvmat=current handles of the GUI uvmat.fig
7%%
8%
9%INPUT:
10% input: input file name (if character chain), or input image matrix to
11% visualize, or Matlab structure representing  netcdf fields (with fields
12% ListVarName....)
13%
14%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
15%  Copyright Joel Sommeria,  2008, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
16%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
17%     This open is part of the toolbox UVMAT.
18%
19%     UVMAT is free software; you can redistribute it and/or modify
20%     it under the terms of the GNU General Public License as published by
21%     the Free Software Foundation; either version 2 of the License, or
22%     (at your option) any later version.
23%
24%     UVMAT is distributed in the hope that it will be useful,
25%     but WITHOUT ANY WARRANTY; without even the implied warranty of
26%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27%     GNU General Public License (open UVMAT/COPYING.txt) for more details.
28%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
29%
30% Information stored on the interface:
31%    'Strings' of all edit boxes and menus: get(handles.Tag,'String')
32%    'Values' of all menus and toggle buttons: get(handles.Tag,'Value')
33%     Matlab structure stored as 'UserData' of the figure uvmat.fig,(can be obtained by right mouse click on the interface).
34%          It contains the following fields:
35%     - Fixed specifiacation of plotting figures and axes (defined bu uvmat_OpeningFcn)
36%          .PosColorbar: [0.8210 0.4710 0.0190 0.4450]; specified position of the colorbar on figures
37%     - Information read in the documentation open of a series (activated by RootPath_Callback) :
38%          .XmlData, with fields:
39%               .Time: matrix of times of the images with index i and j
40%               .GeometryCalib: [1x1 struct]
41%     - Information defined from the interface:
42%           .NewSeries: =1 when the first view of a new field series is displayed, else 0
43%           .filename:(char string)
44%           .VelType:(char string) type of velocity field selected
45%           .VelType_1:(char string)  REMPLACER LE CELL ACTUEL
46%           .FieldName: (char string) main field selected('image', 'velocity'...)
47%           .FieldName_1:(char string) second field selected('image', 'velocity'...)
48%           .CName: (char string)name of the scalar used for vector colors
49%           .CoordType: (char string) coordinate transform: e.g. 'phys' or 'px'
50%           .MouseAction: store the current effect of mouse button (create or edit objects)
51%     - Information on  projection objects
52%           .Object: {[1x1 struct]}
53%           .CurrentObjectIndex: index of the projection object .Object currently selected for editing
54%     -Information on the current field (Field{i})
55%            .Txt : text information to display (e.g. error message)
56%            .NbDim: number of dimensions (=0 by default)
57%            .NbCoord: number of vector components
58%            .CoordType: expresses the type of coordinate ('px' for image, 'sig' for instruments, or 'phys')
59%            .dt: time interval for the corresponding image pair
60%            .Mesh: estimated typical distance between vectors
61%            .ZMax:
62%            .ZMin:
63%            .X, .Y, .Z: set of vector coordinates
64%            .U,.V,.W: corresponding set of vector components
65%            .F: corresponding set of warning flags
66%            .FF: corresponding set of false flags, =0 for good vectors
67%            .C: corresponding values of the scalar used for vector color
68%             (.X, .Y, .Z,.U,.V,.W,.F,.FF,.C are matlab vectors of the same length,
69%                     equal to the number of vectors stored in the input open)
70%            .CName: name of the scalar .C
71%            .CType: type of the scalar .C, setting how the scalar is obtained (see 'Scalars' below)
72%            .A image or scalar
73%            .AX: vector of dimension 2 representing the first and last values
74%              of the X coordinates for the image or scalar known on a regular grid,
75%              or vector of dimension .A for a scaler defined on irregular grid.
76%            .AY: same as .AX along the Y direction
77%            .AName: name of the scalar, ='image' for an image
78
79% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   DATA FLOW  (for run0_Callback) %%%%%%%%%%%%%%%%%%%%:
80%
81%               Main input open   second input open(_1)        second image (pair animation)
82% read_ncfield.m         |                 |                             
83% read image.m           |                 |                                               
84%                     Field{1}         Field{2}               
85%                                                                         |
86% coord transform (phys.m) or other user defined  fct acting on Field{i}  |
87%                                                                   Field{i}
88%                                                                    |
89% calc_field.m: calculate scalar or other derived fields (vort, div..).
90%
91% sub_field.m: combine the input Field{i} in a single set of fields (vector + scalar):
92%              Field{i=1->3}.X --> UvData.X                          |
93%                                                                    |
94%                                                                 UvData
95%                                                                    |
96% plot histograms of the whole  field
97% proj_field.m: project the set of fields on the current projection objects defined by UvData.Object
98%                                                                    |                                                                          |
99%                                                                ObjectData
100%                                                                    |
101% plot_field.m: plot the projected fields and store them as user     |
102% data of the plotting axes                                          |
103%                                                                    |
104%                                                                AxeData
105%
106%%%%%%%%%%%%%%    SCALARS: %%%%%%%%%%%%??%%%
107% scalars are displayed either as an image or countour plot, either as a color of
108% velocity vectors. The scalar values in the first case is represented by
109% UvData.Field.A, and by UvData.Field.C in the second case. The corresponding set of X
110% and Y coordinates are represented by UvData.Field.AX and UvData.Field.AY, and .X and
111% .Y for C (the same as velocity vectors). If A is a nxxny matrix (scalar
112% on a regtular grid), then .AX andf.AY contains only two elements, represneting the
113% coordinates of the four image corners. The scalar name is represented by
114% the strings .AName and/or .CName.
115% If the scalar exists in an input open (image or scalar stored under its
116% name in a netcdf open), it is directly read at the level of Field{1}or Field{2}.
117% Else only its name AName is recorded in Field{i}, and its field is then calculated
118%by the fuction calc_scal after the coordinate transform or after projection on an edit
119     
120% Properties attached to plotting figures (standard Matlab properties):
121%    'CurrentAxes'= gca or get(gcf,'CurrentAxes');
122%    'CurrentPoint'=get(gcf,'CurrentPoint'): figure coordinates of the point over which the mouse is positioned
123%    'CurrentCharacter'=get(gcf,'CurrentCharacter'): last character typed  over the figure where the mouse is positioned
124%    'WindowButtonMotionFcn': function permanently called by mouse motion over the figure
125%    'KeyPressFcn': function called by pressing a key on the key board
126%    'WindowButtonDownFcn':  function called by pressing the mouse over the  figure
127%    'WindowButtonUpFcn': function called by releasing  the mouse pressure over the  figure
128
129% Properties attached to plotting axes:
130%    'CurrentPoint'=get(gca,'CurrentPoint'); (standard Matlab) same as for the figure, but position in plot coordinates.
131%     AxeData:=get(gca,'UserData');
132%     AxeData.Drawing  = create: create a new edit
133%                       = deform: modify an existing edit by moving its defining create
134%                      = off: no current drawing action
135%                     = translate: translate an existing edit
136%                    = zoom: isolate a subregion for zoom in=1 if an edit is being currently drawn, 0 else (set to 0 by releasing mouse button)
137%            .hset_edit=handle of the set_edit interface (if a projection edit is edited);
138%            .CurrentOrigin: Origin of a curently drawn edit
139%            .CurrentLine: currently drawn menuline (A REVOIR)
140%            .CurrentObject: handle of the currently drawn edit
141%            .CurrentRectZoom: current rectangle used for zoom
142%            .zoomon : zoom state (a revoir)
143%            .X, .Y, .Z: array of coordinates defining the position of the vector: ASSOCIER AU PLAN (OBJET) PLUTOT QU'A L'AXE ?
144%            .U, .V, .W: array of components of the vector
145%            .C:
146%            .F:
147%            .FF:
148%            .A
149
150% Properties attached to projection objects (create, menuline, menuplane...):
151%    'Tag'='proj_edit': for all projection objects
152%    ObjectData.Style=...: style of projection edit:
153%              .ProjMode
154%              .Coord: defines the position of the edit
155%              .XMin,YMin....
156%              .XMax,YMax....
157%              .DX,DY,DZ
158%              .Phi, .Theta, .Psi : Euler angles
159%              .X,.Y,.U,.V.... : field data projected on the edit
160%              .IndexObj: index in the list of UvData.Object
161           %during plotting
162%               .plotaxes: handles of the current axes used to plot the  result of field projection on the object
163%               .plothandle: vector of handle(s) of the object graphic represnetation in all the opened plotting axes
164% To each projection object #iobj, corresponds an axis
165% Object{iobj}.plotaxes and nbobj representation graphs  Object{iobj}.plothandles(:) (where nbobj is the
166% nbre of current objects opened in uvmat. Note that Object{iobj}.plothandles(iobj)=[] : an object is not represented in its own projection field;
167%-------------------------------------------------------------------
168%-------------------------------------------------------------------
169%  I - MAIN FUNCTION UVMAT (DO NOT MODIFY)
170%-------------------------------------------------------------------
171%-------------------------------------------------------------------
172function varargout = uvmat(varargin)
173
174% Begin initialization code - DO NOT EDIT
175gui_Singleton = 1;
176gui_State = struct('gui_Name',          mfilename, ...
177                   'gui_Singleton',     gui_Singleton, ...
178                   'gui_OpeningFcn',    @uvmat_OpeningFcn, ...
179                   'gui_OutputFcn',     @uvmat_OutputFcn, ...
180                   'gui_LayoutFcn',     [], ...
181                   'gui_Callback',      []);
182if nargin && ischar(varargin{1})
183    gui_State.gui_Callback = str2func(varargin{1});
184end
185
186if nargout
187    varargout{1:nargout} = gui_mainfcn(gui_State, varargin{:});
188else
189    gui_mainfcn(gui_State, varargin{:});
190end
191% End initialization code - DO NOT EDIT
192
193%-------------------------------------------------------------------
194% --- Executes just before uvmat is made visible.
195function uvmat_OpeningFcn(hObject, eventdata, handles, input )
196%-------------------------------------------------------------------
197global nb_builtin
198
199% Choose default command menuline output for uvmat
200handles.output = hObject;
201
202% Update handles structure
203guidata(hObject, handles);
204
205% set the position of colorbar and ancillary GUIs:
206set(hObject,'Units','Normalized')
207movegui(hObject,'center')
208UvData.PosColorbar=[0.805 0.022 0.019 0.445];
209UvData.SetObjectOrigin=[-0.05 -0.03]; %position for set_object
210UvData.SetObjectSize=[0.3 0.7];
211UvData.CalOrigin=[0.95 -0.03];%position for geometry_calib (TO IMPROVE)
212UvData.CalSize=[0.28 1];
213
214%functions for the mouse and keyboard
215set(handles.histo_u,'NextPlot','replacechildren');
216set(handles.histo_v,'NextPlot','replacechildren');
217set(hObject,'KeyPressFcn',{'keyboard_callback',handles})%set keyboard action function
218set(hObject,'WindowButtonMotionFcn',{'mouse_motion',handles})%set mouse action functio
219set(hObject,'WindowButtonDownFcn',{'mouse_down'})%set mouse click action function
220set(hObject,'WindowButtonUpFcn',{'mouse_up',handles})
221set(hObject,'DeleteFcn',{@closefcn})%
222
223%TRANSFORM menu: loads the information stored in prefdir to initiate the browser and the list of functions
224menu_str={'';'phys';'px';'phys_polar'};
225nb_builtin=numel(menu_str); %number of functions
226path_uvmat=fileparts(which('uvmat'));
227addpath(fullfile(path_uvmat,'transform_field'))
228fct_handle{1,1}=[];
229testexist=zeros(size(menu_str'));%default
230testexist(1)=1;
231for ilist=2:length(menu_str)
232    if exist(menu_str{ilist},'file')
233        fct_handle{ilist,1}=str2func(menu_str{ilist});
234        testexist(ilist)=1;
235    else
236        testexist(ilist)=0;
237    end
238end
239rmpath(fullfile(path_uvmat,'transform_field'))
240
241%refresh projection plane
242UvData.Object{1}.Style='plane';%main plotting plane
243UvData.Object{1}.ProjMode='projection';%main plotting plane
244if ~isfield(UvData.Object{1},'plotaxes')
245    UvData.Object{1}.plotaxes=handles.axes3;%default plotting axis
246    set(handles.list_object_1,'Value',1);
247    set(handles.list_object_1,'String',{'1-PLANE'});
248end
249
250%load the list of previously browsed files in menus Open and Open_1
251 dir_perso=prefdir; % path to the directory .matlab for personal data
252 profil_perso=fullfile(dir_perso,'uvmat_perso.mat');% personal data file uvmauvmat_perso.mat' in .matlab
253 if exist(profil_perso,'file')
254      h=load (profil_perso);
255      if isfield(h,'MenuFile_1')
256          set(handles.MenuFile_1,'Label',h.MenuFile_1);
257          set(handles.MenuFile_1_1,'Label',h.MenuFile_1);
258      end
259      if isfield(h,'MenuFile_1')
260          set(handles.MenuFile_2,'Label',h.MenuFile_2);
261          set(handles.MenuFile_2_1,'Label',h.MenuFile_2);
262      end
263      if isfield(h,'MenuFile_1')
264          set(handles.MenuFile_3,'Label',h.MenuFile_3);
265          set(handles.MenuFile_3_1,'Label',h.MenuFile_3);
266      end
267      if isfield(h,'MenuFile_1')
268          set(handles.MenuFile_4,'Label',h.MenuFile_4);
269          set(handles.MenuFile_4_1,'Label',h.MenuFile_4);
270      end
271      if isfield(h,'MenuFile_1')
272          set(handles.MenuFile_5,'Label',h.MenuFile_5);
273          set(handles.MenuFile_5_1,'Label',h.MenuFile_5);
274      end
275      if isfield(h,'transform_fct') && iscell(h.transform_fct)
276         for ilist=1:length(h.transform_fct);
277             [path,file]=fileparts(h.transform_fct{ilist});
278             addpath(path)
279             if exist(file,'file')
280                h_func=str2func(file);
281                testexist=[testexist 1]; %#ok<AGROW>
282             else
283                h_func=[];
284                testexist=[testexist 0];  %#ok<AGROW>
285             end
286             fct_handle=[fct_handle; {h_func}];%#ok<AGROW> %concatene the list of paths
287             rmpath(path)
288             menu_str=[menu_str; {file}]; %#ok<AGROW>
289         end
290      end
291 end
292menu_str=menu_str(testexist==1);%=menu_str(testexist~=0)
293fct_handle=fct_handle(testexist==1);
294menu_str=[menu_str;{'more...'}];
295set(handles.transform_fct,'String',menu_str)
296set(handles.transform_fct,'UserData',fct_handle)% store the list of path in UserData of ACTION
297 
298%initiates menu of vector colors
299list_menu=calc_field;
300%list_menu=[{'ima_cor'};{'black'};{'white'};list_menu(3:end)];
301set(handles.col_vec,'String',list_menu)
302
303%check the path and date of modification of all functions in uvmat
304path_to_uvmat=which ('uvmat');% check the path detected for source file uvmat
305[errormsg,date_str]=check_functions;%check the path of the functions called by uvmat.m
306date_str=['last modification: ' date_str];
307%case of an input argument for uvmat
308testinputfield=0;
309inputfile=[];
310Field=[];
311if exist('input','var')
312    if ~isempty(errormsg)
313        msgbox_uvmat('WARNING',errormsg)
314    end
315    if ishandle(handles.UVMAT_title)
316        delete(handles.UVMAT_title)
317    end   
318    if isstruct(input)
319        if isfield(input,'InputFile')
320            inputfile=input.InputFile;
321        end
322        Field=input;
323    elseif ischar(input)% file name introduced as input
324           inputfile=input;
325    elseif isnumeric(input)%simple matrix introduced as input
326        sizinput=size(input);
327        if sizinput(1)<=1 || sizinput(2)<=1
328            msgbox_uvmat('ERROR','bad input for uvmat: file name, structure or numerical matrix accepted')
329            return
330        end
331        Field.ListVarName={'A','coord_y','coord_x'};
332        Field.VarDimName={{'coord_y','coord_x'},'cord_y','coord_x'};
333        Field.A=input;
334        Field.coord_x=[0.5 size(input,2)-0.5];
335        Field.coord_y=[size(input,1)-0.5 0.5];
336    end
337    if ~isempty(inputfile)
338        display_file_name(hObject, eventdata, handles,inputfile)
339        testinputfield=1;
340    else
341        UvData.TestInputFile=0;
342    end
343    if ~isempty(Field)
344        set(handles.Fields,'Value',1)
345        set(handles.Fields,'String',{'get_field...'})   
346       % set(handles.Fields,'UserData',Field)
347        testinputfield=1;
348       
349        % set the colorbar position on the interface:
350        UvData.PosColorbar=[0.805 0.022 0.019 0.445];
351    elseif ischar(input)
352        scan_i_Callback(handles.scan_i, eventdata, handles);       
353    end
354else
355   if ishandle(handles.UVMAT_title)
356       fid=fopen('revision.log');
357       if fid~=-1
358        a=textscan(fid,'%s%s%s',1,'HeaderLines',1,'Delimiter','|');
359        set(handles.UVMAT_title,'String',[{'Copyright Joel Sommeria, 2008, Coriolis/ LEGI / CNRS-UJF-INPG';'GNU General Public License'; path_to_uvmat; ['at revision ' a{1}{1}]};a{3}{1};date_str;errormsg]);
360        fclose(fid);
361       else
362           set(handles.UVMAT_title,'String',[{'Copyright Joel Sommeria, 2008, Coriolis/ LEGI / CNRS-UJF-INPG';'GNU General Public License'; path_to_uvmat;date_str};errormsg]);
363       end
364   end
365end
366UvData.NewSeries=1;
367set(hObject,'UserData',UvData)
368if testinputfield
369    %delete drawn objects
370    hother=findobj(handles.axes3,'Tag','proj_object');%find all the proj objects
371    for iobj=1:length(hother)
372        delete_object(hother(iobj))
373    end 
374    if isempty(inputfile)
375        errormsg=refresh_field(handles,[],[],[],[],[],[],{Field});
376        set(handles.MenuTools,'Enable','on')
377        set(handles.OBJECT_txt,'Visible','on')
378        set(handles.edit,'Visible','on')
379        set(handles.list_object_1,'Visible','on')
380        set(handles.frame_object,'Visible','on')
381        if ~isempty(errormsg)
382            msgbox_uvmat('ERROR',errormsg)
383        end
384    else
385        update_rootinfo(hObject,eventdata,handles);
386    end
387end
388set_vec_col_bar(handles) %update the display of color code for vectors
389
390%-------------------------------------------------------------------
391% --- Outputs from this function are returned to the command menuline.
392function varargout = uvmat_OutputFcn(hObject, eventdata, handles)
393varargout{1} = handles.output;% the only output argument is the handle to the GUI figure
394
395%-------------------------------------------------------------------
396%-------------------------------------------------------------------
397% II - FUNCTIONS FOR INTRODUCING THE INPUT FILES
398% automatically sets the global properties when the rootfile name is introduced
399% then activate the view-field action if selected
400% it is activated either by clicking on the RootPath window or by the
401% browser
402%------------------------------------------------------------------
403%------------------------------------------------------------------
404
405% --- Executes on the menu Open/Browse...
406% search the files, recognize their type according to their name and fill the rootfile input windows
407function MenuBrowse_Callback(hObject, eventdata, handles)
408oldfile=read_file_boxes(handles);
409
410if isempty(oldfile)||isequal(oldfile,'') %loads the previously stored file name and set it as default in the file_input box
411         dir_perso=prefdir;
412         profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
413         if exist(profil_perso,'file')
414              h=load (profil_perso);
415             if isfield(h,'MenuFile_1')
416                  oldfile=h.MenuFile_1;
417             end
418         end
419end
420[FileName, PathName] = uigetfile( ...
421       {'*.xml;*.xls;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol;*.nc;*.cmx;*.fig;*.log;*.dat;*.bat;', ' (*.xml,*.xls,*.civ,*.jpg ,*.png, .tif, *.avi,*.vol,*.nc,*.cmx ,*.fig,*.log,*.dat,*.bat)';
422       '*.xml',  '.xml files '; ...
423        '*.xls',  '.xls files '; ...
424        '*.civ',  '.civ files '; ...
425        '*.jpg',' jpeg image files'; ...
426        '*.png','.png image files'; ...
427        '*.tif','.tif image files'; ...
428        '*.avi;*.AVI','.avi movie files'; ...
429        '*.vol','.volume images (png)'; ...
430        '*.nc','.netcdf files'; ...
431        '*.cdf','.netcdf files'; ...
432        '*.cmx','.cmx text files';...
433        '*.cmx2','.cmx2 text files';...
434        '*.fig','.fig files (matlab fig)';...
435        '*.log','.log text files ';...
436        '*.dat','.dat text files ';...
437        '*.bat','.bat system command text files';...
438        '*.*',  'All Files (*.*)'}, ...
439        'Pick a file',oldfile);
440%global filebase
441fileinput=[PathName FileName];%complete file name
442testblank=findstr(fileinput,' ');%look for blanks
443if ~isempty(testblank)
444    msgbox_uvmat('ERROR',['The input file name ' fileinput ' contains blank character : This is not allowed. Please change name'])
445    return
446end
447sizf=size(fileinput);
448if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
449
450% display the selected field and related information
451display_file_name(hObject, eventdata, handles,fileinput)
452
453%update list of recent files in the menubar
454MenuFile_1=fileinput;
455MenuFile_2=get(handles.MenuFile_1,'Label');
456MenuFile_3=get(handles.MenuFile_2,'Label');
457MenuFile_4=get(handles.MenuFile_3,'Label');
458MenuFile_5=get(handles.MenuFile_4,'Label');
459set(handles.MenuFile_1,'Label',MenuFile_1)
460set(handles.MenuFile_2,'Label',MenuFile_2)
461set(handles.MenuFile_3,'Label',MenuFile_3)
462set(handles.MenuFile_4,'Label',MenuFile_4)
463set(handles.MenuFile_5,'Label',MenuFile_5)
464set(handles.MenuFile_1_1,'Label',MenuFile_1)
465set(handles.MenuFile_2_1,'Label',MenuFile_2)
466set(handles.MenuFile_3_1,'Label',MenuFile_3)
467set(handles.MenuFile_4_1,'Label',MenuFile_4)
468set(handles.MenuFile_5_1,'Label',MenuFile_5)
469dir_perso=prefdir;
470profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
471if exist(profil_perso,'file')
472    save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-append'); %store the file names for future opening of uvmat
473else
474    txt=ver;
475    Release=txt(1).Release;
476    relnumb=str2double(Release(3:4));
477    if relnumb >= 14
478        save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-V6'); %store the file names for future opening of uvmat
479    else
480        save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5'); %store the file names for future opening of uvmat
481    end
482end
483
484% --------------------------------------------------------------------
485function MenuFile_1_Callback(hObject, eventdata, handles)
486fileinput=get(handles.MenuFile_1,'Label');
487display_file_name(hObject, eventdata, handles,fileinput)
488
489% --------------------------------------------------------------------
490function MenuFile_2_Callback(hObject, eventdata, handles)
491fileinput=get(handles.MenuFile_2,'Label');
492display_file_name(hObject, eventdata, handles,fileinput)
493
494% --------------------------------------------------------------------
495function MenuFile_3_Callback(hObject, eventdata, handles)
496fileinput=get(handles.MenuFile_3,'Label');
497display_file_name(hObject, eventdata, handles,fileinput)
498
499% --------------------------------------------------------------------
500function MenuFile_4_Callback(hObject, eventdata, handles)
501fileinput=get(handles.MenuFile_4,'Label');
502display_file_name(hObject, eventdata, handles,fileinput)
503
504% --------------------------------------------------------------------
505function MenuFile_5_Callback(hObject, eventdata, handles)
506fileinput=get(handles.MenuFile_5,'Label');
507display_file_name(hObject, eventdata, handles,fileinput)
508
509%-----------------------------------------------------------------
510% fills the edit boxes RootPath, RootFile,NomType...from an input file name 'fileinput'
511%----------------------------------------------------------------
512function display_file_name(hObject, eventdata, handles,fileinput)
513if ~exist(fileinput,'file')
514    msgbox_uvmat('ERROR',['input file ' fileinput  ' does not exist'])
515    return
516end
517[RootPath,RootFile,i1,i2,str_a,str_b,ext,NomType,SubDir]=name2display(fileinput);
518ext_test=''; %default
519if ~isempty(ext)
520    form=imformats(ext(2:end));%test valid Matlab image formats
521    if ~isempty(form)
522        ext_test='.image';
523        imainfo=imfinfo(fileinput); 
524        if length(imainfo) >1 %case of image with multiple frames
525            i1='1'; % set the frame counter to 1 by default
526            i2='';
527            str_a='';
528            str_b='';
529            NomType='*'; %indicate a set of indexed frames within a single file
530            [RootPath,RootFile]=fileparts(fileinput); %include the indices in the root file
531        end
532    elseif isequal(lower(ext),'.avi')
533        ext_test='.image';
534        i1='1'; % set the frame counter to 1 by default
535        i2='';
536        str_a='';
537        str_b='';
538        NomType='*'; %indicate a set of indexed frames within a single file
539        [RootPath,RootFile]=fileparts(fileinput); %include the indices in the root file
540    else
541        ext_test=lower(ext);
542    end
543end
544switch ext_test
545    case {'.civ','.log','.cmx','.cmx2','.txt','.bat'}  %display text file
546        edit(fileinput) 
547    case '.fig'                           %display matlab figure
548        hfig=open(fileinput);
549        set(hfig,'WindowButtonMotionFcn','mouse_motion')%set mouse action functio
550        set(hfig,'WindowButtonUpFcn','mouse_up')%set mouse click action function
551        set(hfig,'WindowButtonUpFcn','mouse_down')%set mouse click action function
552    case {'.xml','.xls'}                % edit xml or Excel files
553       editxml(fileinput);
554    case {'.avi','.image','.vol','.nc','.cdf'}   
555        set(handles.RootPath,'String',RootPath);
556        if  isequal(SubDir,'')
557            rootname=fullfile(RootPath,RootFile);
558        else
559            rootname=fullfile(RootPath,SubDir,RootFile);
560            SubDir=['/' SubDir]; %display the separator
561        end
562        set(handles.SubDir,'String',SubDir);
563        set(handles.RootFile,'String',['/' RootFile]); %display the separator
564        indices=fileinput(length(rootname)+1:end);
565        indices(end-length(ext)+1:end)=[]; %remove extension
566        set(handles.FileIndex,'String',indices);       
567        set(handles.FileIndex,'UserData',NomType);
568        set(handles.FileExt,'String',ext);
569        % fill file index counters
570        set(handles.i1,'String',i1);   
571        set(handles.i2,'String',i2);
572        set(handles.j1,'String',str_a);
573        set(handles.j2,'String',str_b);
574       
575        % synchronise indices of the second  input file if it exists
576        if get(handles.SubField,'Value')==1% if the subfield button is activated, update the field numbers
577            [ff,rr,FileBase_1,ii,FileExt_1,SubDir_1]=read_file_boxes_1(handles);
578            NomType_1=get(handles.FileIndex_1,'UserData');     
579            FileName_1=name_generator(FileBase_1,stra2num(i1),stra2num(i2),FileExt_1,NomType_1,1,stra2num(str_a),stra2num(str_b),SubDir_1);
580            if exist(FileName_1,'file')
581                FileIndex_1=name_generator('',stra2num(i1),stra2num(i2),'',NomType_1,1,stra2num(str_a),stra2num(str_b),'');
582                set(handles.FileIndex_1,'String',FileIndex_1)
583            else
584                set(handles.SubField,'Value',0)
585                SubField_Callback(hObject, eventdata, handles)
586            end
587        end 
588
589        %enable other menus
590        set(handles.MenuOpen_1,'Enable','on')
591        set(handles.MenuFile_1_1,'Enable','on')
592        set(handles.MenuFile_2_1,'Enable','on')
593        set(handles.MenuFile_3_1,'Enable','on')
594        set(handles.MenuFile_4_1,'Enable','on')
595        set(handles.MenuFile_5_1,'Enable','on')
596        set(handles.MenuExport,'Enable','on')
597        set(handles.MenuExportFigure,'Enable','on')
598        set(handles.MenuExportMovie,'Enable','on')
599        set(handles.MenuTools,'Enable','on')
600        set(handles.OBJECT_txt,'Visible','on')
601        set(handles.edit,'Visible','on')
602        set(handles.list_object_1,'Visible','on')
603        set(handles.frame_object,'Visible','on')
604         % initiate input file:
605        update_rootinfo(hObject,eventdata,handles); 
606    otherwise
607       msgbox_uvmat('ERROR',['invalid input file extension' ext])
608end
609
610%-------------------------------------------------------------------
611function RootPath_Callback(hObject,eventdata,handles)
612update_rootinfo(hObject,eventdata,handles);
613
614%-------------------------------------------------------------------
615%-- called by action in RootFile edit box
616%-------------------------------------------------------------------
617function SubDir_Callback(hObject, eventdata, handles)
618%refresh the menu of input fields
619Fields_Callback(hObject, eventdata, handles);
620% refresh the current field
621run0_Callback(hObject, eventdata, handles);
622
623
624%-------------------------------------------------------------------
625%-- called by action in RootFile edit box
626%-------------------------------------------------------------------
627function RootFile_Callback(hObject, eventdata, handles)
628update_rootinfo(hObject,eventdata,handles)
629
630%-------------------------------------------------------------------
631%-- called by action in FileIndex edit box
632%-------------------------------------------------------------------
633function FileIndex_Callback(hObject, eventdata, handles)
634FileIndices=get(handles.FileIndex,'String');
635if isempty(str2num(FileIndices))
636    [pp,ff,str1,str2,str_a,str_b]=name2display(FileIndices);
637else
638    str1=FileIndices;
639    str2='';
640    str_a='';
641    str_b='';
642end
643set(handles.i1,'String',str1);
644set(handles.i2,'String',str2);
645set(handles.j1,'String',str_a);
646set(handles.j2,'String',str_b);
647run0_Callback(hObject, eventdata, handles)
648
649%-------------------------------------------------------------------
650%-- called by action in FileIndex_1 edit box
651%-------------------------------------------------------------------
652function FileIndex_1_Callback(hObject, eventdata, handles)
653% FileIndices=get(handles.FileIndex_1,'String');
654% if isempty(str2num(FileIndices))
655%     [pp,ff,str1,str2,str_a,str_b]=name2display(FileIndices)
656% else
657%     str1=FileIndices;
658%     str2='';
659%     str_a='';
660%     str_b='';
661% end
662% set(handles.i1,'String',str1);
663% set(handles.i2,'String',str2);
664% set(handles.j1,'String',str_a);
665% set(handles.j2,'String',str_b);
666run0_Callback(hObject, eventdata, handles)
667
668%-------------------------------------------------------------------
669% -- update information about a new field series (indices to scan, timing, calibration from an xml file, then refresh current plots
670%-------------------------------------------------------------------
671function update_rootinfo(hObject,eventdata,handles)
672
673set(handles.RootPath,'BackgroundColor',[1 1 0])
674drawnow
675set(handles.Fields,'UserData',[])% reinialize data from uvmat opening
676UvData=get(handles.uvmat,'UserData');%huvmat=handles of the uvmat interface
677UvData.NewSeries=1; %flag for run0: begin a new series
678UvData.TestInputFile=1;
679set(handles.fix_pair,'Value',1) % activate by default the comp_input '-'input window
680%FileIndex_Callback(hObject, eventdata, handles)% update field counters
681
682[FileName,RootPath,FileBase,FileIndices,FileExt,SubDir]=read_file_boxes(handles);
683if ~exist(FileName,'file')
684   msgbox_uvmat('ERROR',['input file ' FileName ' not found']);
685    return
686end
687nbfield=[];%default
688nburst=[];%default
689
690% read timing and total frame number from the current file (movie files) !! may be overrid by xml file
691XmlData.Time=[];%default
692XmlData.GeometryCalib=[];%default
693TimeUnit=[];%default
694testima=0; %test for image input
695if isequal(lower(FileExt),'.avi') %.avi file
696    testima=1;
697    info=aviinfo([FileBase FileIndices FileExt]);
698    nbfield=info.NumFrames;
699    nburst=1;
700    set(handles.Dt_txt,'String',['Dt=' num2str(1000/info.FramesPerSecond) 'ms']);%display the elementary time interval in millisec
701    XmlData.Time=(0:1/info.FramesPerSecond:(info.NumFrames-1)/info.FramesPerSecond)';
702    TimeUnit='s';
703    set(handles.npx,'String',num2str(info.Width));%fills nbre of pixels x box
704    set(handles.npy,'String',num2str(info.Height));%fills nbre of pixels y box
705    hhh=which('mmreader');
706    if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the function is found (recent version of matlab)
707        UvData.MovieObject=mmreader([FileBase FileIndices FileExt]);
708    end
709elseif ~isempty(imformats(FileExt(2:end))) || isequal(FileExt,'.vol')%&& isequal(NomType,'*')% multi-frame image
710    testima=1;
711    if ~isequal(SubDir,'')
712       RootFile=get(handles.RootFile,'String');
713        imainfo=imfinfo([fullfile(RootPath,SubDir,RootFile) FileIndices FileExt]);
714    else
715        imainfo=imfinfo([FileBase FileIndices FileExt]);
716    end
717    if length(imainfo) >1 %case of image with multiple frames
718        nbfield=length(imainfo);
719        nburst=1;
720    end
721    if isfield(UvData,'MovieObject')
722        UvData=rmfield(UvData,'MovieObject');
723    end
724else
725    %set(handles.last_i,'String','');%fills last_field box
726    set(handles.npx,'String','');%fills nbre of pixels x box
727    set(handles.npy,'String','');%fills nbre of pixels y box
728    if isfield(UvData,'MovieObject')
729        UvData=rmfield(UvData,'MovieObject');
730    end
731end
732
733% read parameters (time, geometric calibration..) from a documentation file (.xml advised)
734filexml=[FileBase '.xml'];
735fileciv=[FileBase '.civ'];
736warntext='';%default warning message
737NbSlice=1;%default
738
739if exist(filexml,'file')
740    set(handles.view_xml,'Visible','on')
741    set(handles.view_xml,'BackgroundColor',[1 1 0])
742    set(handles.RootPath,'BackgroundColor',[1 1 1])
743    set(handles.view_xml,'String','view .xml')
744    drawnow
745    [XmlData,warntext]=imadoc2struct(filexml);
746    if ~isempty(warntext)
747        msgbox_uvmat('WARNING',warntext)
748    end
749    if isfield(XmlData,'TimeUnit')
750        if isfield(XmlData,'TimeUnit')&& ~isempty(XmlData.TimeUnit)
751            TimeUnit=XmlData.TimeUnit;
752        end
753    end
754    set(handles.view_xml,'BackgroundColor',[1 1 1])
755    drawnow
756elseif exist(fileciv,'file')% if .civ file found
757    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
758    GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
759    GeometryCalib.Tx=0;
760    GeometryCalib.Ty=0;
761    GeometryCalib.Tz=1;
762    GeometryCalib.dpx=1;
763    GeometryCalib.dpy=1;
764    GeometryCalib.sx=1;
765    GeometryCalib.Cx=0;
766    GeometryCalib.Cy=0;
767    GeometryCalib.f=1;
768    GeometryCalib.kappa1=0;
769    GeometryCalib.CoordUnit='cm';
770    XmlData.GeometryCalib=GeometryCalib;
771    if error==2, warntext=['no file ' FileBase '.civ'];
772    elseif error==1, warntext='inconsistent number of fields in the .civ file';
773    end 
774    set(handles.npx,'String',num2str(npx));%fills nbre of pixels x box
775    set(handles.npy,'String',num2str(npy));%fills nbre of pixels y box
776    set(handles.pxcm,'String',num2str(pxcmx));%fills scale x (pixel/cm) box
777    set(handles.pycm,'String',num2str(pxcmy));%fills scale y (pixel/cm) box
778    set(handles.pxcm,'Visible','on');%fills scale x (pixel/cm) box
779    set(handles.pycm,'Visible','on');%fills scale y (pixel/cm) box
780    set(handles.view_xml,'Visible','on')   
781    set(handles.view_xml,'String','view .civ')
782else
783    set(handles.view_xml,'Visible','off')
784end
785
786% store last index in handles.lat_i and .last_j
787if ~isempty(XmlData.Time)
788    nbfield=size(XmlData.Time,1);
789    nburst=size(XmlData.Time,2);
790    %transform .Time to a column vector if it is a line vector the nomenclature uses a single index
791    if isequal(nbfield,1) && ~isequal(nburst,1)% .Time is a line vector
792        NomType=get(handles.FileIndex,'UserData');
793        if numel(NomType)>=2 &&(strcmp(NomType,'_i')||strcmp(NomType(1:2),'%0')||strcmp(NomType(1:2),'_%'))
794            XmlData.Time=(XmlData.Time)';
795            nbfield=nburst;
796            nburst=1;
797        end
798    end
799end
800last_i_cell=get(handles.last_i,'String');
801if isempty(nbfield)
802    last_i_cell{1}='';
803else
804    last_i_cell{1}=num2str(nbfield);
805end
806set(handles.last_i,'String',last_i_cell)
807last_j_cell=get(handles.last_j,'String');
808if isempty(nburst)
809     last_j_cell{1}='';
810else
811     last_j_cell{1}=num2str(nburst);
812end
813set(handles.last_j,'String',last_j_cell);
814
815% store geometric calibration in UvData
816if isfield(XmlData,'GeometryCalib')
817    GeometryCalib=XmlData.GeometryCalib;
818    if isempty(GeometryCalib)
819        set(handles.pxcm,'String','')
820        set(handles.pycm,'String','')
821        set(handles.transform_fct,'Value',1); %  no transform by default
822    else
823        if (isfield(GeometryCalib,'R')& ~isequal(GeometryCalib.R(2,1),0) & ~isequal(GeometryCalib.R(1,2),0)) |...
824            (isfield(GeometryCalib,'kappa1')& ~isequal(GeometryCalib.kappa1,0))
825            set(handles.pxcm,'String','var')
826            set(handles.pycm,'String','var')
827        else
828            pixcmx=GeometryCalib.f*GeometryCalib.R(1,1)*GeometryCalib.sx/(GeometryCalib.Tz*GeometryCalib.dpx);
829            pixcmy=GeometryCalib.f*GeometryCalib.R(2,2)/(GeometryCalib.Tz*GeometryCalib.dpy);
830            set(handles.pxcm,'String',num2str(pixcmx))
831            set(handles.pycm,'String',num2str(pixcmy))
832        end
833        if ~get(handles.FixedLimits,'Value')
834            set(handles.transform_fct,'Value',2); % phys transform by default if fixedLimits is off
835        end
836        if isfield(GeometryCalib,'SliceCoord')
837           siz=size(GeometryCalib.SliceCoord);
838           if siz(1)>1
839               NbSlice=siz(1);
840               set(handles.slices,'Visible','on')
841               set(handles.slices,'Value',1)
842           end
843           if isfield(GeometryCalib,'NbSlice') && isequal(GeometryCalib.NbSlice,'volume')
844               set(handles.nb_slice,'String','volume')
845           else
846               set(handles.nb_slice,'String',num2str(NbSlice))
847           end
848           slices_Callback(hObject, eventdata, handles)
849    %        Coord=UvData.XmlData.GeometryCalib.SliceCoord;
850    %        ZIndex=num_i1-NbSlice*(floor((num_i1-1)/NbSlice));
851        end
852
853           
854    end
855end
856
857%update the data attached to the uvmat interface
858% set(handles.nb_slice,'String',num2str(NbSlice))
859if ~isempty(TimeUnit)
860    set(handles.time_txt,'String',['time (' TimeUnit ')'])
861end
862%set(handles.DtUnit,'String',['10^(-3)' TimeUnit])
863UvData.TimeUnit=TimeUnit;
864UvData.XmlData=XmlData;
865UvData.NewSeries=1;
866set(handles.uvmat,'UserData',UvData)
867
868%display warning message
869if ~isequal(warntext,'')
870    msgbox_uvmat('WARNING',warntext);
871end
872
873% set default options in menu 'Fields'
874if testima
875   set(handles.Fields,'Value',1) % set menu to 'image'
876   set(handles.Fields,'String',{'image';'get_field...';'velocity';'vort';'div';'more...'})
877elseif isequal(FileExt,'.nc')||isequal(FileExt,'.cdf')
878   Data=nc2struct(FileName,'ListGlobalAttribute','absolut_time_T0','civ');
879   col_vec=get(handles.col_vec,'String');
880   if ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0)
881       set(handles.Fields,'String',{'image';'get_field...';'velocity';'vort';'div';'more...'})
882       set(handles.Fields,'Value',3) % set menu to 'velocity'
883       col_vec{1}='ima_cor';
884   else %general netcdf file (not civx)
885       set(handles.Fields,'Value',1) % set menu to 'get_field...
886       set(handles.Fields,'String',{'get_field...'})
887       col_vec{1}='get_field...';
888   end
889   set(handles.col_vec,'String',col_vec)
890else
891    msgbox_uvmat('ERROR',['invalid input file extension ' FileExt])
892    return
893end 
894
895% set index navigation options and refresh plots
896set(handles.RootPath,'BackgroundColor',[1 1 1])
897drawnow
898set_scan_options(hObject, eventdata, handles)
899
900
901%-------------------------------------------------------------------
902%-- set index navigation options for new series input and refresh plot
903%-------------------------------------------------------------------
904function set_scan_options(hObject, eventdata, handles)
905
906%  set the corresponding index navigation options
907NomType=get(handles.FileIndex,'UserData');       
908NomType_1=get(handles.FileIndex_1,'UserData');
909last_i_str=get(handles.last_i,'String');
910nbfield=str2num(last_i_str{1});
911if numel(last_i_str)==2
912    nbfield=min(nbfield,str2num(last_i_str{2}));
913end 
914state_j='off'; %default
915scan_option='i';%default
916switch NomType
917    case {'_i_j','_i_j1-j2','_i1-i2_j','#_ab'},% two navigation indices
918        state_j='on';
919        if isequal(nbfield,1)
920            scan_option='j';                 
921        end
922end
923if ~isempty(NomType_1)
924    switch NomType_1
925        case {'_i_j','_i_j1-j2','_i1-i2_j','#_ab'},% two navigation indices
926            state_j='on';
927            if isequal(nbfield,1)
928                scan_option='j';                 
929            end           
930    end
931end
932if isequal(scan_option,'i')
933     set(handles.scan_i,'Value',1)
934     scan_i_Callback(hObject, eventdata, handles);
935else
936     set(handles.scan_j,'Value',1)
937     scan_j_Callback(hObject, eventdata, handles);
938end
939set(handles.scan_j,'Visible',state_j)
940set(handles.j1,'Visible',state_j)
941set(handles.j2,'Visible',state_j)
942set(handles.last_j,'Visible',state_j);
943set(handles.frame_j,'Visible',state_j);
944set(handles.j_text,'Visible',state_j);
945
946% view the field 
947run0_Callback(hObject, eventdata, handles); %view field
948mask_test=get(handles.mask_test,'value');
949if mask_test
950    MaskData=get(handles.mask_test,'UserData');
951    if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
952          delete(MaskData.maskhandle)    %delete old mask
953    end
954    mask_test_Callback(hObject, eventdata, handles)
955end
956
957%-------------------------------------------------------------------
958function MenuBrowse_1_Callback(hObject, eventdata, handles)
959%-------------------------------------------------------------------
960% huvmat=get(handles.run0,'parent');
961UvData=get(handles.uvmat,'UserData');
962
963RootPath=get(handles.RootPath,'String');
964[FileName, PathName, filterindex] = uigetfile( ...
965       {'*.xml;*.xls;*.civ;*.jpg;*.png;*.avi;*.AVI;*.nc;*.cmx;*.fig;*.log;*.dat', ' (*.xml,*.xls,*.civ, *.jpg,*.png, *.avi,*.nc,*.cmx ,*.fig,*.log,*.dat)';
966       '*.xml',  '.xml files '; ...
967        '*.xls',  '.xls files '; ...
968        '*.civ',  '.civ files '; ...
969        '*.jpg','.jpg image files'; ...
970        '*.png','.png image files'; ...
971        '*.avi;*.AVI','.avi movie files'; ...
972        '*.nc','.netcdf files'; ...
973        '*.cdf','.netcdf files'; ...
974        '*.cmx','.cmx text files';...
975        '*.cmx2','.cmx2 text files';...
976        '*.fig','.fig files (matlab fig)';...
977        '*.log','.log text files ';...
978        '*.dat','.dat text files ';...
979        '*.*',  'All Files (*.*)'}, ...
980        'Pick a file',RootPath);
981fileinput_1=[PathName FileName];%complete file name
982testblank=findstr(fileinput_1,' ');%look for blanks
983if ~isempty(testblank)
984    msgbox_uvmat('ERROR',['The input file name ' fileinput_1 ' contains blank character : This is not allowed. Please change name'])
985    return
986end
987sizf=size(fileinput_1);
988if (~ischar(fileinput_1)|~isequal(sizf(1),1)),return;end
989
990% refresh the current displayed field
991display_file_name_1(hObject,eventdata,handles,fileinput_1)
992
993%update list of recent files in the menubar
994MenuFile_1=fileinput_1;
995MenuFile_2=get(handles.MenuFile_1,'Label');
996MenuFile_3=get(handles.MenuFile_2,'Label');
997MenuFile_4=get(handles.MenuFile_3,'Label');
998MenuFile_5=get(handles.MenuFile_4,'Label');
999set(handles.MenuFile_1,'Label',MenuFile_1)
1000set(handles.MenuFile_2,'Label',MenuFile_2)
1001set(handles.MenuFile_3,'Label',MenuFile_3)
1002set(handles.MenuFile_4,'Label',MenuFile_4)
1003set(handles.MenuFile_5,'Label',MenuFile_5)
1004set(handles.MenuFile_1_1,'Label',MenuFile_1)
1005set(handles.MenuFile_2_1,'Label',MenuFile_2)
1006set(handles.MenuFile_3_1,'Label',MenuFile_3)
1007set(handles.MenuFile_4_1,'Label',MenuFile_4)
1008set(handles.MenuFile_5_1,'Label',MenuFile_5)
1009dir_perso=prefdir;
1010profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
1011if exist(profil_perso,'file')
1012    save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-append'); %store the file names for future opening of uvmat
1013else
1014    txt=ver;
1015    Release=txt(1).Release;
1016    relnumb=str2num(Release(3:4));
1017    if relnumb >= 14
1018        save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-V6'); %store the file names for future opening of uvmat
1019    else
1020        save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5'); %store the file names for future opening of uvmat
1021    end
1022end
1023
1024% --------------------------------------------------------------------
1025function MenuFile_1_1_Callback(hObject, eventdata, handles)
1026fileinput_1=get(handles.MenuFile_1_1,'Label');
1027display_file_name_1(hObject,eventdata,handles,fileinput_1)
1028
1029% --------------------------------------------------------------------
1030function MenuFile_2_1_Callback(hObject, eventdata, handles)
1031fileinput_1=get(handles.MenuFile_2_1,'Label');
1032display_file_name_1(hObject,eventdata,handles,fileinput_1)
1033
1034% --------------------------------------------------------------------
1035function MenuFile_3_1_Callback(hObject, eventdata, handles)
1036fileinput_1=get(handles.MenuFile_3_1,'Label');
1037display_file_name_1(hObject,eventdata,handles,fileinput_1)
1038
1039% --------------------------------------------------------------------
1040function MenuFile_4_1_Callback(hObject, eventdata, handles)
1041fileinput_1=get(handles.MenuFile_4_1,'Label');
1042display_file_name_1(hObject,eventdata,handles,fileinput_1)
1043
1044% --------------------------------------------------------------------
1045function MenuFile_5_1_Callback(hObject, eventdata, handles)
1046fileinput_1=get(handles.MenuFile_5_1,'Label');
1047display_file_name_1(hObject,eventdata,handles,fileinput_1)
1048
1049%-----------------------------------------------------------------
1050% fills the edit boxes RootPath, RootFile,NomType...from an input file name 'fileinput'
1051%----------------------------------------------------------------
1052function display_file_name_1(hObject,eventdata,handles,fileinput_1)
1053
1054%[path,name,ext]=fileparts(fileinput_1);
1055[RootPath_1,RootFile_1,field_count,str2,str_a,str_b,FileExt_1,NomType_1,SubDir_1]=name2display(fileinput_1);
1056nbfield_1=1; %default
1057ext_test=FileExt_1;%default
1058form=imformats(FileExt_1(2:end));
1059if ~isempty(form) % if the extension corresponds to an image format recognized by Matlab
1060    imainfo=imfinfo(fileinput_1); 
1061    nbfield_1=length(imainfo);
1062    ext_test='.image';
1063elseif isequal(lower(FileExt_1),'.avi')
1064    info=aviinfo(fileinput_1);
1065    nbfield_1=info.NumFrames;
1066    ext_test='.image';
1067end
1068
1069%open directly fig or text files
1070switch ext_test
1071    case {'.civ','.log','.cmx','.cmx2','.txt'}  %display text file
1072        edit(fileinput) 
1073        return
1074    case '.fig'                           %display matlab figure
1075        hfig=open(fileinput);
1076        set(hfig,'WindowButtonMotionFcn','mouse_motion')%set mouse action functio
1077        set(hfig,'WindowButtonUpFcn','mouse_up')%set mouse click action function
1078        set(hfig,'WindowButtonUpFcn','mouse_down')%set mouse click action function
1079        return
1080    case {'.xml','.xls'}                % edit xml or Excel files
1081       heditxml=editxml(fileinput);
1082       return
1083    case {'.image','.nc','.cdf'}
1084%         set(handles.FileIndex,'UserData',NomType_1);
1085    otherwise
1086        msgbox_uvmat(['invalid input file extension ' FileExt_1 ' for uvmat'],'ERROR')
1087        return
1088end
1089
1090% test for image series in a single file and synchronise file indices of the two series
1091if nbfield_1 >1 %case of image with multiple frames
1092    if nbfield_1 < num_i1
1093        msgbox_uvmat('ERROR','current frame index beyond the input movie length')
1094        return
1095    else
1096        NomType_1='*'; %indicate a set of indexed frames within a single file
1097        filename_new=fileinput_1;
1098    end
1099else  % cases of data files   
1100    RootPath=get(handles.RootPath,'String');
1101    RootFile=get(handles.RootFile,'String');
1102    FileBase=fullfile(RootPath,RootFile);
1103    FileBase_1=fullfile(RootPath_1,RootFile_1);
1104    if isequal(FileBase,FileBase_1)
1105        filename_new=fileinput_1;
1106    else       
1107        num_i1=stra2num(get(handles.i1,'String'));%get the current file indices from counters
1108        num_j1=stra2num(get(handles.j1,'String'));
1109        num_i2=stra2num(get(handles.i2,'String'));
1110        num_j2=stra2num(get(handles.j2,'String'));
1111        [filename_new,idetect]=...
1112           name_generator(FileBase_1,num_i1,num_j1,FileExt_1,NomType_1,1,num_i2,num_j2,SubDir_1);%create name with indices synchronised with the first file
1113        indices=''; %default
1114        if ~idetect
1115            msgbox_uvmat('ERROR','second input file with indices corresponding to the first one does not exist')
1116            return
1117        end
1118    end
1119end
1120set(handles.FileIndex_1,'UserData',NomType_1);
1121
1122% make visible and fill the second raw of edit boxes
1123set(handles.RootPath_1,'Visible','on')
1124set(handles.RootFile_1,'Visible','on')
1125set(handles.SubDir_1,'Visible','on');
1126set(handles.FileIndex_1,'Visible','on');
1127set(handles.FileExt_1,'Visible','on');
1128[FileName,RootPath,FileBase,FileIndices,FileExt,SubDir]=read_file_boxes(handles);
1129if isequal(FileBase,FileBase_1)
1130    set(handles.RootPath_1,'String','"')
1131    set(handles.RootFile_1,'String','"');
1132else
1133    set(handles.RootPath_1,'String',RootPath_1)
1134    set(handles.RootFile_1,'String',['/' RootFile_1]);
1135end
1136if  isequal(SubDir_1,'')
1137     set(handles.SubDir_1,'String','');
1138     FileBaseSub_1=FileBase_1;
1139else 
1140    set(handles.SubDir_1,'String',['/' SubDir_1]);
1141    FileBaseSub_1=fullfile(FileBase_1,SubDir_1);
1142end
1143indices=filename_new(length(FileBaseSub_1)+1:end);
1144indices(end-length(FileExt_1)+1:end)=[]; %remove extension
1145set(handles.FileIndex_1,'String',indices)
1146set(handles.FileIndex_1,'UserData',NomType_1)
1147set(handles.FileExt_1,'String',FileExt_1);
1148
1149% default choice of fields
1150if isequal(ext_test,'.image')
1151   set(handles.Fields_1,'String',{'';'image';'get_field...';'velocity';'vort';'div';'more...'})
1152   set(handles.Fields_1,'Value',2) % set menu to 'image'
1153elseif isequal(FileExt_1,'.nc')|isequal(FileExt_1,'.cdf')
1154   Data=nc2struct(fileinput_1,[]);
1155   if isfield(Data,'absolut_time_T0')
1156       set(handles.Fields_1,'String',{'';'image';'get_field...';'velocity';'vort';'div';'more...'})
1157       set(handles.Fields_1,'Value',4) % set menu to 'velocity'
1158   else   
1159       set(handles.Fields_1,'Value',2) % set menu to 'get_field...'
1160       set(handles.Fields_1,'String',{'';'get_field...'});
1161   end
1162end 
1163set(handles.SubField,'Visible','on')
1164set(handles.SubField,'Value',1)
1165RootPath_1_Callback(hObject,eventdata,handles); 
1166
1167
1168%-----------------------------------------------------
1169function RootPath_1_Callback(hObject,eventdata,handles)
1170update_rootinfo_1(hObject,eventdata,handles)
1171
1172%-------------------------------------------------------------------
1173function RootFile_1_Callback(hObject, eventdata, handles)
1174update_rootinfo_1(hObject,eventdata,handles)
1175%-------------------------------------------------------------------
1176
1177%-------------------------------------------------------------------
1178function update_rootinfo_1(hObject,eventdata,handles) %A REVOIR
1179
1180set(handles.RootPath_1,'BackgroundColor',[1 1 0])% indicate active program by yellow color
1181drawnow
1182%huvmat=get(handles.RootPath,'parent');
1183UvData=get(handles.uvmat,'UserData');%huvmat=handles of the uvmat interface
1184UvData.NewSeries=1; %flag for run0: begin a new series
1185
1186[FileName,RootPath,FileBase,FileIndices,FileExt]=read_file_boxes_1(handles);
1187if ~exist(FileName,'file')
1188    msgbox_uvmat('ERROR',['input file ' FileName ' not found']);
1189end
1190nbfield_1=[];%default
1191nburst_1=[];%default
1192XmlData.Time=[];
1193XmlData.GeometryCalib=[];%default
1194TimeUnit=[];
1195if isfield(UvData,'TimeUnit')
1196    TimeUnit=UvData.TimeUnit;
1197end
1198TimeUnit_1=[];
1199% testima=0; %test for image input
1200if isequal(lower(FileExt),'.avi') %.avi file
1201%     testima=1;
1202    info=aviinfo([FileBase FileIndices FileExt]);
1203    nbfield_1=info.NumFrames;
1204    nburst_1=1;
1205    %set(handles.last_i,'String',num2str(info.NumFrames));%fills last_field box
1206    set(handles.Dt_txt,'String',['Dt=' num2str(1000/info.FramesPerSecond) 'ms']);%display the elementary time interval in millisec
1207%     set(handles.dt,'UserData',info.FramesPerSecond);
1208    time=[0:1/info.FramesPerSecond:(info.NumFrames-1)/info.FramesPerSecond]';
1209    npx=get(handles.npx,'String');
1210    npy=get(handles.npy,'String');
1211    if isequal(npx,'') || isequal(npy,'')
1212        set(handles.npx,'String',num2str(info.Width));%fills nbre of pixels x box
1213        set(handles.npy,'String',num2str(info.Height));%fills nbre of pixels y box
1214    end
1215elseif ~isempty(imformats(FileExt(2:end))) %&& isequal(NomType,'*')% multi-frame image
1216    testima=1;
1217    imainfo=imfinfo([FileBase FileIndices FileExt]); 
1218    if length(imainfo) >1 %case of image with multiple frames
1219        nbfield_1=length(imainfo);
1220        nburst_1=1;
1221        %set(handles.last_i,'String',num2str(length(imainfo)));%fills last_field box
1222    end       
1223else
1224    %set(handles.last_i,'String','');%fills last_field box
1225    set(handles.npx,'String','');%fills nbre of pixels x box
1226    set(handles.npy,'String','');%fills nbre of pixels y box
1227end
1228
1229% find scaling parameters
1230filexml=[FileBase '.xml'];
1231fileciv=[FileBase '.civ'];
1232warntext='';%default warning text
1233if exist(filexml,'file')
1234    [XmlData,warntext]=imadoc2struct(filexml);
1235    if ~isempty(warntext)
1236        msgbox_uvmat('WARNING',warntext)
1237    end
1238    if isfield(XmlData,'Camera')
1239%         if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
1240%             NbSlice=XmlData.Camera.NbSlice;
1241%         end
1242        if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
1243            TimeUnit=XmlData.Camera.TimeUnit;
1244        end
1245    end
1246elseif exist(fileciv,'file')% if .civ file found
1247    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
1248    GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
1249    GeometryCalib.Tx=0;
1250    GeometryCalib.Ty=0;
1251    GeometryCalib.Tz=1;
1252    GeometryCalib.dpx=1;
1253    GeometryCalib.dpy=1;
1254    GeometryCalib.sx=1;
1255    GeometryCalib.Cx=0;
1256    GeometryCalib.Cy=0;
1257    GeometryCalib.f=1;
1258    GeometryCalib.kappa1=0;
1259    GeometryCalib.CoordUnit='cm';
1260    XmlData.GeometryCalib=GeometryCalib;
1261    if error==2, warntext=['no file ' FileBase '.civ'];
1262    elseif error==1, warntext='inconsistent number of fields in the .civ file';
1263    end
1264   
1265    set(handles.npx,'String',num2str(npx));%fills nbre of pixels x box
1266    set(handles.npy,'String',num2str(npy));%fills nbre of pixels y box
1267    set(handles.pxcm,'String',num2str(pxcmx));%fills scale x (pixel/cm) box
1268    set(handles.pycm,'String',num2str(pxcmy));%fills scale y (pixel/cm) box
1269    set(handles.pxcm,'Visible','on');%fills scale x (pixel/cm) box
1270    set(handles.pycm,'Visible','on');%fills scale y (pixel/cm) box
1271end   
1272if ~isempty(TimeUnit_1) && ~isequal(TimeUnit_1,TimeUnit)
1273        msgbox_uvmat('WARNING','the time units for the second series differs from the first one')
1274end
1275       
1276% store last index in handles.lat_i and .last_j
1277if ~isempty(XmlData.Time)
1278    nbfield_1=size(XmlData.Time,1);
1279    nburst_1=size(XmlData.Time,2);   
1280end
1281last_i_cell=get(handles.last_i,'String');
1282if isempty(nbfield_1)
1283    last_i_cell{2}='';
1284else
1285    last_i_cell{2}=num2str(nbfield_1);
1286end
1287set(handles.last_i,'String',last_i_cell)
1288last_j_cell=get(handles.last_j,'String');
1289if isempty(nburst_1)
1290     last_j_cell{2}='';
1291else
1292     last_j_cell{2}=num2str(nburst_1);
1293end
1294set(handles.last_j,'String',last_j_cell);
1295if ~isequal(last_i_cell{1},last_i_cell{2}) || ~isequal(last_j_cell{1},last_j_cell{2})
1296        msgbox_uvmat('WARNING','the numbers of input file of the second series differs from the first one')
1297end
1298
1299% store calibration data
1300GeometryCalib=XmlData.GeometryCalib;
1301if isempty(GeometryCalib)
1302    if isfield(UvData, 'GeometryCalib_1')
1303        UvData=rmfield(UvData,'GeometryCalib_1');
1304    end
1305else
1306    UvData.GeometryCalib_1=GeometryCalib;
1307    if (isfield(GeometryCalib,'R')& ~isequal(GeometryCalib.R(2,1),0) & ~isequal(GeometryCalib.R(1,2),0)) |...
1308        (isfield(GeometryCalib,'kappa1')& ~isequal(GeometryCalib.kappa1,0))
1309        set(handles.pxcm,'String','var')
1310        set(handles.pycm,'String','var')
1311    else
1312        pixcmx=GeometryCalib.f*GeometryCalib.R(1,1)*GeometryCalib.sx/(GeometryCalib.Tz*GeometryCalib.dpx);
1313        pixcmy=GeometryCalib.f*GeometryCalib.R(2,2)/(GeometryCalib.Tz*GeometryCalib.dpy);
1314        set(handles.pxcm,'String',num2str(pixcmx))
1315        set(handles.pycm,'String',num2str(pixcmy))
1316    end
1317end
1318UvData.XmlData_1=XmlData;
1319set(handles.uvmat,'UserData',UvData)%update the data attached to the uvmat interface
1320
1321if ~isequal(warntext,'')
1322    msgbox_uvmat('WARNING',warntext)
1323end
1324
1325set(handles.RootPath_1,'BackgroundColor',[1 1 1])% signa the end the input operation
1326drawnow
1327
1328set_scan_options(hObject, eventdata, handles)
1329
1330%---------------------------------------------------
1331% switch file index scanning options scan_i and scan_j in an exclusive way
1332function scan_i_Callback(hObject, eventdata, handles)
1333%---------------------------------------------------
1334if get(handles.scan_i,'Value')==1
1335    set(handles.scan_i,'BackgroundColor',[1 1 0])
1336    set(handles.scan_j,'Value',0)
1337%     set(handles.scan_j,'BackgroundColor',[0.831 0.816 0.784])
1338else
1339    set(handles.scan_i,'BackgroundColor',[0.831 0.816 0.784])
1340    set(handles.scan_j,'Value',1)
1341%     set(handles.scan_j,'BackgroundColor',[1 1 0])
1342end
1343scan_j_Callback(hObject, eventdata, handles)
1344
1345%-------------------------------------------------------------------
1346% switch file index scanning options scan_i and scan_j in an exclusive way
1347function scan_j_Callback(hObject, eventdata, handles)
1348%-------------------------------------------------------------------
1349if get(handles.scan_j,'Value')==1
1350    set(handles.scan_j,'BackgroundColor',[1 1 0])
1351    set(handles.scan_i,'Value',0)
1352    set(handles.scan_i,'BackgroundColor',[0.831 0.816 0.784])
1353    NomType=get(handles.FileIndex,'UserData');
1354    switch NomType
1355    case {'_i_j1-j2','#_ab','%3dab'},% pair with j index
1356        set(handles.fix_pair,'Visible','on')% option fixed pair on/off made visible (choice of avaible pair with buttons + and - if ='off')
1357    otherwise
1358        set(handles.fix_pair,'Visible','off')
1359    end
1360else
1361    set(handles.scan_j,'BackgroundColor',[0.831 0.816 0.784])
1362    set(handles.scan_i,'Value',1)
1363    set(handles.scan_i,'BackgroundColor',[1 1 0])
1364    set(handles.fix_pair,'Visible','off')
1365end
1366
1367%-------------------------------------------------------------------
1368function i1_Callback(hObject, eventdata, handles)
1369%-------------------------------------------------------------------
1370% [filebase,num1,num_a,num2,num_b,ext,NomType,subdir]=read_input_file(handles);
1371%[FileName,RootPath,filebase,xx,FileExt]=read_file_boxes(handles);
1372NomType=get(handles.FileIndex,'UserData');
1373num1=stra2num(get(handles.i1,'String'));
1374num2=stra2num(get(handles.i2,'String'));
1375num_a=stra2num(get(handles.j1,'String'));
1376num_b=stra2num(get(handles.j2,'String'));
1377indices=name_generator('',num1,num_a,'',NomType,1,num2,num_b,'');
1378set(handles.FileIndex,'String',indices)
1379if get(handles.SubField,'Value')==1
1380    NomType_1=get(handles.FileIndex_1,'String');
1381     FileExt_1=get(handles.FileExt_1,'String');
1382    [P,F,str1,str2,str_a,str_b,Ext,NomType_1]=name2display(['xx' NomType_1 FileExt_1]);
1383     indices=name_generator('',num1,num_a,'',NomType_1,1,num2,num_b,'');
1384     set(handles.FileIndex_1,'String',indices)
1385end
1386run0_Callback(hObject, eventdata, handles)
1387
1388%-------------------------------------------------------------------
1389function i2_Callback(hObject, eventdata, handles)
1390i1_Callback(hObject, eventdata, handles)
1391%-------------------------------------------------------------------
1392
1393%-------------------------------------------------------------------
1394function j1_Callback(hObject, eventdata, handles)
1395i1_Callback(hObject, eventdata, handles)
1396%-------------------------------------------------------------------
1397
1398%-------------------------------------------------------------------
1399function j2_Callback(hObject, eventdata, handles)
1400i1_Callback(hObject, eventdata, handles)
1401%-------------------------------------------------------------------
1402
1403%-------------------------------------------------------------------
1404function slices_Callback(hObject, eventdata, handles)
1405%-------------------------------------------------------------------
1406if get(handles.slices,'Value')==1
1407    set(handles.slices,'BackgroundColor',[1 1 0])
1408    set(handles.nb_slice,'Visible','on')
1409    set(handles.z_text,'Visible','on')
1410    set(handles.z_index,'Visible','on')
1411    nb_slice_Callback(hObject, eventdata, handles)
1412else
1413    set(handles.nb_slice,'Visible','off')
1414    set(handles.slices,'BackgroundColor',[0.7 0.7 0.7])
1415    set(handles.z_text,'Visible','off')
1416    set(handles.z_index,'Visible','off')
1417    set(handles.masklevel,'Value',1)
1418    set(handles.masklevel,'String',{'1'})
1419end
1420
1421%-------------------------------------------------------------------
1422function nb_slice_Callback(hObject, eventdata, handles)
1423%-------------------------------------------------------------------
1424nb_slice_str=get(handles.nb_slice,'String');
1425if isequal(nb_slice_str,'volume')
1426    num=stra2num(get(handles.j1,'String'));
1427    last_j=get(handles.last_j,'String');
1428    nbslice=str2double(last_j{1});
1429else
1430    num=str2double(get(handles.i1,'String'));
1431    nbslice=str2double(get(handles.nb_slice,'String'));
1432end
1433z=mod(num-1,nbslice)+1;
1434set(handles.z_index,'String',num2str(z))
1435for ilist=1:nbslice
1436    list_index{ilist,1}=num2str(ilist);
1437end   
1438set(handles.masklevel,'String',list_index)
1439set(handles.masklevel,'Value',z)
1440
1441
1442%-------------------------------------------------------------------
1443% --- Executes on button press in view_xml.
1444function view_xml_Callback(hObject, eventdata, handles)
1445[FileName,RootPath,FileBase,FileIndices,FileExt]=read_file_boxes(handles);
1446option=get(handles.view_xml,'String');
1447if isequal(option,'view .xml')
1448    FileXml=[FileBase '.xml'];
1449    heditxml=editxml(FileXml);
1450end
1451
1452%-------------------------------------------------------------------
1453% --- Executes on button press in mask_test.
1454function mask_test_Callback(hObject, eventdata, handles)
1455%-------------------------------------------------------------------
1456if isequal(get(handles.mask_test,'Value'),1)
1457    [FF,RootPath,FileBase]=read_file_boxes(handles);
1458    num_i1=stra2num(get(handles.i1,'String'));
1459    num_j1=stra2num(get(handles.j1,'String'));
1460    currentdir=pwd; 
1461    cd(RootPath);
1462    maskfiles=dir('*_*mask_*.png');%look for a mask file
1463    cd(currentdir);%come back to the working directory
1464    mdetect=0;
1465    if isempty(maskfiles)
1466         msgbox_uvmat('ERROR','no mask file detected (format ..._xxmask_ii.png needed), use the menu bar Tools/Make mask')
1467         return
1468    end
1469    for ilist=1:length(maskfiles)
1470        maskname=maskfiles(ilist).name;% take the first mask file in the list
1471        [rr,ff,x1,x2,xa,xb,xext,Mask_NomType{ilist}]=name2display(maskname);
1472        if ~strcmp(Mask_NomType{ilist},Mask_NomType{1})
1473            msgbox_uvmat('ERROR',['inconsistent mask types ' Mask_NomType{1} Mask_NomType{ilist } ' coexist in the current image directory'])
1474            return
1475        end
1476        [Path2,Name,ext]=fileparts(maskname);
1477        Namedouble=double(Name);
1478        val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
1479        ind_mask=findstr('mask',Name);
1480        i=ind_mask-1;
1481        while val(i)==0 & i>0
1482            i=i-1;
1483        end
1484        nbmask_str=str2num(Name(i+1:ind_mask-1));
1485        if ~isempty(nbmask_str)
1486            nbslice(ilist)=nbmask_str; % number of different masks (slices)
1487        end
1488    end
1489    if isequal(min(nbslice),max(nbslice))
1490        nbslice=nbslice(1);
1491    else
1492        msgbox_uvmat('ERROR','several inconsistent mask sets coexist in the current image directory')
1493         return
1494    end
1495    if ~isempty(nbslice) && Name(i)=='_'
1496        Mask.Base=[FileBase Name(i:ind_mask+3)];
1497        Mask.NbSlice=nbslice;
1498        num_i1=mod(num_i1-1,nbslice)+1;
1499        Mask.NomType=Mask_NomType{1};
1500        [maskname,mdetect]=name_generator(Mask.Base,num_i1,num_j1,'.png',Mask.NomType);%
1501        mdetect=exist(maskname,'file');
1502        if mdetect
1503            set(handles.nb_slice,'String',Name(i+1:ind_mask-1));
1504            set(handles.nb_slice,'BackgroundColor',[1 1 0])
1505            set(handles.mask_test,'UserData',Mask); 
1506            set(handles.mask_test,'BackgroundColor',[1 1 0])
1507            if nbslice > 1
1508                set(handles.slices,'value',1)
1509                slices_Callback(hObject, eventdata, handles)
1510            end
1511        end
1512    end
1513    if mdetect==0
1514         msgbox_uvmat('ERROR','no mask file detected (format ..._xxmask_ii.png needed), use the menu bar Tools/Make mask')
1515         set(handles.mask_test,'Value',0)
1516         return
1517    end   
1518    update_mask(handles,num_i1,num_j1);
1519else
1520    MaskData=get(handles.mask_test,'UserData');
1521    if isfield(MaskData,'maskhandle') && ishandle(MaskData.maskhandle)
1522          delete(MaskData.maskhandle)   
1523    end
1524    set(handles.mask_test,'UserData',[])   
1525    %huvmat=get(handles.mask_test,'parent');
1526    UvData=get(handles.uvmat,'UserData');
1527    if isfield(UvData,'MaskName')
1528        UvData=rmfield(UvData,'MaskName');
1529        set(handles.uvmat,'UserData',UvData)
1530    end
1531    set(handles.mask_test,'BackgroundColor',[0.7 0.7 0.7])
1532end
1533
1534%-------------------------------------------------------------------
1535function update_mask(handles,num_i1,num_j1)
1536%-------------------------------------------------------------------
1537
1538MaskData=get(handles.mask_test,'UserData');
1539if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1540    uistack(MaskData.maskhandle,'top');
1541end
1542num_i1_mask=mod(num_i1-1,MaskData.NbSlice)+1;
1543[MaskName,mdetect]=name_generator(MaskData.Base,num_i1_mask,num_j1,'.png',MaskData.NomType);
1544huvmat=get(handles.mask_test,'parent');
1545UvData=get(huvmat,'UserData');
1546
1547%update mask image if the mask is new
1548if ~ (isfield(UvData,'MaskName') && isequal(UvData.MaskName,MaskName))
1549    UvData.MaskName=MaskName; %update the recorded name on UvData
1550    set(huvmat,'UserData',UvData);
1551    if mdetect==0
1552        if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1553            delete(MaskData.maskhandle)   
1554        end
1555    else
1556        %read mask image
1557        Mask.AName='image';
1558        Mask.A=imread(MaskName);
1559        npxy=size(Mask.A);
1560        Mask.AX=[0.5 npxy(2)-0.5];
1561        Mask.AY=[npxy(1)-0.5 0.5 ];
1562        Mask.CoordType='px';
1563        if isequal(get(handles.slices,'Value'),1)
1564           NbSlice=str2num(get(handles.nb_slice,'String'));
1565           num_i1=str2num(get(handles.i1,'String'));
1566           Mask.ZIndex=mod(num_i1-1,NbSlice)+1;
1567        end
1568        %px to phys or other transform on field
1569         menu_transform=get(handles.transform_fct,'String');
1570        choice_value=get(handles.transform_fct,'Value');
1571        transform_name=menu_transform{choice_value};%name of the transform fct  given by the menu 'transform_fct'
1572        transform_list=get(handles.transform_fct,'UserData');
1573        transform=transform_list{choice_value};
1574        if  ~isequal(transform_name,'') && ~isequal(transform_name,'px')
1575            if isfield(UvData,'XmlData') && isfield(UvData.XmlData,'GeometryCalib')%use geometry calib recorded from the ImaDoc xml file as first priority
1576                Calib=UvData.XmlData.GeometryCalib;
1577                Mask=transform(Mask,UvData.XmlData);
1578            end
1579        end
1580        flagmask=Mask.A < 200;
1581       
1582        %make brown color image
1583        imflag(:,:,1)=0.9*flagmask;
1584        imflag(:,:,2)=0.7*flagmask;
1585        imflag(:,:,3)=zeros(size(flagmask));
1586       
1587        %update mask image
1588        hmask=[]; %default
1589        if isfield(MaskData,'maskhandle')&& ishandle(MaskData.maskhandle)
1590            hmask=MaskData.maskhandle;
1591        end
1592        if ~isempty(hmask)
1593            set(hmask,'CData',imflag)   
1594            set(hmask,'AlphaData',flagmask*0.6)
1595            set(hmask,'XData',Mask.AX);
1596            set(hmask,'YData',Mask.AY);
1597%             uistack(hmask,'top')
1598        else
1599            axes(handles.axes3)
1600            hold on   
1601            MaskData.maskhandle=image(Mask.AX,Mask.AY,imflag,'Tag','mask','HitTest','off','AlphaData',0.6*flagmask);
1602%             set(MaskData.maskhandle,'AlphaData',0.6*flagmask)
1603            set(handles.mask_test,'UserData',MaskData)
1604        end
1605    end
1606end
1607
1608
1609%-------------------------------------------------------------------
1610function MenuExportFigure_Callback(hObject, eventdata, handles)
1611%-------------------------------------------------------------------
1612huvmat=get(handles.MenuExport,'parent');
1613UvData=get(huvmat,'UserData');
1614hfig=figure;
1615newaxes=copyobj(handles.axes3,hfig);
1616map=colormap(handles.axes3);
1617colormap(map);%transmit the current colormap to the zoom fig
1618colorbar
1619
1620%-------------------------------------------------------------------
1621%-------------------------------------------------------------------
1622% III - MAIN REFRESH FUNCTIONS : 'FRAME PLOT'
1623%-------------------------------------------------------------------
1624%-------------------------------------------------------------------
1625
1626%Executes on button press in runplus: make one step forward and call
1627%run0. The step forward is along the fields series 1 or 2 depending on
1628%the scan_i and scan_j check box (exclusive each other)
1629%-------------------------------------------------------------------
1630function runplus_Callback(hObject, eventdata, handles)
1631increment=str2num(get(handles.increment_scan,'String')); %get the field increment d
1632runpm(hObject,eventdata,handles,increment)
1633
1634%-------------------------------------------------------------------
1635%Executes on button press in runmin: make one step backward and call
1636%run0. The step backward is along the fields series 1 or 2 depending on
1637%the scan_i and scan_j check box (exclusive each other)
1638%-------------------------------------------------------------------
1639function runmin_Callback(hObject, eventdata, handles)
1640increment=-str2num(get(handles.increment_scan,'String')); %get the field increment d
1641runpm(hObject,eventdata,handles,increment)
1642
1643%-------------------------------------------------------------------
1644% -- Executes on button press in Movie: make a series of +> steps
1645function Movie_Callback(hObject, eventdata, handles)
1646%------------------------------------------------------------------
1647set(handles.Movie,'BackgroundColor',[1 1 0])%paint the command button in yellow
1648drawnow
1649increment=str2num(get(handles.increment_scan,'String')); %get the field increment d
1650set(handles.STOP,'Visible','on')
1651set(handles.speed,'Visible','on')
1652set(handles.speed_txt,'Visible','on')
1653set(handles.Movie,'BusyAction','queue')
1654testavi=0;
1655UvData=get(handles.uvmat,'UserData');
1656
1657while get(handles.speed,'Value')~=0 & isequal(get(handles.Movie,'BusyAction'),'queue') % enable STOP command
1658        errormsg=runpm(hObject,eventdata,handles,increment);
1659        if ~isempty(errormsg)
1660            return
1661        end
1662        pause(1.02-get(handles.speed,'Value'))% wait for next image
1663end
1664if isfield(UvData,'aviobj') && ~isempty( UvData.aviobj),
1665    UvData.aviobj=close(UvData.aviobj);
1666   set(handles.uvmat,'UserData',UvData);
1667end
1668set(handles.Movie,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1669
1670%-------------------------------------------------------------------
1671% -- Executes on button press in Movie: make a series of <- steps
1672function MovieBackward_Callback(hObject, eventdata, handles)
1673%------------------------------------------------------------------
1674set(handles.MovieBackward,'BackgroundColor',[1 1 0])%paint the command button in yellow
1675drawnow
1676increment=-str2num(get(handles.increment_scan,'String')); %get the field increment d
1677set(handles.STOP,'Visible','on')
1678set(handles.speed,'Visible','on')
1679set(handles.speed_txt,'Visible','on')
1680set(handles.MovieBackward,'BusyAction','queue')
1681testavi=0;
1682UvData=get(handles.uvmat,'UserData');
1683
1684while get(handles.speed,'Value')~=0 & isequal(get(handles.MovieBackward,'BusyAction'),'queue') % enable STOP command
1685        errormsg=runpm(hObject,eventdata,handles,increment);
1686        if ~isempty(errormsg)
1687            return
1688        end
1689        pause(1.02-get(handles.speed,'Value'))% wait for next image
1690end
1691if isfield(UvData,'aviobj') && ~isempty( UvData.aviobj),
1692    UvData.aviobj=close(UvData.aviobj);
1693   set(handles.uvmat,'UserData',UvData);
1694end
1695set(handles.MovieBackward,'BackgroundColor',[1 0 0])%paint the command buttonback to red
1696
1697%-------------------------------------------------------------------
1698function STOP_Callback(hObject, eventdata, handles)
1699%-------------------------------------------------------------------
1700set(handles.movie_pair,'BusyAction','Cancel')
1701set(handles.movie_pair,'value',0)
1702set(handles.Movie,'BusyAction','Cancel')
1703set(handles.MovieBackward,'BusyAction','Cancel')
1704set(handles.MenuExportMovie,'BusyAction','Cancel')
1705
1706
1707%------------------------------------------------------------------
1708function errormsg=runpm(hObject,eventdata,handles,increment)
1709%------------------------------------------------------------------
1710%check for mùovie pair status
1711movie_status=get(handles.movie_pair,'Value');
1712if isequal(movie_status,1)
1713    STOP_Callback(hObject, eventdata, handles)%interrupt movie pair if active
1714end
1715%read the data on the current input rootfile(s)
1716
1717[FileName,RootPath,filebase,FileIndices,FileExt,subdir]=read_file_boxes(handles);
1718NomType=get(handles.FileIndex,'UserData');
1719
1720num1=stra2num(get(handles.i1,'String'));
1721num2=stra2num(get(handles.i2,'String'));
1722num_a=stra2num(get(handles.j1,'String'));
1723num_b=stra2num(get(handles.j2,'String'));
1724
1725sub_value= get(handles.SubField,'Value');
1726if sub_value % a second input file has been entered
1727    [FileName_1,RootPath_1,filebase_1,FileIndices_1,FileExt_1,SubDir_1]=read_file_boxes_1(handles);
1728    [pp,ff,str1,str2,str_a,str_b]=name2display(FileIndices_1);
1729    num1_1=stra2num(str1);%current set of indices for the second field (may be set different than the main indices)
1730    num2_1=stra2num(str2);
1731    num_a_1=stra2num(str_a);
1732    num_b_1=stra2num(str_b);
1733    NomType_1=get(handles.FileIndex_1,'UserData');
1734else
1735    filename_1=[];
1736end   
1737
1738comp_input=get(handles.fix_pair,'Value');
1739% if isequal(NomType,'_i1-i2')||isequal(NomType,'_i1-i2_j')
1740%     comp_input=1; %impose a fixed pair
1741%     set(handles.fix_pair,'Value',1)
1742% end
1743
1744%case of scanning along the first direction (rootfile numbers)
1745if get(handles.scan_i,'Value')==1% case of scanning along index i   
1746     num1=num1+increment;
1747     num2=num2+increment;
1748     [filename,num1,num_a,num2,num_b]=name_generator(filebase,num1,num_a,FileExt,NomType,comp_input,num2,num_b,subdir);
1749     if sub_value% set the second field name and indices
1750        num1_1=num1_1+increment;
1751        num2_1=num2_1+increment;
1752        filename_1=name_generator(filebase_1,num1_1,num_a_1,FileExt_1,NomType_1,1,num2_1,num_b_1,SubDir_1);
1753     end   
1754else % case of scanning along index j (burst numbers)
1755    num_a=num_a+increment;
1756    num_b=num_b+increment;
1757    if sub_value
1758        num_a_1=num_a_1+increment;
1759        num_b_1=num_b_1+increment;
1760        filename_1=name_generator(filebase_1,num1_1,num_a_1,FileExt_1,NomType_1,1,num2_1,num_b_1,SubDir_1);
1761%     else % redefine the j index if it exceeds its upper bound (only in the absence of a second field)
1762%         lastfield_cell=get(handles.last_j,'String'); % get the last field number
1763%         lastfield=str2double(lastfield_cell{1});
1764%         if num_a>lastfield && ~isnan(lastfield)
1765%             num_a=mod(num_a,lastfield);
1766%             num_b=mod(num_b,lastfield)
1767%         end
1768    end
1769    [filename,num1,num_a,num2,num_b]=name_generator(filebase,num1,num_a,FileExt,NomType,comp_input,num2,num_b,subdir);
1770end
1771
1772% refresh plots
1773errormsg=refresh_field(handles,filename,filename_1,num1,num2,num_a,num_b);
1774
1775if isempty(errormsg)  %update the index counters
1776    set(handles.i1,'String',num2stra(num1,NomType,1));
1777    if isequal(num2,num1)
1778         set(handles.i2,'String','');
1779    else
1780        set(handles.i2,'String',num2stra(num2,NomType,1));
1781    end
1782    set(handles.j1,'String',num2stra(num_a,NomType,2));
1783    if isequal(num_b,num_a)
1784         set(handles.j2,'String','');
1785    else
1786        set(handles.j2,'String',num2stra(num_b,NomType,2));
1787    end
1788    [indices]=name_generator('',num1,num_a,'',NomType,1,num2,num_b,'');
1789    set(handles.FileIndex,'String',indices);
1790    if ~isempty(filename_1)
1791         indices_1=name_generator('',num1_1,num_a_1,'',NomType_1,1,num2_1,num_b_1,'');
1792         set(handles.FileIndex_1,'String',indices_1);
1793    end
1794    if isequal(movie_status,1)
1795        set(handles.movie_pair,'Value',1)
1796        movie_pair_Callback(hObject, eventdata, handles); %reactivate moviepair if it was activated
1797    end
1798else
1799     msgbox_uvmat('ERROR',errormsg);
1800end
1801
1802
1803
1804%-------------------------------------------------------
1805% --- Executes on button press in movie_pair: create an alternating movie with two view
1806%-------------------------------------------------------
1807function movie_pair_Callback(hObject, eventdata, handles)
1808status=get(handles.movie_pair,'value');
1809if isequal(status,0)
1810    set(handles.movie_pair,'BusyAction','Cancel')%stop movie pair if button is 'off'
1811    set(handles.i2,'String','')
1812    set(handles.j2,'String','')
1813    return
1814else
1815    set(handles.movie_pair,'BusyAction','queue')
1816end
1817%initialisation
1818set(handles.movie_pair,'BackgroundColor',[1 1 0])%paint the command button in yellow
1819drawnow
1820list_fields=get(handles.Fields,'String');% list menu fields
1821index_fields=get(handles.Fields,'Value');% selected string index
1822FieldName=list_fields{index_fields}; % selected field
1823UvData=get(handles.uvmat,'UserData');
1824if isequal(FieldName,'image')
1825    test_1=0;
1826    [ff,rr,filebase,xx,Ext,SubDir]=read_file_boxes(handles);
1827    NomType=get(handles.FileIndex,'UserData');
1828else
1829    list_fields=get(handles.Fields_1,'String');% list menu fields
1830    index_fields=get(handles.Fields_1,'Value');% selected string index
1831    FieldName=list_fields{index_fields}; % selected field
1832    if isequal(FieldName,'image')
1833        test_1=1;
1834        [ff,rr,filebase,xx,Ext,SubDir]=read_file_boxes_1(handles);
1835        NomType=get(handles.FileIndex_1,'UserData');
1836    else
1837        msgbox_uvmat('ERROR','an image or movie must be first introduced as input')
1838        return
1839    end
1840end
1841
1842num_i1=stra2num(get(handles.i1,'String'));
1843num_j1=stra2num(get(handles.j1,'String'));
1844num_i2=stra2num(get(handles.i2,'String'));
1845num_j2=stra2num(get(handles.j2,'String'));
1846if isempty(num_j2)
1847    if isempty(num_i2)   
1848        msgbox_uvmat('ERROR', 'a second image index i2 or j2 is needed to show the pair as a movie')
1849        return
1850    else
1851        num_j2=num_j1;%repeat the index i1 by default
1852    end
1853end
1854if isempty(num_i2)
1855    num_i2=num_i1;%repeat the index i1 by default
1856end
1857imaname_1=name_generator(filebase,num_i2,num_j2,Ext,NomType);
1858if ~exist(imaname_1,'file')
1859      msgbox_uvmat('ERROR',['second input open (-)  ' imaname_1 ' not found']);
1860      return
1861end
1862% set(handles.i2,'String',''); % indicates that the second index i2 is not used
1863% set(handles.j2,'String',''); % indicates that the second index i2 is not used
1864
1865%read the second image
1866Field.AName='image';
1867if test_1
1868    Field_a=UvData.Field_1;
1869else
1870    Field_a=UvData.Field;
1871end
1872Field_b.AX=Field_a.AX;
1873Field_b.AY=Field_a.AY;
1874% z index
1875nbslice=str2double(get(handles.nb_slice,'String'));
1876if ~isempty(nbslice)
1877    Field_b.ZIndex=mod(num_i2-1,nbslice)+1;
1878end
1879Field_b.CoordType='px';
1880%determine the input file type
1881if (test_1 && isfield(UvData,'MovieObject_1'))||(~test_1 && isfield(UvData,'MovieObject'))
1882    FileType='movie';
1883elseif isequal(lower(Ext),'.avi')
1884    FileType='avi';
1885elseif isequal(lower(Ext),'.vol')
1886    FileType='vol';
1887else
1888   form=imformats(Ext([2:end]));
1889   if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
1890       if isequal(NomType,'*');
1891           FileType='multimage';
1892       else
1893           FileType='image';
1894       end
1895   end
1896end
1897switch FileType
1898        case 'movie'
1899            if test_1
1900                Field_b.A=read(UvData.MovieObject_1,num_i2);
1901            else
1902                Field_b.A=read(UvData.MovieObject,num_i2);
1903            end
1904        case 'avi'
1905            mov=aviread(imaname_1,num_i2);
1906            Field_b.A=frame2im(mov(1));
1907        case 'vol'
1908            Field_b.A=imread(imaname_1);
1909        case 'multimage'
1910            Field_b.A=imread(imaname_1,num_i2);
1911        case 'image'
1912            Field_b.A=imread(imaname_1);
1913end
1914if get(handles.slices,'Value')
1915    Field.ZIndex=str2double(get(handles.z_index,'String'));
1916end
1917
1918%px to phys or other transform on field
1919menu_transform=get(handles.transform_fct,'String');
1920choice_value=get(handles.transform_fct,'Value');
1921transform_name=menu_transform{choice_value};%name of the transform fct  given by the menu 'transform_fct'
1922transform_list=get(handles.transform_fct,'UserData');
1923transform=transform_list{choice_value};
1924if  ~isequal(transform_name,'') && ~isequal(transform_name,'px')
1925    if test_1 && isfield(UvData,'XmlData_1') && isfield(UvData.XmlData_1,'GeometryCalib')%use geometry calib recorded from the ImaDoc xml file as first priority
1926        Field_a=transform(Field_a,UvData.XmlData_1);%the first field has been stored without transform
1927        Field_b=transform(Field_b,UvData.XmlData_1);
1928    elseif ~test_1 && isfield(UvData,'XmlData') && isfield(UvData.XmlData,'GeometryCalib')%use geometry calib
1929        Field_b=transform(Field_b,UvData.XmlData);
1930    end
1931end
1932
1933 % make movie until movie speed is set to 0 or STOP is activated
1934hima=findobj(handles.axes3,'Tag','ima');% %handles.axes3 =main plotting window (A GENERALISER)
1935set(handles.STOP,'Visible','on')
1936set(handles.speed,'Visible','on')
1937set(handles.speed_txt,'Visible','on')
1938while get(handles.speed,'Value')~=0 && isequal(get(handles.movie_pair,'BusyAction'),'queue')%isequal(get(handles.run0,'BusyAction'),'queue'); % enable STOP command
1939    % read and plot the series of images in non erase mode
1940    set(hima,'CData',Field_b.A);
1941    pause(1.02-get(handles.speed,'Value'));% wait for next image
1942    set(hima,'CData',Field_a.A);
1943    pause(1.02-get(handles.speed,'Value'));% wait for next image
1944end
1945set(handles.movie_pair,'BackgroundColor',[1 0 0])%paint the command button in red
1946
1947%------------------------------------------------------------------------
1948% --- Executes on button press in run0.
1949function run0_Callback(hObject, eventdata, handles)
1950%------------------------------------------------------------------------
1951filename=read_file_boxes(handles);
1952
1953filename_1=[];%default
1954if get(handles.SubField,'Value')
1955    filename_1=read_file_boxes_1(handles);
1956end
1957num_i1=stra2num(get(handles.i1,'String'));
1958num_i2=stra2num(get(handles.i2,'String'));
1959num_j1=stra2num(get(handles.j1,'String'));
1960num_j2=stra2num(get(handles.j2,'String'));
1961errormsg=refresh_field(handles,filename,filename_1,num_i1,num_i2,num_j1,num_j2);
1962if ~isempty(errormsg)
1963      msgbox_uvmat('ERROR',errormsg);
1964end   
1965
1966%------------------------------------------------------------------------
1967% --- read the input files and refresh all the plots, including projection.
1968% OUTPUT:
1969%  errormsg: error message char string  =[] by default
1970% INPUT:
1971% filename: first input file (=[] in the absence of input file)
1972% filename_1: second input file (=[] in the asbsenc of secodn input file)
1973% num_i1,num_i2,num_j1,num_j2; frame indices
1974% Field: structure describing an optional input field (then replace the input file)
1975
1976function errormsg=refresh_field(handles,filename,filename_1,num_i1,num_i2,num_j1,num_j2,Field)
1977%------------------------------------------------------------------------
1978
1979%initialisation
1980set(handles.run0,'BackgroundColor',[1 1 0])%paint the command button in yellow
1981drawnow
1982errormsg=[]; % default error message
1983abstime=[];
1984abstime_1=[];
1985dt=[];
1986if ~exist('Field','var')
1987    Field={};
1988end
1989UvData=get(handles.uvmat,'UserData');
1990%UvData =structure containing information stored outside the uicontrol of uvmat
1991%      .NewSeries: =1 for a new series (new root fiel introduced), 0 else
1992%      .MovieObject: movie object representing an input movie
1993%      .MovieObject_1: idem for a second input series (_1)
1994%      .filename_1 : last second input file name (to deal with a constant second input without reading again the file)
1995%      .VelType_1: last velocity type (civ1, civ2...) for the second input series
1996%      .FieldName_1: last field name(velocity, vorticity...) for the second input series
1997%      .NbDim: number of space dimensions of the input field
1998%      .ZMin, .ZMax: range of the z coordinate
1999%..... to complement
2000
2001if ishandle(handles.UVMAT_title) %remove title panel on uvmat
2002    delete(handles.UVMAT_title)
2003end
2004
2005% determine the main input file information for action
2006FileType=[];%default
2007if ~isempty(filename)
2008    if ~exist(filename,'file')
2009        errormsg=['input file ' filename ' does not exist'];
2010        return
2011    end
2012    Ext=get(handles.FileExt,'String');
2013    NomType=get(handles.FileIndex,'UserData');
2014    %update the z position index
2015    nbslice_str=get(handles.nb_slice,'String');
2016    z_index=1;%default
2017    if isequal(nbslice_str,'volume')
2018        z_index=num_j1;
2019        set(handles.z_index,'String',num2str(z_index))
2020    else
2021        nbslice=str2num(nbslice_str);
2022        z_index=mod(num_i1-1,nbslice)+1;
2023        set(handles.z_index,'String',num2str(z_index))
2024    end
2025    % refresh menu for save_mask if relevant
2026    masknumber=get(handles.masklevel,'String');
2027    if length(masknumber)>=z_index
2028        set(handles.masklevel,'Value',z_index)
2029    end
2030
2031    % determine the input file type
2032    if isequal(Ext,'.nc')||isequal(Ext,'.cdf')
2033        FileType='netcdf';
2034    elseif isfield(UvData,'MovieObject')
2035        FileType='movie';
2036        FieldName='image';
2037    elseif isequal(lower(Ext),'.avi')
2038        FileType='avi';
2039        FieldName='image';
2040    elseif isequal(lower(Ext),'.vol')
2041        FileType='vol';
2042        FieldName='image';
2043    else
2044       form=imformats(Ext([2:end]));
2045       if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2046           if isequal(NomType,'*');
2047               FileType='multimage';
2048           else
2049               FileType='image';
2050           end
2051           FieldName='image';
2052       end
2053    end
2054else
2055    FileType='netcdf';
2056    FieldName='get_field...';
2057end
2058VelType=[];%default
2059if isequal(FileType,'netcdf')
2060    list_fields=get(handles.Fields,'String');% list menu fields
2061    index_fields=get(handles.Fields,'Value');% selected string index
2062    FieldName= list_fields{index_fields}; % selected field
2063    if ~isequal(FieldName,'get_field...')% read the field names on the interface get_field...
2064       VelType=setfield(handles);
2065    end
2066end
2067
2068% choose a second field if Subfield option is 'on'
2069FieldName_1=[];
2070scal_color=[];
2071VelType_1=setfield_1(handles);
2072sub_value=get(handles.SubField,'Value');
2073FileType_1='none';%default
2074if ~isempty(filename_1)
2075    % test for a constant second field (comparison with a fixed field)
2076    NomType_1=get(handles.FileIndex_1,'UserData');
2077    Ext_1=get(handles.FileExt_1,'String');
2078    % determine the input file type
2079    if isequal(Ext_1,'.nc')||isequal(Ext_1,'.cdf')
2080        FileType_1='netcdf';
2081    elseif isfield(UvData,'MovieObject_1')
2082        FileType_1='movie';
2083        FieldName_1='image';
2084    elseif isequal(lower(Ext_1),'.avi')
2085        FileType='avi';
2086        FieldName_1='image';
2087    elseif isequal(lower(Ext_1),'.vol')
2088        FileType_1='vol';
2089        FieldName_1='image';
2090    else
2091       form=imformats(Ext_1([2:end]));
2092       if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2093           if isequal(NomType_1,'*');
2094               FileType_1='multimage';
2095           else
2096               FileType_1='image';
2097           end
2098           FieldName_1='image';
2099       end
2100    end
2101    if ~isequal(FieldName_1,'image')
2102        list_fields=get(handles.Fields_1,'String');% list menu fields
2103        index_fields=get(handles.Fields_1,'Value');% selected string index
2104        FieldName_1= list_fields{index_fields}; % selected field
2105        if isequal(VelType_1,'*')% free veltype choice
2106            VelType_1=[];
2107        elseif isequal(VelType_1,'"')% veltype the same as for the first field
2108            if isempty(VelType)
2109                VelType_1=[];
2110            else
2111                VelType_1=VelType;
2112            end
2113        end
2114    end
2115    test_keepdata_1=0;% test for keeping the previous stored data if the input files are unchanged
2116    if ~isequal(NomType_1,'*')%in case of a series of files (not avi movie)
2117        if isfield(UvData,'filename_1')&& isfield(UvData,'VelType_1') && isfield(UvData,'FieldName_1')
2118            test_keepdata_1= isequal(filename_1,UvData.filename_1)&&...
2119                isequal(VelType_1,UvData.filename_1) && isequal(FieldName_1,UvData.FieldName_1);
2120        end
2121    end
2122    if test_keepdata_1
2123        Field{2}=UvData.Field_1;   
2124    elseif ~exist(filename_1,'file')
2125        errormsg=['second file ' filename_1 ' does not exist'];
2126        return
2127    end
2128end
2129
2130%read the input field(s)
2131%read images
2132if ~isempty(filename) && isequal(FieldName,'image')
2133     switch FileType
2134        case 'movie'
2135            try
2136                A=read(UvData.MovieObject,num_i1);
2137            catch
2138                errormsg=lasterr;
2139                return
2140            end
2141        case 'avi'
2142            try
2143                mov=aviread(filename,num_i1);
2144            catch
2145                errormsg=lasterr;
2146                return
2147            end
2148            A=frame2im(mov(1));
2149        case 'vol'
2150            A=imread(filename);
2151        case 'multimage'
2152            A=imread(filename,num_i1);
2153        case 'image'
2154            A=imread(filename);
2155    end
2156    npxy=size(A);
2157    set(handles.npx,'String',num2str(npxy(2)));% display image size on the interface
2158    set(handles.npy,'String',num2str(npxy(1)));
2159    Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
2160    Rangy=[npxy(1)-0.5 0.5]; %
2161    Field{1}.AName='image';
2162    Field{1}.ListVarName={'AY','AX','A'}; %
2163    if size(A,3)==3;%color
2164        Field{1}.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
2165    else
2166        Field{1}.VarDimName={'AY','AX',{'AY','AX'}}; %
2167    end
2168    Field{1}.AY=Rangy;
2169    Field{1}.AX=Rangx;
2170    Field{1}.A=A;
2171    Field{1}.CoordType='px'; %used for mouse_motion
2172    Field{1}.CoordUnit='pixel'; %used for mouse_motion
2173end
2174
2175%read a second image
2176if  ~isempty(filename_1) && ~test_keepdata_1 && isequal(FieldName_1,'image')
2177    switch FileType_1
2178        case 'movie'
2179            A=read(UvData.MovieObject_1,num_i1);
2180        case 'avi'
2181            mov=aviread(filename_1,num_i1);
2182            A=frame2im(mov(1));
2183        case 'vol'
2184            A=imread(filename_1);
2185        case 'multimage'
2186            A=imread(filename_1,num_i1);
2187        case 'image'
2188            A=imread(filename_1);
2189        case 'netcdf'
2190        otherwise
2191            errormsg=['unknown input file type ' filename_1];
2192    end
2193    npxy=size(A);
2194    set(handles.npx,'String',num2str(npxy(2)));% display image size on the interface
2195    set(handles.npy,'String',num2str(npxy(1)));
2196    Rangx=[0.5 npxy(2)-0.5]; % coordinates of the first and last pixel centers
2197    Rangy=[npxy(1)-0.5 0.5]; %
2198    Field{2}.AName='image';
2199    Field{2}.ListVarName={'AY','AX','A'}; %
2200    if size(A,3)==3;%color
2201        Field{2}.VarDimName={'AY','AX',{'AY','AX','rgb'}}; %
2202    else
2203        Field{2}.VarDimName={'AY','AX',{'AY','AX'}}; %
2204    end
2205    Field{2}.AY=Rangy;
2206    Field{2}.AX=Rangx;
2207    Field{2}.A=A;
2208    Field{2}.CoordType='px'; %used for mouse_motion
2209    Field{2}.CoordUnit='px'; %used for move_mou
2210end
2211
2212%read ncfile(s)
2213CivStage_1=0;%default
2214VelType_out_1=[];
2215InputField={FieldName};
2216InputField_1={FieldName_1};
2217if (~isempty(filename)&& isequal(FileType,'netcdf')) || (~isempty(filename_1)&& isequal(FileType_1,'netcdf')) ;
2218    %read the velocity field(s) from netcdf rootfile(s)
2219    list_code=get(handles.col_vec,'String');% list menu fields
2220    index_code=get(handles.col_vec,'Value');% selected string index
2221    scal_color= list_code{index_code(1)}; % selected field
2222    if isequal(FieldName,'velocity')&& ~isequal(scal_color,'black') && ~isequal(scal_color,'white')
2223        InputField=[InputField scal_color];
2224    end
2225    if isequal(FieldName_1,'velocity') && ~isequal(scal_color,'black') && ~isequal(scal_color,'white')
2226        InputField_1=[InputField_1 scal_color];
2227    end
2228    if isequal(FileType,'netcdf')  %read the first nc field
2229        if isequal(FieldName,'get_field...')% read the field names on the interface get_field.
2230            %VelType=get(handles.Fields,'UserData');
2231            hget_field=findobj(allchild(0),'Name','get_field');%find the get_field... GUI
2232            if isempty(hget_field)
2233                hget_field= get_field(filename);%open the get_field GUI   
2234            end
2235            hhget_field=guidata(hget_field);
2236            set(hhget_field.inputfile,'String',filename)% update the list of input fields in get_field
2237            set(hhget_field.ACTION,'Value',1)% PLOT option selected
2238            set(hhget_field.list_fig,'Value',2)% plotting axes =uvmat selected
2239            [Field{1},errormsg]=read_get_field(hget_field); %read the names of the variables to plot in the get_field GUI
2240            if ~isempty(errormsg)
2241                errormsg=['error in uvmat/run0_Callback/read_get_field: ' errormsg];
2242                return
2243            end
2244            CivStage=0;
2245            VelType_out=[];         
2246        else
2247            [Field{1},VelType_out]=read_civxdata(filename,InputField,VelType);
2248            if isfield(Field{1},'Txt')
2249                errormsg=Field{1}.Txt;
2250                return
2251            end
2252            CivStage=Field{1}.CivStage;
2253            UvData.NbDim=Field{1}.nb_dim;
2254        end
2255    end
2256    if ~isempty(filename_1) && ~test_keepdata_1 && isequal(FileType_1,'netcdf') %read the second file
2257        if isequal(FieldName_1,'get_field...')% read the field names on the interface get_field.
2258            hget_field=findobj(allchild(0),'Name','get_field_1');%find the get_field... GUI
2259             if isempty(hget_field)
2260                 hget_field= get_field(filename_1);%open the get_field GUI
2261                 set(hget_field,'name','get_field_1')
2262%                 enable_transform(handles,'off')% no field transform (possible transform in the GUI get_field)
2263             end
2264            hhget_field=guidata(hget_field);%handles of GUI elements in get_field
2265            SubField=get_field('read_var_names',hObject,eventdata,hhget_field); %read the names of the variables to plot in the get_field GUI
2266            [Field{2},var_detect]=nc2struct(filename_1,SubField.ListVarName); %read the corresponding input data               
2267            Field{2}.VarAttribute=SubField.VarAttribute;
2268            %update the display on get_field
2269            set(hhget_field.inputfile,'String',filename_1)
2270            set(hhget_field.variables,'Value',1)
2271            Tabchar={''};%default
2272            Tabcell=[];
2273            if isfield(Field{2},'ListGlobalAttribute')& ~isempty(Field{2}.ListGlobalAttribute)
2274                for iline=1:length(Field{2}.ListGlobalAttribute)
2275                    Tabcell{iline,1}=Field{2}.ListGlobalAttribute{iline};
2276                    if isfield(Field{2}, Field{2}.ListGlobalAttribute{iline})
2277                        eval(['val=Field{2}.' Field{2}.ListGlobalAttribute{iline} ';'])
2278                        if ischar(val);
2279                            Tabcell{iline,2}=val;
2280                        else
2281                            Tabcell{iline,2}=num2str(val);
2282                        end
2283                    end
2284                end
2285                if ~isempty(Tabcell)
2286                    Tabchar=cell2tab(Tabcell,'=');
2287                    Tabchar=[{''};Tabchar];
2288                end
2289            end
2290            set(hhget_field.attributes,'String',Tabchar);%update list of global attributes in get_field
2291        else
2292            [Field{2},VelType_out_1]=read_civxdata(filename_1,[],VelType_1);
2293            CivStage_1=Field{2}.CivStage;
2294        end
2295        if ~isequal(FileType,'netcdf')
2296            VelType_out=VelType_out_1;
2297        end
2298    end
2299end
2300
2301%store the second field for possible latter use
2302if numel(Field)==2
2303    UvData.Field_1=Field{2};
2304end
2305
2306%update the display buttons for the first velocity type (first menuline)
2307veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
2308if ~isequal(FileType,'netcdf')
2309    reset_vel_type(veltype_handles)
2310elseif isempty(VelType) && ~isequal(FieldName,'get_field...')
2311    set_veltype_display(veltype_handles,CivStage)%update the display of available velocity types for the first field
2312    if isempty(VelType_out)
2313        reset_vel_type(veltype_handles)
2314    else
2315        handle1=eval(['handles.' VelType_out]);
2316        reset_vel_type(veltype_handles,handle1)
2317    end
2318end
2319
2320%update the display buttons for the second velocity type (second menuline)
2321veltype_handles_1=[handles.civ1_1 handles.interp1_1 handles.filter1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1];
2322if ~isequal(FileType_1,'netcdf')
2323    reset_vel_type(veltype_handles_1)
2324elseif isempty(VelType_1) && ~isequal(FieldName_1,'get_field...')
2325    set_veltype_display(veltype_handles_1,CivStage_1)%update the display of available velocity types for the first field
2326    if isempty(VelType_out_1)
2327        reset_vel_type(veltype_handles_1)
2328    else
2329        handle1=eval(['handles.' VelType_out_1 '_1']);
2330        reset_vel_type(veltype_handles_1,handle1)
2331    end
2332end
2333
2334%introduce w as background image by default for a new series (only for nbdim=2)
2335if ~isfield(UvData,'NewSeries')
2336    UvData.NewSeries=1;
2337end
2338%put W as background image by default if NbDim=2:
2339if ~isfield(UvData,'NbDim')||isempty(UvData.NbDim)||~isequal(UvData.NbDim,3)
2340    if UvData.NewSeries && isequal(get(handles.SubField,'Value'),0) && isfield(Field{1},'W') && ~isempty(Field{1}.W);
2341        set(handles.SubField,'Value',1);
2342        %menu=update_menu(handles.Fields_1,'w');%update the menu for the background scalar nd set the choice to 'w'
2343        set(handles.RootPath_1,'String','"')
2344        set(handles.RootFile_1,'String','"')
2345        set(handles.SubDir_1,'String','"');
2346        [indices]=name_generator('',num_i1,num_j1,'',NomType,1,num_i2,num_j2,'');
2347        set(handles.FileIndex_1,'String',indices)
2348        set(handles.FileExt_1,'String','"');
2349        set(handles.Fields_1,'Visible','on');
2350        set(handles.Fields_1,'Visible','on');
2351        set(handles.RootPath_1,'Visible','on')
2352        set(handles.RootFile_1,'Visible','on')
2353        set(handles.SubDir_1,'Visible','on');
2354        set(handles.FileIndex_1,'Visible','on');
2355        set(handles.FileExt_1,'Visible','on');
2356        set(handles.Fields_1,'Visible','on');
2357        Field{1}.AName='w';
2358        testscal=1;
2359    end
2360end           
2361
2362%multislice case
2363if ~isempty(filename) &&(~isfield(UvData,'NbDim') || isequal(UvData.NbDim,2))&&...%2D case
2364      isfield(UvData,'XmlData') && isfield(UvData.XmlData,'GeometryCalib')&& isfield(UvData.XmlData.GeometryCalib,'SliceCoord')
2365       siz=size(UvData.XmlData.GeometryCalib.SliceCoord);
2366       if siz(1)>1
2367           NbSlice=siz(1);
2368%            set(handles.slices,'Visible','on')
2369%            set(handles.slices,'Value',1)
2370       else
2371           NbSlice=1;
2372       end
2373       %set(handles.nb_slice,'String',num2str(NbSlice))
2374%        slices_Callback(handles.uvmat, [], handles)
2375end
2376
2377%store the current open names, fields and vel types in uvmat interface
2378UvData.filename=filename;
2379UvData.filename_1=filename_1;
2380UvData.VelType=VelType;
2381UvData.VelType_1=VelType_1;
2382UvData.FieldName=FieldName;
2383UvData.FieldName_1=FieldName_1;
2384if ~isempty(scal_color)
2385    UvData.CName=scal_color;
2386end
2387
2388%coordinate transform or user fct
2389XmlData=[];%default
2390if isfield(UvData,'XmlData')%use geometry calib recorded from the ImaDoc xml file as first priority
2391    XmlData=UvData.XmlData;
2392end
2393XmlData_1=[];%default
2394if isfield(UvData,'XmlData_1')
2395   XmlData_1=UvData.XmlData_1;
2396end
2397menu_transform=get(handles.transform_fct,'String');
2398choice_value=get(handles.transform_fct,'Value');
2399transform_list=get(handles.transform_fct,'UserData');
2400transform=transform_list{choice_value};%selected function handles
2401
2402% z index
2403if ~isempty(filename)
2404    Field{1}.ZIndex=z_index;
2405end
2406
2407%px to phys or other transform on field
2408if ~isempty(transform)
2409    if length(Field)>=2
2410        Field{2}.ZIndex=z_index;
2411        [Field{1},Field{2}]=transform(Field{1},XmlData,Field{2},XmlData_1);
2412        if isempty(Field{2})
2413            Field(2)=[];
2414        end
2415    else
2416        Field{1}=transform(Field{1},XmlData);
2417    end
2418end
2419
2420%calculate scalar
2421if isequal(FileType,'netcdf') && ~isequal(FieldName,'get_field...')%
2422    Field{1}=calc_field(InputField,Field{1});
2423end
2424if length(Field)==2 && ~test_keepdata_1 && isequal(FileType_1,'netcdf') && ~isequal(FieldName_1,'get_field...')
2425    Field{2}=calc_field(InputField_1,Field{2});
2426end
2427
2428% combine the two input fields (e.g. substract velocity fields)
2429if numel(Field)==2
2430   UvData.Field=sub_field(Field{1},Field{2}); %TO UPDATE FOR MORE GENERAL INPUT 
2431else
2432   UvData.Field=Field{1};
2433end
2434
2435% test 3D , default projection menuplane and typical mesh (needed to menuopen set_object)
2436test_x=0;
2437test_z=0;% test for unstructured z coordinate
2438UvData.ZMax=0;
2439UvData.ZMin=0;%default
2440UvData.Mesh=1; %default
2441[UvData.Field,errormsg]=check_field_structure(UvData.Field);
2442if ~isempty(errormsg)
2443    errormsg=['error in uvmat/run0_Callback/check_field_structure: ' errormsg];
2444    return
2445end
2446[CellVarIndex,NbDim,VarType]=find_field_indices(UvData.Field);
2447[NbDim,imax]=max(NbDim);
2448if isempty(imax)
2449%    DimVarIndex=0;   
2450    coord_x=[];
2451else
2452%     VarIndex=CellVarIndex{imax};
2453    coord_x=VarType{imax}.coord_x;
2454end
2455if isfield(UvData,'NbDim') && ~isempty(UvData.NbDim)
2456    NbDim=UvData.NbDim;
2457else 
2458    UvData.NbDim=NbDim;
2459end
2460if ~isempty(CellVarIndex) && ~isempty(VarType{imax}.coord_x)  && ~isempty(VarType{imax}.coord_y)    %unstructured coordinate z
2461    XName=UvData.Field.ListVarName{VarType{imax}.coord_x};
2462    YName=UvData.Field.ListVarName{VarType{imax}.coord_y};
2463    test_x=1;
2464elseif isfield(UvData.Field,'X') && isfield(UvData.Field,'Y')
2465    XName='X';
2466    YName='Y';
2467    test_x=1;
2468end
2469if test_x
2470    eval(['UvData.XMax=max(UvData.Field.' XName ');'])
2471    eval(['UvData.XMin=min(UvData.Field.' XName ');'])
2472    eval(['UvData.YMax=max(UvData.Field.' YName ');'])
2473    eval(['UvData.YMin=min(UvData.Field.' YName ');'])
2474    eval(['nbvec=length(UvData.Field.' XName ');'])
2475    if NbDim==3%
2476        if ~isempty(CellVarIndex) && ~isempty(VarType{imax}.coord_z)%unstructured coordinate z
2477            ZName=UvData.Field.ListVarName{VarType{imax}.coord_z};
2478            eval(['UvData.ZMax=max(UvData.Field.' ZName ');'])
2479            eval(['UvData.ZMin=min(UvData.Field.' ZName ');'])
2480            test_z=1;   
2481        elseif isfield(UvData,'Z')% usual civ data
2482            UvData.ZMax=max(UvData.Z);
2483            UvData.ZMin=min(UvData.Z);
2484            test_z=1;
2485        end
2486    end
2487    if isequal(UvData.ZMin,UvData.ZMax)%no z dependency
2488        NbDim=2;
2489        test_z=0;
2490    end   
2491    if test_z
2492         UvData.Mesh=((UvData.XMax-UvData.XMin)*(UvData.YMax-UvData.YMin)*(UvData.ZMax-UvData.ZMin))/nbvec;% volume per vector
2493         UvData.Mesh=(UvData.Mesh)^(1/3);
2494    else
2495        UvData.Mesh=sqrt((UvData.XMax-UvData.XMin)*(UvData.YMax-UvData.YMin)/nbvec);%2D
2496    end
2497end
2498%case of structured coordinates
2499if isfield(UvData.Field,'AX') & isfield(UvData.Field,'AY')& isfield(UvData.Field,'A')
2500    UvData.XMax=max(UvData.Field.AX);
2501    UvData.XMin=min(UvData.Field.AX);
2502    UvData.YMax=max(UvData.Field.AY);
2503    UvData.YMin=min(UvData.Field.AY);
2504    np_A=size(UvData.Field.A);
2505    UvData.Mesh=sqrt((UvData.XMax-UvData.XMin)*(UvData.YMax-UvData.YMin)/((np_A(1)-1) * (np_A(2)-1))) ;
2506end
2507if  isempty(coord_x)&~isempty(CellVarIndex)
2508    VarIndex=CellVarIndex{imax}; % list of variable indices
2509    DimIndex=UvData.Field.VarDimIndex{VarIndex(1)}; %list of dim indices for the variable
2510    if NbDim==3
2511        nbpoints=UvData.Field.DimValue(DimIndex(1));
2512        if isfield(VarType{imax},'coord_3')&& ~isequal(VarType{imax}.coord_3,0) % z is a dimension variable
2513            ZName=UvData.Field.ListVarName{VarType{imax}.coord_3};
2514            eval(['UvData.ZMax=max(UvData.Field.' ZName ');'])
2515            eval(['UvData.ZMin=min(UvData.Field.' ZName ');'])
2516        else
2517            testcoord_z=0;
2518            if length(UvData.Field.VarAttribute)>=VarIndex(1)
2519                if isfield(UvData.Field.VarAttribute{VarIndex(1)},'Coord_1')%regular grid
2520                    Coord_z=UvData.Field.VarAttribute{VarIndex(1)}.Coord_1;
2521                    UvData.ZMax=max(Coord_z);
2522                    UvData.ZMin=min(Coord_z);
2523                    testcoord_z=1;
2524                end
2525            end
2526            if ~testcoord_z
2527                  UvData.ZMin=1;
2528                  UvData.ZMax=UvData.Field.DimValue(DimIndex(1));
2529            end
2530        end
2531        UvData.Mesh=(UvData.ZMax-UvData.ZMin)/(nbpoints-1);
2532    elseif NbDim==2
2533        nbpoints_y=UvData.Field.DimValue(DimIndex(1));       
2534        Yvar=VarType{imax}.coord_y;
2535        if Yvar~=0  % x is a dimension variable
2536            YName=UvData.Field.ListVarName{Yvar};
2537            eval(['UvData.YMax=max(UvData.Field.' YName ');'])
2538            eval(['UvData.YMin=min(UvData.Field.' YName ');'])
2539        else
2540            testcoord_y=0;
2541            if ~testcoord_y
2542                  UvData.YMin=1;
2543                  UvData.YMax=UvData.Field.DimValue(DimIndex(1));
2544            end
2545        end
2546        DY=(UvData.YMax-UvData.YMin)/(nbpoints_y-1);
2547        nbpoints_x=UvData.Field.DimValue(DimIndex(2));
2548        Xvar=VarType{imax}.coord_x;
2549        if Xvar~=0  % x is a dimension variable
2550            XName=UvData.Field.ListVarName{Xvar};
2551            eval(['UvData.XMax=max(UvData.Field.' XName ');'])
2552            eval(['UvData.XMin=min(UvData.Field.' XName ');'])
2553        else
2554            testcoord_x=0;
2555            if ~testcoord_x
2556                  UvData.XMin=1;
2557                  UvData.XMax=UvData.Field.DimValue(DimIndex(2));
2558            end
2559        end
2560        DX=(UvData.XMax-UvData.XMin)/(nbpoints_x-1);
2561        UvData.Mesh= sqrt(DX*DY);
2562    end
2563end
2564
2565%create a default projection menuplane
2566UvData.Object{1}.Style='plane';%main plotting plane
2567UvData.Object{1}.ProjMode='projection';%main plotting plane
2568if ~isfield(UvData.Object{1},'plotaxes')
2569    UvData.Object{1}.plotaxes=handles.axes3;%default plotting axis
2570    set(handles.list_object_1,'Value',1);
2571    set(handles.list_object_1,'String',{'1-PLANE'}); 
2572end
2573
2574%3D case (menuvolume)
2575if NbDim==3 && UvData.NewSeries
2576    UvData.Object{1}.NbDim=UvData.NbDim;%test for 3D objects
2577    UvData.Object{1}.RangeZ=UvData.Mesh;%main plotting plane
2578    UvData.Object{1}.Coord(1,3)=(UvData.ZMin+UvData.ZMax)/2;%section at a middle plane chosen
2579    UvData.Object{1}.Phi=0;
2580    UvData.Object{1}.Theta=0;
2581    UvData.Object{1}.Psi=0;
2582    UvData.Object{1}.HandlesDisplay=plot(0,0,'Tag','proj_object');% A REVOIR 
2583    PlotHandles=get_plot_handles(handles);
2584    ZBounds(1)=UvData.ZMin; %minimum for the Z slider
2585    ZBounds(2)=UvData.ZMax;%maximum for the Z slider
2586    UvData.Object{1}.Name='1-PLANE';
2587    UvData.Object{1}.enable_plot=1;
2588    set_object(UvData.Object{1},PlotHandles,ZBounds);
2589    set(handles.list_object_1,'Value',1);
2590%multilevel case (single menuplane in a 3D space)
2591elseif isfield(UvData,'Z')
2592    if isfield(UvData,'CoordType')& isequal(UvData.CoordType,'phys') & isfield(UvData,'XmlData')
2593        XmlData=UvData.XmlData;
2594        if isfield(XmlData,'PlanePos')
2595             UvData.Object{1}.Coord=XmlData.PlanePos(UvData.ZIndex,:);
2596        end
2597        if isfield(XmlData,'PlaneAngle')
2598            siz=size(XmlData.PlaneAngle);
2599            indangle=min(siz(1),UvData.ZIndex);%take first angle if a single angle is defined (translating scanning)             
2600            UvData.Object{1}.Phi=XmlData.PlaneAngle(indangle,1);
2601            UvData.Object{1}.Theta=XmlData.PlaneAngle(indangle,2);
2602            UvData.Object{1}.Psi=XmlData.PlaneAngle(indangle,3);
2603        end
2604    elseif isfield(UvData,'ZIndex')
2605        UvData.Object{1}.ZObject=UvData.ZIndex;
2606    end
2607end
2608
2609%Plot the projections on all existing  projection objects
2610keeplim=get(handles.FixedLimits,'Value');
2611%reset the min and max of scalar if only the mask is displayed
2612if isfield(UvData,'Mask')&~isfield(UvData,'A')
2613    set(handles.MinA,'String','0')
2614    set(handles.MaxA,'String','255')
2615end
2616IndexObj(1)=get(handles.list_object_1,'Value');
2617if IndexObj(1)> numel(UvData.Object)
2618    IndexObj(1)=1;
2619end
2620IndexObj_2=get(handles.list_object_2,'Value');
2621if IndexObj_2 <= numel(UvData.Object)
2622    IndexObj(2)=IndexObj_2;
2623end
2624for imap=1:numel(IndexObj)
2625    iobj=IndexObj(imap);
2626    if ~isempty(UvData.Object{iobj})%& isfield(Object{iobj},'plotaxes')& ishandle(Object{iobj}.plotaxes)
2627        %Projeter les champs sur l'objet:*
2628        ObjectData=proj_field(UvData.Field,UvData.Object{iobj},iobj);
2629        if imap==2
2630            UvData.ProjField_2=ObjectData;%store the projection field on uvmat: ***** WILL REPLACE THE FIELD SORED ON THE AXES: AxeData *****
2631        else
2632            UvData.ProjField_1=ObjectData;%store the projection field on view_field
2633        end
2634        %use of mask
2635        if isfield(ObjectData,'NbDim')&isequal(ObjectData.NbDim,2)
2636            if isfield(ObjectData,'Mask') & isfield(ObjectData,'A')
2637                 flag_mask=double(ObjectData.Mask>200);%=0 for masked regions
2638                 AX=ObjectData.AX;
2639                 AY=ObjectData.AY;
2640                 MaskX=ObjectData.MaskX;
2641                 MaskY=ObjectData.MaskY;
2642                 if ~isequal(MaskX,AX)|~isequal(MaskY,AY)
2643                     nxy=size(flag_mask);
2644                     sizpx=(ObjectData.MaskX(end)-ObjectData.MaskX(1))/(nxy(2)-1);%size of a mask pixel
2645                     sizpy=(ObjectData.MaskY(1)-ObjectData.MaskY(end))/(nxy(1)-1);
2646                     x_mask=[ObjectData.MaskX(1):sizpx:ObjectData.MaskX(end)]; % pixel x coordinates for image display
2647                     y_mask=[ObjectData.MaskY(1):-sizpy:ObjectData.MaskY(end)];% pixel x coordinates for image display
2648                     %project on the positions of the scalar
2649                     npxy=size(ObjectData.A);
2650                     dxy(1)=(ObjectData.AY(end)-ObjectData.AY(1))/(npxy(1)-1);%grid mesh in y
2651                     dxy(2)=(ObjectData.AX(end)-ObjectData.AX(1))/(npxy(2)-1);%grid mesh in x
2652                     xi=[ObjectData.AX(1):dxy(2):ObjectData.AX(end)];
2653                     yi=[ObjectData.AY(1):dxy(1):ObjectData.AY(end)];     
2654                     [XI,YI]=meshgrid(xi,yi);% creates the matrix of regular coordinates
2655                    flag_mask = interp2(x_mask,y_mask,flag_mask,XI,YI);
2656                 end
2657                 AClass=class(ObjectData.A);
2658                 ObjectData.A=flag_mask.*double(ObjectData.A);
2659                 ObjectData.A=feval(AClass,ObjectData.A);
2660                 ind_off=[];
2661                 if isfield(ObjectData,'ListVarName')
2662                      for ilist=1:length(ObjectData.ListVarName)
2663                           if isequal(ObjectData.ListVarName{ilist},'Mask')|isequal(ObjectData.ListVarName{ilist},'MaskX')|isequal(ObjectData.ListVarName{ilist},'MaskY')
2664                               ind_off=[ind_off ilist];
2665                           end
2666                      end
2667                      ObjectData.ListVarName(ind_off)=[];
2668                      ObjectData.VarDimIndex(ind_off)=[];
2669                      ind_off=[];       
2670                      for ilist=1:length(ObjectData.ListDimName)       
2671                           if isequal(ObjectData.ListDimName{ilist},'MaskX')|isequal(ObjectData.ListDimName{ilist},'MaskY')
2672                               ind_off=[ind_off ilist];
2673                           end
2674                      end
2675                      ObjectData.ListDimName(ind_off)=[];
2676                      ObjectData.DimValue(ind_off)=[];
2677                 end
2678            end 
2679        end
2680        if ~isempty(ObjectData)
2681            haxes=[];%default
2682            if isfield(UvData.Object{iobj},'plotaxes')
2683                haxes=UvData.Object{iobj}.plotaxes;%axes used for representing the projection on the object
2684            end
2685            PosColorbar=[];%default: no colorbar
2686            if ishandle(haxes) & isequal(get(haxes,'Tag'),'axes3')& isfield(UvData,'PosColorbar')
2687                PosColorbar=UvData.PosColorbar;%prescribe the colorbar position on the uvmat interface
2688            else
2689                PosColorbar='*';%default position
2690            end
2691            PlotParam=read_plot_param(handles);%read plotting parameters on the uvmat interface
2692            [PlotType,ScalOut,UvData.Object{iobj}.plotaxes]=plot_field(ObjectData,haxes,PlotParam,keeplim,PosColorbar);
2693            if isequal(PlotType,'none')
2694                hget_field=findobj(allchild(0),'name','get_field');
2695                if isempty(hget_field)
2696                    get_field([],ObjectData)% the projected field cannot be automatically plotted: use get_field to specify the variablesdelete(hget_field)
2697                else
2698                    errormsg='The field defined by get_field cannot be plotted';
2699                    return
2700                end
2701            end 
2702            UvData.Object{iobj}.PlotParam=ScalOut; %record the plotting parameters
2703        end       
2704    end
2705end
2706
2707%display the updated plotting parameters for the base menuplane
2708write_plot_param(handles,UvData.Object{1}.PlotParam);% update the display of the plotting parameters
2709UvData.NewSeries=0;% put to 0 the test for a new field series (set by RootPath_callback)
2710set(handles.uvmat,'UserData',UvData)
2711
2712%update the mask
2713if isequal(get(handles.mask_test,'Value'),1)%if the mask option is on
2714   update_mask(handles,num_i1,num_i2);
2715end
2716
2717%prepare the menus of histograms (for the whole menuvolume in 3D case)
2718menu_histo=(UvData.Field.ListVarName)';
2719ind_bad=[];
2720nb_histo=1;
2721for ivar=1:numel(menu_histo)
2722    if isfield(UvData.Field,'VarAttribute') && numel(UvData.Field.VarAttribute)>=ivar && isfield(UvData.Field.VarAttribute{ivar},'Role')
2723        Role=UvData.Field.VarAttribute{ivar}.Role;
2724        switch Role
2725            case {'coord_x','coord_y','coord_z','dimvar'}
2726                ind_bad=[ind_bad ivar];
2727            case {'vector_y'}
2728                nb_histo=nb_histo+1;
2729        end
2730    end
2731    DimCell=UvData.Field.VarDimName{ivar};
2732    DimName='';
2733    if ischar(DimCell)
2734        DimName=DimCell;
2735    elseif iscell(DimCell)&& numel(DimCell)==1
2736        DimName=DimCell{1};
2737    end
2738    if strcmp(DimName,menu_histo{ivar})
2739        ind_bad=[ind_bad ivar];
2740    end
2741end
2742menu_histo(ind_bad)=[];
2743test_v=0;
2744if ~isempty(menu_histo)
2745    set(handles.histo1_menu,'Value',1)
2746    set(handles.histo1_menu,'String',menu_histo)
2747    histo1_menu_Callback(handles.histo1_menu, [], handles)
2748    if nb_histo > 1
2749        test_v=1;
2750        set(handles.histo2_menu,'Visible','on')
2751        set(handles.histo_v,'Visible','on')
2752        set(handles.histo2_menu,'String',menu_histo)
2753        set(handles.histo2_menu,'Value',2)
2754        histo2_menu_Callback(handles.histo2_menu,[], handles)
2755    end
2756end
2757if ~test_v
2758    set(handles.histo2_menu,'Visible','off')
2759    set(handles.histo_v,'Visible','off')
2760    cla(handles.histo_v)
2761    set(handles.histo2_menu,'Value',1)
2762end
2763
2764%display time
2765testimedoc=0;
2766if isfield(UvData,'XmlData') && isfield(UvData.XmlData,'Time')
2767    if isempty(num_i2)
2768        num_i2=num_i1;
2769    end
2770    if isempty(num_j1)
2771        num_j1=1;
2772    end
2773    if isempty(num_j2)
2774        num_j2=num_j1;
2775    end
2776    siz=size(UvData.XmlData.Time);
2777    if siz(1)>=max(num_i1,num_i2) & siz(2)>=max(num_j1,num_j2)
2778        abstime=(UvData.XmlData.Time(num_i1,num_j1)+UvData.XmlData.Time(num_i2,num_j2))/2;%overset the time read from files
2779        dt=(UvData.XmlData.Time(num_i2,num_j2)-UvData.XmlData.Time(num_i1,num_j1));
2780        testimedoc=1;
2781    end
2782end
2783if isfield(UvData,'XmlData_1') && isfield(UvData.XmlData_1,'Time')
2784    [P,F,str1,str2,str_a,str_b,E,NomType]=name2display(['xx' get(handles.FileIndex_1,'String') get(handles.FileExt_1,'String')]);
2785    num_i2=str2num(str2);
2786    if isempty(num_i2)
2787        num_i2=num_i1;
2788    end
2789    num_j1=str2num(str_a);
2790    if isempty(num_j1)
2791        num_j1=1;
2792    end
2793    num_j2=str2num(str_b);
2794    if isempty(num_j2)
2795        num_j2=num_j1;
2796    end
2797    num_i1=str2num(str1);
2798    siz=size(UvData.XmlData_1.Time);
2799    if siz(1)>=max(num_i1,num_i2) & siz(2)>=max(num_j1,num_j2)
2800        abstime_1=(UvData.XmlData_1.Time(num_i1,num_j1)+UvData.XmlData_1.Time(num_i2,num_j2))/2;%overset the time read from files
2801    end
2802end
2803set(handles.abs_time,'String',num2str(abstime,4))
2804set(handles.abs_time_1,'String',num2str(abstime_1,4))
2805if testimedoc && isfield(UvData,'dt')
2806    dt=UvData.dt;
2807end
2808if isequal(dt,0)
2809    set(handles.Dt_txt,'String','')
2810else
2811    if ~(isfield(UvData,'TimeUnit') && ~isempty(UvData.TimeUnit))
2812        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  10^(-3)'] )
2813    else
2814        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  m' UvData.TimeUnit] )
2815    end
2816end
2817set(handles.run0,'BackgroundColor',[1 0 0])
2818
2819
2820
2821%-------------------------------------------------------------------
2822% --- translate coordinate to matrix index
2823%-------------------------------------------------------------------
2824function [indx,indy]=pos2ind(x0,rangx0,nxy)
2825indx=1+round((nxy(2)-1)*(x0-rangx0(1))/(rangx0(2)-rangx0(1)));% index x of pixel 
2826indy=1+round((nxy(1)-1)*(y12-rangy0(1))/(rangy0(2)-rangy0(1)));% index y of pixel
2827
2828%-------------------------------------------------------------------
2829% --- Executes on button press in 'FixedLimits'.
2830%-------------------------------------------------------------------
2831function FixedLimits_Callback(hObject, eventdata, handles)
2832test=get(handles.FixedLimits,'Value');
2833if test
2834    set(handles.FixedLimits,'BackgroundColor',[1 1 0])
2835else
2836    set(handles.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
2837    update_plot(handles);
2838end
2839
2840%-------------------------------------------------------------------
2841% --- Executes on button press in auto_xy.
2842function auto_xy_Callback(hObject, eventdata, handles)
2843test=get(handles.auto_xy,'Value');
2844if test
2845    set(handles.auto_xy,'BackgroundColor',[1 1 0])
2846    cla(handles.axes3)
2847    update_plot(handles)
2848else
2849    set(handles.auto_xy,'BackgroundColor',[0.7 0.7 0.7])
2850    update_plot(handles)
2851%     axis(handles.axes3,'image')
2852end
2853
2854
2855%-------------------------------------------------------------------
2856
2857%-------------------------------------------------------------------
2858% --- Executes on button press in 'zoom'.
2859%-------------------------------------------------------------------
2860function zoom_Callback(hObject, eventdata, handles)
2861
2862if (get(handles.zoom,'Value') == 1);
2863    set(handles.zoom,'BackgroundColor',[1 1 0])
2864    set(handles.FixedLimits,'Value',1)% propose by default fixed limits for the plotting axes
2865    set(handles.FixedLimits,'BackgroundColor',[1 1 0])
2866else
2867    set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
2868end
2869
2870
2871%-------------------------------------------------------------------
2872%----Executes on button press in 'record': records the current flags of manual correction.
2873%-------------------------------------------------------------------
2874function record_Callback(hObject, eventdata, handles)
2875% [filebase,num_i1,num_j1,num_i2,num_j2,Ext,NomType,SubDir]=read_input_file(handles);
2876filename=read_file_boxes(handles);
2877AxeData=get(gca,'UserData');
2878[erread,message]=fileattrib(filename);
2879if ~isempty(message) && ~isequal(message.UserWrite,1)
2880     msgbox_uvmat('ERROR',['no writting access to ' filename])
2881     return
2882end
2883test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
2884test_civ1=isequal(get(handles.civ1,'BackgroundColor'),[1 1 0]);
2885if ~test_civ2 && ~test_civ1
2886    msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
2887end
2888if test_civ2
2889    nbname='nb_vectors2';
2890   flagname='vec2_FixFlag';
2891   attrname='fix2';
2892end
2893if test_civ1
2894    nbname='nb_vectors';
2895   flagname='vec_FixFlag';
2896   attrname='fix';
2897end
2898%write fix flags in the netcdf file
2899hhh=which('netcdf.open');% look for built-in matlab netcdf library
2900if ~isequal(hhh,'')% case of new builtin Matlab netcdf library
2901    nc=netcdf.open(filename,'NC_WRITE');
2902    netcdf.reDef(nc)
2903    netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),attrname,1)
2904    dimid = netcdf.inqDimID(nc,nbname);
2905    try
2906        varid = netcdf.inqVarID(nc,flagname);% look for already existing fixflag variable
2907    catch
2908        varid=netcdf.defVar(nc,flagname,'double',dimid);%create fixflag variable if it does not exist
2909    end
2910    netcdf.endDef(nc)
2911    netcdf.putVar(nc,varid,AxeData.FF);
2912    netcdf.close(nc) 
2913else %old netcdf library
2914    netcdf_toolbox(filename,AxeData,attrname,nbname,flagname)
2915end
2916
2917function netcdf_toolbox(filename,AxeData,attrname,nbname,flagname)
2918nc=netcdf(filename,'write'); %open netcdf file
2919result=redef(nc);
2920eval(['nc.' attrname '=1;']);
2921theDim=nc(nbname) ;% get the number of velocity vectors
2922nb_vectors=size(theDim);
2923var_FixFlag=ncvar(flagname,nc);% var_FixFlag will be written as the netcdf variable vec_FixFlag
2924var_FixFlag(1:nb_vectors)=AxeData.FF;%
2925fin=close(nc);
2926
2927
2928%-------------------------------------------------------------------
2929%determines the fields to read from the interface
2930%------------------------------------------------------------------
2931function [VelType,civ]=setfield(handles)
2932
2933VelType=[]; %default
2934if (get(handles.civ1,'Value') == 1);
2935        VelType='civ1';
2936% interp1   
2937elseif (get(handles.interp1,'Value') == 1);
2938    VelType='interp1';
2939% filter1   
2940elseif (get(handles.filter1,'Value') == 1);
2941    VelType='filter1'; 
2942% CIV2
2943elseif (get(handles.civ2,'Value') == 1);
2944    VelType='civ2';
2945% interp2   
2946elseif (get(handles.interp2,'Value') == 1);
2947    VelType='interp2';
2948% filter2   
2949elseif (get(handles.filter2,'Value') == 1); 
2950    VelType='filter2';
2951end
2952
2953if isequal(get(handles.filter2,'Visible'),'on');
2954    civ=6;
2955% interp1   
2956elseif isequal(get(handles.interp2,'Visible'),'on');
2957    civ=5;
2958% filter1   
2959elseif isequal(get(handles.civ2,'Visible'),'on');
2960    civ=4; 
2961% CIV2
2962elseif isequal(get(handles.filter1,'Visible'),'on');
2963   civ=3;
2964% interp2   
2965elseif isequal(get(handles.interp1,'Visible'),'on');
2966    civ=2;
2967% filter2   
2968elseif isequal(get(handles.civ1,'Visible'),'on'); 
2969    civ=1;
2970else
2971    civ=0;
2972end
2973
2974%-------------------------------------------------------------------
2975%determines the veltype of the second field to read from the iinterface
2976%------------------------------------------------------------------
2977function VelType=setfield_1(handles)
2978
2979VelType=[]; %default
2980if (get(handles.civ1_1,'Value') == 1);
2981    VelType='civ1';
2982% interp1   
2983elseif (get(handles.interp1_1,'Value') == 1);
2984    VelType='interp1';
2985% filter1   
2986elseif (get(handles.filter1_1,'Value') == 1);
2987    VelType='filter1'; 
2988% CIV2
2989elseif (get(handles.civ2_1,'Value') == 1);
2990    VelType='civ2';
2991% interp2   
2992elseif (get(handles.interp2_1,'Value') == 1);
2993    VelType='interp2';
2994% filter2   
2995elseif (get(handles.filter2_1,'Value') == 1); 
2996    VelType='filter2';
2997end
2998
2999
3000%---------------------------------------------------
3001% --- Executes on button press in SubField
3002function SubField_Callback(hObject, eventdata, handles)
3003huvmat=get(handles.run0,'parent');
3004UvData=get(huvmat,'UserData');
3005if get(handles.SubField,'Value')==0% if the subfield button is desactivated   
3006    set(handles.RootPath_1,'String','')
3007    set(handles.RootFile_1,'String','')
3008    set(handles.SubDir_1,'String','');
3009    set(handles.FileIndex_1,'String','');
3010    set(handles.FileExt_1,'String','');
3011    set(handles.RootPath_1,'Visible','off')
3012    set(handles.RootFile_1,'Visible','off')
3013    set(handles.SubDir_1,'Visible','off');
3014    set(handles.FileIndex_1,'Visible','off');
3015    set(handles.FileExt_1,'Visible','off');
3016    set(handles.Fields_1,'Value',1);%set to blank state
3017    set_veltype_display([handles.civ1_1 handles.interp1_1 handles.filter1_1 ...
3018            handles.civ2_1 handles.interp2_1 handles.filter2_1],0)
3019    if isfield(UvData,'XmlData_1')
3020        UvData=rmfield(UvData,'XmlData_1');
3021    end
3022    set(huvmat,'UserData',UvData);
3023    run0_Callback(hObject, eventdata, handles); %run
3024else
3025    MenuBrowse_1_Callback(hObject, eventdata, handles)
3026end
3027
3028% %----------------------------------------------
3029% %read the data displayed for the input rootfile windows (new)
3030% %-------------------------------------------------
3031function [FileName,RootPath,FileBase,FileIndices,FileExt,SubDir]=read_file_boxes(handles)
3032RootPath=get(handles.RootPath,'String');
3033FileName=RootPath; %default
3034SubDir=get(handles.SubDir,'String');
3035if ~isempty(SubDir) && ~isequal(SubDir,'')
3036    if (isequal(SubDir(1),'/')|| isequal(SubDir(1),'\'))
3037        SubDir(1)=[]; %suppress possible / or \ separator
3038    end
3039    FileName=fullfile(RootPath,SubDir);
3040end
3041RootFile=get(handles.RootFile,'String');
3042if ~isempty(RootFile) && ~isequal(RootFile,'')
3043    if (isequal(RootFile(1),'/')|| isequal(RootFile(1),'\'))
3044        RootFile(1)=[]; %suppress possible / or \ separator
3045    end
3046    FileName=fullfile(FileName,RootFile);
3047end
3048FileBase=fullfile(RootPath,RootFile);
3049FileIndices=get(handles.FileIndex,'String');
3050FileExt=get(handles.FileExt,'String');
3051FileName=[FileName FileIndices FileExt];
3052
3053%----------------------------------------------
3054%read the data displayed for the second input rootfile windows
3055%-------------------------------------------------
3056function [FileName_1,RootPath_1,FileBase_1,FileIndices_1,FileExt_1,SubDir_1]=read_file_boxes_1(handles)
3057RootPath_1=get(handles.RootPath_1,'String'); % read the data from the file1_input window
3058if isequal(RootPath_1,'"'),RootPath_1=get(handles.RootPath,'String'); end;
3059FileName_1=RootPath_1; %default
3060SubDir_1=get(handles.SubDir_1,'String');
3061if isequal(SubDir_1,'"')
3062    SubDir_1=get(handles.SubDir,'String');
3063end
3064if ~isempty(SubDir_1) && ~isequal(SubDir_1,'')
3065    if (isequal(SubDir_1(1),'/')|| isequal(SubDir_1(1),'\'))
3066        SubDir_1(1)=[]; %suppress possible / or \ separator
3067    end
3068    FileName_1=fullfile(RootPath_1,SubDir_1);
3069end
3070RootFile_1=get(handles.RootFile_1,'String');
3071if isequal(RootFile_1,'"'),RootFile_1=get(handles.RootFile,'String'); end;
3072if ~isempty(RootFile_1) && ~isequal(RootFile_1,'')
3073    if ~(isequal(RootFile_1(1),'/')|isequal(RootFile_1(1),'\'))
3074        RootFile_1(1)=[];%suppress possible / or \ separator
3075    end
3076    FileName_1=fullfile(FileName_1,RootFile_1);
3077end
3078FileBase_1=fullfile(RootPath_1,RootFile_1);
3079FileIndices_1=get(handles.FileIndex_1,'String');
3080FileExt_1=get(handles.FileExt_1,'String');
3081if isequal(FileExt_1,'"'),FileExt_1=get(handles.FileExt,'String'); end;
3082FileName_1=[FileName_1 FileIndices_1 FileExt_1];
3083
3084%---------------------------------------------------
3085% --- Executes on menu selection Fields
3086function Fields_Callback(hObject, eventdata, handles)
3087%-------------------------------------------------
3088
3089list_fields=get(handles.Fields,'String');% list menu fields
3090index_fields=get(handles.Fields,'Value');% selected string index
3091field= list_fields{index_fields(1)}; % selected string
3092if isequal(field,'get_field...')
3093     veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
3094     set_veltype_display(veltype_handles,0) % unvisible civ buttons
3095     filename=read_file_boxes(handles);
3096     hget_field=findobj(allchild(0),'name','get_field');
3097     if ~isempty(hget_field)
3098         delete(hget_field)
3099     end
3100     get_field(filename)
3101    return %no action
3102end
3103list_fields=get(handles.Fields_1,'String');% list menu fields
3104index_fields=get(handles.Fields_1,'Value');% selected string index
3105field_1= list_fields{index_fields(1)}; % selected string
3106UvData=get(handles.uvmat,'UserData');
3107
3108%read the rootfile input display
3109[FileName,RootPath,FileBase,FileIndices,FileExt]=read_file_boxes(handles);
3110[P,F,str1,str2,str_a,str_b,E,NomType]=name2display(['xxx' get(handles.FileIndex,'String') FileExt]);
3111NomTypeNew=NomType;%default
3112if isequal(field,'image')
3113    % transform netc type to the corresponding image type
3114    if isequal(NomType,'_i1-i2_j')||isequal(NomType,'_i_j1-j2')|| isequal(NomType,'#_ab')|| isequal(NomType,'_i1-i2')
3115        UvData.SubDir=get(handles.SubDir,'String'); %preserve the subdir in memory
3116        if ~isempty(UvData.SubDir) && (isequal(UvData.SubDir(1),'/')||isequal(UvData.SubDir(1),'/'))
3117            UvData.SubDir(1)=[];
3118        end
3119        set(handles.SubDir,'String','')
3120        set(handles.FileExt,'String','.png');
3121        if isequal(NomType,'_i1-i2_j')||isequal(NomType,'_i_j1-j2')
3122            NomTypeNew='_i_j';
3123        elseif isequal(NomType,'#_ab')
3124            NomTypeNew='#a';
3125        elseif isequal(NomType,'_i1-i2')
3126            NomTypeNew='_i';
3127            % TODO: look for other types
3128        end 
3129    end
3130    veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
3131    set_veltype_display(veltype_handles,0) % unvisible civ buttons
3132else
3133    ext=get(handles.FileExt,'String');
3134    if ~isequal(ext,'.nc') %find the new NomType if the previous display was not already a netcdf file
3135         MenuBrowse_Callback(hObject, eventdata, handles)
3136    end
3137    if isequal(field,'vort') || isequal(field,'div') || isequal(field,'strain')
3138        set(handles.civ1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3139        set(handles.civ2,'BackgroundColor',[0.702 0.702 0.702])
3140        set(handles.interp1,'BackgroundColor',[0.702 0.702 0.702])
3141        set(handles.interp2,'BackgroundColor',[0.702 0.702 0.702])
3142    elseif isequal(field,'more...');
3143        set(handles.civ1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3144        set(handles.civ2,'BackgroundColor',[0.702 0.702 0.702])
3145        str=calc_field;%get the list of available scalars by the function calc_scal
3146        [ind_answer] = listdlg('PromptString','Select a file:',...
3147                'SelectionMode','single',...
3148                'ListString',str);
3149       % edit the choice in the field and action menu
3150        scalar=cell2mat(str(ind_answer));
3151        menu=update_menu(handles.Fields,scalar);
3152        menu=[{''};menu];
3153        set(handles.Fields_1,'String',menu);% store the selected scalar type
3154    end
3155end
3156indices=name_generator('',str2double(str1),str2double(str_a),'',NomTypeNew,1,str2double(str2),str2double(str_b),'');
3157set(handles.FileIndex,'String',indices)
3158set(handles.FileIndex,'UserData',NomTypeNew)
3159%common to Fields_1_Callback
3160if isequal(field,'image')||isequal(field_1,'image')
3161    set(handles.npx_title,'Visible','on')% visible npx,pxcm... buttons
3162    set(handles.npy_title,'Visible','on')
3163    set(handles.npx,'Visible','on')
3164    set(handles.npy,'Visible','on')
3165%     set(handles.fix_pair,'Value',0)
3166else
3167    set(handles.npx_title,'Visible','off')% visible npx,pxcm... buttons
3168    set(handles.npy_title,'Visible','off')
3169    set(handles.npx,'Visible','off')
3170    set(handles.npy,'Visible','off')
3171%     set(handles.fix_pair,'Value',1)
3172end
3173setfield(handles);% update the field structure ('civ1'....)
3174
3175if ~isfield(UvData,'NewSeries')||isequal(UvData.NewSeries,0)
3176    run0_Callback(hObject, eventdata, handles)
3177end
3178
3179%---------------------------------------------------
3180% --- Executes on menu selection Fields
3181function Fields_1_Callback(hObject, eventdata, handles)
3182%-------------------------------------------------
3183list_fields=get(handles.Fields,'String');% list menu fields
3184index_fields=get(handles.Fields,'Value');% selected string index
3185field= list_fields{index_fields(1)}; % selected string
3186list_fields=get(handles.Fields_1,'String');% list menu fields
3187index_fields=get(handles.Fields_1,'Value');% selected string index
3188field_1= list_fields{index_fields(1)}; % selected string for the second field
3189if isequal(field_1,'') %remove second field if 'blank' field is selected
3190    set(handles.SubField,'Value',0)
3191    SubField_Callback(hObject, eventdata, handles)
3192    return
3193end
3194UvData=get(handles.uvmat,'UserData');
3195
3196%read the rootfile input display
3197FileExt_prev=get(handles.FileExt_1,'String');
3198if isempty(FileExt_prev)|isequal(FileExt_prev,'')
3199    FileExt_1=get(handles.FileExt,'String');
3200else
3201    FileExt_1=FileExt_prev;
3202end
3203NomType_1=get(handles.FileIndex_1,'UserData');
3204if isempty(NomType_1)|isequal(NomType_1,'')
3205    NomType_1=get(handles.FileIndex,'UserData');
3206end
3207NomTypeNew=NomType_1;%default
3208
3209set(handles.SubField,'Value',1)%introduce second field
3210if isfield(UvData,'XmlData')
3211    UvData.XmlData_1=UvData.XmlData;
3212end
3213set(handles.FileIndex_1,'Visible','on')
3214set(handles.FileExt_1,'Visible','on')
3215RootPath_1=get(handles.RootPath_1,'String');
3216RootFile_1=get(handles.RootFile_1,'String');
3217if isempty(RootPath_1)||isequal(RootPath_1,'')
3218    set(handles.RootPath_1,'String','"')
3219end
3220if isempty(RootFile_1) || isequal(RootFile_1,'')
3221    set(handles.RootFile_1,'String','"')
3222end
3223if ~isempty(RootFile_1)&&(isequal(RootFile_1(1),'/')||isequal(RootFile_1(1),'\'))
3224    RootFile_1(1)=[];
3225end
3226
3227if isequal(field_1,'get_field...')
3228     veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
3229     set_veltype_display(veltype_handles,0) % unvisible civ buttons
3230     filename=read_file_boxes_1(handles);
3231     hget_field=findobj(allchild(0),'name','get_field_1');
3232     if ~isempty(hget_field)
3233         delete(hget_field)
3234     end
3235     hget_field=get_field(filename);
3236     set(hget_field,'name','get_field_1')
3237    return %no action
3238end
3239if isequal(field_1,'image')
3240    % transform netc type to the corresponding image type
3241    set(handles.FileExt_1,'String','.png');
3242    if isequal(NomType_1,'_i1-i2_j')|isequal(NomType_1,'_i_j1-j2')| isequal(NomType_1,'#_ab')| isequal(NomType_1,'_i1-i2')
3243        UvData.SubDir_1=get(handles.SubDir_1,'String'); %preserve the subdir in memory
3244        set(handles.SubDir_1,'String','')
3245%         set(handles.FileExt_1,'String','.png');       
3246        if isequal(NomType_1,'_i1-i2_j')|isequal(NomType_1,'_i_j1-j2')
3247            NomTypeNew='_i_j';
3248        elseif isequal(NomType_1,'#_ab')
3249            NomTypeNew='#a';
3250        elseif isequal(NomType_1,'_i1-i2')
3251            NomTypeNew='_i';
3252        end 
3253    end
3254    veltype_handles=[handles.civ1_1 handles.interp1_1 handles.filter1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1];
3255    set_veltype_display(veltype_handles,0) % unvisible civ buttons
3256else
3257    set(handles.SubDir_1,'Visible','on')
3258    if ~isequal(FileExt_prev,'.nc') %find the new NomType if the previous display was not already a netcdf file
3259        veltype_handles=[handles.civ1_1 handles.interp1_1 handles.filter1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1];
3260        set_veltype_display(veltype_handles,6); % make all civ buttons visible
3261        RootPath_1=get(handles.RootPath_1,'String');
3262        RootFile_1=get(handles.RootFile_1,'String');
3263        if isempty(RootPath_1)|isequal(RootPath_1,'')
3264            set(handles.RootPath_1,'String','"')
3265        end
3266        if isempty(RootFile_1) | isequal(RootFile_1,'')
3267            set(handles.RootFile_1,'String','"')
3268        end
3269        if ~isempty(RootFile_1)&(isequal(RootFile_1(1),'/')|isequal(RootFile_1(1),'\'))
3270            RootFile_1(1)=[];
3271        end
3272        filebase_1=fullfile(RootPath_1,RootFile_1);
3273        SubDir_1=get(handles.SubDir,'String');
3274        if isempty(SubDir_1)||isequal(SubDir_1,'')
3275            if isfield(UvData,'SubDir_1')
3276                SubDir_1=UvData.SubDir_1;%retrieve previous subdir
3277            else
3278                SubDir_1='?';
3279            end
3280        end
3281        str1=get(handles.i1,'String');
3282        str_a=get(handles.j1,'String');
3283        if isequal(NomType_1,'#_ab')||isequal(NomType_1,'_i1-i2_j')||isequal(NomType_1,'_i_j1-j2')||isequal(NomType_1,'_i1-i2')
3284            NomTypeNew=NomType_1;
3285        elseif isequal(NomType_1,'#a')
3286             [filename, n1,na,n2,nb,SubDir_1]=name_generator(filebase_1, str2num(str1),stra2num(str_a),'.nc','#_ab',0,[],[],SubDir_1);
3287             NomTypeNew='#_ab';
3288        elseif isequal(NomType_1,'_i_j')
3289             [filename,n1,na,n2,nb,SubDir_1]=name_generator(filebase_1,str2num(str1),stra2num(str_a),'.nc','_i1-i2_j',0,str2num(str1),[],SubDir_1);
3290            if idetect==1
3291                NomTypeNew='_i1-i2_j';
3292            else
3293                NomTypeNew='_i_j1-j2';
3294            end
3295        else %for instance avi files or any ima_num series
3296            [filename,n1,na,n2,nb,SubDir_1]=name_generator(filebase_1,str2num(str1),stra2num(str_a),'.nc','_i1-i2',0,str2num(str1),[],SubDir_1);
3297            NomTypeNew='_i1-i2';
3298        end           
3299        [Path,Name]=fileparts(filebase_1);
3300        set(handles.FileExt_1,'String','.nc');
3301        if ~isempty(SubDir_1) & ~isequal(SubDir_1,'''')& ~isequal(SubDir_1,'"')
3302            SubDir_1=['/' SubDir_1];
3303        end
3304        set(handles.SubDir_1,'String',SubDir_1);
3305    end
3306    if isequal(field,'vort') | isequal(field,'div') | isequal(field,'strain')
3307        set(handles.civ1_1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3308        set(handles.civ2_1,'BackgroundColor',[0.702 0.702 0.702])
3309        set(handles.interp1_1,'BackgroundColor',[0.702 0.702 0.702])
3310        set(handles.interp2_1,'BackgroundColor',[0.702 0.702 0.702])
3311    elseif isequal(field_1,'more...'); %add new item to the menu
3312        set(handles.civ1_1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3313        set(handles.civ2_1,'BackgroundColor',[0.702 0.702 0.702])
3314        str=calc_field;%get the list of available scalars by the function calc_scal
3315        [ind_answer,v] = listdlg('PromptString','Select a file:',...
3316                'SelectionMode','single',...
3317                'ListString',str);
3318       % edit the choice in the field and action menu
3319        scalar=cell2mat(str(ind_answer));
3320        menu=update_menu(handles.Fields_1,scalar);
3321        set(handles.Fields_1,'String',menu);% store the selected scalar type
3322    end
3323end
3324str1=get(handles.i1,'String');
3325str2=get(handles.i2,'String');
3326str_a=get(handles.j1,'String');
3327str_b=get(handles.j2,'String');
3328indices=name_generator('',str2num(str1),stra2num(str_a),'',NomTypeNew,1,str2num(str2),stra2num(str_b),'');
3329set(handles.FileIndex_1,'String',indices)
3330set(handles.FileIndex_1,'UserData',NomTypeNew)
3331
3332%common to Fields_Callback
3333if isequal(field,'image')|isequal(field_1,'image')
3334    set(handles.npx_title,'Visible','on')% visible npx,pxcm... buttons
3335    set(handles.npy_title,'Visible','on')
3336    set(handles.npx,'Visible','on')
3337    set(handles.npy,'Visible','on')
3338%     set(handles.fix_pair,'Value',0)
3339else
3340    set(handles.npx_title,'Visible','off')% visible npx,pxcm... buttons
3341    set(handles.npy_title,'Visible','off')
3342    set(handles.npx,'Visible','off')
3343    set(handles.npy,'Visible','off')
3344%     set(handles.fix_pair,'Value',1)
3345end
3346if isequal(field,'velocity')|isequal(field_1,'velocity');
3347    state_vect='on';
3348else
3349    state_vect='off';
3350end
3351if ~isequal(field,'velocity')|(~isequal(field_1,'velocity')&~isequal(field_1,''));
3352    state_scal='on';
3353else
3354    state_scal='off';
3355end
3356set(handles.uvmat,'UserData',UvData)
3357setfield(handles);% update the field structure ('civ1'....)
3358if ~isfield(UvData,'NewSeries')|isequal(UvData.NewSeries,0)
3359    run0_Callback(hObject, eventdata, handles)
3360end
3361
3362%------------------------------------------------------------------------
3363% --- set the visibility of relevant velocity type menus:
3364function set_veltype_display(handles,Civ)
3365%------------------------------------------------------------------------
3366%Civ=0; all states 'off'
3367%Civ=6; all states 'on'
3368if isequal(Civ,0)
3369    imax=0;
3370%    set(handles(1),'Visible','on')  % unvisible civ buttons
3371% else
3372%    set(handles(1),'String','civ1')
3373% end
3374elseif isequal(Civ,1) || isequal(Civ,2)
3375   imax=1;
3376elseif isequal(Civ,3)
3377    imax=3;
3378elseif isequal(Civ,4) || isequal(Civ,5)
3379    imax=4;
3380elseif isequal(Civ,6) %patch2
3381    imax=6;
3382end
3383for ibutton=1:imax;
3384    set(handles(ibutton),'Visible','on')  % unvisible civ buttons
3385end
3386% for ibutton=max(imax+1,2):6;
3387for ibutton=imax+1:6;
3388    set(handles(ibutton),'Visible','off')  % unvisible civ buttons
3389    set(handles(ibutton),'Value',0)%unactivate unvisible buttons
3390end
3391
3392%-------------------------------------------------------------------
3393% --- Executes on button press in civ1.
3394function civ1_Callback(hObject, eventdata, handles)
3395%-------------------------------------------------------------------
3396if get(handles.civ1,'Value')==1
3397    reset_vel_type([handles.interp1 handles.civ2 handles.filter1 handles.interp1 handles.interp2 handles.filter2],handles.civ1)
3398else
3399    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3400end
3401run0_Callback(hObject, eventdata, handles)
3402
3403%-------------------------------------------------------------------
3404% --- Executes on button press in interp1.
3405function interp1_Callback(hObject, eventdata, handles)
3406%-------------------------------------------------------------------
3407if get(handles.interp1,'Value')==1
3408    reset_vel_type([handles.civ1 handles.civ2 handles.filter1 handles.interp2 handles.filter2],handles.interp1)
3409else
3410     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3411end
3412run0_Callback(hObject, eventdata, handles)
3413
3414%-------------------------------------------------------------------
3415% --- Executes on button press in filter1.
3416function filter1_Callback(hObject, eventdata, handles)
3417%-------------------------------------------------------------------
3418if get(handles.filter1,'Value')==1
3419    reset_vel_type([handles.civ1 handles.civ2 handles.interp1 handles.interp2 handles.filter2],handles.filter1)
3420else
3421     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3422end
3423run0_Callback(hObject, eventdata, handles)
3424
3425%-------------------------------------------------------------------
3426% --- Executes on button press in civ2.
3427function civ2_Callback(hObject, eventdata, handles)
3428%-------------------------------------------------------------------
3429if get(handles.civ2,'Value')==1
3430    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.interp2 handles.filter2],handles.civ2)
3431else
3432     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3433end
3434run0_Callback(hObject, eventdata, handles)
3435
3436%-----------------------------------------
3437% --- Executes on button press in interp2.
3438%-------------------------------------------
3439function interp2_Callback(hObject, eventdata, handles)
3440if get(handles.interp2,'Value')==1
3441    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.filter2],handles.interp2)
3442else
3443     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3444end
3445run0_Callback(hObject, eventdata, handles)
3446%---------------------------------------------
3447% --- Executes on button press in filter2.
3448%-------------------------------------------
3449function filter2_Callback(hObject, eventdata, handles)
3450if get(handles.filter2,'Value')==1
3451    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2],handles.filter2)
3452else
3453     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3454end
3455run0_Callback(hObject, eventdata, handles)
3456
3457%---------------------------------------------
3458function civ1_1_Callback(hObject, eventdata, handles)
3459%---------------------------------------------
3460if get(handles.civ1_1,'Value')==1
3461    reset_vel_type([handles.interp1_1 handles.civ2_1 handles.filter1_1 handles.interp1_1 handles.interp2_1 handles.filter2_1],handles.civ1_1)
3462else
3463     reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3464end
3465run0_Callback(hObject, eventdata, handles)
3466
3467%--------------------------------------------
3468function interp1_1_Callback(hObject, eventdata, handles)
3469%--------------------------------------------
3470if get(handles.interp1_1,'Value')==1
3471    reset_vel_type([handles.civ1_1 handles.civ2_1 handles.filter1_1 handles.interp2_1 handles.filter2_1],handles.interp1_1)
3472else
3473    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3474end
3475run0_Callback(hObject, eventdata, handles)
3476
3477%--------------------------------------------
3478function filter1_1_Callback(hObject, eventdata, handles)
3479%--------------------------------------------
3480if get(handles.filter1_1,'Value')==1
3481    reset_vel_type([handles.interp1_1 handles.civ2_1 handles.interp1_1 handles.interp2_1 handles.filter2_1],handles.filter1_1)
3482else
3483    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3484end
3485run0_Callback(hObject, eventdata, handles)
3486
3487%--------------------------------------------
3488function civ2_1_Callback(hObject, eventdata, handles)
3489%--------------------------------------------
3490if get(handles.civ2_1,'Value')==1
3491    reset_vel_type([handles.civ1_1 handles.interp1_1  handles.filter1_1 handles.interp2_1 handles.filter2_1],handles.civ2_1)
3492else
3493    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3494end
3495run0_Callback(hObject, eventdata, handles)
3496
3497%--------------------------------------------
3498function interp2_1_Callback(hObject, eventdata, handles)
3499%--------------------------------------------
3500if get(handles.interp2_1,'Value')==1
3501    reset_vel_type([handles.civ1_1 handles.civ2_1 handles.filter1_1 handles.interp1_1 handles.filter2_1],handles.interp2_1)
3502else
3503    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3504end
3505run0_Callback(hObject, eventdata, handles)
3506
3507%--------------------------------------------
3508function filter2_1_Callback(hObject, eventdata, handles)
3509%--------------------------------------------
3510if get(handles.filter2_1,'Value')==1
3511    reset_vel_type([handles.civ1_1 handles.interp1_1 handles.civ2_1 handles.filter1_1 handles.interp1_1 handles.interp2_1],handles.filter2_1)
3512else
3513    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3514end
3515run0_Callback(hObject, eventdata, handles)
3516
3517%-----------------------------------------------
3518% --- reset civ buttons
3519function reset_vel_type(handles_civ0,handle1)
3520for ibutton=1:length(handles_civ0)
3521    set(handles_civ0(ibutton),'BackgroundColor',[0.831 0.816 0.784])
3522    set(handles_civ0(ibutton),'Value',0)
3523end
3524if exist('handle1','var')%handles of selected button
3525        set(handle1,'BackgroundColor',[1 1 0]) 
3526end
3527
3528%-------------------------------------------------------
3529% --- Executes on button press in MENUVOLUME.
3530%-------------------------------------------------------
3531function VOLUME_Callback(hObject, eventdata, handles)
3532%errordlg('command VOL not implemented yet')
3533if ishandle(handles.UVMAT_title)
3534    delete(handles.UVMAT_title)
3535end
3536UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3537if isequal(get(handles.VOLUME,'Value'),1)
3538    set(handles.zoom,'Value',0)
3539    set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
3540    set(handles.edit_vect,'Value',0)
3541    edit_vect_Callback(hObject, eventdata, handles)
3542    set(handles.edit,'Value',0)
3543    set(handles.edit,'BackgroundColor',[0.7 0.7 0.7])
3544%     set(handles.cal,'Value',0)
3545%     set(handles.cal,'BackgroundColor',[0 1 0])
3546    set(handles.edit_vect,'Value',0)
3547    edit_vect_Callback(hObject, eventdata, handles)
3548    %initiate set_object GUI
3549    data.TITLE='VOLUME';
3550    if isfield(UvData,'CoordType')
3551        data.CoordType=UvData.CoordType;
3552    end
3553    if isfield(UvData,'Mesh')&~isempty(UvData.Mesh)
3554        data.RangeY=UvData.Mesh;
3555        data.RangeX=UvData.Mesh;
3556        data.DX=UvData.Mesh;
3557        data.DY=UvData.Mesh;
3558    elseif isfield(UvData.Field,'AX')&isfield(UvData.Field,'AY')& isfield(UvData.Field,'A')%only image
3559        np=size(UvData.Field.A);
3560        meshx=(UvData.Field.AX(end)-UvData.Field.AX(1))/np(2);
3561        meshy=abs(UvData.Field.AY(end)-UvData.Field.AY(1))/np(1);
3562        data.RangeY=max(meshx,meshy);
3563        data.RangeX=max(meshx,meshy);
3564        data.DX=max(meshx,meshy);
3565    end
3566    data.ParentButton=handles.VOLUME;
3567    PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
3568    [hset_object,UvData.sethandles]=set_object(data,PlotHandles);% call the set_object interface with action on haxes,
3569                                                      % associate the set_edit interface handle to the plotting axes
3570    if isfield(UvData,'SetObjectOrigin')                                               
3571    pos_uvmat=get(huvmat,'Position');
3572    pos_set_object(1:2)=UvData.SetObjectOrigin + pos_uvmat(1:2);
3573    pos_set_object(3:4)=UvData.SetObjectSize .* pos_uvmat(3:4); 
3574    set(hset_object,'Position',pos_set_object)
3575    end
3576    UvData.MouseAction='create_object';
3577else
3578    set(handles.VOLUME,'BackgroundColor',[0 1 0])
3579    UvData.MouseAction='none';
3580end
3581set(huvmat,'UserData',UvData)
3582
3583%-------------------------------------------------------
3584function edit_vect_Callback(hObject, eventdata, handles)
3585%-------------------------------------------------------
3586
3587UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3588if isequal(get(handles.edit_vect,'Value'),1)
3589    test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
3590    test_civ1=isequal(get(handles.civ1,'BackgroundColor'),[1 1 0]);
3591    if ~test_civ2 && ~test_civ1
3592        msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
3593    end
3594    set(handles.record,'Visible','on')
3595    set(handles.edit_vect,'BackgroundColor',[1 1 0])
3596    set(handles.edit,'Value',0)
3597    set(handles.zoom,'Value',0)
3598    set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
3599%     set(handles.create,'Value',0)
3600%     set(handles.create,'BackgroundColor',[0 1 0])
3601    set(handles.edit,'BackgroundColor',[0.7 0.7 0.7])
3602    set(gcf,'Pointer','arrow')
3603    UvData.MouseAction='edit_vect';
3604else
3605    set(handles.record,'Visible','off')
3606    set(handles.edit_vect,'BackgroundColor',[0.7 0.7 0.7])
3607    UvData.MouseAction='none';
3608end
3609set(handles.uvmat,'UserData',UvData)
3610
3611%----------------------------------------------
3612function save_mask_Callback(hObject, eventdata, handles)
3613%-----------------------------------------------------------------------
3614UvData=get(handles.uvmat,'UserData');
3615
3616flag=1;
3617npx=size(UvData.Field.A,2);
3618npy=size(UvData.Field.A,1);
3619xi=[0.5:npx-0.5];
3620yi=[0.5:npy-0.5];
3621[Xi,Yi]=meshgrid(xi,yi);
3622if isfield(UvData,'Object')
3623    for iobj=1:length(UvData.Object)
3624        ObjectData=UvData.Object{iobj};
3625        if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
3626            flagobj=1;
3627            testphys=0; %coordinates in pixels by default
3628            if isfield(ObjectData,'CoordType') && isequal(ObjectData.CoordType,'phys')
3629                if isfield(UvData,'XmlData')&& isfield(UvData.XmlData,'GeometryCalib')
3630                    Calib=UvData.XmlData.GeometryCalib;
3631                    testphys=1;
3632                end
3633            end
3634            if isfield(ObjectData,'Coord')& isfield(ObjectData,'Style')
3635                if isequal(ObjectData.Style,'polygon')
3636                    X=ObjectData.Coord(:,1);
3637                    Y=ObjectData.Coord(:,2);
3638                    if testphys
3639                        [X,Y]=px_XYZ(Calib,X,Y,0);% to generalise with 3D cases
3640                    end
3641                    flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside                 
3642                elseif isequal(ObjectData.Style,'ellipse')
3643                    if testphys
3644                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3645                    end
3646                    RangeX=max(ObjectData.RangeX);
3647                    RangeY=max(ObjectData.RangeY);
3648                    X2Max=RangeX*RangeX;
3649                    Y2Max=RangeY*RangeY;
3650                    distX=(Xi-ObjectData.Coord(1,1));
3651                    distY=(Yi-ObjectData.Coord(1,2));
3652                    flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
3653                elseif isequal(ObjectData.Style,'rectangle')
3654                    if testphys
3655                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3656                    end
3657                    distX=abs(Xi-ObjectData.Coord(1,1));
3658                    distY=abs(Yi-ObjectData.Coord(1,2));
3659                    flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
3660                end
3661                if isequal(ObjectData.ProjMode,'mask_outside')
3662                    flagobj=~flagobj;
3663                end
3664                flag=flag & flagobj;
3665            end
3666        end
3667    end
3668end
3669% flag=~flag;
3670%mask name
3671RootPath=get(handles.RootPath,'String');
3672RootFile=get(handles.RootFile,'String');
3673if ~isempty(RootFile)&(isequal(RootFile(1),'/')| isequal(RootFile(1),'\'))
3674        RootFile(1)=[];
3675end
3676filebase=fullfile(RootPath,RootFile);
3677list=get(handles.masklevel,'String');
3678masknumber=num2str(length(list));
3679maskindex=get(handles.masklevel,'Value');
3680mask_name=name_generator([filebase '_' masknumber 'mask'],maskindex,1,'.png','_i');
3681imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
3682imflag=flipdim(imflag,1);
3683% imflag=uint8(255*flag);% =0 for flag=0 (vectors=0 when 20<imflag<200)
3684msgbox_uvmat('CONFIRMATION',[mask_name ' saved'])
3685imwrite(imflag,mask_name,'BitDepth',8);
3686
3687%display the mask
3688%update_mask(handles,num_i1,num_j1)
3689figure;
3690vec=linspace(0,1,256);%define a linear greyscale colormap
3691map=[vec' vec' vec'];
3692colormap(map)
3693
3694image(imflag);
3695
3696%-------------------------------------------------------------------
3697%-------------------------------------------------------------------
3698%  - FUNCTIONS FOR SETTING PLOTTING PARAMETERS
3699
3700%------------------------------------------------------------------
3701
3702
3703
3704%------------------------------------------------------------------
3705% --- Executes on selection change in col_vec: choice of the color code.
3706%
3707function col_vec_Callback(hObject, eventdata, handles)
3708%------------------------------------------------------------------
3709% edit the choice for color code
3710list_code=get(handles.col_vec,'String');% list menu fields
3711index_code=get(handles.col_vec,'Value');% selected string index
3712col_code= list_code{index_code(1)}; % selected field
3713if isequal(col_code,'black') | isequal(col_code,'white')
3714   set(handles.slider1,'Visible','off')
3715   set(handles.slider2,'Visible','off')
3716   set(handles.colcode1,'Visible','off')
3717   set(handles.colcode2,'Visible','off')
3718   set(handles.AutoVecColor,'Visible','off')
3719   set_vec_col_bar(handles)
3720else
3721   set(handles.slider1,'Visible','on')
3722   set(handles.slider2,'Visible','on')
3723   set(handles.colcode1,'Visible','on')
3724   set(handles.colcode2,'Visible','on')
3725   set(handles.AutoVecColor,'Visible','on') 
3726   if isequal(col_code,'ima_cor')
3727       set(handles.AutoVecColor,'Value',0)%fixed scale by default
3728       set(handles.vec_col_bar,'Value',0)% 3 colors r,g,b by default
3729       set(handles.slider1,'Min',0);
3730       set(handles.slider1,'Max',1);
3731       set(handles.slider2,'Min',0);
3732       set(handles.slider2,'Max',1);
3733 %      set(handles.min_title_vec,'String','0')
3734       set(handles.max_vec,'String','1')
3735       set(handles.colcode1,'String','0.333')
3736       colcode1_Callback(hObject, eventdata, handles)
3737       set(handles.colcode2,'String','0.666')
3738       colcode2_Callback(hObject, eventdata, handles)
3739   else
3740       set(handles.AutoVecColor,'Value',1)%auto scale between min,max by default
3741       set(handles.vec_col_bar,'Value',1)% colormap 'jet' by default
3742       minval=get(handles.slider1,'Min');
3743       maxval=get(handles.slider1,'Max');
3744       set(handles.slider1,'Value',minval)
3745       set(handles.slider2,'Value',maxval)
3746       set_vec_col_bar(handles)
3747   end
3748%    slider_update(handles)
3749end
3750%replot the current graph
3751run0_Callback(hObject, eventdata, handles)
3752
3753
3754%----------------------------------------------------------------
3755% -- Executes on slider movement to set the color code
3756%
3757function slider1_Callback(hObject, eventdata, handles)
3758%------------------------------------------------------------------
3759slider1=get(handles.slider1,'Value');
3760min_val=str2num(get(handles.min_vec,'String'));
3761max_val=str2num(get(handles.max_vec,'String'));
3762col=min_val+(max_val-min_val)*slider1;
3763set(handles.colcode1,'String',num2str(col))
3764if(get(handles.slider2,'Value') < col)%move also the second slider at the same value if needed
3765    set(handles.slider2,'Value',col)
3766    set(handles.colcode2,'String',num2str(col))
3767end
3768colcode1_Callback(hObject, eventdata, handles)
3769
3770%----------------------------------------------------------------
3771% Executes on slider movement to set the color code
3772%----------------------------------------------------------------
3773function slider2_Callback(hObject, eventdata, handles)
3774slider2=get(handles.slider2,'Value');
3775min_val=str2num(get(handles.min_vec,'String'));
3776max_val=str2num(get(handles.max_vec,'String'));
3777col=min_val+(max_val-min_val)*slider2;
3778set(handles.colcode2,'String',num2str(col))
3779if(get(handles.slider1,'Value') > col)%move also the first slider at the same value if needed
3780    set(handles.slider1,'Value',col)
3781    set(handles.colcode1,'String',num2str(col))
3782end
3783colcode2_Callback(hObject, eventdata, handles)
3784
3785%----------------------------------------------------------------
3786%execute on return carriage on the edit box corresponding to slider 1
3787%----------------------------------------------------------------
3788function colcode1_Callback(hObject, eventdata, handles)
3789% col=str2num(get(handles.colcode1,'String'));
3790% set(handles.slider1,'Value',col)
3791set_vec_col_bar(handles)
3792update_plot(handles)
3793
3794%----------------------------------------------------------------
3795%execute on return carriage on the edit box corresponding to slider 2
3796%----------------------------------------------------------------
3797function colcode2_Callback(hObject, eventdata, handles)
3798% col=str2num(get(handles.colcode2,'String'));
3799% set(handles.slider2,'Value',col)
3800% slider2_Callback(hObject, eventdata, handles)
3801set_vec_col_bar(handles)
3802update_plot(handles)
3803%------------------------------------------------------------
3804%update the slider values after displaying vectors
3805%--------------------------------------------------------
3806% function slider_update(handles,auto,minC,colcode1,colcode2,maxC)
3807% set(handles.slider1,'Min',minC)
3808% set(handles.slider1,'Max',maxC)
3809% set(handles.slider2,'Min',minC)
3810% set(handles.slider2,'Max',maxC)
3811% set(handles.min_title_vec,'String',num2str(minC))
3812% set(handles.max_vec,'String',num2str(maxC))
3813% if auto
3814%         set(handles.colcode1,'String',num2str(colcode1,3))%update display
3815%         set(handles.colcode2,'String',num2str(colcode2,3))
3816% end
3817% set(handles.slider1,'Value',colcode1)%update slider with constant display
3818% set(handles.slider2,'Value',colcode2)
3819% set_vec_col_bar(handles)
3820
3821
3822%-------------------------------------------------------
3823% --- Executes on button press in AutoVecColor.
3824%-------------------------------------------------------
3825function vec_col_bar_Callback(hObject, eventdata, handles)
3826set_vec_col_bar(handles)
3827
3828% %--------------------------------------------
3829% %update the display of color code for vectors
3830% %--------------------------------------------
3831% function set_vec_col_bar(handles)
3832% %get the image of the color display button 'vec_col_bar' in pixels
3833% uni=get(handles.vec_col_bar,'Unit');
3834% set(handles.vec_col_bar,'Unit','pixel')
3835% pos_vert=get(handles.vec_col_bar,'Position');
3836% set(handles.vec_col_bar,'Unit','Normalized')
3837% width=ceil(pos_vert(3));
3838% height=ceil(pos_vert(4));
3839% %get slider indications
3840% colcode.min=get(handles.slider1,'Min');
3841% colcode.max=get(handles.slider1,'Max');
3842% colcode.colcode1=get(handles.slider1,'Value');
3843% colcode.colcode2=get(handles.slider2,'Value');
3844% colcode.option=get(handles.vec_col_bar,'Value');
3845% colcode.auto=1;
3846% list_code=get(handles.col_vec,'String');% list menu fields
3847% index_code=get(handles.col_vec,'Value');% selected string index
3848% colcode.CName= list_code{index_code(1)}; % selected field used for vector color
3849% vec_C=colcode.min+(colcode.max-colcode.min)*[0.5:width-0.5]/width;%sample of vec_C values from min to max
3850% [colorlist,col_vec]=set_col_vec(colcode,vec_C);
3851% oneheight=ones(1,height);
3852% A1=colorlist(col_vec,1)*oneheight;
3853% A2=colorlist(col_vec,2)*oneheight;
3854% A3=colorlist(col_vec,3)*oneheight;
3855% A(:,:,1)=A1';
3856% A(:,:,2)=A2';
3857% A(:,:,3)=A3';
3858% set(handles.vec_col_bar,'Cdata',A)
3859
3860%--------------------------------------------------------
3861% --- Executes on button press in cal.
3862function cal_Callback(hObject, eventdata, handles)
3863
3864
3865
3866%-------------------------------------------------------------
3867% --- Executes on selection change in transform_fct.
3868function transform_fct_Callback(hObject, eventdata, handles)
3869%-------------------------------------------------------------
3870global nb_builtin
3871
3872huvmat=get(handles.transform_fct,'parent');
3873menu=get(handles.transform_fct,'String');
3874ind_coord=get(handles.transform_fct,'Value');
3875coord_option=menu{ind_coord};
3876list_transform=get(handles.transform_fct,'UserData');
3877ff=functions(list_transform{end}); 
3878if isequal(coord_option,'more...');
3879    coord_fct='';
3880    prompt = {'Enter the name of the transform function'};
3881    dlg_title = 'user defined transform';
3882    num_lines= 1;
3883    [FileName, PathName, filterindex] = uigetfile( ...
3884       {'*.m', ' (*.m)';
3885        '*.m',  '.m files '; ...
3886        '*.*', 'All Files (*.*)'}, ...
3887        'Pick a file', ff.file);
3888    if isequal(PathName(end),'/')||isequal(PathName(end),'\')
3889        PathName(end)=[];
3890    end
3891    transform_selected =fullfile(PathName,FileName);
3892    if ~exist(transform_selected,'file')
3893           return
3894    end
3895   [ppp,transform,ext_fct]=fileparts(FileName);% removes extension .m
3896   if ~isequal(ext_fct,'.m')
3897        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
3898        return
3899   end
3900   menu=update_menu(handles.transform_fct,transform);%add the selected fct to the menu
3901   ind_coord=get(handles.transform_fct,'Value');
3902   addpath(PathName)
3903   list_transform{ind_coord}=str2func(transform);% create the function handle corresponding to the newly seleced function
3904   set(handles.transform_fct,'UserData',list_transform)
3905   rmpath(PathName)
3906   % save the new menu in the personal file 'uvmat_perso.mat'
3907   dir_perso=prefdir;%personal Matalb directory
3908   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
3909   if exist(profil_perso,'file')
3910       for ilist=nb_builtin+1:numel(list_transform)
3911           ff=functions(list_transform{ilist});
3912           transform_fct{ilist-nb_builtin}=ff.file;
3913       end
3914        save (profil_perso,'transform_fct','-append'); %store the root name for future opening of uvmat
3915   end   
3916end
3917
3918%check the current path to the selected function
3919if isa(list_transform{ind_coord},'function_handle')
3920    func=functions(list_transform{ind_coord});
3921    set(handles.path_transform,'String',fileparts(func.file)); %show the path to the senlected function
3922else
3923    set(handles.path_transform,'String','')
3924end
3925
3926set(handles.FixedLimits,'Value',0)
3927set(handles.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
3928
3929UvData=get(huvmat,'UserData');
3930
3931%delete drawn objects
3932hother=findobj('Tag','proj_object');%find all the proj objects
3933for iobj=1:length(hother)
3934    delete_object(hother(iobj))
3935end
3936hother=findobj('Tag','DeformPoint');%find all the proj objects
3937for iobj=1:length(hother)
3938    delete_object(hother(iobj))
3939end
3940hh=findobj('Tag','calib_points');
3941if ~isempty(hh)
3942    delete(hh)
3943end
3944hhh=findobj('Tag','calib_marker');
3945if ~isempty(hhh)
3946    delete(hhh)
3947end
3948if isfield(UvData,'Object')
3949     UvData.Object=UvData.Object(1);
3950end
3951list_object=get(handles.list_object_1,'String');
3952set(handles.list_object_1,'Value',1)
3953set(handles.list_object_1,'String',list_object(1))
3954set(handles.list_object_2,'Value',2)
3955set(handles.list_object_2,'String',[list_object(1);{'...'}])
3956list_object_2_Callback(hObject, eventdata, handles)
3957
3958
3959
3960%delete mask if it is displayed
3961if isequal(get(handles.mask_test,'Value'),1)%if the mask option is on
3962   UvData=rmfield(UvData,'MaskName'); %will impose mask refresh 
3963end
3964set(huvmat,'UserData',UvData)
3965run0_Callback(hObject, eventdata, handles)
3966
3967%--------------------------------------------
3968function histo1_menu_Callback(hObject, eventdata, handles)
3969%--------------------------------------------
3970%plot first histo
3971huvmat=get(handles.histo1_menu,'parent');
3972histo_menu=get(handles.histo1_menu,'String');
3973histo_value=get(handles.histo1_menu,'Value');
3974FieldName=histo_menu{histo_value};
3975UvData=get(huvmat,'UserData');
3976update_histo(handles.histo_u,huvmat,FieldName)
3977
3978%----------------------------------------------
3979function histo2_menu_Callback(hObject, eventdata, handles)
3980%----------------------------------------------
3981%plot second histo
3982huvmat=get(handles.histo2_menu,'parent');
3983histo_menu=get(handles.histo2_menu,'String');
3984histo_value=get(handles.histo2_menu,'Value');
3985FieldName=histo_menu{histo_value};
3986UvData=get(huvmat,'UserData');
3987update_histo(handles.histo_v,huvmat,FieldName)
3988
3989
3990%--------------------------------------------
3991%read the field .Fieldname stored in UvData and plot its histogram
3992function update_histo(haxes,huvmat,FieldName)
3993UvData=get(huvmat,'UserData');
3994
3995if ~isfield(UvData.Field,FieldName)
3996    msgbox_uvmat('ERROR',['no field  ' FieldName ' for histogram'])
3997    return
3998end
3999Field=UvData.Field;
4000FieldHisto=eval(['Field.' FieldName]);
4001if isfield(Field,'FF') & ~isempty(Field.FF) & isequal(size(Field.FF),size(FieldHisto))
4002    indsel=find(Field.FF==0);%find values marked as false
4003    if ~isempty(indsel)
4004        FieldHisto=FieldHisto(indsel);
4005    end
4006end
4007if isempty(Field)
4008    msgbox_uvmat('ERROR',['empty field ' FieldName])
4009else
4010    nxy=size(FieldHisto);
4011    Amin=double(min(min(min(FieldHisto))));%min of image
4012    Amax=double(max(max(max(FieldHisto))));%max of image
4013    if isequal(Amin,Amax)
4014       Histo.Txt=['uniform field =' num2str(Amin)];
4015    else
4016    Histo.ListVarName={FieldName,'histo'};
4017    if numel(nxy)==2
4018        Histo.VarDimName={FieldName,FieldName}; %dimensions for the histogram
4019    else %color images
4020        Histo.VarDimName={FieldName,{FieldName,'rgb'}}; %dimensions for the histogram
4021    end
4022    %unit
4023    units=[]; %default
4024    for ivar=1:numel(Field.ListVarName)   
4025        if strcmp(Field.ListVarName{ivar},FieldName)
4026            if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=ivar && isfield(Field.VarAttribute{ivar},'units')
4027                units=Field.VarAttribute{ivar}.units;
4028                break
4029            end
4030        end
4031    end
4032    if ~isempty(units)
4033        Histo.VarAttribute{1}.units=units;
4034    end
4035    eval(['Histo.' FieldName '=linspace(Amin,Amax,50);'])%absissa values for histo
4036    for col=1:size(FieldHisto,3)
4037        B=FieldHisto(:,:,col);
4038        C=reshape(double(B),1,nxy(1)*nxy(2));% reshape in a vector
4039       eval(['Histo.histo(:,col)=hist(C, Histo.' FieldName ');']);  %calculate histogram
4040    end
4041    set(haxes,'XLimMode','auto')%reset auto mode (after zoom effect)
4042    set(haxes,'YLimMode','auto')
4043    plot_field(Histo,haxes);
4044    end
4045end
4046
4047
4048
4049%------------------------------------------------
4050%CALLBACKS FOR PLOTTING PARAMETERS
4051%-------------------------------------------------
4052
4053%-----------------------------------------------------------------
4054function MinA_Callback(hObject, eventdata, handles)
4055%------------------------------------------
4056set(handles.AutoScal,'Value',1) %suppress auto mode
4057set(handles.AutoScal,'BackgroundColor',[1 1 0])
4058update_plot(handles)
4059
4060%-----------------------------------------------------------------
4061function MaxA_Callback(hObject, eventdata, handles)
4062%--------------------------------------------
4063set(handles.AutoScal,'Value',1) %suppress auto mode
4064set(handles.AutoScal,'BackgroundColor',[1 1 0])
4065update_plot(handles)
4066
4067%-----------------------------------------------
4068function AutoScal_Callback(hObject, eventdata, handles)
4069%--------------------------------------------
4070test=get(handles.AutoScal,'Value');
4071if test
4072    set(handles.AutoScal,'BackgroundColor',[1 1 0])
4073else
4074    set(handles.AutoScal,'BackgroundColor',[0.7 0.7 0.7])
4075    update_plot(handles);
4076%     set(handles.MinA,'String',num2str(ScalOut.MinA,3))
4077%     set(handles.MaxA,'String',num2str(ScalOut.MaxA,3))
4078end
4079
4080%-------------------------------------------------------------------
4081function BW_Callback(hObject, eventdata, handles)
4082%-------------------------------------------------------------------
4083update_plot(handles)
4084
4085%-------------------------------------------------------------------
4086function Contours_Callback(hObject, eventdata, handles)
4087%-------------------------------------------------------------------
4088val=get(handles.Contours,'Value');
4089if val==2
4090    set(handles.interval_txt,'Visible','on')
4091    set(handles.IncrA,'Visible','on')
4092else
4093    set(handles.interval_txt,'Visible','off')
4094    set(handles.IncrA,'Visible','off')
4095end
4096update_plot(handles)
4097
4098%-------------------------------------------------------------------
4099function IncrA_Callback(hObject, eventdata, handles)
4100%-------------------------------------------------------------------
4101update_plot(handles)
4102
4103%-------------------------------------------------------------------
4104function HideWarning_Callback(hObject, eventdata, handles)
4105%-------------------------------------------------------------------
4106update_plot(handles)
4107
4108%-------------------------------------------------------------------
4109function HideFalse_Callback(hObject, eventdata, handles)
4110%-------------------------------------------------------------------
4111update_plot(handles)
4112
4113%-------------------------------------------------------------------
4114function VecScale_Callback(hObject, eventdata, handles)
4115%-------------------------------------------------------------------
4116set(handles.AutoVec,'Value',1);
4117set(handles.AutoVec,'BackgroundColor',[1 1 0])
4118update_plot(handles)
4119
4120%-------------------------------------------------------------------
4121function AutoVec_Callback(hObject, eventdata, handles)
4122%-------------------------------------------------------------------
4123test=get(handles.AutoVec,'Value');
4124if test
4125    set(handles.AutoVec,'BackgroundColor',[1 1 0])
4126else
4127    update_plot(handles);
4128    %set(handles.VecScale,'String',num2str(ScalOut.VecScale,3))
4129    set(handles.AutoVec,'BackgroundColor',[0.7 0.7 0.7])
4130end
4131
4132%-------------------------------------------------------
4133% --- Executes on selection change in decimate4 (nb_vec/4).
4134%-------------------------------------------------------
4135function decimate4_Callback(hObject, eventdata, handles)
4136update_plot(handles)
4137
4138
4139%-------------------------------------------------------
4140% --- Executes on selection change in color_code menu
4141%-------------------------------------------------------
4142function color_code_Callback(hObject, eventdata, handles)
4143set_vec_col_bar(handles)
4144update_plot(handles);
4145
4146%-------------------------------------------------------
4147% --- Executes on button press in AutoVecColor.
4148%-------------------------------------------------------
4149function AutoVecColor_Callback(hObject, eventdata, handles)
4150test=get(handles.AutoVecColor,'Value');
4151if test
4152    set(handles.AutoVecColor,'BackgroundColor',[1 1 0])
4153else
4154    update_plot(handles);
4155    %set(handles.VecScale,'String',num2str(ScalOut.VecScale,3))
4156    set(handles.AutoVecColor,'BackgroundColor',[0.7 0.7 0.7])
4157end
4158%set_vec_col_bar(handles)
4159
4160%-------------------------------------------------------
4161% --- Executes on selection change in max_vec.
4162%-------------------------------------------------------
4163function min_vec_Callback(hObject, eventdata, handles)
4164max_vec_Callback(hObject, eventdata, handles)
4165
4166% --- Executes on selection change in max_vec.
4167function max_vec_Callback(hObject, eventdata, handles)
4168set(handles.AutoVecColor,'Value',1)
4169AutoVecColor_Callback(hObject, eventdata, handles)
4170min_val=str2num(get(handles.min_vec,'String'));
4171max_val=str2num(get(handles.max_vec,'String'));
4172slider1=get(handles.slider1,'Value');
4173slider2=get(handles.slider2,'Value');
4174colcode1=min_val+(max_val-min_val)*slider1;
4175colcode2=min_val+(max_val-min_val)*slider2;
4176set(handles.colcode1,'String',num2str(colcode1))
4177set(handles.colcode2,'String',num2str(colcode2))
4178update_plot(handles);
4179
4180%-------------------------------------------------------------------
4181%update the display of color code for vectors
4182function set_vec_col_bar(handles)
4183%-------------------------------------------------------------------
4184%get the image of the color display button 'vec_col_bar' in pixels
4185set(handles.vec_col_bar,'Unit','pixel');
4186pos_vert=get(handles.vec_col_bar,'Position');
4187set(handles.vec_col_bar,'Unit','Normalized');
4188width=ceil(pos_vert(3));
4189height=ceil(pos_vert(4));
4190
4191%get slider indications
4192list=get(handles.color_code,'String');
4193ichoice=get(handles.color_code,'Value');
4194colcode.ColorCode=list{ichoice};
4195colcode.MinC=str2num(get(handles.min_vec,'String'));
4196colcode.MaxC=str2num(get(handles.max_vec,'String'));
4197test3color=strcmp(colcode.ColorCode,'rgb') || strcmp(colcode.ColorCode,'bgr');
4198if test3color
4199    colcode.colcode1=str2num(get(handles.colcode1,'String'));
4200    colcode.colcode2=str2num(get(handles.colcode2,'String'));
4201end
4202% colcode.option=get(handles.vec_col_bar,'Value');
4203colcode.FixedCbounds=0;
4204% list_code=get(handles.col_vec,'String');% list menu fields
4205% index_code=get(handles.col_vec,'Value');% selected string index
4206% colcode.CName= list_code{index_code(1)}; % selected field used for vector color
4207colcode.FixedCbounds=1;
4208vec_C=colcode.MinC+(colcode.MaxC-colcode.MinC)*[0.5:width-0.5]/width;%sample of vec_C values from min to max
4209[colorlist,col_vec]=set_col_vec(colcode,vec_C);
4210oneheight=ones(1,height);
4211A1=colorlist(col_vec,1)*oneheight;
4212A2=colorlist(col_vec,2)*oneheight;
4213A3=colorlist(col_vec,3)*oneheight;
4214A(:,:,1)=A1';
4215A(:,:,2)=A2';
4216A(:,:,3)=A3';
4217set(handles.vec_col_bar,'Cdata',A)
4218
4219
4220%-------------------------------------------------------------------
4221function [PlotType,ScalOut]=update_plot(handles)
4222%-------------------------------------------------------------------
4223haxes= handles.axes3;
4224AxeData=get(haxes,'UserData');
4225PlotParam=read_plot_param(handles);
4226[PlotType,PlotParamOut]= plot_field(AxeData,haxes,PlotParam,1);
4227write_plot_param(handles,PlotParamOut); %update the auto plot parameters
4228
4229%-------------------------------------------------------------------
4230% --- Executes on button press in grid.
4231function grid_Callback(hObject, eventdata, handles)
4232
4233
4234
4235%-------------------------------------------------------------------
4236% --- Executes on selection change in edit.
4237function edit_Callback(hObject, eventdata, handles)
4238%-------------------------------------------------------------------
4239UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4240test=get(handles.edit,'Value');
4241if test
4242    UvData.MouseAction='edit_object';
4243    set(handles.edit,'BackgroundColor',[1,1,0]) 
4244    %suppress the other options
4245    set(handles.zoom,'Value',0)
4246    zoom_Callback(hObject, eventdata, handles)
4247    set(handles.edit_vect,'Value',0)
4248    edit_vect_Callback(hObject, eventdata, handles) 
4249    %list_object_Callback(hObject, eventdata, handles)
4250else
4251    UvData.MouseAction='none';
4252    set(handles.edit,'BackgroundColor',[0.7,0.7,0.7])   
4253end
4254set(handles.uvmat,'UserData',UvData);
4255hset_object=findobj(allchild(0),'Tag','set_object');
4256if ~isempty(hset_object)
4257    hhset_object=guidata(hset_object);
4258    if test
4259        set(hhset_object.PLOT,'enable','on');
4260    else
4261       set(hhset_object.PLOT,'enable','off');
4262    end
4263end
4264
4265%------------------------------------------------------------------------
4266% --- Executes on selection change in list_object_1.
4267function list_object_1_Callback(hObject, eventdata, handles)
4268%------------------------------------------------------------------------
4269list_str=get(handles.list_object_1,'String');
4270IndexObj=get(handles.list_object_1,'Value');
4271str_1=list_str{IndexObj};
4272val_2=get(handles.list_object_2,'Value');
4273str_2=get(handles.list_object_2,'String');
4274if isequal(val_2,IndexObj)% if the first selection is equal to the second, it will suppress the second
4275    set(handles.list_object_2,'Value',numel(str_2))%select the end of the list ('...')
4276    list_object_2_Callback(hObject, eventdata, handles)
4277end
4278update_object(handles,IndexObj,1,str_1)
4279
4280%------------------------------------------------------------------------
4281% --- Executes on selection change in list_object_1.
4282function list_object_2_Callback(hObject, eventdata, handles)
4283%------------------------------------------------------------------------
4284list_str=get(handles.list_object_2,'String');
4285IndexObj=get(handles.list_object_2,'Value');
4286if isequal(list_str{IndexObj},'...')
4287    hview_field=findobj(allchild(0),'Tag','view_field');
4288    if ~isempty(hview_field)
4289        delete(hview_field)
4290    end
4291else
4292    update_object(handles,IndexObj,2,list_str{IndexObj})
4293end
4294
4295%------------------------------------------------------------------------
4296function update_object(handles,IndexObj,option,ObjectName)
4297%------------------------------------------------------------------------
4298UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4299if ~(length(UvData.Object)>=IndexObj);
4300    return
4301end
4302ObjectData=UvData.Object{IndexObj};
4303ObjectData.Name=ObjectName;
4304if isequal(get(handles.edit,'Value'),1)
4305    ObjectData.enable_plot=1; % desable the PLOT option in the set_object GUI (editing mode
4306end
4307PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
4308ZBounds=0; % default
4309    if isfield(UvData,'ZMin') && isfield(UvData,'ZMax')
4310        ZBounds(1)=UvData.ZMin; %minimum for the Z slider
4311        ZBounds(2)=UvData.ZMax;%maximum for the Z slider
4312    end
4313hset_object=findobj(allchild(0),'tag','set_object');
4314if ~isempty(hset_object)
4315    delete(hset_object)% delete existing version of set_object
4316end
4317hset_object=set_object(ObjectData,PlotHandles,ZBounds);% call the set_object interface,
4318set(hset_object,'name',ObjectName)
4319
4320% %project the current field on the object and plot it
4321% ProjData= proj_field(UvData.Field,ObjectData,IndexObj);%project the current interface field on ObjectData
4322% if option==1%length(UvData.Object)>= IndexObj && isfield(UvData.Object{IndexObj},'plotaxes')&& ishandle(UvData.Object{IndexObj}.plotaxes)
4323%     plot_field(ProjData,handles.axes3,PlotHandles);
4324%     UvData.Object{IndexObj}.plotaxes=handles.axes3;
4325% else
4326%     UvData.Object{IndexObj}.plotaxes=view_field(ProjData);
4327% end
4328% set(handles.uvmat,'UserData',UvData)
4329% hother=findobj('Tag','proj_object');%find all the proj objects
4330% for iobj=1:length(hother)
4331%     if isequal(get(hother(iobj),'Type'),'rectangle')|isequal(get(hother(iobj),'Type'),'patch')
4332%         set(hother(iobj),'EdgeColor','b')
4333%         if isequal(get(hother(iobj),'FaceColor'),'m')
4334%             set(hother(iobj),'FaceColor','b')
4335%         end
4336%     elseif isequal(get(hother(iobj),'Type'),'image')
4337%            Acolor=get(hother(iobj),'CData');
4338%            Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
4339%            set(hother(iobj),'CData',Acolor);
4340%     else
4341%          set(hother(iobj),'Color','b')
4342%     end
4343%     set(hother(iobj),'Selected','off')
4344% end
4345hother=findobj('Tag','DeformPoint');
4346set(hother,'Color','b');
4347set(hother,'Selected','off')   
4348if isfield(ObjectData,'HandlesDisplay')
4349    for iview=1:length(ObjectData.HandlesDisplay)
4350        if ishandle(ObjectData.HandlesDisplay(iview))
4351            uistack(ObjectData.HandlesDisplay(iview),'top')
4352            linetype=get(ObjectData.HandlesDisplay(iview),'Type');
4353            if isequal(linetype,'line')
4354                set(ObjectData.HandlesDisplay(iview),'Color','m'); %set the selected object to magenta color
4355            elseif isequal(linetype,'rectangle')
4356                set(ObjectData.HandlesDisplay(iview),'EdgeColor','m'); %set the selected object to magenta color
4357            elseif isequal(linetype,'patch')
4358                set(ObjectData.HandlesDisplay(iview),'FaceColor','m'); %set the selected object to magenta color
4359            end
4360            SubObjectData=get(ObjectData.HandlesDisplay(iview),'UserData');
4361            if isfield(SubObjectData,'SubObject')& ishandle(SubObjectData.SubObject)
4362                uistack(SubObjectData.SubObject,'top')
4363                for iobj=1:length(SubObjectData.SubObject)
4364                    hsub=SubObjectData.SubObject(iobj);
4365                    if isequal(get(hsub,'Type'),'rectangle')
4366                        set(hsub,'EdgeColor','m'); %set the selected object to magenta color
4367                    elseif isequal(get(hsub,'Type'),'image')
4368                       Acolor=get(hsub,'CData');
4369                       Acolor(:,:,1)=Acolor(:,:,3);
4370                       set(hsub,'CData',Acolor);
4371                    else
4372                        set(hsub,'Color','m')
4373                    end
4374                end
4375            end
4376            if isfield(SubObjectData,'DeformPoint')& ishandle(SubObjectData.DeformPoint)
4377                set(SubObjectData.DeformPoint,'Color','m')
4378            end
4379        end
4380    end
4381end
4382pause(0.1)
4383figure(hset_object)%put set_object in front
4384
4385%------------------------------------------------------
4386% --- Executes on button press in Menu/Export/field in workspace.
4387%------------------------------------------------------
4388function MenuExportField_Callback(hObject, eventdata, handles)
4389global CurData
4390CurData=get(handles.uvmat,'UserData');
4391evalin('base','global CurData')%make CurData global in the workspace
4392display(['current field :'])
4393evalin('base','CurData') %display CurData in the workspace
4394commandwindow; %brings the Matlab command window to the front
4395
4396%------------------------------------------------------
4397% --- Executes on button press in Menu/Export/extract figure.
4398%------------------------------------------------------
4399function MenuExport_plot_Callback(hObject, eventdata, handles)
4400huvmat=get(handles.MenuExport_plot,'parent');
4401UvData=get(huvmat,'UserData');
4402hfig=figure;
4403newaxes=copyobj(handles.axes3,hfig);
4404map=colormap(handles.axes3);
4405colormap(map);%transmit the current colormap to the zoom fig
4406colorbar
4407
4408
4409% --------------------------------------------------------------------
4410function Insert_Callback(hObject, eventdata, handles)
4411
4412
4413% --------------------------------------------------------------------
4414function MenuHelp_Callback(hObject, eventdata, handles)
4415% --------------------------------------------------------------------
4416path_to_uvmat=which ('uvmat');% check the path of uvmat
4417pathelp=fileparts(path_to_uvmat);
4418helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
4419if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
4420else
4421    addpath (fullfile(pathelp,'uvmat_doc'))
4422    web(helpfile);
4423end
4424
4425
4426% --------------------------------------------------------------------
4427function MenuExportMovie_Callback(hObject, eventdata, handles)
4428% --------------------------------------------------------------------
4429set(handles.MenuExportMovie,'BusyAction','queue')% activate the button
4430huvmat=get(handles.run0,'parent');
4431UvData=get(huvmat,'UserData');
4432[xx,xx,FileBase]=read_file_boxes(handles);
4433 %read the current input file name
4434prompt = {'movie file name';'frames per second';'frame resolution (*[512x384] pixels)';'axis position relative to the frame';'total frame number (starting from the current uvmat display)'};
4435dlg_title = 'select properties of the output avi movie';
4436num_lines= 1;
4437nbfield_cell=get(handles.last_i,'String');
4438def     = {[FileBase '_out.avi'];'10';'1';'[0.03 0.05 0.95 0.92]';'10'};
4439answer = inputdlg(prompt,dlg_title,num_lines,def,'on');
4440aviname=answer{1};
4441fps=str2num(answer{2});
4442% check for existing file with output name aviname
4443if exist(aviname,'file')
4444    backup=aviname;
4445    testexist=2;
4446    while testexist==2
4447        backup=[backup '~'];
4448        testexist=exist(backup,'file');     
4449    end
4450    [success,message]=copyfile(aviname,backup);%make backup of the existing file
4451    if isequal(success,1)
4452        delete(aviname)%delete existing file
4453    else
4454        msgbox_uvmat('ERROR',message)
4455        return
4456    end
4457end
4458%create avi open
4459aviobj=avifile(aviname,'Compression','None','fps',fps);
4460
4461%display first view for tests
4462newfig=figure;
4463newaxes=copyobj(handles.axes3,newfig);%new plotting axes in the new figure
4464set(newaxes,'Tag','movieaxes')
4465nbpix=[512 384]*str2num(answer{3});
4466set(gcf,'Position',[1 1 nbpix])% resolution XVGA
4467set(newaxes,'Position',eval(answer{4}));
4468map=colormap(handles.axes3);
4469colormap(map);%transmit the current colormap to the zoom fig
4470msgbox_uvmat('INPUT_Y-N',{['adjust figure ' num2str(newfig) ' with its matlab edit menu '] ;...
4471        ['then press OK to get the avi movie as a copy of figure ' num2str(newfig) ' display']});
4472UvData.Object{1}.plotaxes=newaxes;% the axis in the new figure becomes the current main plotting axes
4473set(huvmat,'UserData',UvData);
4474increment=str2num(get(handles.increment_scan,'String')); %get the field increment d
4475set(handles.STOP,'Visible','on')
4476set(handles.speed,'Visible','on')
4477set(handles.speed_txt,'Visible','on')
4478set(handles.Movie,'BusyAction','queue')
4479
4480imin=str2num(get(handles.i1,'String'));
4481imax=str2num(answer{5});
4482% if isfield(UvData,'Time')
4483htitle=get(newaxes,'Title');
4484xlim=get(newaxes,'XLim');
4485ylim=get(newaxes,'YLim');
4486set(htitle,'Position',[xlim(2)+0.07*(xlim(2)-xlim(1)) ylim(2)-0.05*(ylim(2)-ylim(1)) 0])
4487time_str=get(handles.abs_time,'String');
4488set(htitle,'String',['t=' time_str])
4489set(handles.speed,'Value',1)
4490for i=1:imax
4491    if get(handles.speed,'Value')~=0 & isequal(get(handles.MenuExportMovie,'BusyAction'),'queue') % enable STOP command
4492            runpm(hObject,eventdata,handles,increment)
4493            drawnow
4494            time_str=get(handles.abs_time,'String');
4495            if ishandle(htitle)
4496             set(htitle,'String',['t=' time_str])
4497            end
4498            mov=getframe(newfig);
4499            aviobj=addframe(aviobj,mov);
4500    end
4501end
4502aviobj=close(aviobj);
4503UvData.Object{1}.plotaxes=handles.axes3;
4504set(huvmat,'UserData',UvData);
4505msgbox_uvmat('CONFIRMATION',{['movie ' aviname ' created '];['with ' num2str(imax) ' frames']})
4506
4507
4508% ------------------------------------------------------------------
4509function MenuCalib_Callback(hObject, eventdata, handles)
4510
4511UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4512
4513%suppress competing options
4514set(handles.zoom,'Value',0)
4515set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
4516set(handles.MenuMask,'enable','off')
4517set(handles.MenuGrid,'enable','off')
4518set(handles.MenuObject,'enable','off')
4519set(handles.MenuEdit,'enable','off')
4520set(handles.list_object_1,'Value',1)     
4521% initiate display of GUI geometry_calib
4522data=[]; %default
4523if isfield(UvData,'CoordType')
4524    data.CoordType=UvData.CoordType;
4525end
4526pos=get(handles.uvmat,'Position');
4527pos(1)=pos(1)+pos(3)-0.311+0.04; %0.311= width of the geometry_calib interface (units relative to the srcreen)
4528pos(2)=pos(2)-0.02;
4529[FileName,RootPath,FileBase,FileIndices,FileExt,SubDir]=read_file_boxes(handles);
4530[UvData.hset_object,UvData.sethandles]=geometry_calib(handles,pos,FileName);% call the set_object interface     
4531pos_uvmat=get(handles.uvmat,'Position');
4532
4533if isfield(UvData,'CalOrigin')
4534    pos_cal(1)=pos_uvmat(1)+UvData.CalOrigin(1)*pos_uvmat(3);
4535    pos_cal(2)=pos_uvmat(2)+UvData.CalOrigin(2)*pos_uvmat(4);
4536    pos_cal(3:4)=UvData.CalSize .* pos_uvmat(3:4);
4537    set(UvData.hset_object,'Position',pos_cal)
4538end
4539UvData.MouseAction='calib';
4540set(handles.uvmat,'UserData',UvData);
4541
4542
4543% ------------------------------------------------------------------
4544function MenuMask_Callback(hObject, eventdata, handles)
4545% set(handles.TOOLS_txt,'Visible','on')
4546% set(handles.frame_tools,'Visible','on')
4547% % set(handles.create,'Visible','on')
4548% % set(handles.create,'Value',1)
4549% % set(handles.create,'BackgroundColor',[1 1 0]) %visualise in yellow
4550% set(handles.save_mask,'Visible','on')
4551% set(handles.masklevel,'Visible','on')
4552% if isequal(get(handles.z_index,'Visible'),'on')
4553%     nb_slice=str2num(get(handles.nb_slice,'String'));
4554%     for ilist=1:nb_slice
4555%         list_index{ilist,1}=num2str(ilist);
4556%     end   
4557%     set(handles.masklevel,'String',list_index)
4558%     val=str2num(get(handles.z_index,'String'));
4559%     if val<=nb_slice
4560%         set(handles.masklevel,'Value',val)
4561%     end
4562% else
4563%     set(handles.masklevel,'String',{'1'})
4564%     set(handles.masklevel,'Value',1)
4565% end
4566% if ishandle(handles.UVMAT_title)
4567%     delete(handles.UVMAT_title)
4568% end
4569UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4570% set(handles.zoom,'Value',0)
4571% set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
4572% set(handles.edit_vect,'Value',0)
4573% edit_vect_Callback(hObject, eventdata, handles)
4574% set(handles.edit,'Value',0)
4575% set(handles.edit,'BackgroundColor',[0.7 0.7 0.7])
4576% set(handles.edit_vect,'Value',0) 
4577% edit_vect_Callback(hObject, eventdata, handles)
4578% set(handles.cal,'Value',0)
4579% set(handles.cal,'BackgroundColor',[0 1 0])
4580
4581%initiate the GUI set_object
4582% data.TITLE='MASK';
4583% if isfield(UvData,'CoordType')
4584%     data.CoordType=UvData.CoordType;
4585% end
4586% if isfield(UvData,'Mesh')&&~isempty(UvData.Mesh)
4587%     data.YMax=UvData.Mesh;
4588% elseif isfield(UvData.Field,'AX')&&isfield(UvData.Field,'AY')&& isfield(UvData.Field,'A')%only image
4589%     np=size(UvData.Field.A);
4590%     meshx=(UvData.Field.AX(end)-UvData.Field.AX(1))/(np(2)-1);
4591%     meshy=abs(UvData.Field.AY(end)-UvData.Field.AY(1))/(np(1)-1);
4592%     data.YMax=max(meshx,meshy);
4593%     data.DX=max(meshx,meshy);
4594% end
4595% data.Coord=[0 0 0]; %default
4596% PlotHandles=get_plot_handles(handles);%get the handles of the graphic objects setting the plotting parameters
4597% [hset_object,UvData.sethandles]=set_object(data,PlotHandles);% call the set_object interface
4598% pos_uvmat=get(handles.uvmat,'Position');
4599% if isfield(UvData,'SetObjectOrigin')
4600%     pos_set_object(1:2)=UvData.SetObjectOrigin + pos_uvmat(1:2);
4601%     pos_set_object(3:4)=UvData.SetObjectSize .* pos_uvmat(3:4);
4602%     set(hset_object,'Position',pos_set_object)
4603% end
4604ListObj=UvData.Object;
4605select=zeros(1,numel(ListObj));
4606for iobj=1:numel(ListObj);
4607    if strcmp(ListObj{iobj}.ProjMode,'mask_inside')||strcmp(ListObj{iobj}.ProjMode,'mask_outside')
4608        select(iobj)=1;
4609    end
4610end
4611val=find(select);
4612if isempty(val)
4613    msgbox_uvmat('ERROR','polygons must be first created by Projection object/mask polygon in the menu bar');
4614    return
4615else
4616    set(handles.list_object_1,'Max',2);%allow multiple selection
4617    set(handles.list_object_1,'Value',val);
4618%     answer=msgbox_uvmat('INPUT_Y-N',['make the mask image from ' num2str(numel(val)) ' polygons']);
4619%     if ~isempty(answer)
4620        flag=1;
4621        npx=size(UvData.Field.A,2);
4622        npy=size(UvData.Field.A,1);
4623        xi=[0.5:npx-0.5];
4624        yi=[0.5:npy-0.5];
4625        [Xi,Yi]=meshgrid(xi,yi);
4626        if isfield(UvData,'Object')
4627            for iobj=1:length(UvData.Object)
4628                ObjectData=UvData.Object{iobj};
4629                if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
4630                    flagobj=1;
4631                    testphys=0; %coordinates in pixels by default
4632                    if isfield(ObjectData,'CoordType') && isequal(ObjectData.CoordType,'phys')
4633                        if isfield(UvData,'XmlData')&& isfield(UvData.XmlData,'GeometryCalib')
4634                            Calib=UvData.XmlData.GeometryCalib;
4635                            testphys=1;
4636                        end
4637                    end
4638                    if isfield(ObjectData,'Coord')& isfield(ObjectData,'Style')
4639                        if isequal(ObjectData.Style,'polygon')
4640                            X=ObjectData.Coord(:,1);
4641                            Y=ObjectData.Coord(:,2);
4642                            if testphys
4643                                [X,Y]=px_XYZ(Calib,X,Y,0);% to generalise with 3D cases
4644                            end
4645                            flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside
4646                        elseif isequal(ObjectData.Style,'ellipse')
4647                            if testphys
4648                                %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4649                            end
4650                            RangeX=max(ObjectData.RangeX);
4651                            RangeY=max(ObjectData.RangeY);
4652                            X2Max=RangeX*RangeX;
4653                            Y2Max=RangeY*RangeY;
4654                            distX=(Xi-ObjectData.Coord(1,1));
4655                            distY=(Yi-ObjectData.Coord(1,2));
4656                            flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
4657                        elseif isequal(ObjectData.Style,'rectangle')
4658                            if testphys
4659                                %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4660                            end
4661                            distX=abs(Xi-ObjectData.Coord(1,1));
4662                            distY=abs(Yi-ObjectData.Coord(1,2));
4663                            flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
4664                        end
4665                        if isequal(ObjectData.ProjMode,'mask_outside')
4666                            flagobj=~flagobj;
4667                        end
4668                        flag=flag & flagobj;
4669                    end
4670                end
4671            end
4672        end
4673        % flag=~flag;
4674        %mask name
4675        RootPath=get(handles.RootPath,'String');
4676        RootFile=get(handles.RootFile,'String');
4677        if ~isempty(RootFile)&(isequal(RootFile(1),'/')| isequal(RootFile(1),'\'))
4678            RootFile(1)=[];
4679        end
4680        filebase=fullfile(RootPath,RootFile);
4681        list=get(handles.masklevel,'String');
4682        masknumber=num2str(length(list));
4683        maskindex=get(handles.masklevel,'Value');
4684        mask_name=name_generator([filebase '_' masknumber 'mask'],maskindex,1,'.png','_i');
4685        imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
4686        imflag=flipdim(imflag,1);
4687        % imflag=uint8(255*flag);% =0 for flag=0 (vectors=0 when
4688        % 20<imflag<200)
4689
4690        %display the mask
4691        %update_mask(handles,num_i1,num_j1)
4692        figure;
4693        vec=linspace(0,1,256);%define a linear greyscale colormap
4694        map=[vec' vec' vec'];
4695        colormap(map)
4696        image(imflag);
4697        answer=msgbox_uvmat('INPUT_TXT','mask file name:', mask_name)
4698        if ~strcmp(answer,'Cancel')
4699            imwrite(imflag,answer,'BitDepth',8);
4700        end
4701        set(list_object_1,'Value',1)
4702        set(list_object_1,'Max',1)
4703%     end
4704end       
4705     
4706
4707% UvData.MouseAction='create_object';
4708set(handles.uvmat,'UserData',UvData);
4709
4710% ------------------------------------------------------------------
4711%-- open the GUI set_grid.fig to create grid
4712function MenuGrid_Callback(hObject, eventdata, handles)
4713%UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4714
4715%suppress the other options if grid is chosen
4716set(handles.edit_vect,'Value',0)
4717edit_vect_Callback(hObject, eventdata, handles)
4718set(handles.edit,'BackgroundColor',[0.7 0.7 0.7])
4719set(handles.edit_vect,'Value',0) 
4720edit_vect_Callback(hObject, eventdata, handles)
4721set(handles.edit,'BackgroundColor',[0.7 0.7 0.7])
4722set(handles.list_object_1,'Value',1)     
4723
4724%prepare display of the set_grid GUI
4725FileName=read_file_boxes(handles);
4726CoordList=get(handles.transform_fct,'String');
4727val=get(handles.transform_fct,'Value');
4728set_grid(FileName,CoordList{val});% call the set_object interface
4729%set(handles.uvmat,'UserData',UvData);
4730
4731%----------------------------------------------------------------
4732% open the GUI 'series'
4733%----------------------------------------------------------------
4734function MenuSeries_Callback(hObject, eventdata, handles)
4735%-------------------------------------------------------------------
4736
4737[param.FileName]=read_file_boxes(handles);
4738if isequal(get(handles.SubField,'Value'),1)
4739    FileName_1=read_file_boxes_1(handles);%
4740    if ~isequal(FileName_1,param.FileName)
4741        param.FileName_1=FileName_1;
4742    end
4743end
4744param.NomType=get(handles.FileIndex,'UserData');
4745param.NomType_1=get(handles.FileIndex_1,'UserData');
4746param.comp_input=get(handles.fix_pair,'Value');
4747huvmat=get(handles.MenuSeries,'parent');
4748UvData=get(huvmat,'UserData');
4749if isfield(UvData,'Time')
4750    param.Time=UvData.XmlData.Time;
4751end
4752if isequal(get(handles.scan_i,'Value'),1)
4753    param.incr_i=str2num(get(handles.increment_scan,'String'));
4754elseif isequal(get(handles.scan_j,'Value'),1)
4755    param.incr_j=str2num(get(handles.increment_scan,'String'));
4756end
4757param.list_fields=get(handles.Fields,'String');% list menu fields
4758param.list_fields(1)=[]; %suppress  'image' option
4759param.index_fields=get(handles.Fields,'Value');% selected string index
4760if param.index_fields>1
4761    param.index_fields=param.index_fields-1;
4762end
4763param.list_fields_1=get(handles.Fields_1,'String');% list menu fields
4764param.list_fields_1(1)=[]; %suppress  'image' option
4765param.index_fields_1=get(handles.Fields_1,'Value')-1;% selected string index
4766if param.index_fields_1>1
4767    param.index_fields_1=param.index_fields_1-1;
4768end
4769param.civ1=get(handles.civ1,'Value');
4770param.civ2=get(handles.civ2,'Value');
4771param.interp1=get(handles.interp1,'Value');
4772param.interp2=get(handles.interp2,'Value');
4773param.filter1=get(handles.filter1,'Value');
4774param.filter2=get(handles.filter2,'Value');
4775param.menu_coord_str=get(handles.transform_fct,'String');
4776param.menu_coord_val=get(handles.transform_fct,'Value');
4777
4778series(param); %run the series interface
4779
4780% ------------------------------------------------------------------
4781% -- open the GUI civ.fig for civx (PIV)
4782% ------------------------------------------------------------------
4783function MenuPIV_Callback(hObject, eventdata, handles)
4784 
4785[FileName,RootPath,filebase,FileIndices,ext,SubDir]=read_file_boxes(handles);
4786num1=stra2num(get(handles.i1,'String'));
4787num2=stra2num(get(handles.i2,'String'));
4788num_a=stra2num(get(handles.j1,'String'));
4789num_b=stra2num(get(handles.j2,'String'));
4790NomType=get(handles.FileIndex,'UserData');
4791ind_opening=1; % default (images): will advice civ1 option by default in the civ interface
4792if isequal(ext,'.nc') ||  isequal(ext,'.cdf')% netcdf files
4793    ind_opening=2;% propose 'fix' as the default option
4794% +read the current netcdf rootfile
4795    Data=nc2struct(FileName,[]);
4796    if isfield(Data,'fix') & isequal(Data.fix,1)
4797        ind_opening=3;
4798    end
4799    if isfield(Data,'patch') & isequal(Data.patch,1)
4800        ind_opening=4;
4801    end
4802    if isfield(Data,'civ2') & isequal(Data.civ2,1)
4803        ind_opening=5;
4804    end
4805    if isfield(Data,'fix2') & isequal(Data.fix2,1)
4806        ind_opening=6;
4807    end
4808end     
4809param.RootName=filebase;
4810param.NomType=NomType;
4811param.num1=num1;
4812param.num2=num2;
4813param.num_a=num_a;
4814param.num_b=num_b;
4815param.SubDir=SubDir;
4816param.IndOpening=ind_opening;% A REVOIR +TRANSMETTRE IMADOC INFO
4817param.ImaExt=ext;
4818civ(param);% interface de civ(not in the uvmat file)
4819
4820% ------------------------------------------------------------------
4821function MenuTools_Callback(hObject, eventdata, handles)
4822
4823% ------------------------------------------------------------------
4824function MenuEditObject_Callback(hObject, eventdata, handles)
4825set(handles.edit,'Value',1)
4826edit_Callback(hObject, eventdata, handles)
4827
4828%--------------------------------------------------------------------------
4829function enable_transform(handles,state)
4830set(handles.transform_fct,'Visible',state)
4831set(handles.TRANSFORM_txt,'Visible',state)   
4832set(handles.transform_fct,'Visible',state) 
4833set(handles.path_transform,'Visible',state)
4834set(handles.pxcmx_txt,'Visible',state)
4835set(handles.pxcmy_txt,'Visible',state)
4836set(handles.pxcm,'Visible',state)
4837set(handles.pycm,'Visible',state)
4838
4839%------------------------------------------------------------------------
4840function MenuEditVectors_Callback(hObject, eventdata, handles)
4841%------------------------------------------------------------------------
4842set(handles.edit_vect,'Visible','on')
4843set(handles.edit_vect,'Value',1)
4844edit_vect_Callback(hObject, eventdata, handles)
4845
4846% --------------------------------------------------------------------
4847function Menupoints_Callback(hObject, eventdata, handles)
4848data.Style='points';
4849data.ProjMode='projection';%default
4850create_object(data,handles)
4851
4852% --------------------------------------------------------------------
4853function Menuline_Callback(hObject, eventdata, handles)
4854% set(handles.create,'Visible','on')
4855% set(handles.create,'Value',1)
4856% LINE_Callback(hObject,eventdata,handles)
4857data.Style='line';
4858data.ProjMode='projection';%default
4859create_object(data,handles)
4860
4861%------------------------------------------------------------------------
4862function Menupolyline_Callback(hObject, eventdata, handles)
4863%------------------------------------------------------------------------
4864data.Style='polyline';
4865data.ProjMode='projection';%default
4866create_object(data,handles)
4867
4868%------------------------------------------------------------------------
4869function Menupolygon_Callback(hObject, eventdata, handles)
4870%------------------------------------------------------------------------
4871data.Style='polygon';
4872data.ProjMode='inside';%default
4873create_object(data,handles)
4874
4875%------------------------------------------------------------------------
4876function Menurectangle_Callback(hObject, eventdata, handles)
4877%------------------------------------------------------------------------
4878data.Style='rectangle';
4879data.ProjMode='inside';%default
4880create_object(data,handles)
4881
4882%------------------------------------------------------------------------
4883function Menuellipse_Callback(hObject, eventdata, handles)
4884%------------------------------------------------------------------------
4885data.Style='ellipse';
4886data.ProjMode='inside';%default
4887create_object(data,handles)
4888
4889%------------------------------------------------------------------------
4890function MenuMaskObject_Callback(hObject, eventdata, handles)
4891%------------------------------------------------------------------------
4892data.Style='polygon';
4893data.StyleMenu={'polygon'};
4894data.ProjMode='mask_inside';%default
4895data.ProjMenu={'mask_inside';'mask_outside'};
4896create_object(data,handles)
4897
4898% ------------------------------------------------------------------
4899function Menuplane_Callback(hObject, eventdata, handles)
4900data.Style='plane';
4901data.ProjMode='projection';%default
4902
4903create_object(data,handles)
4904
4905% ------------------------------------------------------------------
4906function Menuvolume_Callback(hObject, eventdata, handles)
4907data.Style='volume';
4908data.ProjMode='interp';%default
4909% set(handles.create,'Visible','on')
4910% set(handles.create,'Value',1)
4911% VOLUME_Callback(hObject,eventdata,handles)
4912create_object(data,handles)
4913
4914%------------------------------------------------------------------------
4915function MenuBrowseObject_Callback(hObject, eventdata, handles)
4916%------------------------------------------------------------------------
4917%get the object file
4918[FileName, PathName, filterindex] = uigetfile( ...
4919       {'*.xml;*.mat', ' (*.xml,*.mat)';
4920       '*.xml',  '.xml files '; ...
4921        '*.mat',  '.mat matlab files '}, ...
4922        'Pick a file',get(handles.RootPath,'String'));
4923fileinput=[PathName FileName];%complete file name
4924testblank=findstr(fileinput,' ');%look for blanks
4925if ~isempty(testblank)
4926    msgbox_uvmat('ERROR','forbidden input file name: contain blanks')
4927    return
4928end
4929sizf=size(fileinput);
4930if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
4931
4932%read the file
4933t=xmltree(fileinput);
4934data=convert(t);
4935data.enable_plot=1;
4936[pp,data.Name]=fileparts(FileName);
4937%PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
4938hset_object=findobj(allchild(0),'Name','set_object');
4939if ~isempty(hset_object)
4940    delete(hset_object)% delete existing version of set_object
4941end
4942UvData=get(handles.uvmat,'UserData');
4943[hset_object,UvData.sethandles]=set_object(data);% call the set_object interface
4944% %position the set_object GUI with respect to uvmat
4945% pos_uvmat=get(handles.uvmat,'Position');
4946% if isfield(UvData,'SetObjectOrigin')
4947%     pos_set_object(1:2)=UvData.SetObjectOrigin + pos_uvmat(1:2);
4948%     pos_set_object(3:4)=UvData.SetObjectSize .* pos_uvmat(3:4);
4949%     set(hset_object,'Position',pos_set_object)
4950% end
4951set(handles.edit,'Value',0); %suppress the object edit mode
4952set(handles.edit,'BackgroundColor',[0.7,0.7,0.7]) 
4953UvData.MouseAction='create_object';
4954set(handles.uvmat,'UserData',UvData)
4955set(handles.delete_object,'Visible','on')
4956set(handles.uvmat_title,'Visible','on')
4957set(handles.view_field_title,'Visible','on')
4958
4959%------------------------------------------------------------------------
4960% --- generic function used for the creation of a projection object
4961function create_object(data,handles)
4962%------------------------------------------------------------------------
4963hset_object=findobj(allchild(0),'tag','set_object');
4964if ~isempty(hset_object)
4965    delete(hset_object)% delete existing version of set_object
4966end
4967UvData=get(handles.uvmat,'UserData');
4968set(handles.edit,'Value',0); %suppress the object edit mode
4969set(handles.edit,'BackgroundColor',[0.7,0.7,0.7]) 
4970data.enable_plot=1;
4971transform_list=get(handles.transform_fct,'String');
4972val=get(handles.transform_fct,'Value');
4973data.CoordType=transform_list{val};
4974% if isfield(UvData,'CoordType')
4975%     data.CoordType=UvData.CoordType;
4976% end
4977if isfield(UvData,'Mesh')&&~isempty(UvData.Mesh)
4978    data.RangeX=UvData.Mesh;
4979    data.RangeY=UvData.Mesh;
4980    data.DX=UvData.Mesh;
4981    data.DY=UvData.Mesh;
4982elseif isfield(UvData.Field,'AX')&& isfield(UvData.Field,'AY')&& isfield(UvData.Field,'A')%only image
4983    np=size(UvData.Field.A);
4984    meshx=(UvData.Field.AX(end)-UvData.Field.AX(1))/np(2);
4985    meshy=abs(UvData.Field.AY(end)-UvData.Field.AY(1))/np(1);
4986    data.RangeY=max(meshx,meshy);
4987    data.RangeX=max(meshx,meshy);
4988    data.DX=max(meshx,meshy);
4989end
4990if isfield(UvData,'NbDim')
4991    data.NbDim=UvData.NbDim;
4992end
4993data.Coord=[0 0 0]; %default
4994if isfield(data,'Style') && isequal(data.Style,'line')
4995    if isfield(data,'DX')
4996        data.Coord=[[0 0 0];[data.DX 0 0]]; %default
4997    else
4998        data.Coord=[[0 0 0];[1 0 0]]; %default
4999    end
5000end
5001%data.ParentButton=handles.create;
5002if ishandle(handles.UVMAT_title)
5003    delete(handles.UVMAT_title)%delete the initial display of uvmat if no field has been entered
5004end
5005PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
5006[hset_object,UvData.sethandles]=set_object(data,PlotHandles);% call the set_object interface
5007
5008UvData.MouseAction='create_object';
5009set(handles.uvmat,'UserData',UvData)
5010set(handles.zoom,'Value',0)
5011zoom_Callback(handles.uvmat, [], handles)
5012set(handles.delete_object,'Visible','on')
5013set(handles.uvmat_title,'Visible','on')
5014set(handles.view_field_title,'Visible','on')
5015
5016%------------------------------------------------------------------------
5017function MenuRuler_Callback(hObject, eventdata, handles)
5018%------------------------------------------------------------------------
5019set(handles.zoom,'Value',0)
5020zoom_Callback(handles.uvmat, [], handles)
5021UvData=get(handles.uvmat,'UserData');
5022UvData.MouseAction='ruler';
5023set(handles.uvmat,'UserData',UvData);
5024
5025%------------------------------------------------------------------------
5026% --- executed when closing: set the parent interface button to value 0
5027function closefcn(gcbo,eventdata)
5028%------------------------------------------------------------------------
5029%delete all the associated figures if exist
5030hh=findobj(allchild(0),'tag','view_field');
5031if ~isempty(hh)
5032    delete(hh)
5033end
5034hh=findobj(allchild(0),'name','geometry_calib');
5035if ~isempty(hh)
5036    delete(hh)
5037end
5038hh=findobj(allchild(0),'tag','set_object');
5039if ~isempty(hh)
5040    hhh=findobj(hh,'tag','PLOT');
5041    set(hhh,'enable','off')
5042end
5043
5044
5045% --- Executes on button press in delete_object.
5046function delete_object_Callback(hObject, eventdata, handles)
5047IndexObj=get(handles.list_object_2,'Value');
5048    if IndexObj>1
5049        delete_object(IndexObj)
5050    end
5051   
5052
5053
5054
5055
Note: See TracBrowser for help on using the repository browser.