source: trunk/src/uvmat.m @ 234

Last change on this file since 234 was 234, checked in by gostiaux, 13 years ago

revision file info in revision.info, not revision.log

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