source: trunk/src/uvmat.m @ 183

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

small bug repair: reading movies in read_fields, plotting rectangles and ellipes

File size: 211.6 KB
Line 
1%'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization
2%------------------------------------------------------------------------
3% function huvmat=uvmat(input)
4%
5%OUTPUT
6% huvmat=current handles of the GUI uvmat.fig
7%%
8%
9%INPUT:
10% input: input file name (if character chain), or input image matrix to
11% visualize, or Matlab structure representing  netcdf fields (with fields
12% ListVarName....)
13%
14%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
15%  Copyright Joel Sommeria,  2008, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
16%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
17%     This open is part of the toolbox UVMAT.
18%
19%     UVMAT is free software; you can redistribute it and/or modify
20%     it under the terms of the GNU General Public License as published by
21%     the Free Software Foundation; either version 2 of the License, or
22%     (at your option) any later version.
23%
24%     UVMAT is distributed in the hope that it will be useful,
25%     but WITHOUT ANY WARRANTY; without even the implied warranty of
26%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27%     GNU General Public License (open UVMAT/COPYING.txt) for more details.
28%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
29%
30% Information stored on the interface:
31%    'Strings' of all edit boxes and menus: get(handles.Tag,'String')
32%    'Values' of all menus and toggle buttons: get(handles.Tag,'Value')
33%     Matlab structure called UvData stored as 'UserData' of the figure uvmat.fig,(can be obtained by right mouse click on the interface).
34%          It contains the following fields:
35%     - Fixed specifiacation of plotting figures and axes (defined bu uvmat_OpeningFcn)
36%          .PosColorbar: [0.8210 0.4710 0.0190 0.4450]; specified position of the colorbar on figures
37%     - Information read in the documentation open of a series (activated by RootPath_Callback) :
38%          .XmlData, with fields:
39%               .Time: matrix of times of the images with index i and j
40%               .GeometryCalib: [1x1 struct]
41%     - Information defined from the interface:
42%           .NewSeries: =1 when the first view of a new field series is displayed, else 0
43%           .filename:(char string)
44%           .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,num_i1);
2162    if ~isempty(errormsg)
2163        errormsg=['error in reading ' filename ': ' errormsg];
2164        return
2165    end
2166    if isfield(ParamOut,'Npx')&& isfield(ParamOut,'Npy')
2167        set(handles.npx,'String',num2str(ParamOut.Npx));% display image size on the interface
2168        set(handles.npy,'String',num2str(ParamOut.Npy));
2169    end
2170end
2171
2172%% choose a second field if Subfield option is 'on', and if the field has changed
2173VelType_1=[];%default
2174FieldName_1=[];
2175ParamOut_1=[];
2176if ~isempty(filename_1)
2177    if ~exist(filename_1,'file')
2178        errormsg=['second file ' filename_1 ' does not exist'];
2179        return
2180    else
2181        Name=filename_1;
2182        FieldName_1=[];%default
2183        VelType_1=[];%default
2184        Ext_1=get(handles.FileExt_1,'String');
2185        NomType_1=get(handles.FileIndex_1,'UserData');
2186        if isequal(Ext_1,'.nc')||isequal(Ext_1,'.cdf')
2187            FileType_1='netcdf';
2188        elseif isfield(UvData,'MovieObject_1')
2189            Name=UvData.MovieObject_1;
2190            FileType_1='movie';
2191        elseif isequal(lower(Ext_1),'.avi')
2192            FileType_1='avi';
2193        elseif isequal(lower(Ext_1),'.vol')
2194            FileType_1='vol';
2195            if isfield(UvData.XmlData_1,'Npy') && isfield(UvData.XmlData_1,'Npx')
2196                ParamIn.Npy=UvData.XmlData_1.Npy;
2197                ParamIn.Npx=UvData.XmlData_1.Npx;
2198            else
2199                errormsg='Npx and Npy need to be defined in the xml file for volume images .vol';
2200                return
2201            end
2202        else
2203           form=imformats(Ext_1(2:end));
2204           if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2205               if isequal(NomType_1,'*');
2206                   FileType_1='multimage';
2207               else
2208                   FileType_1='image';
2209               end
2210           end
2211        end
2212        if strcmp(FileType_1,'netcdf')
2213            list_fields=get(handles.Fields_1,'String');% list menu fields
2214            index_fields=get(handles.Fields_1,'Value');% selected string index
2215            FieldName_1= list_fields{index_fields}; % selected field
2216            if ~isequal(FieldName_1,'get_field...')% read the field names on the interface get_field...
2217                VelType_1=setfield_1(handles);
2218            end
2219            if strcmp(VelType_1,'*')% free veltype choice
2220                VelType_1=[];
2221            elseif strcmp(VelType_1,'"')% veltype the same as for the first field
2222                if isempty(VelType)
2223                    VelType_1=[];
2224                else
2225                    VelType_1=VelType;
2226                end
2227            end
2228            if strcmp(FieldName_1,'velocity')
2229                list_code=get(handles.color_code,'String');% list menu fields
2230                index_code=get(handles.color_code,'Value');% selected string index
2231                if  ~strcmp(list_code{index_code},'black') &&  ~strcmp(list_code{index_code},'white')
2232                    list_code=get(handles.col_vec,'String');% list menu fields
2233                    index_code=get(handles.col_vec,'Value');% selected string index
2234                    ParamIn.ColorVar= list_code{index_code}; % selected field
2235                end
2236            end
2237        end
2238        test_keepdata_1=0;% test for keeping the previous stored data if the input files are unchanged
2239        if ~isequal(NomType_1,'*')%in case of a series of files (not avi movie)
2240            if isfield(UvData,'filename_1')&& isfield(UvData,'VelType_1') && isfield(UvData,'FieldName_1')
2241                test_keepdata_1= strcmp(filename_1,UvData.filename_1) && strcmp(VelType_1,UvData.VelType_1) && strcmp(FieldName_1,UvData.FieldName_1);
2242            end
2243        end
2244        if test_keepdata_1
2245            Field{2}=UvData.Field_1;
2246        else
2247            ParamIn.FieldName=FieldName_1;
2248            ParamIn.VelType=VelType_1;
2249            ParamIn.GUIName='get_field_1';
2250            [Field{2},ParamOut_1,errormsg] = read_field(Name,FileType_1,ParamIn,num_i1);
2251            if ~isempty(errormsg)
2252                errormsg=['error in reading ' filename_1 ': ' errormsg];
2253                return
2254            end
2255            UvData.Field_1=Field{2}; %store the second field for possible use at next RUN
2256        end
2257    end
2258end
2259
2260%% update uvmat interface
2261if isfield(ParamOut,'Npx')
2262    set(handles.npx,'String',num2str(ParamOut.Npx));% display image size on the interface
2263    set(handles.npy,'String',num2str(ParamOut.Npy));
2264elseif isfield(ParamOut_1,'Npx')
2265    set(handles.npx,'String',num2str(ParamOut_1.Npx));% display image size on the interface
2266    set(handles.npy,'String',num2str(ParamOut_1.Npy));
2267end
2268
2269%% update the display buttons for the first velocity type (first menuline)
2270veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
2271if ~isequal(FileType,'netcdf')|| isequal(FieldName,'get_field...')
2272%     reset_vel_type(veltype_handles)
2273    set(veltype_handles,'Visible','off')
2274else% if isempty(ParamOut.VelType) && ~isequal(FieldName,'get_field...')
2275    set_veltype_display(veltype_handles,ParamOut.CivStage)%update the display of available velocity types for the first field
2276    if isempty(ParamOut.VelType)
2277        reset_vel_type(veltype_handles)
2278    else
2279        handle1=eval(['handles.' ParamOut.VelType]);
2280        reset_vel_type(veltype_handles,handle1)
2281    end
2282end
2283field_index=strcmp(ParamOut.FieldName,ParamOut.FieldList);
2284set(handles.Fields,'String',ParamOut.FieldList); %update the field menu
2285set(handles.Fields,'Value',find(field_index,1))
2286
2287%% update the display buttons for the second velocity type (second menuline)
2288if ~isempty(filename_1)
2289    veltype_handles_1=[handles.civ1_1 handles.interp1_1 handles.filter1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1];
2290    if ~isequal(FileType_1,'netcdf')|| isequal(FieldName_1,'get_field...')
2291        set(veltype_handles_1,'Visible','off')
2292        %reset_vel_type(veltype_handles_1)
2293    else %if isempty(VelType_1) && ~isequal(FieldName_1,'get_field...')
2294        set_veltype_display(veltype_handles_1,ParamOut_1.CivStage)%update the display of available velocity types for the first field
2295        if isempty(ParamOut_1.VelType)
2296            reset_vel_type(veltype_handles_1)
2297        else
2298            handle1=eval(['handles.' ParamOut_1.VelType '_1']);
2299            reset_vel_type(veltype_handles_1,handle1)
2300        end
2301    end
2302    field_index=strcmp(ParamOut_1.FieldName,ParamOut_1.FieldList);
2303    set(handles.Fields_1,'String',ParamOut_1.FieldList); %update the field menu
2304    set(handles.Fields_1,'Value',find(field_index,1))
2305end
2306
2307%% introduce w as background image by default for a new series (only for nbdim=2)
2308if ~isfield(UvData,'NewSeries')
2309    UvData.NewSeries=1;
2310end
2311%put W as background image by default if NbDim=2:
2312if  UvData.NewSeries && isequal(get(handles.SubField,'Value'),0) && isfield(Field{1},'W') && ~isempty(Field{1}.W) && ~isequal(Field{1}.NbDim,3);
2313        set(handles.SubField,'Value',1);
2314        %menu=update_menu(handles.Fields_1,'w');%update the menu for the background scalar nd set the choice to 'w'
2315        set(handles.RootPath_1,'String','"')
2316        set(handles.RootFile_1,'String','"')
2317        set(handles.SubDir_1,'String','"');
2318        [indices]=name_generator('',num_i1,num_j1,'',NomType,1,num_i2,num_j2,'');
2319        set(handles.FileIndex_1,'String',indices)
2320        set(handles.FileExt_1,'String','"');
2321        set(handles.Fields_1,'Visible','on');
2322        set(handles.Fields_1,'Visible','on');
2323        set(handles.RootPath_1,'Visible','on')
2324        set(handles.RootFile_1,'Visible','on')
2325        set(handles.SubDir_1,'Visible','on');
2326        set(handles.FileIndex_1,'Visible','on');
2327        set(handles.FileExt_1,'Visible','on');
2328        set(handles.Fields_1,'Visible','on');
2329        Field{1}.AName='w';
2330end           
2331
2332%% store the current open names, fields and vel types in uvmat interface
2333UvData.filename_1=filename_1;
2334UvData.VelType_1=[];%default
2335UvData.FieldName_1=[];
2336if isfield(ParamOut_1,VelType)
2337    UvData.VelType_1=ParamOut_1.VelType;
2338end
2339if isfield(ParamOut_1,FieldName)
2340    UvData.FieldName_1=ParamOut_1.FieldName;
2341end
2342
2343%% apply coordinate transform or other user fct
2344XmlData=[];%default
2345if isfield(UvData,'XmlData')%use geometry calib recorded from the ImaDoc xml file as first priority
2346    XmlData=UvData.XmlData;
2347end
2348XmlData_1=[];%default
2349if isfield(UvData,'XmlData_1')
2350   XmlData_1=UvData.XmlData_1;
2351end
2352% menu_transform=get(handles.transform_fct,'String');
2353choice_value=get(handles.transform_fct,'Value');
2354transform_list=get(handles.transform_fct,'UserData');
2355transform=transform_list{choice_value};%selected function handles
2356% z index
2357if ~isempty(filename)
2358    Field{1}.ZIndex=z_index;
2359end
2360%px to phys or other transform on field
2361if ~isempty(transform)
2362    if length(Field)>=2
2363        Field{2}.ZIndex=z_index;
2364        [Field{1},Field{2}]=transform(Field{1},XmlData,Field{2},XmlData_1);
2365        if isempty(Field{2})
2366            Field(2)=[];
2367        end
2368    else
2369        Field{1}=transform(Field{1},XmlData);
2370    end
2371end
2372
2373%% calculate scalar
2374if isequal(FileType,'netcdf') && ~isequal(ParamOut.CivStage,0)%&&~isempty(FieldName)%
2375    Field{1}=calc_field([{ParamOut.FieldName} {ParamOut.ColorVar}],Field{1});
2376end
2377if length(Field)==2 && ~test_keepdata_1 && isequal(FileType_1,'netcdf') && ~isequal(ParamOut_1.FieldName,'get_field...')%&&~isempty(FieldName_1)
2378    Field{2}=calc_field([{ParamOut_1.FieldName} {ParamOut_1.ColorVar}],Field{2});
2379end
2380
2381%% combine the two input fields (e.g. substract velocity fields)
2382if numel(Field)==2
2383   UvData.Field=sub_field(Field{1},Field{2}); 
2384else
2385   UvData.Field=Field{1};
2386end
2387
2388%% get bounds and mesh (needed for mouse action and to open set_object)
2389test_x=0;
2390test_z=0;% test for unstructured z coordinate
2391[UvData.Field,errormsg]=check_field_structure(UvData.Field);
2392if ~isempty(errormsg)
2393    errormsg=['error in uvmat/refresh_field/check_field_structure: ' errormsg];
2394    return
2395end
2396[CellVarIndex,NbDim,VarType,errormsg]=find_field_indices(UvData.Field);
2397if ~isempty(errormsg)
2398    errormsg=['error in uvmat/refresh_field/find_field_indices: ' errormsg];
2399    return
2400end
2401[NbDim,imax]=max(NbDim);
2402if ~isempty(VarType{imax}.coord_x)  && ~isempty(VarType{imax}.coord_y)    %unstructured coordinates
2403    XName=UvData.Field.ListVarName{VarType{imax}.coord_x};
2404    YName=UvData.Field.ListVarName{VarType{imax}.coord_y};
2405    eval(['nbvec=length(UvData.Field.' XName ');'])%nbre of measurement points (e.g. vectors)
2406    test_x=1;%test for unstructured coordinates
2407else %structured coordinate
2408    YName=UvData.Field.ListVarName{VarType{imax}.coord(NbDim-1)}; %structured coordinates
2409    XName=UvData.Field.ListVarName{VarType{imax}.coord(NbDim)};
2410end
2411if NbDim==3
2412    ZName=UvData.Field.ListVarName{VarType{imax}.coord(1)};%structured coordinates in 3D
2413    eval(['ZMax=max(UvData.Field.' ZName ');'])
2414    eval(['ZMin=min(UvData.Field.' ZName ');'])
2415    test_z=1;
2416    if isequal(ZMin,ZMax)%no z dependency
2417        NbDim=2;
2418        test_z=0;
2419    end
2420end
2421eval(['XMax=max(UvData.Field.' XName ');'])
2422eval(['XMin=min(UvData.Field.' XName ');'])
2423eval(['YMax=max(UvData.Field.' YName ');'])
2424eval(['YMin=min(UvData.Field.' YName ');'])
2425eval(['nbvec=length(UvData.Field.' XName ');'])
2426if test_x %unstructured coordinates
2427    if test_z
2428        UvData.Field.Mesh=((XMax-XMin)*(YMax-YMin)*(ZMax-ZMin))/nbvec;% volume per vector
2429        UvData.Field.Mesh=(UvData.Field.Mesh)^(1/3);
2430    else
2431        UvData.Field.Mesh=sqrt((XMax-XMin)*(YMax-YMin)/nbvec);%2D
2432    end
2433else
2434    VarIndex=CellVarIndex{imax}; % list of variable indices
2435    DimIndex=UvData.Field.VarDimIndex{VarIndex(1)}; %list of dim indices for the variable
2436    nbpoints_y=UvData.Field.DimValue(DimIndex(NbDim-1));
2437    nbpoints_x=UvData.Field.DimValue(DimIndex(NbDim));
2438    DX=(XMax-XMin)/(nbpoints_x-1);
2439    DY=(YMax-YMin)/(nbpoints_y-1);
2440    if NbDim==3
2441        nbpoints_z=UvData.Field.DimValue(DimIndex(1));
2442        DZ=(ZMax-ZMin)/(nbpoints_z-1);
2443        UvData.Field.Mesh=sqrt(DX*DY*DZ);
2444        UvData.Field.ZMax=ZMax;
2445        UvData.Field.ZMin=ZMin;
2446    else
2447        UvData.Field.Mesh=sqrt(DX*DY);
2448    end
2449end
2450UvData.Field.NbDim=NbDim;
2451UvData.Field.XMax=XMax;
2452UvData.Field.XMin=XMin;
2453UvData.Field.YMax=YMax;
2454UvData.Field.YMin=YMin;
2455
2456
2457%% 3D case (menuvolume)
2458if NbDim==3% && UvData.NewSeries
2459    test_set_object=1;
2460    hset_object=findobj(allchild(0),'tag','set_object');% look for the set_object GUI
2461    ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
2462    ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
2463    if ~isempty(hset_object) %if set_object is detected
2464%         hhset_object=guidata(hset_object);
2465% %         ZBounds_old(1)=get(hhset_object.z_slider,'Min');
2466% %         ZBounds_old(2)=get(hhset_object.z_slider,'Max');
2467% %         if isequal(ZBounds_old,ZBounds)
2468%             test_set_object=0;% do not refresh the GUI set_object
2469%         else
2470            delete(hset_object);% delete the GUI set_object if it does not fit
2471%         end
2472    end
2473    if test_set_object% reinitiate the GUI set_object
2474        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
2475        UvData.Object{1}.Style='plane';%main plotting plane
2476        UvData.Object{1}.ProjMode='projection';%main plotting plane
2477        UvData.Object{1}.DisplayHandle_uvmat=[]; %plane not visible in uvmat
2478        UvData.Object{1}.NbDim=NbDim;%test for 3D objects
2479        UvData.Object{1}.RangeZ=UvData.Field.Mesh;%main plotting plane
2480        UvData.Object{1}.Coord(1,3)=(UvData.Field.ZMin+UvData.Field.ZMax)/2;%section at a middle plane chosen
2481        UvData.Object{1}.Phi=0;
2482        UvData.Object{1}.Theta=0;
2483        UvData.Object{1}.Psi=0;
2484        UvData.Object{1}.HandlesDisplay=plot(0,0,'Tag','proj_object');% A REVOIR
2485        PlotHandles=get_plot_handles(handles);
2486        UvData.Object{1}.Name='1-PLANE';
2487        UvData.Object{1}.enable_plot=1;
2488        set_object(UvData.Object{1},PlotHandles,ZBounds);
2489        set(handles.list_object_1,'Value',1);
2490        set(handles.list_object_1,'String',{'1-PLANE'});
2491        set(handles.edit_object,'Value',1)% put the plane in edit mode to enable the z cursor
2492        edit_object_Callback([],[], handles)
2493    end
2494    %multilevel case (single menuplane in a 3D space)
2495elseif isfield(UvData,'Z')
2496    if isfield(UvData,'CoordType')&& isequal(UvData.CoordType,'phys') && isfield(UvData,'XmlData')
2497        XmlData=UvData.XmlData;
2498        if isfield(XmlData,'PlanePos')
2499            UvData.Object{1}.Coord=XmlData.PlanePos(UvData.ZIndex,:);
2500        end
2501        if isfield(XmlData,'PlaneAngle')
2502            siz=size(XmlData.PlaneAngle);
2503            indangle=min(siz(1),UvData.ZIndex);%take first angle if a single angle is defined (translating scanning)
2504            UvData.Object{1}.Phi=XmlData.PlaneAngle(indangle,1);
2505            UvData.Object{1}.Theta=XmlData.PlaneAngle(indangle,2);
2506            UvData.Object{1}.Psi=XmlData.PlaneAngle(indangle,3);
2507        end
2508    elseif isfield(UvData,'ZIndex')
2509        UvData.Object{1}.ZObject=UvData.ZIndex;
2510    end
2511else
2512    % create a default projection menuplane
2513    UvData.Object{1}.Style='plane';%main plotting plane
2514    UvData.Object{1}.ProjMode='projection';%main plotting plane
2515    UvData.Object{1}.DisplayHandle_uvmat=[]; %plane not visible in uvmat
2516    set(handles.list_object_1,'Value',1);
2517    set(handles.list_object_1,'String',{'1-PLANE'});
2518end
2519if ~isfield(UvData.Object{1},'plotaxes')
2520    UvData.Object{1}.plotaxes=handles.axes3;%default plotting axis
2521end
2522
2523%% reset the min and max of scalar if only the mask is displayed(TODO: check the need)
2524if isfield(UvData,'Mask')&& ~isfield(UvData,'A')
2525    set(handles.MinA,'String','0')
2526    set(handles.MaxA,'String','255')
2527end
2528
2529%% Plot the projections on the selected  projection objects
2530
2531% main projection object (uvmat display)
2532IndexObj(1)=get(handles.list_object_1,'Value');%selected projection object for main view
2533if IndexObj(1)> numel(UvData.Object)
2534    IndexObj(1)=1;%select the first object if the selected one does not exist
2535    set(handles.list_object_1,'Value',1)
2536end
2537plot_handles{1}=handles;
2538haxes(1)=handles.axes3;
2539PlotParam{1}=read_plot_param(handles);%read plotting parameters on the uvmat interfac
2540keeplim(1)=get(handles.FixedLimits,'Value');% test for fixed graph limits
2541PosColorbar{1}=UvData.OpenParam.PosColorbar;%prescribe the colorbar position on the uvmat interface
2542
2543% second projection object (view_field display)
2544IndexObj_2=get(handles.list_object_2,'Value');%selected projection object for the second view
2545if isequal(get(handles.list_object_2,'Visible'),'on') && IndexObj_2 <= numel(UvData.Object)&& ~isempty(UvData.Object{IndexObj_2})
2546    IndexObj(2)=IndexObj_2;
2547    view_field_handle=findobj(allchild(0),'tag','view_field');%handles of the view_field GUI
2548    if ~isempty(view_field_handle)
2549        plot_handles{2}=guidata(view_field_handle);
2550        haxes(2)=plot_handles{2}.axes3;
2551        PlotParam{2}=read_plot_param(plot_handles{2});%read plotting parameters on the uvmat interface
2552        keeplim(2)=get(plot_handles{2}.FixedLimits,'Value');
2553        PosColorbar{2}='*'; %TODO: deal with colorbar position on view_field
2554    end
2555end
2556
2557%loop on the projection objects: one or two
2558for imap=1:numel(IndexObj)
2559    iobj=IndexObj(imap);   
2560    [ObjectData,errormsg]=proj_field(UvData.Field,UvData.Object{iobj});% project field on the object
2561    if ~isempty(errormsg)
2562        return
2563    end
2564    %use of mask (TODO: check)
2565    if isfield(ObjectData,'NbDim') && isequal(ObjectData.NbDim,2) && isfield(ObjectData,'Mask') && isfield(ObjectData,'A')
2566        flag_mask=double(ObjectData.Mask>200);%=0 for masked regions
2567        AX=ObjectData.AX;%x coordiantes for the scalar field
2568        AY=ObjectData.AY;%y coordinates for the scalar field
2569        MaskX=ObjectData.MaskX;%x coordiantes for the mask
2570        MaskY=ObjectData.MaskY;%y coordiantes for the mask
2571        if ~isequal(MaskX,AX)||~isequal(MaskY,AY)
2572            nxy=size(flag_mask);
2573            sizpx=(ObjectData.MaskX(end)-ObjectData.MaskX(1))/(nxy(2)-1);%size of a mask pixel
2574            sizpy=(ObjectData.MaskY(1)-ObjectData.MaskY(end))/(nxy(1)-1);
2575            x_mask=ObjectData.MaskX(1):sizpx:ObjectData.MaskX(end); % pixel x coordinates for image display
2576            y_mask=ObjectData.MaskY(1):-sizpy:ObjectData.MaskY(end);% pixel x coordinates for image display
2577            %project on the positions of the scalar
2578            npxy=size(ObjectData.A);
2579            dxy(1)=(ObjectData.AY(end)-ObjectData.AY(1))/(npxy(1)-1);%grid mesh in y
2580            dxy(2)=(ObjectData.AX(end)-ObjectData.AX(1))/(npxy(2)-1);%grid mesh in x
2581            xi=ObjectData.AX(1):dxy(2):ObjectData.AX(end);
2582            yi=ObjectData.AY(1):dxy(1):ObjectData.AY(end);
2583            [XI,YI]=meshgrid(xi,yi);% creates the matrix of regular coordinates
2584            flag_mask = interp2(x_mask,y_mask,flag_mask,XI,YI);
2585        end
2586        AClass=class(ObjectData.A);
2587        ObjectData.A=flag_mask.*double(ObjectData.A);
2588        ObjectData.A=feval(AClass,ObjectData.A);
2589        ind_off=[];
2590        if isfield(ObjectData,'ListVarName')
2591            for ilist=1:length(ObjectData.ListVarName)
2592                if isequal(ObjectData.ListVarName{ilist},'Mask')||isequal(ObjectData.ListVarName{ilist},'MaskX')||isequal(ObjectData.ListVarName{ilist},'MaskY')
2593                    ind_off=[ind_off ilist];
2594                end
2595            end
2596            ObjectData.ListVarName(ind_off)=[];
2597            ObjectData.VarDimIndex(ind_off)=[];
2598            ind_off=[];
2599            for ilist=1:length(ObjectData.ListDimName)
2600                if isequal(ObjectData.ListDimName{ilist},'MaskX') || isequal(ObjectData.ListDimName{ilist},'MaskY')
2601                    ind_off=[ind_off ilist];
2602                end
2603            end
2604            ObjectData.ListDimName(ind_off)=[];
2605            ObjectData.DimValue(ind_off)=[];
2606        end
2607    end
2608   
2609    if ~isempty(ObjectData)
2610        PlotType='none'; %default
2611        if imap==2 && isempty(view_field_handle)
2612            view_field(ObjectData)
2613        else
2614            [PlotType,PlotParamOut]=plot_field(ObjectData,haxes(imap),PlotParam{imap},keeplim(imap),PosColorbar{imap});
2615            write_plot_param(plot_handles{imap},PlotParamOut) %update the auto plot parameters
2616            if isfield(Field,'Mesh')&&~isempty(Field.Mesh)
2617                ObjectData.Mesh=Field.Mesh; % gives an estimated mesh size (useful for mouse action on the plot)
2618            end
2619            if imap==1           
2620                UvData.axes3=ObjectData;
2621            else
2622                ViewFieldData=get(view_field_handle,'UserData');
2623                ViewFieldData.axes3=ObjectData;
2624                set(view_field_handle,'UserData',ViewFieldData)
2625            end
2626        end
2627        if isequal(PlotType,'none')
2628            hget_field=findobj(allchild(0),'name','get_field');
2629            if isempty(hget_field)
2630                get_field(filename)% the projected field cannot be automatically plotted: use get_field to specify the variablesdelete(hget_field)
2631            end
2632            errormsg='The field defined by get_field cannot be plotted';
2633            return
2634        end
2635    end
2636end
2637
2638%write_plot_param(handles,UvData.Object{1}.PlotParam);% update the display of the plotting parameters
2639UvData.NewSeries=0;% put to 0 the test for a new field series (set by RootPath_callback)
2640set(handles.uvmat,'UserData',UvData)
2641
2642%% update the mask
2643if isequal(get(handles.mask_test,'Value'),1)%if the mask option is on
2644   update_mask(handles,num_i1,num_i2);
2645end
2646
2647%% prepare the menus of histograms and plot them (histogram of the whole volume in 3D case)
2648menu_histo=(UvData.Field.ListVarName)';%list of field variables to be displayed for the menu of histogram display
2649ind_bad=[];
2650nb_histo=1;
2651
2652% suppress coordinates from the histogram menu
2653for ivar=1:numel(menu_histo)%l loop on field variables:
2654    if isfield(UvData.Field,'VarAttribute') && numel(UvData.Field.VarAttribute)>=ivar && isfield(UvData.Field.VarAttribute{ivar},'Role')
2655        Role=UvData.Field.VarAttribute{ivar}.Role;
2656        switch Role
2657            case {'coord_x','coord_y','coord_z','dimvar'}
2658                ind_bad=[ind_bad ivar];
2659            case {'vector_y'}
2660                nb_histo=nb_histo+1;
2661        end
2662    end
2663    DimCell=UvData.Field.VarDimName{ivar};
2664    DimName='';
2665    if ischar(DimCell)
2666        DimName=DimCell;
2667    elseif iscell(DimCell)&& numel(DimCell)==1
2668        DimName=DimCell{1};
2669    end
2670    if strcmp(DimName,menu_histo{ivar})
2671        ind_bad=[ind_bad ivar];
2672    end
2673end
2674menu_histo(ind_bad)=[];
2675
2676%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2677% display menus and plot histograms
2678test_v=0;
2679if ~isempty(menu_histo)
2680    set(handles.histo1_menu,'Value',1)
2681    set(handles.histo1_menu,'String',menu_histo)
2682    histo1_menu_Callback(handles.histo1_menu, [], handles)% plot first histogram
2683    % case of more than one variables (eg vector components)
2684    if nb_histo > 1
2685        test_v=1;
2686        set(handles.histo2_menu,'Visible','on')
2687        set(handles.histo_v,'Visible','on')
2688        set(handles.histo2_menu,'String',menu_histo)
2689        set(handles.histo2_menu,'Value',2)
2690        histo2_menu_Callback(handles.histo2_menu,[], handles)% plot second histogram
2691    end
2692end
2693if ~test_v
2694    set(handles.histo2_menu,'Visible','off')
2695    set(handles.histo_v,'Visible','off')
2696    cla(handles.histo_v)
2697    set(handles.histo2_menu,'Value',1)
2698end
2699%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2700
2701%% display time
2702testimedoc=0;
2703if isfield(UvData,'XmlData') && isfield(UvData.XmlData,'Time')
2704    if isempty(num_i2)||isnan(num_i2)
2705        num_i2=num_i1;
2706    end
2707    if isempty(num_j1)||isnan(num_j1)
2708        num_j1=1;
2709    end
2710    if isempty(num_j2)||isnan(num_j2)
2711        num_j2=num_j1;
2712    end
2713    siz=size(UvData.XmlData.Time);
2714    if siz(1)>=max(num_i1,num_i2) && siz(2)>=max(num_j1,num_j2)
2715        abstime=(UvData.XmlData.Time(num_i1,num_j1)+UvData.XmlData.Time(num_i2,num_j2))/2;%overset the time read from files
2716        dt=(UvData.XmlData.Time(num_i2,num_j2)-UvData.XmlData.Time(num_i1,num_j1));
2717        testimedoc=1;
2718    end
2719end
2720if isfield(UvData,'XmlData_1') && isfield(UvData.XmlData_1,'Time')
2721    [P,F,str1,str2,str_a,str_b,E]=name2display(['xx' get(handles.FileIndex_1,'String') get(handles.FileExt_1,'String')]);
2722    num_i2=str2double(str2);
2723    if isnan(num_i2)
2724        num_i2=num_i1;
2725    end
2726    num_j1=str2double(str_a);
2727    if isnan(num_j1)
2728        num_j1=1;
2729    end
2730    num_j2=str2double(str_b);
2731    if isnan(num_j2)
2732        num_j2=num_j1;
2733    end
2734    num_i1=str2double(str1);
2735    siz=size(UvData.XmlData_1.Time);
2736    if siz(1)>=max(num_i1,num_i2) && siz(2)>=max(num_j1,num_j2)
2737        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
2738    end
2739end
2740set(handles.abs_time,'String',num2str(abstime,4))
2741set(handles.abs_time_1,'String',num2str(abstime_1,4))
2742if testimedoc && isfield(UvData,'dt')
2743    dt=UvData.dt;
2744end
2745if isempty(dt)||isequal(dt,0)
2746    set(handles.Dt_txt,'String','')
2747else
2748    if ~(isfield(UvData,'TimeUnit') && ~isempty(UvData.TimeUnit))
2749        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  10^(-3)'] )
2750    else
2751        set(handles.Dt_txt,'String',['Dt=' num2str(1000*dt,3) '  m' UvData.TimeUnit] )
2752    end
2753end
2754
2755
2756%-------------------------------------------------------------------
2757% --- translate coordinate to matrix index
2758%-------------------------------------------------------------------
2759function [indx,indy]=pos2ind(x0,rangx0,nxy)
2760indx=1+round((nxy(2)-1)*(x0-rangx0(1))/(rangx0(2)-rangx0(1)));% index x of pixel 
2761indy=1+round((nxy(1)-1)*(y12-rangy0(1))/(rangy0(2)-rangy0(1)));% index y of pixel
2762
2763%-------------------------------------------------------------------
2764% --- Executes on button press in 'FixedLimits'.
2765%-------------------------------------------------------------------
2766function FixedLimits_Callback(hObject, eventdata, handles)
2767test=get(handles.FixedLimits,'Value');
2768if test
2769    set(handles.FixedLimits,'BackgroundColor',[1 1 0])
2770else
2771    set(handles.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
2772    update_plot(handles);
2773end
2774
2775%-------------------------------------------------------------------
2776% --- Executes on button press in auto_xy.
2777function auto_xy_Callback(hObject, eventdata, handles)
2778test=get(handles.auto_xy,'Value');
2779if test
2780    set(handles.auto_xy,'BackgroundColor',[1 1 0])
2781    cla(handles.axes3)
2782    update_plot(handles);
2783else
2784    set(handles.auto_xy,'BackgroundColor',[0.7 0.7 0.7])
2785    update_plot(handles);
2786%     axis(handles.axes3,'image')
2787end
2788
2789
2790%-------------------------------------------------------------------
2791
2792%-------------------------------------------------------------------
2793% --- Executes on button press in 'zoom'.
2794%-------------------------------------------------------------------
2795function zoom_Callback(hObject, eventdata, handles)
2796
2797if (get(handles.zoom,'Value') == 1);
2798    set(handles.zoom,'BackgroundColor',[1 1 0])
2799    set(handles.FixedLimits,'Value',1)% propose by default fixed limits for the plotting axes
2800    set(handles.FixedLimits,'BackgroundColor',[1 1 0])
2801else
2802    set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
2803end
2804
2805
2806%-------------------------------------------------------------------
2807%----Executes on button press in 'record': records the current flags of manual correction.
2808%-------------------------------------------------------------------
2809function record_Callback(hObject, eventdata, handles)
2810% [filebase,num_i1,num_j1,num_i2,num_j2,Ext,NomType,SubDir]=read_input_file(handles);
2811filename=read_file_boxes(handles);
2812[erread,message]=fileattrib(filename);
2813if ~isempty(message) && ~isequal(message.UserWrite,1)
2814     msgbox_uvmat('ERROR',['no writting access to ' filename])
2815     return
2816end
2817test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
2818test_civ1=isequal(get(handles.civ1,'BackgroundColor'),[1 1 0]);
2819if ~test_civ2 && ~test_civ1
2820    msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
2821end
2822if test_civ2
2823    nbname='nb_vectors2';
2824   flagname='vec2_FixFlag';
2825   attrname='fix2';
2826end
2827if test_civ1
2828    nbname='nb_vectors';
2829   flagname='vec_FixFlag';
2830   attrname='fix';
2831end
2832%write fix flags in the netcdf file
2833UvData=get(handles.uvmat,'UserData');
2834hhh=which('netcdf.open');% look for built-in matlab netcdf library
2835if ~isequal(hhh,'')% case of new builtin Matlab netcdf library
2836    nc=netcdf.open(filename,'NC_WRITE');
2837    netcdf.reDef(nc)
2838    netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),attrname,1)
2839    dimid = netcdf.inqDimID(nc,nbname);
2840    try
2841        varid = netcdf.inqVarID(nc,flagname);% look for already existing fixflag variable
2842    catch
2843        varid=netcdf.defVar(nc,flagname,'double',dimid);%create fixflag variable if it does not exist
2844    end
2845    netcdf.endDef(nc)
2846    netcdf.putVar(nc,varid,UvData.axes3.FF);
2847    netcdf.close(nc) 
2848else %old netcdf library
2849    netcdf_toolbox(filename,AxeData,attrname,nbname,flagname)
2850end
2851
2852%-------------------------------------------------------------------
2853%----Correct the netcdf file, using toolbox (old versions of Matlab).
2854%-------------------------------------------------------------------
2855function netcdf_toolbox(filename,AxeData,attrname,nbname,flagname)
2856nc=netcdf(filename,'write'); %open netcdf file
2857result=redef(nc);
2858eval(['nc.' attrname '=1;']);
2859theDim=nc(nbname) ;% get the number of velocity vectors
2860nb_vectors=size(theDim);
2861var_FixFlag=ncvar(flagname,nc);% var_FixFlag will be written as the netcdf variable vec_FixFlag
2862var_FixFlag(1:nb_vectors)=AxeData.FF;%
2863fin=close(nc);
2864
2865
2866%-------------------------------------------------------------------
2867%determines the fields to read from the interface
2868%------------------------------------------------------------------
2869function [VelType,civ]=setfield(handles)
2870
2871VelType=[]; %default
2872if (get(handles.civ1,'Value') == 1);
2873        VelType='civ1';
2874% interp1   
2875elseif (get(handles.interp1,'Value') == 1);
2876    VelType='interp1';
2877% filter1   
2878elseif (get(handles.filter1,'Value') == 1);
2879    VelType='filter1'; 
2880% CIV2
2881elseif (get(handles.civ2,'Value') == 1);
2882    VelType='civ2';
2883% interp2   
2884elseif (get(handles.interp2,'Value') == 1);
2885    VelType='interp2';
2886% filter2   
2887elseif (get(handles.filter2,'Value') == 1); 
2888    VelType='filter2';
2889end
2890
2891if isequal(get(handles.filter2,'Visible'),'on');
2892    civ=6;
2893% interp1   
2894elseif isequal(get(handles.interp2,'Visible'),'on');
2895    civ=5;
2896% filter1   
2897elseif isequal(get(handles.civ2,'Visible'),'on');
2898    civ=4; 
2899% CIV2
2900elseif isequal(get(handles.filter1,'Visible'),'on');
2901   civ=3;
2902% interp2   
2903elseif isequal(get(handles.interp1,'Visible'),'on');
2904    civ=2;
2905% filter2   
2906elseif isequal(get(handles.civ1,'Visible'),'on'); 
2907    civ=1;
2908else
2909    civ=0;
2910end
2911
2912%-------------------------------------------------------------------
2913%determines the veltype of the second field to read from the iinterface
2914%------------------------------------------------------------------
2915function VelType=setfield_1(handles)
2916
2917VelType=[]; %default
2918if (get(handles.civ1_1,'Value') == 1);
2919    VelType='civ1';
2920% interp1   
2921elseif (get(handles.interp1_1,'Value') == 1);
2922    VelType='interp1';
2923% filter1   
2924elseif (get(handles.filter1_1,'Value') == 1);
2925    VelType='filter1'; 
2926% CIV2
2927elseif (get(handles.civ2_1,'Value') == 1);
2928    VelType='civ2';
2929% interp2   
2930elseif (get(handles.interp2_1,'Value') == 1);
2931    VelType='interp2';
2932% filter2   
2933elseif (get(handles.filter2_1,'Value') == 1); 
2934    VelType='filter2';
2935end
2936
2937
2938%---------------------------------------------------
2939% --- Executes on button press in SubField
2940function SubField_Callback(hObject, eventdata, handles)
2941huvmat=get(handles.run0,'parent');
2942UvData=get(huvmat,'UserData');
2943if get(handles.SubField,'Value')==0% if the subfield button is desactivated   
2944    set(handles.RootPath_1,'String','')
2945    set(handles.RootFile_1,'String','')
2946    set(handles.SubDir_1,'String','');
2947    set(handles.FileIndex_1,'String','');
2948    set(handles.FileExt_1,'String','');
2949    set(handles.RootPath_1,'Visible','off')
2950    set(handles.RootFile_1,'Visible','off')
2951    set(handles.SubDir_1,'Visible','off');
2952    set(handles.FileIndex_1,'Visible','off');
2953    set(handles.FileExt_1,'Visible','off');
2954    set(handles.Fields_1,'Value',1);%set to blank state
2955    set_veltype_display([handles.civ1_1 handles.interp1_1 handles.filter1_1 ...
2956            handles.civ2_1 handles.interp2_1 handles.filter2_1],0)
2957    if isfield(UvData,'XmlData_1')
2958        UvData=rmfield(UvData,'XmlData_1');
2959    end
2960    set(huvmat,'UserData',UvData);
2961    run0_Callback(hObject, eventdata, handles); %run
2962else
2963    MenuBrowse_1_Callback(hObject, eventdata, handles)
2964end
2965
2966%------------------------------------------------------------------------
2967% --- read the data displayed for the input rootfile windows (new)
2968function [FileName,RootPath,FileBase,FileIndices,FileExt,SubDir]=read_file_boxes(handles)
2969%------------------------------------------------------------------------
2970RootPath=get(handles.RootPath,'String');
2971FileName=RootPath; %default
2972SubDir=get(handles.SubDir,'String');
2973if ~isempty(SubDir) && ~isequal(SubDir,'')
2974    if (isequal(SubDir(1),'/')|| isequal(SubDir(1),'\'))
2975        SubDir(1)=[]; %suppress possible / or \ separator
2976    end
2977    FileName=fullfile(RootPath,SubDir);
2978end
2979RootFile=get(handles.RootFile,'String');
2980if ~isempty(RootFile) && ~isequal(RootFile,'')
2981    if (isequal(RootFile(1),'/')|| isequal(RootFile(1),'\'))
2982        RootFile(1)=[]; %suppress possible / or \ separator
2983    end
2984    FileName=fullfile(FileName,RootFile);
2985end
2986FileBase=fullfile(RootPath,RootFile);
2987FileIndices=get(handles.FileIndex,'String');
2988FileExt=get(handles.FileExt,'String');
2989FileName=[FileName FileIndices FileExt];
2990
2991%------------------------------------------------------------------------
2992% ---- read the data displayed for the second input rootfile windows
2993function [FileName_1,RootPath_1,FileBase_1,FileIndices_1,FileExt_1,SubDir_1]=read_file_boxes_1(handles)
2994%------------------------------------------------------------------------
2995RootPath_1=get(handles.RootPath_1,'String'); % read the data from the file1_input window
2996if isequal(get(handles.RootPath_1,'Visible'),'off') || isequal(RootPath_1,'"')
2997    RootPath_1=get(handles.RootPath,'String');
2998end;
2999FileName_1=RootPath_1; %default
3000SubDir_1=get(handles.SubDir_1,'String');
3001if isequal(get(handles.SubDir_1,'Visible'),'off')|| isequal(SubDir_1,'"')
3002    SubDir_1=get(handles.SubDir,'String');
3003end
3004if numel(SubDir_1)>=1
3005    if (isequal(SubDir_1(1),'/')|| isequal(SubDir_1(1),'\'))
3006        SubDir_1(1)=[]; %suppress possible / or \ separator
3007    end
3008    FileName_1=fullfile(RootPath_1,SubDir_1);
3009end
3010RootFile_1=get(handles.RootFile_1,'String');
3011if isequal(get(handles.RootFile_1,'Visible'),'off') || isequal(RootFile_1,'"')
3012    RootFile_1=get(handles.RootFile,'String');
3013end
3014if numel(RootFile_1)>=1
3015    if ~(isequal(RootFile_1(1),'/')|isequal(RootFile_1(1),'\'))
3016        RootFile_1(1)=[];%suppress possible / or \ separator
3017    end
3018    FileName_1=fullfile(FileName_1,RootFile_1);
3019end
3020FileBase_1=fullfile(RootPath_1,RootFile_1);
3021if isequal(get(handles.FileIndex_1,'Visible'),'off')
3022    FileIndices_1=get(handles.FileIndex,'String');
3023else
3024    FileIndices_1=get(handles.FileIndex_1,'String');
3025end
3026FileExt_1=get(handles.FileExt_1,'String');
3027if isequal(FileExt_1,'"'),FileExt_1=get(handles.FileExt,'String'); end;
3028FileName_1=[FileName_1 FileIndices_1 FileExt_1];
3029
3030%------------------------------------------------------------------------
3031% --- Executes on menu selection Fields
3032function Fields_Callback(hObject, eventdata, handles)
3033%------------------------------------------------------------------------
3034list_fields=get(handles.Fields,'String');% list menu fields
3035index_fields=get(handles.Fields,'Value');% selected string index
3036field= list_fields{index_fields(1)}; % selected string
3037if isequal(field,'get_field...')
3038     veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
3039     set_veltype_display(veltype_handles,0) % unvisible civ buttons
3040     filename=read_file_boxes(handles);
3041     hget_field=findobj(allchild(0),'name','get_field');
3042     if ~isempty(hget_field)
3043         delete(hget_field)
3044     end
3045     hget_field=get_field(filename);
3046     set(hget_field,'Name','get_field')       
3047     hhget_field=guidata(hget_field);
3048     set(hhget_field.list_fig,'Value',1)
3049     set(hhget_field.list_fig,'String',{'uvmat'})
3050     set(handles.transform_fct,'Value',1)% no transform by default
3051     set(handles.path_transform,'String','')
3052    return %no action
3053end
3054list_fields=get(handles.Fields_1,'String');% list menu fields
3055index_fields=get(handles.Fields_1,'Value');% selected string index
3056field_1= list_fields{index_fields(1)}; % selected string
3057UvData=get(handles.uvmat,'UserData');
3058
3059%read the rootfile input display
3060[FileName,RootPath,FileBase,FileIndices,FileExt]=read_file_boxes(handles);
3061[P,F,str1,str2,str_a,str_b,E,NomType]=name2display(['xxx' get(handles.FileIndex,'String') FileExt]);
3062NomTypeNew=NomType;%default
3063if isequal(field,'image')
3064    % transform netc type to the corresponding image type
3065%     if isequal(NomType,'_i1-i2_j')||isequal(NomType,'_i_j1-j2')|| isequal(NomType,'#_ab')|| isequal(NomType,'_i1-i2')
3066%         UvData.SubDir=get(handles.SubDir,'String'); %preserve the subdir in memory
3067%         if ~isempty(UvData.SubDir) && (isequal(UvData.SubDir(1),'/')||isequal(UvData.SubDir(1),'/'))
3068%             UvData.SubDir(1)=[];
3069%         end
3070%         set(handles.SubDir,'String','')
3071%         set(handles.FileExt,'String','.png');
3072        if isequal(NomType,'_i1-i2_j')||isequal(NomType,'_i_j1-j2')
3073            NomTypeNew='_i_j';
3074        elseif isequal(NomType,'#_ab')
3075            NomTypeNew='#a';
3076        elseif isequal(NomType,'_i1-i2')
3077            NomTypeNew='_i';
3078        end 
3079        imagename=name_generator(FileBase,str2double(str1),str2double(str_a),'.png',NomTypeNew,1,str2double(str2),str2double(str_b),'');
3080        if ~exist(imagename,'file')
3081                [FileName,PathName] = uigetfile( ...
3082           {'*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol', ' (*.png, .tif, *.avi,*.vol)';
3083            '*.jpg',' jpeg image files'; ...
3084            '*.png','.png image files'; ...
3085            '*.tif','.tif image files'; ...
3086            '*.avi;*.AVI','.avi movie files'; ...
3087            '*.vol','.volume images (png)'; ...
3088            '*.*',  'All Files (*.*)'}, ...
3089            'Pick an image',imagename);           
3090            % display the selected field and related information
3091           imagename=[PathName FileName];
3092        end
3093        display_file_name(hObject, eventdata, handles,imagename)%display the image
3094        return
3095%     end
3096%     veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
3097%     set_veltype_display(veltype_handles,0) % unvisible civ buttons
3098else
3099    ext=get(handles.FileExt,'String');
3100    if ~isequal(ext,'.nc') %find the new NomType if the previous display was not already a netcdf file
3101                [FileName,PathName] = uigetfile( ...
3102           {'*.nc', ' (*.nc)';
3103            '*.nc',' netcdf files'; ...
3104            '*.*',  'All Files (*.*)'}, ...
3105            'Pick a netcdf file',FileBase);           
3106            % display the selected field and related information
3107           filename=[PathName FileName];
3108        display_file_name(hObject, eventdata, handles,filename)
3109        return
3110       %  MenuBrowse_Callback(hObject, eventdata, handles)
3111    end
3112    if isequal(field,'vort') || isequal(field,'div') || isequal(field,'strain')
3113        set(handles.civ1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3114        set(handles.civ2,'BackgroundColor',[0.702 0.702 0.702])
3115        set(handles.interp1,'BackgroundColor',[0.702 0.702 0.702])
3116        set(handles.interp2,'BackgroundColor',[0.702 0.702 0.702])
3117    elseif isequal(field,'more...');
3118        set(handles.civ1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3119        set(handles.civ2,'BackgroundColor',[0.702 0.702 0.702])
3120        str=calc_field;%get the list of available scalars by the function calc_scal
3121        [ind_answer] = listdlg('PromptString','Select a file:',...
3122                'SelectionMode','single',...
3123                'ListString',str);
3124       % edit the choice in the field and action menu
3125        scalar=cell2mat(str(ind_answer));
3126        menu=update_menu(handles.Fields,scalar);
3127        menu=[{''};menu];
3128        set(handles.Fields_1,'String',menu);% store the selected scalar type
3129    end
3130end
3131indices=name_generator('',str2double(str1),str2double(str_a),'',NomTypeNew,1,str2double(str2),str2double(str_b),'');
3132set(handles.FileIndex,'String',indices)
3133set(handles.FileIndex,'UserData',NomTypeNew)
3134%common to Fields_1_Callback
3135if isequal(field,'image')||isequal(field_1,'image')
3136    set(handles.npx_title,'Visible','on')% visible npx,pxcm... buttons
3137    set(handles.npy_title,'Visible','on')
3138    set(handles.npx,'Visible','on')
3139    set(handles.npy,'Visible','on')
3140else
3141    set(handles.npx_title,'Visible','off')% visible npx,pxcm... buttons
3142    set(handles.npy_title,'Visible','off')
3143    set(handles.npx,'Visible','off')
3144    set(handles.npy,'Visible','off')
3145end
3146setfield(handles);% update the field structure ('civ1'....)
3147
3148if ~isfield(UvData,'NewSeries')||isequal(UvData.NewSeries,0)
3149    run0_Callback(hObject, eventdata, handles)
3150end
3151
3152%---------------------------------------------------
3153% --- Executes on menu selection Fields
3154function Fields_1_Callback(hObject, eventdata, handles)
3155%-------------------------------------------------
3156list_fields=get(handles.Fields,'String');% list menu fields
3157index_fields=get(handles.Fields,'Value');% selected string index
3158field= list_fields{index_fields(1)}; % selected string
3159list_fields=get(handles.Fields_1,'String');% list menu fields
3160index_fields=get(handles.Fields_1,'Value');% selected string index
3161field_1= list_fields{index_fields(1)}; % selected string for the second field
3162if isequal(field_1,'') %remove second field if 'blank' field is selected
3163    set(handles.SubField,'Value',0)
3164    SubField_Callback(hObject, eventdata, handles)
3165    return
3166end
3167UvData=get(handles.uvmat,'UserData');
3168
3169%read the rootfile input display
3170[FileName,RootPath,FileBase,FileIndices,FileExt_prev]=read_file_boxes_1(handles);
3171[P,F,str1,str2,str_a,str_b,E,NomType]=name2display(['xxx' get(handles.FileIndex,'String') FileExt_prev]);
3172if isempty(FileExt_prev)|isequal(FileExt_prev,'')
3173    FileExt_1=get(handles.FileExt,'String');
3174else
3175    FileExt_1=FileExt_prev;
3176end
3177NomType_1=get(handles.FileIndex_1,'UserData');
3178if isempty(NomType_1)|isequal(NomType_1,'')
3179    NomType_1=get(handles.FileIndex,'UserData');
3180end
3181NomTypeNew=NomType_1;%default
3182
3183set(handles.SubField,'Value',1)%introduce second field
3184if isfield(UvData,'XmlData')
3185    UvData.XmlData_1=UvData.XmlData;
3186end
3187set(handles.FileIndex_1,'Visible','on')
3188set(handles.FileExt_1,'Visible','on')
3189RootPath_1=get(handles.RootPath_1,'String');
3190RootFile_1=get(handles.RootFile_1,'String');
3191if isempty(RootPath_1)||isequal(RootPath_1,'')
3192    set(handles.RootPath_1,'String','"')
3193end
3194if isempty(RootFile_1) || isequal(RootFile_1,'')
3195    set(handles.RootFile_1,'String','"')
3196end
3197if ~isempty(RootFile_1)&&(isequal(RootFile_1(1),'/')||isequal(RootFile_1(1),'\'))
3198    RootFile_1(1)=[];
3199end
3200
3201if isequal(field_1,'get_field...')
3202     veltype_handles=[handles.civ1 handles.interp1 handles.filter1 handles.civ2 handles.interp2 handles.filter2];
3203     set_veltype_display(veltype_handles,0) % unvisible civ buttons
3204     filename=read_file_boxes_1(handles);
3205     hget_field=findobj(allchild(0),'name','get_field_1');
3206     if ~isempty(hget_field)
3207         delete(hget_field)
3208     end
3209     hget_field=get_field(filename);
3210     set(hget_field,'name','get_field_1')
3211     hhget_field=guidata(hget_field);
3212     set(hhget_field.list_fig,'Value',1)
3213     set(hhget_field.list_fig,'String',{'uvmat'})
3214     set(handles.transform_fct,'Value',1)% no transform by default
3215     set(handles.path_transform,'String','')
3216    return %no action
3217end
3218if isequal(field_1,'image')
3219    % transform netc type to the corresponding image type
3220%     set(handles.FileExt_1,'String','.png');
3221    if isequal(NomType_1,'_i1-i2_j')|isequal(NomType_1,'_i_j1-j2')| isequal(NomType_1,'#_ab')| isequal(NomType_1,'_i1-i2')
3222        UvData.SubDir_1=get(handles.SubDir_1,'String'); %preserve the subdir in memory
3223%         set(handles.SubDir_1,'String','')
3224%         set(handles.FileExt_1,'String','.png');       
3225        if isequal(NomType_1,'_i1-i2_j')|isequal(NomType_1,'_i_j1-j2')
3226            NomTypeNew='_i_j';
3227        elseif isequal(NomType_1,'#_ab')
3228            NomTypeNew='#a';
3229        elseif isequal(NomType_1,'_i1-i2')
3230            NomTypeNew='_i';
3231        end 
3232    end
3233    imagename=name_generator(FileBase,str2double(str1),str2double(str_a),'.png',NomTypeNew,1,str2double(str2),str2double(str_b),'');
3234    if ~exist(imagename,'file')
3235        [FileName,PathName] = uigetfile( ...
3236            {'*.png;*.jpg;*.tif;*.avi;*.AVI;*.vol', ' (*.png, .tif, *.avi,*.vol)';
3237            '*.jpg',' jpeg image files'; ...
3238            '*.png','.png image files'; ...
3239            '*.tif','.tif image files'; ...
3240            '*.avi;*.AVI','.avi movie files'; ...
3241            '*.vol','.volume images (png)'; ...
3242            '*.*',  'All Files (*.*)'}, ...
3243            'Pick an image',imagename);
3244        % display the selected field and related information
3245        imagename=[PathName FileName];
3246    end
3247    display_file_name_1(hObject, eventdata, handles,imagename)%display the image
3248    return
3249%     veltype_handles=[handles.civ1_1 handles.interp1_1 handles.filter1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1];
3250%     set_veltype_display(veltype_handles,0) % unvisible civ buttons
3251else
3252    set(handles.SubDir_1,'Visible','on')
3253    if ~isequal(FileExt_prev,'.nc') %find the new NomType if the previous display was not already a netcdf file
3254        veltype_handles=[handles.civ1_1 handles.interp1_1 handles.filter1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1];
3255        set_veltype_display(veltype_handles,6); % make all civ buttons visible
3256        RootPath_1=get(handles.RootPath_1,'String');
3257        RootFile_1=get(handles.RootFile_1,'String');
3258        if isempty(RootPath_1)|isequal(RootPath_1,'')
3259            set(handles.RootPath_1,'String','"')
3260        end
3261        if isempty(RootFile_1) | isequal(RootFile_1,'')
3262            set(handles.RootFile_1,'String','"')
3263        end
3264        if ~isempty(RootFile_1)&(isequal(RootFile_1(1),'/')|isequal(RootFile_1(1),'\'))
3265            RootFile_1(1)=[];
3266        end
3267        filebase_1=fullfile(RootPath_1,RootFile_1);
3268        SubDir_1=get(handles.SubDir,'String');
3269        if isempty(SubDir_1)||isequal(SubDir_1,'')
3270            if isfield(UvData,'SubDir_1')
3271                SubDir_1=UvData.SubDir_1;%retrieve previous subdir
3272            else
3273                SubDir_1='?';
3274            end
3275        end
3276        str1=get(handles.i1,'String');
3277        str_a=get(handles.j1,'String');
3278        if isequal(NomType_1,'#_ab')||isequal(NomType_1,'_i1-i2_j')||isequal(NomType_1,'_i_j1-j2')||isequal(NomType_1,'_i1-i2')
3279            NomTypeNew=NomType_1;
3280        elseif isequal(NomType_1,'#a')
3281             [filename, n1,na,n2,nb,SubDir_1]=name_generator(filebase_1, str2num(str1),stra2num(str_a),'.nc','#_ab',0,[],[],SubDir_1);
3282             NomTypeNew='#_ab';
3283        elseif isequal(NomType_1,'_i_j')
3284             [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);
3285            if idetect==1
3286                NomTypeNew='_i1-i2_j';
3287            else
3288                NomTypeNew='_i_j1-j2';
3289            end
3290        else %for instance avi files or any ima_num series
3291            [filename,n1,na,n2,nb,SubDir_1]=name_generator(filebase_1,str2num(str1),stra2num(str_a),'.nc','_i1-i2',0,str2num(str1),[],SubDir_1);
3292            NomTypeNew='_i1-i2';
3293        end           
3294        [Path,Name]=fileparts(filebase_1);
3295        set(handles.FileExt_1,'String','.nc');
3296        if ~isempty(SubDir_1) && ~strcmp(SubDir_1,'''')&& ~strcmp(SubDir_1,'"')&& ~strcmp(SubDir_1(1),'/')
3297            SubDir_1=['/' SubDir_1];
3298        end
3299        set(handles.SubDir_1,'String',SubDir_1);
3300    end
3301    if isequal(field,'vort') | isequal(field,'div') | isequal(field,'strain')
3302        set(handles.civ1_1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3303        set(handles.civ2_1,'BackgroundColor',[0.702 0.702 0.702])
3304        set(handles.interp1_1,'BackgroundColor',[0.702 0.702 0.702])
3305        set(handles.interp2_1,'BackgroundColor',[0.702 0.702 0.702])
3306    elseif isequal(field_1,'more...'); %add new item to the menu
3307        set(handles.civ1_1,'BackgroundColor',[0.702 0.702 0.702]) % put their color to grey
3308        set(handles.civ2_1,'BackgroundColor',[0.702 0.702 0.702])
3309        str=calc_field;%get the list of available scalars by the function calc_scal
3310        [ind_answer,v] = listdlg('PromptString','Select a file:',...
3311                'SelectionMode','single',...
3312                'ListString',str);
3313       % edit the choice in the field and action menu
3314        scalar=cell2mat(str(ind_answer));
3315        menu=update_menu(handles.Fields_1,scalar);
3316        set(handles.Fields_1,'String',menu);% store the selected scalar type
3317    end
3318end
3319str1=get(handles.i1,'String');
3320str2=get(handles.i2,'String');
3321str_a=get(handles.j1,'String');
3322str_b=get(handles.j2,'String');
3323indices=name_generator('',str2num(str1),stra2num(str_a),'',NomTypeNew,1,str2num(str2),stra2num(str_b),'');
3324set(handles.FileIndex_1,'String',indices)
3325set(handles.FileIndex_1,'UserData',NomTypeNew)
3326
3327%common to Fields_Callback
3328if isequal(field,'image')|isequal(field_1,'image')
3329    set(handles.npx_title,'Visible','on')% visible npx,pxcm... buttons
3330    set(handles.npy_title,'Visible','on')
3331    set(handles.npx,'Visible','on')
3332    set(handles.npy,'Visible','on')
3333%     set(handles.fix_pair,'Value',0)
3334else
3335    set(handles.npx_title,'Visible','off')% visible npx,pxcm... buttons
3336    set(handles.npy_title,'Visible','off')
3337    set(handles.npx,'Visible','off')
3338    set(handles.npy,'Visible','off')
3339%     set(handles.fix_pair,'Value',1)
3340end
3341if isequal(field,'velocity')|isequal(field_1,'velocity');
3342    state_vect='on';
3343else
3344    state_vect='off';
3345end
3346if ~isequal(field,'velocity')|(~isequal(field_1,'velocity')&~isequal(field_1,''));
3347    state_scal='on';
3348else
3349    state_scal='off';
3350end
3351set(handles.uvmat,'UserData',UvData)
3352setfield(handles);% update the field structure ('civ1'....)
3353if ~isfield(UvData,'NewSeries')||isequal(UvData.NewSeries,0)
3354    run0_Callback(hObject, eventdata, handles)
3355end
3356
3357%------------------------------------------------------------------------
3358% --- set the visibility of relevant velocity type menus:
3359function set_veltype_display(handles,Civ)
3360%------------------------------------------------------------------------
3361%Civ=0; all states 'off'
3362%Civ=6; all states 'on'
3363if isequal(Civ,0)
3364    imax=0;
3365%    set(handles(1),'Visible','on')  % unvisible civ buttons
3366% else
3367%    set(handles(1),'String','civ1')
3368% end
3369elseif isequal(Civ,1) || isequal(Civ,2)
3370   imax=1;
3371elseif isequal(Civ,3)
3372    imax=3;
3373elseif isequal(Civ,4) || isequal(Civ,5)
3374    imax=4;
3375elseif isequal(Civ,6) %patch2
3376    imax=6;
3377end
3378for ibutton=1:imax;
3379    set(handles(ibutton),'Visible','on')  % unvisible civ buttons
3380end
3381% for ibutton=max(imax+1,2):6;
3382for ibutton=imax+1:6;
3383    set(handles(ibutton),'Visible','off')  % unvisible civ buttons
3384    set(handles(ibutton),'Value',0)%unactivate unvisible buttons
3385end
3386
3387%-------------------------------------------------------------------
3388% --- Executes on button press in civ1.
3389function civ1_Callback(hObject, eventdata, handles)
3390%-------------------------------------------------------------------
3391if get(handles.civ1,'Value')==1
3392    reset_vel_type([handles.interp1 handles.civ2 handles.filter1 handles.interp1 handles.interp2 handles.filter2],handles.civ1)
3393else
3394    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3395end
3396run0_Callback(hObject, eventdata, handles)
3397
3398%-------------------------------------------------------------------
3399% --- Executes on button press in interp1.
3400function interp1_Callback(hObject, eventdata, handles)
3401%-------------------------------------------------------------------
3402if get(handles.interp1,'Value')==1
3403    reset_vel_type([handles.civ1 handles.civ2 handles.filter1 handles.interp2 handles.filter2],handles.interp1)
3404else
3405     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3406end
3407run0_Callback(hObject, eventdata, handles)
3408
3409%-------------------------------------------------------------------
3410% --- Executes on button press in filter1.
3411function filter1_Callback(hObject, eventdata, handles)
3412%-------------------------------------------------------------------
3413if get(handles.filter1,'Value')==1
3414    reset_vel_type([handles.civ1 handles.civ2 handles.interp1 handles.interp2 handles.filter2],handles.filter1)
3415else
3416     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3417end
3418run0_Callback(hObject, eventdata, handles)
3419
3420%-------------------------------------------------------------------
3421% --- Executes on button press in civ2.
3422function civ2_Callback(hObject, eventdata, handles)
3423%-------------------------------------------------------------------
3424if get(handles.civ2,'Value')==1
3425    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.interp2 handles.filter2],handles.civ2)
3426else
3427     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3428end
3429run0_Callback(hObject, eventdata, handles)
3430
3431%-----------------------------------------
3432% --- Executes on button press in interp2.
3433%-------------------------------------------
3434function interp2_Callback(hObject, eventdata, handles)
3435if get(handles.interp2,'Value')==1
3436    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.filter2],handles.interp2)
3437else
3438     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3439end
3440run0_Callback(hObject, eventdata, handles)
3441%---------------------------------------------
3442% --- Executes on button press in filter2.
3443%-------------------------------------------
3444function filter2_Callback(hObject, eventdata, handles)
3445if get(handles.filter2,'Value')==1
3446    reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2],handles.filter2)
3447else
3448     reset_vel_type([handles.civ1 handles.filter1 handles.interp1 handles.civ2 handles.interp2 handles.filter2])
3449end
3450run0_Callback(hObject, eventdata, handles)
3451
3452%---------------------------------------------
3453function civ1_1_Callback(hObject, eventdata, handles)
3454%---------------------------------------------
3455if get(handles.civ1_1,'Value')==1
3456    reset_vel_type([handles.interp1_1 handles.civ2_1 handles.filter1_1 handles.interp1_1 handles.interp2_1 handles.filter2_1],handles.civ1_1)
3457else
3458     reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3459end
3460run0_Callback(hObject, eventdata, handles)
3461
3462%--------------------------------------------
3463function interp1_1_Callback(hObject, eventdata, handles)
3464%--------------------------------------------
3465if get(handles.interp1_1,'Value')==1
3466    reset_vel_type([handles.civ1_1 handles.civ2_1 handles.filter1_1 handles.interp2_1 handles.filter2_1],handles.interp1_1)
3467else
3468    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3469end
3470run0_Callback(hObject, eventdata, handles)
3471
3472%--------------------------------------------
3473function filter1_1_Callback(hObject, eventdata, handles)
3474%--------------------------------------------
3475if get(handles.filter1_1,'Value')==1
3476    reset_vel_type([handles.interp1_1 handles.civ2_1 handles.interp1_1 handles.interp2_1 handles.filter2_1],handles.filter1_1)
3477else
3478    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3479end
3480run0_Callback(hObject, eventdata, handles)
3481
3482%--------------------------------------------
3483function civ2_1_Callback(hObject, eventdata, handles)
3484%--------------------------------------------
3485if get(handles.civ2_1,'Value')==1
3486    reset_vel_type([handles.civ1_1 handles.interp1_1  handles.filter1_1 handles.interp2_1 handles.filter2_1],handles.civ2_1)
3487else
3488    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3489end
3490run0_Callback(hObject, eventdata, handles)
3491
3492%--------------------------------------------
3493function interp2_1_Callback(hObject, eventdata, handles)
3494%--------------------------------------------
3495if get(handles.interp2_1,'Value')==1
3496    reset_vel_type([handles.civ1_1 handles.civ2_1 handles.filter1_1 handles.interp1_1 handles.filter2_1],handles.interp2_1)
3497else
3498    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3499end
3500run0_Callback(hObject, eventdata, handles)
3501
3502%--------------------------------------------
3503function filter2_1_Callback(hObject, eventdata, handles)
3504%--------------------------------------------
3505if get(handles.filter2_1,'Value')==1
3506    reset_vel_type([handles.civ1_1 handles.interp1_1 handles.civ2_1 handles.filter1_1 handles.interp1_1 handles.interp2_1],handles.filter2_1)
3507else
3508    reset_vel_type([handles.civ1_1 handles.filter1_1 handles.interp1_1 handles.civ2_1 handles.interp2_1 handles.filter2_1])
3509end
3510run0_Callback(hObject, eventdata, handles)
3511
3512%-----------------------------------------------
3513% --- reset civ buttons
3514function reset_vel_type(handles_civ0,handle1)
3515for ibutton=1:length(handles_civ0)
3516    set(handles_civ0(ibutton),'BackgroundColor',[0.831 0.816 0.784])
3517    set(handles_civ0(ibutton),'Value',0)
3518end
3519if exist('handle1','var')%handles of selected button
3520        set(handle1,'BackgroundColor',[1 1 0]) 
3521end
3522
3523%-------------------------------------------------------
3524% --- Executes on button press in MENUVOLUME.
3525%-------------------------------------------------------
3526function VOLUME_Callback(hObject, eventdata, handles)
3527%errordlg('command VOL not implemented yet')
3528if ishandle(handles.UVMAT_title)
3529    delete(handles.UVMAT_title)
3530end
3531UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3532if isequal(get(handles.VOLUME,'Value'),1)
3533    set(handles.zoom,'Value',0)
3534    set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
3535    set(handles.edit_vect,'Value',0)
3536    edit_vect_Callback(hObject, eventdata, handles)
3537    set(handles.edit_object,'Value',0)
3538    set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
3539%     set(handles.cal,'Value',0)
3540%     set(handles.cal,'BackgroundColor',[0 1 0])
3541    set(handles.edit_vect,'Value',0)
3542    edit_vect_Callback(hObject, eventdata, handles)
3543    %initiate set_object GUI
3544    data.TITLE='VOLUME';
3545    if isfield(UvData,'CoordType')
3546        data.CoordType=UvData.CoordType;
3547    end
3548    if isfield(UvData,'Mesh')&~isempty(UvData.Mesh)
3549        data.RangeY=UvData.Mesh;
3550        data.RangeX=UvData.Mesh;
3551        data.DX=UvData.Mesh;
3552        data.DY=UvData.Mesh;
3553    elseif isfield(UvData.Field,'AX')&isfield(UvData.Field,'AY')& isfield(UvData.Field,'A')%only image
3554        np=size(UvData.Field.A);
3555        meshx=(UvData.Field.AX(end)-UvData.Field.AX(1))/np(2);
3556        meshy=abs(UvData.Field.AY(end)-UvData.Field.AY(1))/np(1);
3557        data.RangeY=max(meshx,meshy);
3558        data.RangeX=max(meshx,meshy);
3559        data.DX=max(meshx,meshy);
3560    end
3561    data.ParentButton=handles.VOLUME;
3562    PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
3563    [hset_object,UvData.sethandles]=set_object(data,PlotHandles);% call the set_object interface with action on haxes,
3564                                                      % associate the set_object interface handle to the plotting axes
3565    if isfield(UvData.OpenParam,'SetObjectOrigin')                                               
3566    pos_uvmat=get(huvmat,'Position');
3567    pos_set_object(1:2)=UvData.OpenParam.SetObjectOrigin + pos_uvmat(1:2);
3568    pos_set_object(3:4)=UvData.OpenParam.SetObjectSize .* pos_uvmat(3:4); 
3569    set(hset_object,'Position',pos_set_object)
3570    end
3571    UvData.MouseAction='create_object';
3572else
3573    set(handles.VOLUME,'BackgroundColor',[0 1 0])
3574    UvData.MouseAction='none';
3575end
3576set(huvmat,'UserData',UvData)
3577
3578%-------------------------------------------------------
3579function edit_vect_Callback(hObject, eventdata, handles)
3580%-------------------------------------------------------
3581%
3582% UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
3583if isequal(get(handles.edit_vect,'Value'),1)
3584    test_civ2=isequal(get(handles.civ2,'BackgroundColor'),[1 1 0]);
3585    test_civ1=isequal(get(handles.civ1,'BackgroundColor'),[1 1 0]);
3586    if ~test_civ2 && ~test_civ1
3587        msgbox_uvmat('ERROR','manual correction only possible for CIV1 or CIV2 velocity fields')
3588    end
3589    set(handles.record,'Visible','on')
3590    set(handles.edit_vect,'BackgroundColor',[1 1 0])
3591    set(handles.edit_object,'Value',0)
3592    set(handles.zoom,'Value',0)
3593    set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
3594%     set(handles.create,'Value',0)
3595%     set(handles.create,'BackgroundColor',[0 1 0])
3596    set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
3597    set(gcf,'Pointer','arrow')
3598%     UvData.MouseAction='edit_vect';
3599else
3600    set(handles.record,'Visible','off')
3601    set(handles.edit_vect,'BackgroundColor',[0.7 0.7 0.7])
3602%     UvData.MouseAction='none';
3603end
3604% set(handles.uvmat,'UserData',UvData)
3605
3606%----------------------------------------------
3607function save_mask_Callback(hObject, eventdata, handles)
3608%-----------------------------------------------------------------------
3609UvData=get(handles.uvmat,'UserData');
3610
3611flag=1;
3612npx=size(UvData.Field.A,2);
3613npy=size(UvData.Field.A,1);
3614xi=0.5:npx-0.5;
3615yi=0.5:npy-0.5;
3616[Xi,Yi]=meshgrid(xi,yi);
3617if isfield(UvData,'Object')
3618    for iobj=1:length(UvData.Object)
3619        ObjectData=UvData.Object{iobj};
3620        if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
3621            flagobj=1;
3622            testphys=0; %coordinates in pixels by default
3623            if isfield(ObjectData,'CoordType') && isequal(ObjectData.CoordType,'phys')
3624                if isfield(UvData,'XmlData')&& isfield(UvData.XmlData,'GeometryCalib')
3625                    Calib=UvData.XmlData.GeometryCalib;
3626                    testphys=1;
3627                end
3628            end
3629            if isfield(ObjectData,'Coord')& isfield(ObjectData,'Style')
3630                if isequal(ObjectData.Style,'polygon')
3631                    X=ObjectData.Coord(:,1);
3632                    Y=ObjectData.Coord(:,2);
3633                    if testphys
3634                        [X,Y]=px_XYZ(Calib,X,Y,0);% to generalise with 3D cases
3635                    end
3636                    flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside                 
3637                elseif isequal(ObjectData.Style,'ellipse')
3638                    if testphys
3639                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3640                    end
3641                    RangeX=max(ObjectData.RangeX);
3642                    RangeY=max(ObjectData.RangeY);
3643                    X2Max=RangeX*RangeX;
3644                    Y2Max=RangeY*RangeY;
3645                    distX=(Xi-ObjectData.Coord(1,1));
3646                    distY=(Yi-ObjectData.Coord(1,2));
3647                    flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
3648                elseif isequal(ObjectData.Style,'rectangle')
3649                    if testphys
3650                        %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
3651                    end
3652                    distX=abs(Xi-ObjectData.Coord(1,1));
3653                    distY=abs(Yi-ObjectData.Coord(1,2));
3654                    flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
3655                end
3656                if isequal(ObjectData.ProjMode,'mask_outside')
3657                    flagobj=~flagobj;
3658                end
3659                flag=flag & flagobj;
3660            end
3661        end
3662    end
3663end
3664% flag=~flag;
3665%mask name
3666RootPath=get(handles.RootPath,'String');
3667RootFile=get(handles.RootFile,'String');
3668if ~isempty(RootFile)&&(isequal(RootFile(1),'/')|| isequal(RootFile(1),'\'))
3669        RootFile(1)=[];
3670end
3671filebase=fullfile(RootPath,RootFile);
3672list=get(handles.masklevel,'String');
3673masknumber=num2str(length(list));
3674maskindex=get(handles.masklevel,'Value');
3675mask_name=name_generator([filebase '_' masknumber 'mask'],maskindex,1,'.png','_i');
3676imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
3677imflag=flipdim(imflag,1);
3678% imflag=uint8(255*flag);% =0 for flag=0 (vectors=0 when 20<imflag<200)
3679msgbox_uvmat('CONFIRMATION',[mask_name ' saved'])
3680imwrite(imflag,mask_name,'BitDepth',8);
3681
3682%display the mask
3683%update_mask(handles,num_i1,num_j1)
3684figure;
3685vec=linspace(0,1,256);%define a linear greyscale colormap
3686map=[vec' vec' vec'];
3687colormap(map)
3688
3689image(imflag);
3690
3691%-------------------------------------------------------------------
3692%-------------------------------------------------------------------
3693%  - FUNCTIONS FOR SETTING PLOTTING PARAMETERS
3694
3695%------------------------------------------------------------------
3696
3697
3698
3699%------------------------------------------------------------------
3700% --- Executes on selection change in col_vec: choice of the color code.
3701%
3702function col_vec_Callback(hObject, eventdata, handles)
3703%------------------------------------------------------------------
3704% edit the choice for color code
3705list_code=get(handles.col_vec,'String');% list menu fields
3706index_code=get(handles.col_vec,'Value');% selected string index
3707col_code= list_code{index_code(1)}; % selected field
3708if isequal(col_code,'black') || isequal(col_code,'white')
3709   set(handles.slider1,'Visible','off')
3710   set(handles.slider2,'Visible','off')
3711   set(handles.colcode1,'Visible','off')
3712   set(handles.colcode2,'Visible','off')
3713   set(handles.AutoVecColor,'Visible','off')
3714   set_vec_col_bar(handles)
3715else
3716   set(handles.slider1,'Visible','on')
3717   set(handles.slider2,'Visible','on')
3718   set(handles.colcode1,'Visible','on')
3719   set(handles.colcode2,'Visible','on')
3720   set(handles.AutoVecColor,'Visible','on') 
3721   if isequal(col_code,'ima_cor')
3722       set(handles.AutoVecColor,'Value',0)%fixed scale by default
3723       set(handles.vec_col_bar,'Value',0)% 3 colors r,g,b by default
3724       set(handles.slider1,'Min',0);
3725       set(handles.slider1,'Max',1);
3726       set(handles.slider2,'Min',0);
3727       set(handles.slider2,'Max',1);
3728 %      set(handles.min_title_vec,'String','0')
3729       set(handles.max_vec,'String','1')
3730       set(handles.colcode1,'String','0.333')
3731       colcode1_Callback(hObject, eventdata, handles)
3732       set(handles.colcode2,'String','0.666')
3733       colcode2_Callback(hObject, eventdata, handles)
3734   else
3735       set(handles.AutoVecColor,'Value',1)%auto scale between min,max by default
3736       set(handles.vec_col_bar,'Value',1)% colormap 'jet' by default
3737       minval=get(handles.slider1,'Min');
3738       maxval=get(handles.slider1,'Max');
3739       set(handles.slider1,'Value',minval)
3740       set(handles.slider2,'Value',maxval)
3741       set_vec_col_bar(handles)
3742   end
3743%    slider_update(handles)
3744end
3745%replot the current graph
3746run0_Callback(hObject, eventdata, handles)
3747
3748
3749%----------------------------------------------------------------
3750% -- Executes on slider movement to set the color code
3751%
3752function slider1_Callback(hObject, eventdata, handles)
3753%------------------------------------------------------------------
3754slider1=get(handles.slider1,'Value');
3755min_val=str2num(get(handles.min_vec,'String'));
3756max_val=str2num(get(handles.max_vec,'String'));
3757col=min_val+(max_val-min_val)*slider1;
3758set(handles.colcode1,'String',num2str(col))
3759if(get(handles.slider2,'Value') < col)%move also the second slider at the same value if needed
3760    set(handles.slider2,'Value',col)
3761    set(handles.colcode2,'String',num2str(col))
3762end
3763colcode1_Callback(hObject, eventdata, handles)
3764
3765%----------------------------------------------------------------
3766% Executes on slider movement to set the color code
3767%----------------------------------------------------------------
3768function slider2_Callback(hObject, eventdata, handles)
3769slider2=get(handles.slider2,'Value');
3770min_val=str2num(get(handles.min_vec,'String'));
3771max_val=str2num(get(handles.max_vec,'String'));
3772col=min_val+(max_val-min_val)*slider2;
3773set(handles.colcode2,'String',num2str(col))
3774if(get(handles.slider1,'Value') > col)%move also the first slider at the same value if needed
3775    set(handles.slider1,'Value',col)
3776    set(handles.colcode1,'String',num2str(col))
3777end
3778colcode2_Callback(hObject, eventdata, handles)
3779
3780%----------------------------------------------------------------
3781%execute on return carriage on the edit box corresponding to slider 1
3782%----------------------------------------------------------------
3783function colcode1_Callback(hObject, eventdata, handles)
3784% col=str2num(get(handles.colcode1,'String'));
3785% set(handles.slider1,'Value',col)
3786set_vec_col_bar(handles)
3787update_plot(handles);
3788
3789%----------------------------------------------------------------
3790%execute on return carriage on the edit box corresponding to slider 2
3791%----------------------------------------------------------------
3792function colcode2_Callback(hObject, eventdata, handles)
3793% col=str2num(get(handles.colcode2,'String'));
3794% set(handles.slider2,'Value',col)
3795% slider2_Callback(hObject, eventdata, handles)
3796set_vec_col_bar(handles)
3797update_plot(handles);
3798%------------------------------------------------------------
3799%update the slider values after displaying vectors
3800%--------------------------------------------------------
3801% function slider_update(handles,auto,minC,colcode1,colcode2,maxC)
3802% set(handles.slider1,'Min',minC)
3803% set(handles.slider1,'Max',maxC)
3804% set(handles.slider2,'Min',minC)
3805% set(handles.slider2,'Max',maxC)
3806% set(handles.min_title_vec,'String',num2str(minC))
3807% set(handles.max_vec,'String',num2str(maxC))
3808% if auto
3809%         set(handles.colcode1,'String',num2str(colcode1,3))%update display
3810%         set(handles.colcode2,'String',num2str(colcode2,3))
3811% end
3812% set(handles.slider1,'Value',colcode1)%update slider with constant display
3813% set(handles.slider2,'Value',colcode2)
3814% set_vec_col_bar(handles)
3815
3816
3817%-------------------------------------------------------
3818% --- Executes on button press in AutoVecColor.
3819%-------------------------------------------------------
3820function vec_col_bar_Callback(hObject, eventdata, handles)
3821set_vec_col_bar(handles)
3822
3823%-------------------------------------------------------------
3824% --- Executes on selection change in transform_fct.
3825function transform_fct_Callback(hObject, eventdata, handles)
3826%-------------------------------------------------------------
3827global nb_builtin
3828
3829huvmat=get(handles.transform_fct,'parent');
3830menu=get(handles.transform_fct,'String');
3831ind_coord=get(handles.transform_fct,'Value');
3832coord_option=menu{ind_coord};
3833list_transform=get(handles.transform_fct,'UserData');
3834ff=functions(list_transform{end}); 
3835if isequal(coord_option,'more...');
3836    coord_fct='';
3837    prompt = {'Enter the name of the transform function'};
3838    dlg_title = 'user defined transform';
3839    num_lines= 1;
3840    [FileName, PathName] = uigetfile( ...
3841       {'*.m', ' (*.m)';
3842        '*.m',  '.m files '; ...
3843        '*.*', 'All Files (*.*)'}, ...
3844        'Pick a file', ff.file);
3845    if isequal(PathName(end),'/')||isequal(PathName(end),'\')
3846        PathName(end)=[];
3847    end
3848    transform_selected =fullfile(PathName,FileName);
3849    if ~exist(transform_selected,'file')
3850           return
3851    end
3852   [ppp,transform,ext_fct]=fileparts(FileName);% removes extension .m
3853   if ~isequal(ext_fct,'.m')
3854        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
3855        return
3856   end
3857   menu=update_menu(handles.transform_fct,transform);%add the selected fct to the menu
3858   ind_coord=get(handles.transform_fct,'Value');
3859   addpath(PathName)
3860   list_transform{ind_coord}=str2func(transform);% create the function handle corresponding to the newly seleced function
3861   set(handles.transform_fct,'UserData',list_transform)
3862   rmpath(PathName)
3863   % save the new menu in the personal file 'uvmat_perso.mat'
3864   dir_perso=prefdir;%personal Matalb directory
3865   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
3866   if exist(profil_perso,'file')
3867       for ilist=nb_builtin+1:numel(list_transform)
3868           ff=functions(list_transform{ilist});
3869           transform_fct{ilist-nb_builtin}=ff.file;
3870       end
3871        save (profil_perso,'transform_fct','-append'); %store the root name for future opening of uvmat
3872   end   
3873end
3874
3875%check the current path to the selected function
3876if isa(list_transform{ind_coord},'function_handle')
3877    func=functions(list_transform{ind_coord});
3878    set(handles.path_transform,'String',fileparts(func.file)); %show the path to the senlected function
3879else
3880    set(handles.path_transform,'String','')
3881end
3882
3883set(handles.FixedLimits,'Value',0)
3884set(handles.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
3885
3886UvData=get(huvmat,'UserData');
3887
3888%delete drawn objects
3889hother=findobj('Tag','proj_object');%find all the proj objects
3890for iobj=1:length(hother)
3891    delete_object(hother(iobj))
3892end
3893hother=findobj('Tag','DeformPoint');%find all the proj objects
3894for iobj=1:length(hother)
3895    delete_object(hother(iobj))
3896end
3897hh=findobj('Tag','calib_points');
3898if ~isempty(hh)
3899    delete(hh)
3900end
3901hhh=findobj('Tag','calib_marker');
3902if ~isempty(hhh)
3903    delete(hhh)
3904end
3905if isfield(UvData,'Object')
3906     UvData.Object=UvData.Object(1);
3907end
3908list_object=get(handles.list_object_1,'String');
3909set(handles.list_object_1,'Value',1)
3910set(handles.list_object_1,'String',list_object(1))
3911set(handles.list_object_2,'Value',2)
3912set(handles.list_object_2,'String',[list_object(1);{'...'}])
3913list_object_2_Callback(hObject, eventdata, handles)
3914
3915%delete mask if it is displayed
3916if isequal(get(handles.mask_test,'Value'),1)%if the mask option is on
3917   UvData=rmfield(UvData,'MaskName'); %will impose mask refresh 
3918end
3919set(huvmat,'UserData',UvData)
3920run0_Callback(hObject, eventdata, handles)
3921
3922%--------------------------------------------
3923function histo1_menu_Callback(hObject, eventdata, handles)
3924%--------------------------------------------
3925%plot first histo
3926huvmat=get(handles.histo1_menu,'parent');
3927histo_menu=get(handles.histo1_menu,'String');
3928histo_value=get(handles.histo1_menu,'Value');
3929FieldName=histo_menu{histo_value};
3930% UvData=get(huvmat,'UserData');
3931update_histo(handles.histo_u,huvmat,FieldName)
3932
3933%----------------------------------------------
3934function histo2_menu_Callback(hObject, eventdata, handles)
3935%----------------------------------------------
3936%plot second histo
3937huvmat=get(handles.histo2_menu,'parent');
3938histo_menu=get(handles.histo2_menu,'String');
3939histo_value=get(handles.histo2_menu,'Value');
3940FieldName=histo_menu{histo_value};
3941% UvData=get(huvmat,'UserData');
3942update_histo(handles.histo_v,huvmat,FieldName)
3943
3944
3945%--------------------------------------------
3946%read the field .Fieldname stored in UvData and plot its histogram
3947function update_histo(haxes,huvmat,FieldName)
3948UvData=get(huvmat,'UserData');
3949if ~isfield(UvData.Field,FieldName)
3950    msgbox_uvmat('ERROR',['no field  ' FieldName ' for histogram'])
3951    return
3952end
3953Field=UvData.Field;
3954FieldHisto=eval(['Field.' FieldName]);
3955if isfield(Field,'FF') && ~isempty(Field.FF) && isequal(size(Field.FF),size(FieldHisto))
3956    indsel=find(Field.FF==0);%find values marked as false
3957    if ~isempty(indsel)
3958        FieldHisto=FieldHisto(indsel);
3959    end
3960end
3961if isempty(Field)
3962    msgbox_uvmat('ERROR',['empty field ' FieldName])
3963else
3964    nxy=size(FieldHisto);
3965    Amin=double(min(min(min(FieldHisto))));%min of image
3966    Amax=double(max(max(max(FieldHisto))));%max of image
3967    if isequal(Amin,Amax)
3968        msgbox_uvmat('WARNING',['uniform field =' num2str(Amin)]);
3969    else
3970        Histo.ListVarName={FieldName,'histo'};
3971        if isequal(Field.NbDim,3)
3972            Histo.VarDimName={FieldName,FieldName}; %dimensions for the histogram
3973        else
3974            if numel(nxy)==2
3975                Histo.VarDimName={FieldName,FieldName}; %dimensions for the histogram
3976            else
3977                Histo.VarDimName={FieldName,{FieldName,'rgb'}}; %dimensions for the histogram
3978            end
3979        end
3980        %unit
3981        units=[]; %default
3982        for ivar=1:numel(Field.ListVarName)
3983            if strcmp(Field.ListVarName{ivar},FieldName)
3984                if isfield(Field,'VarAttribute') && numel(Field.VarAttribute)>=ivar && isfield(Field.VarAttribute{ivar},'units')
3985                    units=Field.VarAttribute{ivar}.units;
3986                    break
3987                end
3988            end
3989        end
3990        if ~isempty(units)
3991            Histo.VarAttribute{1}.units=units;
3992        end
3993        eval(['Histo.' FieldName '=linspace(Amin,Amax,50);'])%absissa values for histo
3994        if isequal(Field.NbDim,3)
3995            C=reshape(double(FieldHisto),1,[]);% reshape in a vector
3996            eval(['Histo.histo(:,1)=hist(C, Histo.' FieldName ');']);  %calculate histogram
3997        else
3998            for col=1:size(FieldHisto,3)
3999                B=FieldHisto(:,:,col);
4000                C=reshape(double(B),1,nxy(1)*nxy(2));% reshape in a vector
4001                eval(['Histo.histo(:,col)=hist(C, Histo.' FieldName ');']);  %calculate histogram
4002            end
4003        end
4004        set(haxes,'XLimMode','auto')%reset auto mode (after zoom effect)
4005        set(haxes,'YLimMode','auto')
4006        plot_field(Histo,haxes);
4007    end
4008end
4009
4010
4011
4012%------------------------------------------------
4013%CALLBACKS FOR PLOTTING PARAMETERS
4014%-------------------------------------------------
4015
4016%-----------------------------------------------------------------
4017function MinA_Callback(hObject, eventdata, handles)
4018%------------------------------------------
4019set(handles.AutoScal,'Value',1) %suppress auto mode
4020set(handles.AutoScal,'BackgroundColor',[1 1 0])
4021update_plot(handles);
4022
4023%-----------------------------------------------------------------
4024function MaxA_Callback(hObject, eventdata, handles)
4025%--------------------------------------------
4026set(handles.AutoScal,'Value',1) %suppress auto mode
4027set(handles.AutoScal,'BackgroundColor',[1 1 0])
4028update_plot(handles);
4029
4030%-----------------------------------------------
4031function AutoScal_Callback(hObject, eventdata, handles)
4032%--------------------------------------------
4033test=get(handles.AutoScal,'Value');
4034if test
4035    set(handles.AutoScal,'BackgroundColor',[1 1 0])
4036else
4037    set(handles.AutoScal,'BackgroundColor',[0.7 0.7 0.7])
4038    update_plot(handles);
4039end
4040
4041%-------------------------------------------------------------------
4042function BW_Callback(hObject, eventdata, handles)
4043%-------------------------------------------------------------------
4044update_plot(handles);
4045
4046%-------------------------------------------------------------------
4047function Contours_Callback(hObject, eventdata, handles)
4048%-------------------------------------------------------------------
4049val=get(handles.Contours,'Value');
4050if val==2
4051    set(handles.interval_txt,'Visible','on')
4052    set(handles.IncrA,'Visible','on')
4053else
4054    set(handles.interval_txt,'Visible','off')
4055    set(handles.IncrA,'Visible','off')
4056end
4057update_plot(handles);
4058
4059%-------------------------------------------------------------------
4060function IncrA_Callback(hObject, eventdata, handles)
4061%-------------------------------------------------------------------
4062update_plot(handles);
4063
4064%-------------------------------------------------------------------
4065function HideWarning_Callback(hObject, eventdata, handles)
4066%-------------------------------------------------------------------
4067update_plot(handles);
4068
4069%-------------------------------------------------------------------
4070function HideFalse_Callback(hObject, eventdata, handles)
4071%-------------------------------------------------------------------
4072update_plot(handles);
4073
4074%-------------------------------------------------------------------
4075function VecScale_Callback(hObject, eventdata, handles)
4076%-------------------------------------------------------------------
4077set(handles.AutoVec,'Value',1);
4078set(handles.AutoVec,'BackgroundColor',[1 1 0])
4079update_plot(handles);
4080
4081%-------------------------------------------------------------------
4082function AutoVec_Callback(hObject, eventdata, handles)
4083%-------------------------------------------------------------------
4084test=get(handles.AutoVec,'Value');
4085if test
4086    set(handles.AutoVec,'BackgroundColor',[1 1 0])
4087else
4088    update_plot(handles);
4089    %set(handles.VecScale,'String',num2str(ScalOut.VecScale,3))
4090    set(handles.AutoVec,'BackgroundColor',[0.7 0.7 0.7])
4091end
4092
4093%------------------------------------------------------------------------
4094% --- Executes on selection change in decimate4 (nb_vec/4).
4095function decimate4_Callback(hObject, eventdata, handles)
4096%------------------------------------------------------------------------
4097update_plot(handles);
4098
4099%------------------------------------------------------------------------
4100% --- Executes on selection change in color_code menu
4101function color_code_Callback(hObject, eventdata, handles)
4102%------------------------------------------------------------------------
4103set_vec_col_bar(handles)
4104update_plot(handles);
4105
4106%------------------------------------------------------------------------
4107% --- Executes on button press in AutoVecColor.
4108function AutoVecColor_Callback(hObject, eventdata, handles)
4109%------------------------------------------------------------------------
4110test=get(handles.AutoVecColor,'Value');
4111if test
4112    set(handles.AutoVecColor,'BackgroundColor',[1 1 0])
4113else
4114    update_plot(handles);
4115    %set(handles.VecScale,'String',num2str(ScalOut.VecScale,3))
4116    set(handles.AutoVecColor,'BackgroundColor',[0.7 0.7 0.7])
4117end
4118
4119%------------------------------------------------------------------------
4120% --- Executes on selection change in max_vec.
4121function min_vec_Callback(hObject, eventdata, handles)
4122%------------------------------------------------------------------------
4123max_vec_Callback(hObject, eventdata, handles)
4124
4125%------------------------------------------------------------------------
4126% --- Executes on selection change in max_vec.
4127function max_vec_Callback(hObject, eventdata, handles)
4128%------------------------------------------------------------------------
4129set(handles.AutoVecColor,'Value',1)
4130AutoVecColor_Callback(hObject, eventdata, handles)
4131min_val=str2num(get(handles.min_vec,'String'));
4132max_val=str2num(get(handles.max_vec,'String'));
4133slider1=get(handles.slider1,'Value');
4134slider2=get(handles.slider2,'Value');
4135colcode1=min_val+(max_val-min_val)*slider1;
4136colcode2=min_val+(max_val-min_val)*slider2;
4137set(handles.colcode1,'String',num2str(colcode1))
4138set(handles.colcode2,'String',num2str(colcode2))
4139update_plot(handles);
4140
4141%------------------------------------------------------------------------
4142% --- update the display of color code for vectors
4143function set_vec_col_bar(handles)
4144%------------------------------------------------------------------------
4145%get the image of the color display button 'vec_col_bar' in pixels
4146set(handles.vec_col_bar,'Unit','pixel');
4147pos_vert=get(handles.vec_col_bar,'Position');
4148set(handles.vec_col_bar,'Unit','Normalized');
4149width=ceil(pos_vert(3));
4150height=ceil(pos_vert(4));
4151
4152%get slider indications
4153list=get(handles.color_code,'String');
4154ichoice=get(handles.color_code,'Value');
4155colcode.ColorCode=list{ichoice};
4156colcode.MinC=str2num(get(handles.min_vec,'String'));
4157colcode.MaxC=str2num(get(handles.max_vec,'String'));
4158test3color=strcmp(colcode.ColorCode,'rgb') || strcmp(colcode.ColorCode,'bgr');
4159if test3color
4160    colcode.colcode1=str2num(get(handles.colcode1,'String'));
4161    colcode.colcode2=str2num(get(handles.colcode2,'String'));
4162end
4163% colcode.option=get(handles.vec_col_bar,'Value');
4164colcode.FixedCbounds=0;
4165% list_code=get(handles.col_vec,'String');% list menu fields
4166% index_code=get(handles.col_vec,'Value');% selected string index
4167% colcode.CName= list_code{index_code(1)}; % selected field used for vector color
4168colcode.FixedCbounds=1;
4169vec_C=colcode.MinC+(colcode.MaxC-colcode.MinC)*(0.5:width-0.5)/width;%sample of vec_C values from min to max
4170[colorlist,col_vec]=set_col_vec(colcode,vec_C);
4171oneheight=ones(1,height);
4172A1=colorlist(col_vec,1)*oneheight;
4173A2=colorlist(col_vec,2)*oneheight;
4174A3=colorlist(col_vec,3)*oneheight;
4175A(:,:,1)=A1';
4176A(:,:,2)=A2';
4177A(:,:,3)=A3';
4178set(handles.vec_col_bar,'Cdata',A)
4179
4180
4181%-------------------------------------------------------------------
4182function update_plot(handles)
4183%-------------------------------------------------------------------
4184haxes= handles.axes3;
4185UvData=get(handles.uvmat,'UserData');
4186AxeData=UvData.axes3;
4187PlotParam=read_plot_param(handles);
4188[PlotType,PlotParamOut]= plot_field(AxeData,haxes,PlotParam,1);
4189write_plot_param(handles,PlotParamOut); %update the auto plot parameters
4190
4191%-------------------------------------------------------------------
4192% --- Executes on button press in grid.
4193function grid_Callback(hObject, eventdata, handles)
4194
4195
4196
4197%-------------------------------------------------------------------
4198% --- Executes on selection change in edit_object.
4199function edit_object_Callback(hObject, eventdata, handles)
4200%-------------------------------------------------------------------
4201UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4202test=get(handles.edit_object,'Value');
4203if test
4204    set(handles.edit_object,'BackgroundColor',[1,1,0]) 
4205    %suppress the other options
4206    set(handles.zoom,'Value',0)
4207    zoom_Callback(hObject, eventdata, handles)
4208    hgeometry_calib=findobj(allchild(0),'tag','geometry_calib');
4209    if ishandle(hgeometry_calib)
4210        hhgeometry_calib=guidata(hgeometry_calib);
4211        set(hhgeometry_calib.edit_append,'Value',0)% desactivate mouse action in geometry_calib
4212        set(hhgeometry_calib.edit_append,'BackgroundColor',[0.7 0.7 0.7])
4213    end
4214else
4215    UvData.MouseAction='none';
4216    set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7])   
4217    hset_object=findobj(allchild(0),'tag','set_object');% look for the set_object GUI
4218    if ~isempty(hset_object)
4219        delete(hset_object)% delete the current GUI set_object
4220    end
4221end
4222set(handles.uvmat,'UserData',UvData);
4223hset_object=findobj(allchild(0),'Tag','set_object');
4224if ~isempty(hset_object)
4225    hhset_object=guidata(hset_object);
4226    if test
4227        set(hhset_object.PLOT,'enable','on');
4228    else
4229       set(hhset_object.PLOT,'enable','off');
4230    end
4231end
4232
4233%------------------------------------------------------------------------
4234% --- Executes on selection change in list_object_1.
4235function list_object_1_Callback(hObject, eventdata, handles)
4236%------------------------------------------------------------------------
4237list_str=get(handles.list_object_1,'String');
4238IndexObj=get(handles.list_object_1,'Value');
4239str_1=list_str{IndexObj};
4240val_2=get(handles.list_object_2,'Value');
4241str_2=get(handles.list_object_2,'String');
4242if isequal(val_2,IndexObj)% if the first selection is equal to the second, it will suppress the second
4243    set(handles.list_object_2,'Value',numel(str_2))%select the end of the list ('...')
4244    list_object_2_Callback(hObject, eventdata, handles)
4245end
4246update_object(handles,IndexObj,1,str_1)
4247
4248%------------------------------------------------------------------------
4249% --- Executes on selection change in list_object_1.
4250function list_object_2_Callback(hObject, eventdata, handles)
4251%------------------------------------------------------------------------
4252list_str=get(handles.list_object_2,'String');
4253IndexObj=get(handles.list_object_2,'Value');
4254if ischar(list_str) || strcmp(list_str{IndexObj},'...')
4255    hview_field=findobj(allchild(0),'Tag','view_field');
4256    if ~isempty(hview_field)
4257        delete(hview_field)
4258    end
4259    hset_object=findobj(allchild(0),'Tag','set_object');
4260    if ~isempty(hset_object)
4261        delete(hset_object)
4262    end
4263else
4264    update_object(handles,IndexObj,2,list_str{IndexObj})
4265end
4266
4267%------------------------------------------------------------------------
4268function update_object(handles,IndexObj,option,ObjectName)
4269%------------------------------------------------------------------------
4270UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4271if numel(UvData.Object)<IndexObj;
4272    return
4273end
4274ObjectData=UvData.Object{IndexObj};
4275ObjectData.Name=ObjectName;
4276if isequal(get(handles.edit_object,'Value'),1)
4277    ObjectData.enable_plot=1; % desable the PLOT option in the set_object GUI (editing mode
4278end
4279ZBounds=0; % default
4280if isfield(UvData.Field,'ZMin') && isfield(UvData.Field,'ZMax')
4281    ZBounds(1)=UvData.Field.ZMin; %minimum for the Z slider
4282    ZBounds(2)=UvData.Field.ZMax;%maximum for the Z slider
4283end
4284hset_object=findobj(allchild(0),'tag','set_object');
4285if ~isempty(hset_object)
4286    delete(hset_object)% delete existing version of set_object
4287end
4288edit_test=get(handles.edit_object,'Value');
4289if edit_test
4290    ObjectData.enable_plot=1;
4291else
4292        if isfield(ObjectData,'enable_plot')
4293        ObjectData=rmfield(ObjectData,'enable_plot');
4294    end
4295end
4296hset_object=set_object(ObjectData,[],ZBounds);% call the set_object interface,
4297
4298% %project the current field on the object and plot it
4299ProjData= proj_field(UvData.Field,ObjectData);%project the current interface field on ObjectData
4300if option==1%length(UvData.Object)>= IndexObj && isfield(UvData.Object{IndexObj},'plotaxes')&& ishandle(UvData.Object{IndexObj}.plotaxes)
4301    PlotHandles=handles;
4302else
4303    hview_field=findobj(allchild(0),'tag','view_field');
4304    if isempty(hview_field)
4305        hview_field=view_field;
4306    end
4307    PlotHandles=guidata(hview_field);
4308end
4309if ~isempty(ProjData)
4310    plot_field(ProjData,PlotHandles.axes3,PlotHandles);
4311end
4312set(handles.uvmat,'UserData',UvData)
4313hother=findobj('Tag','proj_object');%find all the proj objects
4314for iobj=1:length(hother)
4315    if isequal(get(hother(iobj),'Type'),'rectangle')||isequal(get(hother(iobj),'Type'),'patch')
4316        set(hother(iobj),'EdgeColor','b')
4317        if isequal(get(hother(iobj),'FaceColor'),'m')
4318            set(hother(iobj),'FaceColor','b')
4319        end
4320    elseif isequal(get(hother(iobj),'Type'),'image')
4321        Acolor=get(hother(iobj),'CData');
4322        Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
4323        set(hother(iobj),'CData',Acolor);
4324    else
4325        set(hother(iobj),'Color','b')
4326    end
4327    set(hother(iobj),'Selected','off')
4328end
4329hother=findobj('Tag','DeformPoint');
4330set(hother,'Color','b');
4331set(hother,'Selected','off')
4332if isfield(ObjectData,'DisplayHandle_uvmat')
4333    if ishandle(ObjectData.DisplayHandle_uvmat)
4334        uistack(ObjectData.DisplayHandle_uvmat,'top')
4335        linetype=get(ObjectData.DisplayHandle_uvmat,'Type');
4336        if isequal(linetype,'line')
4337            set(ObjectData.DisplayHandle_uvmat,'Color','m'); %set the selected object to magenta color
4338        elseif isequal(linetype,'rectangle')
4339            set(ObjectData.DisplayHandle_uvmat,'EdgeColor','m'); %set the selected object to magenta color
4340        elseif isequal(linetype,'patch')
4341            set(ObjectData.DisplayHandle_uvmat,'FaceColor','m'); %set the selected object to magenta color
4342        end
4343        SubObjectData=get(ObjectData.DisplayHandle_uvmat,'UserData');
4344        if isfield(SubObjectData,'SubObject') & ishandle(SubObjectData.SubObject)
4345            uistack(SubObjectData.SubObject,'top')
4346            for iobj=1:length(SubObjectData.SubObject)
4347                hsub=SubObjectData.SubObject(iobj);
4348                if isequal(get(hsub,'Type'),'rectangle')
4349                    set(hsub,'EdgeColor','m'); %set the selected object to magenta color
4350                elseif isequal(get(hsub,'Type'),'image')
4351                    Acolor=get(hsub,'CData');
4352                    Acolor(:,:,1)=Acolor(:,:,3);
4353                    set(hsub,'CData',Acolor);
4354                else
4355                    set(hsub,'Color','m')
4356                end
4357            end
4358        end
4359        if isfield(SubObjectData,'DeformPoint') & ishandle(SubObjectData.DeformPoint)
4360            set(SubObjectData.DeformPoint,'Color','m')
4361        end
4362    end
4363    %     end
4364end
4365pause(0.1)
4366figure(hset_object)%put set_object in front
4367
4368%------------------------------------------------------
4369% --- Executes on button press in Menu/Export/field in workspace.
4370%------------------------------------------------------
4371function MenuExportField_Callback(hObject, eventdata, handles)
4372global Data_uvmat
4373Data_uvmat=get(handles.uvmat,'UserData');
4374evalin('base','global Data_uvmat')%make CurData global in the workspace
4375display('current field :')
4376evalin('base','Data_uvmat') %display CurData in the workspace
4377commandwindow; %brings the Matlab command window to the front
4378
4379%------------------------------------------------------
4380% --- Executes on button press in Menu/Export/extract figure.
4381%------------------------------------------------------
4382function MenuExport_plot_Callback(hObject, eventdata, handles)
4383huvmat=get(handles.MenuExport_plot,'parent');
4384UvData=get(huvmat,'UserData');
4385hfig=figure;
4386newaxes=copyobj(handles.axes3,hfig);
4387map=colormap(handles.axes3);
4388colormap(map);%transmit the current colormap to the zoom fig
4389colorbar
4390
4391
4392% --------------------------------------------------------------------
4393function Insert_Callback(hObject, eventdata, handles)
4394
4395
4396% --------------------------------------------------------------------
4397function MenuHelp_Callback(hObject, eventdata, handles)
4398% --------------------------------------------------------------------
4399path_to_uvmat=which ('uvmat');% check the path of uvmat
4400pathelp=fileparts(path_to_uvmat);
4401helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
4402if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
4403else
4404    addpath (fullfile(pathelp,'uvmat_doc'))
4405    web(helpfile);
4406end
4407
4408%------------------------------------------------------------------------
4409% --------------------------------------------------------------------
4410function MenuExportMovie_Callback(hObject, eventdata, handles)
4411% --------------------------------------------------------------------
4412set(handles.MenuExportMovie,'BusyAction','queue')% activate the button
4413huvmat=get(handles.run0,'parent');
4414UvData=get(huvmat,'UserData');
4415[xx,xx,FileBase]=read_file_boxes(handles);
4416 %read the current input file name
4417prompt = {'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)'};
4418dlg_title = 'select properties of the output avi movie';
4419num_lines= 1;
4420% nbfield_cell=get(handles.last_i,'String');
4421def     = {[FileBase '_out.avi'];'10';'1';'[0.03 0.05 0.95 0.92]';'10'};
4422answer = inputdlg(prompt,dlg_title,num_lines,def,'on');
4423aviname=answer{1};
4424fps=str2double(answer{2});
4425% check for existing file with output name aviname
4426if exist(aviname,'file')
4427    backup=aviname;
4428    testexist=2;
4429    while testexist==2
4430        backup=[backup '~'];
4431        testexist=exist(backup,'file');     
4432    end
4433    [success,message]=copyfile(aviname,backup);%make backup of the existing file
4434    if isequal(success,1)
4435        delete(aviname)%delete existing file
4436    else
4437        msgbox_uvmat('ERROR',message)
4438        return
4439    end
4440end
4441%create avi open
4442aviobj=avifile(aviname,'Compression','None','fps',fps);
4443
4444%display first view for tests
4445newfig=figure;
4446newaxes=copyobj(handles.axes3,newfig);%new plotting axes in the new figure
4447set(newaxes,'Tag','movieaxes')
4448nbpix=[512 384]*str2double(answer{3});
4449set(gcf,'Position',[1 1 nbpix])% resolution XVGA
4450set(newaxes,'Position',eval(answer{4}));
4451map=colormap(handles.axes3);
4452colormap(map);%transmit the current colormap to the zoom fig
4453msgbox_uvmat('INPUT_Y-N',{['adjust figure ' num2str(newfig) ' with its matlab edit menu '] ;...
4454        ['then press OK to get the avi movie as a copy of figure ' num2str(newfig) ' display']});
4455UvData.Object{1}.plotaxes=newaxes;% the axis in the new figure becomes the current main plotting axes
4456set(huvmat,'UserData',UvData);
4457increment=str2double(get(handles.increment_scan,'String')); %get the field increment d
4458if isnan(increment)
4459    set(handles.increment_scan,'String','1')%default value
4460    increment=1;
4461end
4462set(handles.STOP,'Visible','on')
4463set(handles.speed,'Visible','on')
4464set(handles.speed_txt,'Visible','on')
4465set(handles.Movie,'BusyAction','queue')
4466
4467%imin=str2double(get(handles.i1,'String'));
4468imax=str2double(answer{5});
4469% if isfield(UvData,'Time')
4470htitle=get(newaxes,'Title');
4471xlim=get(newaxes,'XLim');
4472ylim=get(newaxes,'YLim');
4473set(htitle,'Position',[xlim(2)+0.07*(xlim(2)-xlim(1)) ylim(2)-0.05*(ylim(2)-ylim(1)) 0])
4474time_str=get(handles.abs_time,'String');
4475set(htitle,'String',['t=' time_str])
4476set(handles.speed,'Value',1)
4477for i=1:imax
4478    if get(handles.speed,'Value')~=0 && isequal(get(handles.MenuExportMovie,'BusyAction'),'queue') % enable STOP command
4479            runpm(hObject,eventdata,handles,increment)% run plus
4480            drawnow
4481            time_str=get(handles.abs_time,'String');
4482            if ishandle(htitle)
4483             set(htitle,'String',['t=' time_str])
4484            end
4485            mov=getframe(newfig);
4486            aviobj=addframe(aviobj,mov);
4487    end
4488end
4489close(aviobj);
4490UvData.Object{1}.plotaxes=handles.axes3;
4491set(huvmat,'UserData',UvData);
4492msgbox_uvmat('CONFIRMATION',{['movie ' aviname ' created '];['with ' num2str(imax) ' frames']})
4493
4494%------------------------------------------------------------------------
4495function MenuCalib_Callback(hObject, eventdata, handles)
4496%------------------------------------------------------------------------
4497
4498UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4499
4500%suppress competing options
4501set(handles.zoom,'Value',0)
4502set(handles.zoom,'BackgroundColor',[0.7 0.7 0.7])
4503set(handles.list_object_1,'Value',1)     
4504% initiate display of GUI geometry_calib
4505data=[]; %default
4506if isfield(UvData,'CoordType')
4507    data.CoordType=UvData.CoordType;
4508end
4509pos=get(handles.uvmat,'Position');
4510pos(1)=pos(1)+pos(3)-0.311+0.04; %0.311= width of the geometry_calib interface (units relative to the srcreen)
4511pos(2)=pos(2)-0.02;
4512[FileName,RootPath,FileBase,FileIndices,FileExt,SubDir]=read_file_boxes(handles);
4513set(handles.view_xml,'Backgroundcolor',[1 1 0])%indicate the reading of the current xml file by geometry_calib
4514if isfield(UvData.OpenParam,'CalOrigin')
4515    pos_uvmat=get(handles.uvmat,'Position');
4516    pos_cal(1)=pos_uvmat(1)+UvData.OpenParam.CalOrigin(1)*pos_uvmat(3);
4517    pos_cal(2)=pos_uvmat(2)+UvData.OpenParam.CalOrigin(2)*pos_uvmat(4);
4518    pos_cal(3:4)=UvData.OpenParam.CalSize .* pos_uvmat(3:4);
4519end
4520geometry_calib(FileName,pos_cal);% call the geometry_calib interface   
4521set(handles.view_xml,'Backgroundcolor',[1 1 1])%indicate the end of reading of the current xml file by geometry_calib
4522set(handles.MenuCalib,'checked','on')% indicate that MenuCalib is activated, test used by mouse action
4523
4524%------------------------------------------------------------------------
4525function MenuMask_Callback(hObject, eventdata, handles)
4526%------------------------------------------------------------------------
4527UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4528ListObj=UvData.Object;
4529select=zeros(1,numel(ListObj));
4530for iobj=1:numel(ListObj);
4531    if strcmp(ListObj{iobj}.ProjMode,'mask_inside')||strcmp(ListObj{iobj}.ProjMode,'mask_outside')
4532        select(iobj)=1;
4533    end
4534end
4535val=find(select);
4536if isempty(val)
4537    msgbox_uvmat('ERROR','polygons must be first created by Projection object/mask polygon in the menu bar');
4538    return
4539else
4540    set(handles.list_object_1,'Max',2);%allow multiple selection
4541    set(handles.list_object_1,'Value',val);
4542    flag=1;
4543    npx=size(UvData.Field.A,2);
4544    npy=size(UvData.Field.A,1);
4545    xi=0.5:npx-0.5;
4546    yi=0.5:npy-0.5;
4547    [Xi,Yi]=meshgrid(xi,yi);
4548    if isfield(UvData,'Object')
4549        for iobj=1:length(UvData.Object)
4550            ObjectData=UvData.Object{iobj};
4551            if isfield(ObjectData,'ProjMode') &&(isequal(ObjectData.ProjMode,'mask_inside')||isequal(ObjectData.ProjMode,'mask_outside'));
4552                flagobj=1;
4553                testphys=0; %coordinates in pixels by default
4554                if isfield(ObjectData,'CoordUnit') && ~isequal(ObjectData.CoordUnit,'pixel')
4555                    if isfield(UvData,'XmlData')&& isfield(UvData.XmlData,'GeometryCalib')
4556                        Calib=UvData.XmlData.GeometryCalib;
4557                        testphys=1;
4558                    end
4559                end
4560                if isfield(ObjectData,'Coord')& isfield(ObjectData,'Style')
4561                    if isequal(ObjectData.Style,'polygon')
4562                        X=ObjectData.Coord(:,1);
4563                        Y=ObjectData.Coord(:,2);
4564                        if testphys
4565                            [X,Y]=px_XYZ(Calib,X,Y,0);% to generalise with 3D cases
4566                        end
4567                        flagobj=~inpolygon(Xi,Yi,X',Y');%=0 inside the polygon, 1 outside
4568                    elseif isequal(ObjectData.Style,'ellipse')
4569                        if testphys
4570                            %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4571                        end
4572                        RangeX=max(ObjectData.RangeX);
4573                        RangeY=max(ObjectData.RangeY);
4574                        X2Max=RangeX*RangeX;
4575                        Y2Max=RangeY*RangeY;
4576                        distX=(Xi-ObjectData.Coord(1,1));
4577                        distY=(Yi-ObjectData.Coord(1,2));
4578                        flagobj=(distX.*distX/X2Max+distY.*distY/Y2Max)>1;
4579                    elseif isequal(ObjectData.Style,'rectangle')
4580                        if testphys
4581                            %[X,Y]=px_XYZ(Calib,X,Y,0);% TODO:create a polygon boundary and transform to phys
4582                        end
4583                        distX=abs(Xi-ObjectData.Coord(1,1));
4584                        distY=abs(Yi-ObjectData.Coord(1,2));
4585                        flagobj=distX>max(ObjectData.RangeX) | distY>max(ObjectData.RangeY);
4586                    end
4587                    if isequal(ObjectData.ProjMode,'mask_outside')
4588                        flagobj=~flagobj;
4589                    end
4590                    flag=flag & flagobj;
4591                end
4592            end
4593        end
4594    end
4595   
4596    %mask name
4597    RootPath=get(handles.RootPath,'String');
4598    RootFile=get(handles.RootFile,'String');
4599    if ~isempty(RootFile)&&(isequal(RootFile(1),'/')|| isequal(RootFile(1),'\'))
4600        RootFile(1)=[];
4601    end
4602    filebase=fullfile(RootPath,RootFile);
4603    list=get(handles.masklevel,'String');
4604    masknumber=num2str(length(list));
4605    maskindex=get(handles.masklevel,'Value');
4606    mask_name=name_generator([filebase '_' masknumber 'mask'],maskindex,1,'.png','_i');
4607    imflag=uint8(255*(0.392+0.608*flag));% =100 for flag=0 (vectors not computed when 20<imflag<200)
4608    imflag=flipdim(imflag,1);
4609
4610    %display the mask
4611    figure;
4612    vec=linspace(0,1,256);%define a linear greyscale colormap
4613    map=[vec' vec' vec'];
4614    colormap(map)
4615    image(imflag);
4616    answer=msgbox_uvmat('INPUT_TXT','mask file name:', mask_name);
4617    if ~strcmp(answer,'Cancel')
4618        mask_dir=fileparts(answer);
4619        if ~exist(mask_dir,'dir')
4620            msgbox_uvmat('ERROR',['directory ' mask_dir ' does not exist'])
4621            return
4622        end
4623        imwrite(imflag,answer,'BitDepth',8);
4624    end
4625    set(handles.list_object_1,'Value',1)
4626    set(handles.list_object_1,'Max',1)
4627end
4628
4629%------------------------------------------------------------------------
4630%-- open the GUI set_grid.fig to create grid
4631function MenuGrid_Callback(hObject, eventdata, handles)
4632%------------------------------------------------------------------------
4633%UvData=get(handles.uvmat,'UserData');%read UvData properties stored on the uvmat interface
4634
4635%suppress the other options if grid is chosen
4636set(handles.edit_vect,'Value',0)
4637edit_vect_Callback(hObject, eventdata, handles)
4638set(handles.edit_object,'BackgroundColor',[0.7 0.7 0.7])
4639set(handles.list_object_1,'Value',1)     
4640
4641%prepare display of the set_grid GUI
4642FileName=read_file_boxes(handles);
4643CoordList=get(handles.transform_fct,'String');
4644val=get(handles.transform_fct,'Value');
4645set_grid(FileName,CoordList{val});% call the set_object interface
4646%set(handles.uvmat,'UserData',UvData);
4647
4648%------------------------------------------------------------------------
4649% open the GUI 'series'
4650function MenuSeries_Callback(hObject, eventdata, handles)
4651%------------------------------------------------------------------------
4652series; %first display of the GUI to fill waiting time
4653[param.FileName]=read_file_boxes(handles);
4654if isequal(get(handles.SubField,'Value'),1)
4655    FileName_1=read_file_boxes_1(handles);%
4656    if ~isequal(FileName_1,param.FileName)
4657        param.FileName_1=FileName_1;
4658    end
4659end
4660param.NomType=get(handles.FileIndex,'UserData');
4661param.NomType_1=get(handles.FileIndex_1,'UserData');
4662param.comp_input=get(handles.fix_pair,'Value');
4663huvmat=get(handles.MenuSeries,'parent');
4664UvData=get(huvmat,'UserData');
4665if isfield(UvData,'Time')
4666    param.Time=UvData.XmlData.Time;
4667end
4668if isequal(get(handles.scan_i,'Value'),1)
4669    param.incr_i=str2double(get(handles.increment_scan,'String'));
4670elseif isequal(get(handles.scan_j,'Value'),1)
4671    param.incr_j=str2double(get(handles.increment_scan,'String'));
4672end
4673param.list_fields=get(handles.Fields,'String');% list menu fields
4674param.list_fields(1)=[]; %suppress  'image' option
4675param.index_fields=get(handles.Fields,'Value');% selected string index
4676if param.index_fields>1
4677    param.index_fields=param.index_fields-1;
4678end
4679param.list_fields_1=get(handles.Fields_1,'String');% list menu fields
4680param.list_fields_1(1)=[]; %suppress  'image' option
4681param.index_fields_1=get(handles.Fields_1,'Value')-1;% selected string index
4682if param.index_fields_1>1
4683    param.index_fields_1=param.index_fields_1-1;
4684end
4685param.civ1=get(handles.civ1,'Value');
4686param.civ2=get(handles.civ2,'Value');
4687param.interp1=get(handles.interp1,'Value');
4688param.interp2=get(handles.interp2,'Value');
4689param.filter1=get(handles.filter1,'Value');
4690param.filter2=get(handles.filter2,'Value');
4691param.menu_coord_str=get(handles.transform_fct,'String');
4692param.menu_coord_val=get(handles.transform_fct,'Value');
4693
4694series(param); %run the series interface
4695
4696%------------------------------------------------------------------------
4697% -- open the GUI civ.fig for civx (PIV)
4698function MenuPIV_Callback(hObject, eventdata, handles)
4699%------------------------------------------------------------------------
4700 
4701[FileName,RootPath,filebase,FileIndices,ext,SubDir]=read_file_boxes(handles);
4702num1=stra2num(get(handles.i1,'String'));
4703num2=stra2num(get(handles.i2,'String'));
4704num_a=stra2num(get(handles.j1,'String'));
4705num_b=stra2num(get(handles.j2,'String'));
4706NomType=get(handles.FileIndex,'UserData');
4707ind_opening=1; % default (images): will advice civ1 option by default in the civ interface
4708if isequal(ext,'.nc') ||  isequal(ext,'.cdf')% netcdf files
4709    ind_opening=2;% propose 'fix' as the default option
4710% +read the current netcdf rootfile
4711    Data=nc2struct(FileName,'ListGlobalAttribute','fix','patch','civ2','fix2');
4712    if isfield(Data,'fix') && isequal(Data.fix,1)
4713        ind_opening=3;
4714    end
4715    if isfield(Data,'patch') && isequal(Data.patch,1)
4716        ind_opening=4;
4717    end
4718    if isfield(Data,'civ2') && isequal(Data.civ2,1)
4719        ind_opening=5;
4720    end
4721    if isfield(Data,'fix2') && isequal(Data.fix2,1)
4722        ind_opening=6;
4723    end
4724end     
4725param.RootName=filebase;
4726param.NomType=NomType;
4727param.num1=num1;
4728param.num2=num2;
4729param.num_a=num_a;
4730param.num_b=num_b;
4731param.SubDir=SubDir;
4732param.IndOpening=ind_opening;% A REVOIR +TRANSMETTRE IMADOC INFO
4733param.ImaExt=ext;
4734civ(param);% interface de civ(not in the uvmat file)
4735
4736%------------------------------------------------------------------------
4737function MenuTools_Callback(hObject, eventdata, handles)
4738%------------------------------------------------------------------------
4739
4740%------------------------------------------------------------------------
4741function MenuEditObject_Callback(hObject, eventdata, handles)
4742%------------------------------------------------------------------------
4743set(handles.edit_object,'Value',1)
4744edit_Callback(hObject, eventdata, handles)
4745
4746%------------------------------------------------------------------------
4747function enable_transform(handles,state)
4748%------------------------------------------------------------------------
4749set(handles.transform_fct,'Visible',state)
4750set(handles.TRANSFORM_txt,'Visible',state)   
4751set(handles.transform_fct,'Visible',state) 
4752set(handles.path_transform,'Visible',state)
4753set(handles.pxcmx_txt,'Visible',state)
4754set(handles.pxcmy_txt,'Visible',state)
4755set(handles.pxcm,'Visible',state)
4756set(handles.pycm,'Visible',state)
4757
4758%------------------------------------------------------------------------
4759function MenuEditVectors_Callback(hObject, eventdata, handles)
4760%------------------------------------------------------------------------
4761set(handles.edit_vect,'Visible','on')
4762set(handles.edit_vect,'Value',1)
4763edit_vect_Callback(hObject, eventdata, handles)
4764
4765% -----------------------------------------------------------------------
4766function Menupoints_Callback(hObject, eventdata, handles)
4767%------------------------------------------------------------------------
4768data.Style='points';
4769data.ProjMode='projection';%default
4770create_object(data,handles)
4771
4772% -----------------------------------------------------------------------
4773function Menuline_Callback(hObject, eventdata, handles)
4774%------------------------------------------------------------------------
4775data.Style='line';
4776data.ProjMode='projection';%default
4777create_object(data,handles)
4778
4779%------------------------------------------------------------------------
4780function Menupolyline_Callback(hObject, eventdata, handles)
4781%------------------------------------------------------------------------
4782data.Style='polyline';
4783data.ProjMode='projection';%default
4784create_object(data,handles)
4785
4786%------------------------------------------------------------------------
4787function Menupolygon_Callback(hObject, eventdata, handles)
4788%------------------------------------------------------------------------
4789data.Style='polygon';
4790data.ProjMode='inside';%default
4791create_object(data,handles)
4792
4793%------------------------------------------------------------------------
4794function Menurectangle_Callback(hObject, eventdata, handles)
4795%------------------------------------------------------------------------
4796data.Style='rectangle';
4797data.ProjMode='inside';%default
4798create_object(data,handles)
4799
4800%------------------------------------------------------------------------
4801function Menuellipse_Callback(hObject, eventdata, handles)
4802%------------------------------------------------------------------------
4803data.Style='ellipse';
4804data.ProjMode='inside';%default
4805create_object(data,handles)
4806
4807%------------------------------------------------------------------------
4808function MenuMaskObject_Callback(hObject, eventdata, handles)
4809%------------------------------------------------------------------------
4810data.Style='polygon';
4811data.StyleMenu={'polygon'};
4812data.ProjMode='mask_inside';%default
4813data.ProjMenu={'mask_inside';'mask_outside'};
4814create_object(data,handles)
4815
4816%------------------------------------------------------------------------
4817function Menuplane_Callback(hObject, eventdata, handles)
4818%------------------------------------------------------------------------
4819data.Style='plane';
4820data.ProjMode='projection';%default
4821
4822create_object(data,handles)
4823
4824%------------------------------------------------------------------------
4825function Menuvolume_Callback(hObject, eventdata, handles)
4826%------------------------------------------------------------------------
4827data.Style='volume';
4828data.ProjMode='interp';%default
4829% set(handles.create,'Visible','on')
4830% set(handles.create,'Value',1)
4831% VOLUME_Callback(hObject,eventdata,handles)
4832create_object(data,handles)
4833
4834%------------------------------------------------------------------------
4835function MenuBrowseObject_Callback(hObject, eventdata, handles)
4836%------------------------------------------------------------------------
4837%get the object file
4838[FileName, PathName, filterindex] = uigetfile( ...
4839       {'*.xml;*.mat', ' (*.xml,*.mat)';
4840       '*.xml',  '.xml files '; ...
4841        '*.mat',  '.mat matlab files '}, ...
4842        'Pick an xml Object file',get(handles.RootPath,'String'));
4843fileinput=[PathName FileName];%complete file name
4844% testblank=findstr(fileinput,' ');%look for blanks
4845% if ~isempty(testblank)
4846%     msgbox_uvmat('ERROR','forbidden input file name: contain blanks')
4847%     return
4848% end
4849sizf=size(fileinput);
4850if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
4851
4852%read the file
4853t=xmltree(fileinput);
4854data=convert(t);
4855data.enable_plot=1;
4856[pp,data.Name]=fileparts(FileName);
4857%PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
4858hset_object=findobj(allchild(0),'tag','set_object');
4859if ~isempty(hset_object)
4860    delete(hset_object)% delete existing version of set_object
4861end
4862% UvData=get(handles.uvmat,'UserData');
4863set_object(data);% call the set_object interface
4864% %position the set_object GUI with respect to uvmat
4865% pos_uvmat=get(handles.uvmat,'Position');
4866% if isfield(UvData,'SetObjectOrigin')
4867%     pos_set_object(1:2)=UvData.SetObjectOrigin + pos_uvmat(1:2);
4868%     pos_set_object(3:4)=UvData.SetObjectSize .* pos_uvmat(3:4);
4869%     set(hset_object,'Position',pos_set_object)
4870% end
4871set(handles.edit_object,'Value',0); %suppress the object edit mode
4872set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4873set(handles.MenuObject,'checked','on')
4874%UvData.MouseAction='create_object';
4875% set(handles.uvmat,'UserData',UvData)
4876set(handles.delete_object,'Visible','on')
4877set(handles.uvmat_title,'Visible','on')
4878set(handles.view_field_title,'Visible','on')
4879
4880%------------------------------------------------------------------------
4881% --- generic function used for the creation of a projection object
4882function create_object(data,handles)
4883%------------------------------------------------------------------------
4884hset_object=findobj(allchild(0),'tag','set_object');
4885if ~isempty(hset_object)
4886    delete(hset_object)% delete existing version of set_object
4887end
4888hgeometry_calib=findobj(allchild(0),'tag','geometry_calib');
4889if ishandle(hgeometry_calib)
4890    hhgeometry_calib=guidata(hgeometry_calib);
4891    set(hhgeometry_calib.edit_append,'Value',0)% desactivate mouse action in geometry_calib
4892    set(hhgeometry_calib.edit_append,'BackgroundColor',[0.7 0.7 0.7])
4893end
4894UvData=get(handles.uvmat,'UserData');
4895set(handles.edit_object,'Value',0); %suppress the object edit mode
4896set(handles.edit_object,'BackgroundColor',[0.7,0.7,0.7]) 
4897data.enable_plot=1;
4898transform_list=get(handles.transform_fct,'String');
4899val=get(handles.transform_fct,'Value');
4900%data.CoordType=transform_list{val};
4901if isfield(UvData,'Field')
4902    Field=UvData.Field;
4903    if isfield(Field,'Mesh')&&~isempty(Field.Mesh)
4904        data.RangeX=Field.Mesh;
4905        data.RangeY=Field.Mesh;
4906        data.DX=Field.Mesh;
4907        data.DY=Field.Mesh;
4908    elseif isfield(Field,'AX')&& isfield(Field,'AY')&& isfield(Field,'A')%only image
4909        np=size(Field.A);
4910        meshx=(Field.AX(end)-Field.AX(1))/np(2);
4911        meshy=abs(Field.AY(end)-Field.AY(1))/np(1);
4912        data.RangeY=max(meshx,meshy);
4913        data.RangeX=max(meshx,meshy);
4914        data.DX=max(meshx,meshy);
4915    end
4916    if isfield(Field,'NbDim')
4917        data.NbDim=Field.NbDim;
4918    end
4919    if isfield(Field,'CoordUnit')
4920        data.CoordUnit=Field.CoordUnit;
4921    end
4922end
4923data.Coord=[0 0 0]; %default
4924if isfield(data,'Style') && isequal(data.Style,'line')
4925    if isfield(data,'DX')
4926        data.Coord=[[0 0 0];[data.DX 0 0]]; %default
4927    else
4928        data.Coord=[[0 0 0];[1 0 0]]; %default
4929    end
4930end
4931if ishandle(handles.UVMAT_title)
4932    delete(handles.UVMAT_title)%delete the initial display of uvmat if no field has been entered
4933end
4934PlotHandles=get_plot_handles(handles);%get the handles of the interface elements setting the plotting parameters
4935set_object(data,PlotHandles);% call the set_object interface
4936set(handles.MenuObject,'checked','on')
4937set(handles.uvmat,'UserData',UvData)
4938set(handles.zoom,'Value',0)
4939zoom_Callback(handles.uvmat, [], handles)
4940set(handles.delete_object,'Visible','on')
4941set(handles.uvmat_title,'Visible','on')
4942set(handles.view_field_title,'Visible','on')
4943
4944%------------------------------------------------------------------------
4945function MenuRuler_Callback(hObject, eventdata, handles)
4946%------------------------------------------------------------------------
4947set(handles.zoom,'Value',0)
4948zoom_Callback(handles.uvmat, [], handles)
4949set(handles.MenuRuler,'checked','on')
4950UvData=get(handles.uvmat,'UserData');
4951UvData.MouseAction='ruler';
4952set(handles.uvmat,'UserData',UvData);
4953
4954%------------------------------------------------------------------------
4955% --- executed when closing: set the parent interface button to value 0
4956function closefcn(gcbo,eventdata)
4957%------------------------------------------------------------------------
4958%delete all the associated figures if exist
4959hh=findobj(allchild(0),'tag','view_field');
4960if ~isempty(hh)
4961    delete(hh)
4962end
4963hh=findobj(allchild(0),'tag','geometry_calib');
4964if ~isempty(hh)
4965    delete(hh)
4966end
4967hh=findobj(allchild(0),'tag','set_object');
4968if ~isempty(hh)
4969    hhh=findobj(hh,'tag','PLOT');
4970    set(hhh,'enable','off')
4971end
4972
4973%------------------------------------------------------------------------
4974% --- Executes on button press in delete_object.
4975function delete_object_Callback(hObject, eventdata, handles)
4976%------------------------------------------------------------------------
4977IndexObj=get(handles.list_object_2,'Value');
4978if IndexObj>1
4979    delete_object(IndexObj)
4980end
4981   
4982
4983
4984
4985
Note: See TracBrowser for help on using the repository browser.