source: trunk/src/series.m @ 525

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

various bug repairs and cleaning

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