source: trunk/src/uvmat.m @ 180

Last change on this file since 180 was 180, checked in by sommeria, 13 years ago

rationalisation of uvmat, introduction of the new function read_field, links with get_field, several bug repairs

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