source: trunk/src/uvmat.m @ 515

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

improvement of calc-field and combination of two fields

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