source: trunk/src/civ.m @ 274

Last change on this file since 274 was 274, checked in by sommeria, 12 years ago

further bug repair with renovated civ GUI

File size: 213.3 KB
Line 
1%'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)
2%------------------------------------------------------------------------
3%  provides an interface for the software CIVx
4% function varargout = civ(varargin)
5% provides an interface for the software CIVx
6%
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%     This file is part of the toolbox UVMAT.
11%
12%     UVMAT is free software; you can redistribute it and/or modify
13%     it under the terms of the GNU General Public License as published by
14%     the Free Software Foundation; either version 2 of the License, or
15%     (at your option) any later version.
16%
17%     UVMAT is distributed in the hope that it will be useful,
18%     but WITHOUT ANY WARRANTY; without even the implied warranty of
19%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
21%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
22function varargout = civ(varargin)
23%TODO: search range
24
25% Last Modified by GUIDE v2.5 17-Nov-2011 23:05:55
26% Begin initialization code - DO NOT EDIT
27gui_Singleton = 1;
28gui_State = struct('gui_Name',       mfilename, ...
29    'gui_Singleton',  gui_Singleton, ...
30    'gui_OpeningFcn', @civ_OpeningFcn, ...
31    'gui_OutputFcn',  @civ_OutputFcn, ...
32    'gui_LayoutFcn',  [] , ...
33    'gui_Callback',   []);
34if nargin && ischar(varargin{1})
35    gui_State.gui_Callback = str2func(varargin{1});
36end
37
38if nargout
39    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
40else
41    gui_mainfcn(gui_State, varargin{:});
42end
43% End initialization code - DO NOT EDIT
44
45%------------------------------------------------------------------------
46% --- Executes just before civ is made visible.
47function civ_OpeningFcn(hObject, eventdata, handles, param)
48%------------------------------------------------------------------------
49% This function has no output args, see OutputFcn.
50global patch_newBin %=1 if new patch processing available
51%filebase: root name
52%nom_type: nomencalture used ('png_old','_i_j'...)
53%list of field numbers to process
54%subdir: subdirectory of the opened netcdf file
55%ind_opening: operation number advised for beginning (1=civ1,2=fix1,3=patch1,4=civ2,5=fix2,6=patch2),
56%ind_a_opening ind_b_opening chosen pair from the opened netcdf file
57% Choose default command line output for civ
58handles.output = hObject;
59% Update handles structure
60guidata(hObject, handles);
61set(hObject,'WindowButtonDownFcn',{'mouse_alt_gui',handles}) % allows mouse action with right button (zoom for uicontrol display)
62%default initial parameters
63filebase=''; % root file name ('filebase'.civ)
64ext=[];
65
66%% read names of the .exe file to adjust the interface according to available binaries
67path_uvmat=which('uvmat');% check the path detected for source file uvmat
68path_UVMAT=fileparts(path_uvmat); %path to UVMAT
69errormsg=[];%default error message
70xmlfile='PARAM.xml';
71if exist(xmlfile,'file')
72    try
73        t=xmltree(xmlfile);
74        sparam=convert(t);
75    catch
76        errormsg={' Unable to read the file PARAM.xml defining the civx binaries:'; lasterr};
77        return
78    end
79else
80    errormsg=[xmlfile ' not found: path to civx binaries undefined'];
81    return
82end
83if ~isempty(errormsg)
84    msgbox_uvmat('ERROR',errormsg);
85end
86
87test_batch=0;%default: ,no batch mode available
88if isfield(sparam,'BatchParam') && isfield(sparam.BatchParam,'BatchMode')
89    test_batch=strcmp(sparam.BatchParam.BatchMode,'sge'); %sge is currently the only implemented batch mod
90end
91if test_batch==0
92    set(handles.BATCH,'Enable','off')% put the BATCH button in grey (unactivated)
93    set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])% put the BATCH button in grey (unactivated)
94end
95if isfield(sparam.RunParam,'CivBin')
96    if ~exist(sparam.RunParam.CivBin,'file')
97        sparam.RunParam.CivBin=fullfile(path_UVMAT,sparam.RunParam.CivBin);
98    end
99else
100    sparam.RunParam.CivBin='';
101end
102
103%% load the list of previously browsed files in menu Open
104 dir_perso=prefdir; % path to the directory .matlab for personal data
105 profil_perso=fullfile(dir_perso,'uvmat_perso.mat');% personal data file uvmauvmat_perso.mat' in .matlab
106 if exist(profil_perso,'file')
107      h=load (profil_perso);
108      if isfield(h,'MenuFile')
109          set(handles.MenuFile_1,'Label',h.MenuFile{1})
110          %set(handles.MenuFile_1,'Label',h.MenuFile_1);
111      end
112%       if isfield(h,'MenuFile_2')
113%           set(handles.MenuFile_2,'Label',h.MenuFile_2);
114%       end
115%       if isfield(h,'MenuFile_3')
116%           set(handles.MenuFile_3,'Label',h.MenuFile_3);
117%       end
118%       if isfield(h,'MenuFile_4')
119%           set(handles.MenuFile_4,'Label',h.MenuFile_4);
120%       end
121%       if isfield(h,'MenuFile_5')
122%           set(handles.MenuFile_5,'Label',h.MenuFile_5);
123%       end
124end
125
126%default input parameters:
127num_i1=1; % set of field i numbers
128num_i2=1; % set of field i numbers
129num_j1=1; % set of field j numbers (fields a)
130num_j2=1; % second set of field j numbers (fields b)
131%subdir='A'; % subdir for the netcdf result files
132ind_opening=0; % proposed operation number (1=civ1,2=fix1,3=patch1,4=civ2,5=fix2,6=patch2)
133%load the initial parameters if the interface is started from uvmat
134if exist('param','var')&&isstruct(param)% the interface is opened from uvmat
135    filebase=param.RootName;
136    nom_type_read=param.NomType;
137    num_i1=param.num1;
138    if isnan(num_i1),num_i1=1;end
139    num_i2=param.num2;
140    if isnan(num_i2),num_i2=num_i1;end
141    num_j1=param.num_a;
142    if isnan(num_j1),num_j1=1;end
143    num_j2=param.num_b;
144    if isnan(num_j2),num_j2=num_j1;end
145    subdir=param.SubDir;
146    ind_opening=param.IndOpening;
147    ext=param.ImaExt;
148end
149browse.num_i1=num_i1;
150browse.num_i2=num_i2;
151browse.num_j1=num_j1;
152browse.num_j2=num_j2;
153if ~isempty(ext) && (~isempty(imformats(ext(2:end)))||strcmpi(ext,'.avi'));%if an image file has been opened by uvmat
154    set(handles.ImaExt,'String',ext)
155    browse.ext_ima=ext;
156    if exist('nom_type_read','var')
157        browse.nom_type_ima=nom_type_read; % the image nomenclature is stored
158    end
159elseif isequal(ext,'.nc')
160    if exist('nom_type_read','var')
161        browse.nom_type_nc=nom_type_read;% the netcdf  nomenclature is stored
162    end
163end
164set(handles.RootName,'String',filebase);
165set(handles.ImaDoc,'String',ext)
166
167% patch_newBin=exist(sparam.RunParam.CivBin,'file');
168% set(handles.subdir_civ1,'String',subdir)%default subdir on which uvmat was working
169% set(handles.subdir_civ2,'String',subdir)%default subdir on which uvmat was working
170
171%initiate advised operations
172% if isemp(ind_opening,[])
173%     ind_opening=1; % default
174% end
175% set default operation options
176enable_civ1(handles,0)
177enable_civ2(handles,0)
178%enable_pair1(handles,'on')
179enable_fix1(handles,0)
180enable_patch1(handles,0)
181enable_fix2(handles,0)
182enable_patch2(handles,0)
183set(handles.CIV1,'Value',0)
184set(handles.FIX1,'Value',0)
185set(handles.PATCH1,'Value',0)
186set(handles.CIV2,'Value',0)
187set(handles.FIX2,'Value',0)
188set(handles.PATCH2,'Value',0)
189%set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
190if isequal(ind_opening,1)
191    set(handles.CIV1,'Value',1)
192    enable_civ1(handles,1)
193elseif isequal(ind_opening,2)
194    set(handles.FIX1,'Value',1)
195    enable_fix1(handles,1)
196elseif isequal(ind_opening,3)
197    set(handles.PATCH1,'Value',1)
198    enable_patch1(handles,1)
199elseif isequal(ind_opening,4)
200    set(handles.CIV2,'Value',1)
201    enable_civ2(handles,1)
202elseif isequal(ind_opening,5)
203    set(handles.FIX2,'Value',1)
204    enable_fix2(handles,1)
205    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
206    set(handles.list_pair_civ2,'Enable','On')
207    set(handles.list_pair_civ2,'Enable','On')
208    enable_pair1(handles,'off')
209elseif isequal(ind_opening,6)
210    set(handles.PATCH2,'Value',1)
211    enable_patch2(handles,1)
212    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
213    set(handles.list_pair_civ2,'Enable','On')
214    enable_pair1(handles,'off')
215end
216
217% set the range of fields (1:1 by default) and selected pair
218if isequal(num_i2,num_i1)
219    num_ref_i=num_i1;
220else
221    num_ref_i=floor((num_i1+num_i2)/2);
222    browse.incr_pair(1)=num_i2-num_i1;
223    browse.incr_pair(2)=0;
224end
225if isequal(num_j1,num_j2)
226    if isnan(num_j1)
227        num_ref_j=1;
228    else
229        num_ref_j=num_j1;
230    end
231else
232    num_ref_j=floor((num_j1+num_j2)/2);
233    browse.incr_pair(2)=num_j2-num_j1;
234end
235set(handles.first_i,'String',num2str(num_ref_i));
236set(handles.last_i,'String',num2str(num_ref_i));
237set(handles.first_j,'String',num2str(num_ref_j));
238set(handles.last_j,'String',num2str(num_ref_j));
239set(handles.ref_i,'String',num2str(num_ref_i));
240set(handles.ref_j,'String',num2str(num_ref_j));
241% set(handles.ref_i_civ2,'String',num2str(num_ref_i));
242% set(handles.ref_j_civ2,'String',num2str(num_ref_j));
243set(handles.RootName,'UserData',browse);
244if exist('param','var') && isfield(param,'RootName') && ~isempty(param.RootName)%varargin the interface is opened from uvmat
245    RootName_Callback(hObject, eventdata, handles);
246end
247
248
249%TESTS
250
251
252struct=read_panel(handles.panel_Patch2)
253
254%------------------------------------------------------------------------
255% --- Outputs from this function are returned to the command line.
256function varargout = civ_OutputFcn(hObject, eventdata, handles)
257%------------------------------------------------------------------------
258% varargout  cell array for returning output args (see VARARGOUT);
259% hObject    handle to figure
260% eventdata  reserved - to be defined in a future version of MATLAB
261% handles    structure with handles and user data (see GUIDATA)
262% Get default command line output from handles structure
263varargout{1} = handles.output;
264
265% --------------------------------------------------------------------
266function MenuBrowse_Callback(hObject, eventdata, handles)
267
268%get the input file properties
269filebase=get(handles.RootName,'String');
270oldfile=''; %default
271if isempty(filebase)|| isequal(filebase,'')%loads the previously stored file name and set it as default in the file_input box
272    dir_perso=prefdir;
273    profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
274    if exist(profil_perso,'file')
275        h=load (profil_perso);
276        if isfield(h,'filebase')&& ischar(h.filebase)
277            oldfile=h.filebase;
278        end
279        if isfield(h,'RootPath') && ischar(h.RootPath)
280            oldfile=h.RootPath;
281        end
282    end
283else
284    oldfile=filebase;
285end
286ind_opening=1;%default
287browse.incr_pair=[0 0]; %default
288menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
289       '*.xml',  '.xml files '; ...
290        '*.civ',  '.civ files '; ...
291        '*.png','.png image files'; ...
292        '*.jpg',' jpeg image files'; ...
293        '*.tif','.tif image files'; ...
294        '*.avi;*.AVI','.avi movie files'; ...
295        '*.nc','.netcdf files'; ...
296        '*.*',  'All Files (*.*)'};
297[FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
298fileinput=[PathName FileName];%complete file name
299sizf=size(fileinput);
300if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
301[path,name,ext]=fileparts(fileinput);
302testeditxml=0;
303if isequal(ext,'.xml')
304    testeditxml=1;
305    t_browse=xmltree(fileinput);
306    head_element=get(t_browse,1);
307    if isfield(head_element,'name')&& isequal(head_element.name,'ImaDoc')
308        testeditxml=0;
309    end
310end
311if testeditxml==1 || isequal(ext,'.xls')
312    heditxml=editxml({fileinput});
313    set(heditxml,'Tag','browser')
314    waitfor(heditxml,'Tag','idle')
315    if ~ishandle(heditxml)
316        return
317    end
318    attr=findobj(get(heditxml,'children'),'Tag','CurrentAttributes');
319    set(handles.browse,'UserData',fileinput)% store for future opening with browser
320    fileinput=get(attr,'UserData');
321    if ~exist(fileinput,'file')
322        return
323    end
324end
325[RootPath,RootFile,str1,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileinput);
326filebase=fullfile(RootPath,RootFile);
327num_i1=str2double(str1);
328if isnan(num_i1),num_i1=1;end
329num_i2=str2double(str2);
330if isnan(num_i2),num_i2=num_i1;end
331num_j1=stra2num(str_a);
332if isnan(num_j1),num_j1=1;end
333num_j2=stra2num(str_b);
334if isnan(num_j2),num_j2=num_j1;end
335if isequal(get(handles.compare,'Value'),1)
336    browse=[];%initialisation
337else
338    browse=get(handlesRootName,'UserData');
339end
340browse.num_i1=num_i1;
341browse.num_i2=num_i2;
342browse.num_j1=num_j1;
343browse.num_j2=num_j2;
344if length(ext)>1 && (~isempty(imformats(ext(2:end)))||strcmpi(ext,'.avi'));%if an image file has been opened by uvmat
345    browse.nom_type_ima=nom_type;
346    browse.ext_ima=ext;
347    set(handles.ImaExt,'String',ext)
348end
349set(handles.ImaDoc,'String',ext);
350
351%%%%% read the state of the selected netcdf file to advise default operation
352if isequal(ext,'.nc')
353    browse.nom_type_nc=nom_type;
354    ind_opening=2;% propose 'fix' as the default option
355    Data=nc2struct(fileinput,'ListGlobalAttribute','CivStage','absolut_time_T0','fix','patch','civ2','fix2');
356    if ~isempty(Data.CivStage)%test for civ files
357        ind_opening=Data.CivStage;
358        set(handles.CivMode,'Value',3)
359    end
360    if ~isempty(Data.absolut_time_T0)%test for civx files
361        set(handles.CivMode,'Value',1)
362        if isfield(Data,'fix') && isequal(Data.fix,1)
363            ind_opening=3;
364        end
365        if isequal(Data.patch,1)
366            ind_opening=4;
367        end
368        if isequal(Data.civ2,1)
369            ind_opening=5;
370        end
371        if  isequal(Data.fix2,1)
372            ind_opening=6;
373        end
374        testciv=1; %TO SUPPRESS WITH NEW VERSION OF CIVX
375    else
376        ind_opening=3; %GUI used only for patch
377        testciv=0;
378    end
379    set(handles.subdir_civ1,'String',subdir);%set the default subdir directories for installing the .nc results
380    set(handles.subdir_civ2,'String',subdir);
381    browse.testciv=testciv;
382    browse.ind_opening=ind_opening;
383end
384set(handles.RootName,'String',filebase);
385set(handles.ImaDoc,'String',ext);
386if ~isempty(num_i1)
387    ref_i=num_i1;
388    if ~isempty(num_i2)
389        ref_i=floor((ref_i+num_i2)/2);% reference image number corresponding to the file
390        browse.incr_pair(1)=num_i2-num_i1;
391        browse.incr_pair(2)=0;
392    end
393    set(handles.first_i,'String',num2str(ref_i));
394    set(handles.last_i,'String',num2str(ref_i));
395    set(handles.ref_i,'String',num2str(ref_i));
396%     set(handles.ref_i_civ2,'String',num2str(ref_i))
397end
398if isempty(num_j1)
399    set(handles.ref_j,'String','1');
400    set(handles.ref_j_civ2,'String','1');
401else
402    ref_j=num_j1;
403    if ~isempty(num_j2)
404        ref_j=floor((num_j1+num_j2)/2);
405        browse.incr_pair(2)=num_j2-num_j1;
406    end
407    set(handles.first_j,'String',num2str(ref_j));
408    set(handles.last_j,'String',num2str(ref_j));
409    set(handles.ref_j,'String',num2str(ref_j));
410%     set(handles.ref_j_civ2,'String',num2str(ref_j));
411end
412
413% set default operation options
414enable_civ1(handles,0)
415enable_civ2(handles,0')
416enable_pair1(handles,'on')
417enable_fix1(handles,0)
418enable_patch1(handles,0)
419enable_fix2(handles,0)
420enable_patch2(handles,0)
421set(handles.CIV1,'Value',0)
422set(handles.FIX1,'Value',0)
423set(handles.PATCH1,'Value',0)
424set(handles.CIV2,'Value',0)
425set(handles.FIX2,'Value',0)
426set(handles.PATCH2,'Value',0)
427% set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
428if isequal(ind_opening,1)
429    set(handles.CIV1,'Value',1)
430    enable_civ1(handles,'on')
431elseif isequal(ind_opening,2)
432    set(handles.FIX1,'Value',1)
433    enable_fix1(handles,'on')
434elseif isequal(ind_opening,3)
435    set(handles.PATCH1,'Value',1)
436    enable_patch1(handles)
437elseif isequal(ind_opening,4)
438    set(handles.CIV2,'Value',1)
439    enable_civ2(handles,'on')
440elseif isequal(ind_opening,5)
441    enable_pair1(handles,'off')
442    set(handles.FIX2,'Value',1)
443    enable_fix2(handles)
444    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
445    set(handles.list_pair_civ2,'Enable','On')
446    set(handles.list_pair_civ2,'Enable','On')
447elseif isequal(ind_opening,6)
448    enable_pair1(handles,'off')
449    set(handles.PATCH2,'Value',1)
450    enable_patch2(handles)
451    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
452    set(handles.list_pair_civ2,'Enable','On')
453end
454set(handles.RootName,'UserData',browse);% store information from browser
455RootName_Callback(hObject, eventdata, handles);
456
457
458% -----------------------------------------------------------------------
459% --- Open again the file whose name has been recorded in MenuFile_1
460function MenuFile_1_Callback(hObject, eventdata, handles)
461%------------------------------------------------------------------------
462fileinput=get(handles.MenuFile_1,'Label');
463display_file_name(hObject, eventdata, handles,fileinput)
464
465% -----------------------------------------------------------------------
466% --- Open again the file whose name has been recorded in MenuFile_2
467function MenuFile_2_Callback(hObject, eventdata, handles)
468%------------------------------------------------------------------------
469fileinput=get(handles.MenuFile_2,'Label');
470display_file_name(hObject, eventdata, handles,fileinput)
471
472% -----------------------------------------------------------------------
473% --- Open again the file whose name has been recorded in MenuFile_3
474function MenuFile_3_Callback(hObject, eventdata, handles)
475%------------------------------------------------------------------------
476fileinput=get(handles.MenuFile_3,'Label');
477display_file_name(hObject, eventdata, handles,fileinput)
478
479% -----------------------------------------------------------------------
480% --- Open again the file whose name has been recorded in MenuFile_4
481function MenuFile_4_Callback(hObject, eventdata, handles)
482%------------------------------------------------------------------------
483fileinput=get(handles.MenuFile_4,'Label');
484display_file_name(hObject, eventdata, handles,fileinput)
485
486% -----------------------------------------------------------------------
487% --- Open again the file whose name has been recorded in MenuFile_5
488function MenuFile_5_Callback(hObject, eventdata, handles)
489%------------------------------------------------------------------------
490fileinput=get(handles.MenuFile_5,'Label');
491display_file_name(hObject, eventdata, handles,fileinput)
492
493%------------------------------------------------------------------------
494function ImaDoc_Callback(hObject, eventdata, handles)
495%------------------------------------------------------------------------
496RootName_Callback(hObject, eventdata, handles)
497
498%------------------------------------------------------------------------
499% --- function activated when a new filebase (image series) is introduced
500function RootName_Callback(hObject, eventdata, handles)
501%------------------------------------------------------------------------
502filebase=get(handles.RootName,'String');
503display_file_name(hObject, eventdata, handles,filebase)
504
505%------------------------------------------------------------------------
506function display_file_name(hObject, eventdata, handles,filebase)
507%------------------------------------------------------------------------
508set(handles.compare,'Visible','on')
509ext_ima='';%default
510nom_type_ima=[];%default
511field_count=1;%default
512nom_type_nc=[];
513time=[];
514TimeUnit='frame'; %default
515CoordUnit='px';%default
516pxcmx_search=[];%default
517pxcmy_search=[];%default
518
519ext_imadoc=get(handles.ImaDoc,'String');
520browse=get(handles.RootName,'UserData');%default
521if isfield(browse,'nom_type_ima')
522    nom_type_ima=browse.nom_type_ima;% get an image nomenclature type already determined by an input image name
523end
524if isfield(browse,'ext_ima')
525    ext_ima=browse.ext_ima;
526end
527if isfield(browse,'nom_type_nc')
528    nom_type_nc=browse.nom_type_nc;% get an image nomenclature type already determined by an input image name
529end
530if isfield(browse,'num_i1')
531    field_count=browse.num_i1;% get an image index type already determined by an input file
532end
533set(handles.civ,'UserData',[]); %refresh list of previous civ files (for STATUS)
534
535%default first_i and j and increments
536first_i=str2double(get(handles.first_i,'String'));%value possibly set by uvmat_Opening
537if isnan(first_i)|| first_i < 1
538    first_i=1; %default first_i
539end
540last_i=str2double(get(handles.last_i,'String'));
541if isnan(last_i)|| last_i < first_i
542    last_i=first_i;  %default last_i
543end
544first_j=str2double(get(handles.first_j,'String'));
545if isnan(first_j)|| first_j < 1
546    first_j=1; %default first_j
547end
548last_j=str2double(get(handles.last_j,'String'));
549if isnan(last_j)|| last_j < first_j
550    last_j=first_j; %default last_j
551end
552incr_i=str2double(get(handles.incr_i,'String'));
553if isnan(incr_i) || incr_i < 1;
554    set(handles.incr_i,'String','1') %default incr_i
555end
556incr_j=str2double(get(handles.incr_j,'String'));
557if isnan(incr_j) || incr_j < 1;
558    set(handles.incr_j,'String','1') %default incr_j
559end
560dt=[];%default
561testmode=0;%default
562nbfield=[]; %default
563nburst=[];%default
564pxcmx=1;
565pxcmy=1;
566
567%look for an image documentation file
568if ~strcmp(ext_imadoc,'.xml') && ~strcmp(ext_imadoc,'.civ')&& ~strcmpi(ext_imadoc,'.avi')
569    if exist([filebase '.xml'],'file')
570        ext_imadoc='.xml';
571    elseif exist([filebase '.civxml'],'file')
572        ext_imadoc='.civxml';
573    elseif exist([filebase '.civ'],'file')
574        ext_imadoc='.civ';
575    elseif exist([filebase '.avi'],'file')
576        ext_imadoc='.avi';
577    elseif exist([filebase '.AVI'],'file')
578        ext_imadoc='.AVI';
579    end
580    set(handles.ImaDoc,'String',ext_imadoc)
581end
582
583%%%%%%%%   read image documentation file  %%%%%%%%%%%%%%%%%%%%%%%%%%%
584mode=''; %default
585set(handles.ImaDoc,'BackgroundColor',[1 1 0])
586drawnow
587if isequal(ext_imadoc,'.civxml') || isequal(ext_imadoc,'.xml')|| isequal(ext_imadoc,'.civ')
588    set(handles.ref_i,'Visible','On')%use a reference index
589    set(handles.ref_j,'Visible','On')
590elseif isequal(ext_imadoc,'.avi') || isequal(ext_imadoc,'.AVI')
591    set(handles.ref_j,'Visible','Off')
592else
593    set(handles.ref_i,'Visible','Off')
594    set(handles.ref_j,'Visible','Off')
595end
596testima_xml=0;
597if isequal(ext_imadoc,'.civxml')%TO ABANDON
598    [nbfield,nbfield2,time]=read_civxml([filebase '.civxml']);
599    mode='pair j1-j2';
600    if isempty(nom_type_ima)% dtermine types by default if not already selected by browser or uvmat
601        nom_type_ima='_i_j';
602    end
603elseif isequal(ext_imadoc,'.xml')
604    [XmlData,warntext]=imadoc2struct([filebase '.xml']);
605    ext_ima_read=[];
606    nom_type_read=[];
607    if isfield(XmlData,'Heading')&&isfield(XmlData.Heading','ImageName')&&ischar(XmlData.Heading.ImageName)% get image nom type and extension from the xml file
608        [PP,FF,fc,str2,str_a,str_b,ext_ima_read,nom_type_read]=name2display(XmlData.Heading.ImageName);
609        fullname=fullfile(fileparts(filebase),XmlData.Heading.ImageName); %full name (including path) of the first image defined by the xmle file,
610        if ~exist(fullname,'file')
611            msgbox_uvmat('WARNING',['FirstImage ' fullname ' defined in the xml file does not exist'])
612        end
613    end
614    if isfield(XmlData,'Time')
615        time=XmlData.Time;
616        nbfield=size(time,1);
617        nbfield2=size(time,2);
618        %transform .Time to a column vector if it is a line vector thenomenclature uses a single index: correct possible bug in xml
619        if isequal(nbfield,1) && ~isequal(nbfield2,1)% .Time is a line vector
620            if numel(nom_type_read)>=2 && isempty(regexp(nom_type_read(2:end),'\D'))
621                time=time';
622                nbfield=nbfield2;
623                nbfield2=1;
624            end
625        end
626    end
627    if isfield(XmlData,'TimeUnit')
628        TimeUnit=XmlData.TimeUnit;
629    end
630    if isfield(XmlData,'Npx')
631        npx=XmlData.Npx;
632        npy=XmlData.Npy;
633    end
634    pxcmx_search=1;
635    pxcmy_search=1;
636    if isfield(XmlData,'GeometryCalib')
637        tsai=XmlData.GeometryCalib;
638        if isfield(tsai,'f') && isfield(tsai,'Tz') && isfield(tsai,'dpx') && isfield(tsai,'dpy')&& isfield(tsai,'R')
639            rot2D=tsai.R(1:2,[1,2]);
640            pxcmx_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpx);
641            pxcmy_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpy);
642        end
643        if isfield(tsai,'CoordUnit')
644            CoordUnit=tsai.CoordUnit;
645        end
646    end
647elseif strcmp(ext_imadoc,'.civ')% case of .civ image documentation file
648    [error,time,TimeUnit,mode,npx,npy]=read_imatext([filebase '.civ']);
649    if error==2, msgbox_uvmat('WARNING',['no file ' filebase '.civ']);
650    elseif error==1, msgbox_uvmat('WARNING','inconsistent number of fields in the .civ file');
651    end
652    nom_type_ima='001a';
653elseif strcmpi(ext_imadoc,'.avi')
654    nom_type_ima='*';
655    ext_ima=ext_imadoc;
656    set(handles.CivMode,'Value',1);
657    set(handles.CivMode,'String',{'series(Di)'})
658    dt=0.04;%default
659    if exist([filebase ext_imadoc],'file')==2
660        info=aviinfo([filebase ext_imadoc]);%read infos on the avi movie
661        dt=1/info.FramesPerSecond;%time interval between successive frames
662        nbfield=info.NumFrames;%number of frames
663    end
664    time=(dt*(0:nbfield-1))';%list of image times
665end
666if isempty(time)
667    set(handles.ImaDoc,'String',''); %xml file not used for timing
668end
669set(handles.ImaDoc,'BackgroundColor',[1 1 1])
670
671%get the imabe nomenclature type if not defined by the input file nor by the xml file
672dirima=[];%default
673if isempty(nom_type_ima)
674    %look for double image series '_i_j'
675    dirima=dir([filebase '_' num2str(first_i) '_' num2str(first_j) '.*']);
676    if isempty(dirima)
677        % look for images series  with sub marker '_'
678        dirima=dir([filebase '_*' num2str(first_i) '.*']);
679        if isempty(dirima)
680            % look for other images series
681            dirima=dir([filebase '*' num2str(first_i) '.*']);
682            if isempty(dirima)
683                % look for other images series witth letter appendix
684                appendix=char(96+first_j);
685                dirima=dir([filebase '*' num2str(first_i) appendix '.*']);
686            end
687        end
688    end
689end
690for ilist=1:numel(dirima)
691    [pp,ff,fc,str2,str_a,str_b,ext_list,nom_type_list]=name2display(dirima(ilist).name);
692    form=imformats(ext_list(2:end));
693    if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
694        ext_ima=ext_list;
695        nom_type_ima=nom_type_list;
696            break
697    end
698end
699% no image documentation file found: look for a series of existing files,images by priority or .nc files
700if isempty(nom_type_ima)
701    ext_search=ext_imadoc;
702    nom_type_search=nom_type_nc;
703else
704    ext_search=ext_ima;
705    nom_type_search=nom_type_ima;
706end
707if isempty(time) && ~strcmp(nom_type_search,'none') && ~strcmp(nom_type_search,'') && ~strcmp(nom_type_search,'*')
708    subdir=get(handles.subdir_civ1,'String');
709    incr_pair=[0 0];%default
710    if isfield(browse,'incr_pair')
711        incr_pair=browse.incr_pair;
712    end
713    %     nbdetect=0;%test of detected images
714    field_i=browse.num_i2;
715    imagename=name_generator(filebase,field_i,1,ext_search,nom_type_search);
716    imagename_plus='';
717    idetect=1;
718    while idetect %look for the maximum file number in the series
719        imagename_plus=name_generator(filebase,field_i+1,1,ext_search,nom_type_search);
720        idetect=(exist(imagename_plus,'file')==2)&& ~strcmp(imagename,imagename_plus);
721        if idetect
722            field_i=field_i+1;
723        end
724        %SEE CASE OF NETCDF FILES
725        %             nbdetect=nbdetect+(exist(imagename,'file')==2);
726    end
727    nbfield=field_i;% last detected field number
728    field_i=browse.num_i1;%look for the minimum file number in the series
729    imagename_min='';
730    idetect=1;
731    while idetect==1
732        imagename_min=name_generator(filebase,field_i-1,1,ext_search,nom_type_search);
733        idetect=(exist(imagename_min,'file')==2)&& ~strcmp(imagename,imagename_min);
734        if idetect
735            field_i=field_i-1;
736        end
737    end
738    first_i=max(field_i,1);
739    nom_type_search
740    if numel(regexp(nom_type_search,'\D'))>=1%two indices i and j
741        field_i=browse.num_i1;
742        field_j=browse.num_j2;
743        imagename=name_generator(filebase,field_i,field_j,ext_search,nom_type_search);
744        imagename_plus='';
745        jdetect=1;
746        while jdetect==1 %look for the maximum file number in the series
747            imagename_plus=name_generator(filebase,field_i,field_j+1,ext_search,nom_type_search);
748            jdetect=(exist(imagename_plus,'file')==2)&& ~strcmp(imagename,imagename_plus);
749            if jdetect
750                field_j=field_j+1;
751            end
752            %SEE CASE OF NETCDF FILES
753            %             nbdetect=nbdetect+(exist(imagename,'file')==2);
754        end
755        nbfield2=field_j;% last detected field number
756     end
757 
758    %determine the set of times and possible intervals for CIV
759    %   dt=(1/1000)*str2double(get(handles.dt,'String'));
760    time=(0:nbfield-1)';% time=file index -1  by default
761    if numel(regexp(nom_type_search,'\D'))>=1%two indices i and j
762        [x,y]=meshgrid(0:nbfield2-1,0:nbfield-1);
763        time=x+y;
764    end
765end
766
767if exist('time','var')
768    if size(time,1)+size(time,2)>=3 % if there are at least two time values to define dt
769        nbfield=size(time,1);
770        nbfield2=size(time,2);
771        set(handles.ImaDoc,'UserData',time); %store the set of times
772        set(handles.dt_unit,'String',['dt in m' TimeUnit]);
773%         set(handles.dt_unit_civ2,'String',['dt in m' TimeUnit]);
774        set(handles.TimeUnit,'String',TimeUnit);
775        set(handles.nb_field,'String',num2str(nbfield));
776        set(handles.nb_field2,'String',num2str(nbfield2));
777    end
778end
779set(handles.CoordUnit,'String',CoordUnit)
780set(handles.SearchRange,'UserData',[pxcmx_search pxcmy_search]);
781% npxy=[npy npx];
782set(handles.ImaExt,'String',ext_ima)
783set(handles.first_i,'String',num2str(first_i));
784set(handles.last_i,'String',num2str(last_i));%
785set(handles.first_j,'String',num2str(first_j));
786set(handles.last_j,'String',num2str(last_j));%
787browse.nom_type_ima=nom_type_ima;
788set(handles.RootName,'UserData',browse)% store the nomenclature type
789
790%%  set the menus of image pairs and default selection for civ   %%%%%%%%%%%%%%%%%%%
791test_ima_i=numel(nom_type_ima)>1 && isempty(regexp(nom_type_ima(2:end),'\D','once'));%images with single indexing
792if test_ima_i || isequal(nom_type_nc,'_i1-i2')||~(exist('nbfield2','var')&&(nbfield2~=1))
793    set(handles.CivMode,'Value',1)
794    set(handles.CivMode,'String',{'series(Di)'})   
795elseif (nbfield==1)% simple series in j
796    set(handles.CivMode,'Value',1)
797    set(handles.CivMode,'String',{'series(Dj)'})
798else
799    set(handles.CivMode,'String',{'pair j1-j2';'series(Dj)';'series(Di)'})%multiple choice
800    if nbfield2 <= 10
801        set(handles.CivMode,'Value',1)% advice 'pair j1-j2' for small burst
802    end
803end
804
805
806%% desable status and RUN button
807set(handles.RUN, 'Enable','On')
808set(handles.RUN,'BackgroundColor',[1 0 0])
809set(handles.BATCH,'Enable','On')
810set(handles.BATCH,'BackgroundColor',[1 0 0])
811if isfield(handles,'status')
812set(handles.status,'Value',0);%suppress status display
813status_Callback(hObject, eventdata, handles)
814end
815
816%% store the root input filename for future opening
817dir_perso=prefdir;
818profil_perso=fullfile(prefdir,'uvmat_perso.mat');
819RootPath=fileparts(filebase);
820if exist(profil_perso,'file')
821    save (profil_perso,'RootPath','-append'); %store the root name for future opening of uvmat
822else
823    txt=ver('MATLAB');
824    Release=txt.Release;
825    relnumb=str2double(Release(3:4));
826    if relnumb >= 14
827        save (profil_perso,'RootPath','-V6'); %store the root name for future opening of uvmat
828    else
829        save (profil_perso,'RootPath'); %store the root name for future opening of uvmat
830    end
831end
832
833%% update the subdir
834pathdir=fileparts(filebase);%path to the current xml file
835listot=dir(pathdir);
836idir=0;
837listdir={''};%default
838for ilist=1:length(listot)
839    if listot(ilist).isdir
840        name=listot(ilist).name;
841        if ~isequal(name,'.') && ~isequal(name,'..')
842            idir=idir+1;
843            listdir{idir,1}=listot(ilist).name;
844        end
845    end
846end
847set(handles.list_subdir_civ1,'Value',1)
848set(handles.list_subdir_civ2,'Value',1)
849set(handles.list_subdir_civ1,'String',[listdir;'new...'])
850set(handles.list_subdir_civ2,'String',[listdir;'new...'])
851if isempty(listdir)
852    dirname=listdir{1};
853else
854    dirname='CIV'; %default civ directory name
855end
856set(handles.subdir_civ1,'String',dirname)
857set(handles.subdir_civ2,'String',dirname)
858%check wether the current subdir exists:
859% subdir_civ1=get(handles.subdir_civ1,'String');
860% subdir_civ2=get(handles.subdir_civ2,'String');
861
862CivMode_Callback(hObject, eventdata, handles)
863
864%------------------------------------------------------------------------
865% --- Executes on button press in CivMode.
866function CivMode_Callback(hObject, eventdata, handles)
867%------------------------------------------------------------------------
868browse=get(handles.RootName,'UserData');
869compare_list=get(handles.compare,'String');
870val=get(handles.compare,'Value');
871compare=compare_list{val};
872if strcmp(compare,'displacement')
873    mode='displacement';
874else
875    mode_list=get(handles.CivMode,'String');
876    if ischar(mode_list)
877        mode_list={mode_list};
878    end
879    mode_value=get(handles.CivMode,'Value');
880    mode=mode_list{mode_value};
881end
882displ_num=[];%default
883ref_i=str2double(get(handles.ref_i,'String'));
884% last_i=str2num(get(handles.last_i,'String'));
885time=get(handles.ImaDoc,'UserData'); %get the set of times
886siztime=size(time);
887nbfield=siztime(1);
888nbfield2=siztime(2);
889indchosen=1;  %%first pair selected by default
890if isequal(mode,'pair j1-j2')%| isequal(mode,'st_pair j1-j2')
891    dt=1;
892    displ='';
893    index=0;
894    numlist_a=[];
895    numlist_B=[];
896    %get all the time intervals in bursts
897    displ_dt=1;%default
898    nbfield2=min(nbfield2,10);%limitate the number of pairs to 10x10
899    for numod_a=1:nbfield2-1 %nbfield2 always >=2 for 'pair j1-j2' mode
900        for numod_b=(numod_a+1):nbfield2
901            index=index+1;
902            numlist_a(index)=numod_a;
903            numlist_b(index)=numod_b;
904            if ~isempty(time)
905                dt(numod_a,numod_b)=time(ref_i,numod_b)-time(ref_i,numod_a);%first time interval dt
906                displ_dt(index)=dt(numod_a,numod_b);
907            else
908                displ_dt(index)=1;
909            end
910        end
911    end
912    [dtsort,indsort]=sort(displ_dt);
913    if ~isempty(numlist_a)
914        displ_num(1,:)=numlist_a(indsort);
915        displ_num(2,:)=numlist_b(indsort);
916    end
917    displ_num(3,:)=0;
918    displ_num(4,:)=0;
919    set(handles.jtext,'Visible','Off')
920    set(handles.first_j,'Visible','Off')
921    set(handles.last_j,'Visible','Off')
922    set(handles.incr_j,'Visible','Off')
923    set(handles.nb_field2,'Visible','Off')
924    set(handles.ref_j,'Visible','Off')
925elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)')
926    for index=1:min(nbfield2-1,200)
927        displ_num(1,index)=-floor(index/2);
928        displ_num(2,index)=ceil(index/2);
929        displ_num(3,index)=0;
930        displ_num(4,index)=0;
931    end
932    set(handles.jtext,'Visible','On')
933    set(handles.first_j,'Visible','On')
934    set(handles.last_j,'Visible','On')
935    set(handles.incr_j,'Visible','On')
936    set(handles.nb_field2,'Visible','On')
937    set(handles.ref_j,'Visible','On')
938    if nbfield > 1
939        set(handles.itext,'Visible','On')
940        set(handles.first_i,'Visible','On')
941        set(handles.last_i,'Visible','On')
942        set(handles.incr_i,'Visible','On')
943        set(handles.nb_field,'Visible','On')
944        set(handles.ref_i,'Visible','On')
945    else
946        set(handles.itext,'Visible','Off')
947        set(handles.first_i,'Visible','Off')
948        set(handles.last_i,'Visible','Off')
949        set(handles.incr_i,'Visible','Off')
950        set(handles.nb_field,'Visible','Off')
951        set(handles.ref_i,'Visible','Off')
952    end
953elseif isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
954    for index=1:200%min(nbfield-1,200)
955        displ_num(1,index)=0;
956        displ_num(2,index)=0;
957        displ_num(3,index)=-floor(index/2);
958        displ_num(4,index)=ceil(index/2);
959    end
960    set(handles.itext,'Visible','On')
961    set(handles.first_i,'Visible','On')
962    set(handles.last_i,'Visible','On')
963    set(handles.incr_i,'Visible','On')
964    set(handles.nb_field,'Visible','On')
965    set(handles.ref_i,'Visible','On')
966    if nbfield2 > 1
967        set(handles.jtext,'Visible','On')
968        set(handles.first_j,'Visible','On')
969        set(handles.last_j,'Visible','On')
970        set(handles.incr_j,'Visible','On')
971        set(handles.nb_field2,'Visible','On')
972        set(handles.ref_j,'Visible','On')
973    else
974        set(handles.jtext,'Visible','Off')
975        set(handles.first_j,'Visible','Off')
976        set(handles.last_j,'Visible','Off')
977        set(handles.incr_j,'Visible','Off')
978        set(handles.nb_field2,'Visible','Off')
979        set(handles.ref_j,'Visible','Off')
980    end
981elseif isequal(mode,'displacement')%the pairs have the same indices
982    displ_num(1,1)=0;
983    displ_num(2,1)=0;
984    displ_num(3,1)=0;
985    displ_num(4,1)=0;
986    if nbfield > 1
987        set(handles.itext,'Visible','On')
988        set(handles.first_i,'Visible','On')
989        set(handles.last_i,'Visible','On')
990        set(handles.incr_i,'Visible','On')
991        set(handles.nb_field,'Visible','On')
992        set(handles.ref_i,'Visible','On')
993    else
994        set(handles.itext,'Visible','Off')
995        set(handles.first_i,'Visible','Off')
996        set(handles.last_i,'Visible','Off')
997        set(handles.incr_i,'Visible','Off')
998        set(handles.nb_field,'Visible','Off')
999        set(handles.ref_i,'Visible','Off')
1000    end
1001    if nbfield2 > 1
1002        set(handles.jtext,'Visible','On')
1003        set(handles.first_j,'Visible','On')
1004        set(handles.last_j,'Visible','On')
1005        set(handles.incr_j,'Visible','On')
1006        set(handles.nb_field2,'Visible','On')
1007        set(handles.ref_j,'Visible','On')
1008    else
1009        set(handles.jtext,'Visible','Off')
1010        set(handles.first_j,'Visible','Off')
1011        set(handles.last_j,'Visible','Off')
1012        set(handles.incr_j,'Visible','Off')
1013        set(handles.nb_field2,'Visible','Off')
1014        set(handles.ref_j,'Visible','Off')
1015    end
1016end
1017set(handles.list_pair_civ1,'UserData',displ_num);
1018find_netcpair_civ1(hObject, eventdata, handles)
1019find_netcpair_civ2(hObject, eventdata, handles)
1020
1021%------------------------------------------------------------------------
1022% determine the menu for civ1 pairs depending on existing netcdf file at the middle of
1023% the field series set by first_i, incr, last_i
1024function find_netcpair_civ1(hObject, eventdata, handles)
1025%------------------------------------------------------------------------
1026set(gcf,'Pointer','watch')
1027%nomenclature types
1028'TESTpair'
1029filebase=get(handles.RootName,'String');
1030[filepath,Nme,ext_dir]=fileparts(filebase);
1031browse=get(handles.RootName,'UserData');
1032compare_list=get(handles.compare,'String');
1033val=get(handles.compare,'Value');
1034compare=compare_list{val};
1035if strcmp(compare,'displacement')
1036    mode='displacement';
1037else
1038    mode_list=get(handles.CivMode,'String');
1039    mode_value=get(handles.CivMode,'Value');
1040    if isempty(mode_list)
1041        return
1042    end
1043    mode=mode_list{mode_value};
1044end
1045
1046% nomenclature type of the .nc files
1047nom_type_ima=[];%default
1048if isfield(browse,'nom_type_ima')
1049    nom_type_ima=browse.nom_type_ima;
1050end
1051
1052%determine nom_type_nc:
1053nom_type_nc=[];%default
1054if isfield(browse,'nom_type_nc')
1055    nom_type_nc=browse.nom_type_nc;
1056end
1057if isempty(nom_type_nc)
1058    [nom_type_nc]=nomtype2pair(nom_type_ima,isequal(mode,'series(Di)'),isequal(mode,'series(Dj)'));
1059end
1060browse.nom_type_nc=nom_type_nc;
1061set(handles.RootName,'UserData',browse)
1062
1063%reads .nc subdirectoy and image numbers from the interface
1064subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
1065ref_i=str2double(get(handles.ref_i,'String'));
1066if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
1067    ref_j=0;
1068else
1069    ref_j=str2double(get(handles.ref_j,'String'));
1070end
1071time=get(handles.ImaDoc,'UserData');%get the set of times
1072if isempty(time)
1073    time=[0 1];
1074end
1075dt_unit=1000;%default
1076displ_num=get(handles.list_pair_civ1,'UserData');
1077
1078%eliminate the first pairs inconsistent with the position
1079if isempty(displ_num)
1080    nbpair=0;
1081else
1082    nbpair=length(displ_num(1,:));%nbre of displayed pairs
1083    if  isequal(mode,'series(Di)')  %| isequal(mode,'st_series(Di)')
1084        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
1085    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1086        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
1087    end
1088end
1089nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
1090
1091%look for existing processed pairs involving the field at the middle of the series if civ1 will not
1092% be performed, while the result is needed for next steps.
1093displ_pair={''};
1094select=ones(size(1:nbpair));%flag for displayed pairs =1 for display
1095testpair=0;
1096% case with no civ1 operation, netcdf files need to exist for reading
1097if get(handles.CIV1,'Value')==0 %
1098    if ~exist(fullfile(filepath,subdir_civ1,ext_dir),'dir')
1099        msgbox_uvmat('ERROR',['no civ1 file available: subdirectory ' subdir_civ1 ' does not exist']);
1100        set(handles.list_pair_civ1,'String',{});
1101        return
1102    end
1103    for ipair=1:nbpair
1104        filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1105            ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1106        select(ipair)=exist(filename,'file')==2;% put flag to 0 if the file does not exist
1107    end   
1108    % case of no displayed pair
1109    if isequal(select,zeros(size(1:nbpair)))
1110        'TESTzero'
1111        browse
1112        if isfield(browse,'incr_pair') && ~isequal(browse.incr_pair,[0 0])
1113            num_i1=ref_i-floor(browse.incr_pair(1)/2);
1114            num_i2=ref_i+ceil(browse.incr_pair(1)/2);
1115            num_j1=ref_j-floor(browse.incr_pair(2)/2);
1116            num_j2=ref_j+ceil(browse.incr_pair(2)/2);
1117            filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ1);
1118            select(1)=exist(filename,'file')==2;
1119            testpair=1;
1120        else
1121            if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1122                msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ1]);
1123            else
1124                msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ1]);
1125            end
1126            set(handles.list_pair_civ1,'String',{''});
1127            %COMPLETER CAS STEREO
1128            return
1129        end
1130    end
1131end
1132if isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
1133    if testpair
1134        displ_pair{1}=['Di= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
1135    else
1136        for ipair=1:nbpair
1137            if select(ipair)
1138                if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1139                    dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1140                    displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1141                else
1142                    displ_pair{ipair}='...'; %pair not displayed in the menu
1143                end
1144            end
1145        end
1146    end
1147elseif isequal(mode,'series(Dj)')%|isequal(mode,'st_series(Dj)')% series on the j index
1148    if testpair
1149        displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];
1150    else
1151        for ipair=1:nbpair
1152            if select(ipair)
1153                if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1154                    dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1155                    displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1156                end
1157            elseif testpair
1158                displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(2)/2)) '|' num2str(ceil(browse.incr_pair(2)/2))];
1159            else
1160                displ_pair{ipair}='...'; %pair not displayed in the menu
1161            end
1162        end
1163    end
1164elseif isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')%case of pairs
1165    for ipair=1:nbpair
1166        if select(ipair)
1167            dt=time(ref_i+displ_num(4,ipair),displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),displ_num(1,ipair));%time interval dt
1168            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1169                ' :dt= ' num2str(dt*1000)];
1170        else
1171            displ_pair{ipair}='...'; %pair not displayed in the menu
1172        end
1173    end
1174elseif isequal(mode,'displacement')
1175    displ_pair={'Di=Dj=0'};
1176end
1177set(handles.list_pair_civ1,'String',displ_pair');
1178ichoice=find(select,1);
1179if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
1180initial=get(handles.list_pair_civ1,'Value');%initial choice of pair
1181if initial>nbpair
1182    set(handles.list_pair_civ1,'Value',ichoice);% first valid pair proposed by default in the menu
1183end
1184if numel(select)>=initial && ~isequal(select(initial),1)
1185    set(handles.list_pair_civ1,'Value',ichoice);% first valid pair proposed by default in the menu
1186end
1187
1188%set(handles.list_pair_civ2,'String',displ_pair');
1189initial=get(handles.list_pair_civ2,'Value');
1190if initial>length(displ_pair')%|~isequal(select(initial),1)
1191    if ichoice <= length(displ_pair')
1192        set(handles.list_pair_civ2,'Value',ichoice);% same pair proposed by default for civ2
1193    else
1194        set(handles.list_pair_civ2,'Value',1);% same pair proposed by default for civ2
1195    end
1196end
1197set(handles.list_pair_civ2,'String',displ_pair');
1198set(gcf,'Pointer','arrow')
1199
1200%------------------------------------------------------------------------
1201% determine the menu for civ2 pairs depending on the existing netcdf file at the
1202%middle of the series set by first_i, incr, last_i
1203function find_netcpair_civ2(hObject, eventdata, handles)
1204%------------------------------------------------------------------------
1205set(gcf,'Pointer','watch')
1206%nomenclature types
1207filebase=get(handles.RootName,'String');
1208[filepath,Nme,ext_dir]=fileparts(filebase);
1209browse=get(handles.RootName,'UserData');
1210compare_list=get(handles.compare,'String');
1211val=get(handles.compare,'Value');
1212compare=compare_list{val};
1213if strcmp(compare,'displacement')
1214    mode='displacement';
1215else
1216    mode_list=get(handles.CivMode,'String')
1217    if isempty(mode_list)
1218        msgbox_uvmat('ERROR','please enter an input image or netcdf file')
1219        return
1220    end
1221    mode_value=get(handles.CivMode,'Value')
1222    mode=mode_list{mode_value};
1223end
1224
1225% nomenclature type of the .nc files
1226nom_type_ima='ima_num';%default
1227if isfield(browse,'nom_type_ima')
1228    nom_type_ima=browse.nom_type_ima;
1229end
1230nom_type_nc='_i1-i2';%default
1231if isfield(browse,'nom_type_nc')
1232    nom_type_nc=browse.nom_type_nc;
1233end
1234if isequal(nom_type_ima,'png_old') || isequal(nom_type_ima,'netc_old')|| isequal(nom_type_ima,'raw_SMD')|| isequal(nom_type_nc,'netc_old')
1235    nom_type_nc='netc_old';%nom_type for the netcdf files
1236elseif isequal(nom_type_ima,'none')||isequal(nom_type_nc,'none')
1237    nom_type_nc='none';
1238elseif isequal(nom_type_ima,'avi')||isequal(nom_type_ima,'_i')||isequal(nom_type_ima,'ima_num')||isequal(nom_type_nc,'_i1-i2')
1239    nom_type_nc='_i1-i2';
1240else
1241    if  isequal(mode,'series(Di)')%|isequal(mode,'st_series(Di)')
1242        nom_type_nc='_i1-i2_j'; % PIV in volume
1243    else
1244        nom_type_nc='_i_j1-j2';
1245    end
1246end
1247browse.nom_type_nc=nom_type_nc;
1248set(handles.RootName,'UserData',browse)
1249
1250%reads .nc subdirectory and image numbers from the interface
1251subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
1252subdir_civ2=get(handles.subdir_civ2,'String');%subdirectory subdir_civ2 for the netcdf data
1253% first_i=str2num(get(handles.first_i,'String'));
1254% last_i=str2num(get(handles.last_i,'String'));
1255% incr=str2num(get(handles.incr_i,'String'));
1256% num1=first_i:incr:last_i;
1257% if isempty(num1)
1258%     set(handles.list_pair_civ2,'Value',1);
1259%     set(handles.list_pair_civ2,'String',{''});
1260%     return
1261% end
1262ref_i=str2double(get(handles.ref_i,'String'));
1263if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
1264    ref_j=0;
1265else
1266    ref_j=str2double(get(handles.ref_j,'String'));
1267end
1268time=get(handles.ImaDoc,'UserData'); %get the set of times
1269if isempty(time)
1270    time=[0 1];%default
1271end
1272%dt_unit=str2num(get(handles.dt,'String'));% used when there is no image documentation file
1273%dt_unit=1000;
1274displ_num=get(handles.list_pair_civ1,'UserData');
1275
1276
1277%eliminate the first pairs inconsistent with the position
1278if isempty(displ_num)
1279    nbpair=0;
1280else
1281    nbpair=length(displ_num(1,:));%nbre of displayed pairs
1282    if  isequal(mode,'series(Di)')% | isequal(mode,'st_series(Di)')
1283        nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
1284    elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1285        nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
1286    end
1287end
1288nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
1289
1290%look for existing processed pairs involving the field at the middle of the series if civ1 will not
1291% be performed, while the result is needed for next steps.
1292displ_pair={''}; %default
1293select=ones(size(1:nbpair));%default =1 for numbers of displayed pairs
1294if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0%&...
1295    if ~exist(fullfile(filepath,subdir_civ2,ext_dir),'dir')
1296        errordlg(['no civ2 file available: subdirectory ' subdir_civ2 ' does not exist'])
1297        set(handles.list_pair_civ2,'Value',1);
1298        set(handles.list_pair_civ2,'String',{''});
1299        return
1300    end
1301    for ipair=1:nbpair
1302        filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1303            ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1304        select(ipair)=exist(filename,'file')==2;
1305    end
1306    if  isequal(select,zeros(size(1:nbpair)))
1307        if isfield(browse,'incr_pair')
1308            num_i1=ref_i-floor(browse.incr_pair(1)/2);
1309            num_i2=ref_i+floor((browse.incr_pair(1)+1)/2);
1310            num_j1=ref_j-floor(browse.incr_pair(2)/2);
1311            num_j2=ref_j+floor((browse.incr_pair(2)+1)/2);
1312            filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ2);
1313            select(1)=exist(filename,'file')==2;
1314        else
1315            if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1316                errordlg(['no civ2 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ2])
1317            else
1318                errordlg(['no civ2 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ2])
1319            end
1320            set(handles.list_pair_civ2,'Value',1);
1321            set(handles.list_pair_civ2,'String',{''});
1322            return
1323        end
1324    end
1325end
1326if isequal(mode,'series(Di)') % | isequal(mode,'st_series(Di)')
1327    for ipair=1:nbpair
1328        if select(ipair)
1329            if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1330                dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1331                displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1332            end
1333        else
1334            displ_pair{ipair}='...'; %pair not displayed in the menu
1335        end
1336    end
1337elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)') % series on the j index
1338    for ipair=1:nbpair
1339        if select(ipair)
1340            if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1341                dt=time(ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair));%time interval dt
1342                displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1343            end
1344        else
1345            displ_pair{ipair}='...'; %pair not displayed in the menu
1346        end
1347    end
1348elseif isequal(mode,'pair j1-j2')% | isequal(mode,'st_pair j1-j2') %case of pairs
1349    for ipair=1:nbpair
1350        if select(ipair)
1351            dt=time(ref_i+displ_num(4,ipair),displ_num(2,ipair))-time(ref_i+displ_num(3,ipair),displ_num(1,ipair));%time interval dt
1352            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1353                ' :dt= ' num2str(dt*1000)];
1354        else
1355            displ_pair{ipair}='...'; %pair not displayed in the menu
1356        end
1357    end
1358elseif isequal(mode,'displacement')
1359    displ_pair={'Di=Dj=0'};
1360end
1361val=get(handles.list_pair_civ2,'Value');
1362ichoice=find(select,1);
1363if (isempty(ichoice) || ichoice < 1); ichoice=1; end;
1364if get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0
1365    val=ichoice;% first valid pair proposed by default in the menu
1366end
1367if val>length(displ_pair')
1368    set(handles.list_pair_civ2,'Value',1);% first valid pair proposed by default in the menu
1369else
1370    set(handles.list_pair_civ2,'Value',val);
1371end
1372set(handles.list_pair_civ2,'String',displ_pair');
1373set(gcf,'Pointer','arrow')
1374
1375
1376
1377
1378%------------------------------------------------------------------------
1379% --- Executes on selection change in list_pair_civ1.
1380function list_pair_civ1_Callback(hObject, eventdata, handles)
1381%------------------------------------------------------------------------
1382%reproduce by default the chosen pair in the civ2 menu
1383list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
1384index_pair=get(handles.list_pair_civ1,'Value');
1385displ_num=get(handles.list_pair_civ1,'UserData');
1386% num_a=displ_num(1,index_pair);
1387% num_b=displ_num(2,index_pair);
1388list_pair2=get(handles.list_pair_civ2,'String');%get the menu of image pairs
1389if index_pair<=length(list_pair2)
1390    set(handles.list_pair_civ2,'Value',index_pair);
1391end
1392
1393%update first_i and last_i according to the chosen image pairs
1394mode_list=get(handles.CivMode,'String');
1395mode_value=get(handles.CivMode,'Value');
1396mode=mode_list{mode_value};
1397if isequal(mode,'series(Di)')
1398    first_i=str2double(get(handles.first_i,'String'));
1399    last_i=str2double(get(handles.last_i,'String'));
1400    incr_i=str2double(get(handles.incr_i,'String'));
1401    num1=first_i:incr_i:last_i;
1402    lastfield=str2double(get(handles.nb_field,'String'));
1403    if ~isnan(lastfield)
1404        test_find=(num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1405            (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield);
1406        num1=num1(test_find);
1407    end
1408    set(handles.first_i,'String',num2str(num1(1)));
1409    set(handles.last_i,'String',num2str(num1(end)));
1410elseif isequal(mode,'series(Dj)')
1411    first_j=str2double(get(handles.first_j,'String'));
1412    last_j=str2double(get(handles.last_j,'String'));
1413    incr_j=str2double(get(handles.incr_j,'String'));
1414    num_j=first_j:incr_j:last_j;
1415    lastfield2=str2double(get(handles.nb_field2,'String'));
1416    if ~isnan(lastfield2)
1417        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1418            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
1419        num1=num_j(test_find);
1420    end
1421    set(handles.first_j,'String',num2str(num1(1)));
1422    set(handles.last_j,'String',num2str(num1(end)));
1423end
1424
1425%------------------------------------------------------------------------
1426% --- Executes on selection change in list_pair_civ2.
1427function list_pair_civ2_Callback(hObject, eventdata, handles)
1428%------------------------------------------------------------------------
1429index_pair=get(handles.list_pair_civ2,'Value');%get the selected position index in the menu
1430
1431%update first_i and last_i according to the chosen image pairs
1432mode_list=get(handles.CivMode,'String');
1433mode_value=get(handles.CivMode,'Value');
1434mode=mode_list{mode_value};
1435if isequal(mode,'series(Di)')
1436    first_i=str2double(get(handles.first_i,'String'));
1437    last_i=str2double(get(handles.last_i,'String'));
1438    incr_i=str2double(get(handles.incr_i,'String'));
1439    num1=first_i:incr_i:last_i;
1440    lastfield=str2double(get(handles.nb_field,'String'));
1441    if ~isnan(lastfield)
1442        test_find=(num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1443            (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield);
1444        num1=num1(test_find);
1445    end
1446    set(handles.first_i,'String',num2str(num1(1)));
1447    set(handles.last_i,'String',num2str(num1(end)));
1448elseif isequal(mode,'series(Dj)')
1449    first_j=str2double(get(handles.first_j,'String'));
1450    last_j=str2double(get(handles.last_j,'String'));
1451    incr_j=str2double(get(handles.incr_j,'String'));
1452    num_j=first_j:incr_j:last_j;
1453    lastfield2=str2double(get(handles.nb_field2,'String'));
1454    if ~isnan(lastfield2)
1455        test_find=(num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1456            (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2);
1457        num1=num_j(test_find);
1458    end
1459    set(handles.first_j,'String',num2str(num1(1)));
1460    set(handles.last_j,'String',num2str(num1(end)));
1461end
1462
1463%------------------------------------------------------------------------
1464% --- Executes on button press in RUN: processing on local computer
1465function RUN_Callback(hObject, eventdata, handles)
1466%------------------------------------------------------------------------
1467set(handles.RUN, 'Enable','Off')
1468set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1469batch=0;
1470errormsg=launch_jobs(hObject, eventdata, handles,batch);
1471set(handles.RUN, 'Enable','On')
1472set(handles.RUN,'BackgroundColor',[1 0 0])
1473
1474% start status callback to visualise results
1475if ~isempty(errormsg)
1476    display(errormsg)
1477    msgbox_uvmat('ERROR',errormsg)
1478elseif  isfield(handles,'status') %&& ~isequal(get(handles.CivMode,'Value'),3)
1479    set(handles.status,'Value',1);%suppress status display
1480    status_Callback(hObject, eventdata, handles)
1481end
1482
1483%------------------------------------------------------------------------
1484% --- Executes on button press in BATCH: remote processing
1485function BATCH_Callback(hObject, eventdata, handles)
1486% -----------------------------------------------------------------------
1487set(handles.BATCH, 'Enable','Off')
1488set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])
1489batch=1;
1490errormsg=launch_jobs(hObject, eventdata, handles, batch);
1491set(handles.BATCH, 'Enable','On')
1492set(handles.BATCH,'BackgroundColor',[1 0 0])
1493
1494% start status callback to visualise results
1495if ~isempty(errormsg)
1496    display(errormsg)
1497    msgbox_uvmat('ERROR',errormsg)
1498elseif isfield(handles,'status')
1499    set(handles.status,'Value',1);%suppress status display
1500    status_Callback(hObject, eventdata, handles)
1501end
1502
1503%------------------------------------------------------------------------
1504% --- Lauch command called by RUN and BATCH: remote processing
1505function errormsg=launch_jobs(hObject, eventdata, handles, batch)
1506%-----------------------------------------------------------------------
1507errormsg='';%default
1508
1509
1510%% check the selected list of operations:
1511operations={'CIV1','FIX1','PATCH1','CIV2','FIX2','PATCH2'};
1512box_test(1)=get(handles.CIV1,'Value');
1513box_test(2)=get(handles.FIX1,'Value');
1514box_test(3)=get(handles.PATCH1,'Value');
1515box_test(4)=get(handles.CIV2,'Value');
1516box_test(5)=get(handles.FIX2,'Value');
1517box_test(6)=get(handles.PATCH2,'Value');
1518index_first=find(box_test==1,1);
1519if isempty(index_first)
1520    errormsg='no selected operation';
1521    return
1522end
1523index_last=find(box_test==1,1,'last');
1524box_used=box_test(index_first : index_last);
1525[box_missing,ind_missing]=min(box_used);
1526if isequal(box_missing,0); %there is a missing step in the sequence of operations
1527    errormsg=['missing' cell2mat(operations(ind_missing))];
1528    return
1529end
1530
1531%% check mask if selecetd
1532%could be included in get_mask callback ?
1533if isequal(get(handles.get_mask_civ1,'Value'),1)
1534    maskname=get(handles.mask_civ1,'String');
1535    if ~exist(maskname,'file')
1536        get_mask_civ1_Callback(hObject, eventdata, handles);
1537    end
1538end
1539if isequal(get(handles.get_mask_fix1,'Value'),1)
1540    maskname=get(handles.mask_fix1,'String');
1541    if ~exist(maskname,'file')
1542        get_mask_fix1_Callback(hObject, eventdata, handles);
1543    end
1544end
1545if isequal(get(handles.get_mask_civ2,'Value'),1)
1546    maskname=get(handles.mask_civ2,'String');
1547    if ~exist(maskname,'file')
1548        get_mask_civ2_Callback(hObject, eventdata, handles);
1549    end
1550end
1551if isequal(get(handles.get_mask_fix2,'Value'),1)
1552    maskname=get(handles.mask_fix2,'String');
1553    if ~exist(maskname,'file')
1554        get_mask_fix2_Callback(hObject, eventdata, handles);
1555    end
1556end
1557
1558%% reinitialise status callback
1559if isfield(handles,'status')
1560    set(handles.status,'Value',0);%suppress status display
1561    status_Callback(hObject, eventdata, handles)
1562end
1563
1564%% set the list of files and check them
1565display('checking the files...')
1566[ref_i,ref_j,errormsg]=find_ref_indices(handles);
1567if ~isempty(errormsg)
1568    return
1569end
1570[filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,xx,yy,compare]=...
1571    set_civ_filenames(handles,ref_i,ref_j,box_test);
1572% ADDED
1573   [Rootbat,Filebat]=fileparts(filecell.nc.civ1{1,1});%output netcdf file (without extention)
1574%ADDED
1575set(handles.civ,'UserData',filecell);%store for futur use of status callback
1576if isempty(filecell)% (error message displayed in fct set_civ_filenames)
1577    return
1578end
1579nbfield=numel(num1_civ1);
1580nbslice=numel(num_a_civ1);
1581
1582%% read the PARAM.xml file to get the binaries (and batch_mode if batch)
1583path_UVMAT=fileparts(which('uvmat')); %path to the source directory of uvmat
1584xmlfile='PARAM.xml';
1585if exist(xmlfile,'file')% search parameter xml file in the whole matlab path
1586    t=xmltree(xmlfile);
1587    s=convert(t);
1588else
1589    errormsg=['no file ' xmlfile];
1590    return
1591end
1592
1593test_interp=0; %eviter les variables test_ (LG)
1594
1595if batch
1596    if isfield(s,'BatchParam')
1597        param.global=s.BatchParam;
1598        if isfield(param.global,'BatchMode')
1599            batch_mode=param.global.BatchMode;
1600            if ~ismember(batch_mode,{'sge','oar'})
1601                errormsg=['batch mode ' batch_mode ' not supported by UVMAT'];
1602                return
1603            end
1604        end
1605    else
1606        errormsg='no batch civ binaries defined in PARAM.xml';
1607        return
1608    end
1609else % run
1610    if isfield(s,'RunParam')
1611        param.global=s.RunParam;
1612    else
1613        msgbox_uvmat('ERROR','no run civ binaries defined in PARAM.xml')
1614        return
1615    end
1616end
1617
1618%% check batch mode supported
1619if batch
1620    switch batch_mode
1621        case 'sge'
1622            test_command='qstat';
1623        case 'oar'
1624            test_command='oartat';
1625    end   
1626    [s,w]=system(test_command);
1627    if ~isequal(s,0)
1628        msgbox_uvmat('ERROR',[batch_mode ' batch system not available'])
1629        return
1630    end
1631end
1632
1633%% check if the binaries exist
1634if isequal(get(handles.Matlab,'checked'),'on')
1635    CivMode='Matlab';
1636else
1637    CivMode='CivX';
1638end
1639switch CivMode
1640     case {'CivX','CivAll'}     
1641         for bin_name={'Civ1Bin','Civ2Bin','PatchBin','FixBin','CivBin'}
1642             if isfield(param.global,bin_name{1})
1643                 if ~exist(param.global.(bin_name{1}),'file')%look for the full path if the file name has been defined with a relative path in PARAM.xml
1644                     fullname=fullfile(path_UVMAT,param.global.(bin_name{1}));
1645                     if exist(fullname,'file')
1646                         param.global.(bin_name{1})=fullname;
1647                     else
1648                         msgbox_uvmat('ERROR',['Binary ' param.global.(bin_name{1}) ' defined in PARAM.xm does not exist'])
1649                         return
1650                     end
1651                 else
1652                     [path,name,ext]=fileparts(param.global.(bin_name{1}));
1653                     currentdir=pwd;
1654                     cd(path);
1655                     binpath=pwd;%path of the binary
1656                     param.global.(bin_name{1})=fullfile(binpath,[name ext]);
1657%                      display(param.global.(bin_name{1}));
1658                     cd(currentdir);
1659                 end
1660                 
1661             end
1662         end
1663    case 'Matlab'
1664        if batch
1665            % vérifier Matlab installé sur le cluster
1666            % difficile a faire a priori
1667        end         
1668end
1669
1670display('files OK, processing...')
1671
1672%% get civ1 parameters
1673if box_test(1)
1674    param.civ1=read_param_civ1(handles,filecell);
1675end
1676
1677%% get fix1 parameters
1678if box_test(2)
1679    param.fix1=read_param_fix1(handles,filecell)
1680end
1681
1682%% get patch1 parameters
1683if box_test(3)
1684    param.Patch1=read_panel(handles.panel_Patch1);
1685    %param.patch1=read_param_patch1(handles)
1686end
1687
1688%% get civ2 parameters:
1689if box_test(4)
1690    param.civ2=read_param_civ2(handles,cell2mat(filecell.ima1.civ2(1,1)));
1691end
1692
1693%% get fix2 parameters
1694if box_test(5)
1695    flagindex2(1)=get(handles.vec_Fmin2_2, 'Value');
1696    flagindex2(2)=get(handles.vec_F3_2, 'Value');
1697    flagindex2(3)=get(handles.vec_F4, 'Value');
1698    thresh_vec2C=str2double(get(handles.thresh_vec2C,'String'));%threshold on image correlation vec_C
1699    thresh_vel2=str2double(get(handles.thresh_vel2,'String'));%threshold on velocity modulus
1700    test_mask=get(handles.get_mask_fix2,'Value');
1701    nbslice_mask=get(handles.mask_fix2,'UserData'); % get the number of slices (= number of masks)
1702    %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX AVEC REF FILE ET OPTION inf_sup=2
1703    %     inf_sup=get(handles.inf_sup2,'Value');
1704    %     ref=get(handles.ref_fix2,'UserData');
1705   
1706    %%%%%%%%%%%%%%%%%%%
1707end
1708
1709%% get patch2 parameters
1710if box_test(6)==1
1711    param.Patch2=read_panel(handles.panel_Patch1);
1712%     rho_patch2=str2double(get(handles.num_SmoothParam,'String'));
1713%     if isnan(rho_patch2)
1714%         rho_patch2='1000';
1715%         set(handles.num_SmoothParam,'String','1')
1716%     else
1717%         rho_patch2=num2str(1000*rho_patch2);
1718%     end
1719%     nx_patch2=get(handles.num_Nx,'String');
1720%     ny_patch2=get(handles.num_Ny,'String');
1721%     if isnan(str2double(nx_patch2))
1722%         nx_patch2='50' ;%default
1723%         set(handles.num_Nx,'String','50');
1724%     end
1725%     if isnan(str2double(ny_patch2))
1726%         ny_patch2='50' ;%default
1727%         set(handles.num_Ny,'String','50');
1728%     end
1729%     subdomain_patch2=get(handles.num_SubdomainSize,'String');
1730%     thresh_patch2=get(handles.num_MaxDiff,'String');
1731end
1732
1733%%
1734
1735
1736%% MAIN LOOP
1737time=get(handles.ImaDoc,'UserData'); %get the set of times
1738
1739super_cmd=[];
1740batch_file_list=[];
1741   
1742for ifile=1:nbfield
1743    for j=1:nbslice
1744        % initiate system command
1745        switch CivMode
1746            case 'CivX'
1747                if isunix % check: necessaire aussi en RUN?
1748                    cmd=['#!/bin/bash \n '...
1749                        '#$ -cwd \n '...
1750                        'hostname && date \n '...
1751                        'umask 002 \n'];%allow writting access to created files for user group
1752                else
1753                    cmd=[];
1754                end
1755            case 'CivAll'
1756                %         if CivAll
1757                CivAllxml=xmltree;% xml contents,  all parameters
1758                CivAllCmd='';
1759                CivAllxml=set(CivAllxml,1,'name','CivDoc');
1760        end
1761       
1762       
1763        % define output file name
1764        if box_test(4)==1 || box_test(5)==1 || box_test(6)==1
1765            OutputFile=filecell.nc.civ2{ifile,j};
1766        else
1767            OutputFile=filecell.nc.civ1{ifile,j};
1768        end
1769        OutputFile=regexprep(OutputFile,'.nc','');
1770
1771       
1772        %CIV1
1773        if box_test(1)
1774            % read image-dependent parameters
1775            param.civ1.filename_ima_a=filecell.ima1.civ1{ifile,j};
1776            param.civ1.filename_ima_b=filecell.ima2.civ1{ifile,j};
1777            param.civ1.Dt=num2str(time(num2_civ1(ifile),num_b_civ1(j))-time(num1_civ1(ifile),num_a_civ1(j)));
1778            param.civ1.T0=num2str((time(num2_civ1(ifile),num_b_civ1(j))+time(num1_civ1(ifile),num_a_civ1(j)))/2);
1779            param.civ1.term_a=num2stra(num_a_civ1(j),nom_type_nc);%UTILITE?
1780            param.civ1.term_b=num2stra(num_b_civ1(j),nom_type_nc);%
1781           
1782            % read mask parameters
1783            if get(handles.get_mask_civ1,'Value')
1784                maskdispl=get(handles.mask_civ1,'String');
1785                if exist(maskdispl,'file')
1786                    param.civ1.maskname=maskdispl;
1787                    param.civ1.maskflag='y';
1788                else
1789                    maskbase=[filecell.filebase '_' maskdispl]; %
1790                    nbslice_mask=str2double(maskdispl(1:end-4)); %
1791                    num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
1792                    param.civ1.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1793                    if exist(param.civ1.maskname,'file')
1794                        param.civ1.maskflag='y';
1795                    else
1796                        param.civ1.maskname='noFile use default';
1797                        param.civ1.maskflag='n';
1798                    end
1799                end
1800            else
1801                param.civ1.maskname='noFile use default';
1802                param.civ1.maskflag='n';
1803            end
1804           
1805            % read grid parameters
1806            if get(handles.browse_gridciv1,'Value')
1807                param.civ1.gridflag='y';
1808                gridname=get(handles.grid_civ1,'String');
1809                if isequal(gridname(end-3:end),'grid')
1810                    nbslice_grid=str2double(gridname(1:end-4)); %
1811                    if ~isnan(nbslice_grid)
1812                        num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
1813                        param.civ1.gridname=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1814                        if ~exist(param.civ1.gridname,'file')
1815                            msgbox_uvmat('ERROR','grid file absent for civ1')
1816                        end
1817                    elseif exist(gridname,'file')
1818                        param.civ1.gridname=gridname;
1819                    else
1820                        msgbox_uvmat('ERROR','grid file absent for civ1')
1821                    end
1822                end
1823            else
1824                param.civ1.gridname='noFile use default';
1825                param.civ1.gridflag='n';
1826            end
1827           
1828            switch CivMode
1829                case 'CivX'
1830                    civ1_exe=CIV1_CMD(filecell.nc.civ1{ifile,j},param);%create the parameter file .civ1.cmx and set the execution string civ1_exe
1831                    cmd=[cmd civ1_exe '\n'];
1832                case 'CivAll'
1833                    CivAllCmd=[CivAllCmd ' civ1 '];
1834                    str=CIV1_CMD_Unified(filecell.nc.civ1{ifile,j},'',param.civ1);
1835                    fieldnames=fields(str);
1836                    [CivAllxml,uid_civ1]=add(CivAllxml,1,'element','civ1');
1837                    for ilist=1:length(fieldnames)
1838                        val=eval(['str.' fieldnames{ilist}]);
1839                        if ischar(val)
1840                            [CivAllxml,uid_t]=add(CivAllxml,uid_civ1,'element',fieldnames{ilist});
1841                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1842                        end
1843                    end
1844            end
1845        end
1846       
1847        % FIX1
1848        if box_test(2)
1849           switch CivMode
1850                case 'CivX'
1851                    if isunix %unix system
1852                        cmd_FIX=[param.global.FixBin ' -f ' filecell.nc.civ1{ifile,j} ' -fi1 ' num2str(param.fix1.flagindex1(1)) ...
1853                            ' -fi2 ' num2str(param.fix1.flagindex1(2)) ' -fi3 ' num2str(param.fix1.flagindex1(3)) ...
1854                            ' -threshC ' num2str(param.fix1.thresh_vecC1) ' -threshV ' num2str(param.fix1.thresh_vel1) ' -maskName ' param.fix1.maskname];
1855                    else %windows system
1856                        cmd_FIX=['"' param.global.FixBin '" -f "' filecell.nc.civ1{ifile,j} '" -fi1 ' num2str(param.fix1.flagindex1(1)) ...
1857                            ' -fi2 ' num2str(param.fix1.flagindex1(2)) ' -fi3 ' num2str(param.fix1.flagindex1(3)) ...
1858                            ' -threshC ' num2str(param.fix1.thresh_vecC1) ' -threshV ' num2str(param.fix1.thresh_vel1) ' -maskName "' param.fix1.maskname '"'];
1859                        cmd_FIX=regexprep(cmd_FIX,'\\','\\\\');
1860                    end
1861                    cmd=[cmd cmd_FIX '\n'];
1862                case 'CivAll'
1863                    fix1.inputFileName=filecell.nc.civ1{ifile,j} ;
1864                    fix1.fi1=num2str(param.fix1.flagindex1(1));
1865                    fix1.fi2=num2str(param.fix1.flagindex1(2));
1866                    fix1.fi3=num2str(param.fix1.flagindex1(3));
1867                    fix1.threshC=num2str(param.fix1.thresh_vecC1);
1868                    fix1.threshV=num2str(param.fix1.thresh_vel1);
1869                    fieldnames=fields(fix1);
1870                    [CivAllxml,uid_fix1]=add(CivAllxml,1,'element','fix1');
1871                    for ilist=1:length(fieldnames)
1872                        val=eval(['fix1.' fieldnames{ilist}]);
1873                        if ischar(val)
1874                            [CivAllxml,uid_t]=add(CivAllxml,uid_fix1,'element',fieldnames{ilist});
1875                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1876                        end
1877                    end
1878                    CivAllCmd=[CivAllCmd ' fix1 '];
1879            end
1880        end
1881       
1882        %PATCH1
1883        if box_test(3)==1
1884            switch CivMode
1885                case 'CivX'
1886                    cmd_PATCH=cmd_patch(filecell.nc.civ1{ifile,j},param.Patch1,param.global.PatchBin);
1887                    cmd=[cmd cmd_PATCH '\n'];
1888                case 'CivAll'
1889                    patch1.inputFileName=filecell.nc.civ1{ifile,j} ;
1890                    patch1.nopt=subdomain_patch1;
1891                    patch1.maxdiff=thresh_patch1;
1892                    patch1.ro=rho_patch1;
1893                    test_grid=get(handles.get_gridpatch1,'Value');
1894                    if test_grid
1895                        patch1.gridflag='y';
1896                        gridname=get(handles.grid_patch1,'String');
1897                        if isequal(gridname(end-3:end),'grid')
1898                            nbslice_grid=str2double(gridname(1:end-4)); %
1899                            if ~isnan(nbslice_grid)
1900                                num1_grid=mod(num1_civ1(ifile)-1,nbslice_grid)+1;
1901                                patch1.gridPatch=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1902                                if ~exist(patch1.gridPatch,'file')
1903                                    msgbox_uvmat('ERROR','grid file absent for patch1')
1904                                end
1905                            elseif exist(gridname,'file')
1906                                patch1.gridPatch=gridname;
1907                            else
1908                                msgbox_uvmat('ERROR','grid file absent for patch1')
1909                            end
1910                        end
1911                    else
1912                        patch1.gridPatch='none';
1913                        patch1.gridflag='n';
1914                        patch1.m=nx_patch1;
1915                        patch1.n=ny_patch1;
1916                    end
1917                    patch1.convectFlow='n';
1918                    fieldnames=fields(patch1);
1919                    [CivAllxml,uid_patch1]=add(CivAllxml,1,'element','patch1');
1920                    for ilist=1:length(fieldnames)
1921                        val=eval(['patch1.' fieldnames{ilist}]);
1922                        if ischar(val)
1923                            [CivAllxml,uid_t]=add(CivAllxml,uid_patch1,'element',fieldnames{ilist});
1924                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1925                        end
1926                    end
1927                    CivAllCmd=[CivAllCmd ' patch1 '];
1928            end
1929        end
1930        if box_test(4)==1
1931            param.civ2.filename_ima_a=filecell.ima1.civ2{ifile,j};
1932            param.civ2.filename_ima_b=filecell.ima2.civ2{ifile,j};
1933            param.civ2.Dt=num2str(time(num2_civ2(ifile),num_b_civ2(j))-time(num1_civ2(ifile),num_a_civ2(j)));
1934            param.civ2.T0=num2str((time(num2_civ1(ifile),num_b_civ2(j))+time(num1_civ2(ifile),num_a_civ2(j)))/2);
1935            param.civ2.term_a=num2stra(num_a_civ2(j),nom_type_nc);
1936            param.civ2.term_b=num2stra(num_b_civ2(j),nom_type_nc);
1937            param.civ2.filename_nc1=filecell.nc.civ1{ifile,j};
1938            param.civ2.filename_nc1(end-2:end)=[]; % remove '.nc'
1939            test_mask=get(handles.get_mask_civ2,'Value');
1940            if test_mask==0
1941                param.civ2.maskname='noFile use default';
1942                param.civ2.maskflag='n';
1943            else
1944                maskdispl=get(handles.mask_civ2,'String');
1945                if exist(maskdispl,'file')
1946                    param.civ2.maskname=maskdispl;
1947                    param.civ2.maskflag='y';
1948                else
1949                    maskbase=[filecell.filebase '_' maskdispl]; %
1950                    nbslice_mask=str2double(maskdispl(1:end-4)); %
1951                    num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
1952                    param.civ2.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
1953                    if exist(param.civ2.maskname,'file')
1954                        param.civ2.maskflag='y';
1955                    else
1956                        param.civ2.maskname='noFile use default';
1957                        param.civ2.maskflag='n';
1958                    end
1959                end
1960            end
1961            gridname=get(handles.grid_civ2,'String');
1962            if numel(gridname)>=4 && isequal(gridname(end-3:end),'grid')
1963                nbslice_grid=str2double(gridname(1:end-4)); %
1964                if ~isnan(nbslice_grid)
1965                    param.civ2.gridflag='y';
1966                    num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
1967                    param.civ2.gridname=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1968                    if exist(param.civ2.gridname,'file')
1969                        param.civ2.gridflag='y';
1970                    else
1971                        param.civ2.gridname='noFile use default';
1972                        param.civ2.gridflag='n';
1973                    end
1974                elseif exist(gridname,'file')
1975                    param.civ2.gridflag='y';
1976                else
1977                    param.civ2.gridname='noFile use default';
1978                    param.civ2.gridflag='n';
1979                end
1980            end
1981            switch CivMode
1982                case 'CivX'
1983                    cmd_CIV2=CIV2_CMD(filecell.nc.civ2{ifile,j},[],param);%creates the cmx file [fullfile(Rootbat,Filebat) '.civ2.cmx]
1984                    cmd=[cmd cmd_CIV2 '\n'];
1985                case 'CivAll'
1986                    CivAllCmd=[CivAllCmd ' civ2 '];
1987                    str=CIV2_CMD_Unified(filecell.nc.civ2{ifile,j},'',param.civ2);
1988                    fieldnames=fields(str);
1989                    [CivAllxml,uid_civ2]=add(CivAllxml,1,'element','civ2');
1990                    for ilist=1:length(fieldnames)
1991                        val=eval(['str.' fieldnames{ilist}]);
1992                        if ischar(val)
1993                            [CivAllxml,uid_t]=add(CivAllxml,uid_civ2,'element',fieldnames{ilist});
1994                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1995                        end
1996                    end
1997            end
1998        end
1999       
2000        % FIX2
2001        if box_test(5)==1
2002            test_mask=get(handles.get_mask_fix2,'Value');
2003            if test_mask==0
2004                maskname=''; %no mask used
2005            else
2006                maskdispl=get(handles.mask_fix2,'String');
2007                maskbase=[filecell.filebase '_' maskdispl]; %
2008                nbslice_mask=str2double(maskdispl(1:end-4)); %
2009                num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
2010                maskname =name_generator(maskbase,num1_mask,1,'.png','_i');
2011            end
2012            switch CivMode
2013                case 'CivX'
2014                    if isunix
2015                        cmd_FIX=[param.global.FixBin ' -f ' filecell.nc.civ2{ifile,j} ' -fi1 ' num2str(flagindex2(1)) ...
2016                            ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
2017                            ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName ' maskname];
2018                    else
2019                        cmd_FIX=['"' param.global.FixBin '" -f "' filecell.nc.civ2{ifile,j} '" -fi1 ' num2str(flagindex2(1)) ...
2020                            ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
2021                            ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName "' maskname '"'];
2022                        cmd_FIX=regexprep(cmd_FIX,'\\','\\\\');
2023                    end
2024                    cmd=[cmd cmd_FIX '\n'];
2025                case 'CivAll'
2026                    fix2.inputFileName=filecell.nc.civ2{ifile,j} ;
2027                    fix2.fi1=num2str(flagindex2(1));
2028                    fix2.fi2=num2str(flagindex2(2));
2029                    fix2.fi3=num2str(flagindex2(3));
2030                    fix2.threshC=num2str(thresh_vec2C);
2031                    fix2.threshV=num2str(thresh_vel2);
2032                    fieldnames=fields(fix2);
2033                    [CivAllxml,uid_fix2]=add(CivAllxml,1,'element','fix2');
2034                    for ilist=1:length(fieldnames)
2035                        val=eval(['fix2.' fieldnames{ilist}]);
2036                        if ischar(val)
2037                            [CivAllxml,uid_t]=add(CivAllxml,uid_fix2,'element',fieldnames{ilist});
2038                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
2039                        end
2040                    end
2041                    CivAllCmd=[CivAllCmd ' fix2 '];
2042            end
2043        end
2044       
2045        %PATCH2
2046        if box_test(6)==1
2047            switch CivMode
2048                case 'CivX'
2049                    cmd_PATCH=cmd_path(filecell.nc.civ2{ifile,j},param);
2050                    cmd=[cmd cmd_PATCH '\n'];
2051                case 'CivAll'
2052                    patch2.inputFileName=filecell.nc.civ1{ifile,j} ;
2053                    patch2.nopt=subdomain_patch2;
2054                    patch2.maxdiff=thresh_patch2;
2055                    patch2.ro=rho_patch2;
2056                    test_grid=get(handles.get_gridpatch2,'Value');
2057                    if test_grid
2058                        patch2.gridflag='y';
2059                        gridname=get(handles.grid_patch2,'String');
2060                        if isequal(gridname(end-3:end),'grid')
2061                            nbslice_grid=str2double(gridname(1:end-4)); %
2062                            if ~isnan(nbslice_grid)
2063                                num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
2064                                patch2.gridPatch=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
2065                                if ~exist(patch2.gridPatch,'file')
2066                                    msgbox_uvmat('ERROR','grid file absent for patch2')
2067                                end
2068                            elseif exist(gridname,'file')
2069                                patch2.gridPatch=gridname;
2070                            else
2071                                msgbox_uvmat('ERROR','grid file absent for patch2')
2072                            end
2073                        end
2074                    else
2075                        patch2.gridPatch='none';
2076                        patch2.gridflag='n';
2077                        patch2.m=nx_patch2;
2078                        patch2.n=ny_patch2;
2079                    end
2080                    patch2.convectFlow='n';
2081                    fieldnames=fields(patch2);
2082                    [CivAllxml,uid_patch2]=add(CivAllxml,1,'element','patch2');
2083                    for ilist=1:length(fieldnames)
2084                        val=eval(['patch2.' fieldnames{ilist}]);
2085                        if ischar(val)
2086                            [CivAllxml,uid_t]=add(CivAllxml,uid_patch2,'element',fieldnames{ilist});
2087                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
2088                        end
2089                    end
2090                    CivAllCmd=[CivAllCmd ' patch2 '];
2091            end
2092        end
2093 
2094        switch CivMode
2095            case {'CivX','CivAll'}
2096                if isequal(CivMode,'CivAll')
2097                    save(CivAllxml,[OutputFile '.xml']);
2098                    cmd=[cmd sparam.CivBin ' -f ' OutputFile '.xml '  CivAllCmd ' >' OutputFile '.log' '\n'];
2099                end
2100               
2101                % create the .bat file used in run or batch
2102                filename_bat=[OutputFile '.bat'];
2103                [fid,message]=fopen(filename_bat,'w');
2104                if isequal(fid,-1)
2105                    msgbox_uvmat('ERROR', ['creation of .bat file: ' message])
2106                    return
2107                end
2108                fprintf(fid,cmd);
2109                fclose(fid);
2110               
2111                batch_file_list{length(batch_file_list)+1}=filename_bat;
2112               
2113%                 if batch
2114%                     switch batch_mode
2115%                         case 'sge'                           
2116%                             display(['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2117%                             eval(  ['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2118%                         case 'oar'
2119% %                             eval(  ['!chmod +x ' filename_bat]);
2120% %                             %eval(  ['!oarsub -n CIVX -l /core=1,walltime=00:10:00  ' filename_bat]);                   
2121% %                             eval(  ['!oarsub -n CIVX -l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:10:00"   ' filename_bat]);
2122%
2123%                     cmd_str=['sh ' filename_bat];
2124%                     super_cmd{length(super_cmd)+1}=cmd_str;
2125%                           
2126%                     end
2127%                 else
2128%                     %% to lauch the jobs locally :
2129%                     if(isunix)
2130%                         cmd_str=['. ' filename_bat];
2131%                     else %case of Windows
2132%                         cmd_str=['@call "' regexprep(filename_bat,'\\','\\\\') '"'];
2133%                     end
2134%                     super_cmd=[super_cmd cmd_str '\n'];
2135%                     disp(cmd_str);
2136%                 end
2137            case 'Matlab'
2138                drawnow
2139                if box_test(1)==1
2140                    Param.Civ1=param.civ1;
2141                end
2142                if box_test(2)==1
2143                    fix1.WarnFlags=[];
2144                    if get(handles.vec_Fmin2,'Value')
2145                        fix1.WarnFlags=[fix1.WarnFlags -2];
2146                    end
2147                    if get(handles.vec_F3,'Value')
2148                        fix1.WarnFlags=[fix1.WarnFlags 3];
2149                    end
2150                    fix1.LowerBoundCorr=thresh_vecC1;
2151                    if get(handles.inf_sup1,'Value')
2152                        fix1.UppperBoundVel=thresh_vel1;
2153                    else
2154                        fix1.LowerBoundVel=thresh_vel1;
2155                    end
2156                    if get(handles.get_mask_fix1,'Value')
2157                        fix1.MaskName=maskname;
2158                    end
2159                    Param.Fix1=fix1;
2160                end
2161                if box_test(3)==1
2162                    if strcmp(compare,'stereo PIV')
2163                        filebase_A=filecell.filebase;
2164                        [pp,ff]=fileparts(filebase_A);
2165                        filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
2166                        RUN_STLIN(filecell.ncA.civ1{ifile,j},filecell.nc.civ1{ifile,j},'civ1',filecell.st{ifile,j},...
2167                            str2num(nx_patch1),str2num(ny_patch1),str2num(thresh_patch1),[filebase_A '.xml'],[filebase_B '.xml'])
2168                    else
2169                        Param.Patch1.Rho=rho_patch1;
2170                        Param.Patch1.Threshold=thresh_patch1;
2171                        Param.Patch1.SubDomain=subdomain_patch1;
2172                    end
2173                end
2174                if box_test(4)==1
2175                    Param.Civ2=param.civ2;
2176                end
2177                if box_test(5)==1
2178                    fix2.WarnFlags=[];
2179                    if get(handles.vec_Fmin2_2,'Value')
2180                        fix2.WarnFlags=[fix2.WarnFlags -2];
2181                    end
2182                    if get(handles.vec_F4,'Value')
2183                        fix2.WarnFlags=[fix2.WarnFlags 4];
2184                    end
2185                    if get(handles.vec_F3_2,'Value')
2186                        fix2.WarnFlags=[fix2.WarnFlags 3];
2187                    end
2188                    fix2.LowerBoundCorr=thresh_vec2C;
2189                    if get(handles.inf_sup2,'Value')
2190                        fix2.UppperBoundVel=thresh_vel2;
2191                    else
2192                        fix2.LowerBoundVel=thresh_vel2;
2193                    end
2194                    if get(handles.get_mask_fix2,'Value')
2195                        fix2.MaskName=maskname;
2196                    end
2197                    Param.Fix2=fix2;
2198                end
2199                if box_test(6)==1
2200                    if strcmp(compare,'stereo PIV')
2201                        filebase_A=filecell.filebase;
2202                        [pp,ff]=fileparts(filebase_A);
2203                        filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
2204                        RUN_STLIN(filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},'civ2',filecell.st{ifile,j},...
2205                            str2num(nx_patch2),str2num(ny_patch2),str2num(thresh_patch2),[filebase_A '.xml'],[filebase_B '.xml'])
2206                    else
2207                        Param.Patch2.Rho=rho_patch2;
2208                        Param.Patch2.Threshold=thresh_patch2;
2209                        Param.Patch2.SubDomain=subdomain_patch2;
2210                    end
2211                end
2212                if ~strcmp(compare,'stereo PIV')
2213                    [Data,erromsg]=civ_uvmat(Param,filecell.nc.civ1{ifile,j});
2214                    if isempty(errormsg)
2215                        display([filecell.nc.civ1{ifile,j} ' written'])
2216                    else
2217                        msgbox_uvmat('ERROR',errormsg)
2218                    end
2219
2220                end
2221        end
2222    end
2223end
2224
2225if batch
2226    switch batch_mode                       
2227        case 'sge'
2228            for p=1:length(batch_file_list)
2229                cmd=['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' batch_file_list{p}];
2230                display(cmd);eval(cmd);
2231            end
2232        case 'sge'
2233            [s,w]=unix('qstat -q civ.q|grep job_| wc -l'); %check the waiting list (command unix)
2234            w(end)=[];
2235            str_displ={[w ' jobs in the waiting list'];...
2236                '***********************';...
2237                'JOBS PRIORITY POLICY';...
2238                '- urgent = less than 100 images pairs';...
2239                '- normal = during the experiments';...
2240                '- low = post processing';...
2241                '***********************';...
2242                'Select a priority:'};
2243            str={'urgent';'normal';'low'};
2244            [ind_answer,v] = listdlg('PromptString',str_displ,...
2245                'SelectionMode','single',...
2246                'ListString',str,'ListSize',[200 100],'Name','job priority','InitialValue',3);
2247            pvalue=num2str((1-ind_answer)*500); %
2248            if isequal(v,0) % to handle Cancel button and figure close
2249                errormsg='job submission cancelled';
2250                return
2251            end
2252        case 'oar'
2253            for p=0:floor(length(batch_file_list)/6);               
2254                filename_batch_group=fullfile(Rootbat,['job_list_' num2str(p) '.bat']);
2255                fid=fopen(filename_batch_group,'w');
2256                if fid==-1
2257                    msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2258                    return
2259                end
2260                if p==floor(length(batch_file_list)/6)
2261                    kmax=mod(length(batch_file_list),6);
2262                else
2263                    kmax=6;
2264                end
2265                for k=1:kmax
2266                    fprintf(fid,['sh ' batch_file_list{p*6+k} '\n']);
2267                end
2268                fclose(fid);
2269                system(['chmod +x ' filename_batch_group]);
2270                eval(  ['!oarsub -n CIVX -q nicejob -l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:60:00"   ' filename_batch_group]);
2271            end
2272        case 'oar_new' % to be develloped with Patrick Begou
2273                filename_joblist=fullfile(Rootbat,'job_list.txt');
2274                fid=fopen(filename_superbat,'w');
2275                if fid==-1
2276                    msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2277                    return
2278                end
2279                for p=1:length(batch_file_list)
2280                    fprintf(fid,[batch_file_list{p} '\n']);
2281                end
2282                fclose(fid);
2283                walltime=datestr(length(super_cmd)*10/24/60,13);
2284                eval(  ['!oarsub -n CIVX -q nicejob -l "/core=1+{type = ''smalljob''}/licence=1,walltime=' walltime '"   ' filename_superbat]);
2285    end
2286else
2287    if ~isequal(CivMode,'Matlab')
2288        filename_superbat=fullfile(Rootbat,'job_list.bat');
2289        fid=fopen(filename_superbat,'w');
2290        if fid==-1
2291            msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2292            return
2293        end
2294        for p=1:length(batch_file_list)
2295            if isunix
2296                fprintf(fid,['sh ' batch_file_list{p} '\n']);
2297            else
2298                fprintf(fid,['@call "' regexprep(filename_bat,'\\','\\\\') '"' '\n']);
2299            end
2300        end
2301        fclose(fid);
2302        if(isunix)
2303            system(['chmod +x ' filename_superbat]);
2304        end
2305        system([filename_superbat ' &']);% execute main commmand
2306    end
2307   
2308end
2309
2310
2311%% save interface state
2312if isfield(filecell,'nc')
2313    if isfield(filecell.nc,'civ2')
2314        fileresu=filecell.nc.civ2{1,1};
2315    else
2316        fileresu=filecell.nc.civ1{1,1};
2317    end
2318end
2319[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2320namedoc=fullfile(RootPath,subdir,RootFile);
2321detect=1;
2322while detect==1
2323    namefigfull=[namedoc '.fig'];
2324    hh=dir(namefigfull);
2325    if ~isempty(hh)
2326        detect=1;
2327        namedoc=[namedoc '.0'];
2328    else
2329        detect=0;
2330    end
2331end
2332saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2333
2334%Save info in personal profile (initiate browser next time) TODO
2335MenuFile={};
2336dir_perso=prefdir;
2337profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2338if exist(profil_perso,'file')
2339    hh=load (profil_perso);
2340      if isfield(hh,'MenuFile')
2341          MenuFile=hh.MenuFile;
2342      end
2343      MenuFile=[filecell.ima1.civ1(1,1); MenuFile];
2344      save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
2345else
2346    MenuFile=filecell.ima1.civ1(1,1);
2347    save (profil_perso,'MenuFile')
2348end
2349
2350
2351%------------------------------------------------------------------------
2352% --- determine the list of reference indices of processing file
2353function [ref_i,ref_j,errormsg]=find_ref_indices(handles)
2354%------------------------------------------------------------------------
2355errormsg=''; %default error message
2356first_i=str2double(get(handles.first_i,'String'));%first index i
2357last_i=str2double(get(handles.last_i,'String'));%last index i
2358incr_i=str2double(get(handles.incr_i,'String'));% increment
2359if isequal(get(handles.first_j,'Visible'),'on')
2360    first_j=str2double(get(handles.first_j,'String'));%first index j
2361    last_j=str2double(get(handles.last_j,'String'));%last index j
2362    incr_j=str2double(get(handles.incr_j,'String'));% increment
2363else
2364    first_j=1;
2365    last_j=1;
2366    incr_j=1;
2367end
2368ref_i=first_i:incr_i:last_i;% list of i indices (reference values for each pair)
2369ref_j=first_j:incr_j:last_j;% list of j indices (reference values for each pair)
2370if isnan(first_i)||isnan(first_j)
2371    errormsg='first field number not defined';
2372elseif isnan(last_i)||isnan(last_j)
2373    errormsg='last field number not defined';
2374elseif isnan(incr_i)||isnan(incr_j)
2375    errormsg='increment in field number not defined';
2376elseif last_i < first_i || last_j < first_j
2377    errormsg='last field number must be larger than the first one';
2378end
2379
2380%------------------------------------------------------------------------
2381% --- determine the list of filenames and indices needed for launch_job
2382function [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2,compare]=...
2383    set_civ_filenames(handles,ref_i,ref_j,box_test)
2384%------------------------------------------------------------------------
2385filecell=[];%default
2386
2387%% get the root names nomenclature and numbers
2388filebase=get(handles.RootName,'String');
2389
2390if isempty(filebase)||isequal(filebase,'')
2391    msgbox_uvmat('ERROR','no input files')
2392    return
2393end
2394
2395%filebase=regexprep(filebase,'\.fsnet','fsnet');% temporary fix for cluster Coriolis
2396filecell.filebase=filebase;
2397
2398browse=get(handles.RootName,'UserData');
2399compare_list=get(handles.compare,'String');
2400val=get(handles.compare,'Value');
2401compare=compare_list{val};
2402if strcmp(compare,'displacement')
2403    mode='displacement';
2404else
2405    mode_list=get(handles.CivMode,'String');
2406    mode_value=get(handles.CivMode,'Value');
2407    mode=mode_list{mode_value};
2408end
2409%time=get(handles.RootName,'UserData'); %get the set of times
2410ext_ima=get(handles.ImaExt,'String');
2411nom_type_nc=browse.nom_type_nc;
2412if isfield(browse,'nom_type_ima')
2413    nom_type_ima2=browse.nom_type_ima;
2414end
2415if isempty(nom_type_ima2),nom_type_ima2='1';end; %default
2416if isempty(nom_type_nc),nom_type_nc='_i1-i2';end; %default
2417[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2418    find_pair_indices(handles,ref_i,ref_j,mode);
2419%determine the new filebase for 'displacement' CivMode (comparison of two series)
2420filebase_B=filebase;% root name of the second field series for stereo
2421if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2422%     test_disp=1;
2423    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2424    [Path2,Name2]=fileparts(filebase_B);
2425    Path1=Path2;
2426    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2427    filebase_A=fullfile(Path1,Name1);
2428    if length(Name1)>6
2429        Name1=Name1(end-5:end);
2430    end
2431    if length(Name2)>6
2432        Name2=Name2(end-5:end);
2433    end
2434    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2435else
2436%     test_disp=0;
2437    filebase_A=filebase;
2438    nom_type_ima1=nom_type_ima2;
2439    filebase_AB=filebase;
2440end
2441if strcmp(compare,'displacement')
2442    filebase_ima1=filebase_A;
2443    filebase_ima2=filebase_B;
2444    filebase_nc=filebase_AB; %root name for the result of civ2
2445else
2446    filebase_ima1=filebase_B;
2447    filebase_ima2=filebase_B;
2448    filebase_nc=filebase_B;
2449end
2450
2451%determine reference files for fix:
2452file_ref_fix1={};%default
2453file_ref_fix2={};
2454nbfield=length(num1_civ1);
2455nbslice=length(num_a_civ1);
2456if box_test(2)==1% fix1 performed
2457    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2458    if ~isempty(ref)
2459        first_i=str2double(get(handles.first_i,'String'));
2460        last_i=str2double(get(handles.last_i,'String'));
2461        incr_i=str2double(get(handles.incr_i,'String'));
2462        first_j=str2double(get(handles.first_j,'String'));
2463        last_j=str2double(get(handles.last_j,'String'));
2464        incr_j=str2double(get(handles.incr_j,'String'));
2465        num_i_ref=first_i:incr_i:last_i;
2466        num_j_ref=first_j:incr_j:last_j;
2467        if isequal(mode,'displacement')
2468            num_i1=num_i_ref;
2469            num_i2=num_i_ref;
2470            num_j1=num_j_ref;
2471            num_j2=num_j_ref;
2472        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2473            num_i1=num_i_ref;
2474            num_i2=num_i1;
2475            num_j1=ref.num_a*ones(size(num_i_ref));
2476            num_j2=ref.num_b*ones(size(num_i_ref));
2477        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2478            delta1=floor((ref.num2-ref.num1)/2);
2479            delta2=ceil((ref.num2-ref.num1)/2);
2480            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2481            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2482            if isempty(ref.num_a)
2483                ref.num_a=1;
2484            end
2485            num_j1=ref.num_a*ones(size(num_i1));
2486            num_j2=num_j1;
2487        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2488            delta1=floor((ref.num_b-ref.num_a)/2);
2489            delta2=ceil((ref.num_b-ref.num_a)/2);
2490            num_i1=ref.num1*ones(size(num_i_ref));
2491            num_i2=num_i1;
2492            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2493            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2494        end
2495        for ifile=1:nbfield
2496            for j=1:nbslice
2497                file_ref=name_generator(ref.filebase,num_i1(ifile),num_j1(j),'.nc',ref.nom_type,1,num_i2(ifile),num_j2(j),ref.subdir);%
2498                file_ref_fix1(ifile,j)={file_ref};
2499                if ~exist(file_ref,'file')
2500                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2501                    filecell=[];
2502                    return
2503                end
2504            end
2505        end
2506    end
2507end
2508
2509%determine reference files for fix2:
2510if box_test(5)==1% fix2 performed
2511    ref=get(handles.ref_fix2,'UserData');
2512    if ~isempty(ref)
2513        first_i=str2double(get(handles.first_i,'String'));
2514        last_i=str2double(get(handles.last_i,'String'));
2515        incr_i=str2double(get(handles.incr_i,'String'));
2516        first_j=str2double(get(handles.first_j,'String'));
2517        last_j=str2double(get(handles.last_j,'String'));
2518        incr_j=str2double(get(handles.incr_j,'String'));
2519        num_i_ref=first_i:incr_i:last_i;
2520        num_j_ref=first_j:incr_j:last_j;
2521        if isequal(mode,'displacement')
2522            num_i1=num_i_ref;
2523            num_i2=num_i_ref;
2524            num_j1=num_j_ref;
2525            num_j2=num_j_ref;
2526        elseif isequal(mode,'pair j1-j2')
2527            num_i1=num_i_ref;
2528            num_i2=num_i1;
2529            num_j1=ref.num_a;
2530            num_j2=ref.num_b;
2531        elseif isequal(mode,'series(Di)')
2532            delta1=floor((ref.num2-ref.num1)/2);
2533            delta2=ceil((ref.num2-ref.num1)/2);
2534            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2535            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2536            num_j1=ref.num_a*ones(size(num_i1));
2537            num_j2=num_j1;
2538        elseif isequal(mode,'series(Dj)')
2539            delta1=floor((ref.num_b-ref.num_a)/2);
2540            delta2=ceil((ref.num_b-ref.num_a)/2);
2541            num_i1=ref.num1*ones(size(num_i_ref));
2542            num_i2=num_i1;
2543            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2544            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2545        end
2546        for ifile=1:nbfield
2547            for j=1:nbslice
2548                file_ref=name_generator(ref.filebase,num_i1(ifile),num_j1(j),'.nc',ref.nom_type,1,num_i2(ifile),num_j2(j),ref.subdir);%
2549                file_ref_fix2(ifile,j)={file_ref};
2550                if ~exist(file_ref,'file')
2551                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2552                    filecell={};
2553                    return
2554                end
2555            end
2556        end
2557    end
2558end
2559
2560%check dir
2561subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2562subdir_civ2=get(handles.subdir_civ2,'String');
2563if isequal(subdir_civ1,''),subdir_civ1='CIV'; end% put default subdir
2564if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2565% currentdir=pwd;%store the current working directory
2566[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2567if ~exist(Path_ima,'dir')
2568    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2569    filecell={};
2570    return
2571end
2572[xx,message]=fileattrib(Path_ima);
2573if ~isempty(message) && ~isequal(message.UserWrite,1)
2574    msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2575    filecell={};
2576%     cd(currentdir);
2577    return
2578end
2579
2580%check the existence of the netcdf and image files involved
2581% %%%%%%%%%%%%  case CIV1 activated   %%%%%%%%%%%%%
2582if box_test(1)==1;
2583    detect=1;
2584    vers=0;
2585    subdir_civ1_new=subdir_civ1;
2586    while detect==1 %create a new subdir if the netcdf files already exist
2587        for ifile=1:nbfield
2588            for j=1:nbslice
2589                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1_new);
2590                detect=exist(filename,'file')==2;
2591                if detect% if a netcdf file already exists
2592                    indstr=regexp(subdir_civ1_new,'\D');
2593                    if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2594                        vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2595                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2596                    else
2597                        vers=vers+1;
2598                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) '_' num2str(vers)];       
2599                    end
2600                    subdir_civ2=subdir_civ1_new;
2601                    break
2602                end
2603                filecell.nc.civ1(ifile,j)={filename};
2604            end
2605            if detect% if a netcdf file already exists
2606                break
2607            end
2608        end
2609 
2610        %create the new subdir_civ1
2611        if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2612%             cd(Path_ima);         
2613            [xx,msg1]=mkdir(fullfile(Path_ima,subdir_civ1_new));
2614
2615            if ~strcmp(msg1,'')
2616                msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])%error message for directory creation
2617                filecell={};
2618                return
2619            elseif isunix         
2620                [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2621                if ~strcmp(msg2,'')
2622                    msgbox_uvmat('ERROR',['pb of permission for  ' fullfile(Path_ima,subdir_civ1_new) ': ' msg2])%error message for directory creation
2623                    filecell={};
2624                    return
2625                end
2626            end
2627%             cd(currentdir);
2628        end
2629        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2630            for ifile=1:nbfield
2631                for j=1:nbslice
2632                    filename=name_generator(filebase_A,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1_new);%
2633                    detect=exist(filename,'file')==2;
2634                    if detect% if a netcdf file already exists
2635                       indstr=regexp(subdir_civ1_new,'\D');
2636                       if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2637                           vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2638                           subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2639                       else
2640                           vers=vers+1;
2641                           subdir_civ1_new=[subdir_civ1_new '_' num2str(vers)];
2642                       end
2643                       subdir_civ2=subdir_civ1;
2644                       break
2645                    end
2646                    filecell.ncA.civ1(ifile,j)={filename};
2647                end
2648                if detect% if a netcdf file already exists
2649                    break
2650                end
2651            end
2652            %create the new subdir_civ1
2653            if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2654%                    cd(Path_ima);         
2655                [xx,msg1]=mkdir(fullfile(Path_ima,subdir_civ1_new));
2656%                             cd(currentdir);
2657                if ~strcmp(msg1,'')
2658                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])
2659%                     cd(currentdir)
2660                    filecell={};
2661                    return
2662                else
2663                    [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2664                    if ~strcmp(msg2,'')
2665                        msgbox_uvmat('ERROR',['pb of permission for ' subdir_civ1_new ': ' msg2])%error message for directory creation
2666%                         cd(currentdir)
2667                        filecell={};
2668                        return
2669                    end
2670                end
2671            end
2672        end
2673    end
2674    subdir_civ1=subdir_civ1_new;
2675    % get image names
2676    for ifile=1:nbfield
2677        for j=1:nbslice
2678            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2679            idetect(j)=exist(filename,'file')==2;
2680            filecell.ima1.civ1(ifile,j)={filename}; %first image
2681            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2682            idetect_1(j)=exist(filename,'file')==2;
2683            filecell.ima2.civ1(ifile,j)={filename};%second image
2684        end
2685        [idetectmin,indexj]=min(idetect);
2686        if idetectmin==0,
2687            msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
2688            filecell={};
2689           % cd(currentdir)
2690            return
2691        end
2692        [idetectmin,indexj]=min(idetect_1);
2693        if idetectmin==0,
2694            msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
2695            filecell={};
2696            %cd(currentdir)
2697            return
2698        end
2699    end
2700    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
2701        for ifile=1:nbfield
2702            for j=1:nbslice
2703                filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2704                idetect(j)=exist(filename,'file')==2;
2705                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
2706                filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2707                idetect_1(j)=exist(filename,'file')==2;
2708                filecell.imaA2.civ1(ifile,j)={filename};%second image
2709            end
2710            [idetectmin,indexj]=min(idetect);
2711            if idetectmin==0,
2712                msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
2713                filecell={};
2714               % cd(currentdir)
2715                return
2716            end
2717            [idetectmin,indexj]=min(idetect_1);
2718            if idetectmin==0,
2719                msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
2720                filecell={};
2721               % cd(currentdir)
2722                return
2723            end
2724        end
2725    end
2726   
2727    %%%%%%%%%%%%%  fix1 or patch1 activated but no civ1   %%%%%%%%%%%%%
2728elseif (box_test(2)==1 || box_test(3)==1);
2729    for ifile=1:nbfield
2730        for j=1:nbslice
2731            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2732                nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2733            detect=exist(filename,'file')==2;
2734            if detect==0
2735                msgbox_uvmat('ERROR',[filename ' not found'])
2736                filecell={};
2737               % cd(currentdir)
2738                return
2739            end
2740            filecell.nc.civ1(ifile,j)={filename};
2741        end
2742    end
2743    if strcmp(compare,'stereo PIV')
2744        for ifile=1:nbfield
2745            for j=1:nbslice
2746                filename=name_generator(filebase_A,num1_civ1(ifile),num_a_civ1(j),'.nc',nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2747                filecell.ncA.civ1(ifile,j)={filename};
2748                if ~exist(filename,'file')
2749                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2750                    set(handles.RUN, 'Enable','On')
2751                    set(handles.RUN,'BackgroundColor',[1 0 0])
2752                    filecell={};
2753                    %cd(currentdir)
2754                    return
2755                end
2756            end
2757        end
2758    end
2759end
2760
2761%%%%%%%%%%%%%  if civ2 performed with pairs different than civ1  %%%%%%%%%%%%%
2762testdiff=0;
2763if (box_test(4)==1)&&...
2764        ((get(handles.list_pair_civ1,'Value')~=get(handles.list_pair_civ2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
2765    testdiff=1;
2766    detect=1;
2767    vers=0;
2768    subdir_civ2_new=subdir_civ2;
2769    while detect==1 %create a new subdir if the netcdf files already exist
2770        for ifile=1:nbfield
2771            for j=1:nbslice
2772                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2_new);%
2773                detect=exist(filename,'file')==2;
2774                if detect% if a netcdf file already exists
2775                    indstr=regexp(subdir_civ2,'\D');
2776                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2777                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2778                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2779                    else
2780                        vers=vers+1;
2781                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2782                    end
2783                    break
2784                end
2785                filecell.nc.civ2(ifile,j)={filename};
2786            end
2787            if detect% if a netcdf file already exists
2788                break
2789            end
2790        end
2791        %create the new subdir_civ2_new
2792        if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2793            [xx,m2]=mkdir(fullfile(Path_ima,subdir_civ2_new));
2794            [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2795            if ~isequal(m2,'')
2796                msgbox_uvmat('ERROR',['cannot create ' fullfile(Path_ima,subdir_civ2_new) ': ' m2])
2797                filecell={};
2798               % cd(currentdir)
2799                return
2800            end
2801        end
2802        if strcmp(compare,'stereo PIV')%check second nc series
2803            for ifile=1:nbfield
2804                for j=1:nbslice
2805                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2806                        nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2_new);%
2807                    detect=exist(filename,'file')==2;
2808                    if detect% if a netcdf file already exists
2809                        indstr=regexp(subdir_civ2,'\D');
2810                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2811                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2812                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2813                        else
2814                           vers=vers+1;
2815                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2816                        end
2817                        break
2818                    end
2819                    filecell.ncA.civ2(ifile,j)={filename};
2820                end
2821                if detect% if a netcdf file already exists
2822                    break
2823                end
2824            end
2825            subdir_civ2=subdir_civ2_new;
2826            %create the new subdir_civ1
2827            if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2828                [xx,m2]=mkdir(subdir_civ2_new);
2829                 [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2830                if ~isequal(m2,'')
2831                    msgbox_uvmat('ERROR', ['cannot create ' fullfile(Path_ima,subdir_civ2_new) ': ' m2])%error message for directory creation
2832                  %  cd(currentdir)
2833                    filecell={};
2834                    return
2835                end
2836            end
2837        end
2838    end
2839    subdir_civ2=subdir_civ2_new;
2840end
2841%cd(currentdir);%come back to the current working directory
2842
2843%%%%%%%%%%%%%  if civ2 results are obtained or used  %%%%%%%%%%%%%
2844if box_test(4)==1 || box_test(5)==1 || box_test(6)==1 %civ2
2845    %check source netcdf file of civ1 estimates
2846    if box_test(1)==0; %no civ1 performed
2847        for ifile=1:nbfield
2848            for j=1:nbslice
2849                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2850                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2851                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
2852                if ~exist(filename,'file')
2853                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2854                    filecell={};
2855                    return
2856                end
2857                if ~testdiff % civ2 or patch2 are written in the same file as civ1
2858                    if box_test(4)==0 ; %check the existence of civ2 if it is not calculated
2859                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2860                        if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
2861                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2862                            filecell=[];
2863                            return
2864                        elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2865                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2866                            filecell=[];
2867                            return
2868                        end
2869                    elseif box_test(3)==0; %check the existence of patch if it is not calculated
2870                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','patch')
2871                        if ~isempty(Data.CivStage)
2872                            if Data.CivStage<3 %test for civ files
2873                                msgbox_uvmat('ERROR',['no patch data in ' filename])
2874                                filecell=[];
2875                                return
2876                            end
2877                        elseif isempty(Data.patch)||isequal(Data.patch,0)
2878                            msgbox_uvmat('ERROR',['no patch data in ' filename])
2879                            filecell=[];
2880                            return
2881                        end
2882                    end
2883                end
2884            end
2885        end
2886        if strcmp(compare,'stereo PIV')
2887            for ifile=1:nbfield
2888                for j=1:nbslice
2889                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2890                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2891                    filecell.ncA.civ2(ifile,j)={filename};
2892                    if ~exist(filename,'file')
2893                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2894                        set(handles.RUN, 'Enable','On')
2895                        set(handles.RUN,'BackgroundColor',[1 0 0])
2896                        return
2897                    end
2898                end
2899            end
2900        end
2901    end
2902   
2903    detect=1;
2904    %     while detect==1%creates a new subdir if the netcdf files already contain civ2 data
2905    for ifile=1:nbfield
2906        for j=1:nbslice
2907            filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2908                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
2909            detect=exist(filename,'file')==2;
2910            filecell.nc.civ2(ifile,j)={filename};
2911        end
2912    end
2913    %get first image names for civ2
2914    if box_test(1)==1 & isequal(num1_civ1,num1_civ2) & isequal(num_a_civ1,num_a_civ2)
2915        filecell.ima1.civ2=filecell.ima1.civ1;
2916    elseif box_test(4)==1
2917        for ifile=1:nbfield
2918            for j=1:nbslice
2919                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
2920                idetect_2(j)=exist(filename,'file')==2;
2921                filecell.ima1.civ2(ifile,j)={filename};%first image
2922            end
2923            [idetectmin,indexj]=min(idetect_2);
2924            if idetectmin==0,
2925                msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
2926                filecell=[];
2927                return
2928            end
2929        end
2930    end
2931   
2932    %get second image names for civ2
2933    if box_test(1)==1 & isequal(num2_civ1,num2_civ2) & isequal(num_b_civ1,num_b_civ2)
2934        filecell.ima2.civ2=filecell.ima2.civ1;
2935    elseif box_test(4)==1
2936        for ifile=1:nbfield
2937            for j=1:nbslice
2938                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
2939                idetect_3(j)=exist(filename,'file')==2;
2940                filecell.ima2.civ2(ifile,j)={filename};%first image
2941            end
2942            [idetectmin,indexj]=min(idetect_3);
2943            if idetectmin==0,
2944                msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
2945                filecell=[];
2946                return
2947            end
2948        end
2949    end
2950end
2951if (box_test(5)==1 || box_test(6)==1 ) && box_test(4)==0  % need to read an existing netcdf civ2 file
2952    if ~testdiff
2953        filecell.nc.civ2=filecell.nc.civ1;% file already checked
2954    else     % check the civ2 files
2955        for ifile=1:nbfield
2956            for j=1:nbslice
2957                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2958                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2959                filecell.nc.civ2(ifile,j)={filename};
2960                if ~exist(filename,'file')
2961                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2962                    filecell=[];
2963                    return
2964                else
2965                    Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2966                    if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
2967                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2968                            filecell=[];
2969                            return
2970                    elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2971                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2972                        filecell=[];
2973                        return
2974                    end
2975                end
2976            end
2977        end
2978    end
2979end
2980
2981%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
2982if strcmp(compare,'stereo PIV')
2983    if  box_test(3)==1 && isequal(get(handles.test_stereo1,'Value'),1)
2984        for ifile=1:nbfield
2985            for j=1:nbslice
2986                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2987                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2988                filecell.st(ifile,j)={filename};
2989            end
2990        end
2991    end
2992    if  box_test(6)==1 && isequal(get(handles.check_Stereo,'Value'),1)
2993        for ifile=1:nbfield
2994            for j=1:nbslice
2995                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2996                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2997                filecell.st(ifile,j)={filename};
2998            end
2999        end
3000    end
3001end
3002set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
3003set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
3004browse.nom_type_nc=nom_type_nc;
3005set(handles.RootName,'UserData',browse); %update the nomenclature type for uvmat
3006
3007
3008%COPY IMAGES TO THE FORMAT .png IF NEEDED
3009if isequal(nom_type_ima1,'*')%case of movie files
3010    nom_type_imanew1='_i';
3011else
3012    nom_type_imanew1=nom_type_ima1;
3013end
3014if isequal(nom_type_ima2,'*')%case of movie files
3015    nom_type_imanew2='_i';
3016else
3017    nom_type_imanew2=nom_type_ima2;
3018end
3019if ~isequal(ext_ima,'.png')
3020    %%type of image file
3021    type_ima1='none';%default
3022    movieobject1=[];%default
3023    if strcmpi(ext_ima,'.avi')
3024        hhh=which('mmreader');
3025        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3026            type_ima1='movie';
3027            movieobject1=mmreader([filebase_ima2 ext_ima]);
3028        else
3029            type_ima1='avi';
3030        end
3031    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
3032        form=imformats(ext_ima(2:end));
3033        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3034            if isequal(nom_type_ima1,'*');
3035                type_ima1='multimage';%image series in a single image file
3036            else
3037                type_ima1='image';
3038            end
3039        end
3040    end
3041    type_ima2='none';%default
3042    movieobject2=[];
3043    if strcmpi(ext_ima,'.avi')
3044        hhh=which('mmreader');
3045        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3046            type_ima2='movie';
3047            movieobject2=mmreader([filebase_ima2 ext_ima]);
3048        else
3049            type_ima2='avi';
3050        end
3051    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
3052        form=imformats(ext_ima(2:end));
3053        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3054            if isequal(nom_type_ima1,'*');
3055                type_ima2='multimage';%image series in a single image file
3056            else
3057                type_ima2='image';
3058            end
3059        end
3060    end
3061    %npxy=get(handles.ImaExt,'UserData');
3062    % %     if numel(npxy)<2
3063    %
3064    %         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
3065    %         A=imread(filename);
3066    %         npxy=size(A);
3067    % %     end
3068    %     npy=npxy(1);
3069    %     npx=npxy(2);
3070    if box_test(1)==1 %if civ1 is performed
3071        h = waitbar(0,'copy images to the .png format for civ1');% display a wait bar
3072        for ifile=1:nbfield
3073            waitbar(ifile/nbfield);
3074            for j=1:nbslice
3075                filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
3076                if ~exist(filename,'file')
3077                    A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
3078                    imwrite(A,filename,'BitDepth',16);
3079                end
3080                filecell.ima1.civ1(ifile,j)={filename};
3081                filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
3082                if ~exist(filename,'file')
3083                    A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
3084                    imwrite(A,filename,'BitDepth',16);
3085                end
3086                filecell.ima2.civ1(ifile,j)={filename};
3087            end
3088        end
3089        close(h)
3090    end
3091    if box_test(4)==1 %if civ2 is performed
3092        h = waitbar(0,'copy images to the .png format for civ2');% display a wait bar
3093        for ifile=1:nbfield
3094            waitbar(ifile/nbfield);
3095            for j=1:nbslice
3096                filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3097                if ~exist(filename,'file')
3098                    A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
3099                    imwrite(A,filename,'BitDepth',16);
3100                end
3101                filecell.ima1.civ2(ifile,j)={filename};
3102                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3103                if ~exist(filename,'file')
3104                    A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
3105                    imwrite(A,filename,'BitDepth',16);
3106                end
3107                filecell.ima2.civ2(ifile,j)={filename};
3108            end
3109        end
3110        close(h);
3111    end
3112end
3113
3114%------------------------------------------------------------------------
3115% --- determine the list of index pairs of processing file
3116function [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
3117    find_pair_indices(handles,ref_i,ref_j,mode)
3118%------------------------------------------------------------------------
3119
3120list_civ1=get(handles.list_pair_civ1,'String');
3121index_civ1=get(handles.list_pair_civ1,'Value');
3122str_civ1=list_civ1{index_civ1};%string defining the image pairs for civ1
3123if isempty(str_civ1)||isequal(str_civ1,'')
3124    msgbox_uvmat('ERROR','no image pair selected for civ1')
3125    return
3126end
3127list_civ2=get(handles.list_pair_civ2,'String');
3128index_civ2=get(handles.list_pair_civ2,'Value');
3129if index_civ2>length(list_civ2)
3130    list_civ2=list_civ1;
3131    index_civ2=index_civ1;
3132end
3133str_civ2=list_civ2{index_civ2};%string defining the image pairs for civ2
3134
3135if isequal (mode,'series(Di)')
3136    lastfield=str2double(get(handles.nb_field,'String'));
3137    num1_civ1=ref_i-floor(index_civ1/2)*ones(size(ref_i));% set of first image numbers
3138    num2_civ1=ref_i+ceil(index_civ1/2)*ones(size(ref_i));
3139    num_a_civ1=ref_j;
3140    num_b_civ1=ref_j;
3141    num1_civ2=ref_i-floor(index_civ2/2)*ones(size(ref_i));
3142    num2_civ2=ref_i+ceil(index_civ2/2)*ones(size(ref_i));
3143    num_a_civ2=ref_j;
3144    num_b_civ2=ref_j;   
3145   
3146    % adjust the first and last field number
3147    lastfield=str2double(get(handles.nb_field,'String'));
3148    if isnan(lastfield)
3149        indsel=find((num1_civ1 >= 1)&(num1_civ2 >= 1));
3150    else
3151        indsel=find((num2_civ1 <= lastfield)&(num2_civ2 <= lastfield)&(num1_civ1 >= 1)&(num1_civ2 >= 1));
3152    end
3153    if length(indsel)>=1
3154        firstind=indsel(1);
3155        lastind=indsel(end);
3156        set(handles.first_i,'String',num2str(ref_i(firstind)))%update the display of first and last fields
3157        set(handles.last_i,'String',num2str(ref_i(lastind)))
3158        ref_i=ref_i(indsel);
3159        num1_civ1=num1_civ1(indsel);
3160        num1_civ2=num1_civ2(indsel);
3161        num2_civ1=num2_civ1(indsel);
3162        num2_civ2=num2_civ2(indsel);
3163    end
3164elseif isequal (mode,'series(Dj)')
3165    lastfield_j=str2double(get(handles.nb_field2,'String'));
3166    num1_civ1=ref_i;% set of first image numbers
3167    num2_civ1=ref_i;
3168    num_a_civ1=ref_j-floor(index_civ1/2)*ones(size(ref_j));
3169    num_b_civ1=ref_j+ceil(index_civ1/2)*ones(size(ref_j));
3170    num1_civ2=ref_i;
3171    num2_civ2=ref_i;
3172    num_a_civ2=ref_j-floor(index_civ2/2)*ones(size(ref_j));
3173    num_b_civ2=ref_j+ceil(index_civ2/2)*ones(size(ref_j));
3174    % adjust the first and last field number
3175    if isnan(lastfield_j)
3176        indsel=find((num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3177    else
3178        indsel=find((num_b_civ1 <= lastfield_j)&(num_b_civ2 <= lastfield_j)&(num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3179    end
3180    if length(indsel)>=1
3181        firstind=indsel(1);
3182        lastind=indsel(end);
3183        set(handles.first_j,'String',num2str(ref_j(firstind)))%update the display of first and last fields
3184        set(handles.last_j,'String',num2str(ref_j(lastind)))
3185        ref_j=ref_j(indsel);
3186        num_a_civ1=num_a_civ1(indsel);
3187        num_b_civ1=num_b_civ1(indsel);
3188        num_a_civ2=num_a_civ2(indsel);
3189        num_b_civ2=num_b_civ2(indsel);
3190    end
3191elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3192    displ_num=get(handles.list_pair_civ1,'UserData');
3193    num1_civ1=ref_i;
3194    num2_civ1=ref_i;
3195    num_a_civ1=displ_num(1,index_civ1);
3196    num_b_civ1=displ_num(2,index_civ1);
3197    num1_civ2=ref_i;
3198    num2_civ2=ref_i;
3199    num_a_civ2=displ_num(1,index_civ2);
3200    num_b_civ2=displ_num(2,index_civ2);
3201elseif isequal(mode,'displacement')
3202    num1_civ1=ref_i;
3203    num2_civ1=ref_i;
3204    num_a_civ1=ref_j;
3205    num_b_civ1=ref_j;
3206    num1_civ2=ref_i;
3207    num2_civ2=ref_i;
3208    num_a_civ2=ref_j;
3209    num_b_civ2=ref_j;
3210end
3211
3212
3213%------------------------------------------------------------------------
3214% --- PATCH
3215function cmd=cmd_patch(filename_nc,Param,PatchBin)
3216%------------------------------------------------------------------------
3217namelog=[filename_nc(1:end-3) '_patch.log'];
3218
3219% if test_interp==0
3220    if isunix
3221    cmd=[PatchBin...
3222        ' -f ' filename_nc ' -m ' num2str(Param.Nx)...
3223        ' -n ' num2str(Param.Ny) ' -ro ' num2str(Param.SmoothingParam)...
3224        ' -nopt ' num2str(Param.SubdomainSize) ...
3225        '  > ' namelog ' 2>&1'] % redirect standard output to the log file
3226    else
3227      cmd=['"' param.global.PatchBin...
3228          '" -f "' filename_nc '" -m ' param.Patch1.Nx...
3229          ' -n ' param.Patch1.Ny ' -ro ' param.Patch1.SmoothParam...
3230          ' -nopt ' Param.Patch1.SubdomainSize ...
3231        '  > "' namelog '" 2>&1']; % redirect standard output to the log file
3232    end
3233% else %nouveau programme patch
3234%     cmd=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3235%         ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3236% end
3237cmd=regexprep(cmd,'\\','\\\\');
3238
3239%------------------------------------------------------------------------
3240% --- STEREO Interp
3241function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3242%------------------------------------------------------------------------
3243namelog=[filename_nc(1:end-3) '_stinterp.log'];
3244cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3245    ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ' -c1 ' xmlA ' -c2 ' xmlB '  -xy  x -Nfy 1024 > ' namelog ' 2>&1']; % redirect standard output to the log file
3246
3247%------------------------------------------------------------------------
3248% --- Executes on button press in CIV1.
3249function CIV1_Callback(hObject, eventdata, handles)
3250%------------------------------------------------------------------------
3251state=get(handles.CIV1,'Value');
3252enable_civ1(handles,state)
3253if state
3254    enable_pair1(handles,'on')
3255end
3256find_netcpair_civ1(hObject, eventdata, handles);
3257
3258%------------------------------------------------------------------------
3259% --- Executes on button press in FIX1.
3260function FIX1_Callback(hObject, eventdata, handles)
3261%------------------------------------------------------------------------
3262enable_fix1(handles,get(handles.FIX1,'Value'))
3263
3264%------------------------------------------------------------------------
3265% --- Executes on button press in PATCH1.
3266function PATCH1_Callback(hObject, eventdata, handles)
3267%------------------------------------------------------------------------
3268enable_patch1(handles,get(handles.PATCH1,'Value'))
3269
3270%------------------------------------------------------------------------
3271% --- Executes on button press in CIV2.
3272function CIV2_Callback(hObject, eventdata, handles)
3273%------------------------------------------------------------------------
3274state=get(handles.CIV2,'Value');
3275enable_civ2(handles,state)
3276if state
3277    find_netcpair_civ2(hObject, eventdata, handles)
3278    enable_pair1(handles,'on')
3279end
3280
3281%------------------------------------------------------------------------
3282% --- Executes on button press in FIX2.
3283function FIX2_Callback(hObject, eventdata, handles)
3284%------------------------------------------------------------------------
3285state=get(handles.FIX2,'Value');
3286enable_fix2(handles,state)
3287if state
3288    find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3289end
3290
3291%------------------------------------------------------------------------
3292% --- Executes on button press in PATCH2.
3293function PATCH2_Callback(hObject, eventdata, handles)
3294%------------------------------------------------------------------------
3295state=get(handles.PATCH2,'Value');
3296enable_patch2(handles,state)
3297if state
3298    find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3299end
3300
3301%------------------------------------------------------------------------
3302function first_i_Callback(hObject, eventdata, handles)
3303%------------------------------------------------------------------------
3304% last_i_Callback(hObject, eventdata, handles)
3305first_i=str2double(get(handles.first_i,'String'));
3306% last_i=str2num(get(handles.last_i,'String'));
3307% ref_i=ceil((first_i+last_i)/2);
3308set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3309%set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3310ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3311
3312%------------------------------------------------------------------------
3313function first_j_Callback(hObject, eventdata, handles)
3314%------------------------------------------------------------------------
3315first_j=str2num(get(handles.first_j,'String'));
3316set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3317ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3318
3319%------------------------------------------------------------------------
3320% --- Executes on button press in SearchRange: determine the search range isx,isy
3321function SearchRange_Callback(hObject, eventdata, handles)
3322%------------------------------------------------------------------------
3323%determine pair numbers
3324if strcmp(get(handles.umin,'Visible'),'off')
3325    set(handles.u_title,'Visible','on')
3326    set(handles.v_title,'Visible','on')
3327    set(handles.umin,'Visible','on')
3328    set(handles.umax,'Visible','on')
3329    set(handles.vmin,'Visible','on')
3330    set(handles.vmax,'Visible','on')
3331    set(handles.CoordUnit,'Visible','on')
3332    set(handles.TimeUnit,'Visible','on')
3333    set(handles.slash_title,'Visible','on')
3334    set(handles.min_title,'Visible','on')
3335    set(handles.max_title,'Visible','on')
3336    set(handles.unit_title,'Visible','on')
3337else
3338    get_search_range(hObject, eventdata, handles)
3339end
3340
3341%------------------------------------------------------------------------
3342% ---  determine the search range isx,isy and shift
3343function get_search_range(hObject, eventdata, handles)
3344umin=str2double(get(handles.umin,'String'));
3345umax=str2double(get(handles.umax,'String'));
3346vmin=str2double(get(handles.umin,'String'));
3347vmax=str2double(get(handles.vmax,'String'));
3348%switch min_title and max_title in case of error
3349if umax<=umin
3350    umin_old=umin;
3351    umin=umax;
3352    umax=umin_old;
3353    set(handles.umin,'String', num2str(umin))
3354    set(handles.umax,'String', num2str(umax))
3355end
3356if vmax<=vmin
3357    vmin_old=vmin;
3358    vmin=vmax;
3359    vmax=vmin_old;
3360    set(handles.vmin,'String', num2str(vmin))
3361    set(handles.vmax,'String', num2str(vmax))
3362end   
3363if ~(isnan(umin)||isnan(umax)||isnan(vmin)||isnan(vmax))
3364    list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
3365    index=get(handles.list_pair_civ1,'Value');
3366    displ_num=get(handles.list_pair_civ1,'UserData');
3367    time=get(handles.ImaDoc,'UserData'); %get the set of times
3368    pxcm_xy=get(handles.SearchRange,'UserData');
3369    pxcmx=pxcm_xy(1);
3370    pxcmy=pxcm_xy(2);
3371    mode_list=get(handles.CivMode,'String');
3372    mode_value=get(handles.CivMode,'Value');
3373    mode=mode_list{mode_value};
3374    if isequal (mode, 'series(Di)' )
3375        ref_i=str2double(get(handles.ref_i,'String'));
3376        num1=ref_i-floor(index/2);%  first image numbers
3377        num2=ref_i+ceil(index/2);
3378        num_a=1;
3379        num_b=1;
3380    elseif isequal (mode, 'series(Dj)')
3381        num1=1;
3382        num2=1;
3383        ref_j=str2double(get(handles.ref_j,'String'));
3384        num_a=ref_j-floor(index/2);%  first image numbers
3385        num_b=ref_j+ceil(index/2);
3386    elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3387        ref_i=str2double(get(handles.ref_i,'String'));
3388        num1=ref_i;
3389        num2=ref_i;
3390        num_a=displ_num(1,index);
3391        num_b=displ_num(2,index);
3392    end
3393    dt=time(num2,num_b)-time(num1,num_a);
3394    ibx=str2double(get(handles.ibx,'String'));
3395    iby=str2double(get(handles.iby,'String'));
3396    umin=dt*pxcmx*umin;
3397    umax=dt*pxcmx*umax;
3398    vmin=dt*pxcmy*vmin;
3399    vmax=dt*pxcmy*vmax;
3400    shiftx=round((umin+umax)/2);
3401    shifty=round((vmin+vmax)/2);
3402    isx=(umax+2-shiftx)*2+ibx;
3403    isx=2*ceil(isx/2)+1;
3404    isy=(vmax+2-shifty)*2+iby;
3405    isy=2*ceil(isy/2)+1;
3406    set(handles.shiftx,'String',num2str(shiftx));
3407    set(handles.shifty,'String',num2str(shifty));
3408    set(handles.isx,'String',num2str(isx));
3409    set(handles.isy,'String',num2str(isy));
3410end
3411
3412%------------------------------------------------------------------------
3413% --- Executes on carriage return on the subdir civ1 edit window
3414function subdir_civ1_Callback(hObject, eventdata, handles)
3415%------------------------------------------------------------------------
3416subdir=get(handles.subdir_civ1,'String');
3417set(handles.subdir_civ2,'String',subdir);% set civ2 directory the same as civ1 by default
3418menu_str=get(handles.list_subdir_civ1,'String');% read the list of subdirectories for update
3419ichoice=find(strcmp(subdir,menu_str),1);
3420if isempty(ichoice)
3421    ilist=numel(menu_str); %select 'new...' in the menu
3422else
3423    ilist=ichoice;
3424end
3425set(handles.list_subdir_civ1,'Value',ilist)% select the selected subdir in the menu
3426if get(handles.CIV1,'Value')==0
3427    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3428end
3429
3430%------------------------------------------------------------------------
3431% --- Executes on carriage return on the subdir civ1 edit window
3432function subdir_civ2_Callback(hObject, eventdata, handles)
3433%------------------------------------------------------------------------
3434subdir=get(handles.subdir_civ1,'String');
3435menu_str=get(handles.list_subdir_civ2,'String');% read the list of subdirectories for update
3436ichoice=find(strcmp(subdir,menu_str),1);
3437if isempty(ichoice)
3438    ilist=numel(menu_str); %select 'new...' in the menu
3439else
3440    ilist=ichoice;
3441end
3442set(handles.list_subdir_civ2,'Value',ilist)% select the selected subdir in the menu
3443%update the list of available pairs from netcdf files in the new directory
3444if ~get(handles.CIV2,'Value') && ~get(handles.CIV1,'Value') && ~get(handles.FIX1,'Value') && ~get(handles.PATCH1,'Value')
3445    find_netcpair_civ2(hObject, eventdata, handles);
3446end
3447
3448%------------------------------------------------------------------------
3449% --- Executes on button press in get_mask_civ1: select box for mask option
3450function get_mask_civ1_Callback(hObject, eventdata, handles)
3451%------------------------------------------------------------------------
3452maskval=get(handles.get_mask_civ1,'Value')
3453if isequal(maskval,0)
3454    set(handles.mask_civ1,'String','')
3455else
3456    mask_displ='no mask'; %default
3457    filebase=get(handles.RootName,'String');
3458    [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3459    if isequal(flag_mask,1)
3460        mask_displ=[num2str(nbslice_mask) 'mask'];
3461    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3462        common_path=fileparts(filebase);
3463        filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3464        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3465        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3466            mask_displ='no mask';
3467        end
3468    end
3469    if isequal(mask_displ,'no mask')
3470        [FileName, PathName, filterindex] = uigetfile( ...
3471            {'*.png', ' (*.png)';
3472            '*.png',  '.png files '; ...
3473            '*.*', 'All Files (*.*)'}, ...
3474            'Pick a mask file *.png',filebase);
3475        mask_displ=fullfile(PathName,FileName);
3476        if ~exist(mask_displ,'file')
3477            mask_displ='no mask';
3478        end
3479    end
3480    if isequal(mask_displ,'no mask')
3481        set(handles.get_mask_civ1,'Value',0)
3482        set(handles.get_mask_fix1,'Value',0)
3483        set(handles.get_mask_civ2,'Value',0)
3484        set(handles.get_mask_fix2,'Value',0)
3485    else
3486        set(handles.get_mask_fix1,'Value',1)
3487        set(handles.get_mask_fix2,'Value',1)
3488    end
3489    set(handles.mask_civ1,'String',mask_displ)
3490    set(handles.mask_fix1,'String',mask_displ)
3491    set(handles.mask_civ2,'String',mask_displ)
3492    set(handles.mask_fix2,'String',mask_displ)
3493end
3494set(handles.get_mask_civ2,'Value',maskval)%update the civ2 mask with the same option as civ1
3495
3496%------------------------------------------------------------------------
3497% --- Executes on button press in get_mask_fix1.
3498function get_mask_fix1_Callback(hObject, eventdata, handles)
3499%------------------------------------------------------------------------
3500maskval=get(handles.get_mask_fix1,'Value');
3501if isequal(maskval,0)
3502    set(handles.mask_fix1,'String','')
3503else
3504    mask_displ='no mask'; %default
3505    filebase=get(handles.RootName,'String');
3506    [nbslice, flag_mask]=get_mask(filebase,handles);
3507    if isequal(flag_mask,1)
3508        mask_displ=[num2str(nbslice) 'mask'];
3509    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3510        filebase_a=get(handles.RootName_1,'String');
3511        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3512        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3513            mask_displ='no mask';
3514        end
3515    end
3516    if isequal(mask_displ,'no mask')
3517        [FileName, PathName, filterindex] = uigetfile( ...
3518            {'*.png', ' (*.png)';
3519            '*.png',  '.png files '; ...
3520            '*.*', 'All Files (*.*)'}, ...
3521            'Pick a mask file *.png',filebase);
3522        mask_displ=fullfile(PathName,FileName);
3523        if ~exist(mask_displ,'file')
3524            mask_displ='no mask';
3525        end
3526    end
3527    if isequal(mask_displ,'no mask')
3528        set(handles.get_mask_fix1,'Value',0)
3529        set(handles.get_mask_civ2,'Value',0)
3530        set(handles.get_mask_fix2,'Value',0)
3531    else
3532        %set(handles.get_mask_civ2,'Value',1)
3533        set(handles.get_mask_fix2,'Value',1)
3534    end
3535    set(handles.mask_fix1,'String',mask_displ)
3536    set(handles.mask_civ2,'String',mask_displ)
3537    set(handles.mask_fix2,'String',mask_displ)
3538end
3539
3540%------------------------------------------------------------------------
3541% --- Executes on button press in get_mask_civ2: select box for mask option
3542function get_mask_civ2_Callback(hObject, eventdata, handles)
3543%------------------------------------------------------------------------
3544maskval=get(handles.get_mask_civ2,'Value');
3545if isequal(maskval,0)
3546    set(handles.mask_civ2,'String','')
3547else
3548    mask_displ='no mask'; %default
3549    filebase=get(handles.RootName,'String');
3550    [nbslice, flag_mask]=get_mask(filebase,handles);
3551    if isequal(flag_mask,1)
3552        mask_displ=[num2str(nbslice) 'mask'];
3553    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3554        filebase_a=get(handles.RootName_1,'String');
3555        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3556        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3557            mask_displ='no mask';
3558        end
3559    end
3560    if isequal(mask_displ,'no mask')
3561        [FileName, PathName, filterindex] = uigetfile( ...
3562            {'*.png', ' (*.png)';
3563            '*.png',  '.png files '; ...
3564            '*.*', 'All Files (*.*)'}, ...
3565            'Pick a mask file *.png',filebase);
3566        mask_displ=fullfile(PathName,FileName);
3567        if ~exist(mask_displ,'file')
3568            mask_displ='no mask';
3569        end
3570    end
3571    if isequal(mask_displ,'no mask')
3572        set(handles.get_mask_civ2,'Value',0)
3573        set(handles.get_mask_fix2,'Value',0)
3574    else
3575        set(handles.get_mask_fix2,'Value',1)
3576    end
3577    set(handles.mask_civ2,'String',mask_displ)
3578    set(handles.mask_fix2,'String',mask_displ)
3579end
3580
3581%------------------------------------------------------------------------
3582% --- Executes on button press in get_mask_fix2.
3583function get_mask_fix2_Callback(hObject, eventdata, handles)
3584%------------------------------------------------------------------------
3585maskval=get(handles.get_mask_fix2,'Value');
3586if isequal(maskval,0)
3587    set(handles.mask_fix2,'String','')
3588else
3589    mask_displ='no mask'; %default
3590    filebase=get(handles.RootName,'String');
3591    [nbslice, flag_mask]=get_mask(filebase,handles);
3592    if isequal(flag_mask,1)
3593        mask_displ=[num2str(nbslice) 'mask'];
3594    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3595        filebase_a=get(handles.RootName_1,'String');
3596        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3597        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3598            mask_displ='no mask';
3599        end
3600    end
3601    if isequal(mask_displ,'no mask')
3602        [FileName, PathName, filterindex] = uigetfile( ...
3603            {'*.png', ' (*.png)';
3604            '*.png',  '.png files '; ...
3605            '*.*', 'All Files (*.*)'}, ...
3606            'Pick a mask file *.png',filebase);
3607        mask_displ=fullfile(PathName,FileName);
3608        if ~exist(mask_displ,'file')
3609            mask_displ='no mask';
3610        end
3611    end
3612    if isequal(mask_displ,'no mask')
3613        set(handles.get_mask_fix2,'Value',0)
3614    end
3615    set(handles.mask_fix2,'String',mask_displ)
3616end
3617
3618%------------------------------------------------------------------------
3619% --- function called to look for mask files
3620function [nbslice, flag_mask]=get_mask(filebase,handles)
3621%------------------------------------------------------------------------
3622%detect mask files, images with appropriate file base
3623%[filebase '_' xx 'mask'], xx=nbslice
3624%flag_mask=1 indicates detection
3625
3626flag_mask=0;%default
3627nbslice=1;
3628
3629% subdir=get(handles.subdir_civ1,'String');
3630[Path,Name]=fileparts(filebase);
3631if ~isdir(Path)
3632    msgbox_uvmat('ERROR','no path for input files')
3633    return
3634end
3635% currentdir=pwd;
3636% cd(Path);%move in the dir of the root name filebase
3637maskfiles=dir(fullfile(Path,[Name '_*mask_*.png']));%look for mask files
3638% cd(currentdir);%come back to the current working directory
3639if ~isempty(maskfiles)
3640    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3641    % else
3642    flag_mask=1;
3643    maskname=maskfiles(1).name;% take the first mask file in the list
3644    [Path2,Name,ext]=fileparts(maskname);
3645    Namedouble=double(Name);
3646    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3647    ind_mask=findstr('mask',Name);
3648    i=ind_mask-1;
3649    while val(i)==0 && i>0
3650        i=i-1;
3651    end
3652    nbslice=str2double(Name(i+1:ind_mask-1));
3653    if ~isnan(nbslice) && Name(i)=='_'
3654        flag_mask=1;
3655    else
3656        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3657        return
3658        nbslice=1;
3659    end
3660end
3661
3662%------------------------------------------------------------------------
3663% --- function called to look for grid files
3664function [nbslice, flag_mask]=get_grid(filebase,handles)
3665%------------------------------------------------------------------------
3666flag_mask=0;%default
3667nbslice=1;
3668[Path,Name]=fileparts(filebase);
3669currentdir=pwd;
3670cd(Path);%move in the dir of the root name filebase
3671maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
3672cd(currentdir);%come back to the current working directory
3673if ~isempty(maskfiles)
3674    flag_mask=1;
3675    maskname=maskfiles(1).name;% take the first mask file in the list
3676    [Path2,Name,ext]=fileparts(maskname);
3677    Namedouble=double(Name);
3678    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3679    ind_mask=findstr('grid',Name);
3680    i=ind_mask-1;
3681    while val(i)==0 && i>0
3682        i=i-1;
3683    end
3684    nbslice=str2double(Name(i+1:ind_mask-1));
3685    if ~isnan(nbslice) && Name(i)=='_'
3686        flag_mask=1;
3687    else
3688        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
3689        return
3690        nbslice=1;
3691    end
3692end
3693
3694%------------------------------------------------------------------------
3695% --- transform numbers to letters
3696function str=num2stra(num,nom_type)
3697%------------------------------------------------------------------------
3698if isempty(nom_type)
3699    str='';
3700elseif strcmp(nom_type(end),'a')
3701    str=char(96+num);
3702elseif strcmp(nom_type(end),'A')
3703    str=char(96+num);
3704elseif isempty(nom_type(2:end))%a single index
3705    str='';
3706else
3707    str=num2str(num);
3708end
3709
3710%------------------------------------------------------------------------
3711% --- Executes on button press in list_subdir_civ1.
3712function list_subdir_civ1_Callback(hObject, eventdata, handles)
3713%------------------------------------------------------------------------
3714list_subdir_civ1=get(handles.list_subdir_civ1,'String');
3715val=get(handles.list_subdir_civ1,'Value');
3716subdir=list_subdir_civ1{val};
3717if strcmp(subdir,'new...')
3718    subdir='CIV'; %default subdirectory
3719end
3720set(handles.subdir_civ1,'String',subdir);
3721find_netcpair_civ1(hObject, eventdata, handles)
3722% end
3723
3724%------------------------------------------------------------------------
3725% --- Executes on button press in list_subdir_civ2.
3726function list_subdir_civ2_Callback(hObject, eventdata, handles)
3727%------------------------------------------------------------------------
3728list_subdir_civ2=get(handles.list_subdir_civ2,'String');
3729val=get(handles.list_subdir_civ2,'Value');
3730subdir=list_subdir_civ2{val};
3731if strcmp(subdir,'new...')
3732    subdir='CIV'; %default subdirectory
3733end
3734set(handles.subdir_civ2,'String',subdir);
3735%     set(handles.list_subdir_civ2,'Value',1);
3736
3737
3738%------------------------------------------------------------------------
3739% --- Executes on button press in browse_gridciv1.
3740function browse_gridciv1_Callback(hObject, eventdata, handles)
3741%------------------------------------------------------------------------
3742value=get(handles.browse_gridciv1,'Value');
3743testgrid=0;
3744if value
3745    filebase=get(handles.RootName,'String');
3746    [nbslice, flag_grid]=get_grid(filebase,handles);
3747    if isequal(flag_grid,1)
3748        filegrid=[num2str(nbslice) 'grid'];
3749        testgrid=1;
3750    else
3751        [FileName, PathName, filterindex] = uigetfile( ...
3752            {'*.grid', ' (*.grid)';
3753            '*.grid',  '.grid files '; ...
3754            '*.*', 'All Files (*.*)'}, ...
3755            'Pick a file',filebase);
3756        filegrid=fullfile(PathName,FileName);
3757        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
3758            testgrid=1;
3759        end
3760    end
3761end
3762if testgrid
3763    set(handles.browse_gridciv2,'Value',1)
3764    set(handles.get_gridpatch1,'Value',1)
3765    set(handles.get_gridpatch2,'Value',1)
3766    set(handles.dx_civ1,'Visible','off');
3767    set(handles.dy_civ1,'Visible','off');
3768    set(handles.dx_civ2,'Visible','off');
3769    set(handles.dy_civ2,'Visible','off');
3770    set(handles.grid_civ1,'String',filegrid)
3771    set(handles.grid_patch1,'String',filegrid)
3772    set(handles.grid_civ2,'String',filegrid)
3773    set(handles.grid_patch2,'String',filegrid)
3774else
3775    set(handles.browse_gridciv1,'Value',0);
3776    set(handles.browse_gridciv2,'Value',0);
3777    set(handles.get_gridpatch1,'Value',0)
3778    set(handles.get_gridpatch2,'Value',0)
3779    set(handles.dx_civ1,'Visible','on');
3780    set(handles.dy_civ1,'Visible','on');
3781    set(handles.dx_civ2,'Visible','on');
3782    set(handles.dy_civ2,'Visible','on');
3783    set(handles.grid_civ1,'String','')
3784    set(handles.grid_patch1,'String','')
3785    set(handles.grid_civ2,'String','')
3786    set(handles.grid_patch2,'String','')
3787end
3788
3789%------------------------------------------------------------------------
3790% --- Executes on button press in browse_gridciv1.
3791function browse_gridciv2_Callback(hObject, eventdata, handles)
3792%------------------------------------------------------------------------
3793value=get(handles.browse_gridciv2,'Value');
3794if value
3795    filebase=get(handles.RootName,'String');
3796    [nbslice, flag_grid]=get_grid(filebase,handles);
3797    if isequal(flag_grid,1)
3798        mask_displ=[num2str(nbslice) 'grid'];
3799        set(handles.grid_civ2,'String',mask_displ)
3800        set(handles.dx_civ2,'Visible','off');
3801        set(handles.dy_civ2,'Visible','off');
3802    else
3803        [FileName, PathName, filterindex] = uigetfile( ...
3804            {'*.grid', ' (*.grid)';
3805            '*.grid',  '.grid files '; ...
3806            '*.*', 'All Files (*.*)'}, ...
3807            'Pick a file',filebase);
3808        filegrid=fullfile(PathName,FileName);
3809        if isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file')
3810            set(handles.browse_gridciv2,'Value',0);
3811            set(handles.grid_civ2,'string','');
3812            set(handles.dx_civ2,'Visible','on');
3813            set(handles.dy_civ2,'Visible','on');
3814            set(handles.grid_civ2,'string','');
3815        else
3816            set(handles.grid_civ2,'string',filegrid);
3817            set(handles.dx_civ2,'Visible','off');
3818            set(handles.dy_civ2,'Visible','off');
3819            set(handles.grid_civ2,'string',filegrid);
3820        end
3821    end
3822else
3823    set(handles.grid_civ2,'string','');
3824    set(handles.dx_civ2,'Visible','on');
3825    set(handles.dy_civ2,'Visible','on');
3826    set(handles.grid_civ2,'string','');
3827end
3828
3829% % --- Executes on button press in browse_gridciv2.
3830% function browse_gridciv2_Callback(hObject, eventdata, handles)
3831%
3832% filebase=get(handles.RootName,'String');
3833% [FileName, PathName, filterindex] = uigetfile( ...
3834%        {'*.grid', ' (*.grid)';
3835%         '*.grid',  '.grid files '; ...
3836%         '*.*', 'All Files (*.*)'}, ...
3837%         'Pick a file',filebase);
3838% filegrid=fullfile(PathName,FileName);
3839% set(handles.grid_civ2,'string',filegrid);
3840% set(handles.dx_civ2,'String',' ');
3841% set(handles.dy_civ2,'String',' ');
3842% % set(handles.grid_patch2,'string',filegrid);
3843
3844% --- Executes on button press in get_gridpatch1.
3845function get_gridpatch1_Callback(hObject, eventdata, handles)
3846% hObject    handle to get_gridpatch1 (see GCBO)
3847% eventdata  reserved - to be defined in a future version of MATLAB
3848% handles    structure with handles and user data (see GUIDATA)
3849
3850filebase=get(handles.RootName,'String');
3851[FileName, PathName, filterindex] = uigetfile( ...
3852    {'*.grid', ' (*.grid)';
3853    '*.grid',  '.grid files '; ...
3854    '*.*', 'All Files (*.*)'}, ...
3855    'Pick a file',filebase);
3856filegrid=fullfile(PathName,FileName);
3857set(handles.grid_patch1,'string',filegrid);
3858% set(handles.grid_patch2,'string',filegrid
3859
3860%------------------------------------------------------------------------
3861% --- Executes on button press in get_gridpatch2.
3862function get_gridpatch2_Callback(hObject, eventdata, handles)
3863%------------------------------------------------------------------------
3864
3865%------------------------------------------------------------------------
3866function enable_civ1(handles,state)
3867%------------------------------------------------------------------------
3868if state
3869    RootName=get(handles.RootName,'String');
3870    if isempty(RootName)
3871         msgbox_uvmat('ERROR','No input file')
3872        return
3873    end
3874    set(handles.panel_Civ1,'Visible','on')   
3875    set(handles.panel_PairIndices,'Visible','on')
3876%     set(handles.frame_civ1,'BackgroundColor',[1 1 0])
3877%     set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
3878%     set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
3879else
3880    set(handles.panel_Civ1,'Visible','off') 
3881%     set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
3882%     set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
3883%     set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
3884end
3885return
3886set(handles.ibx,'Visible',state)
3887set(handles.iby,'Visible',state)
3888set(handles.isx,'Visible',state)
3889set(handles.isy,'Visible',state)
3890set(handles.shiftx,'Visible',state)
3891set(handles.shifty,'Visible',state)
3892set(handles.rho,'Visible',state)
3893set(handles.dx_civ1,'Visible',state)
3894set(handles.dy_civ1,'Visible',state)
3895set(handles.SearchRange,'Visible',state)
3896set(handles.u_title,'Visible',state)
3897set(handles.v_title,'Visible',state)
3898set(handles.min_title,'Visible',state)
3899set(handles.max_title,'Visible',state)
3900set(handles.umin,'Visible',state)
3901set(handles.umax,'Visible',state)
3902set(handles.vmin,'Visible',state)
3903set(handles.vmax,'Visible',state)
3904set(handles.grid_civ1,'Visible',state)
3905set(handles.mask_civ1,'Visible',state)
3906set(handles.browse_gridciv1,'Visible',state)
3907set(handles.get_mask_civ1,'Visible',state)
3908set(handles.parameters,'Visible',state)
3909set(handles.grid,'Visible',state)
3910set(handles.dx_civ1,'Visible',state)
3911set(handles.dy_civ1,'Visible',state)
3912set(handles.ImaThreshold,'Visible',state)
3913if isequal(state,'off')
3914    set(handles.MinIma,'Visible','off')
3915    set(handles.MaxIma,'Visible','off')
3916    set(handles.ImaThreshold,'Value',0)
3917end
3918set(handles.dx_civ1_title,'Visible',state)
3919set(handles.dy_civ1_title,'Visible',state)
3920set(handles.ImaThreshold_title,'Visible',state)
3921set(handles.ib_title,'Visible',state)
3922set(handles.is_title,'Visible',state)
3923set(handles.shift_title,'Visible',state)
3924set(handles.rho_title,'Visible',state)
3925set(handles.TestCiv1,'Visible',state)
3926%set(handles.CivMode,'Visible',state)
3927
3928%------------------------------------------------------------------------
3929function enable_fix1(handles,state)
3930%------------------------------------------------------------------------
3931
3932if isequal(state,0)
3933    state='off';
3934end
3935if isequal(state,1)
3936    state='on';
3937    RootName=get(handles.RootName,'String');
3938    if isempty(RootName)
3939         msgbox_uvmat('ERROR','No input file')
3940        return
3941    end
3942end
3943if isequal(state,'on')
3944    set(handles.panel_Fix1,'Visible','on')
3945%     set(handles.frame_fix1,'BackgroundColor',[1 1 0])
3946else
3947    set(handles.panel_Fix1,'Visible','off')
3948    %set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
3949end
3950% set(handles.REMOVE,'Visible',state)
3951set(handles.vec_Fmin2,'Visible',state)
3952set(handles.vec_F2,'Visible',state)
3953set(handles.vec_F3,'Visible',state)
3954set(handles.thresh_vecC,'Visible',state)
3955set(handles.thresh_vecC_title,'Visible',state)
3956set(handles.thresh_vel,'Visible',state)
3957set(handles.thresh_vel_text,'Visible',state)
3958set(handles.mask_fix1,'Visible',state)
3959set(handles.get_mask_fix1,'Visible',state)
3960set(handles.get_ref_fix1,'Visible',state)
3961set(handles.ref_fix1,'Visible',state)
3962set(handles.inf_sup1,'Visible',state)
3963set(handles.field_ref1,'Visible',state)
3964
3965%------------------------------------------------------------------------
3966function enable_patch1(handles,state)
3967%------------------------------------------------------------------------
3968if state
3969    RootName=get(handles.RootName,'String');
3970    if isempty(RootName)
3971        msgbox_uvmat('ERROR','No input file')
3972        return
3973    end
3974    set(handles.panel_Patch1,'Visible','on')
3975else
3976    set(handles.panel_Patch1,'Visible','off')
3977end
3978return
3979set(handles.frame_patch1,'BackgroundColor',[1 1 0])
3980set(handles.rho_patch1,'Visible','on')
3981set(handles.rho_text1,'Visible','on')
3982if get(handles.CivMode,'Value')==2
3983    set(handles.thresh_patch1,'Visible','on')
3984    set(handles.thresh_text1,'Visible','on')
3985end
3986set(handles.subdomain_patch1,'Visible','on')
3987set(handles.subdomain_text1,'Visible','on')
3988set(handles.nx_patch1,'Visible','on')
3989set(handles.num_Ny,'Visible','on')
3990set(handles.nx_patch1_title,'Visible','on')
3991set(handles.ny_patch1_title,'Visible','on')
3992% if ~isempty(patch_newBin)
3993set(handles.test_interp,'Visible','off');
3994stereo_test=get(handles.compare,'Value');
3995if stereo_test==3
3996    set(handles.test_stereo1,'Visible','on')
3997end
3998% end
3999%set(handles.get_gridpatch1,'Visible','on')
4000%set(handles.grid_patch1,'string','none');
4001%set(handles.grid_patch1,'Visible','on')
4002
4003% %------------------------------------------------------------------------
4004% function desable_patch1(handles)
4005% %------------------------------------------------------------------------
4006% return
4007% set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
4008% set(handles.rho_patch1,'Visible','off')
4009% set(handles.rho_text1,'Visible','off')
4010% set(handles.thresh_patch1,'Visible','off')
4011% set(handles.thresh_text1,'Visible','off')
4012% set(handles.subdomain_patch1,'Visible','off')
4013% set(handles.subdomain_text1,'Visible','off')
4014% set(handles.nx_patch1,'Visible','off')
4015% set(handles.num_Ny,'Visible','off')
4016% set(handles.nx_patch1_title,'Visible','off')
4017% set(handles.ny_patch1_title,'Visible','off')
4018% set(handles.test_stereo1,'Visible','off')
4019% %set(handles.test_interp,'Visible','off')
4020% %set(handles.get_gridpatch1,'Visible','off')
4021% %set(handles.grid_patch1,'Visible','off')
4022
4023%------------------------------------------------------------------------
4024function enable_civ2(handles,state)
4025%------------------------------------------------------------------------
4026if state
4027    RootName=get(handles.RootName,'String');
4028    if isempty(RootName)
4029         msgbox_uvmat('ERROR','No input file')
4030        return
4031    end
4032    set(handles.panel_Civ2,'Visible','on')
4033else
4034    set(handles.panel_Civ2,'Visible','off')
4035end
4036return
4037
4038set(handles.ibx_civ2,'Visible',state)
4039set(handles.iby_civ2,'Visible',state)
4040set(handles.decimal,'Visible',state)
4041set(handles.deformation,'Visible',state)
4042set(handles.rho_civ2,'Visible',state)
4043set(handles.dx_civ2,'Visible',state)
4044set(handles.dy_civ2,'Visible',state)
4045set(handles.browse_gridciv2,'Visible',state)
4046set(handles.get_mask_civ2,'Visible',state)
4047set(handles.parameters,'Visible',state)
4048set(handles.grid,'Visible',state)
4049set(handles.parameters_text,'Visible',state)
4050set(handles.grid_text,'Visible',state)
4051set(handles.grid_civ2,'Visible',state)
4052set(handles.mask_civ2,'Visible',state)
4053set(handles.dx_civ2_title,'Visible',state)
4054set(handles.dy_civ2_title,'Visible',state)
4055set(handles.ibx_civ2_text,'Visible',state)
4056set(handles.rho_civ2_title,'Visible',state)
4057set(handles.ImaThreshold2,'Visible',state)
4058set(handles.ImaThreshold_title2,'Visible',state)
4059if isequal(state,'off')
4060    set(handles.MinIma2,'Visible','off')
4061    set(handles.MaxIma2,'Visible','off')
4062    set(handles.ImaThreshold2,'Value',0)
4063    if isequal(get(handles.FIX2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4064        set(handles.list_pair_civ2,'Visible','off')
4065        set(handles.subdir_civ2,'Visible','off')
4066        set(handles.subdir_civ2_text,'Visible','off')
4067        set(handles.dt_unit_civ2,'Visible','off')
4068        %set(handles.ref_i_civ2,'Visible','off')
4069        set(handles.i_ref_civ2_title,'Visible','off')
4070        set(handles.j_ref_civ2_title,'Visible','off')
4071        set(handles.ref_j_civ2,'Visible','off')
4072    end
4073else
4074    set(handles.list_pair_civ2,'Visible','on')
4075    set(handles.subdir_civ2,'Visible','on')
4076    set(handles.subdir_civ2_text,'Visible','on')
4077    set(handles.dt_unit_civ2,'Visible','on')
4078  %  set(handles.ref_i_civ2,'Visible','on')
4079    set(handles.i_ref_civ2_title,'Visible','on')
4080    set(handles.j_ref_civ2_title,'Visible','on')
4081    set(handles.ref_j_civ2,'Visible','on')
4082end
4083set(handles.rho_civ2_title,'Visible',state)
4084
4085%------------------------------------------------------------------------
4086function enable_fix2(handles,state)
4087%------------------------------------------------------------------------
4088% if isequal(state,'on')
4089%     set(handles.panel_Fix2,'Visible','on') 
4090%     
4091% %     set(handles.frame_civ2,'BackgroundColor',[1 1 0])
4092% %     set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
4093% %     set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
4094% %     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
4095% else
4096if state
4097    RootName=get(handles.RootName,'String');
4098    if isempty(RootName)
4099         msgbox_uvmat('ERROR','No input file')
4100        return
4101    end
4102    set(handles.panel_Fix2,'Visible','on')
4103else
4104    set(handles.panel_Fix2,'Visible','off')
4105end
4106return
4107set(handles.frame_fix2,'BackgroundColor',[1 1 0])
4108set(handles.REMOVE2,'Visible','on')
4109set(handles.vec_Fmin2_2,'Visible','on')
4110set(handles.vec_F4,'Visible','on')
4111set(handles.vec_F3_2,'Visible','on')
4112set(handles.thresh_vec2C,'Visible','on')
4113set(handles.thresh_vec2C_text,'Visible','on')
4114set(handles.thresh_vel2,'Visible','on')
4115set(handles.thresh_vel2_text,'Visible','on')
4116set(handles.mask_fix2,'Visible','on')
4117set(handles.get_mask_fix2,'Visible','on')
4118set(handles.list_pair_civ2,'Visible','on')
4119set(handles.subdir_civ2,'Visible','on')
4120set(handles.subdir_civ2_text,'Visible','on')
4121set(handles.get_ref_fix2,'Visible','on')
4122set(handles.ref_fix2,'Visible','on')
4123set(handles.inf_sup2,'Visible','on')
4124set(handles.field_ref2,'Visible','on')
4125
4126% %------------------------------------------------------------------------
4127% function desable_fix2(handles)
4128% %------------------------------------------------------------------------
4129% set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
4130% set(handles.REMOVE2,'Visible','off')
4131% set(handles.vec_Fmin2_2,'Visible','off')
4132% set(handles.vec_F4,'Visible','off')
4133% set(handles.vec_F3_2,'Visible','off')
4134% set(handles.thresh_vec2C,'Visible','off')
4135% set(handles.thresh_vec2C_text,'Visible','off')
4136% set(handles.thresh_vel2,'Visible','off')
4137% set(handles.thresh_vel2_text,'Visible','off')
4138% set(handles.mask_fix2,'Visible','off')
4139% set(handles.get_mask_fix2,'Visible','off')
4140% set(handles.get_ref_fix2,'Visible','off')
4141% set(handles.ref_fix2,'Visible','off')
4142% set(handles.inf_sup2,'Visible','off')
4143% set(handles.field_ref2,'Visible','off')
4144% if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4145%     set(handles.list_pair_civ2,'Visible','off')
4146%     set(handles.subdir_civ2,'Visible','off')
4147%     set(handles.subdir_civ2_text,'Visible','off')
4148% end
4149
4150%------------------------------------------------------------------------
4151function enable_patch2(handles,state)
4152%------------------------------------------------------------------------
4153if state
4154RootName=get(handles.RootName,'String');
4155    if isempty(RootName)
4156         msgbox_uvmat('ERROR','No input file')
4157        return
4158    end
4159    set(handles.panel_Patch2,'Visible','on')
4160else
4161set(handles.panel_Patch2,'Visible','off')
4162end
4163% set(handles.frame_patch2,'BackgroundColor',[1 1 0])
4164% set(handles.num_SmoothParam,'Visible','on')
4165% set(handles.num_SmoothingParam,'Visible','on')
4166% set(handles.num_MaxDiff,'Visible','on')
4167% set(handles.thresh_text2,'Visible','on')
4168% set(handles.num_SubdomainSize,'Visible','on')
4169% set(handles.subdomain_text2,'Visible','on')
4170% set(handles.num_Nx,'Visible','on')
4171% set(handles.num_Ny,'Visible','on')
4172% set(handles.nx_patch2_title,'Visible','on')
4173% set(handles.ny_patch2_title,'Visible','on')
4174% % set(handles.get_gridpatch2,'Visible','on')
4175% % set(handles.grid_patch2,'Visible','on')
4176% set(handles.list_pair_civ2,'Visible','on')
4177% set(handles.subdir_civ2,'Visible','on')
4178% set(handles.subdir_civ2_text,'Visible','on')
4179% stereo_test=get(handles.compare,'Value');
4180% if stereo_test==3
4181%     set(handles.check_Stereo,'Visible','on')
4182% end
4183
4184% %------------------------------------------------------------------------
4185% function desable_patch2(handles)
4186% %------------------------------------------------------------------------
4187% %set(handles.panel_Patch2,'Visible','off')
4188% return
4189% set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
4190% set(handles.num_SmoothParam,'Visible','off')
4191% set(handles.num_SmoothingParam,'Visible','off')
4192% set(handles.num_MaxDiff,'Visible','off')
4193% set(handles.thresh_text2,'Visible','off')
4194% set(handles.num_SubdomainSize,'Visible','off')
4195% set(handles.subdomain_text2,'Visible','off')
4196% set(handles.num_Nx,'Visible','off')
4197% set(handles.num_Ny,'Visible','off')
4198% set(handles.nx_patch2_title,'Visible','off')
4199% set(handles.ny_patch2_title,'Visible','off')
4200% % set(handles.get_gridpatch2,'Visible','off')
4201% % set(handles.grid_patch2,'Visible','off')
4202% if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.FIX2,'Value'),0)
4203%     set(handles.list_pair_civ2,'Visible','off')
4204%     set(handles.subdir_civ2,'Visible','off')
4205%     set(handles.subdir_civ2_text,'Visible','off')
4206% end
4207% set(handles.check_Stereo,'Visible','off')
4208%------------------------------------------------------------------------
4209function enable_pair1(handles,state)
4210%------------------------------------------------------------------------
4211set(handles.subdir_civ1,'Visible',state)
4212set(handles.list_subdir_civ1,'Visible',state)
4213set(handles.SUBDIR_CIV1_txt,'Visible',state)
4214%set(handles.frame_subdirciv1,'Visible',state)
4215set(handles.list_pair_civ1,'Visible',state)
4216set(handles.PairCiv1_title,'Visible',state)
4217%set(handles.dt_unit,'Visible',state)
4218%set(handles.PAIR_frame,'Visible',state)
4219
4220%------------------------------------------------------------------------
4221% --- Read the parameters for civ1 on the interface
4222function par=read_param_civ1(handles,filecell)
4223%------------------------------------------------------------------------
4224ibx_val=str2double(get(handles.ibx,'String'));
4225par.ibx=num2str(ibx_val);
4226iby_val=str2double(get(handles.iby,'String'));
4227par.iby=num2str(iby_val);
4228isx=get(handles.isx,'String');
4229if isnan(str2double(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
4230if str2double(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
4231isy=get(handles.isy,'String');
4232if isnan(str2double(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
4233if str2double(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
4234par.isx=get(handles.isx,'String');
4235par.isy=get(handles.isy,'String');
4236par.shiftx=get(handles.shiftx,'String');
4237par.shifty=get(handles.shifty,'String');
4238if isnan(str2double(par.isx))
4239    par.isx='41';%default
4240    set(handles.isx,'String','41');
4241end
4242if isnan(str2double(par.isy))
4243    par.isy='41'; %default
4244    set(handles.isy,'String','41');
4245end
4246if isnan(str2double(par.shiftx))
4247    par.shiftx='0';%default
4248    set(handles.shiftx,'String','0');
4249end
4250if isnan(str2double(par.shifty))
4251    par.shifty='0'; %default
4252    set(handles.shifty,'String','0');
4253end
4254par.rho=get(handles.rho,'String');
4255if isequal(get(handles.rho,'Style'),'popupmenu')
4256    index=get(handles.rho,'Value');
4257    par.rho=par.rho{index};
4258end
4259par.dx=get(handles.dx_civ1,'String');
4260par.dy=get(handles.dy_civ1,'String');
4261if isnan(str2double(par.dx))
4262    if isempty(get(handles.grid_civ1,'String'));
4263        par.dx='0'; %just read by civ program, not used
4264    else
4265        par.dx='20';%default
4266        set(handles.dx_civ1,'String','20');
4267    end
4268end
4269if isnan(str2double(par.dy))
4270    if isempty(get(handles.grid_civ1,'String'));
4271        par.dy='0';%just read by civ program, not used
4272    else
4273        par.dy='20';%default
4274        set(handles.dy_civ1_title,'String','20');
4275    end
4276end
4277par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4278par.pxcmy='1';
4279% if exist('file_ima','var')
4280A=imread(filecell.ima1.civ1{1,1});%read the first image to get the size
4281sizim=size(A);
4282par.npx=num2str(sizim(2));
4283par.npy=num2str(sizim(1));
4284%TODO : civ should not need npx and npy
4285
4286
4287% end
4288%time=get(handles.RootName,'UserData'); %get the set of times
4289par.gridname=get(handles.grid_civ1,'String');
4290par.gridflag='y';
4291if strcmp(par.gridname,'')|| isempty(par.gridname)
4292    par.gridname='nogrid';
4293    par.gridflag='n';
4294end
4295
4296%------------------------------------------------------------------------
4297function par=read_param_fix1(handles,filecell)
4298%------------------------------------------------------------------------
4299    par.flagindex1(1)=get(handles.vec_Fmin2, 'Value');
4300    par.flagindex1(2)=get(handles.vec_F3, 'Value');
4301    par.flagindex1(3)=get(handles.vec_F2, 'Value');
4302    par.thresh_vecC1=str2double(get(handles.thresh_vecC,'String'));%threshold on image correlation vec_C
4303    par.thresh_vel1=str2double(get(handles.thresh_vel,'String'));%threshold on velocity modulus
4304    par.test_mask=get(handles.get_mask_fix1,'Value');
4305    par.nbslice_mask=get(handles.mask_fix1,'UserData'); % get the number of slices (= number of masks)
4306    %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX
4307    %     inf_sup=get(handles.inf_sup1,'Value');80
4308    %     fileref=get(handles.ref_fix1,'String');
4309    %     refpath=get(handles.ref_fix1,'UserData');
4310    %     fileref=fullfile(refpath,fileref);
4311    par.menu=get(handles.field_ref1,'String');
4312    par.index=get(handles.field_ref1,'Value');
4313    if isempty(par.menu)
4314        par.fieldchoice='';
4315    else
4316        par.fieldchoice=menu{index};
4317        msgbox_uvmat('WARNING','reference field is not used presently with batch, use RUN option')
4318    end
4319           if par.test_mask==0
4320                par.maskname='';
4321            else
4322                maskdispl=get(handles.mask_fix1,'String');
4323                nbslice_mask=str2double(maskdispl(1:end-4)); %
4324                num1_mask=mod(num1_civ1(ifile)-1,nbslice_mask)+1;
4325                maskbase=[filecell.filebase '_' maskdispl];
4326                par.maskname=name_generator(maskbase,num1_mask,1,'.png','_i');
4327           end
4328 
4329%------------------------------------------------------------------------
4330function par=read_param_patch1(handles)
4331%------------------------------------------------------------------------
4332
4333par.rho_patch=str2double(get(handles.rho_patch1,'String'));
4334if isnan(par.rho_patch)
4335    par.rho_patch='1000';
4336    set(handles.rho_patch1,'String','1')
4337else
4338    par.rho_patch=num2str(1000*par.rho_patch);
4339end
4340par.nx_patch=get(handles.nx_patch1,'String');
4341par.ny_patch=get(handles.ny_patch1,'String');
4342if isnan(str2double(par.nx_patch))
4343    par.nx_patch='50' ;%default
4344    set(handles.nx_patch1,'String','50');
4345end
4346if isnan(str2double(par.ny_patch))
4347    par.ny_patch='50' ;%default
4348    set(handles.ny_patch1,'String','50');
4349end
4350par.subdomain_patch=get(handles.subdomain_patch1,'String');
4351par.thresh_patch=get(handles.thresh_patch1,'String');
4352
4353           
4354%------------------------------------------------------------------------
4355function par=read_param_civ2(handles,file_ima)
4356%------------------------------------------------------------------------
4357par.ibx=get(handles.ibx_civ2,'String');
4358par.iby=get(handles.iby_civ2,'String');
4359par.rho=get(handles.rho_civ2,'String');
4360par.decimal=int2str(get(handles.decimal,'Value'));
4361par.deformation=int2str(get(handles.deformation,'Value'));
4362par.dx=get(handles.dx_civ2,'String');
4363par.dy=get(handles.dy_civ2,'String');
4364if isnan(str2double(par.dx))
4365    if isempty(get(handles.grid_civ2,'String'));
4366        par.dx='0'; %just read by civ program, not used
4367    else
4368        par.dx='20';%default
4369        set(handles.dx_civ2,'String','20');
4370    end
4371end
4372if isnan(str2double(par.dy))
4373    if isempty(get(handles.grid_civ2,'String'));
4374        par.dy='0';%just read by civ program, not used
4375    else
4376        par.dy='20';%default
4377        set(handles.dy_civ2,'String','20');
4378    end
4379end
4380par.pxcmx='1';
4381par.pxcmy='1';
4382A=imread(file_ima);%read the first image to get the size
4383sizim=size(A);
4384par.npx=num2str(sizim(2));
4385par.npy=num2str(sizim(1));
4386%time=get(handles.RootName,'UserData'); %get the set of times
4387par.gridname=get(handles.grid_civ2,'String');
4388par.gridflag='y';
4389if strcmp(par.gridname,'')|| isempty(par.gridname)
4390    par.gridname='nogrid';
4391    par.gridflag='n';
4392end
4393
4394function cmd_CIV1=CIV1_CMD(filename,param)
4395%TODO : include filename in par_civ1
4396%------------------------------------------------------------------------
4397%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4398
4399%changes : filename_cmx -> filename ( no extension )
4400
4401filename=regexprep(filename,'.nc','')
4402
4403if isequal(param.civ1.Dt,'0')
4404    param.civ1.Dt='1' ;%case of 'displacement' mode
4405end
4406param.civ1.filename_ima_a=regexprep(param.civ1.filename_ima_a,'.png','');
4407param.civ1.filename_ima_b=regexprep(param.civ1.filename_ima_b,'.png','');
4408fid=fopen([filename '.civ1.cmx'],'w');
4409fprintf(fid,['##############   CMX file' '\n' ]);
4410fprintf(fid,   ['FirstImage ' regexprep(param.civ1.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4411fprintf(fid,   ['LastImage  ' regexprep(param.civ1.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4412fprintf(fid,  ['XX' '\n' ]);
4413fprintf(fid,  ['Mask ' param.civ1.maskflag '\n' ]);
4414fprintf(fid,  ['MaskName ' regexprep(param.civ1.maskname,'\\','\\\\') '\n' ]);
4415fprintf(fid,   ['ImageSize ' param.civ1.npx ' ' param.civ1.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4416fprintf(fid,   ['CorrelationBoxesSize ' param.civ1.ibx ' ' param.civ1.iby '\n' ]);
4417fprintf(fid,   ['SearchBoxeSize ' param.civ1.isx ' ' param.civ1.isy '\n' ]);
4418fprintf(fid,   ['RO ' param.civ1.rho '\n' ]);
4419fprintf(fid,   ['GridSpacing ' param.civ1.dx ' ' param.civ1.dy '\n' ]);
4420fprintf(fid,   ['XX 1.0' '\n' ]);
4421fprintf(fid,   ['Dt_TO ' param.civ1.Dt ' ' param.civ1.T0 '\n' ]);
4422fprintf(fid,  ['PixCmXY ' param.civ1.pxcmx ' ' param.civ1.pxcmy '\n' ]);
4423fprintf(fid,  ['XX 1' '\n' ]);
4424fprintf(fid,   ['ShiftXY ' param.civ1.shiftx ' '  param.civ1.shifty '\n' ]);
4425fprintf(fid,  ['Grid ' param.civ1.gridflag '\n' ]);
4426fprintf(fid,   ['GridName ' regexprep(param.civ1.gridname,'\\','\\\\') '\n' ]);
4427fprintf(fid,   ['XX 85' '\n' ]);
4428fprintf(fid,   ['XX 1.0' '\n' ]);
4429fprintf(fid,   ['XX 1.0' '\n' ]);
4430fprintf(fid,   ['Hart 1' '\n' ]);
4431fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4432fprintf(fid,   ['Deformation 0' '\n' ]);
4433fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4434fprintf(fid,   ['IntensityMin 0' '\n' ]);
4435fprintf(fid,  ['SeuilImage n' '\n' ]);
4436fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4437fprintf(fid,   ['ImageToUse ' param.civ1.term_a ' ' param.civ1.term_b '\n' ]); % VERIFIER ?
4438fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4439fclose(fid);
4440
4441% cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
4442% cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4443% namelog=regexprep(namelog,'\\','\\\\');
4444if(isunix)
4445    cmd_CIV1=['cp -f ' filename '.civ1.cmx ' filename '.cmx'];
4446    cmd_CIV1=[cmd_CIV1 '\n'...
4447        param.global.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
4448    cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.log' ' ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.civ1.log' '\n' 'chmod g+w ' filename '.nc'];%rename .log as .civ1.log and set the netcdf result file for group user writting
4449   % cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.cmx' ' ' filename '.civ1.cmx' '\n'];%rename .cmx as .civ1.cmx
4450else %Windows system
4451%                     flname=regexprep(flname,'\\','\\\\');
4452%                     cmd=[cmd 'copy /Y "' flname '.civ1.cmx" "' flname '.cmx"\n'];
4453%     filename=regexprep(filename,'\\','\\\\');
4454    cmd_CIV1=['copy /Y "' filename '.civ1.cmx" "' filename '.cmx"'];% copy the .civ1.cmx parameter file to .cmx
4455    cmd_CIV1=[cmd_CIV1 '\n "' regexprep(param.global.Civ1Bin,'\\','\\\\') '" -f "' filename '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4456%     namelog=regexprep(namelog,'\\','\\\\');
4457    cmd_CIV1=[cmd_CIV1 '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ1.log"']; %preserve the log file as .civ1.log
4458
4459    %  cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.civ1.cmx"'];
4460end
4461
4462%------------------------------------------------------------------------
4463% --- CIV1  Unified
4464function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
4465%------------------------------------------------------------------------
4466%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4467%global CivBin%name of the executable for civ1 calculation
4468
4469civ1.image1=par.filename_ima_a;
4470civ1.image2=par.filename_ima_b;
4471civ1.imageSize_X=par.npx;
4472civ1.imageSize_Y=par.npy;
4473civ1.outputFileName=[filename '.nc'];
4474civ1.correlationBoxesSize_X=par.ibx;
4475civ1.correlationBoxesSize_Y=par.iby;
4476civ1.searchBoxesSize_X=par.isx;
4477civ1.searchBoxesSize_Y=par.isy;
4478civ1.globalShift_X=par.shiftx;
4479civ1.globalShift_Y=par.shifty;
4480civ1.ro=par.rho;
4481civ1.hart='y';
4482if isequal(par.gridflag,'y')
4483    civ1.grid=par.gridname;
4484else
4485    civ1.grid='n';
4486    civ1.gridSpacing_X=par.dx;
4487    civ1.gridSpacing_Y=par.dy;
4488end
4489if isequal(par.maskflag,'y')
4490    civ1.mask=par.maskname;
4491end
4492civ1.dt=par.Dt;
4493civ1.unit='pixel';
4494civ1.absolut_time_T0=par.T0;
4495civ1.pixcmx=par.pxcmx;
4496civ1.pixcmy=par.pxcmy;
4497civ1.convectFlow='n';
4498
4499xml_civ1_parameters=civ1;
4500
4501%------------------------------------------------------------------------
4502% --- CIV2  Unified
4503function civ2=CIV2_CMD_Unified(filename,namelog,par)
4504%------------------------------------------------------------------------
4505%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4506%global CivBin%name of the executable for civ1 calculation
4507filename=regexprep(filename,'.nc','');
4508
4509civ2.image1=par.filename_ima_a;
4510civ2.image2=par.filename_ima_b;
4511civ2.imageSize_X=par.npx;
4512civ2.imageSize_Y=par.npy;
4513civ2.inputFileName=[par.filename_nc1 '.nc'];
4514civ2.outputFileName=[filename '.nc'];
4515civ2.correlationBoxesSize_X=par.ibx;
4516civ2.correlationBoxesSize_Y=par.iby;
4517civ2.ro=par.rho;
4518%civ2.decimalShift=par.decimal;
4519%civ2.deformation=par.deformation;
4520if isequal(par.decimal,'1')
4521    civ2.decimalShift='y';
4522else
4523    civ2.decimalShift='n';
4524end
4525if isequal(par.deformation,'1')
4526    civ2.deformation='y';
4527else
4528    civ2.deformation='n';
4529end
4530if isequal(par.gridflag,'y')
4531    civ2.grid=par.gridname;
4532else
4533    civ2.grid='n';
4534    civ2.gridSpacing_X=par.dx;
4535    civ2.gridSpacing_Y=par.dy;
4536end
4537civ2.gridSpacing_X='10';
4538civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4539if isequal(par.maskflag,'y')
4540    civ2.mask=par.maskname;
4541else
4542    civ2.mask='n';
4543end
4544civ2.dt=par.Dt;
4545civ2.unit='pixel';
4546civ2.absolut_time_T0=par.T0;
4547civ2.pixcmx=par.pxcmx;
4548civ2.pixcmy=par.pxcmy;
4549civ2.convectFlow='n';
4550civ2.pixcmx=par.pxcmx;
4551civ2.pixcmy=par.pxcmy;
4552civ2.convectFlow='n';
4553
4554%------------------------------------------------------------------------
4555% --- CIV2  CIV2  CIV2 CIV2
4556function cmd_CIV2=CIV2_CMD(filename,param)
4557%------------------------------------------------------------------------
4558%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4559% global civ2Bin sge%name of the executable for civ1 calculation
4560if isequal(par.Dt,'0')
4561    param.civ2.Dt='1' ;%case of 'displacement' mode
4562end
4563param.civ2.filename_ima_a=regexprep(param.civ2.filename_ima_a,'.png','');
4564param.civ2.filename_ima_b=regexprep(param.civ2.filename_ima_b,'.png','');% bug : .png appears two times ?
4565[fid,errormsg]=fopen([filename '.civ2.cmx'],'w');
4566if isequal(fid,-1)
4567    msgbox_uvmat('ERROR',errormsg)
4568    cmd_CIV2='';
4569    return
4570end
4571fprintf(fid,['##############   CMX file' '\n' ]);
4572fprintf(fid,   ['FirstImage ' regexprep(param.civ2.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4573fprintf(fid,   ['LastImage  ' regexprep(param.civ2.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4574fprintf(fid,  ['XX' '\n' ]);
4575fprintf(fid, ['Mask ' param.civ2.maskflag '\n' ]);
4576fprintf(fid, ['MaskName ' regexprep(param.civ2.maskname,'\\','\\\\') '\n' ]);% for windows compatibility
4577fprintf(fid, ['ImageSize ' param.civ2.npx ' ' param.civ2.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4578fprintf(fid, ['CorrelationBoxesSize ' param.civ2.ibx ' ' param.civ2.iby '\n' ]);
4579fprintf(fid, ['SearchBoxeSize ' param.civ2.ibx ' ' param.civ2.iby '\n']);
4580fprintf(fid, ['RO ' param.civ2.rho '\n']);
4581fprintf(fid, ['GridSpacing ' param.civ2.dx ' ' param.civ2.dy '\n']);
4582fprintf(fid, ['XX 1.0' '\n' ]);
4583fprintf(fid, ['Dt_TO ' param.civ2.Dt ' ' param.civ2.T0 '\n' ]);
4584fprintf(fid, ['PixCmXY ' param.civ2.pxcmx ' ' param.civ2.pxcmy '\n' ]);
4585fprintf(fid, ['XX 1' '\n' ]);
4586fprintf(fid, 'ShiftXY 0 0\n');
4587fprintf(fid, ['Grid ' param.civ2.gridflag '\n' ]);
4588fprintf(fid, ['GridName ' regexprep(param.civ2.gridname,'\\','\\\\') '\n']);
4589fprintf(fid, ['XX 85' '\n' ]);
4590fprintf(fid, ['XX 1.0' '\n' ]);
4591fprintf(fid, ['XX 1.0' '\n' ]);
4592fprintf(fid, ['Hart 1' '\n' ]);
4593fprintf(fid, ['DecimalShift ' param.civ2.decimal '\n']);
4594fprintf(fid, ['Deformation ' param.civ2.deformation '\n']);
4595fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4596fprintf(fid,   ['IntensityMin 0' '\n' ]);
4597fprintf(fid,  ['SeuilImage n' '\n' ]);
4598fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4599fprintf(fid,   ['ImageToUse ' param.civ2.term_a ' ' param.civ2.term_b '\n' ]); % VERIFIER ?
4600fprintf(fid, ['ImageUsedBefore ' regexprep(param.civ2.filename_nc1,'\\','\\\\') '\n']);
4601fclose(fid);
4602
4603if(isunix)
4604    cmd_CIV2=['cp -f ' filename '.civ2.cmx ' filename '.cmx\n'];
4605    cmd_CIV2=[cmd_CIV2 param.global.Civ2Bin ' -f ' filename  '.cmx >' filename '.log' ]; % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
4606    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' filename '.log' ' ' filename '.civ2.log' '\n' 'chmod g+w ' filename '.nc'];%preserve the log file as .civ2.log
4607%    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' filename '.cmx' ' ' filename '.civ2.cmx' '\n'];%rename .cmx as .civ2.cmx, the result file is named [filename '.nc'] by CIVx
4608
4609else
4610    filename=regexprep(filename,'\\','\\\\');
4611    cmd_CIV2=['copy /Y "' filename '.civ2.cmx" "' filename '.cmx"'];
4612    cmd_CIV2=[cmd_CIV2 '\n "' regexprep(param.global.Civ2Bin,'\\','\\\\') '" -f "' filename  '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4613    cmd_CIV2=[cmd_CIV2 '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ2.log"'];
4614 %    cmd_CIV2=[cmd_CIV2 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.civ2.cmx"'];
4615end
4616
4617
4618%------------------------------------------------------------------------
4619%--read images and convert them to the uint16 format used for PIV
4620function A=read_image(filename,type_ima,num,movieobject)
4621%------------------------------------------------------------------------
4622%num is the view number needed for an avi movie
4623switch type_ima
4624    case 'movie'
4625        A=read(movieobject,num);
4626    case 'avi'
4627        mov=aviread(filename,num);
4628        A=frame2im(mov(1));
4629    case 'multimage'
4630        A=imread(filename,num);
4631    case 'image'
4632        A=imread(filename);
4633end
4634siz=size(A);
4635if length(siz)==3;%color images
4636    A=sum(double(A),3);
4637    A=uint16(A);
4638end
4639
4640%------------------------------------------------------------------------
4641function ref_i_Callback(hObject, eventdata, handles)
4642%------------------------------------------------------------------------
4643mode_list=get(handles.CivMode,'String');
4644mode_value=get(handles.CivMode,'Value');
4645mode=mode_list{mode_value};
4646find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4647if isequal(mode,'series(Di)') || ...% we do patch2 only
4648        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4649    find_netcpair_civ2(hObject, eventdata, handles);
4650end
4651
4652%------------------------------------------------------------------------
4653function ref_j_Callback(hObject, eventdata, handles)
4654%------------------------------------------------------------------------
4655mode_list=get(handles.CivMode,'String');
4656mode_value=get(handles.CivMode,'Value');
4657mode=mode_list{mode_value};
4658if isequal(get(handles.CIV1,'Value'),0)|| isequal(mode,'series(Dj)')
4659    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4660end
4661if isequal(mode,'series(Dj)') || ...
4662        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4663    find_netcpair_civ2(hObject, eventdata, handles);
4664end
4665
4666%------------------------------------------------------------------------
4667function ref_i_civ2_Callback(hObject, eventdata, handles)
4668%------------------------------------------------------------------------
4669mode_list=get(handles.CivMode,'String');
4670mode_value=get(handles.CivMode,'Value');
4671mode=mode_list{mode_value};
4672find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4673
4674%------------------------------------------------------------------------
4675function ref_j_civ2_Callback(hObject, eventdata, handles)
4676%------------------------------------------------------------------------
4677mode_list=get(handles.CivMode,'String');
4678mode_value=get(handles.CivMode,'Value');
4679mode=mode_list{mode_value};
4680if  isequal(mode,'series(Dj)')
4681    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4682end
4683
4684%------------------------------------------------------------------------
4685% --- Executes on button press in compare.
4686function compare_Callback(hObject, eventdata, handles)
4687%------------------------------------------------------------------------
4688test=get(handles.compare,'Value');
4689if test==2 || test==3 % case 'dispalcemen' or 'stereo PIV'
4690    filebase=get(handles.RootName,'String');
4691    browse=get(handlesRootName,'Userdata');
4692    browse.nom_type_ima1=browse.nom_type_ima;
4693    set(handlesRootName,'UserData',browse);
4694    set(handles.sub_txt,'Visible','on')
4695    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
4696    mode_store=get(handles.CivMode,'String');%get the present 'mode'
4697    set(handles.compare,'UserData',mode_store);%store the mode display
4698    set(handles.CivMode,'Visible','off')
4699    if test==2
4700        set(handles.CivMode,'Visible','off')
4701        set(handles.CivMode,'Value',1) % mode 'civX' selected by default
4702    else
4703        set(handles.CivMode,'Visible','on')
4704        set(handles.CivMode,'Value',3) % mode 'Matlab' selected for stereo
4705    end
4706   
4707    %% menuopen an image file with the browser
4708    ind_opening=1;%default
4709    browse.incr_pair=[0 0]; %default
4710    oldfile=get(handles.RootName,'String');
4711     menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
4712       '*.xml',  '.xml files '; ...
4713        '*.civ',  '.civ files '; ...
4714        '*.png','.png image files'; ...
4715        '*.jpg',' jpeg image files'; ...
4716        '*.tif','.tif image files'; ...
4717        '*.avi;*.AVI','.avi movie files'; ...
4718        '*.nc','.netcdf files'; ...
4719        '*.*',  'All Files (*.*)'};
4720    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file of the second series',oldfile);
4721    fileinput=[PathName FileName];%complete file name
4722    sizf=size(fileinput);
4723    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4724    [path,name,ext]=fileparts(fileinput)
4725    [path1]=fileparts(filebase);
4726    if isunix
4727        [status,path]=system(['readlink ' path])
4728        [status,path1]=system(['readlink ' path1])% look for the true path in case of symbolic paths
4729    end
4730    if ~strcmp(path1,path)
4731        msgbox_uvmat('ERROR','The second image series must be in the same directory as the first one')
4732        return
4733     end
4734%     set(handles.RootName_1,'String',name);
4735    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
4736    set(handles.RootName_1,'String',RootFile);
4737    browse=get(handlesRootName,'UserData');
4738    browse.nom_type_ima_1=nom_type;
4739    set(handlesRootName,'UserData',browse)
4740   
4741    %check image extension
4742    if ~strcmp(ext,get(handles.ImaExt,'String'))
4743        msgbox_uvmat('ERROR','The second image series must have the same extension name as the first one')
4744        return
4745    end
4746   
4747    %% check coincidence of image sizes
4748%     ref_i=get(handles.ref_i,'string');
4749%     ref_j=get(handles.ref_j,'string');
4750%     [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc]=set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
4751%     A=imread(filecell.ima1.civ1{1});
4752%     A_1=imread(fileinput);
4753%     npxy=size(A);
4754%     npxy_1=size(A_1);
4755%     if ~isequal(size(A),size(A_1))
4756%         msgbox_uvmat('ERROR','The two input image series do not have the same size')
4757%         return
4758%     end
4759else
4760    set(handles.CivMode,'Visible','on')
4761    set(handles.RootName_1,'Visible','Off');
4762    set(handles.sub_txt,'Visible','off')
4763    set(handles.RootName_1,'String',[]);
4764    mode_store=get(handles.compare,'UserData');
4765    set(handles.CivMode,'Value',1)
4766    set(handles.CivMode,'String',mode_store)
4767    set(handles.test_stereo1,'Value',0)
4768    set(handles.check_Stereo,'Value',0)
4769    set(handles.CivMode,'Value',1) % mode 'civX' selected by default
4770end
4771if test==3 && get(handles.PATCH1,'Value')
4772    set(handles.test_stereo1,'Visible','on')
4773else
4774    set(handles.test_stereo1,'Visible','off')
4775end
4776if test==3 && get(handles.PATCH2,'Value')
4777    set(handles.check_Stereo,'Visible','on')
4778else
4779    set(handles.check_Stereo,'Visible','off')
4780end
4781mode_Callback(hObject, eventdata, handles)
4782
4783%------------------------------------------------------------------------
4784% --- Executes on button press in get_ref_fix1.
4785function get_ref_fix1_Callback(hObject, eventdata, handles)
4786%------------------------------------------------------------------------
4787filebase=get(handles.RootName,'String');
4788[FileName, PathName, filterindex] = uigetfile( ...
4789    {'*.nc', ' (*.nc)';
4790    '*.nc',  'netcdf files '; ...
4791    '*.*', 'All Files (*.*)'}, ...
4792    'Pick a file',filebase);
4793
4794fileinput=[PathName FileName];
4795sizf=size(fileinput);
4796if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4797[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4798ref.filebase=fullfile(Path,File);
4799ref.num_a=stra2num(str_a);
4800ref.num_b=stra2num(str_b);
4801ref.num1=str2double(field_count);
4802ref.num2=str2double(str2);
4803browse=[];%initialisation
4804if ~isequal(ref.ext,'.nc')
4805    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4806    return
4807end
4808set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
4809set(handles.ref_fix1,'UserData',ref)
4810menu_field{1}='civ1';
4811Data=nc2struct(fileinput,[]);
4812if isfield(Data,'patch') && isequal(Data.patch,1)
4813    menu_field{2}='filter1';
4814end
4815if isfield(Data,'civ2') && isequal(Data.civ2,1)
4816    menu_field{3}='civ2';
4817end
4818if isfield(Data,'patch2') && isequal(Data.patch2,1)
4819    menu_field{4}='filter2';
4820end
4821set(handles.field_ref1,'String',menu_field);
4822set(handles.field_ref1,'Value',length(menu_field));
4823set(handles.inf_sup1,'Value',2);
4824set(handles.thresh_vel,'String','1');%default threshold
4825set(handles.ref_fix1,'Enable','on')
4826
4827%------------------------------------------------------------------------
4828% --- Executes on button press in get_ref_fix2.
4829function get_ref_fix2_Callback(hObject, eventdata, handles)
4830%------------------------------------------------------------------------
4831if isequal(get(handles.get_ref_fix2,'Value'),1)
4832    filebase=get(handles.RootName,'String');
4833    [FileName, PathName, filterindex] = uigetfile( ...
4834        {'*.nc', ' (*.nc)';
4835        '*.nc',  'netcdf files '; ...
4836        '*.*', 'All Files (*.*)'}, ...
4837        'Pick a file',filebase);
4838    fileinput=[PathName FileName];
4839    sizf=size(fileinput);
4840    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4841    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4842    ref.filebase=fullfile(Path,File);
4843    ref.num_a=stra2num(str_a);
4844    ref.num_b=stra2num(str_b);
4845    ref.num1=str2num(field_count);
4846    ref.num2=str2num(str2);
4847    browse=[];%initialisation
4848    if ~isequal(ref.ext,'.nc')
4849        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4850        return
4851    end
4852    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
4853    set(handles.ref_fix2,'UserData',ref)
4854    menu_field{1}='civ1';
4855    Data=nc2struct(fileinput,[]);
4856    if isfield(Data,'patch') & isequal(Data.patch,1)
4857        menu_field{2}='filter1';
4858    end
4859    if isfield(Data,'civ2') & isequal(Data.civ2,1)
4860        menu_field{3}='civ2';
4861    end
4862    if isfield(Data,'patch2') & isequal(Data.patch2,1)
4863        menu_field{4}='filter2';
4864    end
4865    set(handles.field_ref2,'String',menu_field);
4866    set(handles.field_ref2,'Value',length(menu_field));
4867    set(handles.inf_sup2,'Value',2);
4868    set(handles.thresh_vel2,'String','1');%default threshold
4869    set(handles.ref_fix2,'Enable','on')
4870    set(handles.ref_fix2,'Visible','on')
4871    set(handles.field_ref2,'Visible','on')
4872else
4873    set(handles.ref_fix2,'Visible','off')
4874    set(handles.field_ref2,'Visible','off')
4875end
4876
4877%------------------------------------------------------------------------
4878function ref_fix1_Callback(hObject, eventdata, handles)
4879%------------------------------------------------------------------------
4880set(handles.inf_sup1,'Value',1);
4881set(handles.field_ref1,'Value',1)
4882set(handles.field_ref1,'String',{' '})
4883set(handles.ref_fix1,'UserData',[]);
4884set(handles.ref_fix1,'String','');
4885set(handles.thresh_vel1,'String','0');
4886
4887%------------------------------------------------------------------------
4888function ref_fix2_Callback(hObject, eventdata, handles)
4889%------------------------------------------------------------------------
4890set(handles.inf_sup2,'Value',1);
4891set(handles.field_ref2,'Value',1)
4892set(handles.field_ref2,'String',{' '})
4893set(handles.ref_fix2,'UserData',[]);
4894set(handles.ref_fix2,'String','');
4895set(handles.thresh_vel2,'String','0');
4896
4897%------------------------------------------------------------------------
4898% --- Executes on button press in test_stereo1.
4899function test_stereo1_Callback(hObject, eventdata, handles)
4900%------------------------------------------------------------------------
4901if isequal(get(handles.test_stereo1,'Value'),0)
4902    set(handles.subdomain_patch1,'Visible','on')
4903    set(handles.rho_patch1,'Visible','on')
4904else
4905    set(handles.subdomain_patch1,'Visible','off')
4906    set(handles.rho_patch1,'Visible','off')
4907end
4908
4909%------------------------------------------------------------------------
4910% --- Executes on button press in check_Stereo.
4911function StereoCheck_Callback(hObject, eventdata, handles)
4912%------------------------------------------------------------------------
4913if isequal(get(handles.check_Stereo,'Value'),0)
4914    set(handles.num_SubdomainSize,'Visible','on')
4915    set(handles.num_SmoothParam,'Visible','on')
4916else
4917    set(handles.num_SubdomainSize,'Visible','off')
4918    set(handles.num_SmoothParam,'Visible','off')
4919end
4920
4921%------------------------------------------------------------------------
4922% --- Executes on button press in ImaThreshold.
4923function ImaThreshold_Callback(hObject, eventdata, handles)
4924%------------------------------------------------------------------------
4925if isequal(get(handles.ImaThreshold,'Value'),1)
4926    set(handles.MinIma,'Visible','on')
4927    set(handles.MaxIma,'Visible','on')
4928else
4929    set(handles.MinIma,'Visible','off')
4930    set(handles.MaxIma,'Visible','off')
4931end
4932
4933%------------------------------------------------------------------------
4934% --- Executes on button press in ImaThreshold2.
4935function ImaThreshold2_Callback(hObject, eventdata, handles)
4936%------------------------------------------------------------------------
4937if isequal(get(handles.ImaThreshold2,'Value'),1)
4938    set(handles.MinIma2,'Visible','on')
4939    set(handles.MaxIma2,'Visible','on')
4940else
4941    set(handles.MinIma2,'Visible','off')
4942    set(handles.MaxIma2,'Visible','off')
4943end
4944
4945%------------------------------------------------------------------------
4946% --- Executes on button press in TestCiv1: display image correlation function
4947function TestCiv1_Callback(hObject, eventdata, handles)
4948%------------------------------------------------------------------------
4949set(handles.TestCiv1,'BackgroundColor',[1 1 0])
4950drawnow
4951test_civ1=get(handles.TestCiv1,'Value');
4952if test_civ1
4953    ref_i=str2double(get(handles.ref_i,'String'));
4954    if strcmp(get(handles.ref_j,'Visible'),'on')
4955        ref_j=str2double(get(handles.ref_j,'String'));
4956    else
4957        ref_j=1;%default
4958    end
4959    [filecell,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2,nom_type_nc,file_ref_fix1,file_ref_fix2]=...
4960        set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
4961    Data.ListVarName={'ny','nx','A'};
4962    Data.VarDimName={'ny','nx',{'ny','nx'}};
4963    Data.A=imread(filecell.ima1.civ1{1});
4964    Data.ny=[size(Data.A,1) 1];
4965    Data.nx=[1 size(Data.A,2)];
4966    par_civ1=read_param_civ1(handles,filecell.ima1.civ1{1});
4967    par_civ1.filename_ima_a=filecell.ima1.civ1{1};
4968    par_civ1.filename_ima_b=filecell.ima2.civ1{1};
4969    par_civ1.T0=0;
4970    par_civ1.Dt=1;
4971    Param.Civ1=par_civ1;
4972    Data=civ_uvmat(Param);
4973    Data.ListVarName=[Data.ListVarName {'ny','nx','A'}];
4974    Data.VarDimName=[Data.VarDimName {'ny','nx',{'ny','nx'}}];
4975    Data.A=imread(filecell.ima1.civ1{1});
4976    Data.ny=[size(Data.A,1) 1];
4977    Data.nx=[1 size(Data.A,2)];
4978    hview_field=view_field(Data);
4979    set(0,'CurrentFigure',hview_field)
4980    hhview_field=guihandles(hview_field);
4981    set(hview_field,'CurrentAxes',hhview_field.axes3)
4982    ViewData=get(hview_field,'UserData');
4983    ViewData.CivHandle=handles.civ;% indicate the handle of the civ GUI in view_field
4984    ViewData.axes3.B=imread(filecell.ima2.civ1{1});%store the second image in the UserData of the GUI view_field
4985    ViewData.axes3.X=Data.Civ1_X; %keep the set of points in memeory
4986    ViewData.axes3.Y=Data.Civ1_Y;
4987    set(hview_field,'UserData',ViewData)
4988    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
4989    if isempty(corrfig)
4990        corrfig=figure;
4991        set(corrfig,'tag','corrfig')
4992        set(corrfig,'name','image correlation')
4993        set(corrfig,'DeleteFcn',{@closeview_field})%
4994    end
4995    set(handles.TestCiv1,'BackgroundColor',[1 0 0])
4996else
4997    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
4998    if ~isempty(corrfig)
4999        delete(corrfig)
5000    end
5001    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
5002    if ~isempty(hview_field)
5003        delete(hview_field)
5004    end
5005end
5006
5007function closeview_field(gcbo,eventdata)
5008hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
5009    if ~isempty(hview_field)
5010        delete(hview_field)
5011    end
5012%-------------------------------------------------------------------
5013% --- Executes on button press in status.
5014function status_Callback(hObject, eventdata, handles)
5015%-------------------------------------------------------------------
5016val=get(handles.status,'Value');
5017if val==0
5018    set(handles.status,'BackgroundColor',[0 1 0])
5019    hfig=findobj(allchild(0),'name','civ_status');
5020    if ~isempty(hfig)
5021        delete(hfig)
5022    end
5023    return
5024end
5025set(handles.status,'BackgroundColor',[1 1 0])
5026drawnow
5027listtype={'civ1','fix1','patch1','civ2','fix2','patch2'};
5028box_test(1)=get(handles.CIV1,'Value');
5029box_test(2)=get(handles.FIX1,'Value');
5030box_test(3)=get(handles.PATCH1,'Value');
5031box_test(4)=get(handles.CIV2,'Value');
5032box_test(5)=get(handles.FIX2,'Value');
5033box_test(6)=get(handles.PATCH2,'Value');
5034option_civ=find(box_test,1,'last');%last selected option (non-zero index of box_test)
5035filecell=get(handles.civ,'UserData');%retrieve the list of output files expected for PIV
5036test_new=0;
5037if ~isfield(filecell,'nc')
5038    test_new=1;
5039    [ref_i,ref_j,errormsg]=find_ref_indices(handles);
5040    if ~isempty(errormsg)
5041        msgbox_uvmat('ERROR',errormsg)
5042        return
5043    end
5044    filecell=set_civ_filenames(handles,ref_i,ref_j,box_test);%determine the output file expected from the GUI status
5045end
5046if ~isequal(box_test(4:6),[0 0 0])
5047    civ_files=filecell.nc.civ2;%case of civ2 operations
5048else
5049    civ_files=filecell.nc.civ1;
5050end
5051[root,filename,ext]=fileparts(civ_files{1});
5052[rootroot,subdir,extdir]=fileparts(root);
5053hfig=findobj(allchild(0),'name','civ_status');
5054if isempty(hfig)
5055    hfig=figure('DeleteFcn',@stop_status);
5056    set(hfig,'name','civ_status')
5057    hlist=uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @open_view_field,'tag','list');
5058    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','msgbox','Max',2,'String','checking files...');
5059    uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
5060    uicontrol('Style','pushbutton','Units','normalized', 'Position', [0.7 0.01 0.2 0.07],'String','OK','FontWeight','bold','FontUnits','normalized','FontSize',0.9,'Callback',@close_GUI);
5061    BarPosition=[0.05 0.81 0.01 0.05];
5062    hwaitbar=uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
5063    drawnow
5064end
5065% datnum=[];
5066Tabchar={};
5067nbfiles=numel(civ_files);
5068count=0;
5069testrecent=0;
5070while count<nbfiles
5071    count=0;
5072    datnum=zeros(1,nbfiles);
5073    for ifile=1:nbfiles
5074        detect=exist(civ_files{ifile},'file'); % check the existence of the file
5075        option=0;
5076        if detect==0
5077            option_str='not created';
5078        else
5079            datfile=dir(civ_files{ifile});
5080            if isfield(datfile,'datenum')
5081                datnum(ifile)=datfile.datenum;%only available in recent matlab versions
5082                testrecent=1;
5083            end
5084            filefound(ifile)={datfile.name};
5085            lastfield='';
5086            % check the content  netcdf file
5087            Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','patch2','fix2','civ2','patch','fix');
5088            if ~isempty(Data.patch2) && isequal(Data.patch2,1)
5089                option=6;
5090                option_str='patch2';
5091            elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
5092                option=5;
5093                option_str='fix2';
5094            elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
5095                option=4;
5096                option_str='civ2';
5097            elseif ~isempty(Data.patch) && isequal(Data.patch,1);
5098                option=3;
5099                option_str='patch1';
5100            elseif ~isempty(Data.fix) && isequal(Data.fix,1);
5101                option=2;
5102                option_str='fix1';
5103            else
5104                option=1;
5105                option_str='civ1';
5106            end
5107        end
5108        if option >= option_civ
5109            count=count+1;
5110        end
5111        [rr,filename,ext]=fileparts(civ_files{ifile});
5112        Tabchar{ifile,1}=[fullfile([subdir extdir],filename) ext  '...' option_str];
5113    end
5114    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
5115    if isempty(datnum)
5116        if testrecent
5117            message='no civ result created yet';
5118        else
5119            message='';
5120        end
5121    else
5122        datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
5123        [first,ind]=min(datnum);
5124        [last,indlast]=max(datnum);
5125        if test_new
5126            message='existing file status, no processing launched yet';
5127        else
5128        message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
5129            ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
5130        end
5131    end
5132    hfig=findobj(allchild(0),'name','civ_status');
5133    if isempty(hfig)% the status list has been deleted
5134        return
5135    else
5136        hlist=findobj(hfig,'tag','list');
5137        hmsgbox=findobj(hfig,'tag','msgbox');
5138        hwaitbar=findobj(hfig,'tag','waitbar');
5139        set(hlist,'String',Tabchar)
5140        set(hmsgbox,'String', message)
5141        if count>0 && ~test_new
5142            BarPosition(3)=0.9*count/nbfiles;
5143            set(hwaitbar,'Position',BarPosition)
5144        end
5145    end
5146    set(hlist,'UserData',rootroot)
5147    pause(10)% wait 10 seconds for next check
5148end
5149
5150   
5151%------------------------------------------------------------------------   
5152% call 'view_field.fig' to display the  field selected in the list of 'status'
5153function open_view_field(hObject, eventdata)
5154%------------------------------------------------------------------------
5155list=get(hObject,'String');
5156index=get(hObject,'Value');
5157rootroot=get(hObject,'UserData');
5158filename=list{index};
5159ind_dot=findstr(filename,'...');
5160filename=filename(1:ind_dot-1);
5161filename=fullfile(rootroot,filename);
5162delete(get(hObject,'parent'))%delete the display figure to stop the check process
5163if exist(filename,'file')%visualise the vel field if it exists
5164    uvmat(filename)
5165    set(gcbo,'Value',1)
5166end
5167
5168%------------------------------------------------------------------------   
5169% launched by pressing OK on the status figure
5170function close_GUI(hObject, eventdata)
5171%------------------------------------------------------------------------
5172    delete(gcbf)
5173   
5174%------------------------------------------------------------------------   
5175% launched by deleting the status figure
5176function stop_status(hObject, eventdata)
5177%------------------------------------------------------------------------
5178hciv=findobj(allchild(0),'tag','civ');
5179hhciv=guidata(hciv);
5180set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
5181set(hhciv.status,'BackgroundColor',[0 1 0])
5182
5183%------------------------------------------------------------------------
5184% % --- Executes on button press in CivMode.
5185% function CivMode_Callback(hObject, eventdata, handles)
5186% %------------------------------------------------------------------------
5187% Listprog=get(handles.CivMode,'String');
5188% index=get(handles.CivMode,'Value');
5189% prog=Listprog{index};
5190% switch prog
5191%     case 'CivX'
5192%         set(handles.thresh_patch1,'Visible','off')
5193%         set(handles.thresh_text1,'Visible','off')
5194%         set(handles.num_MaxDiff,'Visible','off')
5195%         set(handles.thresh_text2,'Visible','off')
5196%         set(handles.rho,'Style','edit')
5197%         set(handles.rho,'String','1')
5198%         set(handles.BATCH,'Enable','on')
5199%     case 'CivAll'
5200%         if get(handles.PATCH1,'Value')
5201%             set(handles.thresh_patch1,'Visible','on')
5202%             set(handles.thresh_text1,'Visible','on')
5203%         end
5204%         set(handles.rho,'Style','edit')
5205%         set(handles.rho,'String','1')
5206%         set(handles.BATCH,'Enable','on')
5207%     case 'CivUvmat'
5208%       
5209% end
5210
5211
5212% --- Executes on button press in check_Stereo.
5213function check_Stereo_Callback(hObject, eventdata, handles)
5214% hObject    handle to check_Stereo (see GCBO)
5215% eventdata  reserved - to be defined in a future version of MATLAB
5216% handles    structure with handles and user data (see GUIDATA)
5217
5218% Hint: get(hObject,'Value') returns toggle state of check_Stereo
5219
5220
5221
5222function num_SubdomainSize_Callback(hObject, eventdata, handles)
5223% hObject    handle to num_SubdomainSize (see GCBO)
5224% eventdata  reserved - to be defined in a future version of MATLAB
5225% handles    structure with handles and user data (see GUIDATA)
5226
5227% Hints: get(hObject,'String') returns contents of num_SubdomainSize as text
5228%        str2double(get(hObject,'String')) returns contents of num_SubdomainSize as a double
5229
5230
5231
5232function num_SmoothingParam_Callback(hObject, eventdata, handles)
5233% hObject    handle to num_SmoothingParam (see GCBO)
5234% eventdata  reserved - to be defined in a future version of MATLAB
5235% handles    structure with handles and user data (see GUIDATA)
5236
5237% Hints: get(hObject,'String') returns contents of num_SmoothingParam as text
5238%        str2double(get(hObject,'String')) returns contents of num_SmoothingParam as a double
5239
5240
5241
5242function num_MaxDiff_Callback(hObject, eventdata, handles)
5243% hObject    handle to num_MaxDiff (see GCBO)
5244% eventdata  reserved - to be defined in a future version of MATLAB
5245% handles    structure with handles and user data (see GUIDATA)
5246
5247% Hints: get(hObject,'String') returns contents of num_MaxDiff as text
5248%        str2double(get(hObject,'String')) returns contents of num_MaxDiff as a double
5249
5250
5251function num_Nx_Callback(hObject, eventdata, handles)
5252% hObject    handle to num_Nx (see GCBO)
5253% eventdata  reserved - to be defined in a future version of MATLAB
5254% handles    structure with handles and user data (see GUIDATA)
5255
5256% Hints: get(hObject,'String') returns contents of num_Nx as text
5257%        str2double(get(hObject,'String')) returns contents of num_Nx as a double
5258
5259
5260function num_Ny_Callback(hObject, eventdata, handles)
5261% hObject    handle to num_Ny (see GCBO)
5262% eventdata  reserved - to be defined in a future version of MATLAB
5263% handles    structure with handles and user data (see GUIDATA)
5264
5265% Hints: get(hObject,'String') returns contents of num_Ny as text
5266%        str2double(get(hObject,'String')) returns contents of num_Ny as a double
5267
5268
5269% --------------------------------------------------------------------
5270function MenuHelp_Callback(hObject, eventdata, handles)
5271path_to_uvmat=which ('uvmat');% check the path of uvmat
5272pathelp=fileparts(path_to_uvmat);
5273helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
5274if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
5275else
5276    addpath (fullfile(pathelp,'uvmat_doc'))
5277    web([helpfile '#civ'])
5278end
5279
5280% --------------------------------------------------------------------
5281function CivX_Callback(hObject, eventdata, handles)
5282%set(handles.thresh_patch1,'Visible','off')
5283set(handles.thresh_text1,'Visible','off')
5284set(handles.num_MaxDiff,'Visible','off')
5285set(handles.thresh_text2,'Visible','off')
5286set(handles.rho,'Style','edit')
5287set(handles.rho,'String','1')
5288set(handles.BATCH,'Enable','on')
5289
5290% --------------------------------------------------------------------
5291function Matlab_Callback(hObject, eventdata, handles)
5292set(handles.Matlab,'checked','on')
5293set(handles.CivX,'checked','off')
5294if get(handles.PATCH1,'Value')
5295    set(handles.thresh_patch1,'Visible','on')
5296    set(handles.thresh_text1,'Visible','on')
5297end
5298if get(handles.PATCH2,'Value')
5299    set(handles.num_MaxDiff,'Visible','on')
5300    set(handles.thresh_text2,'Visible','on')
5301end
5302set(handles.rho,'Style','popupmenu')
5303set(handles.rho,'Value',1)
5304set(handles.rho,'String',{'1';'2'})
5305set(handles.BATCH,'Enable','off')
5306
5307% --------------------------------------------------------------------
5308% --- read a panel with handle 'handle' producing a structure 'struct'
5309function struct=read_panel(handle)
5310hchild=get(handle,'children');
5311for ichild=1:numel(hchild)
5312    object_style=get(hchild(ichild),'Style');
5313    tag=get(hchild(ichild),'tag');
5314    check_input=1;%default
5315    switch object_style
5316        case 'edit'
5317            switch(tag(1:4))
5318                case 'num_'
5319                    input=str2double(get(hchild(ichild),'String'));
5320                    %deal with undefined input: retrieve the default value stored as UserData
5321                    if isnan(input)
5322                        input=get(hchild(ichild),'UserData');
5323                        set(hchild(ichild),'String',num2str(input))
5324                    end
5325                case 'txt_'
5326                    input=get(hchild(ichild),'String');
5327            end
5328            key=tag(5:end);
5329        case 'checkbox'
5330            input=get(hchild(ichild),'Value');
5331            key=tag(7:end);
5332        otherwise
5333            check_input=0;         
5334    end
5335    if check_input
5336        key
5337        eval(['struct.' key '=input;'])
5338    end
5339end
Note: See TracBrowser for help on using the repository browser.