source: trunk/src/series.m @ 441

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

many bugs corrected for the new file organisation

File size: 98.3 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 transform_fct (menu for coordinate transforms)
9%      .menu_coord_val: value for transform_fct (menu for coordinate transforms)
10%      .FileName: input file name
11%      .FileName_1: second input file name
12%      .list_field: menu of input fields
13%      .index_fields: chosen index
14%      .civ1=0 or 1, .interp1,  ... : input civ field type
15%
16%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
17%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
18%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
19%     This file is part of the toolbox UVMAT.
20%
21%     UVMAT is free software; you can redistribute it and/or modify
22%     it under the terms of the GNU General Public License as published by
23%     the Free Software Foundation; either version 2 of the License, or
24%     (at your option) any later version.
25%
26%     UVMAT is distributed in the hope that it will be useful,
27%     but WITHOUT ANY WARRANTY; without even the implied warranty of
28%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
30%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
31
32%------------------------------------------------------------------------
33%------------------------------------------------------------------------
34%  I - MAIN FUNCTION series
35%------------------------------------------------------------------------
36%------------------------------------------------------------------------
37function varargout = series(varargin)
38
39% Begin initialization code - DO NOT EDIT
40gui_Singleton = 1;
41gui_State = struct('gui_Name',       mfilename, ...
42                   'gui_Singleton',  gui_Singleton, ...
43                   'gui_OpeningFcn', @series_OpeningFcn, ...
44                   'gui_OutputFcn',  @series_OutputFcn, ...
45                   'gui_LayoutFcn',  [] , ...
46                   'gui_Callback',   []);
47if nargin && ischar(varargin{1})
48    gui_State.gui_Callback = str2func(varargin{1});
49end
50
51if nargout
52    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
53else
54    gui_mainfcn(gui_State, varargin{:});
55end
56% End initialization code - DO NOT EDIT
57
58%--------------------------------------------------------------------------
59% --- Executes just before series is made visible.
60%--------------------------------------------------------------------------
61function series_OpeningFcn(hObject, eventdata, handles,param)
62global nb_builtin_ACTION nb_builtin_transform
63% Choose default command line output for series
64handles.output = hObject;
65% Update handles structure
66guidata(hObject, handles);
67%default initial parameters
68drawnow
69set(hObject,'Units','pixels')
70set(handles.PairString,'ColumnEditable',logical(0))
71set(handles.PairString,'ColumnFormat',{'char'})
72set(handles.PairString,'ColumnWidth',{60})
73set(handles.PairString,'Data',{''})
74% set(0,'Units','pixels')
75% screensize=get(0,'ScreenSize'); %screen size in pixels
76set(hObject,'WindowButtonDownFcn',{'mouse_down'})%allows mouse action with right button (zoom for uicontrol display)
77%set(hObject,'Position',[150 100 1000 600] );%position and size in pixels (get adjusted to the screen size in case of excess)
78%load the list of previously browsed files in menus Open and Open_1
79dir_perso=prefdir;
80test_profil_perso=0;
81profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
82if exist(profil_perso,'file')
83     h=load (profil_perso);
84     test_profil_perso=1;
85     if isfield(h,'MenuFile_1')
86          set(handles.MenuFile_1,'Label',h.MenuFile_1);
87          set(handles.MenuFile_insert_1,'Label',h.MenuFile_1);
88     end
89     if isfield(h,'MenuFile_1')
90          set(handles.MenuFile_2,'Label',h.MenuFile_2);
91          set(handles.MenuFile_insert_2,'Label',h.MenuFile_2);
92     end
93     if isfield(h,'MenuFile_1')
94          set(handles.MenuFile_3,'Label',h.MenuFile_3);
95          set(handles.MenuFile_insert_3,'Label',h.MenuFile_3);
96     end
97     if isfield(h,'MenuFile_1')
98          set(handles.MenuFile_4,'Label',h.MenuFile_4);
99          set(handles.MenuFile_insert_4,'Label',h.MenuFile_4);
100     end
101     if isfield(h,'MenuFile_1')
102          set(handles.MenuFile_5,'Label',h.MenuFile_5);
103          set(handles.MenuFile_insert_5,'Label',h.MenuFile_5);
104     end
105end
106
107%check default input data
108if ~exist('param','var')
109    param=[]; %default
110end
111
112%% file name and browser initialisation
113if isfield(param,'menu_coord_str')
114    set(handles.transform_fct,'String',param.menu_coord_str)
115end
116if isfield(param,'menu_coord_val')
117    set(handles.transform_fct,'Value',param.menu_coord_val);
118else
119     set(handles.transform_fct,'Value',1);%default
120end
121if isfield(param,'FileName')
122    if isfield(param,'FileName_1')
123        display_file_name(handles,param.FileName_1,0)
124        display_file_name(handles,param.FileName,1)
125    else
126        display_file_name(handles,param.FileName,0)
127    end
128end 
129
130%% fields input initialisation
131if isfield(param,'list_fields')&& isfield(param,'index_fields') &&~isempty(param.list_fields) &&~isempty(param.index_fields)
132    set(handles.FieldMenu,'String',param.list_fields);% list menu fields
133    set(handles.FieldMenu,'Value',param.index_fields);% selected string index
134    FieldCell{1}=param.list_fields{param.index_fields};
135end
136
137%loads the information stored in prefdir to initiate  the list of ACTION functions
138fct_menu={'check_data_files';'aver_stat';'time_series';'merge_proj';'clean_civ_cmx'};
139transform_menu={'';'phys';'px';'phys_polar'};
140nb_builtin_ACTION=numel(fct_menu); %number of functions
141nb_transform=numel(transform_menu);
142[path_series,name,ext]=fileparts(which('series'));
143path_series=fullfile(path_series,'series');%path of the function 'series'
144addpath (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)
145path_transform=fullfile(path_series,'transform_field');%path to the field transform functions
146for ilist=1:length(fct_menu)
147    fct_path{ilist,1}=path_series;%paths of the fuctions buil-in in 'series.m'
148end
149
150%% TRANSFORM menu: loads the information stored in prefdir to initiate  the list of field transform functions
151menu_str={'';'phys';'px';'phys_polar'};
152nb_builtin_transform=numel(menu_str); %number of functions
153[path_uvmat,name,ext]=fileparts(which('uvmat'));
154addpath(fullfile(path_uvmat,'transform_field'))
155fct_handle{1,1}=[];
156testexist(1)=1;
157for ilist=2:length(menu_str)
158    if exist(menu_str{ilist},'file')
159        fct_handle{ilist,1}=str2func(menu_str{ilist});
160        testexist(ilist)=1;
161    else
162        testexist(ilist)=0;
163    end
164end
165rmpath(fullfile(path_uvmat,'transform_field'))
166
167%% read the list of functions stored in the personal file 'uvmat_perso.mat' in prefdir
168if test_profil_perso
169    if isfield(h,'series_fct') && iscell(h.series_fct)
170         for ilist=1:length(h.series_fct)
171             [path,file]=fileparts(h.series_fct{ilist});
172             fct_path=[fct_path; {path}];%concatene the list of paths
173             fct_menu=[fct_menu; {file}];
174         end
175    end
176    if isfield(h,'transform_fct') && iscell(h.transform_fct)
177        for ilist=1:length(h.transform_fct);
178             [path,file]=fileparts(h.transform_fct{ilist});
179             addpath(path)
180             if exist(file,'file')
181                h_func=str2func(file);
182                testexist=[testexist 1];
183             else
184                h_func=[];
185                testexist=[testexist 0];
186             end
187             fct_handle=[fct_handle; {h_func}];%concatene the list of paths
188             rmpath(path)
189             menu_str=[menu_str; {file}];
190        end
191    end
192end
193fct_menu=[fct_menu;{'more...'}];
194set(handles.ACTION,'String',fct_menu)
195set(handles.ACTION,'UserData',fct_path)% store the list of path in UserData of ACTION
196menu_str=menu_str(find(testexist));
197fct_handle=fct_handle(find(testexist));
198menu_str=[menu_str;{'more...'}];
199set(handles.transform_fct,'String',menu_str)
200set(handles.transform_fct,'UserData',fct_handle)% store the list of path in UserData of ACTION
201
202% display the GUI for the default action 'check_data_files'
203ACTION_Callback(hObject, eventdata, handles)
204
205%------------------------------------------------------------------------
206% --- Outputs from this function are returned to the command line.
207function varargout = series_OutputFcn(hObject, eventdata, handles)
208%------------------------------------------------------------------------
209% varargout  cell array for returning output args (see VARARGOUT);
210% hObject    handle to figure
211% eventdata  reserved - to be defined in a future version of MATLAB
212% handles    structure with handles and user data (see GUIDATA)
213% Get default command line output from handles structure
214varargout{1} = handles.output;
215
216%------------------------------------------------------------------------
217%------------------------------------------------------------------------
218%  II - FUNCTIONS FOR INTRODUCING THE INPUT FILES
219% automatically sets the global properties when the rootfile name is introduced
220% then activate the view-field action if selected
221% it is activated either by clicking on the RootPath window or by the
222% browser
223%------------------------------------------------------------------------
224%------------------------------------------------------------------------
225function MenuBrowse_Callback(hObject, eventdata, handles)
226%------------------------------------------------------------------------   
227InputTable=get(handles.InputTable,'Data');
228RootPathCell=InputTable(:,1);
229SubDirCell=InputTable(:,2);
230RootFileCell=InputTable(:,3);
231oldfile=''; %default
232if isempty(RootPathCell)||isequal(RootPathCell,{''})%loads the previously stored file name and set it as default in the file_input box
233     dir_perso=prefdir;
234     profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
235     if exist(profil_perso,'file')
236          h=load (profil_perso);
237         if isfield(h,'filebase')&&ischar(h.filebase)
238                 oldfile=h.filebase;
239         end
240         if isfield(h,'RootPath')&&ischar(h.RootPath)
241                 oldfile=h.RootPath;
242         end
243     end
244 else
245     oldfile=fullfile(RootPathCell{1},SubDirCell{1},RootFileCell{1});
246 end
247[FileName, PathName, filterindex] = uigetfile( ...
248       {'*.xml;*.xls;*.png;*.tif;*.avi;*.AVI;*.nc', ' (*.xml,*.xls, *.png,*.tif, *.avi,*.nc)';
249       '*.xml',  '.xml files '; ...
250        '*.xls',  '.xls files '; ...
251        '*.png','.png image files'; ...
252        '*.tif','.tif image files'; ...
253        '*.avi;*.AVI','.avi movie files'; ...
254        '*.nc','.netcdf files'; ...
255        '*.*',  'All Files (*.*)'}, ...
256        'Pick a file',oldfile);
257fileinput=[PathName FileName];%complete file name
258sizf=size(fileinput);
259if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
260[path,name,ext]=fileparts(fileinput);
261SeriesData=[];%dfault
262if isequal(ext,'.xml')
263    msgbox_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
264elseif isequal(ext,'.xls')
265    msg_box_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
266else
267    display_file_name(handles,fileinput,0)
268     %update list of recent files in the menubar
269    MenuFile_1=fileinput;
270    MenuFile_2=get(handles.MenuFile_1,'Label');
271    MenuFile_3=get(handles.MenuFile_2,'Label');
272    MenuFile_4=get(handles.MenuFile_3,'Label');
273    MenuFile_5=get(handles.MenuFile_4,'Label');
274    set(handles.MenuFile_1,'Label',MenuFile_1)
275    set(handles.MenuFile_2,'Label',MenuFile_2)
276    set(handles.MenuFile_3,'Label',MenuFile_3)
277    set(handles.MenuFile_4,'Label',MenuFile_4)
278    set(handles.MenuFile_5,'Label',MenuFile_5)
279    set(handles.MenuFile_insert_1,'Label',MenuFile_1)
280    set(handles.MenuFile_insert_2,'Label',MenuFile_2)
281    set(handles.MenuFile_insert_3,'Label',MenuFile_3)
282    set(handles.MenuFile_insert_4,'Label',MenuFile_4)
283    set(handles.MenuFile_insert_5,'Label',MenuFile_5)
284    dir_perso=prefdir;
285    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
286    if exist(profil_perso,'file')
287        save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-append'); %store the file names for future opening of uvmat
288    else
289    txt=ver('MATLAB');
290    Release=txt.Release;
291        relnumb=str2num(Release(3:4));
292        if relnumb >= 14
293            save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5','-V6'); %store the file names for future opening of uvmat
294        else
295            save (profil_perso,'MenuFile_1','MenuFile_2','MenuFile_3','MenuFile_4', 'MenuFile_5'); %store the file names for future opening of uvmat
296        end
297    end
298end
299
300% --------------------------------------------------------------------
301function MenuFile_1_Callback(hObject, eventdata, handles)
302fileinput=get(handles.MenuFile_1,'Label');
303display_file_name(handles,fileinput,0)
304
305% --------------------------------------------------------------------
306function MenuFile_2_Callback(hObject, eventdata, handles)
307fileinput=get(handles.MenuFile_2,'Label');
308display_file_name(handles,fileinput,0)
309
310% --------------------------------------------------------------------
311function MenuFile_3_Callback(hObject, eventdata, handles)
312fileinput=get(handles.MenuFile_3,'Label');
313display_file_name( handles,fileinput,0)
314
315% --------------------------------------------------------------------
316function MenuFile_4_Callback(hObject, eventdata, handles)
317fileinput=get(handles.MenuFile_4,'Label');
318display_file_name(handles,fileinput,0)
319
320% --------------------------------------------------------------------
321function MenuFile_5_Callback(hObject, eventdata, handles)
322fileinput=get(handles.MenuFile_5,'Label');
323display_file_name(handles,fileinput,0)
324
325% --------------------------------------------------------------------
326function MenuBrowse_insert_Callback(hObject, eventdata, handles)
327InputTable=get(handles.InputTable,'Data');
328RootPathCell=InputTable(:,1);
329SubDirCell=InputTable(:,3);
330RootFileCell=InputTable(:,2);
331% RootPathCell=get(handles.RootPath,'String');
332% RootFileCell=get(handles.RootFile,'String');
333oldfile=''; %default
334if isempty(RootPathCell)||isequal(RootPathCell,{''})%loads the previously stored file name and set it as default in the file_input box
335     dir_perso=prefdir;
336     profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
337     if exist(profil_perso,'file')
338          h=load (profil_perso);
339         if isfield(h,'filebase')&ischar(h.filebase)
340                 oldfile=h.filebase;
341         end
342         if isfield(h,'RootPath')&ischar(h.RootPath)
343                 oldfile=h.RootPath;
344         end
345     end
346 else
347     oldfile=fullfile(RootPathCell{1},RootFileCell{1});
348 end
349[FileName, PathName, filterindex] = uigetfile( ...
350       {'*.xml;*.xls;*.png;*.avi;*.AVI;*.nc', ' (*.xml,*.xls, *.png, *.avi,*.nc)';
351       '*.xml',  '.xml files '; ...
352        '*.xls',  '.xls files '; ...
353        '*.png','.png image files'; ...
354        '*.avi;*.AVI','.avi movie files'; ...
355        '*.nc','.netcdf files'; ...
356        '*.*',  'All Files (*.*)'}, ...
357        'Pick a file',oldfile);
358fileinput=[PathName FileName];%complete file name
359sizf=size(fileinput);
360if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
361[path,name,ext]=fileparts(fileinput);
362if isequal(ext,'.xml')
363    msgbox_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
364elseif isequal(ext,'.xls')
365    msgbox_uvmat('ERROR','input file type not implemented')%A Faire: ouvrir le fichier pour naviguer
366else
367    display_file_name(handles,fileinput,1)
368end
369
370% --------------------------------------------------------------------
371function MenuFile_insert_1_Callback(hObject, eventdata, handles)
372% --------------------------------------------------------------------   
373fileinput=get(handles.MenuFile_insert_1,'Label');
374display_file_name(handles,fileinput,1)
375
376% --------------------------------------------------------------------
377function MenuFile_insert_2_Callback(hObject, eventdata, handles)
378% --------------------------------------------------------------------   
379fileinput=get(handles.MenuFile_insert_2,'Label');
380display_file_name(handles,fileinput,1)
381
382% --------------------------------------------------------------------
383function MenuFile_insert_3_Callback(hObject, eventdata, handles)
384% --------------------------------------------------------------------   
385fileinput=get(handles.MenuFile_insert_3,'Label');
386display_file_name( handles,fileinput,1)
387
388% --------------------------------------------------------------------
389function MenuFile_insert_4_Callback(hObject, eventdata, handles)
390% --------------------------------------------------------------------   
391fileinput=get(handles.MenuFile_insert_4,'Label');
392display_file_name( handles,fileinput,1)
393
394% --------------------------------------------------------------------
395function MenuFile_insert_5_Callback(hObject, eventdata, handles)
396% --------------------------------------------------------------------   
397fileinput=get(handles.MenuFile_insert_5,'Label');
398display_file_name(handles,fileinput,1)
399
400%------------------------------------------------------------------------
401% --- Executes when entered data in editable cell(s) in InputTable.
402function InputTable_CellEditCallback(hObject, eventdata, handles)
403%------------------------------------------------------------------------
404iview=eventdata.Indices(1);
405InputTable=get(handles.InputTable,'Data');
406filename=fullfile(InputTable{iview,1},InputTable{iview,2},[InputTable{iview,3} InputTable{iview,4} InputTable{iview,5}])
407display_file_name(handles,fileinput,0)
408
409% hObject    handle to InputTable (see GCBO)
410% eventdata  structure with the following fields (see UITABLE)
411%       Indices: row and column indices of the cell(s) edited
412%       PreviousData: previous data for the cell(s) edited
413%       EditData: string(s) entered by the user
414%       NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
415%       Error: error string when failed to convert EditData to appropriate value for Data
416% handles    structure with handles and user data (see GUIDATA)
417% check_lines=get(handles.REFRESH_INDICES,'UserData');
418% check_lines(eventdata.Indices(1))=1; %select the edited line for refresh
419% set(handles.REFRESH_INDICES,'UserData',check_lines);
420% set(handles.REFRESH_INDICES,'Visible','on')
421%InputTable=get(handles.InputTable,'Data')
422
423%------------------------------------------------------------------------
424% ---  refresh the GUI data after introduction of a new file
425% INPUT:
426% handles: handles of the elements in the GUI series
427% fileinput: name of the input file
428% append: =0 to refresh the list of file series, =1 to append a new series to the list (from the menu bar option 'Open_insert')
429function display_file_name(handles,fileinput,append)
430%------------------------------------------------------------------------ 
431
432%% get the input root name, indices, file extension and nomenclature NomType
433if ~exist(fileinput,'file')
434    msgbox_uvmat('ERROR',['input file ' fileinput  ' does not exist'])
435    return
436end
437
438%% enable other menus and uicontrols
439set(handles.MenuOpen_insert,'Enable','on')
440set(handles.MenuFile_insert_1,'Enable','on')
441set(handles.MenuFile_insert_2,'Enable','on')
442set(handles.MenuFile_insert_3,'Enable','on')
443set(handles.MenuFile_insert_4,'Enable','on')
444set(handles.MenuFile_insert_5,'Enable','on')
445set(handles.RUN, 'Enable','On')
446set(handles.RUN,'BackgroundColor',[1 0 0])% set RUN button to red
447% set(handles.RootPath,'BackgroundColor',[1 1 0]) % set RootPath edit box  to yellow
448set(handles.InputTable,'BackgroundColor',[1 1 0]) % set RootPath edit box  to yellow
449drawnow
450
451
452%% detect root name, nomenclature and indices in the input file name:
453[FilePath,FileName,FileExt]=fileparts(fileinput);
454% detect the file type, get the movie object if relevant, and look for the corresponding file series:
455% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
456[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,MovieObject,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
457if isempty(RootFile)&&isempty(i1_series)
458    errormsg='no input file in the series';
459    return
460end
461
462%% fill the list of file series
463InputTable=get(handles.InputTable,'Data');
464if append % display the input data as a new line in the table
465     lastview=size(InputTable,1)+1;
466     InputTable(lastview,:)=[{RootPath},{SubDir},{RootFile},{NomType},{FileExt}];
467    set(handles.ListView,'String',[get(handles.ListView,'String');{num2str(lastview)}])
468    set(handles.ListView,'Value',lastview)
469%     check_lines=get(handles.REFRESH_INDICES,'UserData');
470else % or re-initialise the list of  input  file series
471    lastview=1;
472    InputTable=[{RootPath},{SubDir},{RootFile},{NomType},{FileExt}];
473    set(handles.TimeTable,'Data',[{[]},{[]},{[]},{[]}])
474    set(handles.MinIndex,'Data',[{[]},{[]}])
475    set(handles.MaxIndex,'Data',[{[]},{[]}])
476    set(handles.ListView,'Value',1)
477    set(handles.ListView,'String',{'1'})
478end
479set(handles.InputTable,'Data',InputTable)
480% check_lines(lastview)=1; %select the edited line for refresh
481% set(handles.REFRESH_INDICES,'UserData',check_lines);
482
483%% refresh menus with info from the new series: TODO:check
484%REFRESH_INDICES_Callback([],[], handles)
485
486%% determine the selected reference field indices for pair display
487ref_i=1; %default ref_i is a reference frame index used to find existing pairs from PIV
488if ~isempty(i1)
489    ref_i=i1;
490    if ~isempty(i2)
491        ref_i=floor((ref_i+i2)/2);% reference image number corresponding to the file
492    end
493end
494set(handles.num_ref_i,'String',num2str(ref_i));
495ref_j=1; %default  ref_j is a reference frame index used to find existing pairs from PIV
496if ~isempty(j1)
497    ref_j=j1;
498    if ~isempty(j2)
499        ref_j=floor((j1+j2)/2);
500    end         
501end
502set(handles.num_ref_j,'String',num2str(ref_j));
503
504%% update list of recent files in the menubar and save it for future opening
505MenuFile=[{get(handles.MenuFile_1,'Label')};{get(handles.MenuFile_2,'Label')};...
506    {get(handles.MenuFile_3,'Label')};{get(handles.MenuFile_4,'Label')};{get(handles.MenuFile_5,'Label')}];
507str_find=strcmp(FileName,MenuFile);
508if isempty(find(str_find,1))
509    MenuFile=[{FileName};MenuFile];%insert the current file if not already in the list
510end
511for ifile=1:min(length(MenuFile),5)
512    eval(['set(handles.MenuFile_' num2str(ifile) ',''Label'',MenuFile{ifile});'])
513    eval(['set(handles.MenuFile_insert_' num2str(ifile) ',''Label'',MenuFile{ifile});'])
514end
515dir_perso=prefdir;
516profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
517if exist(profil_perso,'file')
518    save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
519else
520    save (profil_perso,'MenuFile','-V6'); %store the file names for future opening of uvmat
521end
522
523set(handles.InputTable,'BackgroundColor',[1 1 1])
524
525%% initiate input file series and refresh the current field view:     
526update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,MovieObject,lastview);
527
528%------------------------------------------------------------------------
529% --- Update information about a new field series (indices to scan, timing,
530%     calibration from an xml file, then refresh current plots
531function update_rootinfo(handles,i1_series,i2_series,j1_series,j2_series,FileType,VideoObject,iview)
532%------------------------------------------------------------------------
533
534%% enable j index visibility
535if isempty(j1_series)
536    state='off';
537else
538    state='on';
539end
540enable_j(handles,state)
541
542%% display the min and max indices for all the file series
543MinIndex=get(handles.MinIndex,'Data');%retrieve the min indices in the table MinIndex
544MaxIndex=get(handles.MaxIndex,'Data');%retrieve the max indices in the table MaxIndex
545% MinIndex_i=min(i1_series(i1_series>0));
546% if ~isempty(i2_series)
547%     MaxIndex_i=max(i2_series(i2_series>0));
548% else
549%     MaxIndex_i=max(i1_series(i1_series>0));
550% end
551% MinIndex_j=min(j1_series(j1_series>0));
552% if ~isempty(j2_series)
553%     MaxIndex_j=max(j2_series(j2_series>0));
554% else
555%     MaxIndex_j=max(j1_series(j1_series>0));
556% end
557i_sum=sum(sum(i1_series,2),3);
558MaxIndex_i=max(find(i_sum>0))-1;
559MinIndex_i=min(find(i_sum>0))-1;
560j_sum=sum(sum(i1_series,1),3);
561MaxIndex_j=max(find(j_sum>0))-1;
562MinIndex_j=min(find(j_sum>0))-1;
563MinIndex{iview,1}=MinIndex_i;
564MinIndex{iview,2}=MinIndex_j;
565MaxIndex{iview,1}=MaxIndex_i;
566MaxIndex{iview,2}=MaxIndex_j;
567set(handles.MinIndex,'Data',MinIndex)%display the min indices in the table MinIndex
568set(handles.MaxIndex,'Data',MaxIndex)%display the max indices in the table MaxIndex
569
570%% adjust the first and last indices if requested by the bounds
571first_i=str2num(get(handles.num_first_i,'String'));
572ref_i=str2num(get(handles.num_ref_i,'String'));
573ref_j=str2num(get(handles.num_ref_j,'String'));
574if isempty(first_i)
575    first_i=ref_i;
576elseif first_i < MinIndex_i
577    first_i=MinIndex_i;
578end
579first_j=str2num(get(handles.num_first_j,'String'));
580if isempty(first_j)
581    first_j=ref_j;
582elseif first_j<MinIndex_j
583    first_j=MinIndex_j;
584end
585last_i=str2num(get(handles.num_last_i,'String'));
586if isempty(last_i)
587    last_i=ref_i;
588elseif last_i > MaxIndex_i
589    last_i=MaxIndex_i;
590end
591last_j=str2num(get(handles.num_first_j,'String'));
592if isempty(last_j)
593    last_j=ref_j;
594elseif last_j>MaxIndex_j
595    last_j=MaxIndex_j;
596end
597set(handles.num_first_i,'String',num2str(first_i));
598set(handles.num_first_j,'String',num2str(first_j));
599set(handles.num_last_i,'String',num2str(last_i));
600set(handles.num_last_j,'String',num2str(last_j));
601
602%% read timing and total frame number from the current file (movie files) !! may be overrid by xml file
603InputTable=get(handles.InputTable,'Data');
604FileBase=fullfile(InputTable{iview,1},InputTable{iview,3});
605time=[];%default
606% case of movies
607if strcmp(InputTable{iview,4},'*')
608    if ~isempty(VideoObject)
609        imainfo=get(VideoObject);
610        time=(0:1/imainfo.FrameRate:(imainfo.NumberOfFrames-1)/imainfo.FrameRate)';
611        set(handles.Dt_txt,'String',['Dt=' num2str(1000/imainfo.FrameRate) 'ms']);%display the elementary time interval in millisec
612        ColorType='truecolor';
613    elseif ~isempty(imformats(regexprep(InputTable{iview,5},'^.',''))) || isequal(InputTable{iview,5},'.vol')%&& isequal(NomType,'*')% multi-frame image
614        if ~isempty(InputTable{iview,2})
615            imainfo=imfinfo(fullfile(InputTable{iview,1},InputTable{iview,2},[InputTable{iview,3} InputTable{iview,5}]));
616        else
617            imainfo=imfinfo([FileBase InputTable{iview,5}]);
618        end
619        ColorType=imainfo.ColorType;%='truecolor' for color images
620        if length(imainfo) >1 %case of image with multiple frames
621            nbfield=length(imainfo);
622            nbfield_j=1;
623        end
624    end
625end
626
627%%  read image documentation file  if found%%%%%%%%%%%%%%%%%%%%%%%%%%%
628
629ext_imadoc='';
630if exist([FileBase '.xml'],'file')
631    ext_imadoc='.xml';
632elseif exist([FileBase '.civ'],'file')
633    ext_imadoc='.civ';
634end
635%read the ImaDoc file
636XmlData=[];
637NbSlice_calib={};
638if isequal(ext_imadoc,'.xml')
639        [XmlData,warntext]=imadoc2struct([FileBase '.xml']);
640        if isfield(XmlData,'Heading') && isfield(XmlData.Heading,'ImageName') && ischar(XmlData.Heading.ImageName)
641            [PP,FF,ext_ima_read]=fileparts(XmlData.Heading.ImageName);
642        end
643        if isfield(XmlData,'Time')
644            time=XmlData.Time;
645        end
646        if isfield(XmlData,'Camera')
647            if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
648                NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
649                if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
650                    msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
651                end
652            end
653            if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
654                TimeUnit=XmlData.Camera.TimeUnit;
655            end
656        end
657        if ~isempty(warntext)
658            msgbox_uvmat('WARNING',warntext)
659        end 
660elseif isequal(ext_imadoc,'.civ')
661    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
662    time=XmlData.Time;
663    if error==2, warntext=['no file ' FileBase '.civ'];
664    elseif error==1, warntext='inconsistent number of fields in the .civ file';
665    end 
666end
667
668%% update time table
669TimeTable=get(handles.TimeTable,'Data');
670if isempty(MinIndex_j)
671    TimeTable{iview,1}=time(MinIndex_i);
672    TimeTable{iview,2}=time(first_i);
673    TimeTable{iview,3}=time(last_i);
674    TimeTable{iview,4}=time(MaxIndex_i);
675elseif ~isempty(time)
676    TimeTable{iview,1}=time(MinIndex_i,MinIndex_j);
677    TimeTable{iview,2}=time(first_i,first_j);
678    TimeTable{iview,3}=time(last_i,last_j);
679    TimeTable{iview,4}=time(MaxIndex_i,MaxIndex_j);
680end
681set(handles.TimeTable,'Data',TimeTable)
682
683%% number of slices
684if isfield(XmlData,'GeometryCalib') && isfield(XmlData.GeometryCalib,'SliceCoord')
685    siz=size(XmlData.GeometryCalib.SliceCoord);
686    if siz(1)>1
687        NbSlice=siz(1);
688    else
689        NbSlice=1;
690    end
691    set(handles.num_NbSlice,'String',num2str(NbSlice))
692end
693
694%% update pair menus
695set(handles.Pairs,'Visible','on')
696set(handles.PairString,'Visible','on')
697ListView=get(handles.ListView,'String');
698ListView{iview}=num2str(iview);
699set(handles.ListView,'String');
700set(handles.ListView,'Value',iview)
701update_mode(handles,i1_series,i2_series,j1_series,j2_series,time)
702
703
704%% display the set of existing files as an image
705set(handles.waitbar_frame,'Units','pixels')
706pos=get(handles.waitbar_frame,'Position');
707xima=0.5:pos(3)-0.5;% pixel positions on the image representing the existing file indices
708yima=0.5:pos(4)-0.5;
709[XIma,YIma]=meshgrid(xima,yima);
710nb_i=size(i1_series,1);
711nb_j=size(i1_series,2);
712ind_i=(0.5:nb_i-0.5)*pos(3)/nb_i;
713ind_j=(0.5:nb_j-0.5)*pos(4)/nb_j;
714[Ind_i,Ind_j]=meshgrid(ind_i,ind_j);
715CData=zeros([size(XIma) 3]);
716file_ima=double((i1_series(:,:,1)>0)');
717if numel(file_ima)>=2
718if size(file_ima,1)==1
719    CLine=interp1(ind_i,file_ima,xima,'nearest');
720    CData(:,:,2)=ones(size(yima'))*CLine;
721else
722    CData(:,:,2)=interp2(Ind_i,Ind_j,file_ima,XIma,YIma,'nearest');
723end
724set(handles.waitbar_frame,'CData',CData)
725end
726set(handles.waitbar_frame,'Units','normalized')
727
728%% enable field and veltype menus
729SeriesData=get(handles.series,'UserData');
730SeriesData.FileType{iview}=FileType;
731check_civ=0;
732check_netcdf=0;
733for iview=1:length(SeriesData.FileType)
734    switch SeriesData.FileType{iview}
735        case {'civx','civdata'}
736            check_civ=check_civ+1;
737        case 'netcdf'
738            check_netcdf=check_netcdf+1;
739    end
740end
741if check_civ
742    enable='on';
743else
744    enable='off';
745end
746set(handles.VelTypeMenu,'Visible',enable)
747set(handles.VelType_text,'Visible',enable)
748if check_civ>=2
749    enable='on';
750else
751    enable='off';
752end
753set(handles.VelTypeMenu_1,'Visible',enable)
754set(handles.VelType_text_1,'Visible',enable)
755if check_civ || check_netcdf
756    enable='on';
757else
758    enable='off';
759end
760set(handles.FieldMenu,'Visible',enable)
761set(handles.Field_text,'Visible',enable)
762if check_civ+ check_netcdf>=2
763    enable='on';
764else
765    enable='off';
766end
767set(handles.FieldMenu_1,'Visible',enable)
768set(handles.Field_text_1,'Visible',enable)
769FieldString={''};
770if check_civ
771    FieldString=[calc_field;{'get_field...'}];
772elseif check_netcdf
773    FieldString={'get_field...'};
774end
775set(handles.FieldMenu,'String',FieldString)
776FieldString={''};
777if check_civ>=2
778    FieldString=[calc_field;{'get_field...'}];
779elseif check_civ+check_netcdf>=2
780    FieldString={'get_field...'};
781end
782set(handles.FieldMenu_1,'String',{'get_field...'})
783% testfield=isequal(get(handles.FieldMenu,'enable'),'on');
784% testfield_1=isequal(get(handles.FieldMenu_1,'enable'),'on');
785% testveltype=isequal(get(handles.VelTypeMenu,'enable'),'on');
786% testveltype_1=isequal(get(handles.VelTypeMenu_1,'enable'),'on');
787% testtransform=isequal(get(handles.transform_fct,'Enable'),'on');
788% testnc=0;
789% testnc_1=0;
790% testcivx=0;
791% testcivx_1=0;
792% testima=0; %test for image input
793% if isequal(lower(FileExt),'.avi') %.avi file
794%     testima=1;
795% elseif ~isempty(imformats(FileExt(2:end)))
796%     testima=1;
797% elseif isequal(FileExt,'.vol')
798%      testima=1;
799% end
800%TODO: update
801% if length(FileExtCell)==1 || length(FileExtCell)>2
802%     for iview=1:length(FileExtCell)
803%         if isequal(FileExtCell{iview},'.nc')
804%             testnc=1;
805%         end
806%         if isequal(FileTypeCell{iview},'civx')
807%             testcivx=1;
808%         end
809%     end
810% elseif length(FileExtCell)==2
811%     testnc=isequal(FileExtCell{1},'.nc');
812%     testnc_1=isequal(FileExtCell{2},'.nc');
813%     testcivx=isequal(FileTypeCell{1},'civx');
814%     testcivx_1=isequal(FileTypeCell{2},'civx');
815% end
816% switch FileType
817%     case {'civx','civdata'}
818%     view_FieldMenu(handles,'on')
819%     menustr=get(handles.FieldMenu,'String');
820%     if isequal(menustr,{'get_field...'})
821%         set(handles.FieldMenu,'String',{'get_field...';'velocity';'vort';'div';'more...'})
822%     end
823%     set(handles.VelTypeMenu,'Visible','on')
824%     set(handles.FieldTransform,'Visible','on')
825%     %      view_TRANSFORM(handles,'on')
826%     %     TODO: second menu
827%     %           view_FieldMenu_1(handles,'on')
828%     %     if testcivx_1
829%     %         menustr=get(handles.FieldMenu_1,'String');
830%     %         if isequal(menustr,{'get_field...'})
831%     %             set(handles.FieldMenu_1,'String',{'get_field...';'velocity';'vort';'div';'more...'})
832%     %         end
833%     %     else
834%     %         set(handles.FieldMenu_1,'Value',1)
835%     %         set(handles.FieldMenu_1,'String',{'get_field...'})
836%     %     set(handles.VelTypeMenu_1,'Visible','on')
837%     %     set(handles.VelType_text_1,'Visible','on');
838%     %     end
839%     %     view_FieldMenu_1(handles,'off')
840%     case 'netcdf'
841%     view_FieldMenu(handles,'on')
842%     set(handles.FieldMenu,'Value',1)
843%     set(handles.FieldMenu,'String',{'get_field...'})
844%     set(handles.FieldTransform,'Visible','off')
845%     %     view_TRANSFORM(handles,'off')
846%     case {'image','multimage','video'}
847%     view_FieldMenu(handles,'off')
848%     view_FieldMenu_1(handles,'off')
849%     set(handles.VelTypeMenu,'Visible','off')
850%     set(handles.VelType_text,'Visible','off');
851% end
852
853
854
855%% store the series info in 'UserData'
856SeriesData.i1_series{iview}=i1_series;
857SeriesData.i2_series{iview}=i2_series;
858SeriesData.j1_series{iview}=j1_series;
859SeriesData.j2_series{iview}=j2_series;
860SeriesData.FileType{iview}=FileType;
861SeriesData.Time{iview}=time;
862set(handles.series,'UserData',SeriesData)
863
864%% check for pair display
865check_pairs=0;
866for iview=1:numel(SeriesData.i2_series)
867    if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
868        check_pairs=1;
869    end
870end
871if check_pairs
872    set(handles.Pairs,'Visible','on')
873    set(handles.PairString,'Visible','on')
874else
875    set(handles.Pairs,'Visible','off')
876    set(handles.PairString,'Visible','off')
877end
878
879return
880
881%% set default options in menu 'Fields'%% TODO: check VelType
882if ~testima
883    testcivx=0;
884    if isfield(UvData,'FieldsString') && isequal(UvData.FieldsString,{'get_field...'})% field menu defined as input (from get_field)
885        set(handles_Fields,'Value',1)
886        set(handles_Fields,'String',{'get_field...'})
887        UvData=rmfield(UvData,'FieldsString');
888    else
889        Data=nc2struct(FileName,'ListGlobalAttribute','Conventions','absolut_time_T0','civ');
890        if strcmp(Data.Conventions,'uvmat/civdata') ||( ~isempty(Data.absolut_time_T0)&& ~isequal(Data.civ,0))%if the new input is Civx
891            FieldList=calc_field;
892            set(handles_Fields,'String',[{'image'};FieldList;{'get_field...'}]);%standard menu for civx data
893            set(handles_Fields,'Value',2) % set menu to 'velocity'
894            col_vec=FieldList;
895            col_vec(1)=[];%remove 'velocity' option for vector color (must be a scalar)
896            testcivx=1;
897        end
898        if ~testcivx
899            set(handles_Fields,'Value',1) % set menu to 'get_field...
900            set(handles_Fields,'String',{'get_field...'})
901            col_vec={'get_field...'};
902        end
903        set(handles.ColorScalar,'String',col_vec)
904    end
905end
906set(handles.uvmat,'UserData',UvData)
907
908%% set index navigation options and refresh plots
909scan_option='i';%default
910state_j='off'; %default
911if index==2
912    if get(handles.scan_j,'Value')
913        scan_option='j'; %keep the scan option for the second fiel series
914    end
915    if strcmp(get(handles.j1,'Visible'),'on')
916        state_j='on';
917    end
918end
919if ~isempty(j1_series)
920        state_j='on';
921        if isequal(nbfield,1) &&index==1
922            scan_option='j'; %scan j index by default if nbfield=1               
923        end
924end
925if isequal(scan_option,'i')
926     set(handles.scan_i,'Value',1)
927     scan_i_Callback([],[], handles);
928else
929     set(handles.scan_j,'Value',1)
930     scan_j_Callback([],[], handles);
931end
932set(handles.scan_j,'Visible',state_j)
933set(handles.j1,'Visible',state_j)
934set(handles.j2,'Visible',state_j)
935set(handles.last_j,'Visible',state_j);
936set(handles.frame_j,'Visible',state_j);
937set(handles.j_text,'Visible',state_j);
938if ~isempty(i2_series)||~isempty(j2_series)
939    set(handles.CheckFixPair,'Visible','on')
940elseif index==1
941    set(handles.CheckFixPair,'Visible','off')
942end
943
944
945mode_Callback(hObject, eventdata, handles)
946
947set(handles.REFRESH_INDICES,'BackgroundColor',[0.7 0.7 0.7])
948InputTable=get(handles.InputTable,'Data');
949check_lines=get(handles.REFRESH_INDICES,'UserData');
950
951%% check the indices and FileTypes for each series (limited to the new ones to save time)
952for ind_list=1:length(check_lines)
953    if  check_lines(ind_list)
954        InputLine=InputTable(ind_list,:);
955        detect_idem=strcmp('"',InputLine);% look for '" (repeat of previous data)
956        detect_idem=detect_idem(detect_idem>0);
957        if ~isempty (detect_idem)
958            InputLine(detect_idem)=InputTable(ind_list-1,detect_idem);
959            set(handles.InputTable,'Data',InputTable)
960        end
961        fileinput=fullfile_uvmat(InputLine{1},InputLine{2},InputLine{3},InputLine{5},InputLine{4},1,2,1,2);
962        %fileinput=name_generator(fullfile(InputLine{1},InputLine{3}),1,1,InputLine{5},InputLine{4},1,2,2,InputLine{2})
963        %update file series defined by the selected line
964        [InputTable{ind_list,3},InputTable{(ind_list),4},errormsg]=update_indices(handles,fileinput,ind_list);
965        if ~isempty(errormsg)
966                msgbox_uvmat('ERROR',errormsg)
967                return
968        end
969    end
970end
971set(handles.InputTable,'Data',InputTable)
972SeriesData=get(handles.series,'UserData');
973
974state_j='off';
975state_Pairs='off';
976state_InputFields='off';
977val=get(handles.ListView,'Value');
978ListViewString={''};
979if ~isempty(SeriesData)
980%     ListViewString={};
981    for iview=1:size(InputTable,1)
982        if ~isempty(SeriesData.j1_series{iview})
983            state_j='on';
984        end
985        if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
986            state_Pairs='on';
987            ListViewString{iview}=num2str(iview);
988            if check_lines(iview)
989                val=iview;%select the last pair if it is a new entry
990            end
991        end
992        if strcmp(SeriesData.FileType{iview},'civx')||strcmp(SeriesData.FileType{iview},'civdata')
993            state_InputFields='on';
994        end
995    end
996end
997set(handles.ListView,'Value',val)
998set(handles.ListView,'String',ListViewString)
999if strcmp(state_Pairs,'on')
1000    ListView_Callback(hObject,eventdata,handles)
1001end
1002set(handles.PairString,'Visible',state_Pairs)
1003enable_j(handles,state_j)
1004
1005
1006%------------------------------------------------------------------------
1007% --- Executes when selected cell(s) is changed in PairString.
1008function PairString_CellSelectionCallback(hObject, eventdata, handles)
1009%------------------------------------------------------------------------   
1010set(handles.ListView,'Value',eventdata.Indices(1))% detect the selected raw index
1011ListView_Callback ([],[],handles) % update the list of available pairs
1012
1013%------------------------------------------------------------------------
1014%------------------------------------------------------------------------
1015%  III - FUNCTIONS ASSOCIATED TO THE FRAME SET PAIRS
1016%------------------------------------------------------------------------
1017%------------------------------------------------------------------------
1018% --- Executes on selection change in ListView.
1019function ListView_Callback(hObject, eventdata, handles)
1020%------------------------------------------------------------------------   
1021SeriesData=get(handles.series,'UserData');
1022i2_series=[];
1023j2_series=[];
1024iview=get(handles.ListView,'Value');
1025if ~isempty(SeriesData.i2_series{iview})
1026    i2_series=SeriesData.i2_series{iview};
1027end
1028if ~isempty(SeriesData.j2_series{iview})
1029    j2_series=SeriesData.j2_series{iview};
1030end
1031update_mode(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1032    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1033
1034%------------------------------------------------------------------------
1035% --- Executes on button press in mode.
1036function mode_Callback(hObject, eventdata, handles)
1037%------------------------------------------------------------------------       
1038SeriesData=get(handles.series,'UserData');
1039iview=get(handles.ListView,'Value');
1040mode_list=get(handles.mode,'String');
1041mode=mode_list{get(handles.mode,'Value')};
1042if isequal(mode,'bursts')
1043    enable_i(handles,'On')
1044    enable_j(handles,'Off') %do not display j index scanning in burst mode (j is fixed by the burst choice)
1045else
1046    enable_i(handles,'On')
1047    enable_j(handles,'Off')
1048end
1049fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1050    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.Time{iview})
1051ListPairs_Callback([],[],handles)
1052
1053%-------------------------------------------------------------
1054% --- Executes on selection in ListPairs.
1055function ListPairs_Callback(hObject,eventdata,handles)
1056%------------------------------------------------------------
1057list_pair=get(handles.ListPairs,'String');%get the menu of image pairs
1058if isempty(list_pair)
1059    string='';
1060else
1061    string=list_pair{get(handles.ListPairs,'Value')};
1062    string=regexprep(string,',.*','');%removes time indication (after ',')
1063end
1064PairString=get(handles.PairString,'Data');
1065iview=get(handles.ListView,'Value');
1066PairString{iview,1}=string;
1067% report the selected pair string to the table PairString
1068set(handles.PairString,'Data',PairString)
1069
1070%------------------------------------------------------------------------
1071function num_ref_i_Callback(hObject, eventdata, handles)
1072%------------------------------------------------------------------------
1073mode_list=get(handles.mode,'String');
1074mode=mode_list{get(handles.mode,'Value')};
1075SeriesData=get(handles.series,'UserData');
1076iview=get(handles.ListView,'Value');
1077fill_ListPair(handles,SeriesData.i1_series{iview},SeriesData.i2_series{iview},...
1078    SeriesData.j1_series{iview},SeriesData.j2_series{iview},SeriesData.time{iview});% update the menu of pairs depending on the available netcdf files
1079ListPairs_Callback([],[],handles)
1080
1081%------------------------------------------------------------------------
1082function num_ref_j_Callback(hObject, eventdata, handles)
1083%------------------------------------------------------------------------
1084num_ref_i_Callback(hObject, eventdata, handles)
1085
1086%------------------------------------------------------------------------
1087function update_mode(handles,i1_series,i2_series,j1_series,j2_series,time)
1088%------------------------------------------------------------------------   
1089check_burst=0;
1090if isempty(j2_series)% no pair menu to display
1091    if isempty(i2_series)
1092        set(handles.mode,'String',{''})
1093    else
1094        set(handles.mode,'Value',1)
1095        set(handles.mode,'String',{'series(Di)'})
1096    end
1097else
1098    nbfield=size(j2_series,1);
1099    nbfield2=size(j2_series,2);
1100    set(handles.mode,'String',{'bursts';'series(Dj)'})
1101    if nbfield2>10 || nbfield==1
1102        set(handles.mode,'Value',2);%set mode to series(Dj) if more than 10 j values
1103    else
1104        set(handles.mode,'Value',1);
1105        check_burst=1;
1106    end
1107end
1108if check_burst
1109    enable_i(handles,'On')
1110    enable_j(handles,'Off') %do not display j index scanning in burst mode (j is fixed by the burst choice)
1111else
1112    enable_i(handles,'On')
1113    enable_j(handles,'On')
1114end
1115fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1116ListPairs_Callback([],[],handles)
1117
1118%--------------------------------------------------------------
1119% determine the menu for civ1 pairstring depending on existing netcdf files
1120% with the reference indices num_ref_i and num_ref_j
1121%----------------------------------------------------------------
1122function fill_ListPair(handles,i1_series,i2_series,j1_series,j2_series,time)
1123
1124mode_list=get(handles.mode,'String');
1125mode=mode_list{get(handles.mode,'Value')};
1126ref_i=str2num(get(handles.num_ref_i,'String'));
1127ref_j=str2num(get(handles.num_ref_j,'String'));
1128if isempty(ref_i)
1129    ref_i=1;
1130end
1131if isempty(ref_j)
1132    ref_j=1;
1133end
1134TimeUnit=get(handles.TimeUnit,'String');
1135if length(TimeUnit)>=1
1136    dtunit=['m' TimeUnit];
1137else
1138    dtunit='e-03';
1139end
1140
1141displ_pair={};
1142if strcmp(mode,'series(Di)')
1143    if isempty(i2_series)
1144        msgbox_uvmat('ERROR','no i1-i2 pair available')
1145        return
1146    end
1147    diff_i=i2_series-i1_series;
1148    min_diff=min(diff_i(diff_i>0));
1149    max_diff=max(diff_i(diff_i>0));
1150    for ipair=min_diff:max_diff
1151        if numel(diff_i(diff_i==ipair))>0
1152            pair_string=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1153            if ~isempty(time)
1154                Dt=time(ref_i+ceil(ipair/2),ref_j)-time(ref_i-floor(ipair/2),ref_j);
1155                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1156            end
1157            displ_pair=[displ_pair;{pair_string}];
1158        end
1159    end
1160    if ~isempty(displ_pair)
1161        displ_pair=[displ_pair;{'Di=*|*'}];
1162    end
1163elseif strcmp(mode,'series(Dj)')
1164    if isempty(j2_series)
1165        msgbox_uvmat('ERROR','no j1-j2 pair available')
1166        return
1167    end
1168    diff_j=j2_series-j1_series;
1169    min_diff=min(diff_j(diff_j>0));
1170    max_diff=max(diff_j(diff_j>0));
1171    for ipair=min_diff:max_diff
1172        if numel(diff_j(diff_j==ipair))>0
1173            pair_string=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ];
1174            if ~isempty(time)
1175                Dt=time(ref_i,ref_j+ceil(ipair/2))-time(ref_i,ref_j-floor(ipair/2));
1176                pair_string=[pair_string ', Dt=' num2str(Dt) ' ' dtunit];
1177            end
1178            displ_pair=[displ_pair;{pair_string}];
1179        end
1180    end
1181    if ~isempty(displ_pair)
1182        displ_pair=[displ_pair;{'Dj=*|*'}];
1183    end
1184elseif strcmp(mode,'bursts')
1185    if isempty(j2_series)
1186        msgbox_uvmat('ERROR','no j1-j2 pair available')
1187        return
1188    end
1189    diff_j=j2_series-j1_series;
1190    min_j1=min(j1_series(j1_series>0));
1191    max_j1=max(j1_series(j1_series>0));
1192    min_j2=min(j2_series(j2_series>0));
1193    max_j2=max(j2_series(j2_series>0));
1194    for pair1=min_j1:min(max_j1,min_j1+20)
1195        for pair2=min_j2:min(max_j2,min_j2+20)
1196        if numel(j1_series(j1_series==pair1))>0 && numel(j2_series(j2_series==pair2))>0
1197            displ_pair=[displ_pair;{['j= ' num2str(pair1) '-' num2str(pair2)]}];
1198        end
1199        end
1200    end
1201    if ~isempty(displ_pair)
1202        displ_pair=[displ_pair;{'j=*-*'}];
1203    end
1204end
1205
1206%% display list of pairstring
1207displ_pair_list=get(handles.ListPairs,'String');
1208NewVal=[];
1209if ~isempty(displ_pair_list)
1210Val=get(handles.ListPairs,'Value');
1211NewVal=find(strcmp(displ_pair_list{Val},displ_pair),1);% look at the previous display in the new menu displ_pï¿œir
1212end
1213if ~isempty(NewVal)
1214    set(handles.ListPairs,'Value',NewVal)
1215else
1216    set(handles.ListPairs,'Value',1)
1217end
1218set(handles.ListPairs,'String',displ_pair)
1219% if isempty(displ_pair)
1220%     msgbox_uvmat('ERROR',['no file available for the selected subdirectory' ])
1221% end
1222
1223
1224
1225% return
1226% %%%%%%%%
1227% %update num_first_i and num_last_i according to the chosen image pairstring
1228% testupdate=0;
1229%
1230% SeriesData=get(handles.series,'UserData');
1231% NomType=SeriesData.NomType{Val};
1232% list_pair=get(handles.ListPairs,'String');%get the menu of image pairs
1233% index_pair=get(handles.ListPairs,'Value');
1234% str_pair=list_pair{index_pair};
1235% ind_equ=strfind(str_pair,'=');%find '='
1236% ind_sep=strfind(str_pair,'|');%find pair separator '|'
1237% ind_com=strfind(str_pair,':');%find ':'
1238% test_bursts=0;
1239% if isempty(ind_sep)
1240%     ind_sep=strfind(str_pair,'-');%find pair separator if it is not '|'
1241%     test_bursts=1;% we are in the case of bursts
1242% end
1243% displ_num=[0 0 0 0]; %default
1244% if ~isempty(ind_sep)&& ~strcmp(str_pair(ind_sep-1),'*')% if there is a pair separator ('|' or '-')
1245%     num1_str=str_pair(ind_equ(1)+1:ind_sep-1);
1246%     num2_str=str_pair(ind_sep+1:ind_com-1);
1247%     num1=str2double(num1_str);
1248%     num2=str2double(num2_str);
1249%     if isequal(num1_str(1),' ')
1250%         num1_str(1)=[];
1251%     end   
1252%     if isequal(num2_str(end),' ')
1253%         num2_str(end)=[];
1254%     end
1255%     switch NomType
1256%        case {'_1-2_1'}
1257%            if isequal(num1_str(1),'0')
1258%                IndexCell{Val}=['_(i-(i+' num2_str ')_j'];
1259%            else
1260%                IndexCell{Val}=['_(i' num1_str ')-(i+' num2_str ')_j'];
1261%            end
1262%            displ_num(3)=num1;
1263%            displ_num(4)=num2;
1264%        case {'_1-2'}
1265%            if isequal(num1_str(1),'0')
1266%                IndexCell{Val}=['_(i' num1_str ')-(i+' num2_str ')'];
1267%            else
1268%                IndexCell{Val}=['_(i' num1_str ')-(i+' num2_str ')'];
1269%            end
1270%            displ_num(3)=num1;
1271%            displ_num(4)=num2;
1272%        case '_1_1-2'
1273%           if test_bursts
1274%               IndexCell{Val}=['_i_' num1_str '-' num2_str ];
1275%           else
1276%               if isequal(num1_str(1),'0')
1277%                  IndexCell{Val}=['_i_j-(j+' num2_str ')'];
1278%               else
1279%                  IndexCell{Val}=['_i_(j' num1_str ')-(j+' num2_str ')'];
1280%               end
1281%           end
1282%           displ_num(1)=num1;
1283%           displ_num(2)=num2;
1284%        case {'#_ab'} %TO COMPLETE
1285%            IndexCell{Val}=['_i_' num1_str '-' num2_str ];
1286%
1287%     end
1288% end
1289% set(handles.NomType,'String',IndexCell)
1290% SeriesData.displ_num(Val,:)=displ_num;
1291% set(handles.series,'UserData',SeriesData)
1292% % set(handles.NomType,'Value',Val)
1293%
1294% if ~isequal(str_pair,'Dj=*|*')&~isequal(str_pair,'Di=*|*')
1295%       mode_list=get(handles.mode,'String');
1296%     mode_value=get(handles.mode,'Value');
1297%     mode=mode_list{mode_value};
1298%       if isequal(mode,'series(Di)')
1299%         first_i=str2num(get(handles.num_first_i,'String'));
1300%         last_i=str2num(get(handles.num_last_i,'String'));
1301%         incr_i=str2num(get(handles.num_incr_i,'String'));
1302%         num1=first_i:incr_i:last_i;
1303%         lastfieldCell=get(handles.num_MaxIndex_i,'String');
1304%         lastfield=str2num(lastfieldCell{1});
1305%         if ~isempty(lastfield)
1306%             ind=find((num1-floor(index_pair/2)*ones(size(num1))>0)& (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield));
1307%             num1=num1(ind);       
1308%         end
1309%         if ~isempty(num1)
1310%             set(handles.num_first_i,'String',num2str(num1(1)));
1311%             set(handles.num_last_i,'String',num2str(num1(end)));
1312%         end
1313%         testupdate=1;
1314%       elseif isequal(mode,'series(Dj)')
1315%         first_j=str2num(get(handles.num_first_j,'String'));
1316%         last_j=str2num(get(handles.num_last_j,'String'));
1317%         incr_j=str2num(get(handles.num_incr_j,'String'));
1318%         num_j=first_j:incr_j:last_j;
1319%         lastfieldCell=get(handles.num_MaxIndex_j,'String');
1320%         if ~isempty(lastfieldCell)
1321%             lastfield2=lastfieldCell{1};
1322%             ind=find((num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1323%                  (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2));
1324%         end
1325%         testupdate=1;
1326%       end
1327%       
1328%       %update the first and last times of the series
1329%       if testupdate && isfield(SeriesData,'Time')
1330%         if ~isempty(SeriesData.Time{1})
1331%             displ_time(handles);
1332%         end
1333%       end
1334% end
1335
1336%-------------------------------------
1337function enable_i(handles,state)
1338set(handles.i_txt,'Visible',state)
1339set(handles.num_first_i,'Visible',state)
1340set(handles.num_last_i,'Visible',state)
1341set(handles.num_incr_i,'Visible',state)
1342% set(handles.num_MaxIndex_i,'Visible',state)
1343set(handles.num_ref_i,'Visible',state)
1344set(handles.ref_i_text,'Visible',state)
1345
1346%-----------------------------------
1347function enable_j(handles,state)
1348set(handles.j_txt,'Visible',state)
1349% set(handles.num_MinIndex_j,'Visible',state)
1350set(handles.num_first_j,'Visible',state)
1351set(handles.num_last_j,'Visible',state)
1352set(handles.num_incr_j,'Visible',state)
1353% set(handles.num_MaxIndex_j,'Visible',state)
1354set(handles.num_ref_j,'Visible',state)
1355set(handles.ref_j_text,'Visible',state)
1356
1357%-----------------------------------
1358function view_FieldMenu(handles,state)
1359% set(handles.FieldMenu,'Visible',state)
1360% set(handles.Field_text,'Visible',state)
1361set(handles.InputFields,'Visible',state)
1362
1363%-----------------------------------
1364function view_FieldMenu_1(handles,state)
1365set(handles.FieldMenu_1,'Visible',state)
1366set(handles.Field_text_1,'Visible',state)
1367
1368% %-----------------------------------
1369% function view_TRANSFORM(handles,state)
1370% set(handles.TRANSFORM_frame,'Visible',state)
1371% set(handles.transform_fct,'Visible',state);
1372% set(handles.TRANSFORM_title,'Visible',state)
1373
1374
1375%list_pair_civ_Callback([],[],handles)
1376
1377
1378
1379%------------------------------------------------------------------------
1380% --- Executes on button press in RUN.
1381function RUN_Callback(hObject, eventdata, handles)
1382%------------------------------------------------------------------------
1383%% Read parameters from series
1384Series=read_GUI(handles.series);%TODO: extend to all input param
1385Series.hseries=handles.series; % handles to the series GUI
1386
1387%% read root name and field type
1388set(handles.RUN,'BusyAction','queue');
1389set(0,'CurrentFigure',handles.series)
1390if isequal(get(handles.GetObject,'Visible'),'on') && isequal(get(handles.GetObject,'Value'),1)
1391    Series.GetObject=1;
1392    GetObject_Callback(hObject, eventdata, handles)
1393else
1394    Series.GetObject=0;
1395end
1396% SeriesData=get(handles.series,'UserData');
1397
1398% Series.hseries=handles.series; % handles to the series GUI
1399   first_i=1;
1400   last_i=1;
1401   incr_i=1;
1402       first_j=1;
1403    last_j=1;
1404    incr_j=1;
1405if isfield(Series.IndexRange,'first_i')
1406    first_i=Series.IndexRange.first_i;
1407    incr_i=Series.IndexRange.incr_i;
1408    last_i=Series.IndexRange.last_i;
1409end
1410if isfield(Series.IndexRange,'first_j')
1411    first_j=Series.IndexRange.first_j;
1412    incr_j=Series.IndexRange.incr_j;
1413    last_j=Series.IndexRange.last_j;
1414end
1415
1416%% read input file parameters and set menus
1417Series.PathProject=get(handles.PathCampaign,'String');
1418% InputTable=get(handles.InputTable,'Data');
1419% RootPath=Series.InputTable(:,1);
1420% SubDir=Series.InputTable(:,2);
1421% RootFile=Series.InputTable(:,3);
1422% NomType=Series.InputTable(:,4);
1423% FileExt=Series.InputTable(:,5);
1424% if isempty(SeriesData)
1425%     msgbox_uvmat('ERROR','no input file series')
1426%     return
1427% end
1428% NomType=SeriesData.NomType;
1429% if length(RootPath)==1 %string character input for user fct
1430%     Series.RootPath=RootPath{1};
1431%     Series.RootFile=RootFile{1};
1432%     Series.SubDir=SubDir{1};
1433%     Series.FileExt=FileExt{1};
1434%     Series.NomType=NomType{1};
1435% else %cell input for user fct
1436%     Series.RootPath=RootPath;
1437%     Series.RootFile=RootFile;
1438%     Series.SubDir=SubDir;
1439%     Series.FileExt=FileExt;
1440%     Series.NomType=NomType;
1441% end
1442% if isequal(get(handles.FieldMenu,'Visible'),'on')
1443%     FieldMenu=get(handles.FieldMenu,'String');
1444%     FieldValue=get(handles.FieldMenu,'Value');
1445%     Series.Field=FieldMenu(FieldValue);
1446% end
1447menu_coord_state=get(handles.transform_fct,'Visible');
1448Series.CoordType='';%default
1449if isequal(menu_coord_state,'on')
1450%     menu_coord=get(handles.transform_fct,'String');
1451    menu_index=get(handles.transform_fct,'Value');
1452    transform_list=get(handles.transform_fct,'UserData');
1453    Series.FieldTransform.fct_handle=transform_list{menu_index};% transform function handles
1454end
1455
1456%reinitiate waitbar position
1457Series.WaitbarPos=get(handles.waitbar_frame,'Position');%TO SUPPRESS
1458waitbarpos=Series.WaitbarPos;
1459waitbarpos(4)=0.005;%reinitialize waitbar to zero height
1460waitbarpos(2)=Series.WaitbarPos(2)+Series.WaitbarPos(4)-0.005;
1461% set(handles.waitbar,'Position',waitbarpos)
1462
1463if isfield(Series.IndexRange,'NbSlice')
1464Series.NbSlice=Series.IndexRange.NbSlice;
1465end
1466if last_i < first_i | last_j < first_j , msgbox_uvmat('ERROR','last field number must be larger than the first one'),...
1467    set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1468num_i=first_i:incr_i:last_i;
1469num_j=first_j:incr_j:last_j;
1470% nbfield_cell=get(handles.num_MaxIndex_i,'String');
1471nbfield=cell2mat(Series.IndexRange.MaxIndex);
1472nb=min(nbfield,1);
1473% nbfield=nb(1);
1474% nbfield2=nb(2);
1475
1476%get complementary information from the 'series' interface
1477list_action=get(handles.ACTION,'String');% list menu action
1478index_action=get(handles.ACTION,'Value');% selected string index
1479action= list_action{index_action}; % selected string
1480mode_list=get(handles.mode,'String');
1481index_mode=get(handles.mode,'Value');
1482mode=mode_list{index_mode};
1483ind_shift=0;%default
1484
1485
1486%% defining the ACTION function handle
1487path_series=which('series');
1488list_path=get(handles.ACTION,'UserData');
1489index=get(handles.ACTION,'Value');
1490fct_path=list_path{index}; %path stored for the function ACTION
1491if ~isequal(fct_path,path_series)
1492    eval(['spath=which(''' action ''');']) %spath = current path of the selected function ACTION
1493    if ~exist(fct_path,'dir')
1494        msgbox_uvmat('ERROR',['The prescibed function path ' fct_path ' does not exist'])
1495        return
1496    end
1497    if ~isequal(spath,fct_path)
1498        addpath(fct_path)% add the prescribed path if not the current one
1499    end
1500end
1501eval(['h_fun=@' action ';'])%create a function handle for ACTION
1502if ~isequal(fct_path,path_series)
1503        rmpath(fct_path)% add the prescribed path if not the current one   
1504end
1505
1506%% RUN ACTION
1507Series.Action=action;%name of the processing programme
1508set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1509h_fun(Series);
1510% if length(RootPath)>1
1511%     h_fun(i1_series_cell,i2_series_cell,j1_series_cell,j2_series_cell,Series);
1512% else
1513%     h_fun(i1_series,i2_series,j1_series,j2_series,Series);
1514% end
1515set(handles.RUN,'BackgroundColor',[1 0 0])
1516
1517% %save the current interface setting as figure namefig, append .0 to the name if it already exists
1518% detect=1;
1519% while detect==1
1520%     namefigfull=[namedoc '.fig'];
1521%     hh=dir(namefigfull);
1522%     if ~isempty(hh)
1523%         detect=1;
1524%         namedoc=[namedoc '.0'];
1525%     else
1526%         detect=0;
1527%     end
1528% end
1529% saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
1530
1531%------------------------------------------------------------------------
1532function STOP_Callback(hObject, eventdata, handles)
1533%------------------------------------------------------------------------
1534set(handles.RUN, 'BusyAction','cancel')
1535set(handles.RUN,'BackgroundColor',[1 0 0])
1536
1537
1538%------------------------------------------------------------------------
1539function num_first_i_Callback(hObject, eventdata, handles)
1540%------------------------------------------------------------------------
1541num_last_i_Callback(hObject, eventdata, handles)
1542
1543%------------------------------------------------------------------------
1544function num_last_i_Callback(hObject, eventdata, handles)
1545%------------------------------------------------------------------------
1546SeriesData=get(handles.series,'UserData');
1547if ~isfield(SeriesData,'Time')
1548    SeriesData.Time{1}=[];
1549end
1550displ_time(handles);
1551
1552%------------------------------------------------------------------------
1553function num_first_j_Callback(hObject, eventdata, handles)
1554%------------------------------------------------------------------------
1555 num_last_j_Callback(hObject, eventdata, handles)
1556
1557%------------------------------------------------------------------------
1558function num_last_j_Callback(hObject, eventdata, handles)
1559%------------------------------------------------------------------------
1560first_j=str2num(get(handles.num_first_j,'String'));
1561last_j=str2num(get(handles.num_last_j,'String'));
1562ref_j=ceil((first_j+last_j)/2);
1563set(handles.num_ref_j,'String', num2str(ref_j))
1564num_ref_j_Callback(hObject, eventdata, handles)
1565SeriesData=get(handles.series,'UserData');
1566if ~isfield(SeriesData,'Time')
1567    SeriesData.Time{1}=[];
1568end
1569displ_time(handles);
1570
1571
1572% NomTypeCell=SeriesData.NomType;
1573% if ~isempty(NomTypeCell)
1574%     Val=get(handles.NomType,'Value');
1575%     NomType=NomTypeCell{Val};
1576%     if isequal(NomType,'_1_1-2')|| isequal(NomType,'_1-2_1')|| isequal(NomType,'_1-2')
1577%         if isequal(mode,'series(Dj)')
1578%             fill_ListPair(handles,Val);% update the menu of pairstring depending on the available netcdf files
1579%         end
1580%     end
1581% end
1582%------------------------------------------------------------------------
1583% ---- find the times corresponding to the first and last indices of a series
1584function displ_time(handles)
1585%------------------------------------------------------------------------
1586SeriesData=get(handles.series,'UserData');%
1587ref_i=[str2num(get(handles.num_first_i,'String')) str2num(get(handles.num_last_i,'String'))];
1588ref_j=[str2num(get(handles.num_first_j,'String')) str2num(get(handles.num_last_j,'String'))];
1589% last_i=str2num(get(handles.num_last_i,'String'));
1590% last_j=str2num(get(handles.num_last_j,'String'));
1591TimeTable=get(handles.TimeTable,'Data');
1592Pairs=get(handles.PairString,'Data');
1593for iview=1:size(TimeTable,1)
1594    if size(SeriesData.Time,1)<iview
1595        break
1596    end
1597    i1=ref_i;
1598    j1=ref_j;
1599    i2=ref_i;
1600    j2=ref_j;
1601    % case of pairs
1602    if ~isempty(Pairs{iview,1})
1603        r=regexp(Pairs{iview,1},'(?<mode>(Di=)|(Dj=)) -*(?<num1>\d+)\|(?<num2>\d+)','names');
1604        if isempty(r)
1605            r=regexp(Pairs.list_pair_civ,'(?<num1>\d+)(?<mode>-)(?<num2>\d+)','names');
1606        end
1607        switch r.mode
1608            case 'Di='  %  case 'series(Di)')
1609                i1=ref_i-str2num(r.num1);
1610                i2=ref_i+str2num(r.num2);
1611            case 'Dj='  %  case 'series(Dj)'
1612                j1=ref_j-str2num(r.num1);
1613                j2=ref_j+str2num(r.num2);
1614            case '-'  % case 'bursts'
1615                j1=str2num(r.num1)*ones(size(ref_i));
1616                j2=str2num(r.num2)*ones(size(ref_i));
1617        end
1618    end
1619    TimeTable{iview,2}=[];
1620    TimeTable{iview,3}=[];
1621    if size(SeriesData.Time{iview},1)>=i2(2)&&size(SeriesData.Time{iview},1)>=j2(2)
1622        if isempty(ref_j)
1623            time_first=(SeriesData.Time{iview}(i1(1))+SeriesData.Time{iview}(i2(1)))/2;
1624            time_last=(SeriesData.Time{iview}(i1(2))+SeriesData.Time{iview}(i2(2)))/2;
1625        else
1626            time_first=(SeriesData.Time{iview}(i1(1),j1(1))+SeriesData.Time{iview}(i2(1),j2(1)))/2;
1627            time_last=(SeriesData.Time{iview}(i1(2),j1(2))+SeriesData.Time{iview}(i2(2),j2(2)))/2;
1628        end
1629        TimeTable{iview,2}=time_first; %TODO: take into account pairs
1630        TimeTable{iview,3}=time_last; %TODO: take into account pairs
1631    end
1632end
1633set(handles.TimeTable,'Data',TimeTable)
1634
1635
1636%
1637% NomType=InputTable(:,4);
1638% mode_list=get(handles.mode,'String');
1639% index_mode=get(handles.mode,'Value');
1640%
1641% mode=mode_list{index_mode};
1642%
1643% time_first=[];
1644% time_last=[];
1645% if ~isfield(SeriesData,'Time')
1646%     SeriesData.Time{1}=[];
1647% end
1648% TimeTable=get(handles.TimeTable,'Data');
1649% for iview=1:size(TimeTable,1)
1650%     time_first_cell{iview}='?';
1651%     time_last_cell{iview}='?';%default
1652%     time=SeriesData.Time{iview};
1653%     if isequal(NomType{iview},'_1-2_1')|isequal(NomType{iview},'_1_1-2')|isequal(NomType{iview},'#_ab')|isequal(NomType{iview},'_1-2')
1654%         if isfield(SeriesData,'displ_num')& ~isempty(SeriesData.displ_num)
1655%             ind_shift=SeriesData.displ_num(iview,:);
1656%             if isequal(mode,'bursts')
1657%                 first_j=0;
1658%                 last_j=0;
1659%             end
1660%             first_i1=first_i +ind_shift(3);
1661%             first_i2 =first_i +ind_shift(4);
1662%             first_j1 =first_j +ind_shift(1);
1663%             first_j2 =first_j +ind_shift(2);
1664%             last_i1=last_i +ind_shift(3);
1665%             last_i2 =last_i +ind_shift(4);   
1666%             last_j1 =last_j +ind_shift(1);
1667%             last_j2 =last_j +ind_shift(2);
1668%             siz=size(SeriesData.Time{1});
1669%             if first_i1>=1 && first_j1>=1 && siz(1)>=last_i2 && siz(2)>=last_j2
1670%                 time_first=(time(first_i1,first_j1)+time(first_i2,first_j2))/2;
1671%                 time_last=(time(last_i1,last_j1)+time(last_i2,last_j2))/2;
1672%             else%read the time in the nc files
1673%                 RootPath=get(handles.RootPath,'String');
1674%                 RootFile=get(handles.RootFile,'String');
1675%                 SubDir=get(handles.SubDir,'String');
1676%                 %VelType=get(handles.VelType,'String');
1677%                 VelType_str=get(handles.VelTypeMenu,'String');
1678%                 VelType_val=get(handles.VelTypeMenu,'Value');
1679%                 VelType=VelType_str{VelType_val};
1680%                 filebase=fullfile(RootPath{1},RootFile{1});
1681%                 [filefirst]=name_generator(filebase,first_i1,first_j1,'.nc',NomType{iview},1,first_i2,first_j2,SubDir{iview});
1682%                 if  exist(filefirst,'file')
1683%                     Attrib=nc2struct(filefirst,[]);
1684%                     if isfield(Attrib,'Time')
1685%                         time_first=Attrib.Time;
1686%                     else
1687%                         if isfield(Attrib,'absolut_time_T0')
1688%                             time_first=Attrib.absolut_time_T0;
1689%                         end
1690%                         if isfield(Attrib,'absolut_time_T0_2')&&~(isequal(VelType,'civ1')||isequal(VelType,'interp1')||isequal(VelType,'filter1'))
1691%                             time_first=Attrib.absolut_time_T0_2;
1692%                         end
1693%                     end
1694%                 end
1695%                 [filelast]=name_generator(filebase,last_i1,last_j1,'.nc',NomType{iview},1,last_i2,last_j2,SubDir{iview});
1696%                 if exist(filelast,'file')
1697%                    Attrib=nc2struct(filelast,[]);
1698%                     if isfield(Attrib,'Time')
1699%                         time_last=Attrib.Time;
1700%                     else
1701%                         if isfield(Attrib,'absolut_time_T0')
1702%                             time_last=Attrib.absolut_time_T0;
1703%                         end
1704%                         if isfield(Attrib,'absolut_time_T0_2')&&~(isequal(VelType,'civ1')||isequal(VelType,'interp1')||isequal(VelType,'filter1'))
1705%                             time_last=Attrib.absolut_time_T0_2;
1706%                         end
1707%                     end
1708%                 end
1709%             end
1710%         end
1711%     else
1712%         siz=size(time);
1713%         if siz(1)>=last_i && siz(2)>=last_j && first_i>=1 && first_j>=1
1714%             time_first=times(first_i,first_j);
1715%             time_last=times(last_i,last_j);
1716%         end
1717%     end
1718%     time_first_cell{iview}=num2str(time_first,4);
1719%     time_last_cell{iview}=num2str(time_last,4);
1720% end
1721%
1722
1723%------------------------------------------------------------------------
1724% --- Executes on selection change in ACTION.
1725function ACTION_Callback(hObject, eventdata, handles)
1726%------------------------------------------------------------------------
1727global nb_builtin_ACTION
1728list_ACTION=get(handles.ACTION,'String');% list menu fields
1729index_ACTION=get(handles.ACTION,'Value');% selected string index
1730ACTION= list_ACTION{index_ACTION}; % selected function name
1731path_series=which('series');%path to series.m
1732list_path=get(handles.ACTION,'UserData');%list of recorded paths to functions of the list ACTION
1733default_file=fullfile(list_path{end},ACTION);
1734% add a new function to the menu if the selected item is 'more...'
1735if isequal(ACTION,'more...')
1736    pathfct=fileparts(path_series);
1737    [FileName, PathName, filterindex] = uigetfile( ...
1738       {'*.m', ' (*.m)';
1739        '*.m',  '.m files '; ...
1740        '*.*', 'All Files (*.*)'}, ...
1741        'Pick a file',default_file);
1742    if length(FileName)<2
1743        return
1744    end
1745    [pp,ACTION,ext_fct]=fileparts(FileName);%(end-1:end);
1746    if ~isequal(ext_fct,'.m')
1747        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
1748        return
1749    end
1750   
1751   % insert the choice in the action menu
1752   menu_str=update_menu(handles.ACTION,ACTION);%new action menu in which the new item has been appended if needed
1753   index_ACTION=get(handles.ACTION,'Value');% currently selected index in the list
1754   list_path{index_ACTION}=PathName;
1755   if length(menu_str)>nb_builtin_ACTION+5; %nb_builtin=nbre of functions always remaining in the initial menu
1756       nbremove=length(menu_str)-nb_builtin_ACTION-5;
1757       menu_str(nb_builtin_ACTION+1:end-5)=[];
1758       list_path(nb_builtin_ACTION+1:end-4)=[];
1759       index_ACTION=index_ACTION-nbremove;
1760       set(handles.ACTION,'Value',index_ACTION)
1761       set(handles.ACTION,'String',menu_str)
1762   end
1763   list_path{index_ACTION}=PathName;
1764   set(handles.ACTION,'UserData',list_path);
1765   set(handles.path,'enable','inactive')% indicate that the current path is accessible (not 'off')
1766   
1767   %record the current menu in personal file profil_perso
1768   dir_perso=prefdir;
1769   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
1770   for ilist=nb_builtin_ACTION+1:length(menu_str)-1
1771       series_fct{ilist-nb_builtin_ACTION}=fullfile(list_path{ilist},[menu_str{ilist} '.m']);     
1772   end
1773   if nb_builtin_ACTION+1<=length(menu_str)-1
1774       if exist(profil_perso,'file')% && nb_builtin_ACTION+1>=length(menu_str)-1
1775           save(profil_perso,'series_fct','-append')
1776       else
1777           txt=ver('MATLAB');
1778           Release=txt.Release;
1779           relnumb=str2num(Release(3:4));
1780           if relnumb >= 14%recent relaese of Matlab
1781               save(profil_perso,'series_fct','-V6')
1782           else
1783               save(profil_perso, 'series_fct')
1784           end
1785       end
1786   end
1787end
1788
1789%check the current path to the selected function
1790PathName=list_path{index_ACTION};%current recorded path
1791set(handles.path,'String',PathName); %show the path to the senlected function
1792
1793%default setting for the visibility of the GUI elements
1794% set(handles.RootPath,'UserData','many')
1795% set(handles.SubDir,'Visible','on')
1796% set(handles.RootFile,'Visible','on')
1797% set(handles.NomType,'Visible','on')
1798% set(handles.FileExt,'Visible','on')
1799set(handles.num_NbSlice,'Visible','off')
1800set(handles.NbSlice_title,'Visible','off')
1801set(handles.VelTypeMenu,'Visible','off');
1802set(handles.VelType_text,'Visible','off');
1803set(handles.VelTypeMenu_1,'Visible','off');
1804set(handles.VelType_text_1,'Visible','off');
1805view_FieldMenu(handles,'off')
1806view_FieldMenu_1(handles,'off')
1807set(handles.FieldTransform,'Visible','off')
1808% view_TRANSFORM(handles,'off')Visible','off')
1809set(handles.Objects,'Visible','off');
1810set(handles.GetMask,'Visible','off')
1811set(handles.Mask,'Visible','off')
1812% set(handles.GetObject,'Visible','off');
1813set(handles.OutputDir,'Visible','off');
1814% set(handles.PARAMETERS_frame,'Visible','off');
1815% set(handles.PARAMETERS_title,'Visible','off');
1816set(handles.ParamKey,'Visible','off')
1817set(handles.ParamVal,'Visible','off')
1818ParamKey={};
1819set(handles.FieldMenu,'Enable','off')
1820set(handles.VelTypeMenu,'Enable','off')
1821set(handles.FieldMenu_1,'Enable','off')
1822set(handles.VelTypeMenu_1,'Enable','off')
1823set(handles.transform_fct,'Enable','off')
1824%set the displayed GUI item needed for input parameters
1825if ~isequal(path_series,PathName)
1826    addpath(PathName)
1827end
1828eval(['h_function=@' ACTION ';']);
1829try
1830    [fid,errormsg] =fopen([ACTION '.m']);
1831    InputText=textscan(fid,'%s',1,'delimiter','\n');
1832    fclose(fid)
1833    set(handles.ACTION,'ToolTipString',InputText{1}{1})
1834end
1835if ~isequal(path_series,PathName)
1836    rmpath(PathName)
1837end
1838varargout=h_function();
1839Param_list={};
1840
1841%nb_series=length(RootFile);
1842% FileExt=get(handles.FileExt,'String');
1843% nb_series=length(FileExt);
1844InputTable=get(handles.InputTable,'Data');
1845nb_series=size(InputTable,1);
1846% if ~isempty(checkcell)
1847% nb_series=checkcell(end);
1848% end
1849% nb_series=size(InputFiles,1)
1850testima_series=1; %test for a list of images only
1851testima=1;
1852testima_1=1;
1853testciv_series=1;
1854for iview=1:nb_series
1855     ext=InputTable{iview,5};
1856    if length(ext)<2
1857        ext='.none';
1858    end
1859    testimaview=~isempty(imformats(ext(2:end))) || isequal(lower(ext),'.avi');
1860    if ~testimaview
1861        if iview==1
1862            testima=0;
1863        end
1864        if iview==2
1865            testima_1=0;
1866        end
1867        testima_series=0;
1868    end
1869end
1870for ilist=1:length(varargout)-1
1871    switch varargout{ilist}
1872
1873                       %RootFile always visible
1874%          case 'RootPath'   %visible by default
1875%             value=lower(varargout{ilist+1});
1876%             if isequal(value,'one')||isequal(value,'two')||isequal(value,'many')
1877%                 set(handles.RootFile,'UserData',value)% for use in menu Open_insert
1878%             end
1879%         case 'SubDir' %visible by default
1880%             if isequal(lower(varargout{ilist+1}),'off')
1881%                 set(handles.SubDir,'Visible','off')
1882%             end
1883%         case 'RootFile'   %visible by default
1884%             value=lower(varargout{ilist+1});
1885%             if isequal(value,'off')
1886%                 set(handles.RootFile,'Visible','off')
1887%             elseif isequal(value,'one')||isequal(value,'two')||isequal(value,'many')
1888%                 set(handles.RootFile,'Visible','on')
1889%                 set(handles.RootFile,'UserData',value)% for use in menu Open_insert
1890%             end
1891%         case 'NomType'   %visible by default
1892%             if isequal(lower(varargout{ilist+1}),'off')
1893%                 set(handles.NomType,'Visible','off')
1894%             end
1895%         case 'FileExt'   %visible by default
1896%             if isequal(lower(varargout{ilist+1}),'off')
1897%                 set(handles.FileExt,'Visible','off')
1898%             end
1899        case 'NbSlice'   %hidden by default
1900            if isequal(lower(varargout{ilist+1}),'on')
1901                set(handles.num_NbSlice,'Visible','on')
1902                set(handles.NbSlice_title,'Visible','on')
1903            end
1904        case 'VelTypeMenu'   %hidden by default
1905             if isequal(lower(varargout{ilist+1}),'one') || isequal(lower(varargout{ilist+1}),'two')
1906                set(handles.VelTypeMenu,'Enable','on')
1907                if nb_series >=1 && ~testima_series
1908                    set(handles.VelTypeMenu,'Visible','on')
1909                    set(handles.VelType_text,'Visible','on');
1910%                     set(handles.Field_frame,'Visible','on')
1911                end
1912             end
1913            if isequal(lower(varargout{ilist+1}),'two')
1914                set(handles.VelTypeMenu_1,'Enable','on')
1915                if nb_series >=2 && ~testima_series
1916                    set(handles.VelTypeMenu_1,'Visible','on')
1917                    set(handles.VelType_text_1,'Visible','on');
1918                end
1919            end
1920        case 'FieldMenu'   %hidden by default
1921            if isequal(lower(varargout{ilist+1}),'one')||isequal(lower(varargout{ilist+1}),'two')
1922                set(handles.FieldMenu,'Enable','on') % test for MenuBorser
1923                if nb_series >=1 && ~testima_series
1924                    view_FieldMenu(handles,'on')
1925                end
1926            end
1927            if isequal(lower(varargout{ilist+1}),'two')
1928                set(handles.FieldMenu_1,'Enable','on')
1929                if nb_series >=2 && ~testima_1
1930                    view_FieldMenu_1(handles,'on')
1931                end
1932            end
1933        case 'CoordType'   %hidden by default
1934            if isequal(lower(varargout{ilist+1}),'on')
1935                set(handles.transform_fct,'Enable','on')
1936                set(handles.FieldTransform,'Visible','on')
1937%                 view_TRANSFORM(handles,'on')
1938            end
1939        case 'GetObject'   %hidden by default
1940            if isequal(lower(varargout{ilist+1}),'on')   
1941                set(handles.Objects,'Visible','on')
1942%                 set(handles.GetObject,'Visible','on');
1943            end
1944        case 'Mask'   %hidden by default
1945            if isequal(lower(varargout{ilist+1}),'on')   
1946                set(handles.Objects,'Visible','on')
1947%                 set(handles.GetMask,'Visible','on');
1948            end
1949        case 'PARAMETER' 
1950            set(handles.PARAMETERS_frame,'Visible','on')
1951            set(handles.PARAMETERS_title,'Visible','on')
1952            set(handles.ParamKey,'Visible','on')
1953            %set(handles.ParamVal,'Visible','on')
1954            Param_str=varargout{ilist+1};
1955            Param_list=[Param_list; {Param_str}];         
1956    end
1957end
1958if ~isempty(Param_list)
1959    set(handles.ParamKey,'String',Param_list)
1960    set(handles.ParamVal,'Visible','on')
1961end
1962
1963%------------------------------------------------------------------------
1964% --- Executes on selection change in FieldMenu.
1965function FieldMenu_Callback(hObject, eventdata, handles)
1966%------------------------------------------------------------------------
1967field_str=get(handles.FieldMenu,'String');
1968field_index=get(handles.FieldMenu,'Value');
1969field=field_str{field_index(1)};
1970if isequal(field,'get_field...')   
1971     hget_field=findobj(allchild(0),'name','get_field');
1972     if ~isempty(hget_field)
1973         delete(hget_field)%delete opened versions of get_field
1974     end
1975     [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(read_GUI(handles.series));
1976     if exist(filecell{1,1},'file')
1977        get_field(filecell{1,1})
1978     end
1979elseif isequal(field,'more...')
1980    str=calc_field;
1981    [ind_answer,v] = listdlg('PromptString','Select a file:',...
1982                'SelectionMode','single',...
1983                'ListString',str);
1984       % edit the choice in the fields and action menu
1985     scalar=cell2mat(str(ind_answer));
1986     update_menu(handles.FieldMenu,scalar)
1987end
1988
1989%------------------------------------------------------------------------
1990% --- Executes on selection change in FieldMenu_1.
1991function FieldMenu_1_Callback(hObject, eventdata, handles)
1992%------------------------------------------------------------------------
1993field_str=get(handles.FieldMenu_1,'String');
1994field_index=get(handles.FieldMenu_1,'Value');
1995field=field_str{field_index};
1996if isequal(field,'get_field...')   
1997     hget_field=findobj(allchild(0),'name','get_field_1');
1998     if ~isempty(hget_field)
1999         delete(hget_field)
2000     end
2001     SeriesData=get(handles.series,'UserData');
2002     filename=SeriesData.CurrentInputFile_1;
2003     if exist(filename,'file')
2004        hget_field=get_field(filename);
2005        set(hget_field,'name','get_field_1')
2006     end
2007elseif isequal(field,'more...')
2008    str=calc_field;
2009    [ind_answer,v] = listdlg('PromptString','Select a file:',...
2010                'SelectionMode','single',...
2011                'ListString',str);
2012       % edit the choice in the fields and action menu
2013     scalar=cell2mat(str(ind_answer));
2014     update_menu(handles.FieldMenu_1,scalar)
2015end   
2016
2017
2018%%%%%%%%%%%%%
2019function [ind_remove]=find_pairs(dirpair,ind_i,last_i)
2020indsel=ind_i;
2021indiff=diff(ind_i); %test index increment to detect multiplets (several pairs with the same index ind_i) and holes in the series
2022indiff=[1 indiff last_i-ind_i(end)+1];%for testing gaps with the imposed bounds
2023if ~isempty(indiff)
2024    indiff2=diff(indiff);
2025    indiffp=[indiff2 1];
2026    indiffm=[1 indiff2];
2027    ind_multi_m=find((indiff==0)&(indiffm<0))-1;%indices of first members of multiplets
2028    ind_multi_p=find((indiff==0)&(indiffp>0));%indices of last members of multiplets
2029    %for each multiplet, select the most recent file
2030    ind_remove=[];
2031    for i=1:length(ind_multi_m)
2032        ind_pairs=ind_multi_m(i):ind_multi_p(i);
2033        for imulti=1:length(ind_pairs)
2034            datepair(imulti)=datenum(dirpair(ind_pairs(imulti)).date);%dates of creation
2035        end
2036        [datenew,indsort2]=sort(datepair); %sort the multiplet by creation date
2037        ind_s=indsort2(1:end-1);%
2038        ind_remove=[ind_remove ind_pairs(ind_s)];%remove these indices, leave the last one
2039    end
2040end
2041
2042%------------------------------------------------------------------------
2043% --- determine the list of index pairstring of processing file
2044function [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)
2045%------------------------------------------------------------------------
2046num_i1=num_i;% set of first image numbers by default
2047num_i2=num_i;
2048num_j1=num_j;
2049num_j2=num_j;
2050num_i_out=num_i;
2051num_j_out=num_j;
2052% if isequal (NomType,'_1-2_1') || isequal (NomType,'_1-2')
2053if isequal(mode,'series(Di)')
2054    num_i1_line=num_i+ind_shift(3);% set of first image numbers
2055    num_i2_line=num_i+ind_shift(4);
2056    % adjust the first and last field number
2057        indsel=find(num_i1_line >= 1);
2058    num_i_out=num_i(indsel);
2059    num_i1_line=num_i1_line(indsel);
2060    num_i2_line=num_i2_line(indsel);
2061    num_j1=meshgrid(num_j,ones(size(num_i1_line)));
2062    num_j2=meshgrid(num_j,ones(size(num_i1_line)));
2063    [xx,num_i1]=meshgrid(num_j,num_i1_line);
2064    [xx,num_i2]=meshgrid(num_j,num_i2_line);
2065elseif isequal (mode,'series(Dj)')||isequal (mode,'bursts')
2066    if isequal(mode,'bursts') %case of bursts (png_old or png_2D)
2067        num_j1=ind_shift(1)*ones(size(num_i));
2068        num_j2=ind_shift(2)*ones(size(num_i));
2069    else
2070        num_j1_col=num_j+ind_shift(1);% set of first image numbers
2071        num_j2_col=num_j+ind_shift(2);
2072        % adjust the first field number
2073        indsel=find((num_j1_col >= 1));   
2074        num_j_out=num_j(indsel);
2075        num_j1_col=num_j1_col(indsel);
2076        num_j2_col=num_j2_col(indsel);
2077        [num_i1,num_j1]=meshgrid(num_i,num_j1_col);
2078        [num_i2,num_j2]=meshgrid(num_i,num_j2_col);
2079    end   
2080end
2081
2082
2083% set(handles.time_first,'Value',1)
2084% set(handles.time_last,'Value',1)
2085% set(handles.time_first,'String',time_first_cell);
2086% set(handles.time_last,'String',time_last_cell);
2087
2088%------------------------------------------------------------------------
2089% --- Executes on button press in GetObject.
2090function GetObject_Callback(hObject, eventdata, handles)
2091%------------------------------------------------------------------------
2092SeriesData=get(handles.series,'UserData');
2093value=get(handles.GetObject,'Value');
2094if value
2095     set(handles.GetObject,'BackgroundColor',[1 1 0])%put unactivated buttons to yellow
2096     hset_object=findobj(allchild(0),'tag','set_object');%find the set_object interface handle
2097     if ishandle(hset_object)
2098         uistack(hset_object,'top')% show the GUI set_object if opened
2099     else
2100         %get the object file
2101         InputTable=get(handles.InputTable,'Data');
2102         defaultname=InputTable{1,1};
2103         if isempty(defaultname)
2104            defaultname={''};
2105         end
2106        [FileName, PathName, filterindex] = uigetfile( ...
2107       {'*.xml;*.mat', ' (*.xml,*.mat)';
2108       '*.xml',  '.xml files '; ...
2109        '*.mat',  '.mat matlab files '}, ...
2110        'Pick an xml object file (or use uvmat to create it)',defaultname);
2111        fileinput=[PathName FileName];%complete file name
2112        sizf=size(fileinput);
2113        if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
2114        %read the file
2115        data=xml2struct(fileinput);
2116%         t=xmltree(fileinput);
2117%         data=convert(t);
2118        if ~isfield(data,'Style')
2119             data.Style='points';
2120        end
2121        if ~isfield(data,'ProjMode')
2122             data.ProjMode='projection';
2123        end
2124%         data.desable_plot=1;
2125        [SeriesData.hset_object,SeriesData.sethandles]=set_object(data);% call the set_object interface
2126     end
2127else
2128    set(handles.GetObject,'BackgroundColor',[0.7 0.7 0.7])%put activated buttons to green
2129end
2130set(handles.series,'UserData',SeriesData)
2131
2132%--------------------------------------------------------------
2133function GetMask_Callback(hObject, eventdata, handles)
2134value=get(handles.GetMask,'Value');
2135if value
2136    msgbox_uvmat('ERROR','not implemented yet')
2137end
2138%--------------------------------------------------------------
2139
2140%-------------------------------------------------------------------
2141%'uv_ncbrowser': interactively calls the netcdf file browser 'get_field.m'
2142function ncbrowser_uvmat(hObject, eventdata)
2143%-------------------------------------------------------------------
2144     bla=get(gcbo,'String');
2145     ind=get(gcbo,'Value');
2146     filename=cell2mat(bla(ind));
2147      blank=find(filename==' ');
2148      filename=filename(1:blank-1);
2149     get_field(filename)
2150
2151% ------------------------------------------------------------------
2152function MenuHelp_Callback(hObject, eventdata, handles)
2153%-------------------------------------------------------------------
2154path_to_uvmat=which ('uvmat');% check the path of uvmat
2155pathelp=fileparts(path_to_uvmat);
2156helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
2157if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
2158else
2159    addpath (fullfile(pathelp,'uvmat_doc'))
2160    web([helpfile '#series'])
2161end
2162
2163%-------------------------------------------------------------------
2164% --- Executes on selection change in transform_fct.
2165function transform_fct_Callback(hObject, eventdata, handles)
2166%-------------------------------------------------------------------
2167global nb_transform
2168
2169menu=get(handles.transform_fct,'String');
2170ind_coord=get(handles.transform_fct,'Value');
2171coord_option=menu{ind_coord};
2172list_transform=get(handles.transform_fct,'UserData');
2173ff=functions(list_transform{end});
2174if isequal(coord_option,'more...');
2175    coord_fct='';
2176    prompt = {'Enter the name of the transform function'};
2177    dlg_title = 'user defined transform';
2178    num_lines= 1;
2179    [FileName, PathName, filterindex] = uigetfile( ...
2180       {'*.m', ' (*.m)';
2181        '*.m',  '.m files '; ...
2182        '*.*', 'All Files (*.*)'}, ...
2183        'Pick a file', ff.file);
2184    if isequal(PathName(end),'/')||isequal(PathName(end),'\')
2185        PathName(end)=[];
2186    end
2187    transform_selected =fullfile(PathName,FileName);
2188    if ~exist(transform_selected,'file')
2189          return
2190    end
2191    [ppp,transform,xt_fct]=fileparts(FileName);% removes extension .m
2192    if ~isequal(ext_fct,'.m')
2193        msgbox_uvmat('ERROR','a Matlab function .m must be introduced');
2194        return
2195    end
2196   menu=update_menu(handles.transform_fct,transform);%add the selected fct to the menu
2197   ind_coord=get(handles.transform_fct,'Value');
2198   addpath(PathName)
2199   list_transform{ind_coord}=str2func(transform);% create the function handle corresponding to the newly seleced function
2200   set(handles.transform_fct,'UserData',list_transform)
2201   rmpath(PathName)
2202   % save the new menu in the personal file 'uvmat_perso.mat'
2203   dir_perso=prefdir;%personal Matalb directory
2204   profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2205   if exist(profil_perso,'file')
2206       for ilist=nb_transform+1:numel(list_transform)
2207           ff=functions(list_transform{ilist});
2208           transform_fct{ilist-nb_transform}=ff.file;
2209       end
2210        save (profil_perso,'transform_fct','-append'); %store the root name for future opening of uvmat
2211   end
2212end
2213
2214%check the current path to the selected function
2215if ~isempty(list_transform{ind_coord})
2216func=functions(list_transform{ind_coord});
2217set(handles.path_transform,'String',fileparts(func.file)); %show the path to the senlected function
2218else
2219   set(handles.path_transform,'String',''); %show the path to the senlected function
2220end
2221
2222%------------------------------------------------------------------------
2223% --- Executes on button press in REFRESH_INDICES.
2224    function REFRESH_INDICES_Callback(hObject, eventdata, handles)
2225%------------------------------------------------------------------------       
2226% hObject    handle to REFRESH_INDICES (see GCBO)
2227% eventdata  reserved - to be defined in a future version of MATLAB
2228% handles    structure with handles and user data (see GUIDATA)
2229set(handles.REFRESH_INDICES,'BackgroundColor',[0.7 0.7 0.7])
2230InputTable=get(handles.InputTable,'Data');
2231check_lines=get(handles.REFRESH_INDICES,'UserData');
2232
2233%% check the indices and FileTypes for each series (limited to the new ones to save time)
2234for ind_list=1:length(check_lines)
2235    if  check_lines(ind_list)
2236        InputLine=InputTable(ind_list,:);
2237        detect_idem=strcmp('"',InputLine);% look for '" (repeat of previous data)
2238        detect_idem=detect_idem(detect_idem>0);
2239        if ~isempty (detect_idem)
2240            InputLine(detect_idem)=InputTable(ind_list-1,detect_idem);
2241            set(handles.InputTable,'Data',InputTable)
2242        end
2243        fileinput=fullfile_uvmat(InputLine{1},InputLine{2},InputLine{3},InputLine{5},InputLine{4},1,2,1,2);
2244        %fileinput=name_generator(fullfile(InputLine{1},InputLine{3}),1,1,InputLine{5},InputLine{4},1,2,2,InputLine{2})
2245        %update file series defined by the selected line
2246        [InputTable{ind_list,3},InputTable{(ind_list),4},errormsg]=update_indices(handles,fileinput,ind_list);
2247        if ~isempty(errormsg)
2248                msgbox_uvmat('ERROR',errormsg)
2249                return
2250        end
2251    end
2252end
2253set(handles.InputTable,'Data',InputTable)
2254SeriesData=get(handles.series,'UserData');
2255
2256state_j='off';
2257state_Pairs='off';
2258state_InputFields='off';
2259val=get(handles.ListView,'Value');
2260ListViewString={''};
2261if ~isempty(SeriesData)
2262%     ListViewString={};
2263    for iview=1:size(InputTable,1)
2264        if ~isempty(SeriesData.j1_series{iview})
2265            state_j='on';
2266        end
2267        if ~isempty(SeriesData.i2_series{iview})||~isempty(SeriesData.j2_series{iview})
2268            state_Pairs='on';
2269            ListViewString{iview}=num2str(iview);
2270            if check_lines(iview)
2271                val=iview;%select the last pair if it is a new entry
2272            end
2273        end
2274        if strcmp(SeriesData.FileType{iview},'civx')||strcmp(SeriesData.FileType{iview},'civdata')
2275            state_InputFields='on';
2276        end
2277    end
2278end
2279set(handles.ListView,'Value',val)
2280set(handles.ListView,'String',ListViewString)
2281if strcmp(state_Pairs,'on')
2282    ListView_Callback(hObject,eventdata,handles)
2283end
2284set(handles.PairString,'Visible',state_Pairs)
2285enable_j(handles,state_j)
2286set(handles.REFRESH_INDICES,'BackgroundColor',[1 0 0])
2287set(handles.REFRESH_INDICES,'visible','off')
2288
2289% -----------------------------------------------------------------------
2290% --- Update min and max indices of a file series by scanning with find_file_series
2291% --- which also changes the root file and NomType in case of movie. Also adjust the string representation of indices (e.g;
2292% --- 1 or 001 by the function find_file_series
2293% --- This function also dispaly the set of availbale files in the series
2294% --- and the menus appropriate to the file type as well as timing possibly set
2295% --- by an xml image documentation file
2296function [RootFile,NomType,errormsg]=update_indices(handles,fileinput,iview)
2297% -----------------------------------------------------------------------
2298%% look for min and max indices existing in the file series and update SeriesData
2299errormsg='';
2300[FilePath,FileName,FileExt]=fileparts(fileinput);
2301% detect the file type, get the movie object if relevant, and look for the corresponding file series:
2302% the root name and indices may be corrected by including the first index i1 if a corresponding xml file exists
2303[RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object,i1,i2,j1,j2]=find_file_series(FilePath,[FileName FileExt]);
2304if isempty(RootFile)&&isempty(i1_series)
2305    errormsg='no input file in the series';
2306    return
2307end
2308
2309%% adjust the min and max indices common to all the file series
2310MinIndex=get(handles.MinIndex,'Data');
2311MaxIndex=get(handles.MaxIndex,'Data');
2312MinIndex_i=min(i1_series(i1_series>0));
2313if ~isempty(i2_series)
2314    MaxIndex_i=max(i2_series(i2_series>0));
2315else
2316    MaxIndex_i=max(i1_series(i1_series>0));
2317end
2318MinIndex_j=min(j1_series(j1_series>0));
2319if ~isempty(j2_series)
2320    MaxIndex_j=max(j2_series(j2_series>0));
2321else
2322    MaxIndex_j=max(j1_series(j1_series>0));
2323end
2324MinIndex{iview,1}=MinIndex_i;
2325MinIndex{iview,2}=MinIndex_j;
2326MaxIndex{iview,1}=MaxIndex_i;
2327MaxIndex{iview,2}=MaxIndex_j;
2328set(handles.MinIndex,'Data',MinIndex)
2329set(handles.MaxIndex,'Data',MaxIndex)
2330SeriesData=get(handles.series,'UserData');
2331SeriesData.i1_series{iview}=i1_series;
2332SeriesData.i2_series{iview}=i2_series;
2333SeriesData.j1_series{iview}=j1_series;
2334SeriesData.j2_series{iview}=j2_series;
2335SeriesData.FileType{iview}=FileType;
2336
2337%% display the set of existing files as an image
2338set(handles.waitbar_frame,'Units','pixels')
2339pos=get(handles.waitbar_frame,'Position');
2340xima=0.5:pos(3)-0.5;% pixel positions on the image representing the existing file indices
2341yima=0.5:pos(4)-0.5;
2342[XIma,YIma]=meshgrid(xima,yima);
2343nb_i=size(i1_series,1);
2344nb_j=size(i1_series,2);
2345ind_i=(0.5:nb_i-0.5)*pos(3)/nb_i;
2346ind_j=(0.5:nb_j-0.5)*pos(4)/nb_j;
2347[Ind_i,Ind_j]=meshgrid(ind_i,ind_j);
2348CData=zeros([size(XIma) 3]);
2349file_ima=double((i1_series(:,:,1)>0)');
2350if numel(file_ima)>=2
2351if size(file_ima,1)==1
2352    CLine=interp1(ind_i,file_ima,xima,'nearest');
2353    CData(:,:,2)=ones(size(yima'))*CLine;
2354else
2355    CData(:,:,2)=interp2(Ind_i,Ind_j,file_ima,XIma,YIma,'nearest');
2356end
2357set(handles.waitbar_frame,'CData',CData)
2358end
2359set(handles.waitbar_frame,'Units','normalized')
2360
2361%% enable field and veltype menus
2362testfield=isequal(get(handles.FieldMenu,'enable'),'on');
2363testfield_1=isequal(get(handles.FieldMenu_1,'enable'),'on');
2364testveltype=isequal(get(handles.VelTypeMenu,'enable'),'on');
2365testveltype_1=isequal(get(handles.VelTypeMenu_1,'enable'),'on');
2366testtransform=isequal(get(handles.transform_fct,'Enable'),'on');
2367% testnc=0;
2368% testnc_1=0;
2369% testcivx=0;
2370% testcivx_1=0;
2371% testima=0; %test for image input
2372% if isequal(lower(FileExt),'.avi') %.avi file
2373%     testima=1;
2374% elseif ~isempty(imformats(FileExt(2:end)))
2375%     testima=1;
2376% elseif isequal(FileExt,'.vol')
2377%      testima=1;
2378% end
2379%TODO: update
2380% if length(FileExtCell)==1 || length(FileExtCell)>2
2381%     for iview=1:length(FileExtCell)
2382%         if isequal(FileExtCell{iview},'.nc')
2383%             testnc=1;
2384%         end
2385%         if isequal(FileTypeCell{iview},'civx')
2386%             testcivx=1;
2387%         end
2388%     end
2389% elseif length(FileExtCell)==2
2390%     testnc=isequal(FileExtCell{1},'.nc');
2391%     testnc_1=isequal(FileExtCell{2},'.nc');
2392%     testcivx=isequal(FileTypeCell{1},'civx');
2393%     testcivx_1=isequal(FileTypeCell{2},'civx');
2394% end
2395switch FileType
2396    case {'civx','civdata'}
2397    view_FieldMenu(handles,'on')
2398    menustr=get(handles.FieldMenu,'String');
2399    if isequal(menustr,{'get_field...'})
2400        set(handles.FieldMenu,'String',{'get_field...';'velocity';'vort';'div';'more...'})
2401    end
2402    set(handles.VelTypeMenu,'Visible','on')
2403    set(handles.FieldTransform,'Visible','on')
2404    %      view_TRANSFORM(handles,'on')
2405    %     TODO: second menu
2406    %           view_FieldMenu_1(handles,'on')
2407    %     if testcivx_1
2408    %         menustr=get(handles.FieldMenu_1,'String');
2409    %         if isequal(menustr,{'get_field...'})
2410    %             set(handles.FieldMenu_1,'String',{'get_field...';'velocity';'vort';'div';'more...'})
2411    %         end
2412    %     else
2413    %         set(handles.FieldMenu_1,'Value',1)
2414    %         set(handles.FieldMenu_1,'String',{'get_field...'})
2415    %     set(handles.VelTypeMenu_1,'Visible','on')
2416    %     set(handles.VelType_text_1,'Visible','on');
2417    %     end
2418    %     view_FieldMenu_1(handles,'off')
2419    case 'netcdf'
2420    view_FieldMenu(handles,'on')
2421    set(handles.FieldMenu,'Value',1)
2422    set(handles.FieldMenu,'String',{'get_field...'})
2423    set(handles.FieldTransform,'Visible','off')
2424    %     view_TRANSFORM(handles,'off')
2425    case {'image','multimage','video'}
2426    view_FieldMenu(handles,'off')
2427    view_FieldMenu_1(handles,'off')
2428    set(handles.VelTypeMenu,'Visible','off')
2429    set(handles.VelType_text,'Visible','off');
2430end
2431
2432
2433%TODO:update
2434% if ~isequal(FileExt,'.nc') && ~isequal(FileExt,'.cdf') && ~testima
2435%     msgbox_uvmat('ERROR',['invalid input file extension ' FileExt])
2436%     return
2437% end 
2438
2439%%  read image documentation file  if found%%%%%%%%%%%%%%%%%%%%%%%%%%%
2440ext_imadoc='';
2441FileBase=fullfile(RootPath,RootFile);
2442if isequal(FileExt,'.xml')||isequal(FileExt,'.civ')
2443    ext_imadoc=FileExt;
2444elseif exist([FileBase '.xml'],'file')
2445    ext_imadoc='.xml';
2446elseif exist([FileBase '.civ'],'file')
2447    ext_imadoc='.civ';
2448end
2449%read the ImaDoc file
2450XmlData=[];
2451NbSlice_calib={};
2452if isequal(ext_imadoc,'.xml')
2453        [XmlData,warntext]=imadoc2struct([FileBase '.xml']);
2454        if isfield(XmlData,'Heading') && isfield(XmlData.Heading,'ImageName') && ischar(XmlData.Heading.ImageName)
2455            [PP,FF,ext_ima_read]=fileparts(XmlData.Heading.ImageName);
2456        end
2457        if isfield(XmlData,'Time')
2458            time{iview}=XmlData.Time;
2459        end
2460        if isfield(XmlData,'Camera')
2461            if isfield(XmlData.Camera,'NbSlice')&& ~isempty(XmlData.Camera.NbSlice)
2462                NbSlice_calib{iview}=XmlData.Camera.NbSlice;% Nbre of slices for Zindex in phys transform
2463                if ~isequal(NbSlice_calib{iview},NbSlice_calib{1})
2464                    msgbox_uvmat('WARNING','inconsistent number of Z indices for the two field series');
2465                end
2466            end
2467            if isfield(XmlData.Camera,'TimeUnit')&& ~isempty(XmlData.Camera.TimeUnit)
2468                TimeUnit=XmlData.Camera.TimeUnit;
2469            end
2470        end
2471        if ~isempty(warntext)
2472            msgbox_uvmat('WARNING',warntext)
2473        end 
2474elseif isequal(ext_imadoc,'.civ')
2475    [error,XmlData.Time,TimeUnit,mode,npx,npy,pxcmx,pxcmy]=read_imatext([FileBase '.civ']);
2476    time{iview}=XmlData.Time;
2477    if error==2, warntext=['no file ' FileBase '.civ'];
2478    elseif error==1, warntext='inconsistent number of fields in the .civ file';
2479    end 
2480end
2481
2482%% update time table
2483TimeTable=get(handles.TimeTable,'Data')
2484TimeTable{iview,1}=time(MinIndex_i,MinIndex_j);
2485TimeTable{iview,4}=time(MaxIndex_i,MaxIndex_j);
2486set(handles.TimeTable,'Data',TimeTable)
2487
2488%% number of slices
2489if isfield(XmlData,'GeometryCalib') && isfield(XmlData.GeometryCalib,'SliceCoord')
2490       siz=size(XmlData.GeometryCalib.SliceCoord);
2491       if siz(1)>1
2492           NbSlice=siz(1);
2493       else
2494           NbSlice=1;
2495       end
2496       set(handles.num_NbSlice,'String',num2str(NbSlice))
2497end
2498% set(handles.mode,'Visible','off') % do not show index pairstring by default
2499set(handles.PairString,'Visible','off')
2500% set(handles.num_ref_i,'Visible','off')
2501% set(handles.ref_i_text,'Visible','off')
2502testpair=0;
2503%set the menus of image pairstring and default selection for series
2504%list pairstring if relevant
2505% Val=get(handles.NomType,'Value');
2506% synchronise_view(handles,Val)
2507
2508% if ~isfield(SeriesData,'j1_series')||isempty(SeriesData.j1_series{index})
2509%     state_j='off'; %no need for j index
2510% else
2511%     state_j='on'; %case of j index
2512% end
2513% show index pairstring if files exist
2514set(handles.series,'UserData',SeriesData)
2515
2516
2517
2518% --- Executes on button press in BATCH.
2519function BATCH_Callback(hObject, eventdata, handles)
2520% hObject    handle to BATCH (see GCBO)
2521% eventdata  reserved - to be defined in a future version of MATLAB
2522% handles    structure with handles and user data (see GUIDATA)
2523Series=read_GUI(handles.series);
2524t=struct2xml(Series);
2525save(t); %TODO: determine a xml file name
2526
2527% list_action=get(handles.ACTION,'String');% list menu action
2528% index_action=get(handles.ACTION,'Value');% selected string index
2529% action= list_action{index_action}; % selected string
2530
2531%% defining the ACTION function handle
2532path_series=which('series');
2533list_path=get(handles.ACTION,'UserData');
2534index=get(handles.ACTION,'Value');
2535fct_path=list_path{index}; %path stored for the function ACTION
2536if ~isequal(fct_path,path_series)
2537    eval(['spath=which(''' action ''');']) %spath = current path of the selected function ACTION
2538    if ~exist(fct_path,'dir')
2539        msgbox_uvmat('ERROR',['The prescibed function path ' fct_path ' does not exist'])
2540        return
2541    end
2542    if ~isequal(spath,fct_path)
2543        addpath(fct_path)% add the prescribed path if not the current one
2544    end
2545end
2546eval(['h_fun=@' action ';'])%create a function handle for ACTION
2547if ~isequal(fct_path,path_series)
2548        rmpath(fct_path)% add the prescribed path if not the current one   
2549end
2550
2551h_fun('BATCH');% TODO modify the called function to read the xml file as input parameter
Note: See TracBrowser for help on using the repository browser.