source: trunk/src/civ.m @ 107

Last change on this file since 107 was 107, checked in by sommeria, 14 years ago

aver_stat and time_series: bug repairs for reading images, for time series, the projection of images on a line now works
get_field: various bug repairs, export field on work space activated
struct2nc and struct2nc_toolbox.m: small bug repaired
nomtype2pair: test on string input introduced to avoid error
cell2tab: test on multiline input introduced
civ: pb about cell repair (bug with recent versions of matlab)

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