source: trunk/src/uvmat.m @ 292

Last change on this file since 292 was 292, checked in by sommeria, 9 years ago

GUI civ patch2 corrected (SmoothingParam?).
uvmatand view_field updated with panels.
read_plot_param removed (replaced by the mostgeneral read_GUI)

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