source: trunk/src/civ.m @ 8

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