source: trunk/src/series.m @ 636

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

mask introduced in series

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