source: trunk/src/uvmat.m @ 236

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

correct Matlab PIV, remove call to image tool box. Improve menu of uvmat VelType? (replacement of buttons)

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