source: trunk/src/civ.m @ 119

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

civ.m: rho_patch2 reactivated after bug corrections on the binary patch

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