source: trunk/src/series.m @ 553

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

bug corrected in series

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