source: trunk/src/civ.m @ 73

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

ima2vol: provides volume images in 16 bit (presently needed for the software 3D3C)
set_grid: small bug repairs
uvmat: small bug repairs.
civ: bug repair following previous changes (RUN and BATCH functions merged)
plot_field: cleaning of parameter transmission. Introduction of a parameter handle of a text box (not yet used)

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