source: trunk/src/civ.m @ 128

Last change on this file since 128 was 128, checked in by sommeria, 13 years ago

series: give writting access to the group for all subdirectories produced
uvmat.fig: change of vect and scalar frames (to be consistent with view_field)
uvmat: various cleaning
plot_field: various cleaning to improve axes definition and avoid blinking
geometry_calib: improved dispay of point coordiantes, improved link with dataview for REPLICATE.
struct2nc: repair bug , file was not closed.
cell2tab: cleaning
dataview: improve the browser
civ: solve pb of image naming

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