source: trunk/src/uvmat.m @ 508

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

various improvements

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