source: trunk/src/uvmat.m @ 276

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

further cleaning after new GUI civ: rationalisation of check_Grid and txt_gridName
records the path of uvmat, series, civ, by addpath

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