source: trunk/src/civ.m @ 71

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

civ3D updated: introduction of image size
imadoc2struct: reding of image size from the xml file
set_object, view_field and related functions: improvement of projection object editing
mouse: possibility of adjusting the calibrations points with the mouse

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