source: trunk/src/civ.m @ 127

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

merge_proj.m: cleaning
sub_field.m: ?
dataview.m: modifs not finished
name-generator: bug repair for new nom_type
nc2struct: cleaning
uvmat: cleaning
read_civxdata: transform all variables to double
civ: bug repair for new nom_type, not finished

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