source: trunk/src/civ.m @ 39

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

civ: bug corrected for civ in batch
series and uvmat: further improvement and debugging for the menu of field transforms

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