source: trunk/src/civ.m @ 41

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

-relabel_i_j: improve the question dlg box for image transform
-aver_stat,time_series,merge_proj: include modification of field transform and call of set_object.
-series: introduce a browser for set_object
-ima2vol: create volume images (png images of a slice concacened along y) for PIV 3D
-set_object: suppress TITLE, introduce Tooltip helps for parameters
-civ: fixed bug on BATCH sequences

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