source: trunk/src/series.m @ 521

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

various bugs corrected

File size: 94.0 KB
Line 
1%'series': master function associated to the GUI series.m for analysis field series 
2%------------------------------------------------------------------------
3% function varargout = series(varargin)
4% associated with the GUI series.fig
5%
6%INPUT
7% param: structure with input parameters (link with the GUI uvmat)
8%      .menu_coord_str: string for the TransformName (menu for coordinate transforms)
9%      .menu_coord_val: value for TransformName (menu for coordinate transforms)
10%      .FileName: input file name
11%      .FileName_1: second input file name
12%      .list_field: menu of input fields
13%      .index_fields: chosen index
14%      .civ1=0 or 1, .interp1,  ... : input civ field type
15%
16%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
17%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
18%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
19%     This file is part of the toolbox UVMAT.
20%
21%     UVMAT is free software; you can redistribute it and/or modify
22%     it under the terms of the GNU General Public License as published by
23%     the Free Software Foundation; either version 2 of the License, or
24%     (at your option) any later version.
25%
26%     UVMAT is distributed in the hope that it will be useful,
27%     but WITHOUT ANY WARRANTY; without even the implied warranty of
28%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
30%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
31
32%------------------------------------------------------------------------
33%------------------------------------------------------------------------
34%  I - MAIN FUNCTION series
35%------------------------------------------------------------------------
36%------------------------------------------------------------------------
37function varargout = series(varargin)
38
39% Begin initialization code - DO NOT EDIT
40gui_Singleton = 1;
41gui_State = struct('gui_Name',       mfilename, ...
42                   'gui_Singleton',  gui_Singleton, ...
43                   'gui_OpeningFcn', @series_OpeningFcn, ...
44                   'gui_OutputFcn',  @series_OutputFcn, ...
45                   'gui_LayoutFcn',  [] , ...
46                   'gui_Callback',   []);
47if nargin && ischar(varargin{1})
48    gui_State.gui_Callback = str2func(varargin{1});
49end
50
51if nargout
52    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
53else
54    gui_mainfcn(gui_State, varargin{:});
55end
56% End initialization code - DO NOT EDIT
57
58%--------------------------------------------------------------------------
59% --- Executes just before series is made visible.
60%--------------------------------------------------------------------------
61function series_OpeningFcn(hObject, eventdata, handles,param)
62global nb_builtin_ACTION nb_builtin_transform
63% Choose default command line output for series
64handles.output = hObject;
65% Update handles structure
66guidata(hObject, handles);
67%default initial parameters
68drawnow
69set(hObject,'Units','pixels')
70set(handles.PairString,'ColumnEditable',logical(0))
71set(handles.PairString,'ColumnFormat',{'char'})
72set(handles.PairString,'ColumnWidth',{60})
73set(handles.PairString,'Data',{''})
74set(hObject,'WindowButtonDownFcn',{'mouse_down'})%allows mouse action with right button (zoom for uicontrol display)
75dir_perso=prefdir;
76test_profil_perso=0;
77profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
78if exist(profil_perso,'file')
79     h=load (profil_perso);
80     if isfield(h,'MenuFile')
81          for ifile=1:min(length(h.MenuFile),5)
82              eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',h.MenuFile{ifile});'])
83          end
84     end
85     test_profil_perso=1;
86end
87
88%check default input data
89if ~exist('param','var')
90    param=[]; %default
91end
92
93%% file name and browser initialisation
94if isfield(param,'menu_coord_str')
95    set(handles.TransformName,'String',param.menu_coord_str)
96end
97if isfield(param,'menu_coord_val')
98    set(handles.TransformName,'Value',param.menu_coord_val);
99else
100     set(handles.TransformName,'Value',1);%default
101end
102if isfield(param,'FileName')
103    InputTable={'','','','',''};
104    set(handles.InputTable,'Data',InputTable)
105    if isfield(param,'FileName_1')
106        display_file_name(handles,param.FileName_1,0)
107        display_file_name(handles,param.FileName,1)
108    else
109        display_file_name(handles,param.FileName,0)
110    end
111end 
112
113%% fields input initialisation
114if isfield(param,'list_fields')&& isfield(param,'index_fields') &&~isempty(param.list_fields) &&~isempty(param.index_fields)
115    set(handles.FieldName,'String',param.list_fields);% list menu fields
116    set(handles.FieldName,'Value',param.index_fields);% selected string index
117%     FieldCell{1}=param.list_fields{param.index_fields};
118end
119
120%loads the information stored in prefdir to initiate  the list of ActionName functions
121fct_menu={'check_data_files';'aver_stat';'time_series';'merge_proj';'clean_civ_cmx'};
122transform_menu={'';'phys';'px';'phys_polar'};
123nb_builtin_ACTION=numel(fct_menu); %number of functions
124nb_transform=numel(transform_menu);
125[path_series,name,ext]=fileparts(which('series'));
126path_series=fullfile(path_series,'series');%path of the function 'series'
127addpath (path_series) ; %add the path to UVMAT, (useful in case of change of working directory after civ has been s opened in the working directory)
128path_transform=fullfile(path_series,'transform_field');%path to the field transform functions
129for ilist=1:length(fct_menu)
130    fct_path{ilist,1}=path_series;%paths of the fuctions buil-in in 'series.m'
131end
132
133%% TRANSFORM menu: loads the information stored in prefdir to initiate  the list of field transform functions
134menu_str={'';'phys';'px';'phys_polar'};
135nb_builtin_transform=numel(menu_str); %number of functions
136[path_uvmat,name,ext]=fileparts(which('uvmat'));
137addpath(fullfile(path_uvmat,'transform_field'))
138fct_handle{1,1}=[];
139testexist(1)=1;
140for ilist=2:length(menu_str)
141    if exist(menu_str{ilist},'file')
142        fct_handle{ilist,1}=str2func(menu_str{ilist});
143        testexist(ilist)=1;
144    else
145        testexist(ilist)=0;
146    end
147end
148rmpath(fullfile(path_uvmat,'transform_field'))
149
150%% read the list of functions stored in the personal file 'uvmat_perso.mat' in prefdir
151if test_profil_perso
152    if isfield(h,'series_fct') && iscell(h.series_fct)
153         for ilist=1:length(h.series_fct)
154             [path,file]=fileparts(h.series_fct{ilist});
155             fct_path=[fct_path; {path}];%concatene the list of paths
156             fct_menu=[fct_menu; {file}];
157         end
158    end
159    if isfield(h,'transform_fct') && iscell(h.transform_fct)
160        for ilist=1:length(h.transform_fct);
161             [path,file]=fileparts(h.transform_fct{ilist});
162             addpath(path)
163             if exist(file,'file')
164                h_func=str2func(file);
165                testexist=[testexist 1];
166             else
167                h_func=[];
168                testexist=[testexist 0];
169             end
170             fct_handle=[fct_handle; {h_func}];%concatene the list of paths
171             rmpath(path)
172             menu_str=[menu_str; {file}];
173        end
174    end
175end
176fct_menu=[fct_menu;{'more...'}];
177set(handles.ActionName,'String',fct_menu)
178set(handles.ActionName,'UserData',fct_path)% store the list of path in UserData of ACTION
179menu_str=menu_str(find(testexist));
180fct_handle=fct_handle(find(testexist));
181menu_str=[menu_str;{'more...'}];
182set(handles.TransformName,'String',menu_str)
183set(handles.TransformName,'UserData',fct_handle)% store the list of path in UserData of ACTION
184
185%% Adjust the GUI according to the binaries available in PARAM.xml
186path_uvmat=fileparts(which('uvmat')); %path to civ
187addpath (path_uvmat) ; %add the path to civ, (useful in case of change of working directory after civ has been s opened in the working directory)
188errormsg=[];%default error message
189xmlfile='PARAM.xml';
190if exist(xmlfile,'file')
191    try
192        t=xmltree(xmlfile);
193        sparam=convert(t);
194    catch ME
195        errormsg={' Unable to read the file PARAM.xml defining the civx binaries:';ME.message};
196    end
197else
198    errormsg=[xmlfile ' not found: path to civx binaries undefined'];
199end
200if ~isempty(errormsg)
201    msgbox_uvmat('WARNING',errormsg);
202end
203test_batch=0;%default: ,no batch mode available
204if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
205    test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
206end
207RUNVal=get(handles.RunMode,'Value');
208if test_batch==0
209   if RUNVal>2
210       set(handles.RunMode,'Value',1)
211   end
212   set(handles.RunMode,'String',{'local';'background'})
213else
214    set(handles.RunMode,'String',{'local';'background';'cluster'})
215end
216% if isfield(sparam.RunParam,'CivBin')
217%     if ~exist(sparam.RunParam.CivBin,'file')
218%         sparam.RunParam.CivBin=fullfile(path_uvmat,sparam.RunParam.CivBin);
219%     end
220% else
221%     sparam.RunParam.CivBin='';
222% end
223% display the GUI for the default actionname 'check_data_files'
224% ActionName_Callback(hObject, eventdata, handles)
225
226%------------------------------------------------------------------------
227% --- Outputs from this function are returned to the command line.
228function varargout = series_OutputFcn(hObject, eventdata, handles)
229%------------------------------------------------------------------------
230% varargout  cell array for returning output args (see VARARGOUT);
231% hObject    handle to figure
232% eventdata  reserved - to be defined in a future version of MATLAB
233% handles    structure with handles and user data (see GUIDATA)
234% Get default command line output from handles structure
235varargout{1} = handles.output;
236
237%------------------------------------------------------------------------
238%------------------------------------------------------------------------
239%  II - FUNCTIONS FOR INTRODUCING THE INPUT FILES
240% automatically sets the global properties when the rootfile name is introduced
241% then activate the view-field actionname if selected
242% it is activated either by clicking on the RootPath window or by the
243% browser
244%------------------------------------------------------------------------
245%------------------------------------------------------------------------
246function MenuBrowse_Callback(hObject, eventdata, handles)
247%------------------------------------------------------------------------   
248InputTable=get(handles.InputTable,'Data');
249if isempty(InputTable)
250    RootPathCell={};
251else
252    RootPathCell=InputTable(:,1);
253end
254oldfile=''; %default
255if isempty(RootPathCell)||isequal(RootPathCell,{''})%loads the previously stored file name and set it as default in the file_input box
256     dir_perso=prefdir;
257     profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
258     if exist(profil_perso,'file')
259          h=load (profil_perso);
260         if isfield(h,'filebase')&&ischar(h.filebase)
261                 oldfile=h.filebase;
262         end
263         if isfield(h,'RootPath')&&ischar(h.RootPath)
264                 oldfile=h.RootPath;
265         end
266     end
267else
268     SubDirCell=InputTable(:,2);
269    RootFileCell=InputTable(:,3);
270     oldfile=fullfile(RootPathCell{1},SubDirCell{1},RootFileCell{1});
271 end
272[FileName, PathName, filterindex] = uigetfile( ...
273       {'*.xml;*.xls;*.png;*.tif;*.avi;*.AVI;*.nc', ' (*.xml,*.xls, *.png,*.tif, *.avi,*.nc)';
274       '*.xml',  '.xml files '; ...
275        '*.xls',  '.xls files '; ...
276        '*.png','.png image files'; ...
277        '*.tif','.tif image files'; ...
278        '*.avi;*.AVI','.avi movie files'; ...
279        '*.nc','.netcdf files'; ...
280        '*.*',  'All Files (*.*)'}, ...
281        'Pick a file',oldfile);
282fileinput=[PathName FileName];%complete file name
283if isempty(fileinput),return;end %abandon if no file is introduced by the browser
284[path,name,ext]=fileparts(fileinput);
285if isequal(ext,'.xml')
286    [Param,Heading]=xml2struct(fileinput);
287    if ~strcmp(Heading,'Series')
288        msg_box_uvmat('ERROR','xml file heading is not <Series>')
289    else
290        fill_GUI(Param,handles);%fill the GUI with the parameters retrieved from the xml file
291        if isfield(Param,'CheckObject')&& Param.CheckObject
292            set_object(Param.ProjObject)
293        end
294        set(handles.REFRESH,'UserData',[1:size(Param.InputTable,1)])
295        REFRESH_Callback([],[], handles)
296        return
297    end
298elseif isequal(ext,'.xls')
299    msg_box_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
300else
301    display_file_name(handles,fileinput,0)
302end
303
304% --------------------------------------------------------------------
305function MenuFile_1_Callback(hObject, eventdata, handles)
306fileinput=get(handles.MenuFile_1,'Label');
307display_file_name(handles,fileinput,0)
308
309% --------------------------------------------------------------------
310function MenuFile_2_Callback(hObject, eventdata, handles)
311fileinput=get(handles.MenuFile_2,'Label');
312display_file_name(handles,fileinput,0)
313
314% --------------------------------------------------------------------
315function MenuFile_3_Callback(hObject, eventdata, handles)
316fileinput=get(handles.MenuFile_3,'Label');
317display_file_name( handles,fileinput,0)
318
319% --------------------------------------------------------------------
320function MenuFile_4_Callback(hObject, eventdata, handles)
321fileinput=get(handles.MenuFile_4,'Label');
322display_file_name(handles,fileinput,0)
323
324% --------------------------------------------------------------------
325function MenuFile_5_Callback(hObject, eventdata, handles)
326fileinput=get(handles.MenuFile_5,'Label');
327display_file_name(handles,fileinput,0)
328
329% --------------------------------------------------------------------
330function MenuBrowse_insert_Callback(hObject, eventdata, handles)
331InputTable=get(handles.InputTable,'Data');
332RootPathCell=InputTable(:,1);
333SubDirCell=InputTable(:,3);
334RootFileCell=InputTable(:,2);
335oldfile=''; %default
336if isempty(RootPathCell)||isequal(RootPathCell,{''})%loads the previously stored file name and set it as default in the file_input box
337     dir_perso=prefdir;
338     profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
339     if exist(profil_perso,'file')
340          h=load (profil_perso);
341         if isfield(h,'filebase')&ischar(h.filebase)
342                 oldfile=h.filebase;
343         end
344         if isfield(h,'RootPath')&ischar(h.RootPath)
345                 oldfile=h.RootPath;
346         end
347     end
348 else
349     oldfile=fullfile(RootPathCell{1},RootFileCell{1});
350 end
351[FileName, PathName, filterindex] = uigetfile( ...
352       {'*.xml;*.xls;*.png;*.avi;*.AVI;*.nc', ' (*.xml,*.xls, *.png, *.avi,*.nc)';
353       '*.xml',  '.xml files '; ...
354        '*.xls',  '.xls files '; ...
355        '*.png','.png image files'; ...
356        '*.avi;*.AVI','.avi movie files'; ...
357        '*.nc','.netcdf files'; ...
358        '*.*',  'All Files (*.*)'}, ...
359        'Pick a file',oldfile);
360fileinput=[PathName FileName];%complete file name
361sizf=size(fileinput);
362if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
363[path,name,ext]=fileparts(fileinput);
364if isequal(ext,'.xml')
365    msgbox_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
366elseif isequal(ext,'.xls')
367    msgbox_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
368else
369    display_file_name(handles,fileinput,'append')
370end
371
372% --------------------------------------------------------------------
373function MenuFile_insert_1_Callback(hObject, eventdata, handles)
374% --------------------------------------------------------------------   
375fileinput=get(handles.MenuFile_insert_1,'Label');
376display_file_name(handles,fileinput,'append')
377
378% --------------------------------------------------------------------
379function MenuFile_insert_2_Callback(hObject, eventdata, handles)
380% --------------------------------------------------------------------   
381fileinput=get(handles.MenuFile_insert_2,'Label');
382display_file_name(handles,fileinput,'append')
383
384% --------------------------------------------------------------------
385function MenuFile_insert_3_Callback(hObject, eventdata, handles)
386% --------------------------------------------------------------------   
387fileinput=get(handles.MenuFile_insert_3,'Label');
388display_file_name( handles,fileinput,'append')
389
390% --------------------------------------------------------------------
391function MenuFile_insert_4_Callback(hObject, eventdata, handles)
392% --------------------------------------------------------------------   
393fileinput=get(handles.MenuFile_insert_4,'Label');
394display_file_name( handles,fileinput,'append')
395
396% --------------------------------------------------------------------
397function MenuFile_insert_5_Callback(hObject, eventdata, handles)
398% --------------------------------------------------------------------   
399fileinput=get(handles.MenuFile_insert_5,'Label');
400display_file_name(handles,fileinput,'append')
401
402%------------------------------------------------------------------------
403% --- Executes when entered data in editable cell(s) in InputTable.
404function InputTable_CellEditCallback(hObject, eventdata, handles)
405%------------------------------------------------------------------------
406set(handles.REFRESH,'Visible','on')
407iview=eventdata.Indices(1);
408view_set=get(handles.REFRESH,'UserData');
409if isempty(find(view_set==iview))
410    set(handles.REFRESH,'UserData',[view_set iview])
411end
412%% enable other menus and uicontrols
413set(handles.MenuOpen_insert,'Enable','on')
414set(handles.MenuFile_insert_1,'Enable','on')
415set(handles.MenuFile_insert_2,'Enable','on')
416set(handles.MenuFile_insert_3,'Enable','on')
417set(handles.MenuFile_insert_4,'Enable','on')
418set(handles.MenuFile_insert_5,'Enable','on')
419set(handles.RUN, 'Enable','On')
420set(handles.RUN,'BackgroundColor',[1 0 0])% set RUN button to red
421
422%update the output dir
423% SubDir=sort(InputTable(:,2)); %set of subdirectories sorted in alphabetical order
424% SubDirOut=SubDir{1};
425% if numel(SubDir)>1
426%     for ilist=2:numel(SubDir)
427%         SubDirOut=[SubDirOut '-' SubDir{ilist}];
428%     end
429% end
430% set(handles.OutputSubDir,'String',SubDirOut)
431
432%------------------------------------------------------------------------
433% --- Executes on button press in REFRESH.
434function REFRESH_Callback(hObject, eventdata, handles)
435%------------------------------------------------------------------------
436InputTable=get(handles.InputTable,'Data');
437view_set=get(handles.REFRESH,'UserData');
438set(handles.REFRESH,'BackgroundColor',[0.7 0.7 0.7])% set REFRESH  button to grey color
439drawnow
440for iview=view_set
441    RootPath=fullfile(InputTable{iview,1},InputTable{iview,2});
442    if ~exist(RootPath,'dir')
443        i1_series=[];
444        RootPath=fileparts(RootPath); %will try the upped forldr
445    else
446        [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,tild,FileType,MovieObject]=...
447            find_file_series(fullfile(InputTable{iview,1},InputTable{iview,2}),[InputTable{iview,3} InputTable{iview,4} InputTable{iview,5}]);
448    end
449    if isempty(i1_series)
450        [FileName, PathName, filterindex] = uigetfile( ...
451            {'*.xml;*.xls;*.png;*.tif;*.avi;*.AVI;*.nc', ' (*.xml,*.xls, *.png,*.tif, *.avi,*.nc)';
452            '*.xml',  '.xml files '; ...
453            '*.xls',  '.xls files '; ...
454            '*.png','.png image files'; ...
455            '*.tif','.tif image files'; ...
456            '*.avi;*.AVI','.avi movie files'; ...
457            '*.nc','.netcdf files'; ...
458            '*.*',  'All Files (*.*)'}, ...
459            ['unvalid entry at line ' num2str(iview) ', pick a file'],RootPath);
460        fileinput=[PathName FileName];%complete file name
461        if isempty(fileinput),return;end %abandon if the operation has been cancelled: no input from browser
462        [path,name,ext]=fileparts(fileinput);
463        display_file_name(handles,fileinput,iview)
464    else
465        update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,iview)
466    end
467end
468set(handles.REFRESH,'BackgroundColor',[1 0 0])% set REFRESH  button to grey color
469set(handles.REFRESH,'Visible','off')
470set(handles.REFRESH,'UserData',[])
471
472%------------------------------------------------------------------------
473% --- Function called when a new file is opened, either by series_OpeningFcn or by the browser
474function display_file_name(handles,fileinput,iview)
475%------------------------------------------------------------------------ 
476%
477% INPUT:
478% handles: handles of elements in the GUI
479% fielinput: input file name, including path
480% append =0 (refresh the Input table with the new file), ='append' append a new line in the table
481
482%% get the input root name, indices, file extension and nomenclature NomType
483if ~exist(fileinput,'file')
484    msgbox_uvmat('ERROR',['input file ' fileinput  ' does not exist'])
485    return
486end
487
488%% enable other menus and uicontrols
489set(handles.MenuOpen_insert,'Enable','on')
490set(handles.MenuFile_insert_1,'Enable','on')
491set(handles.MenuFile_insert_2,'Enable','on')
492set(handles.MenuFile_insert_3,'Enable','on')
493set(handles.MenuFile_insert_4,'Enable','on')
494set(handles.MenuFile_insert_5,'Enable','on')
495set(handles.RUN, 'Enable','On')
496set(handles.RUN,'BackgroundColor',[1 0 0])% set RUN button to red
497set(handles.InputTable,'BackgroundColor',[1 1 0]) % set RootPath edit box  to yellow
498drawnow
499
500%% detect root name, nomenclature and indices in the input file name:
501[FilePath,FileName,FileExt]=fileparts(fileinput);
502% detect the file type, get the movie object if relevant, and look for the corresponding file series:
503% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
504[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,MovieObject,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
505if isempty(RootFile)&&isempty(i1_series)
506    errormsg='no input file in the series';
507    return
508end
509
510%% fill the list of file series
511InputTable=get(handles.InputTable,'Data');
512if strcmp(iview,'append') % display the input data as a new line in the table
513     iview=size(InputTable,1);
514     InputTable(iview+1,:)={'','','','',''};
515     InputTable(iview,:)=[{RootPath},{SubDir},{RootFile},{NomType},{FileExt}];
516elseif iview==0 % or re-initialise the list of  input  file series
517    iview=1;
518    InputTable=[{'','','','',''};{'','','','',''}];
519     InputTable(iview,:)=[{RootPath},{SubDir},{RootFile},{NomType},{FileExt}];
520    set(handles.TimeTable,'Data',[{[]},{[]},{[]},{[]}])
521    set(handles.MinIndex,'Data',[{[]},{[]}])
522    set(handles.MaxIndex,'Data',[{[]},{[]}])
523    set(handles.ListView,'Value',1)
524    set(handles.ListView,'String',{'1'})
525end
526nbview=size(InputTable,1);
527set(handles.ListView,'String',mat2cell((1:nbview)',ones(nbview,1)))
528set(handles.ListView,'Value',iview)
529set(handles.InputTable,'Data',InputTable)
530
531%% determine the selected reference field indices for pair display
532ref_i=1; %default ref_i is a reference frame index used to find existing pairs from PIV
533if ~isempty(i1)
534    ref_i=i1;
535    if ~isempty(i2)
536        ref_i=floor((ref_i+i2)/2);% reference image number corresponding to the file
537    end
538end
539set(handles.num_ref_i,'String',num2str(ref_i));
540ref_j=1; %default  ref_j is a reference frame index used to find existing pairs from PIV
541if ~isempty(j1)
542    ref_j=j1;
543    if ~isempty(j2)
544        ref_j=floor((j1+j2)/2);
545    end         
546end
547set(handles.num_ref_j,'String',num2str(ref_j));
548
549%% update the list of recent files in the menubar and save it for future opening
550MenuFile=[{get(handles.MenuFile_1,'Label')};{get(handles.MenuFile_2,'Label')};...
551    {get(handles.MenuFile_3,'Label')};{get(handles.MenuFile_4,'Label')};{get(handles.MenuFile_5,'Label')}];
552str_find=strcmp(fileinput,MenuFile);
553if isempty(find(str_find,1))
554    MenuFile=[{fileinput};MenuFile];%insert the current file if not already in the list
555end
556for ifile=1:min(length(MenuFile),5)
557    eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',MenuFile{ifile});'])
558    eval(['set(handles.MenuFile_insert_' num2str(ifile) ',''Label'',MenuFile{ifile});'])
559end
560dir_perso=prefdir;
561profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
562if exist(profil_perso,'file')
563    save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
564else
565    save (profil_perso,'MenuFile','-V6'); %store the file names for future opening of uvmat
566end
567
568set(handles.InputTable,'BackgroundColor',[1 1 1])
569
570%% initiate input file series and refresh the current field view:     
571update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,iview);
572
573%------------------------------------------------------------------------
574% --- Update information about a new field series (indices to scan, timing,
575%     calibration from an xml file
576function update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,VideoObject,iview)
577%------------------------------------------------------------------------
578%% update the output dir
579InputTable=get(handles.InputTable,'Data');
580SubDir=sort(InputTable(1:end-1,2)); %set of subdirectories sorted in alphabetical order
581SubDirOut=SubDir{1};
582if numel(SubDir)>1
583    for ilist=2:numel(SubDir)
584        SubDirOut=[SubDirOut '-' SubDir{ilist}];
585    end
586end
587set(handles.OutputSubDir,'String',SubDirOut)
588
589%% display the min and max indices for the file series
590pair_max=squeeze(max(i1_series,[],1)); %max on pair index
591j_max=max(pair_max,[],1);
592%i_sum=sum(sum(i1_series,2),1);%sum of i1_series on the last index
593MaxIndex_i=max(find(j_max))-1;% max ref index i
594MinIndex_i=min(find(j_max))-1;% min ref index i
595i_max=max(pair_max,[],2);
596MaxIndex_j=max(find(i_max))-1;% max ref index i
597MinIndex_j=min(find(i_max))-1;% min ref index i
598% i2_min=[];
599% if ~isempty(i2_series)
600%     i2_min=i2_series(1,2,2);
601% end
602% j1_min=[];
603% if ~isempty(j1_series)
604%     j1_min=j1_series(1,2,2);
605% end
606% j2_min=[];
607% if ~isempty(j2_series)
608%     j2_min=j2_series(1,2,2);
609% end
610% if isequal(MinIndex_i,1) &&...
611%         exist (fullfile_uvmat(InputTable{iview,1},InputTable{iview,2},InputTable{iview,3},InputTable{iview,5},InputTable{iview,4},0,i2_min, j1_min,j2_min),'file')
612%     MinIndex_i=0;
613% end
614% j_sum=sum(sum(j1_series,1),1);
615% MaxIndex_j=max(find(j_sum>0))-1;
616% MinIndex_j=min(find(j_sum>0))-1;
617MinIndex=get(handles.MinIndex,'Data');%retrieve the min indices in the table MinIndex
618MaxIndex=get(handles.MaxIndex,'Data');%retrieve the max indices in the table MaxIndex
619MinIndex{iview,1}=MinIndex_i;
620MinIndex{iview,2}=MinIndex_j;
621MaxIndex{iview,1}=MaxIndex_i;
622MaxIndex{iview,2}=MaxIndex_j;
623
624set(handles.MinIndex,'Data',MinIndex)%display the min indices in the table MinIndex
625set(handles.MaxIndex,'Data',MaxIndex)%display the max indices in the table MaxIndex
626
627%% adjust the first and last indices if requested by the bounds
628first_i=str2num(get(handles.num_first_i,'String'));
629ref_i=str2num(get(handles.num_ref_i,'String'));
630ref_j=str2num(get(handles.num_ref_j,'String'));
631if isempty(first_i)
632    first_i=ref_i;
633elseif first_i < MinIndex_i
634    first_i=MinIndex_i;
635end
636first_j=str2num(get(handles.num_first_j,'String'));
637if isempty(first_j)
638    first_j=ref_j;
639elseif first_j<MinIndex_j
640    first_j=MinIndex_j;
641end
642last_i=str2num(get(handles.num_last_i,'String'));
643if isempty(last_i)
644    last_i=ref_i;
645elseif last_i > MaxIndex_i
646    last_i=MaxIndex_i;
647end
648last_j=str2num(get(handles.num_first_j,'String'));
649if isempty(last_j)
650    last_j=ref_j;
651elseif last_j>MaxIndex_j
652    last_j=MaxIndex_j;
653end
654set(handles.num_first_i,'String',num2str(first_i));
655set(handles.num_first_j,'String',num2str(first_j));
656set(handles.num_last_i,'String',num2str(last_i));
657set(handles.num_last_j,'String',num2str(last_j));
658
659%% read timing and total frame number from the current file (movie files) !! may be overrid by xml file
660InputTable=get(handles.InputTable,'Data');
661FileBase=fullfile(InputTable{iview,1},InputTable{iview,3});
662time=[];%default
663% case of movies
664if strcmp(InputTable{iview,4},'*')
665    if ~isempty(VideoObject)
666        imainfo=get(VideoObject);
667        time=(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames-1)/imainfo.FrameRate)';
668        set(handles.Dt_txt,'String',['Dt=' num2str(1000/imainfo.FrameRate) 'ms']);%display the elementary time interval in millisec
669        ColorType='truecolor';
670    elseif ~isempty(imformats(regexprep(InputTable{iview,5},'^.',''))) || isequal(InputTable{iview,5},'.vol')%&& isequal(NomType,'*')% multi-frame image
671        if ~isempty(InputTable{iview,2})
672            imainfo=imfinfo(fullfile(InputTable{iview,1},InputTable{iview,2},[InputTable{iview,3} InputTable{iview,5}]));
673        else
674            imainfo=imfinfo([FileBase InputTable{iview,5}]);
675        end
676        ColorType=imainfo.ColorType;%='truecolor' for color images
677        if length(imainfo) >1 %case of image with multiple frames
678            nbfield=length(imainfo);
679            nbfield_j=1;
680        end
681    end
682end
683
684%%  read image documentation file  if found%%%%%%%%%%%%%%%%%%%%%%%%%%%
685ext_imadoc='';
686if exist([FileBase '.xml'],'file')
687    ext_imadoc='.xml';
688elseif exist([FileBase '.civ'],'file')
689    ext_imadoc='.civ';
690end
691%read the ImaDoc file
692XmlData=[];
693NbSlice_calib={};
694if isequal(ext_imadoc,'.xml')
695        [XmlData,warntext]=imadoc2struct([FileBase '.xml']);
696        if isfield(XmlData,'Heading') && isfield(XmlData.Heading,'ImageName') && ischar(XmlData.Heading.ImageName)
697            [PP,FF,ext_ima_read]=fileparts(XmlData.Heading.ImageName);
698        end
699        if isfield(XmlData,'Time')
700            time=XmlData.Time;
701        end
702        if isfield(XmlData,'Camera')
703            if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
704                NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
705                if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
706                    msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
707                end
708            end
709            if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
710                TimeUnit=XmlData.Camera.TimeUnit;
711            end
712        end
713        if ~isempty(warntext)
714            msgbox_uvmat('WARNING',warntext)
715        end 
716elseif isequal(ext_imadoc,'.civ')
717    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
718    time=XmlData.Time;
719    if error==2, warntext=['no file ' FileBase '.civ'];
720    elseif error==1, warntext='inconsistent number of fields in the .civ file';
721    end 
722end
723
724%% update time table
725if ~isempty(time)
726TimeTable=get(handles.TimeTable,'Data');
727first_i=str2num(get(handles.num_first_i,'String'));
728last_i=str2num(get(handles.num_last_i,'String'));
729first_j=str2num(get(handles.num_first_j,'String'));
730last_j=str2num(get(handles.num_last_j,'String'));
731MinIndexTable=get(handles.MinIndex,'Data');
732MinIndex_i=MinIndexTable{iview,1};
733MinIndex_j=MinIndexTable{iview,2};
734MaxIndexTable=get(handles.MaxIndex,'Data');
735MaxIndex_i=MaxIndexTable{iview,1};
736MaxIndex_j=MaxIndexTable{iview,2};
737if isempty(MinIndex_j)
738    if MinIndex_i>0
739    TimeTable{iview,1}=time(MinIndex_i);
740    end
741    TimeTable{iview,2}=time(first_i);
742    TimeTable{iview,3}=time(last_i);
743    TimeTable{iview,4}=time(MaxIndex_i);
744elseif ~isempty(time)
745    if MinIndex_i>0
746    TimeTable{iview,1}=time(MinIndex_i,MinIndex_j);
747    end
748    TimeTable{iview,2}=time(first_i,first_j);
749    TimeTable{iview,3}=time(last_i,last_j);
750    TimeTable{iview,4}=time(MaxIndex_i,MaxIndex_j);
751end
752set(handles.TimeTable,'Data',TimeTable)
753end
754
755%% number of slices
756NbSlice=1;%default
757if isfield(XmlData,'GeometryCalib') && isfield(XmlData.GeometryCalib,'SliceCoord')
758    siz=size(XmlData.GeometryCalib.SliceCoord);
759    if siz(1)>1
760        NbSlice=siz(1);
761    end
762end
763set(handles.num_NbSlice,'String',num2str(NbSlice))
764   
765%% update pair menus
766set(handles.Pairs,'Visible','on')
767set(handles.PairString,'Visible','on')
768ListView=get(handles.ListView,'String');
769ListView{iview}=num2str(iview);
770set(handles.ListView,'String',ListView);
771set(handles.ListView,'Value',iview)
772update_mode(handles,i1_series,i2_series,j1_series,j2_series,time)
773
774%% update the series info in 'UserData'
775SeriesData=get(handles.series,'UserData');
776SeriesData.i1_series{iview}=i1_series;
777SeriesData.i2_series{iview}=i2_series;
778SeriesData.j1_series{iview}=j1_series;
779SeriesData.j2_series{iview}=j2_series;
780SeriesData.FileType{iview}=FileType;
781SeriesData.Time{iview}=time;
782set(handles.series,'UserData',SeriesData)
783
784%% enable j index visibilitycellfun(@isempty,regexp(PairString,'^j'))
785state='off';
786check_jindex=~cellfun(@isempty,SeriesData.j1_series); %look for non empty j indices
787if isempty(find(check_jindex))
788    enable_j(handles,'off') % no j index needed
789else
790    PairString=get(handles.PairString,'Data');
791    if isempty(find(cellfun(@isempty,regexp(PairString,'^j'))))% if all pair string begins by j (burst)
792        enable_j(handles,'off') % no j index needed
793    else
794        enable_j(handles,'on')
795    end
796end
797
798%% display the set of existing files as an image
799set(handles.FileStatus,'Units','pixels')
800Position=get(handles.FileStatus,'Position');
801set(handles.FileStatus,'Units','normalized')
802xI=0.5:Position(3)-0.5;
803nbview=numel(SeriesData.i1_series);
804for iview=1:nbview
805    index_min(iview)=min(find(SeriesData.i1_series{iview}(1,2:end,2:end)>0));
806    index_max(iview)=max(find(SeriesData.i1_series{iview}(1,2:end,2:end)>0));
807end
808index_min=min(index_min);
809index_max=max(index_max);
810range_index=index_max-index_min+1;
811scale_y=Position(4)/nbview;
812scale_x=Position(3)/range_index;
813x=(0.5:range_index-0.5)*Position(3)/range_index;
814% y=(0.5:nbview-0.5)*Position(4)/nbview;
815range_y=max(1,floor(Position(4)/nbview));
816CData=zeros(nbview*range_y,Position(3));
817for iview=1:nbview
818    ind_y=1+(iview-1)*range_y:iview*range_y;
819    LineData=zeros(1,range_index);
820    x_index=find(SeriesData.i1_series{iview}(1,2:end,2:end)>0)-index_min+1;
821    LineData(x_index)=1;
822    LineData=interp1(x,LineData,xI,'nearest');
823    CData(ind_y,:)=ones(size(ind_y'))*LineData;
824end
825CData=cat(3,zeros(size(CData)),CData,zeros(size(CData)));
826set(handles.FileStatus,'CData',CData);
827
828
829%% enable field and veltype menus, in accordance with the current action
830ActionName_Callback([],[], handles)
831
832%% check for pair display
833check_pairs=0;
834for iview=1:numel(SeriesData.i2_series)
835    if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
836        check_pairs=1;
837    end
838end
839if check_pairs
840    set(handles.Pairs,'Visible','on')
841    set(handles.PairString,'Visible','on')
842else
843    set(handles.Pairs,'Visible','off')
844    set(handles.PairString,'Visible','off')
845end
846
847%% set length of waitbar
848displ_time(handles)
849
850
851% %% set default options in menu 'Fields'%% TODO: check VelType
852% if ~testima
853%     testcivx=0;
854%     if isfield(UvData,'FieldsString') && isequal(UvData.FieldsString,{'get_field...'})% field menu defined as input (from get_field)
855%         set(handles_Fields,'Value',1)
856%         set(handles_Fields,'String',{'get_field...'})
857%         UvData=rmfield(UvData,'FieldsString');
858%     else
859%         Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ');
860%         if strcmp(Data.Conventions,'uvmat/civdata') ||( ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0))%if the new input is Civx
861%             FieldList=calc_field;
862%             set(handles_Fields,'String',[{'image'};FieldList;{'get_field...'}]);%standard menu for civx data
863%             set(handles_Fields,'Value',2) % set menu to 'velocity'
864%             col_vec=FieldList;
865%             col_vec(1)=[];%remove 'velocity' option for vector color (must be a scalar)
866%             testcivx=1;
867%         end
868%         if ~testcivx
869%             set(handles_Fields,'Value',1) % set menu to 'get_field...
870%             set(handles_Fields,'String',{'get_field...'})
871%             col_vec={'get_field...'};
872%         end
873%         set(handles.ColorScalar,'String',col_vec)
874%     end
875% end
876% set(handles.uvmat,'UserData',UvData)
877%
878% %% set index navigation options and refresh plots
879% scan_option='i';%default
880% state_j='off'; %default
881% if index==2
882%     if get(handles.scan_j,'Value')
883%         scan_option='j'; %keep the scan option for the second fiel series
884%     end
885%     if strcmp(get(handles.j1,'Visible'),'on')
886%         state_j='on';
887%     end
888% end
889% if ~isempty(j1_series)
890%         state_j='on';
891%         if isequal(nbfield,1) &&index==1
892%             scan_option='j'; %scan j index by default if nbfield=1               
893%         end
894% end
895% if isequal(scan_option,'i')
896%      set(handles.scan_i,'Value',1)
897%      scan_i_Callback([],[], handles);
898% else
899%      set(handles.scan_j,'Value',1)
900%      scan_j_Callback([],[], handles);
901% end
902% set(handles.scan_j,'Visible',state_j)
903% set(handles.j1,'Visible',state_j)
904% set(handles.j2,'Visible',state_j)
905% set(handles.last_j,'Visible',state_j);
906% set(handles.frame_j,'Visible',state_j);
907% set(handles.j_text,'Visible',state_j);
908% if ~isempty(i2_series)||~isempty(j2_series)
909%     set(handles.CheckFixPair,'Visible','on')
910% elseif index==1
911%     set(handles.CheckFixPair,'Visible','off')
912% end
913%
914%
915% mode_Callback(hObject, eventdata, handles)
916%
917% set(handles.REFRESH,'BackgroundColor',[0.7 0.7 0.7])
918% InputTable=get(handles.InputTable,'Data');
919% check_lines=get(handles.REFRESH,'UserData');
920%
921% %% check the indices and FileTypes for each series (limited to the new ones to save time)
922% for ind_list=1:length(check_lines)
923%     if  check_lines(ind_list)
924%         InputLine=InputTable(ind_list,:);
925%         detect_idem=strcmp('"',InputLine);% look for '" (repeat of previous data)
926%         detect_idem=detect_idem(detect_idem>0);
927%         if ~isempty (detect_idem)
928%             InputLine(detect_idem)=InputTable(ind_list-1,detect_idem);
929%             set(handles.InputTable,'Data',InputTable)
930%         end
931%         fileinput=fullfile_uvmat(InputLine{1},InputLine{2},InputLine{3},InputLine{5},InputLine{4},1,2,1,2);
932%         %fileinput=name_generator(fullfile(InputLine{1},InputLine{3}),1,1,InputLine{5},InputLine{4},1,2,2,InputLine{2})
933%         %update file series defined by the selected line
934%         [InputTable{ind_list,3},InputTable{(ind_list),4},errormsg]=update_indices(handles,fileinput,ind_list);
935%         if ~isempty(errormsg)
936%                 msgbox_uvmat('ERROR',errormsg)
937%                 return
938%         end
939%     end
940% end
941% set(handles.InputTable,'Data',InputTable)
942% SeriesData=get(handles.series,'UserData');
943%
944% state_j='off';
945% state_Pairs='off';
946% state_InputFields='off';
947% val=get(handles.ListView,'Value');
948% ListViewString={''};
949% if ~isempty(SeriesData)
950% %     ListViewString={};
951%     for iview=1:size(InputTable,1)
952%         if ~isempty(SeriesData.j1_series{iview})
953%             state_j='on';
954%         end
955%         if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
956%             state_Pairs='on';
957%             ListViewString{iview}=num2str(iview);
958%             if check_lines(iview)
959%                 val=iview;%select the last pair if it is a new entry
960%             end
961%         end
962%         if strcmp(SeriesData.FileType{iview},'civx')||strcmp(SeriesData.FileType{iview},'civdata')
963%             state_InputFields='on';
964%         end
965%     end
966% end
967% set(handles.ListView,'Value',val)
968% set(handles.ListView,'String',ListViewString)
969% if strcmp(state_Pairs,'on')
970%     ListView_Callback(hObject,eventdata,handles)
971% end
972% set(handles.PairString,'Visible',state_Pairs)
973% enable_j(handles,state_j)
974
975%------------------------------------------------------------------------
976function num_first_i_Callback(hObject, eventdata, handles)
977%------------------------------------------------------------------------
978num_last_i_Callback(hObject, eventdata, handles)
979
980%------------------------------------------------------------------------
981function num_last_i_Callback(hObject, eventdata, handles)
982%------------------------------------------------------------------------
983SeriesData=get(handles.series,'UserData');
984if ~isfield(SeriesData,'Time')
985    SeriesData.Time{1}=[];
986end
987displ_time(handles);
988
989%------------------------------------------------------------------------
990function num_first_j_Callback(hObject, eventdata, handles)
991%------------------------------------------------------------------------
992 num_last_j_Callback(hObject, eventdata, handles)
993
994%------------------------------------------------------------------------
995function num_last_j_Callback(hObject, eventdata, handles)
996%------------------------------------------------------------------------
997first_j=str2num(get(handles.num_first_j,'String'));
998last_j=str2num(get(handles.num_last_j,'String'));
999ref_j=ceil((first_j+last_j)/2);
1000set(handles.num_ref_j,'String', num2str(ref_j))
1001num_ref_j_Callback(hObject, eventdata, handles)
1002SeriesData=get(handles.series,'UserData');
1003if ~isfield(SeriesData,'Time')
1004    SeriesData.Time{1}=[];
1005end
1006displ_time(handles);
1007
1008
1009%------------------------------------------------------------------------
1010% ---- find the times corresponding to the first and last indices of a series
1011function displ_time(handles)
1012%------------------------------------------------------------------------
1013SeriesData=get(handles.series,'UserData');%
1014ref_i=[str2num(get(handles.num_first_i,'String')) str2num(get(handles.num_last_i,'String'))];
1015ref_j=[str2num(get(handles.num_first_j,'String')) str2num(get(handles.num_last_j,'String'))];
1016TimeTable=get(handles.TimeTable,'Data');
1017Pairs=get(handles.PairString,'Data');
1018for iview=1:size(TimeTable,1)
1019    if size(SeriesData.Time,1)<iview
1020        break
1021    end
1022    i1=ref_i;
1023    j1=ref_j;
1024    i2=ref_i;
1025    j2=ref_j;
1026    % case of pairs
1027    if ~isempty(Pairs{iview,1})
1028        r=regexp(Pairs{iview,1},'(?<mode>(Di=)|(Dj=)) -*(?<num1>\d+)\|(?<num2>\d+)','names');
1029        if isempty(r)
1030            r=regexp(Pairs{iview,1},'(?<num1>\d+)(?<mode>-)(?<num2>\d+)','names');
1031        end
1032        switch r.mode
1033            case 'Di='  %  case 'series(Di)')
1034                i1=ref_i-str2num(r.num1);
1035                i2=ref_i+str2num(r.num2);
1036            case 'Dj='  %  case 'series(Dj)'
1037                j1=ref_j-str2num(r.num1);
1038                j2=ref_j+str2num(r.num2);
1039            case '-'  % case 'bursts'
1040                j1=str2num(r.num1)*ones(size(ref_i));
1041                j2=str2num(r.num2)*ones(size(ref_i));
1042        end
1043    end
1044    TimeTable{iview,2}=[];
1045    TimeTable{iview,3}=[];
1046    if size(SeriesData.Time{iview},1)>=i2(2)&&size(SeriesData.Time{iview},1)>=j2(2)
1047        if isempty(ref_j)
1048            time_first=(SeriesData.Time{iview}(i1(1))+SeriesData.Time{iview}(i2(1)))/2;
1049            time_last=(SeriesData.Time{iview}(i1(2))+SeriesData.Time{iview}(i2(2)))/2;
1050        else
1051            time_first=(SeriesData.Time{iview}(i1(1),j1(1))+SeriesData.Time{iview}(i2(1),j2(1)))/2;
1052            time_last=(SeriesData.Time{iview}(i1(2),j1(2))+SeriesData.Time{iview}(i2(2),j2(2)))/2;
1053        end
1054        TimeTable{iview,2}=time_first; %TODO: take into account pairs
1055        TimeTable{iview,3}=time_last; %TODO: take into account pairs
1056    end
1057end
1058set(handles.TimeTable,'Data',TimeTable)
1059
1060%% set the waitbar position with respect to the min and max in the series
1061% for iview=1:numel(SeriesData.i1_series)
1062% [tild,index_min(iview)]=min(SeriesData.i1_series{iview}(SeriesData.i1_series{iview}>0));
1063% [tild,index_max(iview)]=max(SeriesData.i1_series{iview}(SeriesData.i1_series{iview}>0));
1064% end
1065for iview=1:numel(SeriesData.i1_series)
1066    index_min(iview)=min(find(SeriesData.i1_series{iview}(1,2:end,2:end)>0));
1067    index_max(iview)=max(find(SeriesData.i1_series{iview}(1,2:end,2:end)>0));
1068end
1069[index_min,iview_min]=min(index_min);
1070[index_max,iview_max]=min(index_max);
1071index_first=(ref_i(1)-1)*(size(SeriesData.i1_series{iview_min},2)-1)+ref_j(1);
1072index_last=(ref_i(2)-1)*(size(SeriesData.i1_series{iview_max},2)-1)+ref_j(2);
1073range=index_max-index_min+1;
1074coeff_min=(index_first-index_min)/range;
1075coeff_max=(index_last-index_min+1)/range;
1076Position=get(handles.Waitbar,'Position');
1077Position_status=get(handles.FileStatus,'Position');
1078Position(1)=coeff_min*Position_status(3)+Position_status(1);
1079Position(3)=Position_status(3)*(coeff_max-coeff_min);
1080set(handles.Waitbar,'Position',Position)
1081update_waitbar(handles.Waitbar,0)
1082
1083%------------------------------------------------------------------------
1084% --- Executes when selected cell(s) is changed in PairString.
1085function PairString_CellSelectionCallback(hObject, eventdata, handles)
1086%------------------------------------------------------------------------   
1087set(handles.ListView,'Value',eventdata.Indices(1))% detect the selected raw index
1088ListView_Callback ([],[],handles) % update the list of available pairs
1089
1090%------------------------------------------------------------------------
1091%------------------------------------------------------------------------
1092%  III - FUNCTIONS ASSOCIATED TO THE FRAME SET PAIRS
1093%------------------------------------------------------------------------
1094%------------------------------------------------------------------------
1095% --- Executes on selection change in ListView.
1096function ListView_Callback(hObject, eventdata, handles)
1097%------------------------------------------------------------------------   
1098SeriesData=get(handles.series,'UserData');
1099i2_series=[];
1100j2_series=[];
1101iview=get(handles.ListView,'Value');
1102if ~isempty(SeriesData.i2_series{iview})
1103    i2_series=SeriesData.i2_series{iview};
1104end
1105if ~isempty(SeriesData.j2_series{iview})
1106    j2_series=SeriesData.j2_series{iview};
1107end
1108update_mode(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1109    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1110
1111%------------------------------------------------------------------------
1112% --- Executes on button press in mode.
1113function mode_Callback(hObject, eventdata, handles)
1114%------------------------------------------------------------------------       
1115SeriesData=get(handles.series,'UserData');
1116iview=get(handles.ListView,'Value');
1117mode_list=get(handles.mode,'String');
1118mode=mode_list{get(handles.mode,'Value')};
1119if isequal(mode,'bursts')
1120    enable_i(handles,'On')
1121    enable_j(handles,'Off') %do not display j index scanning in burst mode (j is fixed by the burst choice)
1122else
1123    enable_i(handles,'On')
1124    enable_j(handles,'Off')
1125end
1126fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1127    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1128ListPairs_Callback([],[],handles)
1129
1130%-------------------------------------------------------------
1131% --- Executes on selection in ListPairs.
1132function ListPairs_Callback(hObject,eventdata,handles)
1133%------------------------------------------------------------
1134list_pair=get(handles.ListPairs,'String');%get the menu of image pairs
1135if isempty(list_pair)
1136    string='';
1137else
1138    string=list_pair{get(handles.ListPairs,'Value')};
1139    string=regexprep(string,',.*','');%removes time indication (after ',')
1140end
1141PairString=get(handles.PairString,'Data');
1142iview=get(handles.ListView,'Value');
1143PairString{iview,1}=string;
1144% report the selected pair string to the table PairString
1145set(handles.PairString,'Data',PairString)
1146
1147%------------------------------------------------------------------------
1148function num_ref_i_Callback(hObject, eventdata, handles)
1149%------------------------------------------------------------------------
1150mode_list=get(handles.mode,'String');
1151mode=mode_list{get(handles.mode,'Value')};
1152SeriesData=get(handles.series,'UserData');
1153iview=get(handles.ListView,'Value');
1154fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1155    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview});% update the menu of pairs depending on the available netcdf files
1156ListPairs_Callback([],[],handles)
1157
1158%------------------------------------------------------------------------
1159function num_ref_j_Callback(hObject, eventdata, handles)
1160%------------------------------------------------------------------------
1161num_ref_i_Callback(hObject, eventdata, handles)
1162
1163%------------------------------------------------------------------------
1164function update_mode(handles,i1_series,i2_series,j1_series,j2_series,time)
1165%------------------------------------------------------------------------   
1166% check_burst=0;
1167if isempty(j2_series)% no j pair
1168    if isempty(i2_series)
1169        set(handles.mode,'Value',1)
1170        set(handles.mode,'String',{''})% no pair menu to display
1171    else   
1172        set(handles.mode,'Value',1)
1173        set(handles.mode,'String',{'series(Di)'}) % pair menu with only option Di
1174    end
1175else %existence of j pairs
1176    pair_max=squeeze(max(i1_series,[],1)); %max on pair index
1177    j_max=max(pair_max,[],1);
1178    MaxIndex_i=max(find(j_max))-1;% max ref index i
1179    MinIndex_i=min(find(j_max))-1;% min ref index i
1180    i_max=max(pair_max,[],2);
1181    MaxIndex_j=max(find(i_max))-1;% max ref index i
1182    MinIndex_j=min(find(i_max))-1;% min ref index i
1183    if MaxIndex_j==MinIndex_j
1184        set(handles.mode,'Value',1);
1185        set(handles.mode,'String',{'bursts'})
1186%         check_burst=1;
1187    elseif MaxIndex_i==MinIndex_i
1188        set(handles.mode,'Value',1);
1189        set(handles.mode,'String',{'series(Dj)'})
1190    else
1191        set(handles.mode,'String',{'bursts';'series(Dj)'})
1192        if (MaxIndex_j-MinIndex_j)>10
1193            set(handles.mode,'Value',2);%set mode to series(Dj) if more than 10 j values
1194        else
1195            set(handles.mode,'Value',1);
1196%             check_burst=1;
1197        end
1198    end
1199end
1200% if check_burst
1201%     enable_i(handles,'On')
1202%     enable_j(handles,'Off') %do not display j index scanning in burst mode (j is fixed by the burst choice)
1203% else
1204%     enable_i(handles,'On')
1205%     if isempty(j1_series)
1206%          enable_j(handles,'Off')
1207%     else
1208%         enable_j(handles,'On')
1209%     end
1210% end
1211fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1212ListPairs_Callback([],[],handles)
1213
1214%--------------------------------------------------------------
1215% determine the menu for civ1 pairstring depending on existing netcdf files
1216% with the reference indices num_ref_i and num_ref_j
1217%----------------------------------------------------------------
1218function fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1219
1220mode_list=get(handles.mode,'String');
1221mode=mode_list{get(handles.mode,'Value')};
1222ref_i=str2num(get(handles.num_ref_i,'String'));
1223if isempty(ref_i)
1224    ref_i=1;
1225end
1226if strcmp(get(handles.num_ref_j,'Visible'),'on')
1227    ref_j=str2num(get(handles.num_ref_j,'String'));
1228    if isempty(ref_j)
1229        ref_j=1;
1230    end
1231else
1232    ref_j=1;
1233end
1234TimeUnit=get(handles.TimeUnit,'String');
1235if length(TimeUnit)>=1
1236    dtunit=['m' TimeUnit];
1237else
1238    dtunit='e-03';
1239end
1240
1241displ_pair={};
1242if strcmp(mode,'series(Di)')
1243    if isempty(i2_series)
1244        msgbox_uvmat('ERROR','no i1-i2 pair available')
1245        return
1246    end
1247    diff_i=i2_series-i1_series;
1248    min_diff=min(diff_i(diff_i>0));
1249    max_diff=max(diff_i(diff_i>0));
1250    for ipair=min_diff:max_diff
1251        if numel(diff_i(diff_i==ipair))>0
1252            pair_string=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1253            if ~isempty(time)
1254                if ref_i<=floor(ipair/2)
1255                    ref_i=floor(ipair/2)+1;% shift ref_i to get the first pair
1256                end
1257                Dt=time(ref_i+ceil(ipair/2),ref_j)-time(ref_i-floor(ipair/2),ref_j);
1258                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1259            end
1260            displ_pair=[displ_pair;{pair_string}];
1261        end
1262    end
1263    if ~isempty(displ_pair)
1264        displ_pair=[displ_pair;{'Di=*|*'}];
1265    end
1266elseif strcmp(mode,'series(Dj)')
1267    if isempty(j2_series)
1268        msgbox_uvmat('ERROR','no j1-j2 pair available')
1269        return
1270    end
1271    diff_j=j2_series-j1_series;
1272    min_diff=min(diff_j(diff_j>0));
1273    max_diff=max(diff_j(diff_j>0));
1274    for ipair=min_diff:max_diff
1275        if numel(diff_j(diff_j==ipair))>0
1276            pair_string=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1277            if ~isempty(time)
1278                if ref_j<=floor(ipair/2)
1279                    ref_j=floor(ipair/2)+1;% shift ref_i to get the first pair
1280                end
1281                Dt=time(ref_i,ref_j+ceil(ipair/2))-time(ref_i,ref_j-floor(ipair/2));
1282                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1283            end
1284            displ_pair=[displ_pair;{pair_string}];
1285        end
1286    end
1287    if ~isempty(displ_pair)
1288        displ_pair=[displ_pair;{'Dj=*|*'}];
1289    end
1290elseif strcmp(mode,'bursts')
1291    if isempty(j2_series)
1292        msgbox_uvmat('ERROR','no j1-j2 pair available')
1293        return
1294    end
1295    diff_j=j2_series-j1_series;
1296    min_j1=min(j1_series(j1_series>0));
1297    max_j1=max(j1_series(j1_series>0));
1298    min_j2=min(j2_series(j2_series>0));
1299    max_j2=max(j2_series(j2_series>0));
1300    for pair1=min_j1:min(max_j1,min_j1+20)
1301        for pair2=min_j2:min(max_j2,min_j2+20)
1302        if numel(j1_series(j1_series==pair1))>0 && numel(j2_series(j2_series==pair2))>0
1303            displ_pair=[displ_pair;{['j= ' num2str(pair1) '-' num2str(pair2)]}];
1304        end
1305        end
1306    end
1307    if ~isempty(displ_pair)
1308        displ_pair=[displ_pair;{'j=*-*'}];
1309    end
1310end
1311set(handles.num_ref_i,'String',num2str(ref_i)) % update ref_i and ref_j
1312set(handles.num_ref_j,'String',num2str(ref_j))
1313
1314%% display list of pairstring
1315displ_pair_list=get(handles.ListPairs,'String');
1316NewVal=[];
1317if ~isempty(displ_pair_list)
1318Val=get(handles.ListPairs,'Value');
1319NewVal=find(strcmp(displ_pair_list{Val},displ_pair),1);% look at the previous display in the new menu displ_pï¿œir
1320end
1321if ~isempty(NewVal)
1322    set(handles.ListPairs,'Value',NewVal)
1323else
1324    set(handles.ListPairs,'Value',1)
1325end
1326set(handles.ListPairs,'String',displ_pair)
1327
1328%-------------------------------------
1329function enable_i(handles,state)
1330set(handles.i_txt,'Visible',state)
1331set(handles.num_first_i,'Visible',state)
1332set(handles.num_last_i,'Visible',state)
1333set(handles.num_incr_i,'Visible',state)
1334% set(handles.num_MaxIndex_i,'Visible',state)
1335set(handles.num_ref_i,'Visible',state)
1336set(handles.ref_i_text,'Visible',state)
1337
1338%-----------------------------------
1339function enable_j(handles,state)
1340set(handles.j_txt,'Visible',state)
1341% set(handles.num_MinIndex_j,'Visible',state)
1342set(handles.num_first_j,'Visible',state)
1343set(handles.num_last_j,'Visible',state)
1344set(handles.num_incr_j,'Visible',state)
1345% set(handles.num_MaxIndex_j,'Visible',state)
1346set(handles.num_ref_j,'Visible',state)
1347set(handles.ref_j_text,'Visible',state)
1348
1349%-----------------------------------
1350% function view_FieldMenu(handles,state)
1351% % set(handles.FieldName,'Visible',state)
1352% % set(handles.Field_text,'Visible',state)
1353% set(handles.InputFields,'Visible',state)
1354
1355% %-----------------------------------
1356% function view_FieldMenu_1(handles,state)
1357% set(handles.FieldName_1,'Visible',state)
1358% set(handles.Field_text_1,'Visible',state)
1359
1360
1361%%%%%%%%%%%%%%%%%%%%
1362%%  MAIN ActionName FUNCTIONS
1363%%%%%%%%%%%%%%%%%%%%
1364%------------------------------------------------------------------------
1365% --- Executes on button press in RUN.
1366function RUN_Callback(hObject, eventdata, handles)
1367%------------------------------------------------------------------------
1368set(handles.RUN,'BusyAction','queue');
1369set(0,'CurrentFigure',handles.series)
1370set(handles.RUN, 'Enable','Off')
1371set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1372drawnow
1373[h_fun,Series,filexml,errormsg]=prepare_jobs(handles);
1374if ~isempty(errormsg)
1375    msgbox_uvmat('ERROR',errormsg)
1376    return
1377end
1378RunModeList=get(handles.RunMode,'String');
1379RunMode=RunModeList{get(handles.RunMode,'Value')};
1380
1381switch RunMode
1382    case 'local'
1383        Series=h_fun(Series);
1384        if ~isempty(filexml)
1385            t=struct2xml(Series);
1386            t=set(t,1,'name','Series');
1387            save(t,filexml);
1388        end
1389    case 'background'
1390        if isempty(filexml)
1391            Series=h_fun(Series);% no background in the absence of output file
1392        else
1393            % update the xml file after interactive input with the function
1394            Series.Specific='?';
1395            Series=h_fun(Series);
1396            t=struct2xml(Series);
1397            t=set(t,1,'name','Series');
1398            save(t,filexml);
1399            path_uvmat=fileparts(which('uvmat'));
1400           
1401            filename_bat=regexprep(filexml,'.xml$','.bat');
1402            [fid,message]=fopen(filename_bat,'w');
1403            if isequal(fid,-1)
1404                msgbox_uvmat('ERROR', ['creation of .bat file: ' message]);
1405                return
1406            end
1407            path_fct=get(handles.ActionPath,'String');
1408            filelog=regexprep(filexml,'.xml$','.log');
1409       
1410            switch computer
1411                case {'GLNX86','GLNXA64','MACI64'}
1412                    text_matlabscript=[...
1413                        '#!/bin/bash \n'...
1414                        '. /etc/sysprofile \n'...
1415                        'matlab -nodisplay -nosplash -nojvm -logfile ''' filelog ''' <<END_MATLAB \n'...
1416                        'addpath(''' path_uvmat '''); \n'...
1417                        'addpath(''' Series.Action.ActionPath '''); \n'...
1418                        '' Series.Action.ActionName  '( ''' filexml '''); \n'...
1419                        'exit \n'...
1420                        'END_MATLAB \n'];
1421                    fprintf(fid,text_matlabscript);
1422                    fclose(fid);
1423                    system(['chmod +x ' filename_bat]);% set the file to executable
1424                    system(['. ' filename_bat ' &']);%execute fct
1425                   
1426                case {'PCWIN','PCWIN64'}
1427                    text_matlabscript=['matlab -automation -logfile ' regexprep(filelog,'\\','\\\\')...
1428                        ' -r "addpath(''' regexprep(path_uvmat,'\\','\\\\') ''');'...
1429                        'addpath(''' regexprep(Series.Action.ActionPath,'\\','\\\\') ''');'...
1430                        '' Series.Action.ActionName  '( ''' regexprep(filexml,'\\','\\\\') ''');exit"'];
1431                    fprintf(fid,text_matlabscript);
1432                    fclose(fid);
1433                    dos([filename_bat ' &']);
1434            end
1435        end
1436        update_waitbar(handles.Waitbar,1); % put the waitbar to end position to indicate lounching is finished
1437end
1438
1439set(handles.RUN, 'Enable','On')
1440set(handles.RUN,'BackgroundColor',[1 0 0])
1441
1442%------------------------------------------------------------------------
1443function STOP_Callback(hObject, eventdata, handles)
1444%------------------------------------------------------------------------
1445set(handles.RUN, 'BusyAction','cancel')
1446set(handles.RUN,'BackgroundColor',[1 0 0])
1447set(handles.RUN,'enable','on')
1448% set(handles.BATCH,'BackgroundColor',[1 0 0])
1449% set(handles.BATCH,'enable','on')
1450
1451%------------------------------------------------------------------------
1452% --- Executes on button press in BATCH.
1453function BATCH_Callback(hObject, eventdata, handles)
1454%------------------------------------------------------------------------   
1455
1456
1457% %------------------------------------------------------------------------
1458% % --- Executes on button press in BIN.
1459% function BIN_Callback(hObject, eventdata, handles)
1460% %------------------------------------------------------------------------
1461%     cmd=['#!/bin/bash \n '...
1462%         '#$ -cwd \n '...
1463%         'hostname && date \n '...
1464%         'umask 002 \n'...
1465%         Param.xml.CivmBin ' ' Param.xml.RunTime ' ' filename_xml ' ' OutputFile '.nc'];
1466%     
1467%------------------------------------------------------------------------
1468% --- Main launch command, called by RUN and BATCH
1469function [h_fun,Series,filexml,errormsg]=prepare_jobs(handles,run)
1470%INPUT:
1471% handles: handles of graphic objects on the GUI series
1472% run=0, just to display parameters for MenuExport/GUI config
1473% run=1 (default) prepare the computation
1474
1475%------------------------------------------------------------------------
1476h_fun=[];
1477filexml='';
1478errormsg='';
1479if ~exist('run','var')
1480    run=1;
1481end
1482%% Read parameters from series
1483Series=read_GUI(handles.series);
1484if isfield(Series,'Pairs')
1485Series=rmfield(Series,'Pairs'); %info Pairs not needed for output
1486end
1487
1488%% read index ranges
1489first_i=1;
1490last_i=1;
1491incr_i=1;
1492first_j=1;
1493last_j=1;
1494incr_j=1;
1495if isfield(Series.IndexRange,'first_i')
1496    first_i=Series.IndexRange.first_i;
1497    incr_i=Series.IndexRange.incr_i;
1498    last_i=Series.IndexRange.last_i;
1499end
1500if isfield(Series.IndexRange,'first_j')
1501    first_j=Series.IndexRange.first_j;
1502    incr_j=Series.IndexRange.incr_j;
1503    last_j=Series.IndexRange.last_j;
1504end
1505
1506%% read input file parameters and set menus
1507menu_coord_state=get(handles.TransformName,'Visible');
1508if isequal(menu_coord_state,'on')
1509    menu_index=get(handles.TransformName,'Value');
1510    transform_list=get(handles.TransformName,'UserData');
1511    Series.FieldTransform.TransformHandle=transform_list{menu_index};% transform function handles
1512end
1513
1514if last_i < first_i | last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
1515    set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1516
1517%% projection object
1518if isfield(Series,'CheckObject')
1519    if Series.CheckObject
1520        hset_object=findobj(allchild(0),'tag','set_object');
1521        Series.ProjObject=read_GUI(hset_object);
1522        CheckObject_Callback([], [], handles)
1523    end
1524else
1525    Series.CheckObject=0;
1526end
1527
1528%% get_field GUI
1529if isfield(Series,'InputFields')&&isfield(Series.InputFields,'Field')
1530    if strcmp(Series.InputFields.Field,'get_field...')
1531        hget_field=findobj(allchild(0),'name','get_field');
1532        Series.GetField=read_GUI(hget_field);
1533    end
1534end
1535
1536if ~run
1537    return
1538end
1539
1540%% defining the ActionName function handle
1541list_action=get(handles.ActionName,'String');% list menu action
1542index=get(handles.ActionName,'Value');
1543action= list_action{index}; % selected string
1544%Series.Action=action;%name of the processing programme
1545Series.hseries=handles.series; % handles to the series GUI
1546path_series=which('series');
1547list_path=get(handles.ActionName,'UserData');
1548fct_path=list_path{index}; %path stored for the function ACTION
1549if ~isequal(fct_path,path_series)
1550    eval(['spath=which(''' action ''');']) %spath = current path of the selected function ACTION
1551    if ~exist(fct_path,'dir')
1552        errormsg=['The prescribed function path ' fct_path ' does not exist'];
1553        return
1554    end
1555    if ~isequal(spath,fct_path)
1556        addpath(fct_path)% add the prescribed path if not the current one
1557    end
1558end
1559eval(['h_fun=@' action ';'])%create a function handle for ACTION
1560if ~isequal(fct_path,path_series)
1561        rmpath(fct_path)% add the prescribed path if not the current one   
1562end
1563
1564%% create the output data directory and write in it the xml file from the GUI config
1565%determine the root file corresponding to the first sub dir
1566if isfield(Series,'OutputSubDir')
1567    SubDirOut=[Series.OutputSubDir Series.OutputDirExt];
1568    SubDirOutNew=SubDirOut;
1569    iview=1;
1570    SeriesData=get(handles.series,'UserData');
1571    if size(Series.InputTable,1)>1 && isfield(SeriesData,'AllowInputSort') && isfield(SeriesData.AllowInputSort)
1572        [tild,iview]=sort(Series.InputTable(:,2)); %subdirectories sorted in alphabetical order
1573        Series.InputTable=Series.InputTable(iview,:);
1574    end
1575    detect=exist(fullfile(Series.InputTable{1,1},SubDirOutNew),'dir');% test if  the dir  already exist
1576    check_create=1; %need to create the result directory by default
1577    while detect
1578        answer=msgbox_uvmat('INPUT_Y-N',['use existing ouput directory: ' fullfile(Series.InputTable{1,1},SubDirOutNew) ', possibly delete previous data']);
1579        if isequal(answer,'Yes')
1580            detect=0;
1581            check_create=0;
1582        else
1583            r=regexp(SubDirOutNew,'(?<root>.*\D)(?<num1>\d+)$','names');%detect whether name ends by a number
1584            if isempty(r)
1585                r(1).root=[SubDirOutNew '_'];
1586                r(1).num1='0';
1587            end
1588            SubDirOutNew=[r(1).root num2str(str2num(r(1).num1)+1)];%increment the index by 1 or put 1
1589            detect=exist(fullfile(Series.InputTable{1,1},SubDirOutNew),'dir');% test if  the dir  already exists   
1590            check_create=1;
1591        end
1592    end
1593    Series.OutputDirExt=regexprep(SubDirOutNew,Series.OutputSubDir,'');
1594 %   Series.OutputSubDir=SubDirOutNew;
1595 %   Series.OutputDir=fullfile(Series.InputTable{1,1},Series.OutputSubDir);%directory set for output results
1596    Series.OutputRootFile=Series.InputTable{1,3};% the first sorted RootFile taken for output
1597    set(handles.OutputDirExt,'String',Series.OutputDirExt)
1598    % create output directory
1599    OutputDir=fullfile(Series.InputTable{1,1},[Series.OutputSubDir Series.OutputDirExt]);
1600    if check_create
1601        [tild,msg1]=mkdir(OutputDir);
1602        if ~strcmp(msg1,'')
1603            errormsg=['cannot create ' OutputDir ': ' msg1];%error message for directory creation
1604            return
1605        end
1606    end
1607    filexml=fullfile(OutputDir,[Series.InputTable{1,3} '.xml']);% name of the parameter xml file set in this directory
1608end
1609%removes redondant information
1610Series.IndexRange=rmfield(Series.IndexRange,'TimeTable');
1611Series.IndexRange=rmfield(Series.IndexRange,'MinIndex');
1612Series.IndexRange=rmfield(Series.IndexRange,'MaxIndex');
1613%removes empty lines of InputTable
1614empty_line=zeros(size(Series.InputTable,1),1);
1615for iline=1:size(Series.InputTable,1)
1616    empty_line(iline)=isequal(Series.InputTable(iline,1:3),{'','',''});
1617end
1618Series.InputTable(find(empty_line),:)=[];
1619
1620%------------------------------------------------------------------------
1621% --- Executes on selection change in ActionName.
1622function ActionName_Callback(hObject, eventdata, handles)
1623%------------------------------------------------------------------------
1624global nb_builtin_ACTION
1625list_ACTION=get(handles.ActionName,'String');% list menu fields
1626index_ACTION=get(handles.ActionName,'Value');% selected string index
1627ACTION= list_ACTION{index_ACTION}; % selected function name
1628path_series=which('series');%path to series.m
1629list_path=get(handles.ActionName,'UserData');%list of recorded paths to functions of the list ACTION
1630default_file=fullfile(list_path{end},ACTION);
1631% add a new function to the menu if the selected item is 'more...'
1632if isequal(ACTION,'more...')
1633    pathfct=fileparts(path_series);
1634    [FileName, PathName, filterindex] = uigetfile( ...
1635       {'*.m', ' (*.m)';
1636        '*.m',  '.m files '; ...
1637        '*.*', 'All Files (*.*)'}, ...
1638        'Pick a file',default_file);
1639    if length(FileName)<2
1640        return
1641    end
1642    [pp,ACTION,ext_fct]=fileparts(FileName);%(end-1:end);
1643    if ~isequal(ext_fct,'.m')
1644        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
1645        return
1646    end
1647   
1648   % insert the choice in the actionname menu
1649   menu_str=update_menu(handles.ActionName,ACTION);%new action menu in which the new item has been appended if needed
1650   index_ACTION=get(handles.ActionName,'Value');% currently selected index in the list
1651   list_path{index_ACTION}=PathName;
1652   if length(menu_str)>nb_builtin_ACTION+5; %nb_builtin=nbre of functions always remaining in the initial menu
1653       nbremove=length(menu_str)-nb_builtin_ACTION-5;
1654       menu_str(nb_builtin_ACTION+1:end-5)=[];
1655       list_path(nb_builtin_ACTION+1:end-4)=[];
1656       index_ACTION=index_ACTION-nbremove;
1657       set(handles.ActionName,'Value',index_ACTION)
1658       set(handles.ActionName,'String',menu_str)
1659   end
1660   list_path{index_ACTION}=PathName;
1661   set(handles.ActionName,'UserData',list_path);
1662   set(handles.ActionPath,'enable','inactive')% indicate that the current path is accessible (not 'off')
1663   
1664   %record the current menu in personal file profil_perso
1665   dir_perso=prefdir;
1666   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
1667   for ilist=nb_builtin_ACTION+1:length(menu_str)-1
1668       series_fct{ilist-nb_builtin_ACTION}=fullfile(list_path{ilist},[menu_str{ilist} '.m']);     
1669   end
1670   if nb_builtin_ACTION+1<=length(menu_str)-1
1671       if exist(profil_perso,'file')% && nb_builtin_ACTION+1>=length(menu_str)-1
1672           save(profil_perso,'series_fct','-append')
1673       else
1674           txt=ver('MATLAB');
1675           Release=txt.Release;
1676           relnumb=str2num(Release(3:4));
1677           if relnumb >= 14%recent relaese of Matlab
1678               save(profil_perso,'series_fct','-V6')
1679           else
1680               save(profil_perso, 'series_fct')
1681           end
1682       end
1683   end
1684end
1685
1686%check the current ActionPath to the selected function
1687PathName=list_path{index_ACTION};%current recorded path
1688set(handles.ActionPath,'String',PathName); %show the path to the senlected function
1689
1690%reinitialise the waitbar
1691update_waitbar(handles.Waitbar,0)
1692
1693%default setting for the visibility of the GUI elements
1694set(handles.num_NbSlice,'Visible','off')
1695set(handles.NbSlice_title,'Visible','off')
1696set(handles.VelType,'Visible','off');
1697set(handles.VelType_text,'Visible','off');
1698set(handles.VelType_1,'Visible','off');
1699set(handles.VelType_text_1,'Visible','off');
1700set(handles.InputFields,'Visible','off')
1701set(handles.FieldName_1,'Visible','off')
1702%view_FieldMenu_1(handles,'off')
1703set(handles.FieldTransform,'Visible','off')
1704set(handles.CheckObject,'Visible','off');
1705set(handles.ProjObject,'Visible','off');
1706set(handles.CheckMask,'Visible','off')
1707set(handles.Mask,'Visible','off')
1708set(handles.OutputDirExt,'Visible','off')
1709set(handles.OutputSubDir,'Visible','off')
1710set(handles.OutputDir_title,'Visible','off')
1711%set the displayed GUI item needed for input parameters
1712if ~isequal(path_series,PathName)
1713    addpath(PathName)
1714end
1715eval(['h_function=@' ACTION ';']);
1716try
1717    [fid,errormsg] =fopen([ACTION '.m']);
1718    InputText=textscan(fid,'%s',1,'delimiter','\n');
1719    fclose(fid)
1720    set(handles.ActionName,'ToolTipString',InputText{1}{1})% put the first line of the selected function as tooltip help
1721end
1722if ~isequal(path_series,PathName)
1723    rmpath(PathName)
1724end
1725varargout=h_function();
1726Param_list={};
1727
1728InputTable=get(handles.InputTable,'Data');
1729nbview=size(InputTable,1);
1730SeriesData=get(handles.series,'UserData');
1731nb_civ=numel(find(strcmp('civx',SeriesData.FileType)|strcmp('civdata',SeriesData.FileType)));
1732nb_netcdf=numel(find(strcmp('netcdf',SeriesData.FileType)));
1733for ilist=1:length(varargout)-1
1734    switch varargout{ilist}
1735        case 'AllowInputSort'
1736            if isequal(lower(varargout{ilist+1}),'on')% sort the input table by alphabetical order of the SubDir
1737                SeriesData.AllowInputSort=1;
1738                set(handles.series,'UserData',SeriesData)
1739            end                     
1740        case 'WholeIndexRange'
1741            if isequal(lower(varargout{ilist+1}),'on')% set by default the input index range from min to max
1742                MinIndex=get(handles.MinIndex,'Data');
1743                MaxIndex=get(handles.MaxIndex,'Data');
1744                if ~isempty(MinIndex)
1745                    set(handles.num_first_i,'String',num2str(MinIndex{1}))
1746                    set(handles.num_last_i,'String',num2str(MaxIndex{1}))
1747                    set(handles.num_incr_i,'String','1')
1748                    if size(MinIndex,2)>=2
1749                        set(handles.num_first_j,'String',num2str(MinIndex{1,2}))
1750                        set(handles.num_last_j,'String',num2str(MaxIndex{1,2}))
1751                        set(handles.num_incr_j,'String','1')
1752                    end
1753                end
1754            end           
1755        case 'NbSlice'   %hidden by default
1756            if isequal(lower(varargout{ilist+1}),'on')
1757                set(handles.num_NbSlice,'Visible','on')
1758                set(handles.NbSlice_title,'Visible','on')
1759            end
1760        case 'VelType'   %hidden by default
1761             if isequal(lower(varargout{ilist+1}),'one') || isequal(lower(varargout{ilist+1}),'two')
1762                if nb_civ>=1
1763                    set(handles.VelType,'Visible','on')
1764                    set(handles.VelType_text,'Visible','on');
1765                end
1766             end
1767            if isequal(lower(varargout{ilist+1}),'two')
1768                if nb_civ>=2
1769                    set(handles.VelType_1,'Visible','on')
1770                    set(handles.VelType_text_1,'Visible','on');
1771                end
1772            end
1773        case 'FieldName'   %hidden by default
1774            if isequal(lower(varargout{ilist+1}),'one')||isequal(lower(varargout{ilist+1}),'two')
1775                if (nb_civ+nb_netcdf)>=1
1776                 set(handles.FieldName,'Visible','on') % test for MenuBorser
1777                 set(handles.InputFields,'Visible','on')
1778                end
1779            end
1780            if isequal(lower(varargout{ilist+1}),'two')
1781                if (nb_civ+nb_netcdf)>=1
1782                set(handles.FieldName_1,'Visible','on')
1783                end
1784            end
1785        case 'FieldTransform'   %hidden by default
1786            if isequal(lower(varargout{ilist+1}),'on')
1787                set(handles.TransformName,'Enable','on')
1788                set(handles.FieldTransform,'Visible','on')
1789                TransformName_Callback([],[], handles)
1790            end
1791        case 'ProjObject'   %hidden by default
1792            if isequal(lower(varargout{ilist+1}),'on')   
1793                set(handles.CheckObject,'Visible','on')
1794                set(handles.ProjObject,'Visible','on')
1795            end
1796        case 'Mask'   %hidden by default
1797            if isequal(lower(varargout{ilist+1}),'on')   
1798                set(handles.Mask,'Visible','on')
1799                 set(handles.CheckMask,'Visible','on');
1800            end 
1801        case 'OutputDirExt'
1802            if ~isempty(varargout{ilist+1})
1803            set(handles.OutputDirExt,'String',varargout{ilist+1})
1804            set(handles.OutputDirExt,'Visible','on')
1805            set(handles.OutputSubDir,'Visible','on')
1806            set(handles.OutputDir_title,'Visible','on') 
1807            end
1808    end
1809end
1810if ~isempty(Param_list)
1811    set(handles.ParamKey,'String',Param_list)
1812    set(handles.ParamVal,'Visible','on')
1813end
1814
1815%------------------------------------------------------------------------
1816% --- Executes on selection change in FieldName.
1817function FieldName_Callback(hObject, eventdata, handles)
1818%------------------------------------------------------------------------
1819field_str=get(handles.FieldName,'String');
1820field_index=get(handles.FieldName,'Value');
1821field=field_str{field_index(1)};
1822if isequal(field,'get_field...')   
1823     hget_field=findobj(allchild(0),'name','get_field');
1824     if ~isempty(hget_field)
1825         delete(hget_field)%delete opened versions of get_field
1826     end
1827     filecell=get_file_series(read_GUI(handles.series));
1828     if exist(filecell{1,1},'file')
1829        get_field(filecell{1,1})
1830     end
1831elseif isequal(field,'more...')
1832    str=calc_field;
1833    [ind_answer,v] = listdlg('PromptString','Select a file:',...
1834                'SelectionMode','single',...
1835                'ListString',str);
1836       % edit the choice in the fields and actionname menu
1837     scalar=cell2mat(str(ind_answer));
1838     update_menu(handles.FieldName,scalar)
1839end
1840
1841%------------------------------------------------------------------------
1842% --- Executes on selection change in FieldName_1.
1843function FieldName_1_Callback(hObject, eventdata, handles)
1844%------------------------------------------------------------------------
1845field_str=get(handles.FieldName_1,'String');
1846field_index=get(handles.FieldName_1,'Value');
1847field=field_str{field_index};
1848if isequal(field,'get_field...')   
1849     hget_field=findobj(allchild(0),'name','get_field_1');
1850     if ~isempty(hget_field)
1851         delete(hget_field)
1852     end
1853     SeriesData=get(handles.series,'UserData');
1854     filename=SeriesData.CurrentInputFile_1;
1855     if exist(filename,'file')
1856        hget_field=get_field(filename);
1857        set(hget_field,'name','get_field_1')
1858     end
1859elseif isequal(field,'more...')
1860    str=calc_field;
1861    [ind_answer,v] = listdlg('PromptString','Select a file:',...
1862                'SelectionMode','single',...
1863                'ListString',str);
1864       % edit the choice in the fields and actionname menu
1865     scalar=cell2mat(str(ind_answer));
1866     update_menu(handles.FieldName_1,scalar)
1867end   
1868
1869
1870%%%%%%%%%%%%%
1871function [ind_remove]=find_pairs(dirpair,ind_i,last_i)
1872indsel=ind_i;
1873indiff=diff(ind_i); %test index increment to detect multiplets (several pairs with the same index ind_i) and holes in the series
1874indiff=[1 indiff last_i-ind_i(end)+1];%for testing gaps with the imposed bounds
1875if ~isempty(indiff)
1876    indiff2=diff(indiff);
1877    indiffp=[indiff2 1];
1878    indiffm=[1 indiff2];
1879    ind_multi_m=find((indiff==0)&(indiffm<0))-1;%indices of first members of multiplets
1880    ind_multi_p=find((indiff==0)&(indiffp>0));%indices of last members of multiplets
1881    %for each multiplet, select the most recent file
1882    ind_remove=[];
1883    for i=1:length(ind_multi_m)
1884        ind_pairs=ind_multi_m(i):ind_multi_p(i);
1885        for imulti=1:length(ind_pairs)
1886            datepair(imulti)=datenum(dirpair(ind_pairs(imulti)).date);%dates of creation
1887        end
1888        [datenew,indsort2]=sort(datepair); %sort the multiplet by creation date
1889        ind_s=indsort2(1:end-1);%
1890        ind_remove=[ind_remove ind_pairs(ind_s)];%remove these indices, leave the last one
1891    end
1892end
1893
1894%------------------------------------------------------------------------
1895% --- determine the list of index pairstring of processing file
1896function [num_i1,num_i2,num_j1,num_j2,num_i_out,num_j_out]=find_file_indices(num_i,num_j,ind_shift,NomType,mode)
1897%------------------------------------------------------------------------
1898num_i1=num_i;% set of first image numbers by default
1899num_i2=num_i;
1900num_j1=num_j;
1901num_j2=num_j;
1902num_i_out=num_i;
1903num_j_out=num_j;
1904% if isequal (NomType,'_1-2_1') || isequal (NomType,'_1-2')
1905if isequal(mode,'series(Di)')
1906    num_i1_line=num_i+ind_shift(3);% set of first image numbers
1907    num_i2_line=num_i+ind_shift(4);
1908    % adjust the first and last field number
1909        indsel=find(num_i1_line >= 1);
1910    num_i_out=num_i(indsel);
1911    num_i1_line=num_i1_line(indsel);
1912    num_i2_line=num_i2_line(indsel);
1913    num_j1=meshgrid(num_j,ones(size(num_i1_line)));
1914    num_j2=meshgrid(num_j,ones(size(num_i1_line)));
1915    [xx,num_i1]=meshgrid(num_j,num_i1_line);
1916    [xx,num_i2]=meshgrid(num_j,num_i2_line);
1917elseif isequal (mode,'series(Dj)')||isequal (mode,'bursts')
1918    if isequal(mode,'bursts') %case of bursts (png_old or png_2D)
1919        num_j1=ind_shift(1)*ones(size(num_i));
1920        num_j2=ind_shift(2)*ones(size(num_i));
1921    else
1922        num_j1_col=num_j+ind_shift(1);% set of first image numbers
1923        num_j2_col=num_j+ind_shift(2);
1924        % adjust the first field number
1925        indsel=find((num_j1_col >= 1));   
1926        num_j_out=num_j(indsel);
1927        num_j1_col=num_j1_col(indsel);
1928        num_j2_col=num_j2_col(indsel);
1929        [num_i1,num_j1]=meshgrid(num_i,num_j1_col);
1930        [num_i2,num_j2]=meshgrid(num_i,num_j2_col);
1931    end   
1932end
1933
1934%------------------------------------------------------------------------
1935% --- Executes on button press in CheckObject.
1936function CheckObject_Callback(hObject, eventdata, handles)
1937%------------------------------------------------------------------------
1938% SeriesData=get(handles.series,'UserData');
1939value=get(handles.CheckObject,'Value');
1940if value
1941     set(handles.CheckObject,'BackgroundColor',[1 1 0])%put unactivated buttons to yellow
1942     hset_object=findobj(allchild(0),'tag','set_object');%find the set_object interface handle
1943     if ishandle(hset_object)
1944         uistack(hset_object,'top')% show the GUI set_object if opened
1945     else
1946         %get the object file
1947         InputTable=get(handles.InputTable,'Data');
1948         defaultname=InputTable{1,1};
1949         if isempty(defaultname)
1950            defaultname={''};
1951         end
1952        [FileName, PathName, filterindex] = uigetfile( ...
1953       {'*.xml;*.mat', ' (*.xml,*.mat)';
1954       '*.xml',  '.xml files '; ...
1955        '*.mat',  '.mat matlab files '}, ...
1956        'Pick an xml object file (or use uvmat to create it)',defaultname);
1957        fileinput=[PathName FileName];%complete file name
1958        sizf=size(fileinput);
1959        if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
1960        %read the file
1961        data=xml2struct(fileinput);
1962        if ~isfield(data,'Type')
1963             msgbox_uvmat('ERROR',[fileinput ' is not an object xml file'])
1964             return
1965        end
1966        if ~isfield(data,'ProjMode')
1967             data.ProjMode='none';
1968        end
1969        hset_object=set_object(data);% call the set_object interface
1970     end
1971     Object=read_GUI(hset_object);
1972     set(handles.ProjObject,'String',Object.Name);%display the object name
1973else
1974    set(handles.CheckObject,'BackgroundColor',[0.7 0.7 0.7])%put activated buttons to green
1975end
1976%set(handles.series,'UserData',SeriesData)
1977
1978%--------------------------------------------------------------
1979function CheckMask_Callback(hObject, eventdata, handles)
1980value=get(handles.CheckMask,'Value');
1981if value
1982    msgbox_uvmat('ERROR','not implemented yet')
1983end
1984%--------------------------------------------------------------
1985
1986%-------------------------------------------------------------------
1987%'uv_ncbrowser': interactively calls the netcdf file browser 'get_field.m'
1988function ncbrowser_uvmat(hObject, eventdata)
1989%-------------------------------------------------------------------
1990     bla=get(gcbo,'String');
1991     ind=get(gcbo,'Value');
1992     filename=cell2mat(bla(ind));
1993      blank=find(filename==' ');
1994      filename=filename(1:blank-1);
1995     get_field(filename)
1996
1997% ------------------------------------------------------------------
1998function MenuHelp_Callback(hObject, eventdata, handles)
1999%-------------------------------------------------------------------
2000path_to_uvmat=which ('uvmat');% check the path of uvmat
2001pathelp=fileparts(path_to_uvmat);
2002helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
2003if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
2004else
2005    addpath (fullfile(pathelp,'uvmat_doc'))
2006    web([helpfile '#series'])
2007end
2008
2009%-------------------------------------------------------------------
2010% --- Executes on selection change in TransformName.
2011function TransformName_Callback(hObject, eventdata, handles)
2012%-------------------------------------------------------------------
2013global nb_transform
2014
2015menu=get(handles.TransformName,'String');
2016ind_coord=get(handles.TransformName,'Value');
2017coord_option=menu{ind_coord};
2018list_transform=get(handles.TransformName,'UserData');
2019ff=functions(list_transform{end});
2020if isequal(coord_option,'more...');
2021    coord_fct='';
2022    prompt = {'Enter the name of the transform function'};
2023    dlg_title = 'user defined transform';
2024    num_lines= 1;
2025    [FileName, PathName, filterindex] = uigetfile( ...
2026       {'*.m', ' (*.m)';
2027        '*.m',  '.m files '; ...
2028        '*.*', 'All Files (*.*)'}, ...
2029        'Pick a file', ff.file);
2030    if isequal(PathName(end),'/')||isequal(PathName(end),'\')
2031        PathName(end)=[];
2032    end
2033    transform_selected =fullfile(PathName,FileName);
2034    if ~exist(transform_selected,'file')
2035          return
2036    end
2037    [ppp,transform,xt_fct]=fileparts(FileName);% removes extension .m
2038    if ~isequal(ext_fct,'.m')
2039        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
2040        return
2041    end
2042   menu=update_menu(handles.TransformName,transform);%add the selected fct to the menu
2043   ind_coord=get(handles.TransformName,'Value');
2044   addpath(PathName)
2045   list_transform{ind_coord}=str2func(transform);% create the function handle corresponding to the newly seleced function
2046   set(handles.TransformName,'UserData',list_transform)
2047   rmpath(PathName)
2048   % save the new menu in the personal file 'uvmat_perso.mat'
2049   dir_perso=prefdir;%personal Matalb directory
2050   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2051   if exist(profil_perso,'file')
2052       for ilist=nb_transform+1:numel(list_transform)
2053           ff=functions(list_transform{ilist});
2054           transform_fct{ilist-nb_transform}=ff.file;
2055       end
2056        save (profil_perso,'transform_fct','-append'); %store the root name for future opening of uvmat
2057   end
2058end
2059
2060%check the current ActionPath to the selected function
2061if ~isempty(list_transform{ind_coord})
2062    func=functions(list_transform{ind_coord});
2063    set(handles.TransformPath,'String',fileparts(func.file)); %show the path to the senlected function
2064else
2065    set(handles.TransformPath,'String',''); %show the path to the senlected function
2066end
2067
2068% %------------------------------------------------------------------------
2069% % --- Executes on button press in REFRESH.
2070%     function REFRESH_INDICES_Callback(hObject, eventdata, handles)
2071% %------------------------------------------------------------------------       
2072% % hObject    handle to REFRESH (see GCBO)
2073% % eventdata  reserved - to be defined in a future version of MATLAB
2074% % handles    structure with handles and user data (see GUIDATA)
2075% set(handles.REFRESH,'BackgroundColor',[0.7 0.7 0.7])
2076% InputTable=get(handles.InputTable,'Data');
2077% check_lines=get(handles.REFRESH,'UserData');
2078%
2079% %% check the indices and FileTypes for each series (limited to the new ones to save time)
2080% for ind_list=1:length(check_lines)
2081%     if  check_lines(ind_list)
2082%         InputLine=InputTable(ind_list,:);
2083%         detect_idem=strcmp('"',InputLine);% look for '" (repeat of previous data)
2084%         detect_idem=detect_idem(detect_idem>0);
2085%         if ~isempty (detect_idem)
2086%             InputLine(detect_idem)=InputTable(ind_list-1,detect_idem);
2087%             set(handles.InputTable,'Data',InputTable)
2088%         end
2089%         fileinput=fullfile_uvmat(InputLine{1},InputLine{2},InputLine{3},InputLine{5},InputLine{4},1,2,1,2);
2090%         %fileinput=name_generator(fullfile(InputLine{1},InputLine{3}),1,1,InputLine{5},InputLine{4},1,2,2,InputLine{2})
2091%         %update file series defined by the selected line
2092%         [InputTable{ind_list,3},InputTable{(ind_list),4},errormsg]=update_indices(handles,fileinput,ind_list);
2093%         if ~isempty(errormsg)
2094%                 msgbox_uvmat('ERROR',errormsg)
2095%                 return
2096%         end
2097%     end
2098% end
2099% set(handles.InputTable,'Data',InputTable)
2100% SeriesData=get(handles.series,'UserData');
2101%
2102% state_j='off';
2103% state_Pairs='off';
2104% state_InputFields='off';
2105% val=get(handles.ListView,'Value');
2106% ListViewString={''};
2107% if ~isempty(SeriesData)
2108% %     ListViewString={};
2109%     for iview=1:size(InputTable,1)
2110%         if ~isempty(SeriesData.j1_series{iview})
2111%             state_j='on';
2112%         end
2113%         if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
2114%             state_Pairs='on';
2115%             ListViewString{iview}=num2str(iview);
2116%             if check_lines(iview)
2117%                 val=iview;%select the last pair if it is a new entry
2118%             end
2119%         end
2120%         if strcmp(SeriesData.FileType{iview},'civx')||strcmp(SeriesData.FileType{iview},'civdata')
2121%             state_InputFields='on';
2122%         end
2123%     end
2124% end
2125% set(handles.ListView,'Value',val)
2126% set(handles.ListView,'String',ListViewString)
2127% if strcmp(state_Pairs,'on')
2128%     ListView_Callback(hObject,eventdata,handles)
2129% end
2130% set(handles.PairString,'Visible',state_Pairs)
2131% enable_j(handles,state_j)
2132% set(handles.REFRESH,'BackgroundColor',[1 0 0])
2133% set(handles.REFRESH,'visible','off')
2134
2135% -----------------------------------------------------------------------
2136% --- Update min and max indices of a file series by scanning with find_file_series
2137% --- which also changes the root file and NomType in case of movie. Also adjust the string representation of indices (e.g;
2138% --- 1 or 001 by the function find_file_series
2139% --- This function also dispaly the set of availbale files in the series
2140% --- and the menus appropriate to the file type as well as timing possibly set
2141% --- by an xml image documentation file
2142function [RootFile,NomType,errormsg]=update_indices(handles,fileinput,iview)
2143% -----------------------------------------------------------------------
2144%% look for min and max indices existing in the file series and update SeriesData
2145errormsg='';
2146[FilePath,FileName,FileExt]=fileparts(fileinput);
2147% detect the file type, get the movie object if relevant, and look for the corresponding file series:
2148% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
2149[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
2150if isempty(RootFile)&&isempty(i1_series)
2151    errormsg='no input file in the series';
2152    return
2153end
2154
2155%% adjust the min and max indices common to all the file series
2156MinIndex=get(handles.MinIndex,'Data');
2157MaxIndex=get(handles.MaxIndex,'Data');
2158MinIndex_i=min(i1_series(i1_series>0));
2159if ~isempty(i2_series)
2160    MaxIndex_i=max(i2_series(i2_series>0));
2161else
2162    MaxIndex_i=max(i1_series(i1_series>0));
2163end
2164MinIndex_j=min(j1_series(j1_series>0));
2165if ~isempty(j2_series)
2166    MaxIndex_j=max(j2_series(j2_series>0));
2167else
2168    MaxIndex_j=max(j1_series(j1_series>0));
2169end
2170MinIndex{iview,1}=MinIndex_i;
2171MinIndex{iview,2}=MinIndex_j;
2172MaxIndex{iview,1}=MaxIndex_i;
2173MaxIndex{iview,2}=MaxIndex_j;
2174set(handles.MinIndex,'Data',MinIndex)
2175set(handles.MaxIndex,'Data',MaxIndex)
2176SeriesData=get(handles.series,'UserData');
2177SeriesData.i1_series{iview}=i1_series;
2178SeriesData.i2_series{iview}=i2_series;
2179SeriesData.j1_series{iview}=j1_series;
2180SeriesData.j2_series{iview}=j2_series;
2181SeriesData.FileType{iview}=FileType;
2182
2183%% display the set of existing files as an image
2184set(handles.Waitbar,'Units','pixels')
2185pos=get(handles.Waitbar,'Position');
2186xima=0.5:pos(3)-0.5;% pixel positions on the image representing the existing file indices
2187yima=0.5:pos(4)-0.5;
2188[XIma,YIma]=meshgrid(xima,yima);
2189nb_i=size(i1_series,3);
2190nb_j=size(i1_series,2);
2191ind_i=(0.5:nb_i-0.5)*pos(3)/nb_i;
2192ind_j=(0.5:nb_j-0.5)*pos(4)/nb_j;
2193[Ind_i,Ind_j]=meshgrid(ind_i,ind_j);
2194CData=zeros([size(XIma) 3]);
2195file_ima=double((i1_series(:,:,1)>0)');
2196if numel(file_ima)>=2
2197if size(file_ima,1)==1
2198    CLine=interp1(ind_i,file_ima,xima,'nearest');
2199    CData(:,:,2)=ones(size(yima'))*CLine;
2200else
2201    CData(:,:,2)=interp2(Ind_i,Ind_j,file_ima,XIma,YIma,'nearest');
2202end
2203set(handles.Waitbar,'CData',CData)
2204end
2205set(handles.Waitbar,'Units','normalized')
2206
2207%% enable field and veltype menus
2208% testfield=isequal(get(handles.FieldName,'enable'),'on');
2209% testfield_1=isequal(get(handles.FieldName_1,'enable'),'on');
2210% testveltype=isequal(get(handles.VelType,'enable'),'on');
2211% testveltype_1=isequal(get(handles.VelType_1,'enable'),'on');
2212% testtransform=isequal(get(handles.TransformName,'Enable'),'on');
2213% testnc=0;
2214% testnc_1=0;
2215% testcivx=0;
2216% testcivx_1=0;
2217% testima=0; %test for image input
2218% if isequal(lower(FileExt),'.avi') %.avi file
2219%     testima=1;
2220% elseif ~isempty(imformats(FileExt(2:end)))
2221%     testima=1;
2222% elseif isequal(FileExt,'.vol')
2223%      testima=1;
2224% end
2225%TODO: update
2226% if length(FileExtCell)==1 || length(FileExtCell)>2
2227%     for iview=1:length(FileExtCell)
2228%         if isequal(FileExtCell{iview},'.nc')
2229%             testnc=1;
2230%         end
2231%         if isequal(FileTypeCell{iview},'civx')
2232%             testcivx=1;
2233%         end
2234%     end
2235% elseif length(FileExtCell)==2
2236%     testnc=isequal(FileExtCell{1},'.nc');
2237%     testnc_1=isequal(FileExtCell{2},'.nc');
2238%     testcivx=isequal(FileTypeCell{1},'civx');
2239%     testcivx_1=isequal(FileTypeCell{2},'civx');
2240% end
2241switch FileType
2242    case {'civx','civdata'}
2243    %view_FieldMenu(handles,'on')
2244    menustr=get(handles.FieldName,'String');
2245    if isequal(menustr,{'get_field...'})
2246        set(handles.FieldName,'String',{'get_field...';'velocity';'vort';'div';'more...'})
2247    end
2248    set(handles.VelType,'Visible','on')
2249    set(handles.FieldTransform,'Visible','on')
2250    %      view_TRANSFORM(handles,'on')
2251    %     TODO: second menu
2252    %           view_FieldMenu_1(handles,'on')
2253    %     if testcivx_1
2254    %         menustr=get(handles.FieldName_1,'String');
2255    %         if isequal(menustr,{'get_field...'})
2256    %             set(handles.FieldName_1,'String',{'get_field...';'velocity';'vort';'div';'more...'})
2257    %         end
2258    %     else
2259    %         set(handles.FieldName_1,'Value',1)
2260    %         set(handles.FieldName_1,'String',{'get_field...'})
2261    %     set(handles.VelType_1,'Visible','on')
2262    %     set(handles.VelType_text_1,'Visible','on');
2263    %     end
2264    %     view_FieldMenu_1(handles,'off')
2265    case 'netcdf'
2266  %  view_FieldMenu(handles,'on')
2267    set(handles.FieldName,'Value',1)
2268    set(handles.FieldName,'String',{'get_field...'})
2269    set(handles.FieldTransform,'Visible','off')
2270    %     view_TRANSFORM(handles,'off')
2271    case {'image','multimage','video'}
2272%    view_FieldMenu(handles,'off')
2273 %   view_FieldMenu_1(handles,'off')
2274    set(handles.VelType,'Visible','off')
2275    set(handles.VelType_text,'Visible','off');
2276end
2277
2278
2279%TODO:update
2280% if ~isequal(FileExt,'.nc') && ~isequal(FileExt,'.cdf') && ~testima
2281%     msgbox_uvmat('ERROR',['invalid input file extension ' FileExt])
2282%     return
2283% end 
2284
2285%%  read image documentation file  if found%%%%%%%%%%%%%%%%%%%%%%%%%%%
2286ext_imadoc='';
2287FileBase=fullfile(RootPath,RootFile);
2288if isequal(FileExt,'.xml')||isequal(FileExt,'.civ')
2289    ext_imadoc=FileExt;
2290elseif exist([FileBase '.xml'],'file')
2291    ext_imadoc='.xml';
2292elseif exist([FileBase '.civ'],'file')
2293    ext_imadoc='.civ';
2294end
2295%read the ImaDoc file
2296XmlData=[];
2297NbSlice_calib={};
2298if isequal(ext_imadoc,'.xml')
2299        [XmlData,warntext]=imadoc2struct([FileBase '.xml']);
2300        if isfield(XmlData,'Heading') && isfield(XmlData.Heading,'ImageName') && ischar(XmlData.Heading.ImageName)
2301            [PP,FF,ext_ima_read]=fileparts(XmlData.Heading.ImageName);
2302        end
2303        if isfield(XmlData,'Time')
2304            time{iview}=XmlData.Time;
2305        end
2306        if isfield(XmlData,'Camera')
2307            if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
2308                NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
2309                if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
2310                    msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
2311                end
2312            end
2313            if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
2314                TimeUnit=XmlData.Camera.TimeUnit;
2315            end
2316        end
2317        if ~isempty(warntext)
2318            msgbox_uvmat('WARNING',warntext)
2319        end 
2320elseif isequal(ext_imadoc,'.civ')
2321    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
2322    time{iview}=XmlData.Time;
2323    if error==2, warntext=['no file ' FileBase '.civ'];
2324    elseif error==1, warntext='inconsistent number of fields in the .civ file';
2325    end 
2326end
2327
2328%% update time table
2329TimeTable=get(handles.TimeTable,'Data');
2330TimeTable{iview,1}=time(MinIndex_i,MinIndex_j);
2331TimeTable{iview,4}=time(MaxIndex_i,MaxIndex_j);
2332set(handles.TimeTable,'Data',TimeTable)
2333
2334%% number of slices
2335if isfield(XmlData,'GeometryCalib') && isfield(XmlData.GeometryCalib,'SliceCoord')
2336       siz=size(XmlData.GeometryCalib.SliceCoord);
2337       if siz(1)>1
2338           NbSlice=siz(1);
2339       else
2340           NbSlice=1;
2341       end
2342       set(handles.num_NbSlice,'String',num2str(NbSlice))
2343end
2344% set(handles.mode,'Visible','off') % do not show index pairstring by default
2345set(handles.PairString,'Visible','off')
2346% set(handles.num_ref_i,'Visible','off')
2347% set(handles.ref_i_text,'Visible','off')
2348testpair=0;
2349%set the menus of image pairstring and default selection for series
2350%list pairstring if relevant
2351% Val=get(handles.NomType,'Value');
2352% synchronise_view(handles,Val)
2353
2354% if ~isfield(SeriesData,'j1_series')||isempty(SeriesData.j1_series{index})
2355%     state_j='off'; %no need for j index
2356% else
2357%     state_j='on'; %case of j index
2358% end
2359% show index pairstring if files exist
2360set(handles.series,'UserData',SeriesData)
2361
2362
2363% --------------------------------------------------------------------
2364function MenuExportConfig_Callback(hObject, eventdata, handles)
2365global Series
2366[tild,Series,errormsg]=prepare_jobs(handles,0);
2367% Series=read_GUI(handles.series);
2368
2369evalin('base','global Series')%make CurData global in the workspace
2370display('current series config :')
2371evalin('base','Series') %display CurData in the workspace
2372commandwindow; %brings the Matlab command window to the front
2373
2374
2375% --- Executes on selection change in RunMode.
2376function RunMode_Callback(hObject, eventdata, handles)
Note: See TracBrowser for help on using the repository browser.