source: trunk/src/series.m @ 501

Last change on this file since 501 was 495, checked in by sommeria, 12 years ago

option 'background' set for matlab function in civ and series under windows OS

File size: 93.5 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 all the file series
590i_sum=sum(sum(i1_series,2),3);%sum of i1_series on the last index
591MaxIndex_i=max(find(i_sum>0))-1;% max ref index i
592MinIndex_i=min(find(i_sum>0))-1;% min ref index i
593i2_min=[];
594if ~isempty(i2_series)
595    i2_min=i2_series(1,2);
596end
597j1_min=[];
598if ~isempty(j1_series)
599    j1_min=j1_series(1,2);
600end
601j2_min=[];
602if ~isempty(j2_series)
603    j2_min=j2_series(1,2);
604end
605if isequal(MinIndex_i,1) &&...
606        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')
607    MinIndex_i=0;
608end
609j_sum=sum(sum(j1_series,1),3);
610MaxIndex_j=max(find(j_sum>0))-1;
611MinIndex_j=min(find(j_sum>0))-1;
612MinIndex=get(handles.MinIndex,'Data');%retrieve the min indices in the table MinIndex
613MaxIndex=get(handles.MaxIndex,'Data');%retrieve the max indices in the table MaxIndex
614MinIndex{iview,1}=MinIndex_i;
615MinIndex{iview,2}=MinIndex_j;
616MaxIndex{iview,1}=MaxIndex_i;
617MaxIndex{iview,2}=MaxIndex_j;
618
619set(handles.MinIndex,'Data',MinIndex)%display the min indices in the table MinIndex
620set(handles.MaxIndex,'Data',MaxIndex)%display the max indices in the table MaxIndex
621
622%% adjust the first and last indices if requested by the bounds
623first_i=str2num(get(handles.num_first_i,'String'));
624ref_i=str2num(get(handles.num_ref_i,'String'));
625ref_j=str2num(get(handles.num_ref_j,'String'));
626if isempty(first_i)
627    first_i=ref_i;
628elseif first_i < MinIndex_i
629    first_i=MinIndex_i;
630end
631first_j=str2num(get(handles.num_first_j,'String'));
632if isempty(first_j)
633    first_j=ref_j;
634elseif first_j<MinIndex_j
635    first_j=MinIndex_j;
636end
637last_i=str2num(get(handles.num_last_i,'String'));
638if isempty(last_i)
639    last_i=ref_i;
640elseif last_i > MaxIndex_i
641    last_i=MaxIndex_i;
642end
643last_j=str2num(get(handles.num_first_j,'String'));
644if isempty(last_j)
645    last_j=ref_j;
646elseif last_j>MaxIndex_j
647    last_j=MaxIndex_j;
648end
649set(handles.num_first_i,'String',num2str(first_i));
650set(handles.num_first_j,'String',num2str(first_j));
651set(handles.num_last_i,'String',num2str(last_i));
652set(handles.num_last_j,'String',num2str(last_j));
653
654%% read timing and total frame number from the current file (movie files) !! may be overrid by xml file
655InputTable=get(handles.InputTable,'Data');
656FileBase=fullfile(InputTable{iview,1},InputTable{iview,3});
657time=[];%default
658% case of movies
659if strcmp(InputTable{iview,4},'*')
660    if ~isempty(VideoObject)
661        imainfo=get(VideoObject);
662        time=(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames-1)/imainfo.FrameRate)';
663        set(handles.Dt_txt,'String',['Dt=' num2str(1000/imainfo.FrameRate) 'ms']);%display the elementary time interval in millisec
664        ColorType='truecolor';
665    elseif ~isempty(imformats(regexprep(InputTable{iview,5},'^.',''))) || isequal(InputTable{iview,5},'.vol')%&& isequal(NomType,'*')% multi-frame image
666        if ~isempty(InputTable{iview,2})
667            imainfo=imfinfo(fullfile(InputTable{iview,1},InputTable{iview,2},[InputTable{iview,3} InputTable{iview,5}]));
668        else
669            imainfo=imfinfo([FileBase InputTable{iview,5}]);
670        end
671        ColorType=imainfo.ColorType;%='truecolor' for color images
672        if length(imainfo) >1 %case of image with multiple frames
673            nbfield=length(imainfo);
674            nbfield_j=1;
675        end
676    end
677end
678
679%%  read image documentation file  if found%%%%%%%%%%%%%%%%%%%%%%%%%%%
680ext_imadoc='';
681if exist([FileBase '.xml'],'file')
682    ext_imadoc='.xml';
683elseif exist([FileBase '.civ'],'file')
684    ext_imadoc='.civ';
685end
686%read the ImaDoc file
687XmlData=[];
688NbSlice_calib={};
689if isequal(ext_imadoc,'.xml')
690        [XmlData,warntext]=imadoc2struct([FileBase '.xml']);
691        if isfield(XmlData,'Heading') && isfield(XmlData.Heading,'ImageName') && ischar(XmlData.Heading.ImageName)
692            [PP,FF,ext_ima_read]=fileparts(XmlData.Heading.ImageName);
693        end
694        if isfield(XmlData,'Time')
695            time=XmlData.Time;
696        end
697        if isfield(XmlData,'Camera')
698            if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
699                NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
700                if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
701                    msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
702                end
703            end
704            if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
705                TimeUnit=XmlData.Camera.TimeUnit;
706            end
707        end
708        if ~isempty(warntext)
709            msgbox_uvmat('WARNING',warntext)
710        end 
711elseif isequal(ext_imadoc,'.civ')
712    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
713    time=XmlData.Time;
714    if error==2, warntext=['no file ' FileBase '.civ'];
715    elseif error==1, warntext='inconsistent number of fields in the .civ file';
716    end 
717end
718
719%% update time table
720if ~isempty(time)
721TimeTable=get(handles.TimeTable,'Data');
722first_i=str2num(get(handles.num_first_i,'String'));
723last_i=str2num(get(handles.num_last_i,'String'));
724first_j=str2num(get(handles.num_first_j,'String'));
725last_j=str2num(get(handles.num_last_j,'String'));
726MinIndexTable=get(handles.MinIndex,'Data');
727MinIndex_i=MinIndexTable{iview,1};
728MinIndex_j=MinIndexTable{iview,2};
729MaxIndexTable=get(handles.MaxIndex,'Data');
730MaxIndex_i=MaxIndexTable{iview,1};
731MaxIndex_j=MaxIndexTable{iview,2};
732if isempty(MinIndex_j)
733    if MinIndex_i>0
734    TimeTable{iview,1}=time(MinIndex_i);
735    end
736    TimeTable{iview,2}=time(first_i);
737    TimeTable{iview,3}=time(last_i);
738    TimeTable{iview,4}=time(MaxIndex_i);
739elseif ~isempty(time)
740    if MinIndex_i>0
741    TimeTable{iview,1}=time(MinIndex_i,MinIndex_j);
742    end
743    TimeTable{iview,2}=time(first_i,first_j);
744    TimeTable{iview,3}=time(last_i,last_j);
745    TimeTable{iview,4}=time(MaxIndex_i,MaxIndex_j);
746end
747set(handles.TimeTable,'Data',TimeTable)
748end
749
750%% number of slices
751NbSlice=1;%default
752if isfield(XmlData,'GeometryCalib') && isfield(XmlData.GeometryCalib,'SliceCoord')
753    siz=size(XmlData.GeometryCalib.SliceCoord);
754    if siz(1)>1
755        NbSlice=siz(1);
756    end
757end
758set(handles.num_NbSlice,'String',num2str(NbSlice))
759   
760%% update pair menus
761set(handles.Pairs,'Visible','on')
762set(handles.PairString,'Visible','on')
763ListView=get(handles.ListView,'String');
764ListView{iview}=num2str(iview);
765set(handles.ListView,'String',ListView);
766set(handles.ListView,'Value',iview)
767update_mode(handles,i1_series,i2_series,j1_series,j2_series,time)
768
769%% update the series info in 'UserData'
770SeriesData=get(handles.series,'UserData');
771SeriesData.i1_series{iview}=i1_series;
772SeriesData.i2_series{iview}=i2_series;
773SeriesData.j1_series{iview}=j1_series;
774SeriesData.j2_series{iview}=j2_series;
775SeriesData.FileType{iview}=FileType;
776SeriesData.Time{iview}=time;
777set(handles.series,'UserData',SeriesData)
778
779%% enable j index visibility
780state='off';
781check_jindex=~cellfun(@isempty,SeriesData.j1_series); %look for non empty j indices
782if isempty(find(check_jindex))
783    enable_j(handles,'off') % no j index needed
784else
785    enable_j(handles,'on')
786end
787
788%% display the set of existing files as an image
789set(handles.FileStatus,'Units','pixels')
790Position=get(handles.FileStatus,'Position');
791set(handles.FileStatus,'Units','normalized')
792xI=0.5:Position(3)-0.5;
793nbview=numel(SeriesData.i1_series);
794for iview=1:nbview
795    index_min(iview)=min(find(SeriesData.i1_series{iview}(2:end,2:end,1)>0));
796    index_max(iview)=max(find(SeriesData.i1_series{iview}(2:end,2:end,1)>0));
797end
798index_min=min(index_min);
799index_max=max(index_max);
800range_index=index_max-index_min+1;
801scale_y=Position(4)/nbview;
802scale_x=Position(3)/range_index;
803x=(0.5:range_index-0.5)*Position(3)/range_index;
804% y=(0.5:nbview-0.5)*Position(4)/nbview;
805range_y=max(1,floor(Position(4)/nbview));
806CData=zeros(nbview*range_y,Position(3));
807for iview=1:nbview
808    ind_y=1+(iview-1)*range_y:iview*range_y;
809    LineData=zeros(1,range_index);
810    x_index=find(SeriesData.i1_series{iview}(2:end,2:end,1)>0)-index_min+1;
811    LineData(x_index)=1;
812    LineData=interp1(x,LineData,xI,'nearest');
813    CData(ind_y,:)=ones(size(ind_y'))*LineData;
814end
815CData=cat(3,zeros(size(CData)),CData,zeros(size(CData)));
816set(handles.FileStatus,'CData',CData);
817
818%
819%
820% xima=0.5:pos(3)-0.5;% pixel positions on the image representing the existing file indices
821% yima=0.5:pos(4)-0.5;
822% [XIma,YIma]=meshgrid(xima,yima);
823% nb_i=size(i1_series,1);
824% nb_j=size(i1_series,2);
825% ind_i=(0.5:nb_i-0.5)*pos(3)/nb_i;
826% ind_j=(0.5:nb_j-0.5)*pos(4)/nb_j;
827% [Ind_i,Ind_j]=meshgrid(ind_i,ind_j);
828% CData=zeros([size(XIma) 3]);%black color
829% file_ima=double((i1_series(:,:,1)>0)');
830% if numel(file_ima)>=2
831% if size(file_ima,1)==1
832%     CLine=interp1(ind_i,file_ima,xima,'nearest');
833%     CData(:,:,2)=ones(size(yima'))*CLine;
834% else
835%     CData(:,:,2)=interp2(Ind_i,Ind_j,file_ima,XIma,YIma,'nearest');
836% end
837% set(handles.FileStatus,'CData',CData)
838% end
839% set(handles.FileStatus,'Units','normalized')
840
841
842%% enable field and veltype menus, in accordance with the current action
843ActionName_Callback([],[], handles)
844
845%% check for pair display
846check_pairs=0;
847for iview=1:numel(SeriesData.i2_series)
848    if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
849        check_pairs=1;
850    end
851end
852if check_pairs
853    set(handles.Pairs,'Visible','on')
854    set(handles.PairString,'Visible','on')
855else
856    set(handles.Pairs,'Visible','off')
857    set(handles.PairString,'Visible','off')
858end
859
860%% set length of waitbar
861displ_time(handles)
862
863
864% %% set default options in menu 'Fields'%% TODO: check VelType
865% if ~testima
866%     testcivx=0;
867%     if isfield(UvData,'FieldsString') && isequal(UvData.FieldsString,{'get_field...'})% field menu defined as input (from get_field)
868%         set(handles_Fields,'Value',1)
869%         set(handles_Fields,'String',{'get_field...'})
870%         UvData=rmfield(UvData,'FieldsString');
871%     else
872%         Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ');
873%         if strcmp(Data.Conventions,'uvmat/civdata') ||( ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0))%if the new input is Civx
874%             FieldList=calc_field;
875%             set(handles_Fields,'String',[{'image'};FieldList;{'get_field...'}]);%standard menu for civx data
876%             set(handles_Fields,'Value',2) % set menu to 'velocity'
877%             col_vec=FieldList;
878%             col_vec(1)=[];%remove 'velocity' option for vector color (must be a scalar)
879%             testcivx=1;
880%         end
881%         if ~testcivx
882%             set(handles_Fields,'Value',1) % set menu to 'get_field...
883%             set(handles_Fields,'String',{'get_field...'})
884%             col_vec={'get_field...'};
885%         end
886%         set(handles.ColorScalar,'String',col_vec)
887%     end
888% end
889% set(handles.uvmat,'UserData',UvData)
890%
891% %% set index navigation options and refresh plots
892% scan_option='i';%default
893% state_j='off'; %default
894% if index==2
895%     if get(handles.scan_j,'Value')
896%         scan_option='j'; %keep the scan option for the second fiel series
897%     end
898%     if strcmp(get(handles.j1,'Visible'),'on')
899%         state_j='on';
900%     end
901% end
902% if ~isempty(j1_series)
903%         state_j='on';
904%         if isequal(nbfield,1) &&index==1
905%             scan_option='j'; %scan j index by default if nbfield=1               
906%         end
907% end
908% if isequal(scan_option,'i')
909%      set(handles.scan_i,'Value',1)
910%      scan_i_Callback([],[], handles);
911% else
912%      set(handles.scan_j,'Value',1)
913%      scan_j_Callback([],[], handles);
914% end
915% set(handles.scan_j,'Visible',state_j)
916% set(handles.j1,'Visible',state_j)
917% set(handles.j2,'Visible',state_j)
918% set(handles.last_j,'Visible',state_j);
919% set(handles.frame_j,'Visible',state_j);
920% set(handles.j_text,'Visible',state_j);
921% if ~isempty(i2_series)||~isempty(j2_series)
922%     set(handles.CheckFixPair,'Visible','on')
923% elseif index==1
924%     set(handles.CheckFixPair,'Visible','off')
925% end
926%
927%
928% mode_Callback(hObject, eventdata, handles)
929%
930% set(handles.REFRESH,'BackgroundColor',[0.7 0.7 0.7])
931% InputTable=get(handles.InputTable,'Data');
932% check_lines=get(handles.REFRESH,'UserData');
933%
934% %% check the indices and FileTypes for each series (limited to the new ones to save time)
935% for ind_list=1:length(check_lines)
936%     if  check_lines(ind_list)
937%         InputLine=InputTable(ind_list,:);
938%         detect_idem=strcmp('"',InputLine);% look for '" (repeat of previous data)
939%         detect_idem=detect_idem(detect_idem>0);
940%         if ~isempty (detect_idem)
941%             InputLine(detect_idem)=InputTable(ind_list-1,detect_idem);
942%             set(handles.InputTable,'Data',InputTable)
943%         end
944%         fileinput=fullfile_uvmat(InputLine{1},InputLine{2},InputLine{3},InputLine{5},InputLine{4},1,2,1,2);
945%         %fileinput=name_generator(fullfile(InputLine{1},InputLine{3}),1,1,InputLine{5},InputLine{4},1,2,2,InputLine{2})
946%         %update file series defined by the selected line
947%         [InputTable{ind_list,3},InputTable{(ind_list),4},errormsg]=update_indices(handles,fileinput,ind_list);
948%         if ~isempty(errormsg)
949%                 msgbox_uvmat('ERROR',errormsg)
950%                 return
951%         end
952%     end
953% end
954% set(handles.InputTable,'Data',InputTable)
955% SeriesData=get(handles.series,'UserData');
956%
957% state_j='off';
958% state_Pairs='off';
959% state_InputFields='off';
960% val=get(handles.ListView,'Value');
961% ListViewString={''};
962% if ~isempty(SeriesData)
963% %     ListViewString={};
964%     for iview=1:size(InputTable,1)
965%         if ~isempty(SeriesData.j1_series{iview})
966%             state_j='on';
967%         end
968%         if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
969%             state_Pairs='on';
970%             ListViewString{iview}=num2str(iview);
971%             if check_lines(iview)
972%                 val=iview;%select the last pair if it is a new entry
973%             end
974%         end
975%         if strcmp(SeriesData.FileType{iview},'civx')||strcmp(SeriesData.FileType{iview},'civdata')
976%             state_InputFields='on';
977%         end
978%     end
979% end
980% set(handles.ListView,'Value',val)
981% set(handles.ListView,'String',ListViewString)
982% if strcmp(state_Pairs,'on')
983%     ListView_Callback(hObject,eventdata,handles)
984% end
985% set(handles.PairString,'Visible',state_Pairs)
986% enable_j(handles,state_j)
987
988%------------------------------------------------------------------------
989function num_first_i_Callback(hObject, eventdata, handles)
990%------------------------------------------------------------------------
991num_last_i_Callback(hObject, eventdata, handles)
992
993%------------------------------------------------------------------------
994function num_last_i_Callback(hObject, eventdata, handles)
995%------------------------------------------------------------------------
996SeriesData=get(handles.series,'UserData');
997if ~isfield(SeriesData,'Time')
998    SeriesData.Time{1}=[];
999end
1000displ_time(handles);
1001
1002%------------------------------------------------------------------------
1003function num_first_j_Callback(hObject, eventdata, handles)
1004%------------------------------------------------------------------------
1005 num_last_j_Callback(hObject, eventdata, handles)
1006
1007%------------------------------------------------------------------------
1008function num_last_j_Callback(hObject, eventdata, handles)
1009%------------------------------------------------------------------------
1010first_j=str2num(get(handles.num_first_j,'String'));
1011last_j=str2num(get(handles.num_last_j,'String'));
1012ref_j=ceil((first_j+last_j)/2);
1013set(handles.num_ref_j,'String', num2str(ref_j))
1014num_ref_j_Callback(hObject, eventdata, handles)
1015SeriesData=get(handles.series,'UserData');
1016if ~isfield(SeriesData,'Time')
1017    SeriesData.Time{1}=[];
1018end
1019displ_time(handles);
1020
1021
1022%------------------------------------------------------------------------
1023% ---- find the times corresponding to the first and last indices of a series
1024function displ_time(handles)
1025%------------------------------------------------------------------------
1026SeriesData=get(handles.series,'UserData');%
1027ref_i=[str2num(get(handles.num_first_i,'String')) str2num(get(handles.num_last_i,'String'))];
1028ref_j=[str2num(get(handles.num_first_j,'String')) str2num(get(handles.num_last_j,'String'))];
1029TimeTable=get(handles.TimeTable,'Data');
1030Pairs=get(handles.PairString,'Data');
1031for iview=1:size(TimeTable,1)
1032    if size(SeriesData.Time,1)<iview
1033        break
1034    end
1035    i1=ref_i;
1036    j1=ref_j;
1037    i2=ref_i;
1038    j2=ref_j;
1039    % case of pairs
1040    if ~isempty(Pairs{iview,1})
1041        r=regexp(Pairs{iview,1},'(?<mode>(Di=)|(Dj=)) -*(?<num1>\d+)\|(?<num2>\d+)','names');
1042        if isempty(r)
1043            r=regexp(Pairs{iview,1},'(?<num1>\d+)(?<mode>-)(?<num2>\d+)','names');
1044        end
1045        switch r.mode
1046            case 'Di='  %  case 'series(Di)')
1047                i1=ref_i-str2num(r.num1);
1048                i2=ref_i+str2num(r.num2);
1049            case 'Dj='  %  case 'series(Dj)'
1050                j1=ref_j-str2num(r.num1);
1051                j2=ref_j+str2num(r.num2);
1052            case '-'  % case 'bursts'
1053                j1=str2num(r.num1)*ones(size(ref_i));
1054                j2=str2num(r.num2)*ones(size(ref_i));
1055        end
1056    end
1057    TimeTable{iview,2}=[];
1058    TimeTable{iview,3}=[];
1059    if size(SeriesData.Time{iview},1)>=i2(2)&&size(SeriesData.Time{iview},1)>=j2(2)
1060        if isempty(ref_j)
1061            time_first=(SeriesData.Time{iview}(i1(1))+SeriesData.Time{iview}(i2(1)))/2;
1062            time_last=(SeriesData.Time{iview}(i1(2))+SeriesData.Time{iview}(i2(2)))/2;
1063        else
1064            time_first=(SeriesData.Time{iview}(i1(1),j1(1))+SeriesData.Time{iview}(i2(1),j2(1)))/2;
1065            time_last=(SeriesData.Time{iview}(i1(2),j1(2))+SeriesData.Time{iview}(i2(2),j2(2)))/2;
1066        end
1067        TimeTable{iview,2}=time_first; %TODO: take into account pairs
1068        TimeTable{iview,3}=time_last; %TODO: take into account pairs
1069    end
1070end
1071set(handles.TimeTable,'Data',TimeTable)
1072
1073%% set the waitbar position with respect to the min and max in the series
1074% for iview=1:numel(SeriesData.i1_series)
1075% [tild,index_min(iview)]=min(SeriesData.i1_series{iview}(SeriesData.i1_series{iview}>0));
1076% [tild,index_max(iview)]=max(SeriesData.i1_series{iview}(SeriesData.i1_series{iview}>0));
1077% end
1078for iview=1:numel(SeriesData.i1_series)
1079    index_min(iview)=min(find(SeriesData.i1_series{iview}(2:end,2:end,1)>0));
1080    index_max(iview)=max(find(SeriesData.i1_series{iview}(2:end,2:end,1)>0));
1081end
1082[index_min,iview_min]=min(index_min);
1083[index_max,iview_max]=min(index_max);
1084index_first=(ref_i(1)-1)*(size(SeriesData.i1_series{iview_min},2)-1)+ref_j(1);
1085index_last=(ref_i(2)-1)*(size(SeriesData.i1_series{iview_max},2)-1)+ref_j(2);
1086range=index_max-index_min+1;
1087coeff_min=(index_first-index_min)/range;
1088coeff_max=(index_last-index_min+1)/range;
1089Position=get(handles.Waitbar,'Position');
1090Position_status=get(handles.FileStatus,'Position');
1091Position(1)=coeff_min*Position_status(3)+Position_status(1);
1092Position(3)=Position_status(3)*(coeff_max-coeff_min);
1093set(handles.Waitbar,'Position',Position)
1094update_waitbar(handles.Waitbar,0)
1095
1096%------------------------------------------------------------------------
1097% --- Executes when selected cell(s) is changed in PairString.
1098function PairString_CellSelectionCallback(hObject, eventdata, handles)
1099%------------------------------------------------------------------------   
1100set(handles.ListView,'Value',eventdata.Indices(1))% detect the selected raw index
1101ListView_Callback ([],[],handles) % update the list of available pairs
1102
1103%------------------------------------------------------------------------
1104%------------------------------------------------------------------------
1105%  III - FUNCTIONS ASSOCIATED TO THE FRAME SET PAIRS
1106%------------------------------------------------------------------------
1107%------------------------------------------------------------------------
1108% --- Executes on selection change in ListView.
1109function ListView_Callback(hObject, eventdata, handles)
1110%------------------------------------------------------------------------   
1111SeriesData=get(handles.series,'UserData');
1112i2_series=[];
1113j2_series=[];
1114iview=get(handles.ListView,'Value');
1115if ~isempty(SeriesData.i2_series{iview})
1116    i2_series=SeriesData.i2_series{iview};
1117end
1118if ~isempty(SeriesData.j2_series{iview})
1119    j2_series=SeriesData.j2_series{iview};
1120end
1121update_mode(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1122    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1123
1124%------------------------------------------------------------------------
1125% --- Executes on button press in mode.
1126function mode_Callback(hObject, eventdata, handles)
1127%------------------------------------------------------------------------       
1128SeriesData=get(handles.series,'UserData');
1129iview=get(handles.ListView,'Value');
1130mode_list=get(handles.mode,'String');
1131mode=mode_list{get(handles.mode,'Value')};
1132if isequal(mode,'bursts')
1133    enable_i(handles,'On')
1134    enable_j(handles,'Off') %do not display j index scanning in burst mode (j is fixed by the burst choice)
1135else
1136    enable_i(handles,'On')
1137    enable_j(handles,'Off')
1138end
1139fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1140    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1141ListPairs_Callback([],[],handles)
1142
1143%-------------------------------------------------------------
1144% --- Executes on selection in ListPairs.
1145function ListPairs_Callback(hObject,eventdata,handles)
1146%------------------------------------------------------------
1147list_pair=get(handles.ListPairs,'String');%get the menu of image pairs
1148if isempty(list_pair)
1149    string='';
1150else
1151    string=list_pair{get(handles.ListPairs,'Value')};
1152    string=regexprep(string,',.*','');%removes time indication (after ',')
1153end
1154PairString=get(handles.PairString,'Data');
1155iview=get(handles.ListView,'Value');
1156PairString{iview,1}=string;
1157% report the selected pair string to the table PairString
1158set(handles.PairString,'Data',PairString)
1159
1160%------------------------------------------------------------------------
1161function num_ref_i_Callback(hObject, eventdata, handles)
1162%------------------------------------------------------------------------
1163mode_list=get(handles.mode,'String');
1164mode=mode_list{get(handles.mode,'Value')};
1165SeriesData=get(handles.series,'UserData');
1166iview=get(handles.ListView,'Value');
1167fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1168    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview});% update the menu of pairs depending on the available netcdf files
1169ListPairs_Callback([],[],handles)
1170
1171%------------------------------------------------------------------------
1172function num_ref_j_Callback(hObject, eventdata, handles)
1173%------------------------------------------------------------------------
1174num_ref_i_Callback(hObject, eventdata, handles)
1175
1176%------------------------------------------------------------------------
1177function update_mode(handles,i1_series,i2_series,j1_series,j2_series,time)
1178%------------------------------------------------------------------------   
1179check_burst=0;
1180if isempty(j2_series)% no pair menu to display
1181    if isempty(i2_series)
1182        set(handles.mode,'String',{''})
1183    else
1184        set(handles.mode,'Value',1)
1185        set(handles.mode,'String',{'series(Di)'})
1186    end
1187else
1188    nbfield=size(j2_series,1);
1189    nbfield2=size(j2_series,2);
1190    set(handles.mode,'String',{'bursts';'series(Dj)'})
1191    if nbfield2>10 || nbfield==1
1192        set(handles.mode,'Value',2);%set mode to series(Dj) if more than 10 j values
1193    else
1194        set(handles.mode,'Value',1);
1195        check_burst=1;
1196    end
1197end
1198if check_burst
1199    enable_i(handles,'On')
1200    enable_j(handles,'Off') %do not display j index scanning in burst mode (j is fixed by the burst choice)
1201else
1202    enable_i(handles,'On')
1203    if isempty(j1_series)
1204         enable_j(handles,'Off')
1205    else
1206        enable_j(handles,'On')
1207    end
1208end
1209fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1210ListPairs_Callback([],[],handles)
1211
1212%--------------------------------------------------------------
1213% determine the menu for civ1 pairstring depending on existing netcdf files
1214% with the reference indices num_ref_i and num_ref_j
1215%----------------------------------------------------------------
1216function fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1217
1218mode_list=get(handles.mode,'String');
1219mode=mode_list{get(handles.mode,'Value')};
1220ref_i=str2num(get(handles.num_ref_i,'String'));
1221if isempty(ref_i)
1222    ref_i=1;
1223end
1224if strcmp(get(handles.num_ref_j,'Visible'),'on')
1225    ref_j=str2num(get(handles.num_ref_j,'String'));
1226    if isempty(ref_j)
1227        ref_j=1;
1228    end
1229else
1230    ref_j=1;
1231end
1232TimeUnit=get(handles.TimeUnit,'String');
1233if length(TimeUnit)>=1
1234    dtunit=['m' TimeUnit];
1235else
1236    dtunit='e-03';
1237end
1238
1239displ_pair={};
1240if strcmp(mode,'series(Di)')
1241    if isempty(i2_series)
1242        msgbox_uvmat('ERROR','no i1-i2 pair available')
1243        return
1244    end
1245    diff_i=i2_series-i1_series;
1246    min_diff=min(diff_i(diff_i>0));
1247    max_diff=max(diff_i(diff_i>0));
1248    for ipair=min_diff:max_diff
1249        if numel(diff_i(diff_i==ipair))>0
1250            pair_string=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1251            if ~isempty(time)
1252                if ref_i<=floor(ipair/2)
1253                    ref_i=floor(ipair/2)+1;% shift ref_i to get the first pair
1254                end
1255                Dt=time(ref_i+ceil(ipair/2),ref_j)-time(ref_i-floor(ipair/2),ref_j);
1256                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1257            end
1258            displ_pair=[displ_pair;{pair_string}];
1259        end
1260    end
1261    if ~isempty(displ_pair)
1262        displ_pair=[displ_pair;{'Di=*|*'}];
1263    end
1264elseif strcmp(mode,'series(Dj)')
1265    if isempty(j2_series)
1266        msgbox_uvmat('ERROR','no j1-j2 pair available')
1267        return
1268    end
1269    diff_j=j2_series-j1_series;
1270    min_diff=min(diff_j(diff_j>0));
1271    max_diff=max(diff_j(diff_j>0));
1272    for ipair=min_diff:max_diff
1273        if numel(diff_j(diff_j==ipair))>0
1274            pair_string=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1275            if ~isempty(time)
1276                if ref_j<=floor(ipair/2)
1277                    ref_j=floor(ipair/2)+1;% shift ref_i to get the first pair
1278                end
1279                Dt=time(ref_i,ref_j+ceil(ipair/2))-time(ref_i,ref_j-floor(ipair/2));
1280                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1281            end
1282            displ_pair=[displ_pair;{pair_string}];
1283        end
1284    end
1285    if ~isempty(displ_pair)
1286        displ_pair=[displ_pair;{'Dj=*|*'}];
1287    end
1288elseif strcmp(mode,'bursts')
1289    if isempty(j2_series)
1290        msgbox_uvmat('ERROR','no j1-j2 pair available')
1291        return
1292    end
1293    diff_j=j2_series-j1_series;
1294    min_j1=min(j1_series(j1_series>0));
1295    max_j1=max(j1_series(j1_series>0));
1296    min_j2=min(j2_series(j2_series>0));
1297    max_j2=max(j2_series(j2_series>0));
1298    for pair1=min_j1:min(max_j1,min_j1+20)
1299        for pair2=min_j2:min(max_j2,min_j2+20)
1300        if numel(j1_series(j1_series==pair1))>0 && numel(j2_series(j2_series==pair2))>0
1301            displ_pair=[displ_pair;{['j= ' num2str(pair1) '-' num2str(pair2)]}];
1302        end
1303        end
1304    end
1305    if ~isempty(displ_pair)
1306        displ_pair=[displ_pair;{'j=*-*'}];
1307    end
1308end
1309set(handles.num_ref_i,'String',num2str(ref_i)) % update ref_i and ref_j
1310set(handles.num_ref_j,'String',num2str(ref_j))
1311
1312%% display list of pairstring
1313displ_pair_list=get(handles.ListPairs,'String');
1314NewVal=[];
1315if ~isempty(displ_pair_list)
1316Val=get(handles.ListPairs,'Value');
1317NewVal=find(strcmp(displ_pair_list{Val},displ_pair),1);% look at the previous display in the new menu displ_pï¿œir
1318end
1319if ~isempty(NewVal)
1320    set(handles.ListPairs,'Value',NewVal)
1321else
1322    set(handles.ListPairs,'Value',1)
1323end
1324set(handles.ListPairs,'String',displ_pair)
1325
1326%-------------------------------------
1327function enable_i(handles,state)
1328set(handles.i_txt,'Visible',state)
1329set(handles.num_first_i,'Visible',state)
1330set(handles.num_last_i,'Visible',state)
1331set(handles.num_incr_i,'Visible',state)
1332% set(handles.num_MaxIndex_i,'Visible',state)
1333set(handles.num_ref_i,'Visible',state)
1334set(handles.ref_i_text,'Visible',state)
1335
1336%-----------------------------------
1337function enable_j(handles,state)
1338set(handles.j_txt,'Visible',state)
1339% set(handles.num_MinIndex_j,'Visible',state)
1340set(handles.num_first_j,'Visible',state)
1341set(handles.num_last_j,'Visible',state)
1342set(handles.num_incr_j,'Visible',state)
1343% set(handles.num_MaxIndex_j,'Visible',state)
1344set(handles.num_ref_j,'Visible',state)
1345set(handles.ref_j_text,'Visible',state)
1346
1347%-----------------------------------
1348% function view_FieldMenu(handles,state)
1349% % set(handles.FieldName,'Visible',state)
1350% % set(handles.Field_text,'Visible',state)
1351% set(handles.InputFields,'Visible',state)
1352
1353% %-----------------------------------
1354% function view_FieldMenu_1(handles,state)
1355% set(handles.FieldName_1,'Visible',state)
1356% set(handles.Field_text_1,'Visible',state)
1357
1358
1359%%%%%%%%%%%%%%%%%%%%
1360%%  MAIN ActionName FUNCTIONS
1361%%%%%%%%%%%%%%%%%%%%
1362%------------------------------------------------------------------------
1363% --- Executes on button press in RUN.
1364function RUN_Callback(hObject, eventdata, handles)
1365%------------------------------------------------------------------------
1366set(handles.RUN,'BusyAction','queue');
1367set(0,'CurrentFigure',handles.series)
1368set(handles.RUN, 'Enable','Off')
1369set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1370drawnow
1371[h_fun,Series,filexml,errormsg]=prepare_jobs(handles);
1372if ~isempty(errormsg)
1373    msgbox_uvmat('ERROR',errormsg)
1374    return
1375end
1376RunModeList=get(handles.RunMode,'String');
1377RunMode=RunModeList{get(handles.RunMode,'Value')};
1378
1379switch RunMode
1380    case 'local'
1381        Series=h_fun(Series);
1382        if ~isempty(filexml)
1383            t=struct2xml(Series);
1384            t=set(t,1,'name','Series');
1385            save(t,filexml);
1386        end
1387    case 'background'
1388        if isempty(filexml)
1389            Series=h_fun(Series);% no background in the absence of output file
1390        else
1391            % update the xml file after interactive input with the function
1392            Series.Specific='?';
1393            Series=h_fun(Series);
1394            t=struct2xml(Series);
1395            t=set(t,1,'name','Series');
1396            save(t,filexml);
1397            path_uvmat=fileparts(which('uvmat'));
1398           
1399            filename_bat=regexprep(filexml,'.xml$','.bat');
1400            [fid,message]=fopen(filename_bat,'w');
1401            if isequal(fid,-1)
1402                msgbox_uvmat('ERROR', ['creation of .bat file: ' message]);
1403                return
1404            end
1405            path_fct=get(handles.ActionPath,'String');
1406            filelog=regexprep(filexml,'.xml$','.log');
1407       
1408            switch computer
1409                case {'GLNX86','GLNXA64','MACI64'}
1410                    text_matlabscript=[...
1411                        '#!/bin/bash \n'...
1412                        '. /etc/sysprofile \n'...
1413                        'matlab -nodisplay -nosplash -nojvm -logfile ''' filelog ''' <<END_MATLAB \n'...
1414                        'addpath(''' path_uvmat '''); \n'...
1415                        'addpath(''' Series.Action.ActionPath '''); \n'...
1416                        '' Series.Action.ActionName  '( ''' filexml '''); \n'...
1417                        'exit \n'...
1418                        'END_MATLAB \n'];
1419                    fprintf(fid,text_matlabscript);
1420                    fclose(fid);
1421                    system(['chmod +x ' filename_bat]);% set the file to executable
1422                    system(['. ' filename_bat ' &']);%execute fct
1423                   
1424                case {'PCWIN','PCWIN64'}
1425                    text_matlabscript=['matlab -automation -logfile ' regexprep(filelog,'\\','\\\\')...
1426                        ' -r "addpath(''' regexprep(path_uvmat,'\\','\\\\') ''');'...
1427                        'addpath(''' regexprep(Series.Action.ActionPath,'\\','\\\\') ''');'...
1428                        '' Series.Action.ActionName  '( ''' regexprep(filexml,'\\','\\\\') ''');exit"'];
1429                    fprintf(fid,text_matlabscript);
1430                    fclose(fid);
1431                    dos([filename_bat ' &']);
1432            end
1433        end
1434        update_waitbar(handles.Waitbar,1); % put the waitbar to end position to indicate lounching is finished
1435end
1436
1437set(handles.RUN, 'Enable','On')
1438set(handles.RUN,'BackgroundColor',[1 0 0])
1439
1440%------------------------------------------------------------------------
1441function STOP_Callback(hObject, eventdata, handles)
1442%------------------------------------------------------------------------
1443set(handles.RUN, 'BusyAction','cancel')
1444set(handles.RUN,'BackgroundColor',[1 0 0])
1445set(handles.RUN,'enable','on')
1446% set(handles.BATCH,'BackgroundColor',[1 0 0])
1447% set(handles.BATCH,'enable','on')
1448
1449%------------------------------------------------------------------------
1450% --- Executes on button press in BATCH.
1451function BATCH_Callback(hObject, eventdata, handles)
1452%------------------------------------------------------------------------   
1453
1454
1455% %------------------------------------------------------------------------
1456% % --- Executes on button press in BIN.
1457% function BIN_Callback(hObject, eventdata, handles)
1458% %------------------------------------------------------------------------
1459%     cmd=['#!/bin/bash \n '...
1460%         '#$ -cwd \n '...
1461%         'hostname && date \n '...
1462%         'umask 002 \n'...
1463%         Param.xml.CivmBin ' ' Param.xml.RunTime ' ' filename_xml ' ' OutputFile '.nc'];
1464%     
1465%------------------------------------------------------------------------
1466% --- Main launch command, called by RUN and BATCH
1467function [h_fun,Series,filexml,errormsg]=prepare_jobs(handles,run)
1468%INPUT:
1469% handles: handles of graphic objects on the GUI series
1470% run=0, just to display parameters for MenuExport/GUI config
1471% run=1 (default) prepare the computation
1472
1473%------------------------------------------------------------------------
1474h_fun=[];
1475filexml='';
1476errormsg='';
1477if ~exist('run','var')
1478    run=1;
1479end
1480%% Read parameters from series
1481Series=read_GUI(handles.series);
1482if isfield(Series,'Pairs')
1483Series=rmfield(Series,'Pairs'); %info Pairs not needed for output
1484end
1485
1486%% read index ranges
1487first_i=1;
1488last_i=1;
1489incr_i=1;
1490first_j=1;
1491last_j=1;
1492incr_j=1;
1493if isfield(Series.IndexRange,'first_i')
1494    first_i=Series.IndexRange.first_i;
1495    incr_i=Series.IndexRange.incr_i;
1496    last_i=Series.IndexRange.last_i;
1497end
1498if isfield(Series.IndexRange,'first_j')
1499    first_j=Series.IndexRange.first_j;
1500    incr_j=Series.IndexRange.incr_j;
1501    last_j=Series.IndexRange.last_j;
1502end
1503
1504%% read input file parameters and set menus
1505menu_coord_state=get(handles.TransformName,'Visible');
1506if isequal(menu_coord_state,'on')
1507    menu_index=get(handles.TransformName,'Value');
1508    transform_list=get(handles.TransformName,'UserData');
1509    Series.FieldTransform.TransformHandle=transform_list{menu_index};% transform function handles
1510end
1511
1512if last_i < first_i | last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
1513    set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1514
1515%% projection object
1516if isfield(Series,'CheckObject')
1517    if Series.CheckObject
1518        hset_object=findobj(allchild(0),'tag','set_object');
1519        Series.ProjObject=read_GUI(hset_object);
1520        CheckObject_Callback([], [], handles)
1521    end
1522else
1523    Series.CheckObject=0;
1524end
1525
1526%% get_field GUI
1527if isfield(Series,'InputFields')&&isfield(Series.InputFields,'Field')
1528    if strcmp(Series.InputFields.Field,'get_field...')
1529        hget_field=findobj(allchild(0),'name','get_field');
1530        Series.GetField=read_GUI(hget_field);
1531    end
1532end
1533
1534if ~run
1535    return
1536end
1537
1538%% defining the ActionName function handle
1539list_action=get(handles.ActionName,'String');% list menu action
1540index=get(handles.ActionName,'Value');
1541action= list_action{index}; % selected string
1542%Series.Action=action;%name of the processing programme
1543Series.hseries=handles.series; % handles to the series GUI
1544path_series=which('series');
1545list_path=get(handles.ActionName,'UserData');
1546fct_path=list_path{index}; %path stored for the function ACTION
1547if ~isequal(fct_path,path_series)
1548    eval(['spath=which(''' action ''');']) %spath = current path of the selected function ACTION
1549    if ~exist(fct_path,'dir')
1550        errormsg=['The prescribed function path ' fct_path ' does not exist'];
1551        return
1552    end
1553    if ~isequal(spath,fct_path)
1554        addpath(fct_path)% add the prescribed path if not the current one
1555    end
1556end
1557eval(['h_fun=@' action ';'])%create a function handle for ACTION
1558if ~isequal(fct_path,path_series)
1559        rmpath(fct_path)% add the prescribed path if not the current one   
1560end
1561
1562%% create the output data directory and write in it the xml file from the GUI config
1563%determine the root file corresponding to the first sub dir
1564if isfield(Series,'OutputSubDir')
1565    SubDirOut=[Series.OutputSubDir Series.OutputDirExt];
1566    SubDirOutNew=SubDirOut;
1567    iview=1;
1568    SeriesData=get(handles.series,'UserData');
1569    if size(Series.InputTable,1)>1 && isfield(SeriesData,'AllowInputSort') && isfield(SeriesData.AllowInputSort)
1570        [tild,iview]=sort(Series.InputTable(:,2)); %subdirectories sorted in alphabetical order
1571        Series.InputTable=Series.InputTable(iview,:);
1572    end
1573    detect=exist(fullfile(Series.InputTable{1,1},SubDirOutNew),'dir');% test if  the dir  already exist
1574    check_create=1; %need to create the result directory by default
1575    while detect
1576        answer=msgbox_uvmat('INPUT_Y-N',['use existing ouput directory: ' fullfile(Series.InputTable{1,1},SubDirOutNew) ', possibly delete previous data']);
1577        if isequal(answer,'Yes')
1578            detect=0;
1579            check_create=0;
1580        else
1581            r=regexp(SubDirOutNew,'(?<root>.*\D)(?<num1>\d+)$','names');%detect whether name ends by a number
1582            if isempty(r)
1583                r(1).root=[SubDirOutNew '_'];
1584                r(1).num1='0';
1585            end
1586            SubDirOutNew=[r(1).root num2str(str2num(r(1).num1)+1)];%increment the index by 1 or put 1
1587            detect=exist(fullfile(Series.InputTable{1,1},SubDirOutNew),'dir');% test if  the dir  already exists   
1588            check_create=1;
1589        end
1590    end
1591    Series.OutputDirExt=regexprep(SubDirOutNew,Series.OutputSubDir,'');
1592 %   Series.OutputSubDir=SubDirOutNew;
1593 %   Series.OutputDir=fullfile(Series.InputTable{1,1},Series.OutputSubDir);%directory set for output results
1594    Series.OutputRootFile=Series.InputTable{1,3};% the first sorted RootFile taken for output
1595    set(handles.OutputDirExt,'String',Series.OutputDirExt)
1596    % create output directory
1597    OutputDir=fullfile(Series.InputTable{1,1},[Series.OutputSubDir Series.OutputDirExt]);
1598    if check_create
1599        [tild,msg1]=mkdir(OutputDir);
1600        if ~strcmp(msg1,'')
1601            errormsg=['cannot create ' OutputDir ': ' msg1];%error message for directory creation
1602            return
1603        end
1604    end
1605    filexml=fullfile(OutputDir,[Series.InputTable{1,3} '.xml']);% name of the parameter xml file set in this directory
1606end
1607%removes redondant information
1608Series.IndexRange=rmfield(Series.IndexRange,'TimeTable');
1609Series.IndexRange=rmfield(Series.IndexRange,'MinIndex');
1610Series.IndexRange=rmfield(Series.IndexRange,'MaxIndex');
1611%removes empty lines of InputTable
1612empty_line=zeros(size(Series.InputTable,1),1);
1613for iline=1:size(Series.InputTable,1)
1614    empty_line(iline)=isequal(Series.InputTable(iline,1:3),{'','',''});
1615end
1616Series.InputTable(find(empty_line),:)=[];
1617
1618%------------------------------------------------------------------------
1619% --- Executes on selection change in ActionName.
1620function ActionName_Callback(hObject, eventdata, handles)
1621%------------------------------------------------------------------------
1622global nb_builtin_ACTION
1623list_ACTION=get(handles.ActionName,'String');% list menu fields
1624index_ACTION=get(handles.ActionName,'Value');% selected string index
1625ACTION= list_ACTION{index_ACTION}; % selected function name
1626path_series=which('series');%path to series.m
1627list_path=get(handles.ActionName,'UserData');%list of recorded paths to functions of the list ACTION
1628default_file=fullfile(list_path{end},ACTION);
1629% add a new function to the menu if the selected item is 'more...'
1630if isequal(ACTION,'more...')
1631    pathfct=fileparts(path_series);
1632    [FileName, PathName, filterindex] = uigetfile( ...
1633       {'*.m', ' (*.m)';
1634        '*.m',  '.m files '; ...
1635        '*.*', 'All Files (*.*)'}, ...
1636        'Pick a file',default_file);
1637    if length(FileName)<2
1638        return
1639    end
1640    [pp,ACTION,ext_fct]=fileparts(FileName);%(end-1:end);
1641    if ~isequal(ext_fct,'.m')
1642        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
1643        return
1644    end
1645   
1646   % insert the choice in the actionname menu
1647   menu_str=update_menu(handles.ActionName,ACTION);%new action menu in which the new item has been appended if needed
1648   index_ACTION=get(handles.ActionName,'Value');% currently selected index in the list
1649   list_path{index_ACTION}=PathName;
1650   if length(menu_str)>nb_builtin_ACTION+5; %nb_builtin=nbre of functions always remaining in the initial menu
1651       nbremove=length(menu_str)-nb_builtin_ACTION-5;
1652       menu_str(nb_builtin_ACTION+1:end-5)=[];
1653       list_path(nb_builtin_ACTION+1:end-4)=[];
1654       index_ACTION=index_ACTION-nbremove;
1655       set(handles.ActionName,'Value',index_ACTION)
1656       set(handles.ActionName,'String',menu_str)
1657   end
1658   list_path{index_ACTION}=PathName;
1659   set(handles.ActionName,'UserData',list_path);
1660   set(handles.ActionPath,'enable','inactive')% indicate that the current path is accessible (not 'off')
1661   
1662   %record the current menu in personal file profil_perso
1663   dir_perso=prefdir;
1664   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
1665   for ilist=nb_builtin_ACTION+1:length(menu_str)-1
1666       series_fct{ilist-nb_builtin_ACTION}=fullfile(list_path{ilist},[menu_str{ilist} '.m']);     
1667   end
1668   if nb_builtin_ACTION+1<=length(menu_str)-1
1669       if exist(profil_perso,'file')% && nb_builtin_ACTION+1>=length(menu_str)-1
1670           save(profil_perso,'series_fct','-append')
1671       else
1672           txt=ver('MATLAB');
1673           Release=txt.Release;
1674           relnumb=str2num(Release(3:4));
1675           if relnumb >= 14%recent relaese of Matlab
1676               save(profil_perso,'series_fct','-V6')
1677           else
1678               save(profil_perso, 'series_fct')
1679           end
1680       end
1681   end
1682end
1683
1684%check the current ActionPath to the selected function
1685PathName=list_path{index_ACTION};%current recorded path
1686set(handles.ActionPath,'String',PathName); %show the path to the senlected function
1687
1688%reinitialise the waitbar
1689update_waitbar(handles.Waitbar,0)
1690
1691%default setting for the visibility of the GUI elements
1692set(handles.num_NbSlice,'Visible','off')
1693set(handles.NbSlice_title,'Visible','off')
1694set(handles.VelType,'Visible','off');
1695set(handles.VelType_text,'Visible','off');
1696set(handles.VelType_1,'Visible','off');
1697set(handles.VelType_text_1,'Visible','off');
1698set(handles.InputFields,'Visible','off')
1699set(handles.FieldName_1,'Visible','off')
1700%view_FieldMenu_1(handles,'off')
1701set(handles.FieldTransform,'Visible','off')
1702set(handles.CheckObject,'Visible','off');
1703set(handles.ProjObject,'Visible','off');
1704set(handles.CheckMask,'Visible','off')
1705set(handles.Mask,'Visible','off')
1706set(handles.OutputDirExt,'Visible','off')
1707set(handles.OutputSubDir,'Visible','off')
1708set(handles.OutputDir_title,'Visible','off')
1709%set the displayed GUI item needed for input parameters
1710if ~isequal(path_series,PathName)
1711    addpath(PathName)
1712end
1713eval(['h_function=@' ACTION ';']);
1714try
1715    [fid,errormsg] =fopen([ACTION '.m']);
1716    InputText=textscan(fid,'%s',1,'delimiter','\n');
1717    fclose(fid)
1718    set(handles.ActionName,'ToolTipString',InputText{1}{1})% put the first line of the selected function as tooltip help
1719end
1720if ~isequal(path_series,PathName)
1721    rmpath(PathName)
1722end
1723varargout=h_function();
1724Param_list={};
1725
1726InputTable=get(handles.InputTable,'Data');
1727nbview=size(InputTable,1);
1728SeriesData=get(handles.series,'UserData');
1729nb_civ=numel(find(strcmp('civx',SeriesData.FileType)|strcmp('civdata',SeriesData.FileType)));
1730nb_netcdf=numel(find(strcmp('netcdf',SeriesData.FileType)));
1731for ilist=1:length(varargout)-1
1732    switch varargout{ilist}
1733        case 'AllowInputSort'
1734            if isequal(lower(varargout{ilist+1}),'on')% sort the input table by alphabetical order of the SubDir
1735                SeriesData.AllowInputSort=1;
1736                set(handles.series,'UserData',SeriesData)
1737            end                     
1738        case 'WholeIndexRange'
1739            if isequal(lower(varargout{ilist+1}),'on')% set by default the input index range from min to max
1740                MinIndex=get(handles.MinIndex,'Data');
1741                MaxIndex=get(handles.MaxIndex,'Data');
1742                if ~isempty(MinIndex)
1743                    set(handles.num_first_i,'String',num2str(MinIndex{1}))
1744                    set(handles.num_last_i,'String',num2str(MaxIndex{1}))
1745                    set(handles.num_incr_i,'String','1')
1746                    if size(MinIndex,2)>=2
1747                        set(handles.num_first_j,'String',num2str(MinIndex{1,2}))
1748                        set(handles.num_last_j,'String',num2str(MaxIndex{1,2}))
1749                        set(handles.num_incr_j,'String','1')
1750                    end
1751                end
1752            end           
1753        case 'NbSlice'   %hidden by default
1754            if isequal(lower(varargout{ilist+1}),'on')
1755                set(handles.num_NbSlice,'Visible','on')
1756                set(handles.NbSlice_title,'Visible','on')
1757            end
1758        case 'VelType'   %hidden by default
1759             if isequal(lower(varargout{ilist+1}),'one') || isequal(lower(varargout{ilist+1}),'two')
1760                if nb_civ>=1
1761                    set(handles.VelType,'Visible','on')
1762                    set(handles.VelType_text,'Visible','on');
1763                end
1764             end
1765            if isequal(lower(varargout{ilist+1}),'two')
1766                if nb_civ>=2
1767                    set(handles.VelType_1,'Visible','on')
1768                    set(handles.VelType_text_1,'Visible','on');
1769                end
1770            end
1771        case 'FieldName'   %hidden by default
1772            if isequal(lower(varargout{ilist+1}),'one')||isequal(lower(varargout{ilist+1}),'two')
1773                if (nb_civ+nb_netcdf)>=1
1774                 set(handles.FieldName,'Visible','on') % test for MenuBorser
1775                 set(handles.InputFields,'Visible','on')
1776                end
1777            end
1778            if isequal(lower(varargout{ilist+1}),'two')
1779                if (nb_civ+nb_netcdf)>=1
1780                set(handles.FieldName_1,'Visible','on')
1781                end
1782            end
1783        case 'FieldTransform'   %hidden by default
1784            if isequal(lower(varargout{ilist+1}),'on')
1785                set(handles.TransformName,'Enable','on')
1786                set(handles.FieldTransform,'Visible','on')
1787                TransformName_Callback([],[], handles)
1788            end
1789        case 'ProjObject'   %hidden by default
1790            if isequal(lower(varargout{ilist+1}),'on')   
1791                set(handles.CheckObject,'Visible','on')
1792                set(handles.ProjObject,'Visible','on')
1793            end
1794        case 'Mask'   %hidden by default
1795            if isequal(lower(varargout{ilist+1}),'on')   
1796                set(handles.Mask,'Visible','on')
1797                 set(handles.CheckMask,'Visible','on');
1798            end 
1799        case 'OutputDirExt'
1800            if ~isempty(varargout{ilist+1})
1801            set(handles.OutputDirExt,'String',varargout{ilist+1})
1802            set(handles.OutputDirExt,'Visible','on')
1803            set(handles.OutputSubDir,'Visible','on')
1804            set(handles.OutputDir_title,'Visible','on') 
1805            end
1806    end
1807end
1808if ~isempty(Param_list)
1809    set(handles.ParamKey,'String',Param_list)
1810    set(handles.ParamVal,'Visible','on')
1811end
1812
1813%------------------------------------------------------------------------
1814% --- Executes on selection change in FieldName.
1815function FieldName_Callback(hObject, eventdata, handles)
1816%------------------------------------------------------------------------
1817field_str=get(handles.FieldName,'String');
1818field_index=get(handles.FieldName,'Value');
1819field=field_str{field_index(1)};
1820if isequal(field,'get_field...')   
1821     hget_field=findobj(allchild(0),'name','get_field');
1822     if ~isempty(hget_field)
1823         delete(hget_field)%delete opened versions of get_field
1824     end
1825     filecell=get_file_series(read_GUI(handles.series));
1826     if exist(filecell{1,1},'file')
1827        get_field(filecell{1,1})
1828     end
1829elseif isequal(field,'more...')
1830    str=calc_field;
1831    [ind_answer,v] = listdlg('PromptString','Select a file:',...
1832                'SelectionMode','single',...
1833                'ListString',str);
1834       % edit the choice in the fields and actionname menu
1835     scalar=cell2mat(str(ind_answer));
1836     update_menu(handles.FieldName,scalar)
1837end
1838
1839%------------------------------------------------------------------------
1840% --- Executes on selection change in FieldName_1.
1841function FieldName_1_Callback(hObject, eventdata, handles)
1842%------------------------------------------------------------------------
1843field_str=get(handles.FieldName_1,'String');
1844field_index=get(handles.FieldName_1,'Value');
1845field=field_str{field_index};
1846if isequal(field,'get_field...')   
1847     hget_field=findobj(allchild(0),'name','get_field_1');
1848     if ~isempty(hget_field)
1849         delete(hget_field)
1850     end
1851     SeriesData=get(handles.series,'UserData');
1852     filename=SeriesData.CurrentInputFile_1;
1853     if exist(filename,'file')
1854        hget_field=get_field(filename);
1855        set(hget_field,'name','get_field_1')
1856     end
1857elseif isequal(field,'more...')
1858    str=calc_field;
1859    [ind_answer,v] = listdlg('PromptString','Select a file:',...
1860                'SelectionMode','single',...
1861                'ListString',str);
1862       % edit the choice in the fields and actionname menu
1863     scalar=cell2mat(str(ind_answer));
1864     update_menu(handles.FieldName_1,scalar)
1865end   
1866
1867
1868%%%%%%%%%%%%%
1869function [ind_remove]=find_pairs(dirpair,ind_i,last_i)
1870indsel=ind_i;
1871indiff=diff(ind_i); %test index increment to detect multiplets (several pairs with the same index ind_i) and holes in the series
1872indiff=[1 indiff last_i-ind_i(end)+1];%for testing gaps with the imposed bounds
1873if ~isempty(indiff)
1874    indiff2=diff(indiff);
1875    indiffp=[indiff2 1];
1876    indiffm=[1 indiff2];
1877    ind_multi_m=find((indiff==0)&(indiffm<0))-1;%indices of first members of multiplets
1878    ind_multi_p=find((indiff==0)&(indiffp>0));%indices of last members of multiplets
1879    %for each multiplet, select the most recent file
1880    ind_remove=[];
1881    for i=1:length(ind_multi_m)
1882        ind_pairs=ind_multi_m(i):ind_multi_p(i);
1883        for imulti=1:length(ind_pairs)
1884            datepair(imulti)=datenum(dirpair(ind_pairs(imulti)).date);%dates of creation
1885        end
1886        [datenew,indsort2]=sort(datepair); %sort the multiplet by creation date
1887        ind_s=indsort2(1:end-1);%
1888        ind_remove=[ind_remove ind_pairs(ind_s)];%remove these indices, leave the last one
1889    end
1890end
1891
1892%------------------------------------------------------------------------
1893% --- determine the list of index pairstring of processing file
1894function [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)
1895%------------------------------------------------------------------------
1896num_i1=num_i;% set of first image numbers by default
1897num_i2=num_i;
1898num_j1=num_j;
1899num_j2=num_j;
1900num_i_out=num_i;
1901num_j_out=num_j;
1902% if isequal (NomType,'_1-2_1') || isequal (NomType,'_1-2')
1903if isequal(mode,'series(Di)')
1904    num_i1_line=num_i+ind_shift(3);% set of first image numbers
1905    num_i2_line=num_i+ind_shift(4);
1906    % adjust the first and last field number
1907        indsel=find(num_i1_line >= 1);
1908    num_i_out=num_i(indsel);
1909    num_i1_line=num_i1_line(indsel);
1910    num_i2_line=num_i2_line(indsel);
1911    num_j1=meshgrid(num_j,ones(size(num_i1_line)));
1912    num_j2=meshgrid(num_j,ones(size(num_i1_line)));
1913    [xx,num_i1]=meshgrid(num_j,num_i1_line);
1914    [xx,num_i2]=meshgrid(num_j,num_i2_line);
1915elseif isequal (mode,'series(Dj)')||isequal (mode,'bursts')
1916    if isequal(mode,'bursts') %case of bursts (png_old or png_2D)
1917        num_j1=ind_shift(1)*ones(size(num_i));
1918        num_j2=ind_shift(2)*ones(size(num_i));
1919    else
1920        num_j1_col=num_j+ind_shift(1);% set of first image numbers
1921        num_j2_col=num_j+ind_shift(2);
1922        % adjust the first field number
1923        indsel=find((num_j1_col >= 1));   
1924        num_j_out=num_j(indsel);
1925        num_j1_col=num_j1_col(indsel);
1926        num_j2_col=num_j2_col(indsel);
1927        [num_i1,num_j1]=meshgrid(num_i,num_j1_col);
1928        [num_i2,num_j2]=meshgrid(num_i,num_j2_col);
1929    end   
1930end
1931
1932%------------------------------------------------------------------------
1933% --- Executes on button press in CheckObject.
1934function CheckObject_Callback(hObject, eventdata, handles)
1935%------------------------------------------------------------------------
1936% SeriesData=get(handles.series,'UserData');
1937value=get(handles.CheckObject,'Value');
1938if value
1939     set(handles.CheckObject,'BackgroundColor',[1 1 0])%put unactivated buttons to yellow
1940     hset_object=findobj(allchild(0),'tag','set_object');%find the set_object interface handle
1941     if ishandle(hset_object)
1942         uistack(hset_object,'top')% show the GUI set_object if opened
1943     else
1944         %get the object file
1945         InputTable=get(handles.InputTable,'Data');
1946         defaultname=InputTable{1,1};
1947         if isempty(defaultname)
1948            defaultname={''};
1949         end
1950        [FileName, PathName, filterindex] = uigetfile( ...
1951       {'*.xml;*.mat', ' (*.xml,*.mat)';
1952       '*.xml',  '.xml files '; ...
1953        '*.mat',  '.mat matlab files '}, ...
1954        'Pick an xml object file (or use uvmat to create it)',defaultname);
1955        fileinput=[PathName FileName];%complete file name
1956        sizf=size(fileinput);
1957        if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
1958        %read the file
1959        data=xml2struct(fileinput);
1960        if ~isfield(data,'Type')
1961             msgbox_uvmat('ERROR',[fileinput ' is not an object xml file'])
1962             return
1963        end
1964        if ~isfield(data,'ProjMode')
1965             data.ProjMode='none';
1966        end
1967        hset_object=set_object(data);% call the set_object interface
1968     end
1969     Object=read_GUI(hset_object);
1970     set(handles.ProjObject,'String',Object.Name);%display the object name
1971else
1972    set(handles.CheckObject,'BackgroundColor',[0.7 0.7 0.7])%put activated buttons to green
1973end
1974%set(handles.series,'UserData',SeriesData)
1975
1976%--------------------------------------------------------------
1977function CheckMask_Callback(hObject, eventdata, handles)
1978value=get(handles.CheckMask,'Value');
1979if value
1980    msgbox_uvmat('ERROR','not implemented yet')
1981end
1982%--------------------------------------------------------------
1983
1984%-------------------------------------------------------------------
1985%'uv_ncbrowser': interactively calls the netcdf file browser 'get_field.m'
1986function ncbrowser_uvmat(hObject, eventdata)
1987%-------------------------------------------------------------------
1988     bla=get(gcbo,'String');
1989     ind=get(gcbo,'Value');
1990     filename=cell2mat(bla(ind));
1991      blank=find(filename==' ');
1992      filename=filename(1:blank-1);
1993     get_field(filename)
1994
1995% ------------------------------------------------------------------
1996function MenuHelp_Callback(hObject, eventdata, handles)
1997%-------------------------------------------------------------------
1998path_to_uvmat=which ('uvmat');% check the path of uvmat
1999pathelp=fileparts(path_to_uvmat);
2000helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
2001if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
2002else
2003    addpath (fullfile(pathelp,'uvmat_doc'))
2004    web([helpfile '#series'])
2005end
2006
2007%-------------------------------------------------------------------
2008% --- Executes on selection change in TransformName.
2009function TransformName_Callback(hObject, eventdata, handles)
2010%-------------------------------------------------------------------
2011global nb_transform
2012
2013menu=get(handles.TransformName,'String');
2014ind_coord=get(handles.TransformName,'Value');
2015coord_option=menu{ind_coord};
2016list_transform=get(handles.TransformName,'UserData');
2017ff=functions(list_transform{end});
2018if isequal(coord_option,'more...');
2019    coord_fct='';
2020    prompt = {'Enter the name of the transform function'};
2021    dlg_title = 'user defined transform';
2022    num_lines= 1;
2023    [FileName, PathName, filterindex] = uigetfile( ...
2024       {'*.m', ' (*.m)';
2025        '*.m',  '.m files '; ...
2026        '*.*', 'All Files (*.*)'}, ...
2027        'Pick a file', ff.file);
2028    if isequal(PathName(end),'/')||isequal(PathName(end),'\')
2029        PathName(end)=[];
2030    end
2031    transform_selected =fullfile(PathName,FileName);
2032    if ~exist(transform_selected,'file')
2033          return
2034    end
2035    [ppp,transform,xt_fct]=fileparts(FileName);% removes extension .m
2036    if ~isequal(ext_fct,'.m')
2037        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
2038        return
2039    end
2040   menu=update_menu(handles.TransformName,transform);%add the selected fct to the menu
2041   ind_coord=get(handles.TransformName,'Value');
2042   addpath(PathName)
2043   list_transform{ind_coord}=str2func(transform);% create the function handle corresponding to the newly seleced function
2044   set(handles.TransformName,'UserData',list_transform)
2045   rmpath(PathName)
2046   % save the new menu in the personal file 'uvmat_perso.mat'
2047   dir_perso=prefdir;%personal Matalb directory
2048   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2049   if exist(profil_perso,'file')
2050       for ilist=nb_transform+1:numel(list_transform)
2051           ff=functions(list_transform{ilist});
2052           transform_fct{ilist-nb_transform}=ff.file;
2053       end
2054        save (profil_perso,'transform_fct','-append'); %store the root name for future opening of uvmat
2055   end
2056end
2057
2058%check the current ActionPath to the selected function
2059if ~isempty(list_transform{ind_coord})
2060    func=functions(list_transform{ind_coord});
2061    set(handles.TransformPath,'String',fileparts(func.file)); %show the path to the senlected function
2062else
2063    set(handles.TransformPath,'String',''); %show the path to the senlected function
2064end
2065
2066% %------------------------------------------------------------------------
2067% % --- Executes on button press in REFRESH.
2068%     function REFRESH_INDICES_Callback(hObject, eventdata, handles)
2069% %------------------------------------------------------------------------       
2070% % hObject    handle to REFRESH (see GCBO)
2071% % eventdata  reserved - to be defined in a future version of MATLAB
2072% % handles    structure with handles and user data (see GUIDATA)
2073% set(handles.REFRESH,'BackgroundColor',[0.7 0.7 0.7])
2074% InputTable=get(handles.InputTable,'Data');
2075% check_lines=get(handles.REFRESH,'UserData');
2076%
2077% %% check the indices and FileTypes for each series (limited to the new ones to save time)
2078% for ind_list=1:length(check_lines)
2079%     if  check_lines(ind_list)
2080%         InputLine=InputTable(ind_list,:);
2081%         detect_idem=strcmp('"',InputLine);% look for '" (repeat of previous data)
2082%         detect_idem=detect_idem(detect_idem>0);
2083%         if ~isempty (detect_idem)
2084%             InputLine(detect_idem)=InputTable(ind_list-1,detect_idem);
2085%             set(handles.InputTable,'Data',InputTable)
2086%         end
2087%         fileinput=fullfile_uvmat(InputLine{1},InputLine{2},InputLine{3},InputLine{5},InputLine{4},1,2,1,2);
2088%         %fileinput=name_generator(fullfile(InputLine{1},InputLine{3}),1,1,InputLine{5},InputLine{4},1,2,2,InputLine{2})
2089%         %update file series defined by the selected line
2090%         [InputTable{ind_list,3},InputTable{(ind_list),4},errormsg]=update_indices(handles,fileinput,ind_list);
2091%         if ~isempty(errormsg)
2092%                 msgbox_uvmat('ERROR',errormsg)
2093%                 return
2094%         end
2095%     end
2096% end
2097% set(handles.InputTable,'Data',InputTable)
2098% SeriesData=get(handles.series,'UserData');
2099%
2100% state_j='off';
2101% state_Pairs='off';
2102% state_InputFields='off';
2103% val=get(handles.ListView,'Value');
2104% ListViewString={''};
2105% if ~isempty(SeriesData)
2106% %     ListViewString={};
2107%     for iview=1:size(InputTable,1)
2108%         if ~isempty(SeriesData.j1_series{iview})
2109%             state_j='on';
2110%         end
2111%         if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
2112%             state_Pairs='on';
2113%             ListViewString{iview}=num2str(iview);
2114%             if check_lines(iview)
2115%                 val=iview;%select the last pair if it is a new entry
2116%             end
2117%         end
2118%         if strcmp(SeriesData.FileType{iview},'civx')||strcmp(SeriesData.FileType{iview},'civdata')
2119%             state_InputFields='on';
2120%         end
2121%     end
2122% end
2123% set(handles.ListView,'Value',val)
2124% set(handles.ListView,'String',ListViewString)
2125% if strcmp(state_Pairs,'on')
2126%     ListView_Callback(hObject,eventdata,handles)
2127% end
2128% set(handles.PairString,'Visible',state_Pairs)
2129% enable_j(handles,state_j)
2130% set(handles.REFRESH,'BackgroundColor',[1 0 0])
2131% set(handles.REFRESH,'visible','off')
2132
2133% -----------------------------------------------------------------------
2134% --- Update min and max indices of a file series by scanning with find_file_series
2135% --- which also changes the root file and NomType in case of movie. Also adjust the string representation of indices (e.g;
2136% --- 1 or 001 by the function find_file_series
2137% --- This function also dispaly the set of availbale files in the series
2138% --- and the menus appropriate to the file type as well as timing possibly set
2139% --- by an xml image documentation file
2140function [RootFile,NomType,errormsg]=update_indices(handles,fileinput,iview)
2141% -----------------------------------------------------------------------
2142%% look for min and max indices existing in the file series and update SeriesData
2143errormsg='';
2144[FilePath,FileName,FileExt]=fileparts(fileinput);
2145% detect the file type, get the movie object if relevant, and look for the corresponding file series:
2146% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
2147[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
2148if isempty(RootFile)&&isempty(i1_series)
2149    errormsg='no input file in the series';
2150    return
2151end
2152
2153%% adjust the min and max indices common to all the file series
2154MinIndex=get(handles.MinIndex,'Data');
2155MaxIndex=get(handles.MaxIndex,'Data');
2156MinIndex_i=min(i1_series(i1_series>0));
2157if ~isempty(i2_series)
2158    MaxIndex_i=max(i2_series(i2_series>0));
2159else
2160    MaxIndex_i=max(i1_series(i1_series>0));
2161end
2162MinIndex_j=min(j1_series(j1_series>0));
2163if ~isempty(j2_series)
2164    MaxIndex_j=max(j2_series(j2_series>0));
2165else
2166    MaxIndex_j=max(j1_series(j1_series>0));
2167end
2168MinIndex{iview,1}=MinIndex_i;
2169MinIndex{iview,2}=MinIndex_j;
2170MaxIndex{iview,1}=MaxIndex_i;
2171MaxIndex{iview,2}=MaxIndex_j;
2172set(handles.MinIndex,'Data',MinIndex)
2173set(handles.MaxIndex,'Data',MaxIndex)
2174SeriesData=get(handles.series,'UserData');
2175SeriesData.i1_series{iview}=i1_series;
2176SeriesData.i2_series{iview}=i2_series;
2177SeriesData.j1_series{iview}=j1_series;
2178SeriesData.j2_series{iview}=j2_series;
2179SeriesData.FileType{iview}=FileType;
2180
2181%% display the set of existing files as an image
2182set(handles.Waitbar,'Units','pixels')
2183pos=get(handles.Waitbar,'Position');
2184xima=0.5:pos(3)-0.5;% pixel positions on the image representing the existing file indices
2185yima=0.5:pos(4)-0.5;
2186[XIma,YIma]=meshgrid(xima,yima);
2187nb_i=size(i1_series,1);
2188nb_j=size(i1_series,2);
2189ind_i=(0.5:nb_i-0.5)*pos(3)/nb_i;
2190ind_j=(0.5:nb_j-0.5)*pos(4)/nb_j;
2191[Ind_i,Ind_j]=meshgrid(ind_i,ind_j);
2192CData=zeros([size(XIma) 3]);
2193file_ima=double((i1_series(:,:,1)>0)');
2194if numel(file_ima)>=2
2195if size(file_ima,1)==1
2196    CLine=interp1(ind_i,file_ima,xima,'nearest');
2197    CData(:,:,2)=ones(size(yima'))*CLine;
2198else
2199    CData(:,:,2)=interp2(Ind_i,Ind_j,file_ima,XIma,YIma,'nearest');
2200end
2201set(handles.Waitbar,'CData',CData)
2202end
2203set(handles.Waitbar,'Units','normalized')
2204
2205%% enable field and veltype menus
2206% testfield=isequal(get(handles.FieldName,'enable'),'on');
2207% testfield_1=isequal(get(handles.FieldName_1,'enable'),'on');
2208% testveltype=isequal(get(handles.VelType,'enable'),'on');
2209% testveltype_1=isequal(get(handles.VelType_1,'enable'),'on');
2210% testtransform=isequal(get(handles.TransformName,'Enable'),'on');
2211% testnc=0;
2212% testnc_1=0;
2213% testcivx=0;
2214% testcivx_1=0;
2215% testima=0; %test for image input
2216% if isequal(lower(FileExt),'.avi') %.avi file
2217%     testima=1;
2218% elseif ~isempty(imformats(FileExt(2:end)))
2219%     testima=1;
2220% elseif isequal(FileExt,'.vol')
2221%      testima=1;
2222% end
2223%TODO: update
2224% if length(FileExtCell)==1 || length(FileExtCell)>2
2225%     for iview=1:length(FileExtCell)
2226%         if isequal(FileExtCell{iview},'.nc')
2227%             testnc=1;
2228%         end
2229%         if isequal(FileTypeCell{iview},'civx')
2230%             testcivx=1;
2231%         end
2232%     end
2233% elseif length(FileExtCell)==2
2234%     testnc=isequal(FileExtCell{1},'.nc');
2235%     testnc_1=isequal(FileExtCell{2},'.nc');
2236%     testcivx=isequal(FileTypeCell{1},'civx');
2237%     testcivx_1=isequal(FileTypeCell{2},'civx');
2238% end
2239switch FileType
2240    case {'civx','civdata'}
2241    %view_FieldMenu(handles,'on')
2242    menustr=get(handles.FieldName,'String');
2243    if isequal(menustr,{'get_field...'})
2244        set(handles.FieldName,'String',{'get_field...';'velocity';'vort';'div';'more...'})
2245    end
2246    set(handles.VelType,'Visible','on')
2247    set(handles.FieldTransform,'Visible','on')
2248    %      view_TRANSFORM(handles,'on')
2249    %     TODO: second menu
2250    %           view_FieldMenu_1(handles,'on')
2251    %     if testcivx_1
2252    %         menustr=get(handles.FieldName_1,'String');
2253    %         if isequal(menustr,{'get_field...'})
2254    %             set(handles.FieldName_1,'String',{'get_field...';'velocity';'vort';'div';'more...'})
2255    %         end
2256    %     else
2257    %         set(handles.FieldName_1,'Value',1)
2258    %         set(handles.FieldName_1,'String',{'get_field...'})
2259    %     set(handles.VelType_1,'Visible','on')
2260    %     set(handles.VelType_text_1,'Visible','on');
2261    %     end
2262    %     view_FieldMenu_1(handles,'off')
2263    case 'netcdf'
2264  %  view_FieldMenu(handles,'on')
2265    set(handles.FieldName,'Value',1)
2266    set(handles.FieldName,'String',{'get_field...'})
2267    set(handles.FieldTransform,'Visible','off')
2268    %     view_TRANSFORM(handles,'off')
2269    case {'image','multimage','video'}
2270%    view_FieldMenu(handles,'off')
2271 %   view_FieldMenu_1(handles,'off')
2272    set(handles.VelType,'Visible','off')
2273    set(handles.VelType_text,'Visible','off');
2274end
2275
2276
2277%TODO:update
2278% if ~isequal(FileExt,'.nc') && ~isequal(FileExt,'.cdf') && ~testima
2279%     msgbox_uvmat('ERROR',['invalid input file extension ' FileExt])
2280%     return
2281% end 
2282
2283%%  read image documentation file  if found%%%%%%%%%%%%%%%%%%%%%%%%%%%
2284ext_imadoc='';
2285FileBase=fullfile(RootPath,RootFile);
2286if isequal(FileExt,'.xml')||isequal(FileExt,'.civ')
2287    ext_imadoc=FileExt;
2288elseif exist([FileBase '.xml'],'file')
2289    ext_imadoc='.xml';
2290elseif exist([FileBase '.civ'],'file')
2291    ext_imadoc='.civ';
2292end
2293%read the ImaDoc file
2294XmlData=[];
2295NbSlice_calib={};
2296if isequal(ext_imadoc,'.xml')
2297        [XmlData,warntext]=imadoc2struct([FileBase '.xml']);
2298        if isfield(XmlData,'Heading') && isfield(XmlData.Heading,'ImageName') && ischar(XmlData.Heading.ImageName)
2299            [PP,FF,ext_ima_read]=fileparts(XmlData.Heading.ImageName);
2300        end
2301        if isfield(XmlData,'Time')
2302            time{iview}=XmlData.Time;
2303        end
2304        if isfield(XmlData,'Camera')
2305            if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
2306                NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
2307                if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
2308                    msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
2309                end
2310            end
2311            if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
2312                TimeUnit=XmlData.Camera.TimeUnit;
2313            end
2314        end
2315        if ~isempty(warntext)
2316            msgbox_uvmat('WARNING',warntext)
2317        end 
2318elseif isequal(ext_imadoc,'.civ')
2319    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
2320    time{iview}=XmlData.Time;
2321    if error==2, warntext=['no file ' FileBase '.civ'];
2322    elseif error==1, warntext='inconsistent number of fields in the .civ file';
2323    end 
2324end
2325
2326%% update time table
2327TimeTable=get(handles.TimeTable,'Data');
2328TimeTable{iview,1}=time(MinIndex_i,MinIndex_j);
2329TimeTable{iview,4}=time(MaxIndex_i,MaxIndex_j);
2330set(handles.TimeTable,'Data',TimeTable)
2331
2332%% number of slices
2333if isfield(XmlData,'GeometryCalib') && isfield(XmlData.GeometryCalib,'SliceCoord')
2334       siz=size(XmlData.GeometryCalib.SliceCoord);
2335       if siz(1)>1
2336           NbSlice=siz(1);
2337       else
2338           NbSlice=1;
2339       end
2340       set(handles.num_NbSlice,'String',num2str(NbSlice))
2341end
2342% set(handles.mode,'Visible','off') % do not show index pairstring by default
2343set(handles.PairString,'Visible','off')
2344% set(handles.num_ref_i,'Visible','off')
2345% set(handles.ref_i_text,'Visible','off')
2346testpair=0;
2347%set the menus of image pairstring and default selection for series
2348%list pairstring if relevant
2349% Val=get(handles.NomType,'Value');
2350% synchronise_view(handles,Val)
2351
2352% if ~isfield(SeriesData,'j1_series')||isempty(SeriesData.j1_series{index})
2353%     state_j='off'; %no need for j index
2354% else
2355%     state_j='on'; %case of j index
2356% end
2357% show index pairstring if files exist
2358set(handles.series,'UserData',SeriesData)
2359
2360
2361% --------------------------------------------------------------------
2362function MenuExportConfig_Callback(hObject, eventdata, handles)
2363global Series
2364[tild,Series,errormsg]=prepare_jobs(handles,0);
2365% Series=read_GUI(handles.series);
2366
2367evalin('base','global Series')%make CurData global in the workspace
2368display('current series config :')
2369evalin('base','Series') %display CurData in the workspace
2370commandwindow; %brings the Matlab command window to the front
2371
2372
2373% --- Executes on selection change in RunMode.
2374function RunMode_Callback(hObject, eventdata, handles)
Note: See TracBrowser for help on using the repository browser.