source: trunk/src/civ.m @ 286

Last change on this file since 286 was 286, checked in by gostiaux, 12 years ago

Patch1 OK

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