source: trunk/src/series.m @ 517

Last change on this file since 517 was 512, checked in by sommeria, 12 years ago

various cleaning

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