source: trunk/src/civ.m @ 110

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

reads PARAM.xml in uvmat folder, then in matlab path

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