source: trunk/src/uvmat.m @ 187

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

various bug repairs

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