source: trunk/src/civ.m @ 133

Last change on this file since 133 was 133, checked in by gostiaux, 13 years ago

Code cleaning

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