source: trunk/src/civ.m @ 131

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

remove the linux !at command which is not understood everywhere

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=['!. ' filename_bat ' &'];
2707%                 cmd_str=['!at -qb now -f ' filename_bat ' &']; %ou at -qb now -f bad idea...
2708                eval(cmd_str);
2709                display(cmd_str);
2710            else
2711                cmd_str=['!' filename_bat ' &'];
2712                eval(cmd_str);
2713                display(cmd_str);
2714            end
2715        end
2716    end
2717end
2718% set(handles.BATCH, 'Enable','On')
2719% set(handles.BATCH,'BackgroundColor',[1 0 0])
2720
2721%save interface state
2722if isfield(filecell,'nc')
2723    if isfield(filecell.nc,'civ2')
2724        fileresu=filecell.nc.civ2{1,1};
2725    else
2726        fileresu=filecell.nc.civ1{1,1};
2727    end
2728end
2729[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2730namedoc=fullfile(RootPath,subdir,RootFile);
2731detect=1;
2732while detect==1
2733    namefigfull=[namedoc '.fig'];
2734    hh=dir(namefigfull);
2735    if ~isempty(hh)
2736        detect=1;
2737        namedoc=[namedoc '.0'];
2738    else
2739        detect=0;
2740    end
2741end
2742saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2743
2744%------------------------------------------------------------------------
2745%OUTPUT:
2746% filecell: structure of input and output files
2747%         .ima1.civ1{i,j}, .ima1.civ2{i,j}; set of first image names for  correlations, for civ1 and civ2
2748%         .ima2.civ1{i,j}, .ima2.civ2{i,j} ; set of second image names for correlations
2749%         .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)
2750%         .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)
2751%         .nc.civ1{i,j}, .nc.civ2{i,j}; set of nc files for PIV results
2752%         .ncA.civ1{i,j}, .ncA.civ2{i,j}; set of nc files for PIV results with camA (then .nc corresponds to camB)
2753%         .st{i,j};   set of nc files for the combined stereo fields
2754% num1_civ1(i),num2_civ1(i): lists of first and last i indices for civ1
2755% num_a_civ1(j),num_b_civ1(j): lists of first and last j indices for civ1
2756% num1_civ2(i),num2_civ2(i): lists of first and last i indices for civ2
2757% num_a_civ2(j),num_b_civ2(j): lists of first and last j indices for civ2
2758% nom_type_nc: nomenclature type for velocity files
2759% file_ref_fix1,file_ref_fix2: reference files possibly used by Fix1 and 2
2760function [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]=...
2761    set_civ_filenames(handles,compare,box_test)
2762%------------------------------------------------------------------------
2763filecell=[];%default
2764%get the filename root, nomenclature and numbers
2765filebase=get(handles.RootName,'String');
2766browse=get(handles.browse_root,'UserData');
2767compare_list=get(handles.compare,'String');
2768val=get(handles.compare,'Value');
2769compare=compare_list{val};
2770if strcmp(compare,'displacement')
2771    mode='displacement';
2772else
2773    mode_list=get(handles.mode,'String');
2774    mode_value=get(handles.mode,'Value');
2775    mode=mode_list{mode_value};
2776end
2777time=get(handles.RootName,'UserData'); %get the set of times
2778ext_ima=get(handles.ImaExt,'String');
2779nom_type_nc=browse.nom_type_nc;
2780if isfield(browse,'nom_type_ima')
2781    nom_type_ima2=browse.nom_type_ima;
2782end
2783if isempty(nom_type_ima2),nom_type_ima2='1';end; %default
2784if isempty(nom_type_nc),nom_type_nc='_i1-i2';end; %default
2785[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2786    find_pair_indices(handles,mode);
2787%determine the new filebase for 'displacement' mode (comparison of two series)
2788filebase_B=filebase;% root name of the second field series for stereo
2789if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2790%     test_disp=1;
2791    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2792    [Path2,Name2]=fileparts(filebase_B);
2793    Path1=Path2;
2794    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2795    filebase_A=fullfile(Path1,Name1);
2796    if length(Name1)>6
2797        Name1=Name1(end-5:end);
2798    end
2799    if length(Name2)>6
2800        Name2=Name2(end-5:end);
2801    end
2802    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2803else
2804%     test_disp=0;
2805    filebase_A=filebase;
2806    nom_type_ima1=nom_type_ima2;
2807    filebase_AB=filebase;
2808end
2809if strcmp(compare,'displacement')
2810    filebase_ima1=filebase_A;
2811    filebase_ima2=filebase_B;
2812    filebase_nc=filebase_AB; %root name for the result of civ2
2813else
2814    filebase_ima1=filebase_B;
2815    filebase_ima2=filebase_B;
2816    filebase_nc=filebase_B;
2817end
2818
2819%determine reference files for fix:
2820file_ref_fix1={};%default
2821file_ref_fix2={};
2822nbfield=length(num1_civ1);
2823nbslice=length(num_a_civ1);
2824if box_test(2)==1% fix1 performed
2825    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2826    if ~isempty(ref)
2827        first_i=str2num(get(handles.first_i,'String'));
2828        last_i=str2num(get(handles.last_i,'String'));
2829        incr_i=str2num(get(handles.incr_i,'String'));
2830        first_j=str2num(get(handles.first_j,'String'));
2831        last_j=str2num(get(handles.last_j,'String'));
2832        incr_j=str2num(get(handles.incr_j,'String'));
2833        num_i_ref=first_i:incr_i:last_i;
2834        num_j_ref=first_j:incr_j:last_j;
2835        if isequal(mode,'displacement')
2836            num_i1=num_i_ref;
2837            num_i2=num_i_ref;
2838            num_j1=num_j_ref;
2839            num_j2=num_j_ref;
2840        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2841            num_i1=num_i_ref;
2842            num_i2=num_i1;
2843            num_j1=ref.num_a*ones(size(num_i_ref));
2844            num_j2=ref.num_b*ones(size(num_i_ref));
2845        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2846            delta1=floor((ref.num2-ref.num1)/2);
2847            delta2=ceil((ref.num2-ref.num1)/2);
2848            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2849            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2850            if isempty(ref.num_a)
2851                ref.num_a=1;
2852            end
2853            num_j1=ref.num_a*ones(size(num_i1));
2854            num_j2=num_j1;
2855        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2856            delta1=floor((ref.num_b-ref.num_a)/2);
2857            delta2=ceil((ref.num_b-ref.num_a)/2);
2858            num_i1=ref.num1*ones(size(num_i_ref));
2859            num_i2=num_i1;
2860            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2861            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2862        end
2863        for ifile=1:nbfield
2864            for j=1:nbslice
2865                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);%
2866                file_ref_fix1(ifile,j)={file_ref};
2867                if ~exist(file_ref,'file')
2868                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2869                    filecell=[];
2870                    return
2871                end
2872            end
2873        end
2874    end
2875end
2876
2877%determine reference files for fix2:
2878if box_test(5)==1% fix2 performed
2879    ref=get(handles.ref_fix2,'UserData');
2880    if ~isempty(ref)
2881        first_i=str2double(get(handles.first_i,'String'));
2882        last_i=str2double(get(handles.last_i,'String'));
2883        incr_i=str2double(get(handles.incr_i,'String'));
2884        first_j=str2double(get(handles.first_j,'String'));
2885        last_j=str2double(get(handles.last_j,'String'));
2886        incr_j=str2double(get(handles.incr_j,'String'));
2887        num_i_ref=[first_i:incr_i:last_i];
2888        num_j_ref=[first_j:incr_j:last_j];
2889        if isequal(mode,'displacement')
2890            num_i1=num_i_ref;
2891            num_i2=num_i_ref;
2892            num_j1=num_j_ref;
2893            num_j2=num_j_ref;
2894        elseif isequal(mode,'pair j1-j2')
2895            num_i1=num_i_ref;
2896            num_i2=num_i1;
2897            num_j1=ref.num_a;
2898            num_j2=ref.num_b;
2899        elseif isequal(mode,'series(Di)')
2900            delta1=floor((ref.num2-ref.num1)/2);
2901            delta2=ceil((ref.num2-ref.num1)/2);
2902            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2903            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2904            num_j1=ref.num_a*ones(size(num_i1));
2905            num_j2=num_j1;
2906        elseif isequal(mode,'series(Dj)')
2907            delta1=floor((ref.num_b-ref.num_a)/2);
2908            delta2=ceil((ref.num_b-ref.num_a)/2);
2909            num_i1=ref.num1*ones(size(num_i_ref));
2910            num_i2=num_i1;
2911            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2912            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2913        end
2914        for ifile=1:nbfield
2915            for j=1:nbslice
2916                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);%
2917                file_ref_fix2(ifile,j)={file_ref};
2918                if ~exist(file_ref,'file')
2919                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2920                    filecell={};
2921                    return
2922                end
2923            end
2924        end
2925    end
2926end
2927
2928%check dir
2929subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2930subdir_civ2=get(handles.subdir_civ2,'String');
2931if isequal(subdir_civ1,''),subdir_civ1='A'; end% put default subdir
2932if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2933currentdir=pwd;%store the current working directory
2934[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2935if ~exist(Path_ima,'dir')
2936    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2937    filecell={};
2938    return
2939end
2940cd(Path_ima);%move to the directory of the images: needed to create the result dir by 'mkdir'
2941dircur=pwd; %current working directory
2942m2='';
2943[xx,message]=fileattrib(Path_ima);
2944if ~isempty(message) && ~isequal(message.UserWrite,1)
2945    msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2946    filecell={};
2947    cd(currentdir);
2948    return
2949end
2950
2951%check the existence of the netcdf and image files involved
2952% %%%%%%%%%%%%  case CIV1 activated   %%%%%%%%%%%%%
2953if box_test(1)==1;
2954    detect=1;
2955    vers=0;
2956    subdir_civ1_new=subdir_civ1;
2957    while detect==1 %create a new subdir if the netcdf files already exist
2958        for ifile=1:nbfield
2959            for j=1:nbslice
2960                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);
2961                detect=exist(filename,'file')==2;
2962                if detect% if a netcdf file already exists
2963                    indstr=regexp(subdir_civ1,'\D');
2964                    if indstr(end)<length(subdir_civ1) %subdir_civ1 ends by a number
2965                        vers=str2double(subdir_civ1(indstr(end)+1:end))+1;
2966                        subdir_civ1_new=[subdir_civ1(1:indstr(end)) num2str(vers)];
2967                    else
2968                        vers=vers+1;
2969                        subdir_civ1_new=[subdir_civ1(1:indstr(end)) '_' num2str(vers)];       
2970                    end
2971                    subdir_civ2=subdir_civ1;
2972                    break
2973                end
2974                filecell.nc.civ1(ifile,j)={filename};
2975            end
2976            if detect% if a netcdf file already exists
2977                break
2978            end
2979        end
2980        %create the new subdir_civ1
2981        if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2982            [xx,msg1]=mkdir(subdir_civ1_new);
2983            if ~strcmp(msg1,'')
2984                msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])%error message for directory creation
2985                cd(currentdir)
2986                filecell={};
2987                return
2988            else
2989                [xx,msg2] = fileattrib(subdir_civ1_new,'+w','g'); %yield writing access (+w) to user group (g)
2990                if ~strcmp(msg2,'')
2991                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg2])%error message for directory creation
2992                    cd(currentdir)
2993                    filecell={};
2994                    return
2995                end
2996            end
2997        end
2998        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2999%             vers=0;
3000%             subdir_civ1_new=subdir_civ1;
3001            for ifile=1:nbfield
3002                for j=1:nbslice
3003                    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);%
3004                    detect=exist(filename,'file')==2;
3005                    if detect% if a netcdf file already exists
3006                       indstr=regexp(subdir_civ1,'\D');
3007                       if indstr(end)<length(subdir_civ1) %subdir_civ1 ends by a number
3008                           vers=str2double(subdir_civ1(indstr(end)+1:end))+1;
3009                           subdir_civ1_new=[subdir_civ1(1:indstr(end)) num2str(vers)];
3010                       else
3011                           vers=vers+1;
3012                           subdir_civ1_new=[subdir_civ1 '_' num2str(vers)];
3013                       end
3014                       subdir_civ2=subdir_civ1;
3015                       break
3016                    end
3017                    filecell.ncA.civ1(ifile,j)={filename};
3018                end
3019                if detect% if a netcdf file already exists
3020                    break
3021                end
3022            end
3023%             subdir_civ1=subdir_civ1_new;
3024            %create the new subdir_civ1
3025            if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
3026                [xx,msg1]=mkdir(subdir_civ1_new);
3027                if ~strcmpl(msg1,'')
3028                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])
3029                    cd(currentdir)
3030                    filecell={};
3031                    return
3032                else
3033                    [xx,msg2] = fileattrib(subdir_civ1_new,'+w','g'); %yield writing access (+w) to user group (g)
3034                    if ~strcmp(msg2,'')
3035                        msgbox_uvmat('ERROR',['pb of permission for ' subdir_civ1_new ': ' msg2])%error message for directory creation
3036                        cd(currentdir)
3037                        filecell={};
3038                        return
3039                    end
3040                end
3041            end
3042        end
3043    end
3044    subdir_civ1=subdir_civ1_new;
3045    % get image names
3046    for ifile=1:nbfield
3047        for j=1:nbslice
3048            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
3049            idetect(j)=exist(filename,'file')==2;
3050            filecell.ima1.civ1(ifile,j)={filename}; %first image
3051            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
3052            idetect_1(j)=exist(filename,'file')==2;
3053            filecell.ima2.civ1(ifile,j)={filename};%second image
3054        end
3055        [idetectmin,indexj]=min(idetect);
3056        if idetectmin==0,
3057            msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
3058            filecell={};
3059            cd(currentdir)
3060            return
3061        end
3062        [idetectmin,indexj]=min(idetect_1);
3063        if idetectmin==0,
3064            msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
3065            filecell={};
3066            cd(currentdir)
3067            return
3068        end
3069    end
3070    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
3071        for ifile=1:nbfield
3072            for j=1:nbslice
3073                filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
3074                idetect(j)=exist(filename,'file')==2;
3075                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
3076                filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
3077                idetect_1(j)=exist(filename,'file')==2;
3078                filecell.imaA2.civ1(ifile,j)={filename};%second image
3079            end
3080            [idetectmin,indexj]=min(idetect);
3081            if idetectmin==0,
3082                msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
3083                filecell={};
3084                cd(currentdir)
3085                return
3086            end
3087            [idetectmin,indexj]=min(idetect_1);
3088            if idetectmin==0,
3089                msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
3090                filecell={};
3091                cd(currentdir)
3092                return
3093            end
3094        end
3095    end
3096   
3097    %%%%%%%%%%%%%  fix1 or patch1 activated but no civ1   %%%%%%%%%%%%%
3098elseif (box_test(2)==1 || box_test(3)==1);
3099    for ifile=1:nbfield
3100        for j=1:nbslice
3101            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3102                nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3103            detect=exist(filename,'file')==2;
3104            if detect==0
3105                msgbox_uvmat('ERROR',[filename ' not found'])
3106                filecell={};
3107                cd(currentdir)
3108                return
3109            end
3110            filecell.nc.civ1(ifile,j)={filename};
3111        end
3112    end
3113    if strcmp(compare,'stereo PIV')
3114        for ifile=1:nbfield
3115            for j=1:nbslice
3116                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);%
3117                filecell.ncA.civ1(ifile,j)={filename};
3118                if ~exist(filename,'file')
3119                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3120                    set(handles.RUN, 'Enable','On')
3121                    set(handles.RUN,'BackgroundColor',[1 0 0])
3122                    filecell={};
3123                    cd(currentdir)
3124                    return
3125                end
3126            end
3127        end
3128    end
3129end
3130
3131%%%%%%%%%%%%%  if civ2 performed with pairs different than civ1  %%%%%%%%%%%%%
3132testdiff=0;
3133if (box_test(4)==1)&&...
3134        ((get(handles.list_pair_civ1,'Value')~=get(handles.list_pair_civ2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
3135    testdiff=1;
3136    detect=1;
3137    vers=0;
3138    subdir_civ2_new=subdir_civ2;
3139    while detect==1 %create a new subdir if the netcdf files already exist
3140        for ifile=1:nbfield
3141            for j=1:nbslice
3142                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);%
3143                detect=exist(filename,'file')==2;
3144                if detect% if a netcdf file already exists
3145                    indstr=regexp(subdir_civ2,'\D');
3146                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
3147                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
3148                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
3149                    else
3150                        vers=vers+1;
3151                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
3152                    end
3153                    break
3154                end
3155                filecell.nc.civ2(ifile,j)={filename};
3156            end
3157            if detect% if a netcdf file already exists
3158                break
3159            end
3160        end
3161        %create the new subdir_civ2_new
3162        if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
3163            [xx,m2]=mkdir(subdir_civ2_new);
3164            [xx,msg2] = fileattrib(subdir_civ2_new,'+w','g'); %yield writing access (+w) to user group (g)
3165            if ~isequal(m2,'')
3166                msgbox_uvmat('ERROR',['cannot create ' subdir_civ2_new ': ' m2])
3167                filecell={};
3168                cd(currentdir)
3169                return
3170            end
3171        end
3172        if strcmp(compare,'stereo PIV')%check second nc series
3173            for ifile=1:nbfield
3174                for j=1:nbslice
3175                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3176                        nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2_new);%
3177                    detect=exist(filename,'file')==2;
3178                    if detect% if a netcdf file already exists
3179                        indstr=regexp(subdir_civ2,'\D');
3180                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
3181                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
3182                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
3183                        else
3184                           vers=vers+1;
3185                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
3186                        end
3187                        break
3188                    end
3189                    filecell.ncA.civ2(ifile,j)={filename};
3190                end
3191                if detect% if a netcdf file already exists
3192                    break
3193                end
3194            end
3195            subdir_civ2=subdir_civ2_new;
3196            %create the new subdir_civ1
3197            if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
3198                [xx,m2]=mkdir(subdir_civ2_new);
3199                 [xx,msg2] = fileattrib(subdir_civ2_new,'+w','g'); %yield writing access (+w) to user group (g)
3200                if ~isequal(m2,'')
3201                    msgbox_uvmat('ERROR', ['cannot create ' subdir_civ2_new ': ' m2])%error message for directory creation
3202                    cd(currentdir)
3203                    filecell={};
3204                    return
3205                end
3206            end
3207        end
3208    end
3209    subdir_civ2=subdir_civ2_new;
3210end
3211cd(currentdir);%come back to the current working directory
3212
3213%%%%%%%%%%%%%  if civ2 results are obtained or used  %%%%%%%%%%%%%
3214if box_test(4)==1 || box_test(5)==1 || box_test(6)==1 %civ2
3215    %check source netcdf file of civ1 estimates
3216    if box_test(1)==0; %no civ1 performed
3217        for ifile=1:nbfield
3218            for j=1:nbslice
3219                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3220                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3221                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
3222                if ~exist(filename,'file')
3223                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3224                    filecell={};
3225                    return
3226                end
3227                if ~testdiff % civ2 or patch2 are written in the same file as civ1
3228                    if box_test(4)==0 ; %check the existence of civ2 if it is not calculated
3229                        Data=nc2struct(filename,'ListGlobalAttribute','civ2');
3230                        if isempty(Data.civ2)||isequal(Data.civ2,0)
3231                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
3232                            filecell=[];
3233                            return
3234                        end
3235                    elseif box_test(3)==0; %check the existence of patch if it is not calculated
3236                        Data=nc2struct(filename,'ListGlobalAttribute','patch');
3237                        if isempty(Data.patch)||isequal(Data.patch,0)
3238                            msgbox_uvmat('ERROR',['no patch data in ' filename])
3239                            filecell=[];
3240                            return
3241                        end
3242                    end
3243                end
3244            end
3245        end
3246        if strcmp(compare,'stereo PIV')
3247            for ifile=1:nbfield
3248                for j=1:nbslice
3249                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3250                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3251                    filecell.ncA.civ2(ifile,j)={filename};
3252                    if ~exist(filename,'file')
3253                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3254                        set(handles.RUN, 'Enable','On')
3255                        set(handles.RUN,'BackgroundColor',[1 0 0])
3256                        return
3257                    end
3258                end
3259            end
3260        end
3261    end
3262   
3263    detect=1;
3264    %     while detect==1%creates a new subdir if the netcdf files already contain civ2 data
3265    for ifile=1:nbfield
3266        for j=1:nbslice
3267            filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3268                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
3269            detect=exist(filename,'file')==2;
3270            filecell.nc.civ2(ifile,j)={filename};
3271        end
3272    end
3273    %get first image names for civ2
3274    if box_test(1)==1 & isequal(num1_civ1,num1_civ2) & isequal(num_a_civ1,num_a_civ2)
3275        filecell.ima1.civ2=filecell.ima1.civ1;
3276    elseif box_test(4)==1
3277        for ifile=1:nbfield
3278            for j=1:nbslice
3279                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
3280                idetect_2(j)=exist(filename,'file')==2;
3281                filecell.ima1.civ2(ifile,j)={filename};%first image
3282            end
3283            [idetectmin,indexj]=min(idetect_2);
3284            if idetectmin==0,
3285                msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
3286                filecell=[];
3287                return
3288            end
3289        end
3290    end
3291   
3292    %get second image names for civ2
3293    if box_test(1)==1 & isequal(num2_civ1,num2_civ2) & isequal(num_b_civ1,num_b_civ2)
3294        filecell.ima2.civ2=filecell.ima2.civ1;
3295    elseif box_test(4)==1
3296        for ifile=1:nbfield
3297            for j=1:nbslice
3298                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
3299                idetect_3(j)=exist(filename,'file')==2;
3300                filecell.ima2.civ2(ifile,j)={filename};%first image
3301            end
3302            [idetectmin,indexj]=min(idetect_3);
3303            if idetectmin==0,
3304                msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
3305                filecell=[];
3306                return
3307            end
3308        end
3309    end
3310end
3311if (box_test(5)==1 || box_test(6)==1 ) && box_test(4)==0  % need to read an existing netcdf civ2 file
3312    if ~testdiff
3313        filecell.nc.civ2=filecell.nc.civ1;% file already checked
3314    else     % check the civ2 files
3315        for ifile=1:nbfield
3316            for j=1:nbslice
3317                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3318                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3319                filecell.nc.civ2(ifile,j)={filename};
3320                if ~exist(filename,'file')
3321                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
3322                    filecell=[];
3323                    return
3324                else
3325                    Data=nc2struct(filename,'ListGlobalAttribute','civ2');
3326                    if isempty(Data.civ2)||isequal(Data.civ2,0)
3327                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
3328                        filecell=[];
3329                        return
3330                    end
3331                end
3332            end
3333        end
3334    end
3335end
3336
3337%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
3338if strcmp(compare,'stereo PIV')
3339    if  box_test(3)==1 && isequal(get(handles.test_stereo1,'Value'),1)
3340        for ifile=1:nbfield
3341            for j=1:nbslice
3342                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3343                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3344                filecell.st(ifile,j)={filename};
3345            end
3346        end
3347    end
3348    if  box_test(6)==1 && isequal(get(handles.test_stereo2,'Value'),1)
3349        for ifile=1:nbfield
3350            for j=1:nbslice
3351                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3352                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3353                filecell.st(ifile,j)={filename};
3354            end
3355        end
3356    end
3357end
3358set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
3359set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
3360browse.nom_type_nc=nom_type_nc;
3361set(handles.browse_root,'UserData',browse); %update the nomenclature type for uvmat
3362
3363
3364%COPY IMAGES TO THE FORMAT .png IF NEEDED
3365if isequal(nom_type_ima1,'*')%case of movie files
3366    nom_type_imanew1='_i';
3367else
3368    nom_type_imanew1=nom_type_ima1;
3369end
3370if isequal(nom_type_ima2,'*')%case of movie files
3371    nom_type_imanew2='_i';
3372else
3373    nom_type_imanew2=nom_type_ima2;
3374end
3375if ~isequal(ext_ima,'.png')
3376    %%type of image file
3377    type_ima1='none';%default
3378    movieobject1=[];%default
3379    if isequal(lower(ext_ima),'.avi')
3380        hhh=which('mmreader');
3381        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3382            type_ima1='movie';
3383            movieobject1=mmreader([filebase_ima2 ext_ima]);
3384        else
3385            type_ima1='avi';
3386        end
3387    else
3388        form=imformats(ext_ima(2:end));
3389        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3390            if isequal(nom_type_ima1,'*');
3391                type_ima1='multimage';%image series in a single image file
3392            else
3393                type_ima1='image';
3394            end
3395        end
3396    end
3397    type_ima2='none';%default
3398    movieobject2=[];
3399    if isequal(lower(ext_ima),'.avi')
3400        hhh=which('mmreader');
3401        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
3402            type_ima2='movie';
3403            movieobject2=mmreader([filebase_ima2 ext_ima]);
3404        else
3405            type_ima2='avi';
3406        end
3407    else
3408        form=imformats(ext_ima(2:end));
3409        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
3410            if isequal(nom_type_ima1,'*');
3411                type_ima2='multimage';%image series in a single image file
3412            else
3413                type_ima2='image';
3414            end
3415        end
3416    end
3417    %npxy=get(handles.ImaExt,'UserData');
3418    % %     if numel(npxy)<2
3419    %
3420    %         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
3421    %         A=imread(filename);
3422    %         npxy=size(A);
3423    % %     end
3424    %     npy=npxy(1);
3425    %     npx=npxy(2);
3426    if box_test(1)==1 %if civ1 is performed
3427        h = waitbar(0,['copy images to the .png format for civ1']);% display a wait bar
3428        for ifile=1:nbfield
3429            waitbar(ifile/nbfield);
3430            for j=1:nbslice
3431                filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
3432                if ~exist(filename,'file')
3433                    A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
3434                    imwrite(A,filename,'BitDepth',16);
3435                end
3436                filecell.ima1.civ1(ifile,j)={filename};
3437                filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
3438                if ~exist(filename,'file')
3439                    A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
3440                    imwrite(A,filename,'BitDepth',16);
3441                end
3442                filecell.ima2.civ1(ifile,j)={filename};
3443            end
3444        end
3445        close(h)
3446    end
3447    if box_test(4)==1 %if civ2 is performed
3448        h = waitbar(0,['copy images to the .png format for civ2']);% display a wait bar
3449        for ifile=1:nbfield
3450            waitbar(ifile/nbfield);
3451            for j=1:nbslice
3452                filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3453                if ~exist(filename,'file')
3454                    A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
3455                    imwrite(A,filename,'BitDepth',16);
3456                end
3457                filecell.ima1.civ2(ifile,j)={filename};
3458                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3459                if ~exist(filename,'file')
3460                    A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
3461                    imwrite(A,filename,'BitDepth',16);
3462                end
3463                filecell.ima2.civ2(ifile,j)={filename};
3464            end
3465        end
3466        close(h);
3467    end
3468end
3469
3470
3471%------------------------------------------------------------------------
3472% --- PATCH
3473function cmd_PATCH=PATCH_CMD(filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,test_interp,PatchBin)
3474%------------------------------------------------------------------------
3475namelog=[filename_nc([1:end-3]) '_patch.log'];
3476if test_interp==0
3477    cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3478        '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3479else %nouveau programme patch
3480    cmd_PATCH=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3481        ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3482end
3483
3484%------------------------------------------------------------------------
3485% --- STEREO Interp
3486function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3487%------------------------------------------------------------------------
3488namelog=[filename_nc([1:end-3]) '_stinterp.log'];
3489cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3490    ' -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
3491
3492%------------------------------------------------------------------------
3493% --- Executes on button press in CIV1.
3494function CIV1_Callback(hObject, eventdata, handles)
3495%------------------------------------------------------------------------
3496val=get(handles.CIV1,'Value');
3497if isequal(val,1)
3498    enable_civ1(handles,'on')
3499    enable_pair1(handles,'on')
3500else
3501    enable_civ1(handles,'off')
3502end
3503find_netcpair_civ1(hObject, eventdata, handles);
3504
3505%------------------------------------------------------------------------
3506% --- Executes on button press in FIX1.
3507function FIX1_Callback(hObject, eventdata, handles)
3508%------------------------------------------------------------------------
3509enable_fix1(handles,get(handles.FIX1,'Value'))
3510
3511%------------------------------------------------------------------------
3512% --- Executes on button press in PATCH1.
3513function PATCH1_Callback(hObject, eventdata, handles)
3514%------------------------------------------------------------------------
3515if get(handles.PATCH1,'Value')==1
3516    enable_patch1(handles)
3517else
3518    desable_patch1(handles)
3519end
3520
3521%------------------------------------------------------------------------
3522% --- Executes on button press in CIV2.
3523function CIV2_Callback(hObject, eventdata, handles)
3524%------------------------------------------------------------------------
3525state=get(handles.CIV2,'Value');
3526enable_civ2(handles,state)
3527if state
3528    find_netcpair_civ2(hObject, eventdata, handles)
3529    enable_pair1(handles,'on')
3530end
3531
3532%------------------------------------------------------------------------
3533% --- Executes on button press in FIX2.
3534function FIX2_Callback(hObject, eventdata, handles)
3535%------------------------------------------------------------------------
3536if get(handles.FIX2,'Value')==1
3537    enable_fix2(handles)
3538    if get(handles.CIV2,'Value')==0
3539        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3540    end
3541else
3542    desable_fix2(handles)
3543end
3544
3545%------------------------------------------------------------------------
3546% --- Executes on button press in PATCH2.
3547function PATCH2_Callback(hObject, eventdata, handles)
3548%------------------------------------------------------------------------
3549if get(handles.PATCH2,'Value')==1
3550    enable_patch2(handles)
3551    if get(handles.CIV2,'Value')==0
3552        find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3553    end
3554else
3555    desable_patch2(handles)
3556end
3557
3558%------------------------------------------------------------------------
3559function first_i_Callback(hObject, eventdata, handles)
3560%------------------------------------------------------------------------
3561% last_i_Callback(hObject, eventdata, handles)
3562first_i=str2num(get(handles.first_i,'String'));
3563% last_i=str2num(get(handles.last_i,'String'));
3564% ref_i=ceil((first_i+last_i)/2);
3565set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3566set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3567ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3568
3569%------------------------------------------------------------------------
3570function first_j_Callback(hObject, eventdata, handles)
3571%------------------------------------------------------------------------
3572first_j=str2num(get(handles.first_j,'String'));
3573set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3574ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3575
3576%------------------------------------------------------------------------
3577% --- Executes on button press in calcul_search: determine the search range isx,isy
3578function calcul_search_Callback(hObject, eventdata, handles)
3579%------------------------------------------------------------------------
3580%determine pair numbers
3581list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
3582index=get(handles.list_pair_civ1,'Value');
3583displ_num=get(handles.list_pair_civ1,'UserData');
3584time=get(handles.RootName,'UserData'); %get the set of times
3585pxcm_xy=get(handles.calcul_search,'UserData');
3586pxcmx=pxcm_xy(1);
3587pxcmy=pxcm_xy(2);
3588mode_list=get(handles.mode,'String');
3589mode_value=get(handles.mode,'Value');
3590mode=mode_list{mode_value};
3591if isequal (mode, 'series(Di)' )
3592    ref_i=str2num(get(handles.ref_i,'String'));
3593    num1=ref_i-floor(index/2);%  first image numbers
3594    num2=ref_i+ceil(index/2);
3595    num_a=1;
3596    num_b=1;
3597elseif isequal (mode, 'series(Dj)')
3598    num1=1;
3599    num2=1;
3600    ref_j=str2num(get(handles.ref_j,'String'));
3601    num_a=ref_j-floor(index/2);%  first image numbers
3602    num_b=ref_j+ceil(index/2);
3603elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3604    ref_i=str2num(get(handles.ref_i,'String'));
3605    num1=ref_i;
3606    num2=ref_i;
3607    num_a=displ_num(1,index);
3608    num_b=displ_num(2,index);
3609end
3610dt=time(num2,num_b)-time(num1,num_a);
3611ibx=str2num(get(handles.ibx,'String'));
3612iby=str2num(get(handles.iby,'String'));
3613umin=dt*pxcmx*str2num(get(handles.umin,'String'));
3614umax=dt*pxcmx*str2num(get(handles.umax,'String'));
3615vmin=dt*pxcmy*str2num(get(handles.vmin,'String'));
3616vmax=dt*pxcmy*str2num(get(handles.vmax,'String'));
3617shiftx=round((umin+umax)/2);
3618shifty=round((vmin+vmax)/2);
3619isx=(umax+2-shiftx)*2+ibx;
3620isx=2*ceil(isx/2)+1;
3621isy=(vmax+2-shifty)*2+iby;
3622isy=2*ceil(isy/2)+1;
3623set(handles.shiftx,'String',num2str(shiftx));
3624set(handles.shifty,'String',num2str(shifty));
3625set(handles.isx,'String',num2str(isx));
3626set(handles.isy,'String',num2str(isy));
3627
3628%------------------------------------------------------------------------
3629% --- Executes on carriage return on the subdir civ1 edit window
3630function subdir_civ1_Callback(hObject, eventdata, handles)
3631%------------------------------------------------------------------------
3632subdir=get(handles.subdir_civ1,'String');
3633set(handles.subdir_civ2,'String',subdir);
3634if get(handles.CIV1,'Value')==0
3635    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3636end
3637
3638%------------------------------------------------------------------------
3639% --- Executes on carriage return on the subdir civ1 edit window
3640function subdir_civ2_Callback(hObject, eventdata, handles)
3641%------------------------------------------------------------------------
3642%update the list of available pairs from netcdf files in the new directory
3643if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
3644    find_netcpair_civ2(hObject, eventdata, handles);
3645end
3646
3647%------------------------------------------------------------------------
3648% --- Executes on button press in get_mask_civ1.
3649function get_mask_civ1_Callback(hObject, eventdata, handles)
3650%------------------------------------------------------------------------
3651maskval=get(handles.get_mask_civ1,'Value');
3652if isequal(maskval,0)
3653    set(handles.mask_civ1,'String','')
3654else
3655    mask_displ='no mask'; %default
3656    filebase=get(handles.RootName,'String');
3657    [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3658    if isequal(flag_mask,1)
3659        mask_displ=[num2str(nbslice_mask) 'mask'];
3660    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3661        common_path=fileparts(filebase);
3662        filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3663        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3664        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3665            mask_displ='no mask';
3666        end
3667    end
3668    if isequal(mask_displ,'no mask')
3669        [FileName, PathName, filterindex] = uigetfile( ...
3670            {'*.png', ' (*.png)';
3671            '*.png',  '.png files '; ...
3672            '*.*', 'All Files (*.*)'}, ...
3673            'Pick a mask file *.png',filebase);
3674        mask_displ=fullfile(PathName,FileName);
3675        if ~exist(mask_displ,'file')
3676            mask_displ='no mask';
3677        end
3678    end
3679    if isequal(mask_displ,'no mask')
3680        set(handles.get_mask_civ1,'Value',0)
3681        set(handles.get_mask_fix1,'Value',0)
3682        set(handles.get_mask_civ2,'Value',0)
3683        set(handles.get_mask_fix2,'Value',0)
3684    else
3685        set(handles.get_mask_fix1,'Value',1)
3686        set(handles.get_mask_fix2,'Value',1)
3687    end
3688    set(handles.mask_civ1,'String',mask_displ)
3689    set(handles.mask_fix1,'String',mask_displ)
3690    set(handles.mask_civ2,'String',mask_displ)
3691    set(handles.mask_fix2,'String',mask_displ)
3692end
3693set(handles.mask_civ2,'Value',maskval)
3694
3695%------------------------------------------------------------------------
3696% --- Executes on button press in get_mask_fix1.
3697function get_mask_fix1_Callback(hObject, eventdata, handles)
3698%------------------------------------------------------------------------
3699maskval=get(handles.get_mask_fix1,'Value');
3700if isequal(maskval,0)
3701    set(handles.mask_fix1,'String','')
3702else
3703    mask_displ='no mask'; %default
3704    filebase=get(handles.RootName,'String');
3705    [nbslice, flag_mask]=get_mask(filebase,handles);
3706    if isequal(flag_mask,1)
3707        mask_displ=[num2str(nbslice) 'mask'];
3708    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3709        filebase_a=get(handles.RootName_1,'String');
3710        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3711        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3712            mask_displ='no mask';
3713        end
3714    end
3715    if isequal(mask_displ,'no mask')
3716        [FileName, PathName, filterindex] = uigetfile( ...
3717            {'*.png', ' (*.png)';
3718            '*.png',  '.png files '; ...
3719            '*.*', 'All Files (*.*)'}, ...
3720            'Pick a mask file *.png',filebase);
3721        mask_displ=fullfile(PathName,FileName);
3722        if ~exist(mask_displ,'file')
3723            mask_displ='no mask';
3724        end
3725    end
3726    if isequal(mask_displ,'no mask')
3727        set(handles.get_mask_fix1,'Value',0)
3728        set(handles.get_mask_civ2,'Value',0)
3729        set(handles.get_mask_fix2,'Value',0)
3730    else
3731        %set(handles.get_mask_civ2,'Value',1)
3732        set(handles.get_mask_fix2,'Value',1)
3733    end
3734    set(handles.mask_fix1,'String',mask_displ)
3735    set(handles.mask_civ2,'String',mask_displ)
3736    set(handles.mask_fix2,'String',mask_displ)
3737end
3738
3739%------------------------------------------------------------------------
3740% --- Executes on button press in get_mask_civ2.
3741function get_mask_civ2_Callback(hObject, eventdata, handles)
3742%------------------------------------------------------------------------
3743maskval=get(handles.get_mask_civ2,'Value');
3744if isequal(maskval,0)
3745    set(handles.mask_civ2,'String','')
3746else
3747    mask_displ='no mask'; %default
3748    filebase=get(handles.RootName,'String');
3749    [nbslice, flag_mask]=get_mask(filebase,handles);
3750    if isequal(flag_mask,1)
3751        mask_displ=[num2str(nbslice) 'mask'];
3752    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3753        filebase_a=get(handles.RootName_1,'String');
3754        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3755        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3756            mask_displ='no mask';
3757        end
3758    end
3759    if isequal(mask_displ,'no mask')
3760        [FileName, PathName, filterindex] = uigetfile( ...
3761            {'*.png', ' (*.png)';
3762            '*.png',  '.png files '; ...
3763            '*.*', 'All Files (*.*)'}, ...
3764            'Pick a mask file *.png',filebase);
3765        mask_displ=fullfile(PathName,FileName);
3766        if ~exist(mask_displ,'file')
3767            mask_displ='no mask';
3768        end
3769    end
3770    if isequal(mask_displ,'no mask')
3771        set(handles.get_mask_civ2,'Value',0)
3772        set(handles.get_mask_fix2,'Value',0)
3773    else
3774        set(handles.get_mask_fix2,'Value',1)
3775    end
3776    set(handles.mask_civ2,'String',mask_displ)
3777    set(handles.mask_fix2,'String',mask_displ)
3778end
3779
3780%------------------------------------------------------------------------
3781% --- Executes on button press in get_mask_fix2.
3782function get_mask_fix2_Callback(hObject, eventdata, handles)
3783%------------------------------------------------------------------------
3784maskval=get(handles.get_mask_fix2,'Value');
3785if isequal(maskval,0)
3786    set(handles.mask_fix2,'String','')
3787else
3788    mask_displ='no mask'; %default
3789    filebase=get(handles.RootName,'String');
3790    [nbslice, flag_mask]=get_mask(filebase,handles);
3791    if isequal(flag_mask,1)
3792        mask_displ=[num2str(nbslice) 'mask'];
3793    elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3794        filebase_a=get(handles.RootName_1,'String');
3795        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3796        if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3797            mask_displ='no mask';
3798        end
3799    end
3800    if isequal(mask_displ,'no mask')
3801        [FileName, PathName, filterindex] = uigetfile( ...
3802            {'*.png', ' (*.png)';
3803            '*.png',  '.png files '; ...
3804            '*.*', 'All Files (*.*)'}, ...
3805            'Pick a mask file *.png',filebase);
3806        mask_displ=fullfile(PathName,FileName);
3807        if ~exist(mask_displ,'file')
3808            mask_displ='no mask';
3809        end
3810    end
3811    if isequal(mask_displ,'no mask')
3812        set(handles.get_mask_fix2,'Value',0)
3813    end
3814    set(handles.mask_fix2,'String',mask_displ)
3815end
3816
3817%------------------------------------------------------------------------
3818% --- function called to look for mask files
3819function [nbslice, flag_mask]=get_mask(filebase,handles)
3820%------------------------------------------------------------------------
3821%detect mask files, images with appropriate file base
3822%[filebase '_' xx 'mask'], xx=nbslice
3823%flag_mask=1 indicates detection
3824
3825flag_mask=0;%default
3826nbslice=1;
3827
3828% subdir=get(handles.subdir_civ1,'String');
3829[Path,Name]=fileparts(filebase);
3830if ~isdir(Path)
3831    msgbox_uvmat('ERROR','no path for input files')
3832    return
3833end
3834currentdir=pwd;
3835cd(Path);%move in the dir of the root name filebase
3836maskfiles=dir([Name '_*mask_*.png']);%look for mask files
3837cd(currentdir);%come back to the current working directory
3838if ~isempty(maskfiles)
3839    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3840    % else
3841    flag_mask=1;
3842    maskname=maskfiles(1).name;% take the first mask file in the list
3843    [Path2,Name,ext]=fileparts(maskname);
3844    Namedouble=double(Name);
3845    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3846    ind_mask=findstr('mask',Name);
3847    i=ind_mask-1;
3848    while val(i)==0 & i>0
3849        i=i-1;
3850    end
3851    nbslice=str2num(Name(i+1:ind_mask-1));
3852    if ~isequal(nbslice,[]) & Name(i)=='_'
3853        flag_mask=1;
3854    else
3855        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3856        return
3857        nbslice=1;
3858    end
3859end
3860
3861%------------------------------------------------------------------------
3862% --- function called to look for grid files
3863function [nbslice, flag_mask]=get_grid(filebase,handles)
3864%------------------------------------------------------------------------
3865flag_mask=0;%default
3866nbslice=1;
3867[Path,Name]=fileparts(filebase);
3868currentdir=pwd;
3869cd(Path);%move in the dir of the root name filebase
3870maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
3871cd(currentdir);%come back to the current working directory
3872if ~isempty(maskfiles)
3873    flag_mask=1;
3874    maskname=maskfiles(1).name;% take the first mask file in the list
3875    [Path2,Name,ext]=fileparts(maskname);
3876    Namedouble=double(Name);
3877    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3878    ind_mask=findstr('grid',Name);
3879    i=ind_mask-1;
3880    while val(i)==0 & i>0
3881        i=i-1;
3882    end
3883    nbslice=str2num(Name(i+1:ind_mask-1));
3884    if ~isequal(nbslice,[]) & Name(i)=='_'
3885        flag_mask=1;
3886    else
3887        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
3888        return
3889        nbslice=1;
3890    end
3891end
3892
3893%------------------------------------------------------------------------
3894% --- transform numbers to letters
3895function str=num2stra(num,nom_type)
3896%------------------------------------------------------------------------
3897if isempty(nom_type)
3898    str='';
3899elseif strcmp(nom_type(end),'a')
3900    str=char(96+num);
3901elseif strcmp(nom_type(end),'A')
3902    str=char(96+num);
3903elseif isempty(nom_type(2:end))%a single index
3904    str='';
3905else
3906    str=num2str(num);
3907end
3908
3909%------------------------------------------------------------------------
3910function mask_civ1_Callback(hObject, eventdata, handles)
3911%------------------------------------------------------------------------
3912set(handles.mask_civ1,'UserData',[])
3913set(handles.mask_civ1,'String','')
3914
3915%------------------------------------------------------------------------
3916function mask_civ2_Callback(hObject, eventdata, handles)
3917%------------------------------------------------------------------------
3918set(handles.mask_civ2,'UserData',[])
3919set(handles.mask_civ2,'String','')
3920
3921%------------------------------------------------------------------------
3922function mask_fix1_Callback(hObject, eventdata, handles)
3923%------------------------------------------------------------------------
3924set(handles.mask_fix1,'UserData',[])
3925set(handles.mask_fix1,'String','')
3926
3927%------------------------------------------------------------------------
3928function mask_fix2_Callback(hObject, eventdata, handles)
3929%------------------------------------------------------------------------
3930set(handles.mask_fix2,'UserData',[])
3931set(handles.mask_fix2,'String','')
3932
3933%------------------------------------------------------------------------
3934% --- Executes on button press in list_subdir_civ1.
3935function list_subdir_civ1_Callback(hObject, eventdata, handles)
3936%------------------------------------------------------------------------
3937list_subdir_civ1=get(handles.list_subdir_civ1,'String');
3938val=get(handles.list_subdir_civ1,'Value');
3939if val>1
3940    subdir=list_subdir_civ1{val};
3941    set(handles.subdir_civ1,'String',subdir);
3942    set(handles.list_subdir_civ1,'Value',1);
3943end
3944
3945%------------------------------------------------------------------------
3946% --- Executes on button press in list_subdir_civ2.
3947function list_subdir_civ2_Callback(hObject, eventdata, handles)
3948%------------------------------------------------------------------------
3949list_subdir_civ2=get(handles.list_subdir_civ2,'String');
3950val=get(handles.list_subdir_civ2,'Value');
3951if val>1
3952    subdir=list_subdir_civ2{val};
3953    set(handles.subdir_civ2,'String',subdir);
3954    set(handles.list_subdir_civ2,'Value',1);
3955end
3956
3957%------------------------------------------------------------------------
3958% --- Executes on button press in browse_gridciv1.
3959function browse_gridciv1_Callback(hObject, eventdata, handles)
3960%------------------------------------------------------------------------
3961value=get(handles.browse_gridciv1,'Value');
3962testgrid=0;
3963if value
3964    filebase=get(handles.RootName,'String');
3965    [nbslice, flag_grid]=get_grid(filebase,handles);
3966    if isequal(flag_grid,1)
3967        filegrid=[num2str(nbslice) 'grid'];
3968        testgrid=1;
3969    else
3970        [FileName, PathName, filterindex] = uigetfile( ...
3971            {'*.grid', ' (*.grid)';
3972            '*.grid',  '.grid files '; ...
3973            '*.*', 'All Files (*.*)'}, ...
3974            'Pick a file',filebase);
3975        filegrid=fullfile(PathName,FileName);
3976        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
3977            testgrid=1;
3978        end
3979    end
3980end
3981if testgrid
3982    set(handles.browse_gridciv2,'Value',1)
3983    set(handles.get_gridpatch1,'Value',1)
3984    set(handles.get_gridpatch2,'Value',1)
3985    set(handles.dx_civ1,'Visible','off');
3986    set(handles.dy_civ1,'Visible','off');
3987    set(handles.dx_civ2,'Visible','off');
3988    set(handles.dy_civ2,'Visible','off');
3989    set(handles.grid_civ1,'String',filegrid)
3990    set(handles.grid_patch1,'String',filegrid)
3991    set(handles.grid_civ2,'String',filegrid)
3992    set(handles.grid_patch2,'String',filegrid)
3993else
3994    set(handles.browse_gridciv1,'Value',0);
3995    set(handles.browse_gridciv2,'Value',0);
3996    set(handles.get_gridpatch1,'Value',0)
3997    set(handles.get_gridpatch2,'Value',0)
3998    set(handles.dx_civ1,'Visible','on');
3999    set(handles.dy_civ1,'Visible','on');
4000    set(handles.dx_civ2,'Visible','on');
4001    set(handles.dy_civ2,'Visible','on');
4002    set(handles.grid_civ1,'String','')
4003    set(handles.grid_patch1,'String','')
4004    set(handles.grid_civ2,'String','')
4005    set(handles.grid_patch2,'String','')
4006end
4007
4008%------------------------------------------------------------------------
4009% --- Executes on button press in browse_gridciv1.
4010function browse_gridciv2_Callback(hObject, eventdata, handles)
4011%------------------------------------------------------------------------
4012value=get(handles.browse_gridciv2,'Value');
4013if value
4014    filebase=get(handles.RootName,'String');
4015    [nbslice, flag_grid]=get_grid(filebase,handles);
4016    if isequal(flag_grid,1)
4017        mask_displ=[num2str(nbslice) 'grid'];
4018        set(handles.grid_civ2,'String',mask_displ)
4019        set(handles.dx_civ2,'Visible','off');
4020        set(handles.dy_civ2,'Visible','off');
4021    else
4022        [FileName, PathName, filterindex] = uigetfile( ...
4023            {'*.grid', ' (*.grid)';
4024            '*.grid',  '.grid files '; ...
4025            '*.*', 'All Files (*.*)'}, ...
4026            'Pick a file',filebase);
4027        filegrid=fullfile(PathName,FileName);
4028        if isempty(FileName)|isempty(PathName)|isequal(FileName,0)|~exist(filegrid,'file')
4029            set(handles.browse_gridciv2,'Value',0);
4030            set(handles.grid_civ2,'string','');
4031            set(handles.dx_civ2,'Visible','on');
4032            set(handles.dy_civ2,'Visible','on');
4033            set(handles.grid_civ2,'string','');
4034        else
4035            set(handles.grid_civ2,'string',filegrid);
4036            set(handles.dx_civ2,'Visible','off');
4037            set(handles.dy_civ2,'Visible','off');
4038            set(handles.grid_civ2,'string',filegrid);
4039        end
4040    end
4041else
4042    set(handles.grid_civ2,'string','');
4043    set(handles.dx_civ2,'Visible','on');
4044    set(handles.dy_civ2,'Visible','on');
4045    set(handles.grid_civ2,'string','');
4046end
4047
4048% % --- Executes on button press in browse_gridciv2.
4049% function browse_gridciv2_Callback(hObject, eventdata, handles)
4050%
4051% filebase=get(handles.RootName,'String');
4052% [FileName, PathName, filterindex] = uigetfile( ...
4053%        {'*.grid', ' (*.grid)';
4054%         '*.grid',  '.grid files '; ...
4055%         '*.*', 'All Files (*.*)'}, ...
4056%         'Pick a file',filebase);
4057% filegrid=fullfile(PathName,FileName);
4058% set(handles.grid_civ2,'string',filegrid);
4059% set(handles.dx_civ2,'String',' ');
4060% set(handles.dy_civ2,'String',' ');
4061% % set(handles.grid_patch2,'string',filegrid);
4062
4063% --- Executes on button press in get_gridpatch1.
4064function get_gridpatch1_Callback(hObject, eventdata, handles)
4065% hObject    handle to get_gridpatch1 (see GCBO)
4066% eventdata  reserved - to be defined in a future version of MATLAB
4067% handles    structure with handles and user data (see GUIDATA)
4068
4069filebase=get(handles.RootName,'String');
4070[FileName, PathName, filterindex] = uigetfile( ...
4071    {'*.grid', ' (*.grid)';
4072    '*.grid',  '.grid files '; ...
4073    '*.*', 'All Files (*.*)'}, ...
4074    'Pick a file',filebase);
4075filegrid=fullfile(PathName,FileName);
4076set(handles.grid_patch1,'string',filegrid);
4077% set(handles.grid_patch2,'string',filegrid
4078
4079%------------------------------------------------------------------------
4080% --- Executes on button press in get_gridpatch2.
4081function get_gridpatch2_Callback(hObject, eventdata, handles)
4082%------------------------------------------------------------------------
4083
4084%------------------------------------------------------------------------
4085function enable_civ1(handles,state)
4086%------------------------------------------------------------------------
4087if isequal(state,0)
4088    state='off';
4089end
4090if isequal(state,1)
4091    state='on';
4092end
4093if isequal(state,'on')
4094    set(handles.frame_civ1,'BackgroundColor',[1 1 0])
4095    set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
4096    set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
4097else
4098    set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
4099    set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
4100    set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
4101end
4102set(handles.ibx,'Visible',state)
4103set(handles.iby,'Visible',state)
4104set(handles.isx,'Visible',state)
4105set(handles.isy,'Visible',state)
4106set(handles.shiftx,'Visible',state)
4107set(handles.shifty,'Visible',state)
4108set(handles.rho,'Visible',state)
4109set(handles.dx_civ1,'Visible',state)
4110set(handles.dy_civ1,'Visible',state)
4111set(handles.calcul_search,'Visible',state)
4112set(handles.u_text,'Visible',state)
4113set(handles.v_text,'Visible',state)
4114set(handles.min,'Visible',state)
4115set(handles.max,'Visible',state)
4116set(handles.umin,'Visible',state)
4117set(handles.umax,'Visible',state)
4118set(handles.vmin,'Visible',state)
4119set(handles.vmax,'Visible',state)
4120set(handles.grid_civ1,'Visible',state)
4121set(handles.mask_civ1,'Visible',state)
4122set(handles.browse_gridciv1,'Visible',state)
4123set(handles.get_mask_civ1,'Visible',state)
4124set(handles.parameters,'Visible',state)
4125set(handles.grid,'Visible',state)
4126set(handles.dx_civ1,'Visible',state)
4127set(handles.dy_civ1,'Visible',state)
4128set(handles.ImaThreshold,'Visible',state)
4129if isequal(state,'off')
4130    set(handles.MinIma,'Visible','off')
4131    set(handles.MaxIma,'Visible','off')
4132    set(handles.ImaThreshold,'Value',0)
4133end
4134set(handles.dx_civ1_title,'Visible',state)
4135set(handles.dy_civ1_title,'Visible',state)
4136set(handles.ImaThreshold_title,'Visible',state)
4137set(handles.ib_title,'Visible',state)
4138set(handles.is_title,'Visible',state)
4139set(handles.shift_title,'Visible',state)
4140set(handles.rho_title,'Visible',state)
4141
4142%------------------------------------------------------------------------
4143function enable_fix1(handles,state)
4144%------------------------------------------------------------------------
4145if isequal(state,0)
4146    state='off';
4147end
4148if isequal(state,1)
4149    state='on';
4150end
4151if isequal(state,'on')
4152    set(handles.frame_fix1,'BackgroundColor',[1 1 0])
4153else
4154    set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
4155end
4156set(handles.REMOVE,'Visible',state)
4157set(handles.vec_Fmin2,'Visible',state)
4158set(handles.vec_F2,'Visible',state)
4159set(handles.vec_F3,'Visible',state)
4160set(handles.thresh_vecC,'Visible',state)
4161set(handles.thresh_vecC_title,'Visible',state)
4162set(handles.thresh_vel,'Visible',state)
4163set(handles.thresh_vel_text,'Visible',state)
4164set(handles.mask_fix1,'Visible',state)
4165set(handles.get_mask_fix1,'Visible',state)
4166set(handles.get_ref_fix1,'Visible',state)
4167set(handles.ref_fix1,'Visible',state)
4168set(handles.inf_sup1,'Visible',state)
4169set(handles.field_ref1,'Visible',state)
4170
4171%------------------------------------------------------------------------
4172function enable_patch1(handles)
4173%------------------------------------------------------------------------
4174global patch_newBin
4175set(handles.frame_patch1,'BackgroundColor',[1 1 0])
4176set(handles.rho_patch1,'Visible','on')
4177set(handles.rho_text1,'Visible','on')
4178set(handles.thresh_patch1,'Visible','on')
4179set(handles.thresh_text1,'Visible','on')
4180set(handles.subdomain_patch1,'Visible','on')
4181set(handles.subdomain_text1,'Visible','on')
4182set(handles.nx_patch1,'Visible','on')
4183set(handles.ny_patch1,'Visible','on')
4184set(handles.nx_patch1_title,'Visible','on')
4185set(handles.ny_patch1_title,'Visible','on')
4186% if ~isempty(patch_newBin)
4187%     set(handles.test_interp,'Visible','on');
4188% end
4189set(handles.get_gridpatch1,'Visible','on')
4190set(handles.grid_patch1,'string','none');
4191set(handles.grid_patch1,'Visible','on')
4192
4193%------------------------------------------------------------------------
4194function desable_patch1(handles)
4195%------------------------------------------------------------------------
4196set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
4197set(handles.rho_patch1,'Visible','off')
4198set(handles.rho_text1,'Visible','off')
4199set(handles.thresh_patch1,'Visible','off')
4200set(handles.thresh_text1,'Visible','off')
4201set(handles.subdomain_patch1,'Visible','off')
4202set(handles.subdomain_text1,'Visible','off')
4203set(handles.nx_patch1,'Visible','off')
4204set(handles.ny_patch1,'Visible','off')
4205set(handles.nx_patch1_title,'Visible','off')
4206set(handles.ny_patch1_title,'Visible','off')
4207%set(handles.test_interp,'Visible','off')
4208set(handles.get_gridpatch1,'Visible','off')
4209set(handles.grid_patch1,'Visible','off')
4210
4211%------------------------------------------------------------------------
4212function enable_civ2(handles,state)
4213%------------------------------------------------------------------------
4214if isequal(state,0)
4215    state='off';
4216end
4217if isequal(state,1)
4218    state='on';
4219end
4220if isequal(state,'on')
4221    set(handles.frame_civ2,'BackgroundColor',[1 1 0])
4222    set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
4223    set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
4224    set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
4225else
4226    set(handles.frame_civ2,'BackgroundColor',[0.831 0.816 0.784])
4227    set(handles.frame_para_civ2,'BackgroundColor',[0.831 0.816 0.784])
4228    set(handles.frame_grid_civ2,'BackgroundColor',[0.831 0.816 0.784])
4229    set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
4230end
4231set(handles.ibx_civ2,'Visible',state)
4232set(handles.iby_civ2,'Visible',state)
4233set(handles.decimal,'Visible',state)
4234set(handles.deformation,'Visible',state)
4235set(handles.rho_civ2,'Visible',state)
4236set(handles.dx_civ2,'Visible',state)
4237set(handles.dy_civ2,'Visible',state)
4238set(handles.browse_gridciv2,'Visible',state)
4239set(handles.get_mask_civ2,'Visible',state)
4240set(handles.parameters,'Visible',state)
4241set(handles.grid,'Visible',state)
4242set(handles.parameters_text,'Visible',state)
4243set(handles.grid_text,'Visible',state)
4244set(handles.grid_civ2,'Visible',state)
4245set(handles.mask_civ2,'Visible',state)
4246set(handles.dx_civ2_title,'Visible',state)
4247set(handles.dy_civ2_title,'Visible',state)
4248set(handles.ibx_civ2_text,'Visible',state)
4249set(handles.rho_civ2_title,'Visible',state)
4250set(handles.ImaThreshold2,'Visible',state)
4251set(handles.ImaThreshold_title2,'Visible',state)
4252if isequal(state,'off')
4253    set(handles.MinIma2,'Visible','off')
4254    set(handles.MaxIma2,'Visible','off')
4255    set(handles.ImaThreshold2,'Value',0)
4256    if isequal(get(handles.FIX2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4257        set(handles.list_pair_civ2,'Visible','off')
4258        set(handles.subdir_civ2,'Visible','off')
4259        set(handles.subdir_civ2_text,'Visible','off')
4260        set(handles.dt_unit_civ2,'Visible','off')
4261        set(handles.ref_i_civ2,'Visible','off')
4262        set(handles.i_ref_civ2_title,'Visible','off')
4263        set(handles.j_ref_civ2_title,'Visible','off')
4264        set(handles.ref_j_civ2,'Visible','off')
4265    end
4266else
4267    set(handles.list_pair_civ2,'Visible','on')
4268    set(handles.subdir_civ2,'Visible','on')
4269    set(handles.subdir_civ2_text,'Visible','on')
4270    set(handles.dt_unit_civ2,'Visible','on')
4271    set(handles.ref_i_civ2,'Visible','on')
4272    set(handles.i_ref_civ2_title,'Visible','on')
4273    set(handles.j_ref_civ2_title,'Visible','on')
4274    set(handles.ref_j_civ2,'Visible','on')
4275end
4276set(handles.rho_civ2_title,'Visible',state)
4277
4278%------------------------------------------------------------------------
4279function enable_fix2(handles)
4280%------------------------------------------------------------------------
4281set(handles.frame_fix2,'BackgroundColor',[1 1 0])
4282set(handles.REMOVE2,'Visible','on')
4283set(handles.vec_Fmin2_2,'Visible','on')
4284set(handles.vec_F4,'Visible','on')
4285set(handles.vec_F3_2,'Visible','on')
4286set(handles.thresh_vec2C,'Visible','on')
4287set(handles.thresh_vec2C_text,'Visible','on')
4288set(handles.thresh_vel2,'Visible','on')
4289set(handles.thresh_vel2_text,'Visible','on')
4290set(handles.mask_fix2,'Visible','on')
4291set(handles.get_mask_fix2,'Visible','on')
4292set(handles.list_pair_civ2,'Visible','on')
4293set(handles.subdir_civ2,'Visible','on')
4294set(handles.subdir_civ2_text,'Visible','on')
4295set(handles.get_ref_fix2,'Visible','on')
4296set(handles.ref_fix2,'Visible','on')
4297set(handles.inf_sup2,'Visible','on')
4298set(handles.field_ref2,'Visible','on')
4299
4300%------------------------------------------------------------------------
4301function desable_fix2(handles)
4302%------------------------------------------------------------------------
4303set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
4304set(handles.REMOVE2,'Visible','off')
4305set(handles.vec_Fmin2_2,'Visible','off')
4306set(handles.vec_F4,'Visible','off')
4307set(handles.vec_F3_2,'Visible','off')
4308set(handles.thresh_vec2C,'Visible','off')
4309set(handles.thresh_vec2C_text,'Visible','off')
4310set(handles.thresh_vel2,'Visible','off')
4311set(handles.thresh_vel2_text,'Visible','off')
4312set(handles.mask_fix2,'Visible','off')
4313set(handles.get_mask_fix2,'Visible','off')
4314set(handles.get_ref_fix2,'Visible','off')
4315set(handles.ref_fix2,'Visible','off')
4316set(handles.inf_sup2,'Visible','off')
4317set(handles.field_ref2,'Visible','off')
4318if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0)
4319    set(handles.list_pair_civ2,'Visible','off')
4320    set(handles.subdir_civ2,'Visible','off')
4321    set(handles.subdir_civ2_text,'Visible','off')
4322end
4323
4324%------------------------------------------------------------------------
4325function enable_patch2(handles)
4326%------------------------------------------------------------------------
4327set(handles.frame_patch2,'BackgroundColor',[1 1 0])
4328set(handles.rho_patch2,'Visible','on')
4329set(handles.rho_text2,'Visible','on')
4330set(handles.thresh_patch2,'Visible','on')
4331set(handles.thresh_text2,'Visible','on')
4332set(handles.subdomain_patch2,'Visible','on')
4333set(handles.subdomain_text2,'Visible','on')
4334set(handles.nx_patch2,'Visible','on')
4335set(handles.ny_patch2,'Visible','on')
4336set(handles.nx_patch2_title,'Visible','on')
4337set(handles.ny_patch2_title,'Visible','on')
4338set(handles.get_gridpatch2,'Visible','on')
4339set(handles.grid_patch2,'Visible','on')
4340set(handles.list_pair_civ2,'Visible','on')
4341set(handles.subdir_civ2,'Visible','on')
4342set(handles.subdir_civ2_text,'Visible','on')
4343
4344%------------------------------------------------------------------------
4345function desable_patch2(handles)
4346%------------------------------------------------------------------------
4347set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
4348set(handles.rho_patch2,'Visible','off')
4349set(handles.rho_text2,'Visible','off')
4350set(handles.thresh_patch2,'Visible','off')
4351set(handles.thresh_text2,'Visible','off')
4352set(handles.subdomain_patch2,'Visible','off')
4353set(handles.subdomain_text2,'Visible','off')
4354set(handles.nx_patch2,'Visible','off')
4355set(handles.ny_patch2,'Visible','off')
4356set(handles.nx_patch2_title,'Visible','off')
4357set(handles.ny_patch2_title,'Visible','off')
4358set(handles.get_gridpatch2,'Visible','off')
4359set(handles.grid_patch2,'Visible','off')
4360if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.FIX2,'Value'),0)
4361    set(handles.list_pair_civ2,'Visible','off')
4362    set(handles.subdir_civ2,'Visible','off')
4363    set(handles.subdir_civ2_text,'Visible','off')
4364end
4365
4366%------------------------------------------------------------------------
4367function enable_pair1(handles,state)
4368%------------------------------------------------------------------------
4369set(handles.subdir_civ1,'Visible',state)
4370set(handles.list_subdir_civ1,'Visible',state)
4371set(handles.SUBDIR_CIV1_txt,'Visible',state)
4372set(handles.frame_subdirciv1,'Visible',state)
4373set(handles.list_pair_civ1,'Visible',state)
4374set(handles.PAIR_txt,'Visible',state)
4375%set(handles.dt_unit,'Visible',state)
4376set(handles.PAIR_frame,'Visible',state)
4377
4378%------------------------------------------------------------------------
4379% --- Read the parameters for civ1 on the interface
4380function par=read_param_civ1(handles,file_ima)
4381%------------------------------------------------------------------------
4382ibx_val=str2double(get(handles.ibx,'String'));
4383par.ibx=num2str(ibx_val);
4384iby_val=str2double(get(handles.iby,'String'));
4385par.iby=num2str(iby_val);
4386isx=get(handles.isx,'String');
4387if isnan(str2double(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
4388if str2double(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
4389isy=get(handles.isy,'String');
4390if isnan(str2double(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
4391if str2double(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
4392par.isx=get(handles.isx,'String');
4393par.isy=get(handles.isy,'String');
4394par.shiftx=get(handles.shiftx,'String');
4395par.shifty=get(handles.shifty,'String');
4396if isnan(str2double(par.isx))
4397    par.isx='41';%default
4398    set(handles.isx,'String','41');
4399end
4400if isnan(str2double(par.isy))
4401    par.isy='41'; %default
4402    set(handles.isy,'String','41');
4403end
4404if isnan(str2double(par.shiftx))
4405    par.shiftx='0';%default
4406    set(handles.shiftx,'String','0');
4407end
4408if isnan(str2double(par.shifty))
4409    par.shifty='0'; %default
4410    set(handles.shifty,'String','0');
4411end
4412par.rho=get(handles.rho,'String');
4413par.dx=get(handles.dx_civ1,'String');
4414par.dy=get(handles.dy_civ1,'String');
4415if isnan(str2double(par.dx))
4416    if isempty(get(handles.grid_civ1,'String'));
4417        par.dx='0'; %just read by civ program, not used
4418    else
4419        par.dx='20';%default
4420        set(handles.dx_civ1,'String','20');
4421    end
4422end
4423if isnan(str2double(par.dy))
4424    if isempty(get(handles.grid_civ1,'String'));
4425        par.dy='0';%just read by civ program, not used
4426    else
4427        par.dy='20';%default
4428        set(handles.dy_civ1_title,'String','20');
4429    end
4430end
4431par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4432par.pxcmy='1';
4433%      end
4434A=imread(file_ima);%read the first image to get the size
4435sizim=size(A);
4436par.npx=num2str(sizim(2));
4437par.npy=num2str(sizim(1));
4438%time=get(handles.RootName,'UserData'); %get the set of times
4439par.gridname=get(handles.grid_civ1,'String');
4440par.gridflag='y';
4441if strcmp(par.gridname,'')|| isempty(par.gridname)
4442    par.gridname='nogrid';
4443    par.gridflag='n';
4444end
4445
4446%------------------------------------------------------------------------
4447function par=read_param_civ2(handles,file_ima)
4448%------------------------------------------------------------------------
4449par.ibx=get(handles.ibx_civ2,'String');
4450par.iby=get(handles.iby_civ2,'String');
4451par.rho=get(handles.rho_civ2,'String');
4452par.decimal=int2str(get(handles.decimal,'Value'));
4453par.deformation=int2str(get(handles.deformation,'Value'));
4454par.dx=get(handles.dx_civ2,'String');
4455par.dy=get(handles.dy_civ2,'String');
4456if isnan(str2double(par.dx))
4457    if isempty(get(handles.grid_civ2,'String'));
4458        par.dx='0'; %just read by civ program, not used
4459    else
4460        par.dx='20';%default
4461        set(handles.dx_civ2,'String','20');
4462    end
4463end
4464if isnan(str2double(par.dy))
4465    if isempty(get(handles.grid_civ2,'String'));
4466        par.dy='0';%just read by civ program, not used
4467    else
4468        par.dy='20';%default
4469        set(handles.dy_civ2,'String','20');
4470    end
4471end
4472par.pxcmx='1';
4473par.pxcmy='1';
4474A=imread(file_ima);%read the first image to get the size
4475sizim=size(A);
4476par.npx=num2str(sizim(2));
4477par.npy=num2str(sizim(1));
4478%time=get(handles.RootName,'UserData'); %get the set of times
4479par.gridname=get(handles.grid_civ2,'String');
4480par.gridflag='y';
4481if strcmp(par.gridname,'')|| isempty(par.gridname)
4482    par.gridname='nogrid';
4483    par.gridflag='n';
4484end
4485
4486%------------------------------------------------------------------------
4487% --- CIV1  CIV1  CIV1 CIV1
4488function cmd_CIV1=CIV1_CMD(filename,namelog,par,handles,sparam)
4489%------------------------------------------------------------------------
4490%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4491
4492%changes : filename_cmx -> filename ( no extension )
4493
4494if isequal(par.Dt,'0')
4495    par.Dt='1' ;%case of 'displacement' mode
4496end
4497%
4498%     textcmx={'##############   CMX file';...
4499%     ['FirstImage ' par.filename_ima_a];...
4500%     ['LastImage  ' par.filename_ima_b];...
4501%     'XX' ;...
4502%     ['Mask ' par.maskflag] ;...
4503%     ['MaskName ' par.maskname];...
4504%     ['ImageSize ' par.npx ' ' par.npy];...   %VERIFIER CAS GENERAL ?
4505%     ['CorrelationBoxesSize ' par.ibx ' ' par.iby];...
4506%     ['SearchBoxeSize ' par.isx ' ' par.isy];...
4507%     ['RO ' par.rho];...
4508%     ['GridSpacing ' par.dx ' ' par.dy];...
4509%     'XX 1.0';...
4510%     ['Dt_TO ' par.Dt ' ' par.T0];...
4511%     ['PixCmXY ' par.pxcmx ' ' par.pxcmy];...
4512%     'XX 1';...
4513%     ['ShiftXY ' par.shiftx ' '  par.shifty];...
4514%     ['Grid ' par.gridflag];...
4515%     ['GridName ' par.gridname] ;...
4516%     'XX 85';...
4517%     'XX 1.0';...
4518%     'XX 1.0';...
4519%     'Hart 1';...
4520%     'DecimalShift 0';...
4521%     'Deformation 0';...
4522%     'CorrelationMin 0';...
4523%     'IntensityMin 0';...
4524%     'SeuilImage n';...
4525%     'SeuilImageValues 0 4096';...
4526%     ['ImageToUse ' par.term_a ' ' par.term_b];... % VERIFIER ?
4527%     'ImageUsedBefore null null'};
4528%
4529%             textout=char(textcmx);
4530par.filename_ima_a=regexprep(par.filename_ima_a,'.png','');
4531par.filename_ima_b=regexprep(par.filename_ima_b,'.png','');
4532fid=fopen([filename '.cmx'],'w');
4533fprintf(fid,['##############   CMX file' '\n' ]);
4534fprintf(fid,   ['FirstImage ' regexprep(par.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4535fprintf(fid,   ['LastImage  ' regexprep(par.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4536fprintf(fid,  ['XX' '\n' ]);
4537fprintf(fid,  ['Mask ' par.maskflag '\n' ]);
4538fprintf(fid,  ['MaskName ' regexprep(par.maskname,'\\','\\\\') '\n' ]);
4539fprintf(fid,   ['ImageSize ' par.npx ' ' par.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4540fprintf(fid,   ['CorrelationBoxesSize ' par.ibx ' ' par.iby '\n' ]);
4541fprintf(fid,   ['SearchBoxeSize ' par.isx ' ' par.isy '\n' ]);
4542fprintf(fid,   ['RO ' par.rho '\n' ]);
4543fprintf(fid,   ['GridSpacing ' par.dx ' ' par.dy '\n' ]);
4544fprintf(fid,   ['XX 1.0' '\n' ]);
4545fprintf(fid,   ['Dt_TO ' par.Dt ' ' par.T0 '\n' ]);
4546fprintf(fid,  ['PixCmXY ' par.pxcmx ' ' par.pxcmy '\n' ]);
4547fprintf(fid,  ['XX 1' '\n' ]);
4548fprintf(fid,   ['ShiftXY ' par.shiftx ' '  par.shifty '\n' ]);
4549fprintf(fid,  ['Grid ' par.gridflag '\n' ]);
4550fprintf(fid,   ['GridName ' regexprep(par.gridname,'\\','\\\\') '\n' ]);
4551fprintf(fid,   ['XX 85' '\n' ]);
4552fprintf(fid,   ['XX 1.0' '\n' ]);
4553fprintf(fid,   ['XX 1.0' '\n' ]);
4554fprintf(fid,   ['Hart 1' '\n' ]);
4555fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4556fprintf(fid,   ['Deformation 0' '\n' ]);
4557fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4558fprintf(fid,   ['IntensityMin 0' '\n' ]);
4559fprintf(fid,  ['SeuilImage n' '\n' ]);
4560fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4561fprintf(fid,   ['ImageToUse ' par.term_a ' ' par.term_b '\n' ]); % VERIFIER ?
4562fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4563fclose(fid);
4564
4565cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
4566cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4567namelog=regexprep(namelog,'\\','\\\\');
4568
4569if(isunix)
4570    [Rootbat,Filebat,extbat]=fileparts(namelog);
4571    ncName=fullfile(Rootbat,[ Filebat '.nc']);
4572    cmd_CIV1=[cmd_CIV1 '\n' 'mv ' namelog  ' ' regexprep(namelog,'\.log','') '.civ1.log' '\n' 'chmod g+w ' ncName];
4573else
4574    cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y ' namelog ' ' regexprep(namelog,'\.log','') '.civ1.log'];
4575end
4576
4577%------------------------------------------------------------------------
4578% --- CIV1  Unified
4579function xml_civ1_parameters=CIV1_CMD_Unified(filename,namelog,par)
4580%------------------------------------------------------------------------
4581%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4582%global CivBin%name of the executable for civ1 calculation
4583
4584civ1.image1=par.filename_ima_a;
4585civ1.image2=par.filename_ima_b;
4586civ1.imageSize_X=par.npx;
4587civ1.imageSize_Y=par.npy;
4588civ1.outputFileName=[filename '.nc'];
4589civ1.correlationBoxesSize_X=par.ibx;
4590civ1.correlationBoxesSize_Y=par.iby;
4591civ1.searchBoxesSize_X=par.isx;
4592civ1.searchBoxesSize_Y=par.isy;
4593civ1.globalShift_X=par.shiftx;
4594civ1.globalShift_Y=par.shifty;
4595civ1.ro=par.rho;
4596civ1.hart='y';
4597if isequal(par.gridflag,'y')
4598    civ1.grid=par.gridname;
4599else
4600    civ1.grid='n';
4601    civ1.gridSpacing_X=par.dx;
4602    civ1.gridSpacing_Y=par.dy;
4603end
4604if isequal(par.maskflag,'y')
4605    civ1.mask=par.maskname;
4606end
4607civ1.dt=par.Dt;
4608civ1.unit='pixel';
4609civ1.absolut_time_T0=par.T0;
4610civ1.pixcmx=par.pxcmx;
4611civ1.pixcmy=par.pxcmy;
4612civ1.convectFlow='n';
4613
4614xml_civ1_parameters=civ1;
4615
4616%------------------------------------------------------------------------
4617% --- CIV2  Unified
4618function civ2=CIV2_CMD_Unified(filename,namelog,par)
4619%------------------------------------------------------------------------
4620%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4621%global CivBin%name of the executable for civ1 calculation
4622
4623civ2.image1=par.filename_ima_a;
4624civ2.image2=par.filename_ima_b;
4625civ2.imageSize_X=par.npx;
4626civ2.imageSize_Y=par.npy;
4627civ2.inputFileName=[par.filename_nc1 '.nc'];
4628civ2.outputFileName=[filename '.nc'];
4629civ2.correlationBoxesSize_X=par.ibx;
4630civ2.correlationBoxesSize_Y=par.iby;
4631civ2.ro=par.rho;
4632%civ2.decimalShift=par.decimal;
4633%civ2.deformation=par.deformation;
4634if isequal(par.decimal,'1')
4635    civ2.decimalShift='y';
4636else
4637    civ2.decimalShift='n';
4638end
4639if isequal(par.deformation,'1')
4640    civ2.deformation='y';
4641else
4642    civ2.deformation='n';
4643end
4644if isequal(par.gridflag,'y')
4645    civ2.grid=par.gridname;
4646else
4647    civ2.grid='n';
4648    civ2.gridSpacing_X=par.dx;
4649    civ2.gridSpacing_Y=par.dy;
4650end
4651civ2.gridSpacing_X='10';
4652civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4653if isequal(par.maskflag,'y')
4654    civ2.mask=par.maskname;
4655else
4656    civ2.mask='n';
4657end
4658civ2.dt=par.Dt;
4659civ2.unit='pixel';
4660civ2.absolut_time_T0=par.T0;
4661civ2.pixcmx=par.pxcmx;
4662civ2.pixcmy=par.pxcmy;
4663civ2.convectFlow='n';
4664civ2.pixcmx=par.pxcmx;
4665civ2.pixcmy=par.pxcmy;
4666civ2.convectFlow='n';
4667
4668%------------------------------------------------------------------------
4669% --- CIV2  CIV2  CIV2 CIV2
4670function cmd_CIV2=CIV2_CMD(filename_cmx,namelog,par,sparam)
4671%------------------------------------------------------------------------
4672%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4673% global civ2Bin sge%name of the executable for civ1 calculation
4674if isequal(par.Dt,'0')
4675    par.Dt='1' ;%case of 'displacement' mode
4676end
4677% textcmx=['##############   CMX file'  '\n'...
4678% ['FirstImage ' par.filename_ima_a]  '\n'...
4679% ['LastImage  ' par.filename_ima_b]  '\n'...
4680% 'XX'   '\n'...
4681% ['Mask ' par.maskflag]  '\n'...
4682% ['MaskName ' par.maskname]  '\n'...
4683% ['ImageSize ' par.npx ' ' par.npy]  '\n'...
4684% ['CorrelationBoxesSize ' par.ibx ' ' par.iby]  '\n'...
4685% ['SearchBoxeSize ' par.ibx ' ' par.iby]  '\n'...
4686% ['RO ' par.rho]  '\n'...
4687% ['GridSpacing ' par.dx ' ' par.dy]  '\n'...
4688% 'XX 1.0'  '\n'...
4689% ['Dt_TO ' par.Dt ' ' par.T0]  '\n'...
4690% ['PixCmXY ' par.pxcmx ' ' par.pxcmy]  '\n'...
4691% 'XX 1'  '\n'...
4692% ['ShiftXY 0 0']  '\n'...
4693% ['Grid ' par.gridflag]  '\n'...
4694% ['GridName ' par.gridname]  '\n'...
4695% 'XX 85'  '\n'...
4696% 'XX 1.0'  '\n'...
4697% 'XX 1.0'  '\n'...
4698% 'Hart 1'  '\n'...
4699% ['DecimalShift ' par.decimal]  '\n'...
4700% ['Deformation ' par.deformation]  '\n'...
4701% 'CorrelationMin 0'  '\n'...
4702% 'IntensityMin 0'  '\n'...
4703% 'SeuilImage n'  '\n'...
4704% 'SeuilImageValues 0 4096'  '\n'...
4705% ['ImageToUse ' par.term_a ' ' par.term_b]  '\n'... % VERIFIER ?
4706% ['ImageUsedBefore ' par.filename_nc1]];
4707% textout=char(textcmx);
4708% fid=fopen([filename_cmx '2'],'w');
4709% fprintf(fid,textout);
4710% fclose(fid)
4711
4712par.filename_ima_a=regexprep(par.filename_ima_a,'.png','');
4713par.filename_ima_b=regexprep(par.filename_ima_b,'.png','');% bug : .png appears two times ?
4714fid=fopen([filename_cmx '2'],'w');
4715fprintf(fid,['##############   CMX file' '\n' ]);
4716fprintf(fid,   ['FirstImage ' regexprep(par.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4717fprintf(fid,   ['LastImage  ' regexprep(par.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4718fprintf(fid,  ['XX' '\n' ]);
4719fprintf(fid, ['Mask ' par.maskflag '\n' ]);
4720fprintf(fid, ['MaskName ' regexprep(par.maskname,'\\','\\\\') '\n' ]);% for windows compatibility
4721fprintf(fid, ['ImageSize ' par.npx ' ' par.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4722fprintf(fid, ['CorrelationBoxesSize ' par.ibx ' ' par.iby '\n' ]);
4723fprintf(fid, ['SearchBoxeSize ' par.ibx ' ' par.iby '\n']);
4724fprintf(fid, ['RO ' par.rho '\n']);
4725fprintf(fid, ['GridSpacing ' par.dx ' ' par.dy '\n']);
4726fprintf(fid, ['XX 1.0' '\n' ]);
4727fprintf(fid, ['Dt_TO ' par.Dt ' ' par.T0 '\n' ]);
4728fprintf(fid, ['PixCmXY ' par.pxcmx ' ' par.pxcmy '\n' ]);
4729fprintf(fid, ['XX 1' '\n' ]);
4730fprintf(fid, 'ShiftXY 0 0\n');
4731fprintf(fid, ['Grid ' par.gridflag '\n' ]);
4732fprintf(fid, ['GridName ' regexprep(par.gridname,'\\','\\\\') '\n']);
4733fprintf(fid, ['XX 85' '\n' ]);
4734fprintf(fid, ['XX 1.0' '\n' ]);
4735fprintf(fid, ['XX 1.0' '\n' ]);
4736fprintf(fid, ['Hart 1' '\n' ]);
4737fprintf(fid, ['DecimalShift ' par.decimal '\n']);
4738fprintf(fid, ['Deformation ' par.deformation '\n']);
4739fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4740fprintf(fid,   ['IntensityMin 0' '\n' ]);
4741fprintf(fid,  ['SeuilImage n' '\n' ]);
4742fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4743fprintf(fid,   ['ImageToUse ' par.term_a ' ' par.term_b '\n' ]); % VERIFIER ?
4744fprintf(fid, ['ImageUsedBefore ' regexprep(par.filename_nc1,'\\','\\\\') '\n']);
4745fclose(fid);
4746
4747cmd_CIV2=[sparam.Civ2Bin ' -f ' filename_cmx ]; % redirect standard output to the log file
4748cmd_CIV2=regexprep(cmd_CIV2,'\\','\\\\');
4749
4750%------------------------------------------------------------------------
4751% --- Executes on button press in HELP.
4752function HELP_Callback(hObject, eventdata, handles)
4753%------------------------------------------------------------------------
4754path_to_uvmat=which ('uvmat');% check the path of uvmat
4755pathelp=fileparts(path_to_uvmat);
4756helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
4757if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
4758else
4759    addpath (fullfile(pathelp,'uvmat_doc'))
4760    web([helpfile '#civ'])
4761end
4762
4763%------------------------------------------------------------------------
4764%--read images and convert them to the uint16 format used for PIV
4765function A=read_image(filename,type_ima,num,movieobject)
4766%------------------------------------------------------------------------
4767%num is the view number needed for an avi movie
4768switch type_ima
4769    case 'movie'
4770        A=read(movieobject,num);
4771    case 'avi'
4772        mov=aviread(filename,num);
4773        A=frame2im(mov(1));
4774    case 'multimage'
4775        A=imread(filename,num);
4776    case 'image'
4777        A=imread(filename);
4778end
4779siz=size(A);
4780if length(siz)==3;%color images
4781    A=sum(double(A),3);
4782    A=uint16(A);
4783end
4784
4785%------------------------------------------------------------------------
4786function ref_i_Callback(hObject, eventdata, handles)
4787%------------------------------------------------------------------------
4788mode_list=get(handles.mode,'String');
4789mode_value=get(handles.mode,'Value');
4790mode=mode_list{mode_value};
4791find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4792if isequal(mode,'series(Di)') || ...% we do patch2 only
4793        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4794    find_netcpair_civ2(hObject, eventdata, handles);
4795end
4796
4797%------------------------------------------------------------------------
4798function ref_j_Callback(hObject, eventdata, handles)
4799%------------------------------------------------------------------------
4800mode_list=get(handles.mode,'String');
4801mode_value=get(handles.mode,'Value');
4802mode=mode_list{mode_value};
4803if isequal(get(handles.CIV1,'Value'),0)|| isequal(mode,'series(Dj)')
4804    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4805end
4806if isequal(mode,'series(Dj)') || ...
4807        (get(handles.CIV2,'Value')==0 && get(handles.CIV1,'Value')==0 && get(handles.FIX1,'Value')==0 && get(handles.PATCH1,'Value')==0)
4808    find_netcpair_civ2(hObject, eventdata, handles);
4809end
4810
4811%------------------------------------------------------------------------
4812function ref_i_civ2_Callback(hObject, eventdata, handles)
4813%------------------------------------------------------------------------
4814mode_list=get(handles.mode,'String');
4815mode_value=get(handles.mode,'Value');
4816mode=mode_list{mode_value};
4817find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4818
4819%------------------------------------------------------------------------
4820function ref_j_civ2_Callback(hObject, eventdata, handles)
4821%------------------------------------------------------------------------
4822mode_list=get(handles.mode,'String');
4823mode_value=get(handles.mode,'Value');
4824mode=mode_list{mode_value};
4825if  isequal(mode,'series(Dj)')
4826    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4827end
4828
4829%------------------------------------------------------------------------
4830% --- Executes on button press in compare.
4831function compare_Callback(hObject, eventdata, handles)
4832%------------------------------------------------------------------------
4833test=get(handles.compare,'Value');
4834if test==2 || test==3
4835    filebase=get(handles.RootName,'String');
4836    browse=get(handles.browse_root,'Userdata');
4837    browse.nom_type_ima1=browse.nom_type_ima;
4838    set(handles.browse_root,'UserData',browse);
4839    set(handles.sub_txt,'Visible','on')
4840    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
4841    mode_store=get(handles.mode,'String');%get the present 'mode'
4842    set(handles.compare,'UserData',mode_store);%store the mode display
4843    set(handles.mode,'Visible','off')
4844    if test==2
4845        set(handles.mode,'Visible','off')
4846    else
4847        set(handles.mode,'Visible','on')
4848    end
4849   
4850    % open an image file with the browser
4851    ind_opening=1;%default
4852    browse.incr_pair=[0 0]; %default
4853    oldfile=get(handles.RootName,'String');
4854    menu={'*.xml;*.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
4855        '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc', '.nc files';...
4856        '*.*', 'All Files (*.*)'};
4857    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
4858    fileinput=[PathName FileName];%complete file name
4859    sizf=size(fileinput);
4860    if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4861    [path,name,ext]=fileparts(fileinput);
4862    [path1]=fileparts(filebase);
4863    if ~strcmp(path1,path)
4864        msgbox_uvmat('ERROR','The two  input image series must be in the same directory')
4865        return
4866    end
4867    set(handles.RootName_1,'String',name);
4868    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
4869    browse=get(handles.browse_root,'UserData');
4870    browse.nom_type_ima_1=nom_type;
4871    set(handles.browse_root,'UserData',browse)
4872   
4873    %check image extension
4874    if ~strcmp(ext,get(handles.ImaExt,'String'))
4875        msgbox_uvmat('ERROR','The two  input image series must have the same extenion name')
4876        return
4877    end
4878   
4879    %check image size
4880    A=imread(fileinput);
4881    npxy=get(handles.ImaExt,'UserData');
4882    if ~isequal(npxy(1),size(A,1))|| ~isequal(npxy(2),size(A,2))
4883        msgbox_uvmat('ERROR','The two input image series must have the same size')
4884        return
4885    end
4886else
4887    set(handles.mode,'Visible','on')
4888    set(handles.RootName_1,'Visible','Off');
4889    set(handles.sub_txt,'Visible','off')
4890    set(handles.RootName_1,'String',[]);
4891    mode_store=get(handles.compare,'UserData');
4892    set(handles.mode,'String',mode_store)
4893    set(handles.test_stereo1,'Value',0)
4894    set(handles.test_stereo2,'Value',0)
4895end
4896mode_Callback(hObject, eventdata, handles)
4897
4898%------------------------------------------------------------------------
4899% --- Executes on button press in get_ref_fix1.
4900function get_ref_fix1_Callback(hObject, eventdata, handles)
4901%------------------------------------------------------------------------
4902filebase=get(handles.RootName,'String');
4903[FileName, PathName, filterindex] = uigetfile( ...
4904    {'*.nc', ' (*.nc)';
4905    '*.nc',  'netcdf files '; ...
4906    '*.*', 'All Files (*.*)'}, ...
4907    'Pick a file',filebase);
4908
4909fileinput=[PathName FileName];
4910sizf=size(fileinput);
4911if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4912[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4913ref.filebase=fullfile(Path,File);
4914ref.num_a=stra2num(str_a);
4915ref.num_b=stra2num(str_b);
4916ref.num1=str2double(field_count);
4917ref.num2=str2double(str2);
4918browse=[];%initialisation
4919if ~isequal(ref.ext,'.nc')
4920    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4921    return
4922end
4923set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
4924set(handles.ref_fix1,'UserData',ref)
4925menu_field{1}='civ1';
4926Data=nc2struct(fileinput,[]);
4927if isfield(Data,'patch') && isequal(Data.patch,1)
4928    menu_field{2}='filter1';
4929end
4930if isfield(Data,'civ2') && isequal(Data.civ2,1)
4931    menu_field{3}='civ2';
4932end
4933if isfield(Data,'patch2') && isequal(Data.patch2,1)
4934    menu_field{4}='filter2';
4935end
4936set(handles.field_ref1,'String',menu_field);
4937set(handles.field_ref1,'Value',length(menu_field));
4938set(handles.inf_sup1,'Value',2);
4939set(handles.thresh_vel,'String','1');%default threshold
4940set(handles.ref_fix1,'Enable','on')
4941
4942%------------------------------------------------------------------------
4943% --- Executes on button press in get_ref_fix2.
4944function get_ref_fix2_Callback(hObject, eventdata, handles)
4945%------------------------------------------------------------------------
4946if isequal(get(handles.get_ref_fix2,'Value'),1)
4947    filebase=get(handles.RootName,'String');
4948    [FileName, PathName, filterindex] = uigetfile( ...
4949        {'*.nc', ' (*.nc)';
4950        '*.nc',  'netcdf files '; ...
4951        '*.*', 'All Files (*.*)'}, ...
4952        'Pick a file',filebase);
4953    fileinput=[PathName FileName];
4954    sizf=size(fileinput);
4955    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4956    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4957    ref.filebase=fullfile(Path,File);
4958    ref.num_a=stra2num(str_a);
4959    ref.num_b=stra2num(str_b);
4960    ref.num1=str2num(field_count);
4961    ref.num2=str2num(str2);
4962    browse=[];%initialisation
4963    if ~isequal(ref.ext,'.nc')
4964        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4965        return
4966    end
4967    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
4968    set(handles.ref_fix2,'UserData',ref)
4969    menu_field{1}='civ1';
4970    Data=nc2struct(fileinput,[]);
4971    if isfield(Data,'patch') & isequal(Data.patch,1)
4972        menu_field{2}='filter1';
4973    end
4974    if isfield(Data,'civ2') & isequal(Data.civ2,1)
4975        menu_field{3}='civ2';
4976    end
4977    if isfield(Data,'patch2') & isequal(Data.patch2,1)
4978        menu_field{4}='filter2';
4979    end
4980    set(handles.field_ref2,'String',menu_field);
4981    set(handles.field_ref2,'Value',length(menu_field));
4982    set(handles.inf_sup2,'Value',2);
4983    set(handles.thresh_vel2,'String','1');%default threshold
4984    set(handles.ref_fix2,'Enable','on')
4985    set(handles.ref_fix2,'Visible','on')
4986    set(handles.field_ref2,'Visible','on')
4987else
4988    set(handles.ref_fix2,'Visible','off')
4989    set(handles.field_ref2,'Visible','off')
4990end
4991
4992%------------------------------------------------------------------------
4993function ref_fix1_Callback(hObject, eventdata, handles)
4994%------------------------------------------------------------------------
4995set(handles.inf_sup1,'Value',1);
4996set(handles.field_ref1,'Value',1)
4997set(handles.field_ref1,'String',{' '})
4998set(handles.ref_fix1,'UserData',[]);
4999set(handles.ref_fix1,'String','');
5000set(handles.thresh_vel1,'String','0');
5001
5002%------------------------------------------------------------------------
5003function ref_fix2_Callback(hObject, eventdata, handles)
5004%------------------------------------------------------------------------
5005set(handles.inf_sup2,'Value',1);
5006set(handles.field_ref2,'Value',1)
5007set(handles.field_ref2,'String',{' '})
5008set(handles.ref_fix2,'UserData',[]);
5009set(handles.ref_fix2,'String','');
5010set(handles.thresh_vel2,'String','0');
5011
5012% %------------------------------------------------------------------------
5013% % transform letters to numbers
5014% function numres=stra2num(str)
5015% %------------------------------------------------------------------------
5016% numres=double(str)-96;
5017% if double(str) >= 48 & double(str) <= 57 % = 1 for numbers
5018%     numres=str2num(str);
5019% end
5020
5021%------------------------------------------------------------------------
5022% --- Executes on button press in test_stereo1.
5023function test_stereo1_Callback(hObject, eventdata, handles)
5024%------------------------------------------------------------------------
5025if isequal(get(handles.test_stereo1,'Value'),0)
5026    set(handles.subdomain_patch1,'Visible','on')
5027    set(handles.rho_patch1,'Visible','on')
5028else
5029    set(handles.subdomain_patch1,'Visible','off')
5030    set(handles.rho_patch1,'Visible','off')
5031end
5032
5033%------------------------------------------------------------------------
5034% --- Executes on button press in test_stereo2.
5035function test_stereo2_Callback(hObject, eventdata, handles)
5036%------------------------------------------------------------------------
5037if isequal(get(handles.test_stereo2,'Value'),0)
5038    set(handles.subdomain_patch2,'Visible','on')
5039    set(handles.rho_patch2,'Visible','on')
5040else
5041    set(handles.subdomain_patch2,'Visible','off')
5042    set(handles.rho_patch2,'Visible','off')
5043end
5044
5045%------------------------------------------------------------------------
5046% --- Executes on button press in ImaThreshold.
5047function ImaThreshold_Callback(hObject, eventdata, handles)
5048%------------------------------------------------------------------------
5049if isequal(get(handles.ImaThreshold,'Value'),1)
5050    set(handles.MinIma,'Visible','on')
5051    set(handles.MaxIma,'Visible','on')
5052else
5053    set(handles.MinIma,'Visible','off')
5054    set(handles.MaxIma,'Visible','off')
5055end
5056
5057%------------------------------------------------------------------------
5058% --- Executes on button press in ImaThreshold2.
5059function ImaThreshold2_Callback(hObject, eventdata, handles)
5060%------------------------------------------------------------------------
5061if isequal(get(handles.ImaThreshold2,'Value'),1)
5062    set(handles.MinIma2,'Visible','on')
5063    set(handles.MaxIma2,'Visible','on')
5064else
5065    set(handles.MinIma2,'Visible','off')
5066    set(handles.MaxIma2,'Visible','off')
5067end
5068
5069
5070
5071
Note: See TracBrowser for help on using the repository browser.