source: trunk/src/uvmat.m @ 246

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

thin plate shell (patch) introduced

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