source: trunk/src/uvmat.m @ 179

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

various bug repairs, in particular for 3D fields

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