source: trunk/src/uvmat.m @ 252

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

various bugs corrected.

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