source: trunk/src/series.m @ 625

Last change on this file since 625 was 623, checked in by sommeria, 11 years ago

waitbar system for series improved to aloow use as stand alone fcts.

to add at the head of series fcts:
hseries=findobj(allchild(0),'Tag','series');
RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
WaitbarHandle?=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series

call to waitbar:

update_waitbar(WaitbarHandle?,index/nbfield)
if ishandle(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction?'),'queue')

disp('program stopped by user')
break

end

File size: 114.8 KB
Line 
1%'series': master function associated to the GUI series.m for analysis field series 
2%------------------------------------------------------------------------
3% function varargout = series(varargin)
4% associated with the GUI series.fig
5%
6%INPUT
7% param: structure with input parameters (link with the GUI uvmat)
8%      .menu_coord_str: string for the TransformName (menu for coordinate transforms)
9%      .menu_coord_val: value for TransformName (menu for coordinate transforms)
10%      .FileName: input file name
11%      .FileName_1: second input file name
12%      .list_field: menu of input fields
13%      .index_fields: chosen index
14%      .civ1=0 or 1, .interp1,  ... : input civ field type
15%
16%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
17%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
18%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
19%     This file is part of the toolbox UVMAT.
20%
21%     UVMAT is free software; you can redistribute it and/or modify
22%     it under the terms of the GNU General Public License as published by
23%     the Free Software Foundation; either version 2 of the License, or
24%     (at your option) any later version.
25%
26%     UVMAT is distributed in the hope that it will be useful,
27%     but WITHOUT ANY WARRANTY; without even the implied warranty of
28%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
30%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
31
32%------------------------------------------------------------------------
33%------------------------------------------------------------------------
34%  I - MAIN FUNCTION series
35%------------------------------------------------------------------------
36%------------------------------------------------------------------------
37function varargout = series(varargin)
38
39% Begin initialization code - DO NOT EDIT
40gui_Singleton = 1;
41gui_State = struct('gui_Name',       mfilename, ...
42                   'gui_Singleton',  gui_Singleton, ...
43                   'gui_OpeningFcn', @series_OpeningFcn, ...
44                   'gui_OutputFcn',  @series_OutputFcn, ...
45                   'gui_LayoutFcn',  [] , ...
46                   'gui_Callback',   []);
47if nargin && ischar(varargin{1})
48    gui_State.gui_Callback = str2func(varargin{1});
49end
50
51if nargout
52    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
53else
54    gui_mainfcn(gui_State, varargin{:});
55end
56% End initialization code - DO NOT EDIT
57
58%--------------------------------------------------------------------------
59% --- Executes just before series is made visible.
60%--------------------------------------------------------------------------
61function series_OpeningFcn(hObject, eventdata, handles,Param)
62
63% Choose default command line output for series
64handles.output = hObject;
65% Update handles structure
66guidata(hObject, handles);
67
68%% initial settings
69% position and  size of the GUI at opening
70set(0,'Unit','points')
71ScreenSize=get(0,'ScreenSize');%size of the current screen, in points (1/72 inch)
72Width=900;% prefered width of the GUI in points (1/72 inch)
73Height=624;% prefered height of the GUI in points (1/72 inch)
74%adjust to screen size (reduced by a min margin)
75RescaleFactor=min((ScreenSize(3)-80)/Width,(ScreenSize(4)-80)/Height);
76if RescaleFactor>1
77    %RescaleFactor=RescaleFactor/2+1/2; %reduce the rescale factor to provide an increased margin for a big screen
78    RescaleFactor=min(RescaleFactor,1);
79end
80Width=Width*RescaleFactor;
81Height=Height*RescaleFactor;
82LeftX=80*RescaleFactor;%position of the left fig side, in pixels (put to the left side, with some margin)
83LowY=round(ScreenSize(4)/2-Height/2); % put at the middle height on the screen
84set(hObject,'Units','points')
85set(hObject,'Position',[LeftX LowY Width Height])% position and size of the GUI at opening
86
87% settings of table MinIndex_j
88set(handles.MinIndex_i,'ColumnFormat',{'numeric'})
89set(handles.MinIndex_i,'ColumnEditable',false)
90set(handles.MinIndex_i,'ColumnName',{'i min'})
91
92% settings of table MinIndex_j
93set(handles.MinIndex_j,'ColumnFormat',{'numeric'})
94set(handles.MinIndex_j,'ColumnEditable',false)
95set(handles.MinIndex_j,'ColumnName',{'j min'})
96
97% settings of table MaxIndex_i
98set(handles.MaxIndex_i,'ColumnFormat',{'numeric'})
99set(handles.MaxIndex_i,'ColumnEditable',false)
100set(handles.MaxIndex_i,'ColumnName',{'i max'})
101
102% settings of table MaxIndex_j
103set(handles.MaxIndex_j,'ColumnFormat',{'numeric'})
104set(handles.MaxIndex_j,'ColumnEditable',false)
105set(handles.MaxIndex_j,'ColumnName',{'j max'})
106
107% settings of table PairString
108set(handles.PairString,'ColumnName',{'pairs'})
109set(handles.PairString,'ColumnEditable',false)
110set(handles.PairString,'ColumnFormat',{'char'})
111set(handles.PairString,'Data',{''})
112
113series_ResizeFcn(hObject, eventdata, handles)%resize table according to series GUI size
114set(hObject,'WindowButtonDownFcn',{'mouse_down'})%allows mouse action with right button (zoom for uicontrol display)
115
116% check default input data
117if ~exist('Param','var')
118    Param=[]; %default
119end
120
121%% list of builtin functions in the mebu ActionName
122ActionList={'check_data_files';'aver_stat';'time_series';'merge_proj'};% WARNING: fits with nb_builtin_ACTION=4 in ActionName_callback
123NbBuiltinAction=numel(ActionList);
124[path_series,name,ext]=fileparts(which('series'));% path to the GUI series
125path_series_fct=fullfile(path_series,'series');%path of the functions in subdirectroy 'series'
126ActionExtList={'.m';'.sh'};% default choice of extensions (Matlab fct .m or compiled version .sh
127ActionPathList=cell(NbBuiltinAction,numel(ActionExtList));%initiate the cell matrix of Action fct paths
128ActionPathList(:)={path_series_fct}; %set the default path to series fcts to all list members
129RunModeList={'local';'background'};% default choice of extensions (Matlab fct .m or compiled version .sh)
130[s,w]=system('oarstat');% look for cluster system 'oar'
131if isequal(s,0)
132    RunModeList=[RunModeList;{'cluster_oar'}];
133end
134[s,w]=system('qstat');% look for cluster system 'sge'
135if isequal(s,0)
136    RunModeList=[RunModeList;{'cluster_sge'}];
137end
138set(handles.RunMode,'String',RunModeList)
139
140%% list of builtin transform functions in the mebu TransformName
141TransformList={'';'sub_field';'phys';'phys_polar'};% WARNING: must fit with the corresponding menu in uvmat and nb_builtin_transform=4 in  TransformName_callback
142NbBuiltinTransform=numel(TransformList);
143path_transform_fct=fullfile(path_series,'transform_field');
144TransformPathList=cell(NbBuiltinTransform,1);%initiate the cell matrix of Action fct paths
145TransformPathList(:)={path_transform_fct}; %set the default path to series fcts to all list members
146
147%% get the user defined functions stored in the personal file uvmat_perso.mat
148dir_perso=prefdir;
149profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
150if exist(profil_perso,'file')
151    h=load (profil_perso);
152    %get the list of previous input files in the upper bar menu Open
153    if isfield(h,'MenuFile')
154        for ifile=1:min(length(h.MenuFile),5)
155            eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',h.MenuFile{ifile});'])
156        end
157    end
158    %get the menu of actions
159    if isfield(h,'ActionExtListUser') && iscell(h.ActionExtListUser)
160        ActionExtList=[ActionExtList; h.ActionExtListUser];
161    end
162    if isfield(h,'ActionListUser') && iscell(h.ActionListUser) && isfield(h,'ActionPathListUser') && iscell(h.ActionPathListUser)
163        ActionList=[ActionList;h.ActionListUser];
164        ActionPathList=[ActionPathList;h.ActionPathListUser];
165    end
166    %get the menu of transform fct
167    if isfield(h,'TransformListUser') && iscell(h.TransformListUser) && isfield(h,'TransformPathListUser') && iscell(h.TransformPathListUser)
168        TransformList=[TransformList;h.TransformListUser];
169        TransformPathList=[TransformPathList;h.TransformPathListUser];
170    end
171end
172
173%% selection of the input Action fct
174ActionCheckExist=true(size(ActionList));%initiate the check of the path to the listed action fct
175for ilist=NbBuiltinAction+1:numel(ActionList)%check  the validity of the path of the user defined Action fct
176    ActionCheckExist(ilist)=exist(fullfile(ActionPathList{ilist},[ActionList{ilist} '.m']),'file');
177end
178ActionPathList=ActionPathList(ActionCheckExist,:);% suppress the menu options which are not valid anymore
179ActionList=ActionList(ActionCheckExist);
180set(handles.ActionName,'String',[ActionList;{'more...'}])
181set(handles.ActionName,'UserData',ActionPathList)
182ActionIndex=[];
183if isfield(Param,'ActionName')% copy the selected menu index transferred in Param from uvmat
184    ActionIndex=find(strcmp(Param.ActionName,ActionList),1);
185end
186if isempty(ActionIndex)
187    ActionIndex=1;
188end
189set(handles.ActionName,'Value',ActionIndex)
190set(handles.ActionPath,'String',ActionPathList{ActionIndex})
191set(handles.ActionExt,'Value',1)
192set(handles.ActionExt,'String',ActionExtList)
193
194%% selection of the input transform fct
195TransformCheckExist=true(size(TransformList));
196for ilist=NbBuiltinTransform+1:numel(TransformList)
197    TransformCheckExist(ilist)=exist(fullfile(TransformPathList{ilist},[TransformList{ilist} '.m']),'file');
198end
199TransformPathList=TransformPathList(TransformCheckExist);
200TransformList=TransformList(TransformCheckExist);
201set(handles.TransformName,'String',[TransformList;{'more...'}])
202set(handles.TransformName,'UserData',TransformPathList)
203TransformIndex=[];
204if isfield(Param,'TransformName')% copy the selected menu index transferred in Param from uvmat
205    TransformIndex=find(strcmp(Param.TransformName,TransformList),1);
206end
207if isempty(TransformIndex)
208    TransformIndex=1;
209end
210set(handles.TransformName,'Value',TransformIndex)
211set(handles.TransformPath,'String',TransformPathList{TransformIndex})
212   
213%% fields input initialisation
214if isfield(Param,'list_fields')&& isfield(Param,'index_fields') &&~isempty(Param.list_fields) &&~isempty(Param.index_fields)
215    set(handles.FieldName,'String',Param.list_fields);% list menu fields
216    set(handles.FieldName,'Value',Param.index_fields);% selected string index
217end
218if isfield(Param,'Coord_x_str')&& isfield(Param,'Coord_x_val')
219        set(handles.Coord_x,'String',Param.Coord_x_str);% list menu fields
220    set(handles.Coord_x,'Value',Param.Coord_x_val);% selected string index
221end
222if isfield(Param,'Coord_y_str')&& isfield(Param,'Coord_y_val')
223        set(handles.Coord_y,'String',Param.Coord_y_str);% list menu fields
224    set(handles.Coord_y,'Value',Param.Coord_y_val);% selected string index
225end
226
227%% Adjust the GUI according to the binaries available in PARAM.xml
228% path_uvmat=fileparts(which('uvmat')); %path to civ
229% addpath (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)
230% errormsg=[];%default error message
231% xmlfile='PARAM.xml';
232% if exist(xmlfile,'file')
233%     try
234%         t=xmltree(xmlfile);
235%         sparam=convert(t);
236%     catch ME
237%         errormsg={' Unable to read the file PARAM.xml defining the  binaries:';ME.message};
238%     end
239% else
240%     errormsg=[xmlfile ' not found: path to binaries undefined'];
241% end
242% if ~isempty(errormsg)
243%     msgbox_uvmat('WARNING',errormsg);
244% end
245% test_batch=0;%default: ,no batch mode available
246% if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
247%     test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
248% end
249% RUNVal=get(handles.RunMode,'Value');
250% if test_batch==0
251%    if RUNVal>2
252%        set(handles.RunMode,'Value',1)
253%    end
254%    set(handles.RunMode,'String',{'local';'background'})
255% else
256%     set(handles.RunMode,'String',{'local';'background';'cluster'})
257% end
258
259%% introduce the input file name(s) if defined from input Param
260if isfield(Param,'FileName')
261    InputTable={'','','','',''}; % refresh the file input table
262    set(handles.InputTable,'Data',InputTable)
263    if isfield(Param,'FileName_1')
264        display_file_name(handles,Param.FileName,'one')%refresh the input table
265        display_file_name(handles,Param.FileName_1,1)
266    else
267        display_file_name(handles,Param.FileName,'one')%refresh the input table
268    end
269end 
270if isfield(Param,'incr_i')
271    set(handles.num_incr_i,'String',num2str(Param.incr_i))
272end
273if isfield(Param,'incr_j')
274    set(handles.num_incr_j,'String',num2str(Param.incr_j))
275end
276
277
278%------------------------------------------------------------------------
279% --- Outputs from this function are returned to the command line.
280function varargout = series_OutputFcn(hObject, eventdata, handles)
281%------------------------------------------------------------------------
282% varargout  cell array for returning output args (see VARARGOUT);
283% hObject    handle to figure
284% eventdata  reserved - to be defined in a future version of MATLAB
285% handles    structure with handles and user data (see GUIDATA)
286% Get default command line output from handles structure
287varargout{1} = handles.output;
288
289%------------------------------------------------------------------------
290%------------------------------------------------------------------------
291%  II - FUNCTIONS FOR INTRODUCING THE INPUT FILES
292% automatically sets the global properties when the rootfile name is introduced
293% then activate the view-field actionname if selected
294% it is activated either by clicking on the RootPath window or by the
295% browser
296%------------------------------------------------------------------------
297%------------------------------------------------------------------------
298function MenuBrowse_Callback(hObject, eventdata, handles)
299%------------------------------------------------------------------------   
300%get the previous input file in the Input Table
301oldfile=''; %default
302SeriesData=get(handles.series,'UserData');
303if isfield(SeriesData,'RefFile')
304    oldfile=SeriesData.RefFile{1};
305end
306if ~exist(oldfile,'file')
307    dir_perso=prefdir;
308    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
309    if exist(profil_perso,'file')
310         h=load (profil_perso);
311        if isfield(h,'RootPath')&&ischar(h.RootPath)
312            oldfile=h.RootPath;
313        end
314    end
315end
316fileinput=uigetfile_uvmat('pick a file to refresh the input table',oldfile);
317if ~isempty(fileinput)
318     display_file_name(handles,fileinput,'one')
319end
320
321
322% --------------------------------------------------------------------
323function MenuFile_1_Callback(hObject, eventdata, handles)
324fileinput=get(handles.MenuFile_1,'Label');
325display_file_name(handles,fileinput,'one')
326
327% --------------------------------------------------------------------
328function MenuFile_2_Callback(hObject, eventdata, handles)
329fileinput=get(handles.MenuFile_2,'Label');
330display_file_name(handles,fileinput,'one')
331
332% --------------------------------------------------------------------
333function MenuFile_3_Callback(hObject, eventdata, handles)
334fileinput=get(handles.MenuFile_3,'Label');
335display_file_name( handles,fileinput,'one')
336
337% --------------------------------------------------------------------
338function MenuFile_4_Callback(hObject, eventdata, handles)
339fileinput=get(handles.MenuFile_4,'Label');
340display_file_name(handles,fileinput,'one')
341
342% --------------------------------------------------------------------
343function MenuFile_5_Callback(hObject, eventdata, handles)
344fileinput=get(handles.MenuFile_5,'Label');
345display_file_name(handles,fileinput,'one')
346
347% --------------------------------------------------------------------
348function MenuBrowse_insert_Callback(hObject, eventdata, handles)
349InputTable=get(handles.InputTable,'Data');
350RootPathCell=InputTable(:,1);
351SubDirCell=InputTable(:,3);
352RootFileCell=InputTable(:,2);
353oldfile=''; %default
354if isempty(RootPathCell)||isequal(RootPathCell,{''})%loads the previously stored file name and set it as default in the file_input box
355     dir_perso=prefdir;
356     profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
357     if exist(profil_perso,'file')
358          h=load (profil_perso);
359         if isfield(h,'filebase')&ischar(h.filebase)
360                 oldfile=h.filebase;
361         end
362         if isfield(h,'RootPath')&ischar(h.RootPath)
363                 oldfile=h.RootPath;
364         end
365     end
366 else
367     oldfile=fullfile(RootPathCell{1},RootFileCell{1});
368 end
369[FileName, PathName, filterindex] = uigetfile( ...
370       {'*.xml;*.xls;*.png;*.avi;*.AVI;*.nc', ' (*.xml,*.xls, *.png, *.avi,*.nc)';
371       '*.xml',  '.xml files '; ...
372        '*.xls',  '.xls files '; ...
373        '*.png','.png image files'; ...
374        '*.avi;*.AVI','.avi movie files'; ...
375        '*.nc','.netcdf files'; ...
376        '*.*',  'All Files (*.*)'}, ...
377        'Pick a file',oldfile);
378fileinput=[PathName FileName];%complete file name
379sizf=size(fileinput);
380if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
381[path,name,ext]=fileparts(fileinput);
382if isequal(ext,'.xml')
383    msgbox_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
384elseif isequal(ext,'.xls')
385    msgbox_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
386else
387    display_file_name(handles,fileinput,'append')
388end
389
390% --------------------------------------------------------------------
391function MenuFile_insert_1_Callback(hObject, eventdata, handles)
392% --------------------------------------------------------------------   
393fileinput=get(handles.MenuFile_insert_1,'Label');
394display_file_name(handles,fileinput,'append')
395
396% --------------------------------------------------------------------
397function MenuFile_insert_2_Callback(hObject, eventdata, handles)
398% --------------------------------------------------------------------   
399fileinput=get(handles.MenuFile_insert_2,'Label');
400display_file_name(handles,fileinput,'append')
401
402% --------------------------------------------------------------------
403function MenuFile_insert_3_Callback(hObject, eventdata, handles)
404% --------------------------------------------------------------------   
405fileinput=get(handles.MenuFile_insert_3,'Label');
406display_file_name( handles,fileinput,'append')
407
408% --------------------------------------------------------------------
409function MenuFile_insert_4_Callback(hObject, eventdata, handles)
410% --------------------------------------------------------------------   
411fileinput=get(handles.MenuFile_insert_4,'Label');
412display_file_name( handles,fileinput,'append')
413
414% --------------------------------------------------------------------
415function MenuFile_insert_5_Callback(hObject, eventdata, handles)
416% --------------------------------------------------------------------   
417fileinput=get(handles.MenuFile_insert_5,'Label');
418display_file_name(handles,fileinput,'append')
419
420%------------------------------------------------------------------------
421% --- Executes when entered data in editable cell(s) in InputTable.
422function InputTable_CellEditCallback(hObject, eventdata, handles)
423%------------------------------------------------------------------------
424set(handles.REFRESH,'Visible','on')
425set(handles.REFRESH_title,'Visible','on')
426iview=eventdata.Indices(1);
427view_set=get(handles.REFRESH,'UserData');
428if isempty(find(view_set==iview))
429    set(handles.REFRESH,'UserData',[view_set iview])
430end
431%% enable other menus and uicontrols
432set(handles.MenuOpen_insert,'Enable','on')
433set(handles.MenuFile_insert_1,'Enable','on')
434set(handles.MenuFile_insert_2,'Enable','on')
435set(handles.MenuFile_insert_3,'Enable','on')
436set(handles.MenuFile_insert_4,'Enable','on')
437set(handles.MenuFile_insert_5,'Enable','on')
438set(handles.RUN, 'Enable','On')
439set(handles.RUN,'BackgroundColor',[1 0 0])% set RUN button to red
440
441%------------------------------------------------------------------------
442% --- Executes on button press in REFRESH.
443function REFRESH_Callback(hObject, eventdata, handles)
444%------------------------------------------------------------------------
445InputTable=get(handles.InputTable,'Data');
446view_set=get(handles.REFRESH,'UserData');% list of lines to refresh
447set(handles.REFRESH,'BackgroundColor',[1 1 0])% set REFRESH  button to yellow color (indicate activation)
448drawnow
449for iview=view_set
450    RootPath=fullfile(InputTable{iview,1},InputTable{iview,2});
451    if ~exist(RootPath,'dir')
452        i1_series=[];
453        RootPath=fileparts(RootPath); %will try the upped forldr
454    else
455        [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,tild,FileType,FileInfo,MovieObject]=...
456            find_file_series(fullfile(InputTable{iview,1},InputTable{iview,2}),[InputTable{iview,3} InputTable{iview,4} InputTable{iview,5}]);
457    end
458    if isempty(i1_series)
459        fileinput=uigetfile_uvmat(['wrong input at line ' num2str(iview) ':pick a new input file'],RootPath);
460        if isempty(fileinput)
461            set(handles.REFRESH,'BackgroundColor',[1 0 0])% set REFRESH  back to red color
462            return
463        else
464            display_file_name(handles,fileinput,iview)
465        end
466    else
467       update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,FileInfo,MovieObject,iview)
468    end
469end
470set(handles.REFRESH,'Visible','off')
471set(handles.REFRESH_title,'Visible','off')
472set(handles.REFRESH,'UserData',[])
473
474%------------------------------------------------------------------------
475% --- Function called when a new file is opened, either by series_OpeningFcn or by the browser
476function display_file_name(handles,fileinput,iview)
477%------------------------------------------------------------------------ 
478%
479% INPUT:
480% handles: handles of elements in the GUI
481% fileinput: input file name, including path
482% iview: line index in the input table
483%       or 'one': refresh the list
484%       'append': add a new line to the list
485
486%% get the input root name, indices, file extension and nomenclature NomType
487if ~exist(fileinput,'file')
488    msgbox_uvmat('ERROR',['input file ' fileinput  ' does not exist'])
489    return
490end
491
492%% enable other menus and uicontrols
493set(handles.MenuOpen_insert,'Enable','on')
494set(handles.MenuFile_insert_1,'Enable','on')
495set(handles.MenuFile_insert_2,'Enable','on')
496set(handles.MenuFile_insert_3,'Enable','on')
497set(handles.MenuFile_insert_4,'Enable','on')
498set(handles.MenuFile_insert_5,'Enable','on')
499set(handles.RUN, 'Enable','On')
500set(handles.RUN,'BackgroundColor',[1 0 0])% set RUN button to red
501set(handles.InputTable,'BackgroundColor',[1 1 0]) % set RootPath edit box  to yellow
502drawnow
503
504%% detect root name, nomenclature and indices in the input file name:
505[FilePath,FileName,FileExt]=fileparts(fileinput);
506% detect the file type, get the movie object if relevant, and look for the corresponding file series:
507% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
508[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,FileInfo,MovieObject,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
509if isempty(RootFile)&&isempty(i1_series)
510    errormsg='no input file in the series';
511    return
512end
513
514%% fill the list of file series
515InputTable=get(handles.InputTable,'Data');
516SeriesData=get(handles.series,'UserData');
517if strcmp(iview,'append') % display the input data as a new line in the table
518    iview=size(InputTable,1);% the next line in InputTable becomes the current line
519    InputTable(iview+1,:)={'','','','',''};
520    InputTable(iview,:)=[{RootPath},{SubDir},{RootFile},{NomType},{FileExt}];
521elseif strcmp(iview,'one') % refresh the list of  input  file series
522    iview=1; %the first line in InputTable becomes the current line
523    InputTable=[{'','','','',''};{'','','','',''}];
524    InputTable(iview,:)=[{RootPath},{SubDir},{RootFile},{NomType},{FileExt}];
525    set(handles.TimeTable,'Data',[{[]},{[]},{[]},{[]}])
526    set(handles.MinIndex_i,'Data',[{[]}])
527    set(handles.MaxIndex_i,'Data',[{[]}])
528    set(handles.MinIndex_j,'Data',[{[]}])
529    set(handles.MaxIndex_j,'Data',[{[]}])
530    set(handles.ListView,'Value',1)
531    set(handles.ListView,'String',{'1'})
532    set(handles.PairString,'Data',{''})
533    SeriesData.i1_series={};
534    SeriesData.i2_series={};
535    SeriesData.j1_series={};
536    SeriesData.j2_series={};
537    SeriesData.FileType={};
538    SeriesData.FileInfo={};
539    SeriesData.Time={};
540end
541nbview=size(InputTable,1)-1;% rmq: the last line is set blank to allow manual addition of a line
542set(handles.ListView,'String',mat2cell((1:nbview)',ones(nbview,1)))
543set(handles.ListView,'Value',iview)
544set(handles.InputTable,'Data',InputTable)
545
546%% determine the selected reference field indices for pair display
547if isempty(i1)
548    i1=1;
549end
550if isempty(i2)
551    i2=i1;
552end
553ref_i=floor((i1+i2)/2);% reference image number corresponding to the file
554set(handles.num_ref_i,'String',num2str(ref_i));
555% set(handles.num_ref_i,'UserData',[i1 i2])%store the indices for future opening
556if isempty(j1)
557    j1=1;
558end
559if isempty(j2)
560    j2=j1;
561end
562ref_j=floor((j1+j2)/2);% reference image number corresponding to the file
563set(handles.num_ref_j,'String',num2str(ref_j));
564% set(handles.num_ref_j,'UserData',[j1 j2]);%store the indices for future opening
565
566%% update the list of recent files in the menubar and save it for future opening
567MenuFile=[{get(handles.MenuFile_1,'Label')};{get(handles.MenuFile_2,'Label')};...
568    {get(handles.MenuFile_3,'Label')};{get(handles.MenuFile_4,'Label')};{get(handles.MenuFile_5,'Label')}];
569str_find=strcmp(fileinput,MenuFile);
570if isempty(find(str_find,1))
571    MenuFile=[{fileinput};MenuFile];%insert the current file if not already in the list
572end
573for ifile=1:min(length(MenuFile),5)
574    eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',MenuFile{ifile});'])
575    eval(['set(handles.MenuFile_insert_' num2str(ifile) ',''Label'',MenuFile{ifile});'])
576end
577dir_perso=prefdir;
578profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
579if exist(profil_perso,'file')
580    save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
581else
582    save (profil_perso,'MenuFile','-V6'); %store the file names for future opening of uvmat
583end
584% save the opened file to initiate future opening
585SeriesData.RefFile{iview}=fileinput;% reference opening file for line iview
586SeriesData.Ref_i1=i1;
587SeriesData.Ref_i2=i2;
588SeriesData.Ref_j1=j1;
589SeriesData.Ref_j2=j2;
590set(handles.series,'UserData',SeriesData)
591
592set(handles.InputTable,'BackgroundColor',[1 1 1])
593
594%% initiate input file series and refresh the current field view:     
595update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,FileInfo,MovieObject,iview);
596
597%------------------------------------------------------------------------
598% --- Update information about a new field series (indices to scan, timing,
599%     calibration from an xml file
600function update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,FileInfo,VideoObject,iview)
601%------------------------------------------------------------------------
602InputTable=get(handles.InputTable,'Data');
603
604%% display the min and max indices for the whole file series
605if size(i1_series,2)==2 && min(min(i1_series(:,1,:)))==0
606    MinIndex_j=1;% index j set to 1 by default
607    MaxIndex_j=1;
608    MinIndex_i=find(i1_series(:,2,:), 1 )-1;% min ref index i detected in the series (corresponding to the first non-zero value of i1_series, except for zero index)
609    MaxIndex_i=find(i1_series(:,2,:), 1, 'last' )-1;%max ref index i detected in the series (corresponding to the last non-zero value of i1_series)
610else
611    pair_max=squeeze(max(i1_series,[],1)); %max on pair index
612    j_max=max(pair_max,[],1);
613    MinIndex_i=find(j_max, 1 )-1;% min ref index i detected in the series (corresponding to the first non-zero value of i1_series, except for zero index)
614    MaxIndex_i=find(j_max, 1, 'last' )-1;% max ref index i detected in the series (corresponding to the first non-zero value of i1_series, except for zero index)
615    diff_i_max=diff(j_max);
616    if ~isempty(diff_i_max) && isequal (diff_i_max,diff_i_max(1)*ones(size(diff_i_max)))
617        set(handles.num_incr_i,'String',num2str(diff_i_max(1)))% detect an increment to dispaly by default
618    end
619    i_max=max(pair_max,[],2);
620    MinIndex_j=min(find(i_max))-1;% min ref index j
621    MaxIndex_j=max(find(i_max))-1;% max ref index j
622    diff_j_max=diff(i_max);
623    if isequal (diff_j_max,diff_j_max(1)*ones(size(diff_j_max)))
624        set(handles.num_incr_j,'String',num2str(diff_j_max(1)))
625    end
626end
627if isequal(MinIndex_i,-1)
628    MinIndex_i=0;
629end
630if isequal(MinIndex_j,-1)
631    MinIndex_j=0;
632end
633MinIndex_i_cell=get(handles.MinIndex_i,'Data');%retrieve the min indices in the table MinIndex
634MinIndex_j_cell=get(handles.MinIndex_j,'Data');%retrieve the min indices in the table MinIndex
635MaxIndex_i_cell=get(handles.MaxIndex_i,'Data');%retrieve the min indices in the table MinIndex
636MaxIndex_j_cell=get(handles.MaxIndex_j,'Data');%retrieve the min indices in the table MinIndex
637MinIndex_i_cell{iview,1}=MinIndex_i;
638MinIndex_j_cell{iview,1}=MinIndex_j;
639MaxIndex_i_cell{iview,1}=MaxIndex_i;
640MaxIndex_j_cell{iview,1}=MaxIndex_j;
641set(handles.MinIndex_i,'Data',MinIndex_i_cell)%display the min indices in the table MinIndex
642set(handles.MinIndex_j,'Data',MinIndex_j_cell)%display the max indices in the table MaxIndex
643set(handles.MaxIndex_i,'Data',MaxIndex_i_cell)%display the min indices in the table MinIndex
644set(handles.MaxIndex_j,'Data',MaxIndex_j_cell)%display the max indices in the table MaxIndex
645
646%% adjust the first and last indices for the selected series, only if requested by the bounds
647% i index, compare input to min index i
648first_i=str2num(get(handles.num_first_i,'String'));%retrieve previous first i
649ref_i=str2num(get(handles.num_ref_i,'String'));%index i given by the input field
650if isempty(first_i)
651    first_i=ref_i;% first_i updated by the input value
652elseif first_i < MinIndex_i
653    first_i=MinIndex_i; % first_i set to the min i index (restricted by oter input lines)
654elseif first_i >MaxIndex_i
655    first_i=MaxIndex_i;% first_i set to the max i index (restricted by oter input lines)
656end
657% j index,  compare input to min index j
658first_j=str2num(get(handles.num_first_j,'String'));
659ref_j=str2num(get(handles.num_ref_j,'String'));%index j given by the input field
660if isempty(first_j)
661    first_j=ref_j;% first_j updated by the input value
662elseif first_j<MinIndex_j
663    first_j=MinIndex_j; % first_j set to the min j index (restricted by oter input lines)
664elseif first_j >MaxIndex_j
665    first_j=MaxIndex_j; % first_j set to the max j index (restricted by oter input lines)
666end
667% i index, compare input to max index i
668last_i=str2num(get(handles.num_last_i,'String'));
669if isempty(last_i)
670    last_i=ref_i;
671elseif last_i > MaxIndex_i
672    last_i=MaxIndex_i;
673elseif last_i<first_i
674    last_i=first_i;
675end
676% j index, compare input to max index j
677last_j=str2num(get(handles.num_last_j,'String'));
678if isempty(last_j)
679    last_j=ref_j;
680elseif last_j>MaxIndex_j
681    last_j=MaxIndex_j;
682elseif last_j<first_j
683    last_j=first_j;
684end
685set(handles.num_first_i,'String',num2str(first_i));
686set(handles.num_first_j,'String',num2str(first_j));
687set(handles.num_last_i,'String',num2str(last_i));
688set(handles.num_last_j,'String',num2str(last_j));
689
690%% number of slices set by default
691NbSlice=1;%default
692% read  value set by the first series for the append mode (iwiew >1)
693if iview>1 && strcmp(get(handles.num_NbSlice,'Visible'),'on')
694    NbSlice=str2num(get(handles.num_NbSlice,'String'));
695end
696
697%% default time unit
698TimeUnit='';
699% read  value set by the first series for the append mode (iwiew >1)
700if iview>1
701    TimeUnit=get(handles.TimeUnit,'String');
702end
703TimeSource='';
704Time=[];%default
705
706%%  read image documentation file if found
707XmlData=[];
708check_calib=0;
709XmlFileName=find_imadoc(InputTable{iview,1},InputTable{iview,2},InputTable{iview,3},InputTable{iview,5});
710if ~isempty(XmlFileName)
711    [XmlData,errormsg]=imadoc2struct(XmlFileName);
712    if ~isempty(errormsg)
713         msgbox_uvmat('WARNING',['error in reading ' XmlFileName ': ' errormsg]);
714    end
715    % read time if available
716    if isfield(XmlData,'Time')
717        Time=XmlData.Time;
718        TimeSource='xml';
719    end
720    if isfield(XmlData,'Camera')
721        if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
722            if iview>1 && ~isempty(NbSlice) && ~strcmp(NbSlice,XmlData.Camera.NbSlice)
723                msgbox_uvmat('WARNING','inconsistent number of slices with the first field series');
724            end
725            NbSlice=XmlData.Camera.NbSlice;% Nbre of slices from camera
726        end
727        if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
728            if iview>1 && ~isempty(TimeUnit) && ~strcmp(TimeUnit,XmlData.Camera.TimeUnit)
729                msgbox_uvmat('WARNING','inconsistent time unit with the first field series');
730            end
731            TimeUnit=XmlData.Camera.TimeUnit;
732        end
733    end
734    % number of slices
735    if isfield(XmlData,'GeometryCalib')
736        check_calib=1;
737        if isfield(XmlData.GeometryCalib,'SliceCoord')
738            siz=size(XmlData.GeometryCalib.SliceCoord);
739            if siz(1)>1
740                if iview>1 && ~isempty(NbSlice) && ~strcmp(NbSlice,siz(1))
741                    msgbox_uvmat('WARNING','inconsistent number of Z indices with the first field series');
742                end
743                NbSlice=siz(1);
744            end
745        end
746    end
747    set(handles.num_NbSlice,'String',num2str(NbSlice))
748end
749
750%% read timing and total frame number from the current file (movie files) if not already set by the xml file (prioritary)
751InputTable=get(handles.InputTable,'Data');
752% FileBase=fullfile(InputTable{iview,1},InputTable{iview,3});
753
754% case of movies
755% if strcmp(InputTable{iview,4},'*')
756if isempty(Time)
757    if ~isempty(VideoObject)
758        imainfo=get(VideoObject);
759        if isempty(j1_series); %frame index along i
760            Time=zeros(imainfo.NumberOfFrames+1,2);
761            Time(:,2)=(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames)/imainfo.FrameRate)';
762        else
763            Time=[0;ones(size(i1_series,3)-1,1)]*(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames)/imainfo.FrameRate);
764        end
765        TimeSource='video';
766        % set(han:dles.Dt_txt,'String',['Dt=' num2str(1000/imainfo.FrameRate) 'ms']);%display the elementary time interval in millisec
767        %     ColorType='truecolor';
768%     elseif ~isempty(imformats(regexprep(InputTable{iview,5},'^.',''))) || isequal(InputTable{iview,5},'.vol')%&& isequal(NomType,'*')% multi-frame image
769%         if ~isempty(InputTable{iview,2})
770%             imainfo=imfinfo(fullfile(InputTable{iview,1},InputTable{iview,2},[InputTable{iview,3} InputTable{iview,5}]));
771%         else
772%             imainfo=imfinfo([FileBase InputTable{iview,5}]);
773%         end
774%         %     ColorType=imainfo.ColorType;%='truecolor' for color images
775% %         if length(imainfo) >1 %case of image with multiple frames
776% %             nbfield=length(imainfo);
777% %             nbfield_j=1;
778% %         end
779    end
780end
781
782%% update time table
783if ~isempty(Time)
784    TimeTable=get(handles.TimeTable,'Data');
785    %     first_i=str2num(get(handles.num_first_i,'String'));
786    %     last_i=str2num(get(handles.num_last_i,'String'));
787    %     first_j=str2num(get(handles.num_first_j,'String'));
788    %     last_j=str2num(get(handles.num_last_j,'String'));
789    %     MinIndex_i=get(handles.MinIndex_i,'Data');
790    %     MinIndex_j=get(handles.MinIndex_j,'Data');
791    %     MaxIndex_i=get(handles.MaxIndex_i,'Data');
792    %     MaxIndex_j=get(handles.MaxIndex_j,'Data');
793    TimeTable{iview,1}=Time(MinIndex_i+1,MinIndex_j+1);
794    if size(Time)>=[first_i+1 first_j+1]
795        TimeTable{iview,2}=Time(first_i+1,first_j+1);
796    end
797    if size(Time)>=[last_i+1 last_j+1]
798        TimeTable{iview,3}=Time(last_i+1,last_j+1);
799    end
800    if size(Time)>=[MaxIndex_i+1 MaxIndex_j+1];
801        TimeTable{iview,4}=Time(MaxIndex_i+1,MaxIndex_j+1);
802    end
803    set(handles.TimeTable,'Data',TimeTable)
804end
805   
806
807%% update the series info in 'UserData'
808SeriesData=get(handles.series,'UserData');
809SeriesData.i1_series{iview}=i1_series;
810SeriesData.i2_series{iview}=i2_series;
811SeriesData.j1_series{iview}=j1_series;
812SeriesData.j2_series{iview}=j2_series;
813SeriesData.FileType{iview}=FileType;
814SeriesData.FileInfo{iview}=FileInfo;
815SeriesData.Time{iview}=Time;
816if ~isempty(TimeSource)
817    SeriesData.TimeSource=TimeSource;
818end
819% if ~isempty(TimeUnit)
820%     SeriesData.TimeUnit=TimeUnit;
821% end
822if check_calib
823    SeriesData.GeometryCalib{iview}=XmlData.GeometryCalib;
824end
825set(handles.series,'UserData',SeriesData)
826
827%% update pair menus
828ListView=get(handles.ListView,'String');
829ListView{iview}=num2str(iview);
830set(handles.ListView,'String',ListView);
831set(handles.ListView,'Value',iview)
832update_mode(handles,i1_series,i2_series,j1_series,j2_series,Time)
833
834%% enable j index visibility
835%check_jindex=~isempty(find(~cellfun(@isempty,SeriesData.j1_series))); %look for non empty j indices
836status_j='on';%default
837if isempty(find(~cellfun(@isempty,SeriesData.j1_series), 1)); % case of empty j indices
838    status_j='off'; % no j index needed
839elseif strcmp(get(handles.PairString,'Visible'),'on')
840        PairString=get(handles.PairString,'Data');       
841        check_burst=cellfun(@isempty,regexp(PairString,'^j'));%=0 for burst case, 1 otherwise
842 %   check_nopair=cellfun(@isempty,PairString);
843    if isempty(find(check_burst))% if all pair string begins by j (burst)
844        status_j='off'; % no j index needed for bust case
845    end
846end
847enable_j(handles,status_j) % no j index needed
848
849%% display the set of existing files as an image
850set(handles.FileStatus,'Units','pixels')
851Position=get(handles.FileStatus,'Position');
852set(handles.FileStatus,'Units','normalized')
853xI=0.5:Position(3)-0.5;
854nbview=numel(SeriesData.i1_series);
855pair_max=cell(1,nbview);
856for iview=1:nbview
857    pair_max{iview}=squeeze(max(SeriesData.i1_series{iview},[],1)); %max on pair index
858    if (strcmp(get(handles.num_first_j,'Visible'),'off')&& size(pair_max{iview},2)~=1)
859        pair_max{iview}=squeeze(max(pair_max{iview},[],1)); % consider only the i index
860    end
861    index_min(iview)=find(pair_max{iview}>0, 1 );
862    index_max(iview)=find(pair_max{iview}>0, 1, 'last' );
863end
864index_min=min(index_min);
865index_max=max(index_max);
866range_index=index_max-index_min+1;
867scale_y=Position(4)/nbview;
868scale_x=Position(3)/range_index;
869x=(0.5:range_index-0.5)*Position(3)/range_index;
870% y=(0.5:nbview-0.5)*Position(4)/nbview;
871range_y=max(1,floor(Position(4)/nbview));
872CData=zeros(nbview*range_y,floor(Position(3)));
873for iview=1:nbview
874    ind_y=1+(iview-1)*range_y:iview*range_y;
875    LineData=zeros(1,range_index);
876    x_index=find(pair_max{iview}>0)-index_min+1;
877    LineData(x_index)=1;
878    if numel(x)>1
879    LineData=interp1(x,LineData,xI,'nearest');
880    CData(ind_y,:)=ones(size(ind_y'))*LineData;
881    end
882end
883CData=cat(3,zeros(size(CData)),CData,zeros(size(CData)));
884set(handles.FileStatus,'CData',CData);
885
886
887%% enable field and veltype menus, in accordance with the current action
888ActionName_Callback([],[], handles)
889
890%% check for pair display
891check_pairs=0;
892for iview=1:numel(SeriesData.i2_series)
893    if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
894        check_pairs=1;
895    end
896end
897if check_pairs
898    set(handles.Pairs,'Visible','on')
899    set(handles.PairString,'Visible','on')
900else
901    set(handles.Pairs,'Visible','off')
902    set(handles.PairString,'Visible','off')
903end
904
905%% set length of waitbar
906displ_time(handles)
907
908
909%% set default options in menu 'Fields'
910switch FileType
911    case {'civx','civdata'}
912        [FieldList,ColorList]=set_field_list('U','V','C');
913        set(handles.FieldName,'String',[{'image'};FieldList;{'get_field...'}]);%standard menu for civx data
914        set(handles.FieldName,'Value',2) % set menu to 'velocity
915        set(handles.Coord_x,'Value',1);
916        set(handles.Coord_x,'String',{'X'});
917        set(handles.Coord_y,'Value',1);
918        set(handles.Coord_y,'String',{'Y'});
919    case 'netcdf'
920        set(handles.FieldName,'Value',1)
921        set(handles.FieldName,'String',{'get_field...'})
922        if isempty(i2_series)
923            i2=[];
924        else
925            i2=i2_series(1,ref_j+1,ref_i+1);
926        end
927        if isempty(j1_series)
928            j1=[];j2=[];
929        else
930            j1=j1_series(1,ref_j+1,ref_i+1);
931            if isempty(j2_series)
932                j2=[];
933            else
934                j2=j2_series(1,ref_j+1,ref_i+1);
935            end
936        end
937        FileName=fullfile_uvmat(InputTable{iview,1},InputTable{iview,2},InputTable{iview,3},InputTable{iview,5},InputTable{iview,4},i1_series(1,ref_j+1,ref_i+1),i2,j1,j2);
938%         hget_field=get_field(FileName);
939%         hhget_field=guidata(hget_field);
940%         get_field('RUN_Callback',hhget_field.RUN,[],hhget_field);
941    otherwise
942        set(handles.FieldName,'Value',1) % set menu to 'image'
943        set(handles.FieldName,'String',{'image'})
944        set(handles.Coord_x,'Value',1);
945        set(handles.Coord_x,'String',{'AX'});
946        set(handles.Coord_y,'Value',1);
947        set(handles.Coord_y,'String',{'AY'});
948end
949
950%------------------------------------------------------------------------
951function num_first_i_Callback(hObject, eventdata, handles)
952%------------------------------------------------------------------------
953num_last_i_Callback(hObject, eventdata, handles)
954
955%------------------------------------------------------------------------
956function num_last_i_Callback(hObject, eventdata, handles)
957%------------------------------------------------------------------------
958SeriesData=get(handles.series,'UserData');
959if ~isfield(SeriesData,'Time')
960    SeriesData.Time{1}=[];
961end
962displ_time(handles);
963
964%------------------------------------------------------------------------
965function num_first_j_Callback(hObject, eventdata, handles)
966%------------------------------------------------------------------------
967 num_last_j_Callback(hObject, eventdata, handles)
968
969%------------------------------------------------------------------------
970function num_last_j_Callback(hObject, eventdata, handles)
971%------------------------------------------------------------------------
972first_j=str2num(get(handles.num_first_j,'String'));
973last_j=str2num(get(handles.num_last_j,'String'));
974ref_j=ceil((first_j+last_j)/2);
975set(handles.num_ref_j,'String', num2str(ref_j))
976num_ref_j_Callback(hObject, eventdata, handles)
977SeriesData=get(handles.series,'UserData');
978if ~isfield(SeriesData,'Time')
979    SeriesData.Time{1}=[];
980end
981displ_time(handles);
982
983
984%------------------------------------------------------------------------
985% ---- find the times corresponding to the first and last indices of a series
986function displ_time(handles)
987%------------------------------------------------------------------------
988SeriesData=get(handles.series,'UserData');%
989ref_i=[str2num(get(handles.num_first_i,'String')) str2num(get(handles.num_last_i,'String'))];
990ref_j=[str2num(get(handles.num_first_j,'String')) str2num(get(handles.num_last_j,'String'))];
991TimeTable=get(handles.TimeTable,'Data');
992Pairs=get(handles.PairString,'Data');
993for iview=1:size(TimeTable,1)
994    if size(SeriesData.Time,1)<iview
995        break
996    end
997    i1=ref_i;
998    j1=ref_j;
999    i2=ref_i;
1000    j2=ref_j;
1001    % case of pairs
1002    if ~isempty(Pairs{iview,1})
1003        r=regexp(Pairs{iview,1},'(?<mode>(Di=)|(Dj=)) -*(?<num1>\d+)\|(?<num2>\d+)','names');
1004        if isempty(r)
1005            r=regexp(Pairs{iview,1},'(?<num1>\d+)(?<mode>-)(?<num2>\d+)','names');
1006        end
1007        switch r.mode
1008            case 'Di='  %  case 'series(Di)')
1009                i1=ref_i-str2num(r.num1);
1010                i2=ref_i+str2num(r.num2);
1011            case 'Dj='  %  case 'series(Dj)'
1012                j1=ref_j-str2num(r.num1);
1013                j2=ref_j+str2num(r.num2);
1014            case '-'  % case 'bursts'
1015                j1=str2num(r.num1)*ones(size(ref_i));
1016                j2=str2num(r.num2)*ones(size(ref_i));
1017        end
1018    end
1019    TimeTable{iview,2}=[];
1020    TimeTable{iview,3}=[];
1021    if size(SeriesData.Time{iview},1)>=i2(2)+1&&size(SeriesData.Time{iview},2)>=j2(2)+1
1022        if isempty(ref_j)
1023            time_first=(SeriesData.Time{iview}(i1(1)+1)+SeriesData.Time{iview}(i2(1)+1))/2;
1024            time_last=(SeriesData.Time{iview}(i1(2)+1)+SeriesData.Time{iview}(i2(2))+1)/2;
1025        else
1026            time_first=(SeriesData.Time{iview}(i1(1)+1,j1(1)+1)+SeriesData.Time{iview}(i2(1)+1,j2(1)+1))/2;
1027            time_last=(SeriesData.Time{iview}(i1(2)+1,j1(2)+1)+SeriesData.Time{iview}(i2(2)+1,j2(2)+1))/2;
1028        end
1029        TimeTable{iview,2}=time_first; %TODO: take into account pairs
1030        TimeTable{iview,3}=time_last; %TODO: take into account pairs
1031    end
1032end
1033set(handles.TimeTable,'Data',TimeTable)
1034
1035%% set the waitbar position with respect to the min and max in the series
1036for iview=1:numel(SeriesData.i1_series)
1037    pair_max{iview}=squeeze(max(SeriesData.i1_series{iview},[],1)); %max on pair index
1038    if (strcmp(get(handles.num_first_j,'Visible'),'off')&& size(pair_max{iview},2)~=1)
1039        pair_max{iview}=squeeze(max(pair_max{iview},[],1)); % consider only the i index
1040    end
1041    pair_max{iview}=reshape(pair_max{iview},1,[]);
1042    index_min(iview)=find(pair_max{iview}>0, 1 );
1043    index_max(iview)=find(pair_max{iview}>0, 1, 'last' );
1044end
1045[index_min,iview_min]=min(index_min);
1046[index_max,iview_max]=min(index_max);
1047if size(SeriesData.i1_series{iview_min},2)==1% movie
1048    index_first=ref_i(1);
1049    index_last=ref_i(2);
1050else
1051    index_first=(ref_i(1)-1)*(size(SeriesData.i1_series{iview_min},1))+ref_j(1)+1;
1052    index_last=(ref_i(2)-1)*(size(SeriesData.i1_series{iview_max},1))+ref_j(2)+1;
1053end
1054range=index_max-index_min+1;
1055coeff_min=(index_first-index_min)/range;
1056coeff_max=(index_last-index_min+1)/range;
1057Position=get(handles.Waitbar,'Position');% position of the waitbar:= [ x,y, width, height]
1058Position_status=get(handles.FileStatus,'Position');
1059Position(1)=coeff_min*Position_status(3)+Position_status(1);
1060Position(3)=Position_status(3)*(coeff_max-coeff_min);
1061set(handles.Waitbar,'Position',Position)
1062update_waitbar(handles.Waitbar,0)
1063
1064%------------------------------------------------------------------------
1065% --- Executes when selected cell(s) is changed in PairString.
1066function PairString_CellSelectionCallback(hObject, eventdata, handles)
1067%------------------------------------------------------------------------   
1068set(handles.ListView,'Value',eventdata.Indices(1))% detect the selected raw index
1069ListView_Callback ([],[],handles) % update the list of available pairs
1070
1071%------------------------------------------------------------------------
1072%------------------------------------------------------------------------
1073%  III - FUNCTIONS ASSOCIATED TO THE FRAME SET PAIRS
1074%------------------------------------------------------------------------
1075%------------------------------------------------------------------------
1076% --- Executes on selection change in ListView.
1077function ListView_Callback(hObject, eventdata, handles)
1078%------------------------------------------------------------------------   
1079SeriesData=get(handles.series,'UserData');
1080i2_series=[];
1081j2_series=[];
1082iview=get(handles.ListView,'Value');
1083if ~isempty(SeriesData.i2_series{iview})
1084    i2_series=SeriesData.i2_series{iview};
1085end
1086if ~isempty(SeriesData.j2_series{iview})
1087    j2_series=SeriesData.j2_series{iview};
1088end
1089update_mode(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1090    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1091
1092%------------------------------------------------------------------------
1093% --- Executes on button press in mode.
1094function mode_Callback(hObject, eventdata, handles)
1095%------------------------------------------------------------------------       
1096SeriesData=get(handles.series,'UserData');
1097iview=get(handles.ListView,'Value');
1098mode_list=get(handles.mode,'String');
1099mode=mode_list{get(handles.mode,'Value')};
1100if isequal(mode,'bursts')
1101    enable_i(handles,'On')
1102    enable_j(handles,'Off') %do not display j index scanning in burst mode (j is fixed by the burst choice)
1103else
1104    enable_i(handles,'On')
1105    enable_j(handles,'Off')
1106end
1107fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1108    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1109ListPairs_Callback([],[],handles)
1110
1111%-------------------------------------------------------------
1112% --- Executes on selection in ListPairs.
1113function ListPairs_Callback(hObject,eventdata,handles)
1114%------------------------------------------------------------
1115list_pair=get(handles.ListPairs,'String');%get the menu of image pairs
1116if isempty(list_pair)
1117    string='';
1118else
1119    string=list_pair{get(handles.ListPairs,'Value')};
1120    string=regexprep(string,',.*','');%removes time indication (after ',')
1121end
1122PairString=get(handles.PairString,'Data');
1123iview=get(handles.ListView,'Value');
1124PairString{iview,1}=string;
1125% report the selected pair string to the table PairString
1126set(handles.PairString,'Data',PairString)
1127
1128%------------------------------------------------------------------------
1129function num_ref_i_Callback(hObject, eventdata, handles)
1130%------------------------------------------------------------------------
1131mode_list=get(handles.mode,'String');
1132mode=mode_list{get(handles.mode,'Value')};
1133SeriesData=get(handles.series,'UserData');
1134iview=get(handles.ListView,'Value');
1135fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1136    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview});% update the menu of pairs depending on the available netcdf files
1137ListPairs_Callback([],[],handles)
1138
1139%------------------------------------------------------------------------
1140function num_ref_j_Callback(hObject, eventdata, handles)
1141%------------------------------------------------------------------------
1142num_ref_i_Callback(hObject, eventdata, handles)
1143
1144%------------------------------------------------------------------------
1145function update_mode(handles,i1_series,i2_series,j1_series,j2_series,time)
1146%------------------------------------------------------------------------   
1147% check_burst=0;
1148if isempty(j2_series)% no j pair
1149    if isempty(i2_series)
1150        set(handles.mode,'Value',1)
1151        set(handles.mode,'String',{''})% no pair menu to display
1152    else   
1153        set(handles.mode,'Value',1)
1154        set(handles.mode,'String',{'series(Di)'}) % pair menu with only option Di
1155    end
1156else %existence of j pairs
1157    pair_max=squeeze(max(i1_series,[],1)); %max on pair index
1158    j_max=max(pair_max,[],1);
1159    MaxIndex_i=max(find(j_max))-1;% max ref index i
1160    MinIndex_i=min(find(j_max))-1;% min ref index i
1161    i_max=max(pair_max,[],2);
1162    MaxIndex_j=max(find(i_max))-1;% max ref index i
1163    MinIndex_j=min(find(i_max))-1;% min ref index i
1164    if MaxIndex_j==MinIndex_j
1165        set(handles.mode,'Value',1);
1166        set(handles.mode,'String',{'bursts'})
1167%         check_burst=1;
1168    elseif MaxIndex_i==MinIndex_i
1169        set(handles.mode,'Value',1);
1170        set(handles.mode,'String',{'series(Dj)'})
1171    else
1172        set(handles.mode,'String',{'bursts';'series(Dj)'})
1173        if (MaxIndex_j-MinIndex_j)>10
1174            set(handles.mode,'Value',2);%set mode to series(Dj) if more than 10 j values
1175        else
1176            set(handles.mode,'Value',1);
1177%             check_burst=1;
1178        end
1179    end
1180end
1181fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1182ListPairs_Callback([],[],handles)
1183
1184%--------------------------------------------------------------
1185% determine the menu for pairstring depending on existing netcdf files
1186% with the reference indices num_ref_i and num_ref_j
1187%----------------------------------------------------------------
1188function fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1189
1190mode_list=get(handles.mode,'String');
1191mode=mode_list{get(handles.mode,'Value')};
1192ref_i=str2num(get(handles.num_ref_i,'String'));
1193if isempty(ref_i)
1194    ref_i=1;
1195end
1196if strcmp(get(handles.num_ref_j,'Visible'),'on')
1197    ref_j=str2num(get(handles.num_ref_j,'String'));
1198    if isempty(ref_j)
1199        ref_j=1;
1200    end
1201else
1202    ref_j=1;
1203end
1204TimeUnit=get(handles.TimeUnit,'String');
1205if length(TimeUnit)>=1
1206    dtunit=['m' TimeUnit];
1207else
1208    dtunit='e-03';
1209end
1210
1211displ_pair={};
1212if strcmp(mode,'series(Di)')
1213    if isempty(i2_series)
1214        msgbox_uvmat('ERROR','no i1-i2 pair available')
1215        return
1216    end
1217    diff_i=i2_series-i1_series;
1218    min_diff=min(diff_i(diff_i>0));
1219    max_diff=max(diff_i(diff_i>0));
1220    for ipair=min_diff:max_diff
1221        if numel(diff_i(diff_i==ipair))>0
1222            pair_string=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1223            if ~isempty(time)
1224                if ref_i<=floor(ipair/2)
1225                    ref_i=floor(ipair/2)+1;% shift ref_i to get the first pair
1226                end
1227                Dt=time(ref_i+ceil(ipair/2),ref_j)-time(ref_i-floor(ipair/2),ref_j);
1228                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1229            end
1230            displ_pair=[displ_pair;{pair_string}];
1231        end
1232    end
1233    if ~isempty(displ_pair)
1234        displ_pair=[displ_pair;{'Di=*|*'}];
1235    end
1236elseif strcmp(mode,'series(Dj)')
1237    if isempty(j2_series)
1238        msgbox_uvmat('ERROR','no j1-j2 pair available')
1239        return
1240    end
1241    diff_j=j2_series-j1_series;
1242    min_diff=min(diff_j(diff_j>0));
1243    max_diff=max(diff_j(diff_j>0));
1244    for ipair=min_diff:max_diff
1245        if numel(diff_j(diff_j==ipair))>0
1246            pair_string=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1247            if ~isempty(time)
1248                if ref_j<=floor(ipair/2)
1249                    ref_j=floor(ipair/2)+1;% shift ref_i to get the first pair
1250                end
1251                Dt=time(ref_i,ref_j+ceil(ipair/2))-time(ref_i,ref_j-floor(ipair/2));
1252                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1253            end
1254            displ_pair=[displ_pair;{pair_string}];
1255        end
1256    end
1257    if ~isempty(displ_pair)
1258        displ_pair=[displ_pair;{'Dj=*|*'}];
1259    end
1260elseif strcmp(mode,'bursts')
1261    if isempty(j2_series)
1262        msgbox_uvmat('ERROR','no j1-j2 pair available')
1263        return
1264    end
1265    diff_j=j2_series-j1_series;
1266    min_j1=min(j1_series(j1_series>0));
1267    max_j1=max(j1_series(j1_series>0));
1268    min_j2=min(j2_series(j2_series>0));
1269    max_j2=max(j2_series(j2_series>0));
1270    for pair1=min_j1:min(max_j1,min_j1+20)
1271        for pair2=min_j2:min(max_j2,min_j2+20)
1272        if numel(j1_series(j1_series==pair1))>0 && numel(j2_series(j2_series==pair2))>0
1273            displ_pair=[displ_pair;{['j= ' num2str(pair1) '-' num2str(pair2)]}];
1274        end
1275        end
1276    end
1277    if ~isempty(displ_pair)
1278        displ_pair=[displ_pair;{'j=*-*'}];
1279    end
1280end
1281set(handles.num_ref_i,'String',num2str(ref_i)) % update ref_i and ref_j
1282set(handles.num_ref_j,'String',num2str(ref_j))
1283
1284%% display list of pairstring
1285displ_pair_list=get(handles.ListPairs,'String');
1286NewVal=[];
1287if ~isempty(displ_pair_list)
1288Val=get(handles.ListPairs,'Value');
1289NewVal=find(strcmp(displ_pair_list{Val},displ_pair),1);% look at the previous display in the new menu displ_pï¿œir
1290end
1291if ~isempty(NewVal)
1292    set(handles.ListPairs,'Value',NewVal)
1293else
1294    set(handles.ListPairs,'Value',1)
1295end
1296set(handles.ListPairs,'String',displ_pair)
1297
1298%-------------------------------------
1299function enable_i(handles,state)
1300set(handles.i_txt,'Visible',state)
1301set(handles.num_first_i,'Visible',state)
1302set(handles.num_last_i,'Visible',state)
1303set(handles.num_incr_i,'Visible',state)
1304set(handles.num_ref_i,'Visible',state)
1305set(handles.ref_i_text,'Visible',state)
1306
1307%-----------------------------------
1308function enable_j(handles,state)
1309set(handles.j_txt,'Visible',state)
1310set(handles.num_first_j,'Visible',state)
1311set(handles.num_last_j,'Visible',state)
1312set(handles.num_incr_j,'Visible',state)
1313set(handles.num_ref_j,'Visible',state)
1314set(handles.ref_j_text,'Visible',state)
1315set(handles.MinIndex_j,'Visible',state)
1316set(handles.MaxIndex_j,'Visible',state)
1317
1318
1319%%%%%%%%%%%%%%%%%%%%
1320%%  MAIN ActionName FUNCTIONS
1321%%%%%%%%%%%%%%%%%%%%
1322%------------------------------------------------------------------------
1323% --- Executes on button press in RUN.
1324function RUN_Callback(hObject, eventdata, handles)
1325%------------------------------------------------------------------------
1326
1327set(handles.RUN,'BusyAction','queue');
1328set(0,'CurrentFigure',handles.series)
1329set(handles.RUN, 'Enable','Off')
1330set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1331drawnow
1332
1333%% read the input parameters and set the output dir and nomenclature
1334[Series,OutputDir,errormsg]=prepare_jobs(handles);% get parameters form the GUI series
1335if ~isempty(errormsg)
1336    if ~strcmp(errormsg,'Cancel')
1337    msgbox_uvmat('ERROR',errormsg)
1338    end
1339    STOP_Callback([],[], handles)
1340    return
1341end
1342OutputNomType=nomtype2pair(Series.InputTable{1,4});% nomenclature for output files
1343DirXml=fullfile(OutputDir,'0_XML');
1344if ~exist(DirXml,'dir')
1345    [tild,msg1]=mkdir(DirXml);
1346    if ~strcmp(msg1,'')
1347        msgbox_uvmat('ERROR',['cannot create ' DirXml ': ' msg1]);%error message for directory creation
1348        return
1349    end
1350end
1351
1352%% select the Action modes
1353RunMode='local';%default
1354if isfield(Series.Action,'RunMode')
1355RunMode=Series.Action.RunMode;
1356end
1357ActionExt='.m';%default
1358if isfield(Series.Action,'ActionExt')
1359    ActionExt=Series.Action.ActionExt;% '.m' or '.sh' (compiled)
1360end
1361ActionName=Series.Action.ActionName;
1362ActionPath=Series.Action.ActionPath;
1363path_series=fileparts(which('series'));
1364
1365%% create the Action fct handle if RunMode option = 'local'
1366if strcmp(RunMode,'local')
1367    if ~isequal(ActionPath,path_series)
1368        eval(['spath=which(''' ActionName ''');']) %spath = current path of the selected function ACTION
1369        if ~exist(ActionPath,'dir')
1370            msgbox_uvmat('ERROR',['The prescribed function path ' ActionPath ' does not exist']);
1371            return
1372        end
1373        if ~isequal(spath,ActionPath)
1374            addpath(ActionPath)% add the prescribed path if not the current one
1375        end
1376    end
1377    eval(['h_fun=@' ActionName ';'])%create a function handle for ACTION
1378    if ~isequal(ActionPath,path_series)
1379        rmpath(ActionPath)% add the prescribed path if not the current one
1380    end
1381end
1382
1383%% Get RunTime code from the file PARAM.xml (needed to run compiled functions)
1384errormsg='';%default error message
1385xmlfile=fullfile(path_series,'PARAM.xml');
1386test_batch=0;%default: ,no batch mode available
1387if ~exist(xmlfile,'file')
1388    [success,message]=copyfile(fullfile(path_series,'PARAM.xml.default'),xmlfile);
1389end
1390RunTime='';
1391if strcmp(ActionExt,'.sh')
1392    if exist(xmlfile,'file')
1393        s=xml2struct(xmlfile);
1394        if strcmp(RunMode,'cluster_oar') && isfield(s,'BatchParam')
1395            if isfield(s.BatchParam,'RunTime')
1396                RunTime=s.BatchParam.RunTime;
1397            end
1398            if isfield(s.BatchParam,'NbCore')
1399                NbCore=s.BatchParam.NbCore;
1400            end
1401        elseif (strcmp(RunMode,'background')||strcmp(RunMode,'local')) && isfield(s,'RunParam')
1402            if isfield(s.RunParam,'RunTime')
1403                RunTime=s.RunParam.RunTime;
1404            end
1405            if isfield(s.RunParam,'NbCore')
1406                NbCore=s.RunParam.NbCore;
1407            end
1408        end
1409    end
1410    if isempty(RunTime) && strcmp(RunMode,'cluster_oar')
1411        msgbox_uvmat('ERROR','RunTime name not found in PARAM.xml, compiled version .sh cannot run on cluster')
1412        return
1413    end
1414%     Series.RunTime=RunTime;
1415end
1416
1417%% set nbre of cluster cores and processes
1418switch RunMode
1419    case {'local','background'}
1420        NbCore=1;% no need to split the calculation
1421    case 'cluster_oar'
1422        if strcmp(Series.Action.ActionExt,'.m')% case of Matlab function (uncompiled)
1423            NbCore=1;% one core used only (limitation of Matlab licences)
1424            msgbox_uvmat('WARNING','Number of cores =1: select the compiled version civ_matlab.sh for multi-core processing');
1425            extra_oar='';
1426        else
1427            answer=inputdlg({'Number of cores (max 36)','extra oar options'},'oarsub parameter',1,{'12',''});
1428            NbCore=str2double(answer{1});
1429            extra_oar=answer{2};
1430        end
1431end
1432if ~isfield(Series.IndexRange,'NbSlice')
1433    Series.IndexRange.NbSlice=[];
1434end
1435if isempty(Series.IndexRange.NbSlice)
1436    NbProcess=NbCore;% choose one process per core
1437else
1438    NbProcess=Series.IndexRange.NbSlice;% the nbre of run processes is equal to the number of slices
1439    NbCore=min(NbCore,NbProcess);% at least one process per core
1440end
1441       
1442
1443%% read index ranges
1444[first_i,incr_i,last_i,first_j,incr_j,last_j,errormsg]=get_index_range(Series.IndexRange);
1445if ~isempty(errormsg)
1446    msgbox_uvmat('ERROR',['series/Run_Callback/get_index_range' errormsg])
1447    set(handles.RUN, 'Enable','On'),
1448    set(handles.RUN,'BackgroundColor',[1 0 0])
1449    return
1450else
1451    BlockLength=ceil(numel(first_i:incr_i:last_i)/NbProcess);
1452end
1453nbfield_j=numel(first_j:incr_j:last_j);
1454
1455%% record nbre of output files and starting time for computation for status
1456StatusData=get(handles.status,'UserData');
1457if isfield(StatusData,'OutputFileMode')
1458    switch StatusData.OutputFileMode
1459        case 'NbInput'
1460            StatusData.NbOutputFile=numel(first_i:incr_i:last_i)*numel(first_j:incr_j:last_j);
1461        case 'NbInput_i'
1462            StatusData.NbOutputFile=numel(first_i:incr_i:last_i);
1463        case 'NbSlice'   
1464            StatusData.NbOutputFile=str2num(get(handles.num_NbSlice,'String'));
1465    end
1466end
1467StatusData.TimeStart=now;
1468set(handles.status,'UserData',StatusData)
1469
1470%% direct processing on the current Matlab session
1471if strcmp (RunMode,'local')
1472%     Series.RUNHandle=handles.RUN;
1473%     Series.WaitbarHandle=handles.Waitbar;
1474    for iprocess=1:NbProcess
1475        if isempty(Series.IndexRange.NbSlice)
1476            Series.IndexRange.first_i=first_i+(iprocess-1)*BlockLength*incr_i;
1477            if Series.IndexRange.first_i>last_i
1478                break
1479            end
1480            Series.IndexRange.last_i=min(first_i+(iprocess)*BlockLength*incr_i-1,last_i);
1481        else
1482            Series.IndexRange.first_i= first_i+incr_i*(iprocess-1);
1483            Series.IndexRange.incr_i=incr_i*Series.IndexRange.NbSlice;
1484        end
1485        t=struct2xml(Series);
1486        t=set(t,1,'name','Series');
1487        filexml=fullfile_uvmat(DirXml,'',Series.InputTable{1,3},'.xml',OutputNomType,...
1488            Series.IndexRange.first_i,Series.IndexRange.last_i,first_j,last_j);
1489        save(t,filexml);
1490        switch ActionExt
1491            case '.m'
1492                h_fun(Series);
1493            case '.sh'
1494                switch computer
1495                    case {'PCWIN','PCWIN64'} %Windows system
1496                        filexml=regexprep(filexml,'\\','\\\\');% add '\' so that '\' are left as characters
1497                        system([fullfile(ActionPath,[ActionName '.sh']) ' ' RunTime ' ' filexml]);% TODO: adapt to DOS system
1498                    case {'GLNX86','GLNXA64','MACI64'}%Linux  system
1499                        system([fullfile(ActionPath,[ActionName '.sh']) ' ' RunTime ' ' filexml]);
1500                end
1501        end
1502    end
1503elseif strcmp(get(handles.OutputDirExt,'Visible'),'off')
1504    msgbox_uvmat('ERROR',['no output file for Action ' ActionName ', use run mode = local']);% a output dir is needed for background option
1505    return
1506else
1507    %% processing on a different session of the same computer (background) or cluster, create executable files
1508    batch_file_list=cell(NbProcess,1);% initiate the list of executable files
1509    DirBat=fullfile(OutputDir,'0_BAT');
1510    %create subdirectory for executable files
1511    if ~exist(DirBat,'dir')
1512        [tild,msg1]=mkdir(DirBat);
1513        if ~strcmp(msg1,'')
1514            msgbox_uvmat('ERROR',['cannot create ' DirBat ': ' msg1]);%error message for directory creation
1515            return
1516        end
1517    end
1518    %create subdirectory for log files
1519    DirLog=fullfile(OutputDir,'0_LOG');
1520    if ~exist(DirLog,'dir')
1521        [tild,msg1]=mkdir(DirLog);
1522        if ~strcmp(msg1,'')
1523            msgbox_uvmat('ERROR',['cannot create ' DirLog ': ' msg1]);%error message for directory creation
1524            return
1525        end
1526    end
1527    for iprocess=1:NbProcess
1528        if isempty(Series.IndexRange.NbSlice)% process by blocks of i index
1529            Series.IndexRange.first_i=first_i+(iprocess-1)*BlockLength*incr_i;
1530            if Series.IndexRange.first_i>last_i
1531                NbProcess=iprocess-1;
1532                break% leave the loop, we are at the end of the calculation
1533            end
1534            Series.IndexRange.last_i=min(last_i,first_i+(iprocess)*BlockLength*incr_i-1);
1535        else% process by slices of i index if NbSlice is defined, computation in a single process if NbSlice =1
1536            Series.IndexRange.first_i= first_i+iprocess-1;
1537            Series.IndexRange.incr_i=incr_i*Series.IndexRange.NbSlice;
1538        end
1539       
1540        % create, fill and save the xml parameter file
1541        t=struct2xml(Series);
1542        t=set(t,1,'name','Series');
1543        filexml=fullfile_uvmat(DirXml,'',Series.InputTable{1,3},'.xml',OutputNomType,...
1544            Series.IndexRange.first_i,Series.IndexRange.last_i,first_j,last_j);
1545        save(t,filexml);% save the parameter file
1546       
1547        %create the executable file
1548%         filebat=fullfile_uvmat(DirBat,'',Series.InputTable{1,3},'.bat',OutputNomType,...
1549%             Series.IndexRange.first_i,Series.IndexRange.last_i,first_j,last_j);
1550         filebat=fullfile_uvmat(DirBat,'',Series.InputTable{1,3},'.sh',OutputNomType,...
1551           Series.IndexRange.first_i,Series.IndexRange.last_i,first_j,last_j);
1552        batch_file_list{iprocess}=filebat;
1553        [fid,message]=fopen(filebat,'w');% create the executable file
1554        if isequal(fid,-1)
1555            msgbox_uvmat('ERROR', ['creation of .bat file: ' message]);
1556            return
1557        end
1558       
1559        % set the log file name
1560        filelog=fullfile_uvmat(DirLog,'',Series.InputTable{1,3},'.log',OutputNomType,...
1561            Series.IndexRange.first_i,Series.IndexRange.last_i,first_j,last_j);
1562       
1563        % fill and save the executable file
1564        switch ActionExt
1565            case '.m'% Matlab function
1566                switch computer
1567                    case {'GLNX86','GLNXA64','MACI64'}
1568                        cmd=[...
1569                            '#!/bin/bash \n'...
1570                            '. /etc/sysprofile \n'...
1571                            'matlab -nodisplay -nosplash -nojvm -logfile ''' filelog ''' <<END_MATLAB \n'...
1572                            'addpath(''' path_series '''); \n'...
1573                            'addpath(''' Series.Action.ActionPath '''); \n'...
1574                            '' Series.Action.ActionName  '( ''' filexml '''); \n'...
1575                            'exit \n'...
1576                            'END_MATLAB \n'];
1577                        fprintf(fid,cmd);%fill the executable file with the  char string cmd
1578                        fclose(fid);% close the executable file
1579                        system(['chmod +x ' filebat]);% set the file to executable
1580                    case {'PCWIN','PCWIN64'}
1581                        text_matlabscript=['matlab -automation -logfile ' regexprep(filelog,'\\','\\\\')...
1582                            ' -r "addpath(''' regexprep(path_series,'\\','\\\\') ''');'...
1583                            'addpath(''' regexprep(Series.Action.ActionPath,'\\','\\\\') ''');'...
1584                            '' Series.Action.ActionName  '( ''' regexprep(filexml,'\\','\\\\') ''');exit"'];
1585                        fprintf(fid,text_matlabscript);%fill the executable file with the  char string cmd
1586                        fclose(fid);% close the executable file
1587                end
1588            case '.sh' % compiled Matlab function
1589                switch computer
1590                    case {'GLNX86','GLNXA64','MACI64'}
1591                        cmd=['#!/bin/bash \n '...
1592                            '#$ -cwd \n '...
1593                            'hostname && date \n '...
1594                            'umask 002 \n'...
1595                            fullfile(ActionPath,[ActionName '.sh']) ' ' RunTime ' ' filexml];%allow writting access to created files for user group
1596                        fprintf(fid,cmd);%fill the executable file with the  char string cmd
1597                        fclose(fid);% close the executable file
1598                        system(['chmod +x ' filebat]);% set the file to executable
1599                       
1600                    case {'PCWIN','PCWIN64'}    %       TODO: adapt to Windows system
1601                        %                                 cmd=['matlab -automation -logfile ' regexprep(filelog,'\\','\\\\')...
1602                        %                                     ' -r "addpath(''' regexprep(path_series,'\\','\\\\') ''');'...
1603                        %                                     'addpath(''' regexprep(Series.Action.ActionPath,'\\','\\\\') ''');'...
1604                        %                                     '' Series.Action.ActionName  '( ''' regexprep(filexml,'\\','\\\\') ''');exit"'];
1605                        fprintf(fid,cmd);
1606                        fclose(fid);
1607                        %                               dos([filebat ' &']);
1608                end
1609        end
1610    end
1611end
1612
1613%% launch the executable files for background or cluster processing
1614switch RunMode
1615    case 'background'
1616        for iprocess=1:NbProcess
1617            system([batch_file_list{iprocess} ' &'])% directly execute the command file for each process
1618        end
1619    case 'cluster_oar' % option 'oar-parexec' used
1620        %create subdirectory for oar command and log files
1621        DirOAR=fullfile(OutputDir,'0_OAR');
1622        if ~exist(DirOAR,'dir')
1623            [tild,msg1]=mkdir(DirOAR);
1624            if ~strcmp(msg1,'')
1625                msgbox_uvmat('ERROR',['cannot create ' DirOAR ': ' msg1]);%error message for directory creation
1626                return
1627            end
1628        end
1629        max_walltime=3600*12; % 12h max total calculation
1630        walltime_onejob=600;%seconds, max estimated time for asingle file index value
1631        filename_joblist=fullfile(DirOAR,'job_list.txt');%create name of the global executable file
1632        fid=fopen(filename_joblist,'w');
1633        for p=1:length(batch_file_list)
1634            fprintf(fid,[batch_file_list{p} '\n']);% list of exe files
1635        end
1636        fclose(fid);
1637        system(['chmod +x ' filename_joblist]);% set the file to executable
1638        oar_command=['oarsub -n CIVX '...
1639            '-t idempotent --checkpoint ' num2str(walltime_onejob+60) ' '...
1640            '-l /core=' num2str(NbCore) ','...
1641            'walltime=' datestr(min(1.05*walltime_onejob/86400*max(NbProcess*BlockLength*nbfield_j,NbCore)/NbCore,max_walltime/86400),13) ' '...
1642            '-E ' regexprep(filename_joblist,'\.txt\>','.stderr') ' '...
1643            '-O ' regexprep(filename_joblist,'\.txt\>','.stdout') ' '...
1644            extra_oar ' '...
1645            '"oar-parexec -s -f ' filename_joblist ' '...
1646            '-l ' filename_joblist '.log"\n'];
1647        filename_oarcommand=fullfile(DirOAR,'oar_command');
1648        fid=fopen(filename_oarcommand,'w');
1649        fprintf(fid,oar_command);
1650        fclose(fid);
1651        fprintf(oar_command);% display in command line
1652        %system(['chmod +x ' oar_command]);% set the file to executable
1653        system(oar_command);     
1654end
1655
1656%% reset the GUI series
1657update_waitbar(handles.Waitbar,1); % put the waitbar to end position to indicate launching is finished
1658set(handles.RUN, 'Enable','On')
1659set(handles.RUN,'BackgroundColor',[1 0 0])
1660set(handles.RUN, 'Value',0)
1661
1662%------------------------------------------------------------------------
1663function STOP_Callback(hObject, eventdata, handles)
1664%------------------------------------------------------------------------
1665set(handles.RUN, 'BusyAction','cancel')
1666set(handles.RUN,'BackgroundColor',[1 0 0])
1667set(handles.RUN,'enable','on')
1668set(handles.RUN, 'Value',0)
1669
1670% %------------------------------------------------------------------------
1671% % --- Executes on button press in BIN.
1672% function BIN_Callback(hObject, eventdata, handles)
1673% %------------------------------------------------------------------------
1674%     cmd=['#!/bin/bash \n '...
1675%         '#$ -cwd \n '...
1676%         'hostname && date \n '...
1677%         'umask 002 \n'...
1678%         Param.xml.CivmBin ' ' Param.xml.RunTime ' ' filename_xml ' ' OutputFile '.nc'];
1679%     
1680%------------------------------------------------------------------------
1681% --- Main launch command, called by RUN and BATCH
1682
1683function [Series,OutputDir,errormsg]=prepare_jobs(handles)
1684%INPUT:
1685% handles: handles of graphic objects on the GUI series
1686
1687%------------------------------------------------------------------------
1688OutputDir='';
1689errormsg='';
1690
1691%% Read parameters from series
1692Series=read_GUI(handles.series);
1693
1694%% get_field GUI
1695% if isfield(Series,'InputFields')&&isfield(Series.InputFields,'Field')
1696%     if strcmp(Series.InputFields.Field,'get_field...')
1697%         hget_field=findobj(allchild(0),'name','get_field');
1698%         Series.GetField=read_GUI(hget_field);
1699%     end
1700% end
1701
1702%% create the output data directory
1703%determine the root file corresponding to the first sub dir
1704if get(handles.RUN,'value') && isfield(Series,'OutputSubDir')
1705    SubDirOut=[get(handles.OutputSubDir,'String') Series.OutputDirExt];
1706    SubDirOutNew=SubDirOut;
1707    SeriesData=get(handles.series,'UserData');
1708%     if size(Series.InputTable,1)>1 && isfield(SeriesData,'AllowInputSort') && SeriesData.AllowInputSort
1709%         [tild,iview]=sort(Series.InputTable(:,2)); %subdirectories sorted in alphabetical order
1710%         Series.InputTable=Series.InputTable(iview,:);
1711%     end
1712    detect=exist(fullfile(Series.InputTable{1,1},SubDirOutNew),'dir');% test if  the dir  already exist
1713    check_create=1; %need to create the result directory by default
1714    while detect
1715        answer=msgbox_uvmat('INPUT_Y-N',['use existing ouput directory: ' fullfile(Series.InputTable{1,1},SubDirOutNew) ', possibly delete previous data']);
1716        if strcmp(answer,'Cancel')
1717            errormsg='Cancel';
1718            return
1719        elseif strcmp(answer,'Yes')
1720            detect=0;
1721            check_create=0;
1722        else
1723            r=regexp(SubDirOutNew,'(?<root>.*\D)(?<num1>\d+)$','names');%detect whether name ends by a number
1724            if isempty(r)
1725                r(1).root=[SubDirOutNew '_'];
1726                r(1).num1='0';
1727            end
1728            SubDirOutNew=[r(1).root num2str(str2num(r(1).num1)+1)];%increment the index by 1 or put 1
1729            detect=exist(fullfile(Series.InputTable{1,1},SubDirOutNew),'dir');% test if  the dir  already exists   
1730            check_create=1;
1731        end
1732    end
1733    Series.OutputDirExt=regexprep(SubDirOutNew,Series.OutputSubDir,'');
1734    Series.OutputRootFile=Series.InputTable{1,3};% the first sorted RootFile taken for output
1735    set(handles.OutputDirExt,'String',Series.OutputDirExt)
1736    OutputDir=fullfile(Series.InputTable{1,1},[Series.OutputSubDir Series.OutputDirExt]);% full name (with path) of output directory
1737    if check_create    % create output directory if it does not exist
1738        [tild,msg1]=mkdir(OutputDir);
1739        if ~strcmp(msg1,'')
1740            errormsg=['cannot create ' OutputDir ': ' msg1];%error message for directory creation
1741            return
1742        end
1743    end
1744   % RootOut=fullfile(OutputDir,Series.InputTable{1,3});% name of the parameter xml file set in this directory
1745end
1746
1747%% removes unused information on Series
1748if isfield(Series,'Pairs')
1749    Series=rmfield(Series,'Pairs'); %info Pairs not needed for output
1750end
1751Series.IndexRange=rmfield(Series.IndexRange,'TimeTable');
1752% Series.IndexRange=rmfield(Series.IndexRange,'MinIndex_j');
1753% Series.IndexRange=rmfield(Series.IndexRange,'MaxIndex_i');
1754empty_line=false(size(Series.InputTable,1),1);
1755for iline=1:size(Series.InputTable,1)
1756    empty_line(iline)=isequal(Series.InputTable(iline,1:3),{'','',''});
1757end
1758Series.InputTable(empty_line,:)=[];
1759
1760%------------------------------------------------------------------------
1761% --- Executes on selection change in ActionName.
1762function ActionName_Callback(hObject, eventdata, handles)
1763%------------------------------------------------------------------------
1764%% stop any ongoing series processing
1765if isequal(get(handles.RUN,'Value'),1)
1766    answer= msgbox_uvmat('INPUT_Y-N','stop current Action process?');
1767    if strcmp(answer,'Yes')
1768        STOP_Callback(hObject, eventdata, handles)
1769    else
1770        return
1771    end
1772end
1773set(handles.ActionName,'BackgroundColor',[1 1 0])
1774drawnow
1775
1776%% get Action name and path
1777nb_builtin_ACTION=4; %nbre of functions initially proposed in the menu ActionName (as defined in the Opening fct of series)
1778ActionList=get(handles.ActionName,'String');% list menu fields
1779ActionIndex=get(handles.ActionName,'Value');
1780if ~isequal(ActionIndex,1)% if we are not just opening series
1781    InputTable=get(handles.InputTable,'Data');
1782    if isempty(InputTable{1,4})
1783        msgbox_uvmat('ERROR','no input file available: use Open in the menu bar')
1784        return
1785    end
1786end
1787ActionName= ActionList{get(handles.ActionName,'Value')}; % selected function name
1788ActionPathList=get(handles.ActionName,'UserData');%list of recorded paths to functions of the list ActionName
1789
1790%% add a new function to the menu if 'more...' has been selected in the menu ActionName
1791if isequal(ActionName,'more...')
1792    [FileName, PathName] = uigetfile( ...
1793        {'*.m', ' (*.m)';
1794        '*.m',  '.m files '; ...
1795        '*.*', 'All Files (*.*)'}, ...
1796        'Pick a series processing function ',get(handles.ActionPath,'String'));
1797    if length(FileName)<2
1798        return
1799    end
1800    [ActionPath,ActionName,ActionExt]=fileparts(FileName);
1801   
1802    % insert the choice in the menu ActionName
1803    ActionIndex=find(strcmp(ActionName,ActionList),1);% look for the selected function in the menu Action
1804    if isempty(ActionIndex)%the input string does not exist in the menu
1805        ActionIndex= length(ActionList);
1806        ActionList=[ActionList(1:end-1);{ActionName};ActionList(end)];% the selected function is appended in the menu, before the last item 'more...'
1807        set(handles.ActionName,'String',ActionList)
1808    end
1809   
1810    % record the file extension and extend the path list if it is a new extension
1811    ActionExtList=get(handles.ActionExt,'String');
1812    ActionExtIndex=find(strcmp(ActionExt,ActionExtList), 1);
1813    if isempty(ActionExtIndex)
1814        set(handles.ActionExt,'String',[ActionExtList;{ActionExt}])
1815        ActionExtIndex=numel(ActionExtList)+1;
1816        ActionPathNew=cell(size(ActionPathList,1),1);%new column of ActionPath
1817        ActionPathList=[ActionPathList ActionPathNew];
1818    end
1819    set(handles.ActionName,'UserData',ActionPathList);
1820
1821    % remove old Action options in the menu (keeping a menu length <nb_builtin_ACTION+5)
1822    if length(ActionList)>nb_builtin_ACTION+5; %nb_builtin=nbre of functions always remaining in the initial menu
1823        nbremove=length(ActionList)-nb_builtin_ACTION-5;
1824        ActionList(nb_builtin_ACTION+1:end-5)=[];
1825        ActionPathList(nb_builtin_ACTION+1:end-4,:)=[];
1826        ActionIndex=ActionIndex-nbremove;
1827    end
1828   
1829    % record action menu, choice and path
1830    set(handles.ActionName,'Value',ActionIndex)
1831    set(handles.ActionName,'String',ActionList)
1832    set(handles.ActionExt,'Value',ActionExtIndex)
1833    ActionPathList{ActionIndex,ActionExtIndex}=PathName;
1834       
1835    %record the user defined menu additions in personal file profil_perso
1836    dir_perso=prefdir;
1837    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
1838    if nb_builtin_ACTION+1<=numel(ActionList)-1
1839        ActionListUser=ActionList(nb_builtin_ACTION+1:numel(ActionList)-1);
1840        ActionPathListUser=ActionPathList(nb_builtin_ACTION+1:numel(ActionList)-1,:);
1841        ActionExtListUser={};
1842        if numel(ActionExtList)>2
1843            ActionExtListUser=ActionExtList(3:end);
1844        end
1845        if exist(profil_perso,'file')
1846            save(profil_perso,'ActionListUser','ActionPathListUser','ActionExtListUser','-append')
1847        else
1848            save(profil_perso,'ActionListUser','ActionPathListUser','ActionExtListUser','-V6')
1849        end
1850    end
1851end
1852
1853%% check the current ActionPath to the selected function
1854ActionPath=ActionPathList{ActionIndex};%current recorded path
1855set(handles.ActionPath,'String',ActionPath); %show the path to the senlected function
1856
1857%% reinitialise the waitbar
1858update_waitbar(handles.Waitbar,0)
1859
1860%% default setting for the visibility of the GUI elements
1861% set(handles.FieldTransform,'Visible','off')
1862% set(handles.CheckObject,'Visible','off');
1863% set(handles.ProjObject,'Visible','off');
1864% set(handles.CheckMask,'Visible','off')
1865% set(handles.Mask,'Visible','off')
1866
1867%% create the function handle for Action
1868path_series=which('series');
1869if ~isequal(ActionPath,path_series)
1870    eval(['spath=which(''' ActionName ''');']) %spath = current path of the selected function ACTION
1871    if ~exist(ActionPath,'dir')
1872        errormsg=['The prescribed function path ' ActionPath ' does not exist'];
1873        return
1874    end
1875    if ~isequal(spath,ActionPath)
1876        addpath(ActionPath)% add the prescribed path if not the current one
1877    end
1878end
1879eval(['h_fun=@' ActionName ';'])%create a function handle for ACTION
1880if ~isequal(ActionPath,path_series)
1881        rmpath(ActionPath)% add the prescribed path if not the current one   
1882end
1883
1884%% Activate the Action fct
1885[Series,tild,errormsg]=prepare_jobs(handles);% read the parameters from the GUI series
1886if ~isempty(errormsg)
1887    if ~strcmp(errormsg,'Cancel')
1888    msgbox_uvmat('ERROR',errormsg)
1889    end
1890    return
1891end
1892ParamOut=h_fun(Series);
1893
1894%% Put the first line of the selected Action fct as tooltip help
1895try
1896    [fid,errormsg] =fopen([ActionName '.m']);
1897    InputText=textscan(fid,'%s',1,'delimiter','\n');
1898    fclose(fid);
1899    set(handles.ActionName,'ToolTipString',InputText{1}{1})% put the first line of the selected function as tooltip help
1900end
1901
1902%% Detect the types of input files
1903SeriesData=get(handles.series,'UserData');
1904nb_civ=0;nb_netcdf=0;
1905if ~isempty(SeriesData)
1906    nb_civ=numel(find(strcmp('civx',SeriesData.FileType)|strcmp('civdata',SeriesData.FileType)));
1907    nb_netcdf=numel(find(strcmp('netcdf',SeriesData.FileType)));
1908end
1909
1910%% Check whether alphabetical sorting of input Subdir is alowed by the Action fct  (for multiples series entries)
1911if isfield(ParamOut,'AllowInputSort')&&isequal(ParamOut.AllowInputSort,'on')&& size(Series.InputTable,1)>1
1912    [tild,iview]=sort(InputTable(:,2)); %subdirectories sorted in alphabetical order
1913    set(handles.InputTable,'Data',InputTable(iview,:));
1914    MinIndex_i=get(handles.MinIndex_i,'Data');
1915    MinIndex_j=get(handles.MinIndex_j,'Data');
1916    MaxIndex_i=get(handles.MaxIndex_i,'Data');
1917    MaxIndex_j=get(handles.MaxIndex_j,'Data');
1918    set(handles.MinIndex_i,'Data',MinIndex_i(iview,:));
1919    set(handles.MinIndex_j,'Data',MinIndex_j(iview,:));
1920    set(handles.MaxIndex_i,'Data',MaxIndex_i(iview,:));
1921    set(handles.MaxIndex_j,'Data',MaxIndex_j(iview,:));
1922    TimeTable=get(handles.TimeTable,'Data');
1923    set(handles.TimeTable,'Data',TimeTable(iview,:));
1924    PairString=get(handles.PairString,'Data');
1925    set(handles.PairString,'Data',PairString(iview,:));
1926end
1927
1928%% Impose the whole input file index range if requested
1929if isfield(ParamOut,'WholeIndexRange')&&isequal(ParamOut.WholeIndexRange,'on')
1930    MinIndex_i=get(handles.MinIndex_i,'Data');
1931    MinIndex_j=get(handles.MinIndex_j,'Data');
1932    MaxIndex_i=get(handles.MaxIndex_i,'Data');
1933    MaxIndex_j=get(handles.MaxIndex_j,'Data');
1934    set(handles.num_first_i,'String',num2str(MinIndex_i{1}))% set first as the min index (for the first line)
1935    set(handles.num_last_i,'String',num2str(MaxIndex_i{1}))% set last as the max index (for the first line)
1936    set(handles.num_incr_i,'String','1')
1937    set(handles.num_first_j,'String',num2str(MinIndex_j{1}))% set first as the min index (for the first line)
1938    set(handles.num_last_j,'String',num2str(MaxIndex_j{1}))% set last as the max index (for the first line)
1939    set(handles.num_incr_j,'String','1')
1940else  % check index ranges
1941    first_i=1;last_i=1;first_j=1;last_j=1;
1942    if isfield(Series.IndexRange,'first_i')
1943        first_i=Series.IndexRange.first_i;
1944       % incr_i=Series.IndexRange.incr_i;
1945        last_i=Series.IndexRange.last_i;
1946    end
1947    if isfield(Series.IndexRange,'first_j')
1948        first_j=Series.IndexRange.first_j;
1949       % incr_j=Series.IndexRange.incr_j;
1950        last_j=Series.IndexRange.last_j;
1951    end
1952    if last_i < first_i || last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
1953            set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1954end
1955
1956%% NbSlice visibility
1957NbSliceVisible='off';%default
1958if isfield(ParamOut,'NbSlice') && isequal(ParamOut.NbSlice,'on')
1959    NbSliceVisible='on';
1960    set(handles.num_NbProcess,'String',get(handles.num_NbSlice,'String'))% the nbre of processes is imposed as the nbre of slices
1961else
1962    set(handles.num_NbProcess,'String','')% free nbre of processes
1963end
1964set(handles.num_NbSlice,'Visible',NbSliceVisible)
1965set(handles.NbSlice_title,'Visible',NbSliceVisible)
1966
1967%% Visibility of VelType and VelType_1 menus
1968VelTypeVisible='off';  %hidden by default
1969VelType_1Visible='off';
1970InputFieldsVisible='off';%visibility of the frame Fields
1971if isfield(ParamOut,'VelType')
1972    if strcmp( ParamOut.VelType,'one')||strcmp( ParamOut.VelType,'two')
1973        if nb_civ>=1
1974            VelTypeVisible='on';
1975            InputFieldsVisible='on';
1976        end
1977    end
1978    if strcmp( ParamOut.VelType,'two')
1979        if nb_civ>=2
1980            VelType_1Visible='on';
1981        end
1982    end
1983end
1984set(handles.VelType,'Visible',VelTypeVisible)
1985set(handles.VelType_text,'Visible',VelTypeVisible);
1986set(handles.VelType_1,'Visible',VelType_1Visible)
1987set(handles.VelType_text_1,'Visible',VelType_1Visible);
1988
1989%% Visibility of FieldName and FieldName_1 menus
1990FieldNameVisible='off';  %hidden by default
1991FieldName_1Visible='off';  %hidden by default
1992if isfield(ParamOut,'FieldName')
1993    if strcmp( ParamOut.FieldName,'one')||strcmp( ParamOut.FieldName,'two')
1994        if (nb_civ+nb_netcdf)>=1
1995            InputFieldsVisible='on';
1996            FieldNameVisible='on';
1997        end
1998    end
1999    if strcmp( ParamOut.FieldName,'two')
2000        if (nb_civ+nb_netcdf)>=1
2001            FieldName_1Visible='on';
2002        end
2003    end
2004end
2005set(handles.InputFields,'Visible',InputFieldsVisible)
2006set(handles.FieldName,'Visible',FieldNameVisible) % test for MenuBorser
2007set(handles.FieldName_1,'Visible',FieldName_1Visible)
2008
2009%% Visibility of FieldTransform menu
2010FieldTransformVisible='off';  %hidden by default
2011if isfield(ParamOut,'FieldTransform')
2012    FieldTransformVisible=ParamOut.FieldTransform; 
2013    TransformName_Callback([],[], handles)
2014end
2015set(handles.FieldTransform,'Visible',FieldTransformVisible)
2016if isfield(ParamOut,'TransformPath')
2017    set(handles.ActionExt,'UserData',ParamOut.TransformPath)
2018else
2019    set(handles.ActionExt,'UserData',[])
2020end
2021
2022%% Visibility of projection object
2023ProjObjectVisible='off';  %hidden by default
2024if isfield(ParamOut,'ProjObject')
2025    ProjObjectVisible=ParamOut.ProjObject;
2026end
2027set(handles.CheckObject,'Visible',ProjObjectVisible)
2028if ~get(handles.CheckObject,'Value')
2029    ProjObjectVisible='off';
2030end
2031set(handles.ProjObject,'Visible',ProjObjectVisible)
2032set(handles.DeleteObject,'Visible',ProjObjectVisible)
2033set(handles.ViewObject,'Visible',ProjObjectVisible)
2034
2035
2036%% Visibility of mask input
2037MaskVisible='off';  %hidden by default
2038if isfield(ParamOut,'Mask')
2039    MaskVisible=ParamOut.Mask;
2040end
2041set(handles.Mask,'Visible',MaskVisible)
2042set(handles.CheckMask,'Visible',MaskVisible);
2043
2044%% definition of the directory containing the output files
2045OutputDirVisible='off';
2046if isfield(ParamOut,'OutputDirExt')&&~isempty(ParamOut.OutputDirExt)
2047    set(handles.OutputDirExt,'String',ParamOut.OutputDirExt)
2048    OutputDirVisible='on';
2049    SubDir=InputTable(1:end-1,2); %set of subdirectories sorted in alphabetical order
2050    SubDirOut=SubDir{1};
2051    if numel(SubDir)>1
2052        for ilist=2:numel(SubDir)
2053            SubDirOut=[SubDirOut '-' SubDir{ilist}];
2054        end
2055    end
2056    set(handles.OutputSubDir,'String',SubDirOut)
2057end
2058set(handles.OutputDirExt,'Visible',OutputDirVisible)
2059set(handles.OutputSubDir,'Visible',OutputDirVisible)
2060set(handles.OutputDir_title,'Visible',OutputDirVisible)
2061set(handles.RunMode,'Visible',OutputDirVisible)
2062set(handles.ActionExt,'Visible',OutputDirVisible)
2063set(handles.RunMode_title,'Visible',OutputDirVisible)
2064set(handles.ActionExt_title,'Visible',OutputDirVisible)
2065
2066
2067%% Expected nbre of output files
2068if isfield(ParamOut,'OutputFileMode')
2069StatusData.OutputFileMode=ParamOut.OutputFileMode;
2070set(handles.status,'UserData',StatusData)
2071end
2072
2073%% definition of an additional parameter set, determined by an ancillary GUI
2074if isfield(ParamOut,'ActionInput')
2075    set(handles.ActionInput,'Visible','on')
2076    set(handles.ActionInput_title,'Visible','on')
2077    set(handles.ActionInputView,'Visible','on')
2078    set(handles.ActionInputView,'Value',0)
2079    set(handles.ActionInput,'String',ActionName)
2080    ParamOut.ActionInput.Program=ActionName; % record the program in ActionInput
2081    SeriesData.ActionInput=ParamOut.ActionInput;
2082else
2083    set(handles.ActionInput,'Visible','off')
2084    set(handles.ActionInput_title,'Visible','off')
2085    set(handles.ActionInputView,'Visible','off')
2086    if isfield(SeriesData,'ActionInput')
2087    SeriesData=rmfield(SeriesData,'ActionInput');
2088    end
2089end   
2090set(handles.series,'UserData',SeriesData)
2091set(handles.ActionName,'BackgroundColor',[1 1 1])
2092
2093%------------------------------------------------------------------------
2094% --- Executes on button press in ActionInputView.
2095function ActionInputView_Callback(hObject, eventdata, handles)
2096%------------------------------------------------------------------------
2097if get(handles.ActionInputView,'Value')
2098ActionName_Callback(hObject, eventdata, handles)
2099end
2100
2101%------------------------------------------------------------------------
2102% --- Executes on selection change in FieldName.
2103function FieldName_Callback(hObject, eventdata, handles)
2104%------------------------------------------------------------------------
2105field_str=get(handles.FieldName,'String');
2106field_index=get(handles.FieldName,'Value');
2107field=field_str{field_index(1)};
2108if isequal(field,'get_field...')
2109    hget_field=findobj(allchild(0),'name','get_field');
2110    if ~isempty(hget_field)
2111        delete(hget_field)%delete opened versions of get_field
2112    end
2113    Series=read_GUI(handles.series);
2114    Series.InputTable=Series.InputTable(1,:);
2115    filecell=get_file_series(Series);
2116    if exist(filecell{1,1},'file')
2117        GetFieldData=get_field(filecell{1,1});
2118        FieldList={};
2119        XName=GetFieldData.XVarName;
2120        if GetFieldData.CheckVector
2121            UName=GetFieldData.PanelVectors.vector_x;
2122            VName=GetFieldData.PanelVectors.vector_y;
2123            XName=GetFieldData.XVarName;
2124            YName=GetFieldData.YVarName;
2125            CName=GetFieldData.PanelVectors.vec_color;
2126            [FieldList,VecColorList]=set_field_list(UName,VName,CName);
2127        elseif GetFieldData.CheckScalar
2128            AName=GetFieldData.PanelScalar.scalar;
2129            XName=GetFieldData.XVarName;
2130            YName=GetFieldData.YVarName;
2131            FieldList={AName};
2132        elseif GetFieldData.CheckPlot1D;
2133            YName=GetFieldData.CheckPlot1D.ordinate;
2134        end
2135        set(handles.Coord_x,'String',{XName})
2136        set(handles.Coord_y,'String',{YName})
2137        set(handles.FieldName,'Value',1)
2138        set(handles.FieldName,'String',[FieldList; {'get_field...'}]);
2139        %         set(handles.ColorScalar,'Value',1)
2140        %         set(handles.ColorScalar,'String',VecColorList);
2141        %         UvData.FileType{1}='netcdf';
2142        %         set(handles.uvmat,'UserData',UvData)
2143    end
2144    % elseif isequal(field,'more...')
2145    %     str=calc_field;
2146    %     [ind_answer,v] = listdlg('PromptString','Select a file:',...
2147    %                 'SelectionMode','single',...
2148    %                 'ListString',str);
2149    %        % edit the choice in the fields and actionname menu
2150    %      scalar=cell2mat(str(ind_answer));
2151    %      update_menu(handles.FieldName,scalar)
2152end
2153
2154%------------------------------------------------------------------------
2155% --- Executes on selection change in FieldName_1.
2156function FieldName_1_Callback(hObject, eventdata, handles)
2157%------------------------------------------------------------------------
2158field_str=get(handles.FieldName_1,'String');
2159field_index=get(handles.FieldName_1,'Value');
2160field=field_str{field_index};
2161if isequal(field,'get_field...')   
2162     hget_field=findobj(allchild(0),'name','get_field_1');
2163     if ~isempty(hget_field)
2164         delete(hget_field)
2165     end
2166     SeriesData=get(handles.series,'UserData');
2167     filename=SeriesData.CurrentInputFile_1;
2168     if exist(filename,'file')
2169        hget_field=get_field(filename);
2170        set(hget_field,'name','get_field_1')
2171     end
2172% elseif isequal(field,'more...')
2173%     str=calc_field;
2174%     [ind_answer,v] = listdlg('PromptString','Select a file:',...
2175%                 'SelectionMode','single',...
2176%                 'ListString',str);
2177%        % edit the choice in the fields and actionname menu
2178%      scalar=cell2mat(str(ind_answer));
2179%      update_menu(handles.FieldName_1,scalar)
2180end   
2181
2182
2183%%%%%%%%%%%%%
2184function [ind_remove]=find_pairs(dirpair,ind_i,last_i)
2185indsel=ind_i;
2186indiff=diff(ind_i); %test index increment to detect multiplets (several pairs with the same index ind_i) and holes in the series
2187indiff=[1 indiff last_i-ind_i(end)+1];%for testing gaps with the imposed bounds
2188if ~isempty(indiff)
2189    indiff2=diff(indiff);
2190    indiffp=[indiff2 1];
2191    indiffm=[1 indiff2];
2192    ind_multi_m=find((indiff==0)&(indiffm<0))-1;%indices of first members of multiplets
2193    ind_multi_p=find((indiff==0)&(indiffp>0));%indices of last members of multiplets
2194    %for each multiplet, select the most recent file
2195    ind_remove=[];
2196    for i=1:length(ind_multi_m)
2197        ind_pairs=ind_multi_m(i):ind_multi_p(i);
2198        for imulti=1:length(ind_pairs)
2199            datepair(imulti)=datenum(dirpair(ind_pairs(imulti)).date);%dates of creation
2200        end
2201        [datenew,indsort2]=sort(datepair); %sort the multiplet by creation date
2202        ind_s=indsort2(1:end-1);%
2203        ind_remove=[ind_remove ind_pairs(ind_s)];%remove these indices, leave the last one
2204    end
2205end
2206
2207%------------------------------------------------------------------------
2208% --- determine the list of index pairstring of processing file
2209function [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)
2210%------------------------------------------------------------------------
2211num_i1=num_i;% set of first image numbers by default
2212num_i2=num_i;
2213num_j1=num_j;
2214num_j2=num_j;
2215num_i_out=num_i;
2216num_j_out=num_j;
2217% if isequal (NomType,'_1-2_1') || isequal (NomType,'_1-2')
2218if isequal(mode,'series(Di)')
2219    num_i1_line=num_i+ind_shift(3);% set of first image numbers
2220    num_i2_line=num_i+ind_shift(4);
2221    % adjust the first and last field number
2222        indsel=find(num_i1_line >= 1);
2223    num_i_out=num_i(indsel);
2224    num_i1_line=num_i1_line(indsel);
2225    num_i2_line=num_i2_line(indsel);
2226    num_j1=meshgrid(num_j,ones(size(num_i1_line)));
2227    num_j2=meshgrid(num_j,ones(size(num_i1_line)));
2228    [xx,num_i1]=meshgrid(num_j,num_i1_line);
2229    [xx,num_i2]=meshgrid(num_j,num_i2_line);
2230elseif isequal (mode,'series(Dj)')||isequal (mode,'bursts')
2231    if isequal(mode,'bursts') %case of bursts (png_old or png_2D)
2232        num_j1=ind_shift(1)*ones(size(num_i));
2233        num_j2=ind_shift(2)*ones(size(num_i));
2234    else
2235        num_j1_col=num_j+ind_shift(1);% set of first image numbers
2236        num_j2_col=num_j+ind_shift(2);
2237        % adjust the first field number
2238        indsel=find((num_j1_col >= 1));   
2239        num_j_out=num_j(indsel);
2240        num_j1_col=num_j1_col(indsel);
2241        num_j2_col=num_j2_col(indsel);
2242        [num_i1,num_j1]=meshgrid(num_i,num_j1_col);
2243        [num_i2,num_j2]=meshgrid(num_i,num_j2_col);
2244    end   
2245end
2246
2247%------------------------------------------------------------------------
2248% --- Executes on button press in CheckObject.
2249function CheckObject_Callback(hObject, eventdata, handles)
2250%------------------------------------------------------------------------
2251hset_object=findobj(allchild(0),'tag','set_object');%find the set_object interface handle
2252value=get(handles.CheckObject,'Value');
2253if value
2254    SeriesData=get(handles.series,'UserData');
2255    if ~(isfield(SeriesData,'ProjObject')&&~isempty(SeriesData.ProjObject))
2256        if ishandle(hset_object)
2257            uistack(hset_object,'top')% show the GUI set_object if opened
2258        else
2259            %get the object file
2260            InputTable=get(handles.InputTable,'Data');
2261            defaultname=InputTable{1,1};
2262            if isempty(defaultname)
2263                defaultname={''};
2264            end
2265            [FileName, PathName] = uigetfile( ...
2266                {'*.xml;*.mat', ' (*.xml,*.mat)';
2267                '*.xml',  '.xml files '; ...
2268                '*.mat',  '.mat matlab files '}, ...
2269                'Pick an xml object file (or use uvmat to create it)',defaultname);
2270            fileinput=[PathName FileName];%complete file name
2271            sizf=size(fileinput);
2272            if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
2273            %read the file
2274            data=xml2struct(fileinput);
2275            if ~isfield(data,'Type')
2276                msgbox_uvmat('ERROR',[fileinput ' is not an object xml file'])
2277                return
2278            end
2279            if ~isfield(data,'ProjMode')
2280                data.ProjMode='none';
2281            end
2282            hset_object=set_object(data);% call the set_object interface
2283        end
2284        ProjObject=read_GUI(hset_object);
2285        set(handles.ProjObject,'String',ProjObject.Name);%display the object name
2286        SeriesData=get(handles.series,'UserData');
2287        SeriesData.ProjObject=ProjObject;
2288        set(handles.series,'UserData',SeriesData);
2289    end
2290    set(handles.DeleteObject,'Visible','on');
2291    set(handles.ViewObject,'Visible','on');
2292    set(handles.ProjObject,'Visible','on');
2293else
2294    set(handles.DeleteObject,'Visible','off');
2295    set(handles.ViewObject,'Visible','off');
2296    if ~ishandle(hset_object)
2297    set(handles.ViewObject,'Value',0);
2298    end
2299    set(handles.ProjObject,'Visible','off');
2300end
2301%set(handles.series,'UserData',SeriesData)
2302
2303%--------------------------------------------------------------
2304function CheckMask_Callback(hObject, eventdata, handles)
2305value=get(handles.CheckMask,'Value');
2306if value
2307    msgbox_uvmat('ERROR','not implemented yet')
2308end
2309%--------------------------------------------------------------
2310
2311%-------------------------------------------------------------------
2312%'uv_ncbrowser': interactively calls the netcdf file browser 'get_field.m'
2313function ncbrowser_uvmat(hObject, eventdata)
2314%-------------------------------------------------------------------
2315     bla=get(gcbo,'String');
2316     ind=get(gcbo,'Value');
2317     filename=cell2mat(bla(ind));
2318      blank=find(filename==' ');
2319      filename=filename(1:blank-1);
2320     get_field(filename)
2321
2322% ------------------------------------------------------------------
2323function MenuHelp_Callback(hObject, eventdata, handles)
2324%-------------------------------------------------------------------
2325path_to_uvmat=which ('uvmat');% check the path of uvmat
2326pathelp=fileparts(path_to_uvmat);
2327helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
2328if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
2329else
2330    addpath (fullfile(pathelp,'uvmat_doc'))
2331    web([helpfile '#series'])
2332end
2333
2334%-------------------------------------------------------------------
2335% --- Executes on selection change in TransformName.
2336function TransformName_Callback(hObject, eventdata, handles)
2337%----------------------------------------------------------------------
2338TransformList=get(handles.TransformName,'String');
2339TransformIndex=get(handles.TransformName,'Value');
2340TransformName=TransformList{TransformIndex};
2341TransformPathList=get(handles.TransformName,'UserData');
2342nb_builtin_transform=4;
2343% ff=functions(list_transform{end});
2344if isequal(TransformName,'more...');
2345%     coord_fct='';
2346%     prompt = {'Enter the name of the transform function'};
2347%     dlg_title = 'user defined transform';
2348%     num_lines= 1;
2349    [FileName, PathName] = uigetfile( ...
2350       {'*.m', ' (*.m)';
2351        '*.m',  '.m files '; ...
2352        '*.*', 'All Files (*.*)'}, ...
2353        'Pick a transform function',get(handles.TransformPath,'String'));
2354    if isequal(FileName,0)
2355        return     %browser closed without choice
2356    end
2357    if isequal(PathName(end),'/')||isequal(PathName(end),'\')
2358        PathName(end)=[];
2359    end
2360    [TransformPath,TransformName,TransformExt]=fileparts(FileName);% removes extension .m
2361    if ~strcmp(TransformExt,'.m')
2362        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
2363        return
2364    end
2365     % insert the choice in the menu
2366    TransformIndex=find(strcmp(TransformName,TransformList),1);% look for the selected function in the menu Action
2367    if isempty(TransformIndex)%the input string does not exist in the menu
2368        TransformIndex= length(TransformList);
2369        TransformList=[TransformList(1:end-1);{TransformnName};TransformList(end)];% the selected function is appended in the menu, before the last item 'more...'
2370        set(handles.TransformName,'String',TransformList)
2371        TransformPathList=[TransformPathList;{TransformPath}];
2372    end
2373   % save the new menu in the personal file 'uvmat_perso.mat'
2374   dir_perso=prefdir;%personal Matalb directory
2375   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2376   if exist(profil_perso,'file')
2377       for ilist=nb_builtin_transform+1:numel(TransformPathList)
2378           TransformListUser{ilist-nb_builtin_transform}=TransformList{ilist};
2379           TransformPathListUser{ilist-nb_builtin_transform}=TransformPathList{ilist};
2380       end
2381       save (profil_perso,'TransformPathListUser','TransformListUser','-append'); %store the root name for future opening of uvmat
2382   end
2383end
2384
2385%display the current function path
2386set(handles.TransformPath,'String',TransformPathList{TransformIndex}); %show the path to the senlected function
2387set(handles.TransformName,'UserData',TransformPathList);
2388
2389
2390% --------------------------------------------------------------------
2391function MenuExportConfig_Callback(hObject, eventdata, handles)
2392global Series
2393[Series,errormsg]=prepare_jobs(handles);
2394
2395evalin('base','global Series')%make CurData global in the workspace
2396display('current series config :')
2397evalin('base','Series') %display CurData in the workspace
2398commandwindow; %brings the Matlab command window to the front
2399
2400% --------------------------------------------------------------------
2401function MenuImportConfig_Callback(hObject, eventdata, handles)
2402% --------------------------------------------------------------------
2403InputTable=get(handles.InputTable,'Data');
2404[FileName, PathName] = uigetfile( ...
2405       {'*.xml', ' (*.xml)';
2406       '*.xml',  '.xml files '; ...
2407        '*.*',  'All Files (*.*)'}, ...
2408        'Pick a file',InputTable{1,1});
2409filexml=[PathName FileName];%complete file name
2410if isempty(filexml),return;end %abandon if no file is introduced by the browser
2411Param=xml2struct(filexml);
2412fill_GUI(Param,handles.series)
2413
2414% --- Executes on selection change in RunMode.
2415function RunMode_Callback(hObject, eventdata, handles)
2416
2417% --- Executes on selection change in Coord_x.
2418function Coord_x_Callback(hObject, eventdata, handles)
2419
2420% --- Executes on selection change in Coord_y.
2421function Coord_y_Callback(hObject, eventdata, handles)
2422
2423% --------------------------------------------------------------------
2424% --- Executes when series is resized.
2425function series_ResizeFcn(hObject, eventdata, handles)
2426% --------------------------------------------------------------------
2427%% input table
2428set(handles.InputTable,'Unit','pixel')
2429Pos=get(handles.InputTable,'Position');
2430set(handles.InputTable,'Unit','normalized')
2431ColumnWidth=round([0.5 0.14 0.14 0.14 0.08]*(Pos(3)-52));
2432ColumnWidth=num2cell(ColumnWidth);
2433set(handles.InputTable,'ColumnWidth',ColumnWidth)
2434
2435%% MinIndex_j and MaxIndex_i
2436unit=get(handles.MinIndex_i,'Unit');
2437set(handles.MinIndex_i,'Unit','pixel')
2438Pos=get(handles.MinIndex_i,'Position');
2439set(handles.MinIndex_i,'Unit',unit)
2440set(handles.MinIndex_i,'ColumnWidth',{Pos(3)-18})
2441set(handles.MaxIndex_i,'ColumnWidth',{Pos(3)-18})
2442set(handles.MinIndex_j,'ColumnWidth',{Pos(3)-18})
2443set(handles.MaxIndex_j,'ColumnWidth',{Pos(3)-18})
2444
2445%% TimeTable
2446set(handles.TimeTable,'Unit','pixel')
2447Pos=get(handles.TimeTable,'Position');
2448set(handles.TimeTable,'Unit','normalized')
2449% ColumnWidth=get(handles.TimeTable,'ColumnWidth');
2450ColumnWidth=num2cell(floor([0.25 0.25 0.25 0.25]*(Pos(3)-20)));
2451set(handles.TimeTable,'ColumnWidth',ColumnWidth)
2452
2453
2454%% PairString
2455set(handles.PairString,'Unit','pixel')
2456Pos=get(handles.PairString,'Position');
2457set(handles.PairString,'Unit','normalized')
2458set(handles.PairString,'ColumnWidth',{Pos(3)-5})
2459
2460
2461% --- Executes on button press in status.
2462function status_Callback(hObject, eventdata, handles)
2463
2464if get(handles.status,'Value')
2465    set(handles.status,'BackgroundColor',[1 1 0])
2466    drawnow
2467    %StatusData.time_ref=get(handles.RUN,'UserData');% get the time of launch
2468    Param=read_GUI(handles.series);
2469    RootPath=Param.InputTable{1,1};
2470    if ~isfield(Param,'OutputSubDir')   
2471        msgbox_uvmat('ERROR','no directory defined for output files')
2472        return
2473    end
2474    OutputSubDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
2475    OutputDir=fullfile(RootPath,OutputSubDir);
2476    uigetfile_uvmat('status_display',OutputDir)
2477   
2478%     hfig=findobj(allchild(0),'name','series_status');
2479%     if isempty(hfig)
2480%         ScreenSize=get(0,'ScreenSize');
2481%         hfig=figure('DeleteFcn',@stop_status,'Position',[ScreenSize(3)-600 ScreenSize(4)-640 560 600]);
2482%         set(hfig,'MenuBar','none')% suppress the menu bar
2483%         set(hfig,'NumberTitle','off')%suppress the fig number in the title
2484%         set(hfig,'name','series_status')
2485%         set(hfig,'tag','series_status')
2486%         uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @view_file,'tag','list','UserData',OutputDir);
2487%         uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','titlebox','Max',2,'String',OutputDir);
2488%         uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
2489%         uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'String','Close','FontWeight','bold','FontUnits','points','FontSize',11,'Callback',@stop_status);
2490%         uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.1 0.01 0.2 0.07],'String','Refresh','FontWeight','bold','FontUnits','points','FontSize',11,'Callback',@refresh_GUI);
2491%         %set(hrefresh,'UserData',StatusData)
2492%         BarPosition=[0.05 0.81 0.01 0.05];
2493%         uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
2494%         drawnow
2495%     end
2496%     refresh_GUI(hfig)
2497else
2498    %% delete current display fig if selection is off
2499    set(handles.status,'BackgroundColor',[0 1 0])
2500    hfig=findobj(allchild(0),'name','series_status');
2501    if ~isempty(hfig)
2502        delete(hfig)
2503    end
2504    return
2505end
2506
2507
2508%------------------------------------------------------------------------   
2509% launched by selecting a file on the list
2510function view_file(hObject, eventdata)
2511%------------------------------------------------------------------------
2512list=get(hObject,'String');
2513index=get(hObject,'Value');
2514rootroot=get(hObject,'UserData');
2515selectname=list{index};
2516ind_dot=regexp(selectname,'\.\.\.');
2517if ~isempty(ind_dot)
2518    selectname=selectname(1:ind_dot-1);
2519end
2520FullSelectName=fullfile(rootroot,selectname);
2521if exist(FullSelectName,'dir')% a directory has been selected
2522    ListFiles=dir(FullSelectName);
2523    ListDisplay=cell(numel(ListFiles),1);
2524    for ilist=2:numel(ListDisplay)% suppress the first line '.'
2525        ListDisplay{ilist-1}=ListFiles(ilist).name;
2526    end
2527    set(hObject,'Value',1)
2528    set(hObject,'String',ListDisplay)
2529    if strcmp(selectname,'..')
2530        FullSelectName=fileparts(fileparts(FullSelectName));
2531    end
2532    set(hObject,'UserData',FullSelectName)
2533    hfig=get(hObject,'parent');
2534    htitlebox=findobj(hfig,'tag','titlebox');   
2535    set(htitlebox,'String',FullSelectName)
2536elseif exist(FullSelectName,'file')%visualise the vel field if it exists
2537    FileType=get_file_type(FullSelectName);
2538    if strcmp(FileType,'txt')
2539        edit(FullSelectName)
2540    elseif strcmp(FileType,'xml')
2541        editxml(FullSelectName)
2542    else
2543        uvmat(FullSelectName)
2544    end
2545    set(gcbo,'Value',1)
2546end
2547
2548
2549%------------------------------------------------------------------------   
2550% launched by refreshing the status figure
2551function refresh_GUI(hfig)
2552%------------------------------------------------------------------------
2553htitlebox=findobj(hfig,'tag','titlebox');
2554hlist=findobj(hfig,'tag','list');
2555hseries=findobj(allchild(0),'tag','series');
2556hstatus=findobj(hseries,'tag','status');
2557StatusData=get(hstatus,'UserData');
2558OutputDir=get(htitlebox,'String');
2559if ischar(OutputDir),OutputDir={OutputDir};end
2560ListFiles=dir(OutputDir{1});
2561if numel(ListFiles)<1
2562    return
2563end
2564ListFiles(1)=[];%removes the first line ='.'
2565ListDisplay=cell(numel(ListFiles),1);
2566testrecent=0;
2567datnum=zeros(numel(ListDisplay),1);
2568for ilist=1:numel(ListDisplay)
2569    ListDisplay{ilist}=ListFiles(ilist).name;
2570      if ~ListFiles(ilist).isdir && isfield(ListFiles(ilist),'datenum')
2571            datnum(ilist)=ListFiles(ilist).datenum;%only available in recent matlab versions
2572            testrecent=1;
2573       end
2574end
2575set(hlist,'String',ListDisplay)
2576
2577%% Look at date of creation
2578ListDisplay=ListDisplay(datnum~=0);
2579datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
2580NbOutputFile=[];
2581if isempty(datnum)
2582    if testrecent
2583        message='no civ result created yet';
2584    else
2585        message='';
2586    end
2587else
2588    [first,indfirst]=min(datnum);
2589    [last,indlast]=max(datnum);
2590    NbOutputFile_str='?';
2591    NbOutputFile=[];
2592    if isfield(StatusData,'NbOutputFile')
2593        NbOutputFile=StatusData.NbOutputFile;
2594        NbOutputFile_str=num2str(NbOutputFile);
2595    end
2596    message={[num2str(numel(datnum)) ' file(s) done over ' NbOutputFile_str] ;['oldest modification:  ' ListDisplay{indfirst} ' : ' datestr(first)];...
2597        ['latest modification:  ' ListDisplay{indlast} ' : ' datestr(last)]};
2598end
2599set(htitlebox,'String', [OutputDir{1};message])
2600
2601%% update the waitbar
2602hwaitbar=findobj(hfig,'tag','waitbar');
2603if ~isempty(NbOutputFile)
2604    BarPosition=get(hwaitbar,'Position');
2605    BarPosition(3)=0.9*numel(datnum)/NbOutputFile;
2606    set(hwaitbar,'Position',BarPosition)
2607end
2608%TODO: adjust waitbar
2609
2610% civ_files=get(hfig,'UserData');
2611
2612% [filepath,filename,ext]=fileparts(civ_files{1});
2613% [tild,SubDir,extdir]=fileparts(filepath);
2614% SubDir=[SubDir extdir];
2615% option_civ=StatusData.option_civ;
2616% nbfiles=numel(civ_files);
2617% testrecent=0;
2618% count=0;
2619% datnum=zeros(1,nbfiles);
2620% filefound=cell(1,nbfiles);
2621% for ifile=1:nbfiles
2622%     detect=exist(civ_files{ifile},'file'); % check the existence of the file
2623%     option=0;
2624%     if detect==0
2625%         option_str='not created';
2626%     else
2627%         datfile=dir(civ_files{ifile});
2628%         if isfield(datfile,'datenum')
2629%             datnum(ifile)=datfile.datenum;%only available in recent matlab versions
2630%             testrecent=1;
2631%         end
2632%         filefound(ifile)={datfile.name};
2633%         
2634%         % check the content  netcdf file
2635%         Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','CivStage','patch2','fix2','civ2','patch','fix');
2636%         option_list={'civ1','fix1','patch1','civ2','fix2','patch2'};
2637%         if ~isempty(Data.CivStage)
2638%             option=Data.CivStage;%case of Matlab civ
2639%         else
2640%             if ~isempty(Data.patch2) && isequal(Data.patch2,1)
2641%                 option=6;
2642%             elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
2643%                 option=5;
2644%             elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
2645%                 option=4;
2646%             elseif ~isempty(Data.patch) && isequal(Data.patch,1);
2647%                 option=3;
2648%             elseif ~isempty(Data.fix) && isequal(Data.fix,1);
2649%                 option=2;
2650%             else
2651%                 option=1;
2652%             end
2653%         end
2654%         option_str=option_list{option};
2655%         if datnum(ifile)<StatusData.time_ref
2656%             option_str=[option_str '  --OLD--'];
2657%         end
2658%     end
2659%     if option >= option_civ
2660%         count=count+1;
2661%     end
2662%     [filepath,filename,ext]=fileparts(civ_files{ifile});
2663%     Tabchar{ifile,1}=[fullfile(SubDir,filename) ext  '...' option_str];
2664% end
2665% datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
2666% if isempty(datnum)
2667%     if testrecent
2668%         message='no civ result created yet';
2669%     else
2670%         message='';
2671%     end
2672% else
2673%     datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
2674%     [first,ind]=min(datnum);
2675%     [last,indlast]=max(datnum);
2676%     message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
2677%         ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
2678% end
2679% hlist=findobj(hfig,'tag','list');
2680% htitlebox=findobj(hfig,'tag','titlebox');
2681% hwaitbar=findobj(hfig,'tag','waitbar');
2682% set(hlist,'String',Tabchar)
2683% set(htitlebox,'String', message)
2684%
2685%------------------------------------------------------------------------   
2686% launched by deleting the status figure
2687function stop_status(hObject, eventdata)
2688%------------------------------------------------------------------------
2689hciv=findobj(allchild(0),'tag','series');
2690hhciv=guidata(hciv);
2691set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
2692set(hhciv.status,'BackgroundColor',[0 1 0])
2693delete(gcbf)
2694
2695% --- Executes on selection change in ActionExt.
2696function ActionExt_Callback(hObject, eventdata, handles)
2697ActionExtList=get(handles.ActionExt,'String');
2698ActionExt=ActionExtList{get(handles.ActionExt,'Value')};
2699ActionList=get(handles.ActionName,'String');
2700ActionName=ActionList{get(handles.ActionName,'Value')};
2701TransformPath='';
2702if ~isempty(get(handles.ActionExt,'UserData'))
2703    TransformPath=get(handles.ActionExt,'UserData');
2704end
2705if strcmp(ActionExt,'.sh')
2706    set(handles.ActionExt,'BackgroundColor',[1 1 0])
2707    ActionFullName=fullfile(get(handles.ActionPath,'String'),[ActionName '.sh']);
2708    if ~exist(ActionFullName,'file')
2709        answer=msgbox_uvmat('INPUT_Y-N','compiled version has not been created: compile now?');
2710        if strcmp(answer,'Yes')
2711            set(handles.ActionExt,'BackgroundColor',[1 1 0])
2712            path_uvmat=fileparts(which('series'));
2713            currentdir=pwd;
2714            cd(get(handles.ActionPath,'String'))% go to the directory of Action
2715            %  addpath(get(handles.TransformPath,'String'))
2716            addpath(path_uvmat)% add the path to uvmat to run the fct 'compile'
2717           % addpath(fullfile(path_uvmat,'transform_field'))% add the path to uvmat to run the fct 'compile'
2718            compile(ActionName,TransformPath)
2719            cd(currentdir)
2720        end
2721       
2722    else
2723        sh_file_info=dir(fullfile(get(handles.ActionPath,'String'),[ActionName '.sh']));
2724        m_file_info=dir(fullfile(get(handles.ActionPath,'String'),[ActionName '.m']));
2725        if isfield(m_file_info,'datenum') && m_file_info.datenum>sh_file_info.datenum
2726            set(handles.ActionExt,'BackgroundColor',[1 1 0])
2727            drawnow
2728            answer=msgbox_uvmat('INPUT_Y-N',[ActionName '.sh needs to be updated: recompile now?']);
2729            if strcmp(answer,'Yes')
2730                path_uvmat=fileparts(which('series'));
2731                currentdir=pwd;
2732                cd(get(handles.ActionPath,'String'))% go to the directory of Action
2733                %  addpath(get(handles.TransformPath,'String'))
2734                addpath(path_uvmat)% add the path to uvmat to run the fct 'compile'
2735                addpath(fullfile(path_uvmat,'transform_field'))% add the path to uvmat to run the fct 'compile'
2736                compile(ActionName,TransformPath)
2737                cd(currentdir)
2738            end
2739        end
2740    end
2741    set(handles.ActionExt,'BackgroundColor',[1 1 1])
2742end
2743
2744
2745function ActionInput_Callback(hObject, eventdata, handles)
2746
2747
2748% --- Executes on button press in DeleteObject.
2749function DeleteObject_Callback(hObject, eventdata, handles)
2750if get(handles.DeleteObject,'Value')
2751        SeriesData=get(handles.series,'UserData');
2752    SeriesData.ProjObject=[];
2753    set(handles.series,'UserData',SeriesData)
2754    set(handles.ProjObject,'String','')
2755    set(handles.CheckObject,'Value',0)
2756    set(handles.DeleteObject,'Visible','off')
2757    set(handles.ViewObject,'Visible','off')
2758    set(handles.DeleteObject,'Value',0)
2759end
2760
2761% --- Executes on button press in ViewObject.
2762function ViewObject_Callback(hObject, eventdata, handles)
2763if get(handles.ViewObject,'Value')
2764        UserData=get(handles.series,'UserData');
2765    set_object(UserData.ProjObject)
2766else
2767    hset_object=findobj(allchild(0),'Tag','set_object');
2768    if ~isempty(hset_object)
2769        delete(hset_object)
2770    end
2771end
2772
2773
2774function num_NbProcess_Callback(hObject, eventdata, handles)
2775
2776
2777function num_NbSlice_Callback(hObject, eventdata, handles)
2778NbSlice=str2num(get(handles.num_NbSlice,'String'));
2779set(handles.num_NbProcess,'String',num2str(NbSlice))
Note: See TracBrowser for help on using the repository browser.