source: trunk/src/uvmat.m @ 410

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

projection on object improved: two menus for object projection dispalyed in uvmat and in view_field

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