source: trunk/src/uvmat.m @ 533

Last change on this file since 533 was 530, checked in by sommeria, 12 years ago

new conventions for find_field_cells .

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