source: trunk/src/civ.m @ 113

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

Max number of jobs in run corrected

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