source: trunk/src/civ.m @ 290

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

civ2 added with new name conventions

File size: 220.2 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 19-Nov-2011 19:26:20
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.txt_SubdirCiv1,'String',subdir);%set the default subdir directories for installing the .nc results
366    set(handles.txt_SubdirCiv2,'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.txt_SubdirCiv1,'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.txt_SubdirCiv1,'String',dirname)
842set(handles.txt_SubdirCiv2,'String',dirname)
843%check wether the current subdir exists:
844% txt_SubdirCiv1=get(handles.txt_SubdirCiv1,'String');
845% txt_SubdirCiv2=get(handles.txt_SubdirCiv2,'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(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.txt_SubdirCiv1,'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(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.txt_SubdirCiv1,'String');%subdirectory subdir_civ1 for the netcdf data
1234subdir_civ2=get(handles.txt_SubdirCiv2,'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.txt_MaskName,'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='oarstat';
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            Param.Civ2.pxcmx=1; %velocities are expressed in pixel dispalcement
1853            Param.Civ2.pxcmy=1;
1854            if Param.Civ2.CheckMask
1855                maskdispl=get(handles.txt_Mask,'String');
1856                if exist(maskdispl,'file')
1857                    Param.Civ2.MaskName=maskdispl;
1858                    Param.Civ2.MaskFlag='y';
1859                else
1860                    maskbase=[filecell.filebase '_' maskdispl]; %
1861                    nbslice_mask=str2double(maskdispl(1:end-4)); %
1862                    num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
1863                    Param.Civ2.MaskName=name_generator(maskbase,num1_mask,1,'.png','_i');
1864                    if exist(Param.Civ2.MaskName,'file')
1865                        Param.Civ2.MaskFlag='y';
1866                    else
1867                        Param.Civ2.MaskName='noFile use default';
1868                        Param.Civ2.MaskFlag='n';
1869                    end
1870                end
1871            else
1872                Param.Civ2.MaskName='noFile use default';
1873                Param.Civ2.MaskFlag='n';
1874            end
1875            gridname=get(handles.txt_GridName,'String');
1876            if numel(gridname)>=4 && isequal(gridname(end-3:end),'grid')
1877                nbslice_grid=str2double(gridname(1:end-4)); %
1878                if ~isnan(nbslice_grid)
1879                    Param.Civ2.GridFlag='y';
1880                    num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
1881                    Param.Civ2.GridName=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1882                    if exist(Param.Civ2.GridName,'file')
1883                        Param.Civ2.GridFlag='y';
1884                    else
1885                        Param.Civ2.GridName='noFile use default';
1886                        Param.Civ2.GridFlag='n';
1887                    end
1888                elseif exist(gridname,'file')
1889                    Param.Civ2.GridFlag='y';
1890                else
1891                    Param.Civ2.GridName='noFile use default';
1892                    Param.Civ2.GridFlag='n';
1893                end
1894            else
1895                Param.Civ2.GridName='noFile use default';
1896                Param.Civ2.GridFlag='n';
1897            end
1898            A=imread(filecell.ima1.civ2{1,1});%read the first image to get the size
1899            sizim=size(A);
1900            Param.Civ2.npx=(sizim(2));
1901            Param.Civ2.npy=(sizim(1));
1902                 
1903            switch CivMode
1904                case 'CivX'
1905                     cmd=[cmd...
1906                        cmd_civ2(filecell.nc.civ2{ifile,j},Param) '\n'];
1907                case 'CivAll'
1908                    CivAllCmd=[CivAllCmd ' civ2 '];
1909                    str=CIV2_CMD_Unified(filecell.nc.civ2{ifile,j},'',Param.Civ2);
1910                    fieldnames=fields(str);
1911                    [CivAllxml,uid_civ2]=add(CivAllxml,1,'element','civ2');
1912                    for ilist=1:length(fieldnames)
1913                        val=eval(['str.' fieldnames{ilist}]);
1914                        if ischar(val)
1915                            [CivAllxml,uid_t]=add(CivAllxml,uid_civ2,'element',fieldnames{ilist});
1916                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1917                        end
1918                    end
1919            end
1920        end
1921       
1922        % CheckFix2
1923        if Param.CheckFix2==1
1924            test_mask=get(handles.CheckMask,'Value');
1925            if test_mask==0
1926                maskname=''; %no mask used
1927            else
1928                maskdispl=get(handles.txt_MaskName,'String');
1929                maskbase=[filecell.filebase '_' maskdispl]; %
1930                nbslice_mask=str2double(maskdispl(1:end-4)); %
1931                num1_mask=mod(num1_civ2(ifile)-1,nbslice_mask)+1;
1932                maskname =name_generator(maskbase,num1_mask,1,'.png','_i');
1933            end
1934            switch CivMode
1935                case 'CivX'
1936                    if isunix
1937                        cmd_FIX=[Param.xml.FixBin ' -f ' filecell.nc.civ2{ifile,j} ' -fi1 ' num2str(flagindex2(1)) ...
1938                            ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
1939                            ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName ' maskname];
1940                    else
1941                        cmd_FIX=['"' Param.xml.FixBin '" -f "' filecell.nc.civ2{ifile,j} '" -fi1 ' num2str(flagindex2(1)) ...
1942                            ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
1943                            ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName "' maskname '"'];
1944                        cmd_FIX=regexprep(cmd_FIX,'\\','\\\\');
1945                    end
1946                    cmd=[cmd cmd_FIX '\n'];
1947                case 'CivAll'
1948                    fix2.inputFileName=filecell.nc.civ2{ifile,j} ;
1949                    fix2.fi1=num2str(flagindex2(1));
1950                    fix2.fi2=num2str(flagindex2(2));
1951                    fix2.fi3=num2str(flagindex2(3));
1952                    fix2.threshC=num2str(thresh_vec2C);
1953                    fix2.threshV=num2str(thresh_vel2);
1954                    fieldnames=fields(fix2);
1955                    [CivAllxml,uid_fix2]=add(CivAllxml,1,'element','fix2');
1956                    for ilist=1:length(fieldnames)
1957                        val=eval(['fix2.' fieldnames{ilist}]);
1958                        if ischar(val)
1959                            [CivAllxml,uid_t]=add(CivAllxml,uid_fix2,'element',fieldnames{ilist});
1960                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
1961                        end
1962                    end
1963                    CivAllCmd=[CivAllCmd ' fix2 '];
1964            end
1965        end
1966       
1967        %CheckPatch2
1968        if Param.CheckPatch2==1
1969            switch CivMode
1970                case 'CivX'
1971                    cmd_PATCH=cmd_patch(filecell.nc.civ2{ifile,j},param.Patch2,Param.xml.PatchBin);
1972                    cmd=[cmd cmd_PATCH '\n'];
1973                case 'CivAll'
1974                    patch2.inputFileName=filecell.nc.civ1{ifile,j} ;
1975                    patch2.nopt=subdomain_patch2;
1976                    patch2.maxdiff=thresh_patch2;
1977                    patch2.ro=rho_patch2;
1978                    test_grid=get(handles.get_gridpatch2,'Value');
1979                    if test_grid
1980                        patch2.gridflag='y';
1981                        gridname=get(handles.grid_patch2,'String');
1982                        if isequal(gridname(end-3:end),'grid')
1983                            nbslice_grid=str2double(gridname(1:end-4)); %
1984                            if ~isnan(nbslice_grid)
1985                                num1_grid=mod(num1_civ2(ifile)-1,nbslice_grid)+1;
1986                                patch2.gridPatch=[filecell.filebase '_' name_generator(gridname,num1_grid,1,'.grid','_i')];
1987                                if ~exist(patch2.gridPatch,'file')
1988                                    msgbox_uvmat('ERROR','grid file absent for patch2')
1989                                end
1990                            elseif exist(gridname,'file')
1991                                patch2.gridPatch=gridname;
1992                            else
1993                                msgbox_uvmat('ERROR','grid file absent for patch2')
1994                            end
1995                        end
1996                    else
1997                        patch2.gridPatch='none';
1998                        patch2.gridflag='n';
1999                        patch2.m=nx_patch2;
2000                        patch2.n=ny_patch2;
2001                    end
2002                    patch2.convectFlow='n';
2003                    fieldnames=fields(patch2);
2004                    [CivAllxml,uid_patch2]=add(CivAllxml,1,'element','patch2');
2005                    for ilist=1:length(fieldnames)
2006                        val=eval(['patch2.' fieldnames{ilist}]);
2007                        if ischar(val)
2008                            [CivAllxml,uid_t]=add(CivAllxml,uid_patch2,'element',fieldnames{ilist});
2009                            [CivAllxml,uid_t2]=add(CivAllxml,uid_t,'chardata',val);
2010                        end
2011                    end
2012                    CivAllCmd=[CivAllCmd ' patch2 '];
2013            end
2014        end
2015 
2016        switch CivMode
2017            case {'CivX','CivAll'}
2018                if isequal(CivMode,'CivAll')
2019                    save(CivAllxml,[OutputFile '.xml']);
2020                    cmd=[cmd sparam.CivBin ' -f ' OutputFile '.xml '  CivAllCmd ' >' OutputFile '.log' '\n'];
2021                end
2022               
2023                % create the .bat file used in run or batch
2024                filename_bat=[OutputFile '.bat'];
2025                [fid,message]=fopen(filename_bat,'w');
2026                if isequal(fid,-1)
2027                    msgbox_uvmat('ERROR', ['creation of .bat file: ' message])
2028                    return
2029                end
2030                fprintf(fid,cmd);
2031                fclose(fid);
2032               
2033                if isunix
2034                    system(['chmod +x ' filename_bat]);
2035                end
2036               
2037                batch_file_list{length(batch_file_list)+1}=filename_bat;
2038               
2039%                 if batch
2040%                     switch batch_mode
2041%                         case 'sge'                           
2042%                             display(['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2043%                             eval(  ['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' filename_bat]);
2044%                         case 'oar'
2045% %                             eval(  ['!chmod +x ' filename_bat]);
2046% %                             %eval(  ['!oarsub -n CIVX -l /core=1,walltime=00:10:00  ' filename_bat]);                   
2047% %                             eval(  ['!oarsub -n CIVX -l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:10:00"   ' filename_bat]);
2048%
2049%                     cmd_str=['sh ' filename_bat];
2050%                     super_cmd{length(super_cmd)+1}=cmd_str;
2051%                           
2052%                     end
2053%                 else
2054%                     %% to lauch the jobs locally :
2055%                     if(isunix)
2056%                         cmd_str=['. ' filename_bat];
2057%                     else %case of Windows
2058%                         cmd_str=['@call "' regexprep(filename_bat,'\\','\\\\') '"'];
2059%                     end
2060%                     super_cmd=[super_cmd cmd_str '\n'];
2061%                     disp(cmd_str);
2062%                 end
2063            case 'Matlab'
2064                drawnow
2065                if Param.CheckCiv1==1
2066                    Param.Civ1=Param.Civ1;
2067                end
2068                if Param.CheckFix1==1
2069                    fix1.WarnFlags=[];
2070                    if get(handles.CheckFmin2,'Value')
2071                        fix1.WarnFlags=[fix1.WarnFlags -2];
2072                    end
2073                    if get(handles.CheckF3,'Value')
2074                        fix1.WarnFlags=[fix1.WarnFlags 3];
2075                    end
2076                    fix1.LowerBoundCorr=thresh_vecC1;
2077                    if get(handles.num_MinVel,'Value')
2078                        fix1.UppperBoundVel=thresh_vel1;
2079                    else
2080                        fix1.LowerBoundVel=thresh_vel1;
2081                    end
2082                    if get(handles.CheckMask,'Value')
2083                        fix1.MaskName=maskname;
2084                    end
2085                    Param.Fix1=fix1;
2086                end
2087                if Param.CheckPatch1==1
2088                    if strcmp(compare,'stereo PIV')
2089                        filebase_A=filecell.filebase;
2090                        [pp,ff]=fileparts(filebase_A);
2091                        filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
2092                        RUN_STLIN(filecell.ncA.civ1{ifile,j},filecell.nc.civ1{ifile,j},'civ1',filecell.st{ifile,j},...
2093                            str2num(nx_patch1),str2num(ny_patch1),str2num(thresh_patch1),[filebase_A '.xml'],[filebase_B '.xml'])
2094                    else
2095                        Param.Patch1.Rho=rho_patch1;
2096                        Param.Patch1.Threshold=thresh_patch1;
2097                        Param.Patch1.SubDomain=subdomain_patch1;
2098                    end
2099                end
2100                if Param.CheckCiv2==1
2101                    Param.Civ2=Param.Civ2;
2102                end
2103                if Param.CheckFix2==1
2104                    fix2.WarnFlags=[];
2105                    if get(handles.CheckFmin2,'Value')
2106                        fix2.WarnFlags=[fix2.WarnFlags -2];
2107                    end
2108                    if get(handles.CheckF4,'Value')
2109                        fix2.WarnFlags=[fix2.WarnFlags 4];
2110                    end
2111                    if get(handles.CheckF3,'Value')
2112                        fix2.WarnFlags=[fix2.WarnFlags 3];
2113                    end
2114                    fix2.LowerBoundCorr=thresh_vec2C;
2115                    if get(handles.num_MinVel,'Value')
2116                        fix2.UppperBoundVel=thresh_vel2;
2117                    else
2118                        fix2.LowerBoundVel=thresh_vel2;
2119                    end
2120                    if get(handles.CheckMask,'Value')
2121                        fix2.MaskName=maskname;
2122                    end
2123                    Param.Fix2=fix2;
2124                end
2125                if Param.CheckPatch2==1
2126                    if strcmp(compare,'stereo PIV')
2127                        filebase_A=filecell.filebase;
2128                        [pp,ff]=fileparts(filebase_A);
2129                        filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
2130                        RUN_STLIN(filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},'civ2',filecell.st{ifile,j},...
2131                            str2num(nx_patch2),str2num(ny_patch2),str2num(thresh_patch2),[filebase_A '.xml'],[filebase_B '.xml'])
2132                    else
2133                        Param.Patch2.Rho=rho_patch2;
2134                        Param.Patch2.Threshold=thresh_patch2;
2135                        Param.Patch2.SubDomain=subdomain_patch2;
2136                    end
2137                end
2138                if ~strcmp(compare,'stereo PIV')
2139                    [Data,erromsg]=civ_uvmat(Param,filecell.nc.civ1{ifile,j});
2140                    if isempty(errormsg)
2141                        display([filecell.nc.civ1{ifile,j} ' written'])
2142                    else
2143                        msgbox_uvmat('ERROR',errormsg)
2144                    end
2145
2146                end
2147        end
2148    end
2149end
2150
2151if batch
2152    switch batch_mode                       
2153        case 'sge'
2154            for p=1:length(batch_file_list)
2155                cmd=['!qsub -p ' pvalue ' -q civ.q -e ' flname '.errors -o ' flname '.log' ' ' batch_file_list{p}];
2156                display(cmd);eval(cmd);
2157            end
2158        case 'oar_old'
2159                for p=1:length(batch_file_list)
2160                    oar_command=['!oarsub -n CIVX -q nicejob '...
2161                   '-E ' regexprep(batch_file_list{p},'.bat','.errors') ' -O ' regexprep(batch_file_list{p},'.bat','.log ')...
2162                    '-l "/core=1+{type = ''smalljob''}/licence=1,walltime=00:60:00"   ' batch_file_list{p}];
2163                display(oar_command);eval(oar_command);
2164                end         
2165       case 'oar' %oar-dispatch.pl
2166                filename_joblist=fullfile(Rootbat,'job_list.txt');
2167                fid=fopen(filename_joblist,'w');
2168
2169                for p=1:length(batch_file_list)
2170                    oar_command=['oarsub -n CIVX '...
2171                        '-l "/core=1,walltime=00:10:00"   ' batch_file_list{p}];
2172                    fprintf(fid,[oar_command '\n']);
2173                end
2174                fclose(fid);
2175                walltime=datestr(length(batch_file_list)*10/24/60,13);
2176                oar_command=['oarsub -t container -n civx-container -l /core=24,walltime=' walltime ' "/fsnet/data/legi/calcul9/home/gostiaux/Documents/MATLAB/uvmat_dev/oar-dispatch -f '...
2177                    filename_joblist '"'];
2178                filename_oarcommand=fullfile(Rootbat,'oar_command');
2179                fid=fopen(filename_oarcommand,'w');
2180                fprintf(fid,[oar_command '\n']);
2181                fclose(fid);
2182                display(oar_command);
2183                eval(['! . ' filename_oarcommand])
2184        case 'oar_mpi'
2185                filename_joblist=fullfile(Rootbat,'job_list.txt');
2186                fid=fopen(filename_joblist,'w');
2187
2188                for p=1:length(batch_file_list)
2189                    fprintf(fid,[batch_file_list{p} '\n']);
2190                end
2191                fclose(fid)
2192               
2193               
2194%                 oarsub -S ./oar.sub
2195
2196               
2197               
2198
2199               
2200        case 'oar_new' % to be develloped with Patrick Begou
2201                filename_joblist=fullfile(Rootbat,'job_list.txt');
2202                fid=fopen(filename_superbat,'w');
2203                if fid==-1
2204                    msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2205                    return
2206                end
2207                for p=1:length(batch_file_list)
2208                    fprintf(fid,[batch_file_list{p} '\n']);
2209                end
2210                fclose(fid);
2211                walltime=datestr(length(super_cmd)*10/24/60,13);
2212                eval(  ['!oarsub -n CIVX -q nicejob -l "/core=1+{type = ''smalljob''}/licence=1,walltime=' walltime '"   ' filename_superbat]);
2213    end
2214else
2215    if ~isequal(CivMode,'Matlab')
2216        filename_superbat=fullfile(Rootbat,'job_list.bat');
2217        fid=fopen(filename_superbat,'w');
2218        if fid==-1
2219            msgbox_uvmat('ERROR',['cannot create the command file ' filename_superbat])
2220            return
2221        end
2222        for p=1:length(batch_file_list)
2223            if isunix
2224                fprintf(fid,['sh ' batch_file_list{p} '\n']);
2225            else
2226                fprintf(fid,['@call "' regexprep(filename_bat,'\\','\\\\') '"' '\n']);
2227            end
2228        end
2229        fclose(fid);
2230        if(isunix)
2231            system(['chmod +x ' filename_superbat]);
2232        end
2233        system([filename_superbat ' &']);% execute main commmand
2234    end
2235   
2236end
2237
2238
2239%% save interface state
2240if isfield(filecell,'nc')
2241    if isfield(filecell.nc,'civ2')
2242        fileresu=filecell.nc.civ2{1,1};
2243    else
2244        fileresu=filecell.nc.civ1{1,1};
2245    end
2246end
2247[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2248namedoc=fullfile(RootPath,subdir,RootFile);
2249detect=1;
2250while detect==1
2251    namefigfull=[namedoc '.fig'];
2252    hh=dir(namefigfull);
2253    if ~isempty(hh)
2254        detect=1;
2255        namedoc=[namedoc '.0'];
2256    else
2257        detect=0;
2258    end
2259end
2260saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2261
2262%Save info in personal profile (initiate browser next time) TODO
2263MenuFile={};
2264dir_perso=prefdir;
2265profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
2266if exist(profil_perso,'file')
2267    hh=load (profil_perso);
2268      if isfield(hh,'MenuFile')
2269          MenuFile=hh.MenuFile;
2270      end
2271      if isfield(filecell.nc,'civ2')
2272          MenuFile=[filecell.nc.civ2{1,1}; MenuFile];
2273      else
2274           MenuFile=[filecell.nc.civ1{1,1}; MenuFile];
2275      end
2276      save (profil_perso,'MenuFile','-append'); %store the file names for future opening of uvmat
2277else
2278    MenuFile=filecell.ima1.civ1(1,1);
2279    save (profil_perso,'MenuFile')
2280end
2281
2282
2283%------------------------------------------------------------------------
2284% --- determine the list of reference indices of processing file
2285function [ref_i,ref_j,errormsg]=find_ref_indices(handles)
2286%------------------------------------------------------------------------
2287errormsg=''; %default error message
2288first_i=str2double(get(handles.first_i,'String'));%first index i
2289last_i=str2double(get(handles.last_i,'String'));%last index i
2290incr_i=str2double(get(handles.incr_i,'String'));% increment
2291if isequal(get(handles.first_j,'Visible'),'on')
2292    first_j=str2double(get(handles.first_j,'String'));%first index j
2293    last_j=str2double(get(handles.last_j,'String'));%last index j
2294    incr_j=str2double(get(handles.incr_j,'String'));% increment
2295else
2296    first_j=1;
2297    last_j=1;
2298    incr_j=1;
2299end
2300ref_i=first_i:incr_i:last_i;% list of i indices (reference values for each pair)
2301ref_j=first_j:incr_j:last_j;% list of j indices (reference values for each pair)
2302if isnan(first_i)||isnan(first_j)
2303    errormsg='first field number not defined';
2304elseif isnan(last_i)||isnan(last_j)
2305    errormsg='last field number not defined';
2306elseif isnan(incr_i)||isnan(incr_j)
2307    errormsg='increment in field number not defined';
2308elseif last_i < first_i || last_j < first_j
2309    errormsg='last field number must be larger than the first one';
2310end
2311
2312%------------------------------------------------------------------------
2313% --- determine the list of filenames and indices needed for launch_job
2314function [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]=...
2315    set_civ_filenames(handles,ref_i,ref_j,checkbox)
2316%------------------------------------------------------------------------
2317filecell=[];%default
2318
2319%% get the root names nomenclature and numbers
2320filebase=get(handles.RootName,'String');
2321
2322if isempty(filebase)||isequal(filebase,'')
2323    msgbox_uvmat('ERROR','no input files')
2324    return
2325end
2326
2327%filebase=regexprep(filebase,'\.fsnet','fsnet');% temporary fix for cluster Coriolis
2328filecell.filebase=filebase;
2329
2330browse=get(handles.RootName,'UserData');
2331compare_list=get(handles.ListCompareMode,'String');
2332val=get(handles.ListCompareMode,'Value');
2333compare=compare_list{val};
2334if strcmp(compare,'displacement')
2335    mode='displacement';
2336else
2337    mode_list=get(handles.ListPairMode,'String');
2338    mode_value=get(handles.ListPairMode,'Value');
2339    mode=mode_list{mode_value};
2340end
2341%time=get(handles.RootName,'UserData'); %get the set of times
2342ext_ima=get(handles.ImaExt,'String');
2343nom_type_nc=browse.nom_type_nc;
2344if isfield(browse,'nom_type_ima')
2345    nom_type_ima2=browse.nom_type_ima;
2346end
2347if isempty(nom_type_ima2),nom_type_ima2='1';end; %default
2348if isempty(nom_type_nc),nom_type_nc='_i1-i2';end; %default
2349[num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2350    find_pair_indices(handles,ref_i,ref_j,mode);
2351%determine the new filebase for 'displacement' ListPairMode (comparison of two series)
2352filebase_B=filebase;% root name of the second field series for stereo
2353if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2354%     test_disp=1;
2355    nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2356    [Path2,Name2]=fileparts(filebase_B);
2357    Path1=Path2;
2358    Name1=get(handles.RootName_1,'String');% root name of the first field series for stereo
2359    filebase_A=fullfile(Path1,Name1);
2360    if length(Name1)>6
2361        Name1=Name1(end-5:end);
2362    end
2363    if length(Name2)>6
2364        Name2=Name2(end-5:end);
2365    end
2366    filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2367else
2368%     test_disp=0;
2369    filebase_A=filebase;
2370    nom_type_ima1=nom_type_ima2;
2371    filebase_AB=filebase;
2372end
2373if strcmp(compare,'displacement')
2374    filebase_ima1=filebase_A;
2375    filebase_ima2=filebase_B;
2376    filebase_nc=filebase_AB; %root name for the result of civ2
2377else
2378    filebase_ima1=filebase_B;
2379    filebase_ima2=filebase_B;
2380    filebase_nc=filebase_B;
2381end
2382
2383%determine reference files for fix:
2384file_ref_fix1={};%default
2385file_ref_fix2={};
2386nbfield=length(num1_civ1);
2387nbslice=length(num_a_civ1);
2388if checkbox(2)==1% fix1 performed
2389    ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2390    if ~isempty(ref)
2391        first_i=str2double(get(handles.first_i,'String'));
2392        last_i=str2double(get(handles.last_i,'String'));
2393        incr_i=str2double(get(handles.incr_i,'String'));
2394        first_j=str2double(get(handles.first_j,'String'));
2395        last_j=str2double(get(handles.last_j,'String'));
2396        incr_j=str2double(get(handles.incr_j,'String'));
2397        num_i_ref=first_i:incr_i:last_i;
2398        num_j_ref=first_j:incr_j:last_j;
2399        if isequal(mode,'displacement')
2400            num_i1=num_i_ref;
2401            num_i2=num_i_ref;
2402            num_j1=num_j_ref;
2403            num_j2=num_j_ref;
2404        elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2405            num_i1=num_i_ref;
2406            num_i2=num_i1;
2407            num_j1=ref.num_a*ones(size(num_i_ref));
2408            num_j2=ref.num_b*ones(size(num_i_ref));
2409        elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2410            delta1=floor((ref.num2-ref.num1)/2);
2411            delta2=ceil((ref.num2-ref.num1)/2);
2412            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2413            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2414            if isempty(ref.num_a)
2415                ref.num_a=1;
2416            end
2417            num_j1=ref.num_a*ones(size(num_i1));
2418            num_j2=num_j1;
2419        elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2420            delta1=floor((ref.num_b-ref.num_a)/2);
2421            delta2=ceil((ref.num_b-ref.num_a)/2);
2422            num_i1=ref.num1*ones(size(num_i_ref));
2423            num_i2=num_i1;
2424            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2425            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2426        end
2427        for ifile=1:nbfield
2428            for j=1:nbslice
2429                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);%
2430                file_ref_fix1(ifile,j)={file_ref};
2431                if ~exist(file_ref,'file')
2432                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix1'])
2433                    filecell=[];
2434                    return
2435                end
2436            end
2437        end
2438    end
2439end
2440
2441%determine reference files for checkfix2:
2442if checkbox(5)==1% fix2 performed
2443    ref=get(handles.ref_fix2,'UserData');
2444    if ~isempty(ref)
2445        first_i=str2double(get(handles.first_i,'String'));
2446        last_i=str2double(get(handles.last_i,'String'));
2447        incr_i=str2double(get(handles.incr_i,'String'));
2448        first_j=str2double(get(handles.first_j,'String'));
2449        last_j=str2double(get(handles.last_j,'String'));
2450        incr_j=str2double(get(handles.incr_j,'String'));
2451        num_i_ref=first_i:incr_i:last_i;
2452        num_j_ref=first_j:incr_j:last_j;
2453        if isequal(mode,'displacement')
2454            num_i1=num_i_ref;
2455            num_i2=num_i_ref;
2456            num_j1=num_j_ref;
2457            num_j2=num_j_ref;
2458        elseif isequal(mode,'pair j1-j2')
2459            num_i1=num_i_ref;
2460            num_i2=num_i1;
2461            num_j1=ref.num_a;
2462            num_j2=ref.num_b;
2463        elseif isequal(mode,'series(Di)')
2464            delta1=floor((ref.num2-ref.num1)/2);
2465            delta2=ceil((ref.num2-ref.num1)/2);
2466            num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2467            num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2468            num_j1=ref.num_a*ones(size(num_i1));
2469            num_j2=num_j1;
2470        elseif isequal(mode,'series(Dj)')
2471            delta1=floor((ref.num_b-ref.num_a)/2);
2472            delta2=ceil((ref.num_b-ref.num_a)/2);
2473            num_i1=ref.num1*ones(size(num_i_ref));
2474            num_i2=num_i1;
2475            num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2476            num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2477        end
2478        for ifile=1:nbfield
2479            for j=1:nbslice
2480                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);%
2481                file_ref_fix2(ifile,j)={file_ref};
2482                if ~exist(file_ref,'file')
2483                    msgbox_uvmat('ERROR',['reference file ' file_ref ' not found for fix2'])
2484                    filecell={};
2485                    return
2486                end
2487            end
2488        end
2489    end
2490end
2491
2492%check dir
2493subdir_civ1=get(handles.txt_SubdirCiv1,'String');%subdirectory subdir_civ1 for the netcdf output data
2494subdir_civ2=get(handles.txt_SubdirCiv2,'String');
2495if isequal(subdir_civ1,''),subdir_civ1='CIV'; end% put default subdir
2496if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2497% currentdir=pwd;%store the current working directory
2498[Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2499if ~exist(Path_ima,'dir')
2500    msgbox_uvmat('ERROR',['path to images ' Path_ima ' not found'])
2501    filecell={};
2502    return
2503end
2504[xx,message]=fileattrib(Path_ima);
2505if ~isempty(message) && ~isequal(message.UserWrite,1)
2506    msgbox_uvmat('ERROR',['No writting access to ' Path_ima])
2507    filecell={};
2508%     cd(currentdir);
2509    return
2510end
2511
2512%check the existence of the netcdf and image files involved
2513% %%%%%%%%%%%%  case CheckCiv1 activated   %%%%%%%%%%%%%
2514if checkbox(1)==1;
2515    detect=1;
2516    vers=0;
2517    subdir_civ1_new=subdir_civ1;
2518    while detect==1 %create a new subdir if the netcdf files already exist
2519        for ifile=1:nbfield
2520            for j=1:nbslice
2521                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);
2522                detect=exist(filename,'file')==2;
2523                if detect% if a netcdf file already exists
2524                    indstr=regexp(subdir_civ1_new,'\D');
2525                    if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2526                        vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2527                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2528                    else
2529                        vers=vers+1;
2530                        subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) '_' num2str(vers)];       
2531                    end
2532                    subdir_civ2=subdir_civ1_new;
2533                    break
2534                end
2535                filecell.nc.civ1(ifile,j)={filename};
2536            end
2537            if detect% if a netcdf file already exists
2538                break
2539            end
2540        end
2541 
2542        %create the new txt_SubdirCiv1
2543        if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2544%             cd(Path_ima);         
2545            [xx,msg1]=mkdir(fullfile(Path_ima,subdir_civ1_new));
2546
2547            if ~strcmp(msg1,'')
2548                msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])%error message for directory creation
2549                filecell={};
2550                return
2551            elseif isunix         
2552                [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2553                if ~strcmp(msg2,'')
2554                    msgbox_uvmat('ERROR',['pb of permission for  ' fullfile(Path_ima,subdir_civ1_new) ': ' msg2])%error message for directory creation
2555                    filecell={};
2556                    return
2557                end
2558            end
2559%             cd(currentdir);
2560        end
2561        if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2562            for ifile=1:nbfield
2563                for j=1:nbslice
2564                    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);%
2565                    detect=exist(filename,'file')==2;
2566                    if detect% if a netcdf file already exists
2567                       indstr=regexp(subdir_civ1_new,'\D');
2568                       if indstr(end)<length(subdir_civ1_new) %subdir_civ1 ends by a number
2569                           vers=str2double(subdir_civ1_new(indstr(end)+1:end))+1;
2570                           subdir_civ1_new=[subdir_civ1_new(1:indstr(end)) num2str(vers)];
2571                       else
2572                           vers=vers+1;
2573                           subdir_civ1_new=[subdir_civ1_new '_' num2str(vers)];
2574                       end
2575                       subdir_civ2=subdir_civ1;
2576                       break
2577                    end
2578                    filecell.ncA.civ1(ifile,j)={filename};
2579                end
2580                if detect% if a netcdf file already exists
2581                    break
2582                end
2583            end
2584            %create the new txt_SubdirCiv1
2585            if ~exist(fullfile(Path_ima,subdir_civ1_new),'dir')
2586%                    cd(Path_ima);         
2587                [xx,msg1]=mkdir(fullfile(Path_ima,subdir_civ1_new));
2588%                             cd(currentdir);
2589                if ~strcmp(msg1,'')
2590                    msgbox_uvmat('ERROR',['cannot create ' subdir_civ1_new ': ' msg1])
2591%                     cd(currentdir)
2592                    filecell={};
2593                    return
2594                else
2595                    [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ1_new),'+w','g'); %yield writing access (+w) to user group (g)
2596                    if ~strcmp(msg2,'')
2597                        msgbox_uvmat('ERROR',['pb of permission for ' subdir_civ1_new ': ' msg2])%error message for directory creation
2598%                         cd(currentdir)
2599                        filecell={};
2600                        return
2601                    end
2602                end
2603            end
2604        end
2605    end
2606    subdir_civ1=subdir_civ1_new;
2607    % get image names
2608    for ifile=1:nbfield
2609        for j=1:nbslice
2610            filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2611            idetect(j)=exist(filename,'file')==2;
2612            filecell.ima1.civ1(ifile,j)={filename}; %first image
2613            filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2614            idetect_1(j)=exist(filename,'file')==2;
2615            filecell.ima2.civ1(ifile,j)={filename};%second image
2616        end
2617        [idetectmin,indexj]=min(idetect);
2618        if idetectmin==0,
2619            msgbox_uvmat('ERROR',[filecell.ima1.civ1{ifile,indexj} ' not found'])
2620            filecell={};
2621           % cd(currentdir)
2622            return
2623        end
2624        [idetectmin,indexj]=min(idetect_1);
2625        if idetectmin==0,
2626            msgbox_uvmat('ERROR',[filecell.ima2.civ1{ifile,indexj} ' not found'])
2627            filecell={};
2628            %cd(currentdir)
2629            return
2630        end
2631    end
2632    if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
2633        for ifile=1:nbfield
2634            for j=1:nbslice
2635                filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2636                idetect(j)=exist(filename,'file')==2;
2637                filecell.imaA1.civ1(ifile,j)={filename} ;%first image
2638                filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2639                idetect_1(j)=exist(filename,'file')==2;
2640                filecell.imaA2.civ1(ifile,j)={filename};%second image
2641            end
2642            [idetectmin,indexj]=min(idetect);
2643            if idetectmin==0,
2644                msgbox_uvmat('ERROR',[filecell.imaA1.civ1{ifile,indexj} ' not found'])
2645                filecell={};
2646               % cd(currentdir)
2647                return
2648            end
2649            [idetectmin,indexj]=min(idetect_1);
2650            if idetectmin==0,
2651                msgbox_uvmat('ERROR',[filecell.imaA2.civ1{ifile,indexj} ' not found'])
2652                filecell={};
2653               % cd(currentdir)
2654                return
2655            end
2656        end
2657    end
2658   
2659    %%%%%%%%%%%%%  checkfix1 or checkpatch1 activated but no checkciv1   %%%%%%%%%%%%%
2660elseif (checkbox(2)==1 || checkbox(3)==1);
2661    for ifile=1:nbfield
2662        for j=1:nbslice
2663            filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2664                nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2665            detect=exist(filename,'file')==2;
2666            if detect==0
2667                msgbox_uvmat('ERROR',[filename ' not found'])
2668                filecell={};
2669               % cd(currentdir)
2670                return
2671            end
2672            filecell.nc.civ1(ifile,j)={filename};
2673        end
2674    end
2675    if strcmp(compare,'stereo PIV')
2676        for ifile=1:nbfield
2677            for j=1:nbslice
2678                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);%
2679                filecell.ncA.civ1(ifile,j)={filename};
2680                if ~exist(filename,'file')
2681                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2682                    set(handles.RUN, 'Enable','On')
2683                    set(handles.RUN,'BackgroundColor',[1 0 0])
2684                    filecell={};
2685                    %cd(currentdir)
2686                    return
2687                end
2688            end
2689        end
2690    end
2691end
2692
2693%%%%%%%%%%%%%  if checkciv2 performed with pairs different than checkciv1  %%%%%%%%%%%%%
2694testdiff=0;
2695if (checkbox(4)==1)&&...
2696        ((get(handles.ListPairCiv1,'Value')~=get(handles.ListPairCiv2,'Value'))||~strcmp(subdir_civ2,subdir_civ1))
2697    testdiff=1;
2698    detect=1;
2699    vers=0;
2700    subdir_civ2_new=subdir_civ2;
2701    while detect==1 %create a new subdir if the netcdf files already exist
2702        for ifile=1:nbfield
2703            for j=1:nbslice
2704                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);%
2705                detect=exist(filename,'file')==2;
2706                if detect% if a netcdf file already exists
2707                    indstr=regexp(subdir_civ2,'\D');
2708                    if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2709                        vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2710                        subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2711                    else
2712                        vers=vers+1;
2713                        subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2714                    end
2715                    break
2716                end
2717                filecell.nc.civ2(ifile,j)={filename};
2718            end
2719            if detect% if a netcdf file already exists
2720                break
2721            end
2722        end
2723        %create the new subdir_civ2_new
2724        if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2725            [xx,m2]=mkdir(fullfile(Path_ima,subdir_civ2_new));
2726            [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2727            if ~isequal(m2,'')
2728                msgbox_uvmat('ERROR',['cannot create ' fullfile(Path_ima,subdir_civ2_new) ': ' m2])
2729                filecell={};
2730               % cd(currentdir)
2731                return
2732            end
2733        end
2734        if strcmp(compare,'stereo PIV')%check second nc series
2735            for ifile=1:nbfield
2736                for j=1:nbslice
2737                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2738                        nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2_new);%
2739                    detect=exist(filename,'file')==2;
2740                    if detect% if a netcdf file already exists
2741                        indstr=regexp(subdir_civ2,'\D');
2742                        if indstr(end)<length(subdir_civ2) %subdir_civ1 ends by a number
2743                           vers=str2double(subdir_civ2(indstr(end)+1:end))+1;
2744                           subdir_civ2_new=[subdir_civ2(1:indstr(end)) num2str(vers)];
2745                        else
2746                           vers=vers+1;
2747                           subdir_civ2_new=[subdir_civ1 '_' num2str(vers)];
2748                        end
2749                        break
2750                    end
2751                    filecell.ncA.civ2(ifile,j)={filename};
2752                end
2753                if detect% if a netcdf file already exists
2754                    break
2755                end
2756            end
2757            subdir_civ2=subdir_civ2_new;
2758            %create the new txt_SubdirCiv1
2759            if ~exist(fullfile(Path_ima,subdir_civ2_new),'dir')
2760                [xx,m2]=mkdir(subdir_civ2_new);
2761                 [xx,msg2] = fileattrib(fullfile(Path_ima,subdir_civ2_new),'+w','g'); %yield writing access (+w) to user group (g)
2762                if ~isequal(m2,'')
2763                    msgbox_uvmat('ERROR', ['cannot create ' fullfile(Path_ima,subdir_civ2_new) ': ' m2])%error message for directory creation
2764                  %  cd(currentdir)
2765                    filecell={};
2766                    return
2767                end
2768            end
2769        end
2770    end
2771    subdir_civ2=subdir_civ2_new;
2772end
2773%cd(currentdir);%come back to the current working directory
2774
2775%%%%%%%%%%%%%  if checkciv2 results are obtained or used  %%%%%%%%%%%%%
2776if checkbox(4)==1 || checkbox(5)==1 || checkbox(6)==1 %civ2
2777    %check source netcdf file of checkciv1 estimates
2778    if checkbox(1)==0; %no civ1 performed
2779        for ifile=1:nbfield
2780            for j=1:nbslice
2781                filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2782                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2783                filecell.nc.civ1(ifile,j)={filename};% name of the civ1 file
2784                if ~exist(filename,'file')
2785                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2786                    filecell={};
2787                    return
2788                end
2789                if ~testdiff % civ2 or patch2 are written in the same file as civ1
2790                    if checkbox(4)==0 ; %check the existence of civ2 if it is not calculated
2791                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2792                        if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
2793                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2794                            filecell=[];
2795                            return
2796                        elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2797                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2798                            filecell=[];
2799                            return
2800                        end
2801                    elseif checkbox(3)==0; %check the existence of patch if it is not calculated
2802                        Data=nc2struct(filename,'ListGlobalAttribute','CivStage','patch');
2803                        if ~isempty(Data.CivStage)
2804                            if Data.CivStage<3 %test for civ files
2805                                msgbox_uvmat('ERROR',['no patch data in ' filename])
2806                                filecell=[];
2807                                return
2808                            end
2809                        elseif isempty(Data.patch)||isequal(Data.patch,0)
2810                            msgbox_uvmat('ERROR',['no patch data in ' filename])
2811                            filecell=[];
2812                            return
2813                        end
2814                    end
2815                end
2816            end
2817        end
2818        if strcmp(compare,'stereo PIV')
2819            for ifile=1:nbfield
2820                for j=1:nbslice
2821                    filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2822                        nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2823                    filecell.ncA.civ2(ifile,j)={filename};
2824                    if ~exist(filename,'file')
2825                        msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2826                        set(handles.RUN, 'Enable','On')
2827                        set(handles.RUN,'BackgroundColor',[1 0 0])
2828                        return
2829                    end
2830                end
2831            end
2832        end
2833    end
2834   
2835    detect=1;
2836    %     while detect==1%creates a new subdir if the netcdf files already contain checkciv2 data
2837    for ifile=1:nbfield
2838        for j=1:nbslice
2839            filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2840                nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
2841            detect=exist(filename,'file')==2;
2842            filecell.nc.civ2(ifile,j)={filename};
2843        end
2844    end
2845    %get first image names for checkciv2
2846    if checkbox(1)==1 && isequal(num1_civ1,num1_civ2) && isequal(num_a_civ1,num_a_civ2)
2847        filecell.ima1.civ2=filecell.ima1.civ1;
2848    elseif checkbox(4)==1
2849        for ifile=1:nbfield
2850            for j=1:nbslice
2851                filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
2852                idetect_2(j)=exist(filename,'file')==2;
2853                filecell.ima1.civ2(ifile,j)={filename};%first image
2854            end
2855            [idetectmin,indexj]=min(idetect_2);
2856            if idetectmin==0,
2857                msgbox_uvmat('ERROR',['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'])
2858                filecell=[];
2859                return
2860            end
2861        end
2862    end
2863   
2864    %get second image names for checkciv2
2865    if checkbox(1)==1 && isequal(num2_civ1,num2_civ2) && isequal(num_b_civ1,num_b_civ2)
2866        filecell.ima2.civ2=filecell.ima2.civ1;
2867    elseif checkbox(4)==1
2868        for ifile=1:nbfield
2869            for j=1:nbslice
2870                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
2871                idetect_3(j)=exist(filename,'file')==2;
2872                filecell.ima2.civ2(ifile,j)={filename};%first image
2873            end
2874            [idetectmin,indexj]=min(idetect_3);
2875            if idetectmin==0,
2876                msgbox_uvmat('ERROR',['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'])
2877                filecell=[];
2878                return
2879            end
2880        end
2881    end
2882end
2883if (checkbox(5) || checkbox(6)) && ~checkbox(4)  % need to read an existing netcdf civ2 file
2884    if ~testdiff
2885        filecell.nc.civ2=filecell.nc.civ1;% file already checked
2886    else     % check the civ2 files
2887        for ifile=1:nbfield
2888            for j=1:nbslice
2889                filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2890                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2891                filecell.nc.civ2(ifile,j)={filename};
2892                if ~exist(filename,'file')
2893                    msgbox_uvmat('ERROR',['input file ' filename ' not found'])
2894                    filecell=[];
2895                    return
2896                else
2897                    Data=nc2struct(filename,'ListGlobalAttribute','CivStage','civ2');
2898                    if ~isempty(Data.CivStage) && Data.CivStage<4 %test for civ files
2899                            msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2900                            filecell=[];
2901                            return
2902                    elseif isempty(Data.civ2)||isequal(Data.civ2,0)
2903                        msgbox_uvmat('ERROR',['no civ2 data in ' filename])
2904                        filecell=[];
2905                        return
2906                    end
2907                end
2908            end
2909        end
2910    end
2911end
2912
2913%%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
2914if strcmp(compare,'stereo PIV')
2915    if  checkbox(3) && isequal(get(handles.test_stereo1,'Value'),1)
2916        for ifile=1:nbfield
2917            for j=1:nbslice
2918                filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2919                    nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2920                filecell.st(ifile,j)={filename};
2921            end
2922        end
2923    end
2924    if  checkbox(6) && isequal(get(handles.CheckStereo,'Value'),1)
2925        for ifile=1:nbfield
2926            for j=1:nbslice
2927                filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2928                    nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2929                filecell.st(ifile,j)={filename};
2930            end
2931        end
2932    end
2933end
2934set(handles.txt_SubdirCiv1,'String',subdir_civ1);%update the edit box
2935set(handles.txt_SubdirCiv2,'String',subdir_civ2);%update the edit box
2936browse.nom_type_nc=nom_type_nc;
2937set(handles.RootName,'UserData',browse); %update the nomenclature type for uvmat
2938
2939
2940%COPY IMAGES TO THE FORMAT .png IF NEEDED
2941if isequal(nom_type_ima1,'*')%case of movie files
2942    nom_type_imanew1='_i';
2943else
2944    nom_type_imanew1=nom_type_ima1;
2945end
2946if isequal(nom_type_ima2,'*')%case of movie files
2947    nom_type_imanew2='_i';
2948else
2949    nom_type_imanew2=nom_type_ima2;
2950end
2951if ~isequal(ext_ima,'.png')
2952    %%type of image file
2953    type_ima1='none';%default
2954    movieobject1=[];%default
2955    if strcmpi(ext_ima,'.avi')
2956        hhh=which('mmreader');
2957        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2958            type_ima1='movie';
2959            movieobject1=mmreader([filebase_ima2 ext_ima]);
2960        else
2961            type_ima1='avi';
2962        end
2963    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2964        form=imformats(ext_ima(2:end));
2965        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2966            if isequal(nom_type_ima1,'*');
2967                type_ima1='multimage';%image series in a single image file
2968            else
2969                type_ima1='image';
2970            end
2971        end
2972    end
2973    type_ima2='none';%default
2974    movieobject2=[];
2975    if strcmpi(ext_ima,'.avi')
2976        hhh=which('mmreader');
2977        if ~isequal(hhh,'')&& mmreader.isPlatformSupported()% if the mmreader function is found (recent version of matlab)
2978            type_ima2='movie';
2979            movieobject2=mmreader([filebase_ima2 ext_ima]);
2980        else
2981            type_ima2='avi';
2982        end
2983    elseif ischar(ext_ima) && ~isempty(ext_ima(2:end))
2984        form=imformats(ext_ima(2:end));
2985        if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
2986            if isequal(nom_type_ima1,'*');
2987                type_ima2='multimage';%image series in a single image file
2988            else
2989                type_ima2='image';
2990            end
2991        end
2992    end
2993    %npxy=get(handles.ImaExt,'UserData');
2994    % %     if numel(npxy)<2
2995    %
2996    %         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ImaExt,nom_type_ima1);
2997    %         A=imread(filename);
2998    %         npxy=size(A);
2999    % %     end
3000    %     npy=npxy(1);
3001    %     npx=npxy(2);
3002    if checkbox(1) %if civ1 is performed
3003        h = waitbar(0,'copy images to the .png format for civ1');% display a wait bar
3004        for ifile=1:nbfield
3005            waitbar(ifile/nbfield);
3006            for j=1:nbslice
3007                filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
3008                if ~exist(filename,'file')
3009                    A=read_image(filecell.ima1.civ1{ifile,j},type_ima1,num1_civ1(ifile),movieobject1);
3010                    imwrite(A,filename,'BitDepth',16);
3011                end
3012                filecell.ima1.civ1(ifile,j)={filename};
3013                filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
3014                if ~exist(filename,'file')
3015                    A=read_image(filecell.ima2.civ1{ifile,j},type_ima2,num2_civ1(ifile),movieobject2);
3016                    imwrite(A,filename,'BitDepth',16);
3017                end
3018                filecell.ima2.civ1(ifile,j)={filename};
3019            end
3020        end
3021        close(h)
3022    end
3023    if checkbox(4) %if civ2 is performed
3024        h = waitbar(0,'copy images to the .png format for civ2');% display a wait bar
3025        for ifile=1:nbfield
3026            waitbar(ifile/nbfield);
3027            for j=1:nbslice
3028                filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3029                if ~exist(filename,'file')
3030                    A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),type_ima2,num1_civ2(ifile));
3031                    imwrite(A,filename,'BitDepth',16);
3032                end
3033                filecell.ima1.civ2(ifile,j)={filename};
3034                filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3035                if ~exist(filename,'file')
3036                    A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),type_ima2,num2_civ2(ifile));
3037                    imwrite(A,filename,'BitDepth',16);
3038                end
3039                filecell.ima2.civ2(ifile,j)={filename};
3040            end
3041        end
3042        close(h);
3043    end
3044end
3045
3046%------------------------------------------------------------------------
3047% --- determine the list of index pairs of processing file
3048function [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
3049    find_pair_indices(handles,ref_i,ref_j,mode)
3050%------------------------------------------------------------------------
3051
3052list_civ1=get(handles.ListPairCiv1,'String');
3053index_civ1=get(handles.ListPairCiv1,'Value');
3054str_civ1=list_civ1{index_civ1};%string defining the image pairs for civ1
3055if isempty(str_civ1)||isequal(str_civ1,'')
3056    msgbox_uvmat('ERROR','no image pair selected for civ1')
3057    return
3058end
3059list_civ2=get(handles.ListPairCiv2,'String');
3060index_civ2=get(handles.ListPairCiv2,'Value');
3061if index_civ2>length(list_civ2)
3062    list_civ2=list_civ1;
3063    index_civ2=index_civ1;
3064end
3065str_civ2=list_civ2{index_civ2};%string defining the image pairs for civ2
3066
3067if isequal (mode,'series(Di)')
3068    lastfield=str2double(get(handles.nb_field,'String'));
3069    num1_civ1=ref_i-floor(index_civ1/2)*ones(size(ref_i));% set of first image numbers
3070    num2_civ1=ref_i+ceil(index_civ1/2)*ones(size(ref_i));
3071    num_a_civ1=ref_j;
3072    num_b_civ1=ref_j;
3073    num1_civ2=ref_i-floor(index_civ2/2)*ones(size(ref_i));
3074    num2_civ2=ref_i+ceil(index_civ2/2)*ones(size(ref_i));
3075    num_a_civ2=ref_j;
3076    num_b_civ2=ref_j;   
3077   
3078    % adjust the first and last field number
3079    lastfield=str2double(get(handles.nb_field,'String'));
3080    if isnan(lastfield)
3081        indsel=find((num1_civ1 >= 1)&(num1_civ2 >= 1));
3082    else
3083        indsel=find((num2_civ1 <= lastfield)&(num2_civ2 <= lastfield)&(num1_civ1 >= 1)&(num1_civ2 >= 1));
3084    end
3085    if length(indsel)>=1
3086        firstind=indsel(1);
3087        lastind=indsel(end);
3088        set(handles.first_i,'String',num2str(ref_i(firstind)))%update the display of first and last fields
3089        set(handles.last_i,'String',num2str(ref_i(lastind)))
3090        ref_i=ref_i(indsel);
3091        num1_civ1=num1_civ1(indsel);
3092        num1_civ2=num1_civ2(indsel);
3093        num2_civ1=num2_civ1(indsel);
3094        num2_civ2=num2_civ2(indsel);
3095    end
3096elseif isequal (mode,'series(Dj)')
3097    lastfield_j=str2double(get(handles.nb_field2,'String'));
3098    num1_civ1=ref_i;% set of first image numbers
3099    num2_civ1=ref_i;
3100    num_a_civ1=ref_j-floor(index_civ1/2)*ones(size(ref_j));
3101    num_b_civ1=ref_j+ceil(index_civ1/2)*ones(size(ref_j));
3102    num1_civ2=ref_i;
3103    num2_civ2=ref_i;
3104    num_a_civ2=ref_j-floor(index_civ2/2)*ones(size(ref_j));
3105    num_b_civ2=ref_j+ceil(index_civ2/2)*ones(size(ref_j));
3106    % adjust the first and last field number
3107    if isnan(lastfield_j)
3108        indsel=find((num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3109    else
3110        indsel=find((num_b_civ1 <= lastfield_j)&(num_b_civ2 <= lastfield_j)&(num_a_civ1 >= 1)&(num_a_civ2 >= 1));
3111    end
3112    if length(indsel)>=1
3113        firstind=indsel(1);
3114        lastind=indsel(end);
3115        set(handles.first_j,'String',num2str(ref_j(firstind)))%update the display of first and last fields
3116        set(handles.last_j,'String',num2str(ref_j(lastind)))
3117        ref_j=ref_j(indsel);
3118        num_a_civ1=num_a_civ1(indsel);
3119        num_b_civ1=num_b_civ1(indsel);
3120        num_a_civ2=num_a_civ2(indsel);
3121        num_b_civ2=num_b_civ2(indsel);
3122    end
3123elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3124    displ_num=get(handles.ListPairCiv1,'UserData');
3125    num1_civ1=ref_i;
3126    num2_civ1=ref_i;
3127    num_a_civ1=displ_num(1,index_civ1);
3128    num_b_civ1=displ_num(2,index_civ1);
3129    num1_civ2=ref_i;
3130    num2_civ2=ref_i;
3131    num_a_civ2=displ_num(1,index_civ2);
3132    num_b_civ2=displ_num(2,index_civ2);
3133elseif isequal(mode,'displacement')
3134    num1_civ1=ref_i;
3135    num2_civ1=ref_i;
3136    num_a_civ1=ref_j;
3137    num_b_civ1=ref_j;
3138    num1_civ2=ref_i;
3139    num2_civ2=ref_i;
3140    num_a_civ2=ref_j;
3141    num_b_civ2=ref_j;
3142end
3143
3144
3145%------------------------------------------------------------------------
3146% --- STEREO Interp
3147function cmd=RUN_STINTERP(stinterpBin,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3148%------------------------------------------------------------------------
3149namelog=[filename_nc(1:end-3) '_stinterp.log'];
3150cmd=[stinterpBin ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3151    ' -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
3152
3153%------------------------------------------------------------------------
3154% --- Executes on button press in CheckCiv1.
3155function CheckCiv1_Callback(hObject, eventdata, handles)
3156%------------------------------------------------------------------------
3157update_CivOptions(handles)
3158
3159%------------------------------------------------------------------------
3160% --- Executes on button press in CheckFix1.
3161function CheckFix1_Callback(hObject, eventdata, handles)
3162%------------------------------------------------------------------------
3163update_CivOptions(handles)
3164
3165%------------------------------------------------------------------------
3166% --- Executes on button press in CheckPatch1.
3167function CheckPatch1_Callback(hObject, eventdata, handles)
3168%------------------------------------------------------------------------
3169update_CivOptions(handles)
3170
3171%------------------------------------------------------------------------
3172% --- Executes on button press in CheckCiv2.
3173function CheckCiv2_Callback(hObject, eventdata, handles)
3174%------------------------------------------------------------------------
3175update_CivOptions(handles)
3176
3177%------------------------------------------------------------------------
3178% --- Executes on button press in CheckFix2.
3179function CheckFix2_Callback(hObject, eventdata, handles)
3180%------------------------------------------------------------------------
3181update_CivOptions(handles)
3182
3183%------------------------------------------------------------------------
3184% --- Executes on button press in CheckPatch2.
3185function CheckPatch2_Callback(hObject, eventdata, handles)
3186%------------------------------------------------------------------------
3187update_CivOptions(handles)
3188
3189%------------------------------------------------------------------------
3190% --- activated by any checkbox controling the selection of Civ1,Fix1,Patch1,Civ2,Fix2,Patch2
3191function update_CivOptions(handles)
3192%------------------------------------------------------------------------
3193checkbox=zeros(1,6);
3194checkbox(1)=get(handles.CheckCiv1,'Value');
3195checkbox(2)=get(handles.CheckFix1,'Value');
3196checkbox(3)=get(handles.CheckPatch1,'Value');
3197checkbox(4)=get(handles.CheckCiv2,'Value');
3198checkbox(5)=get(handles.CheckFix2,'Value');
3199checkbox(6)=get(handles.CheckPatch2,'Value');
3200ind_selected=find(checkbox);
3201if ~isempty(ind_selected)
3202  RootName=get(handles.RootName,'String');
3203    if isempty(RootName)
3204         msgbox_uvmat('ERROR','No input file')
3205        return
3206    end
3207end
3208set(handles.PairIndices,'Visible','on')
3209set(handles.txt_SubdirCiv1,'Visible','on')
3210set(handles.ListSubdirCiv1,'Visible','on')
3211find_netcpair_civ1(handles) % select the available netcdf files
3212% check_civ2pair=ind_selected(4;6);
3213if max(checkbox(4:6))% case of civ2 pair choice needed
3214    set(handles.TitlePairCiv2,'Visible','on')
3215    set(handles.TitleSubdirCiv2,'Visible','on')
3216    set(handles.txt_SubdirCiv2,'Visible','on')
3217    set(handles.ListSubdirCiv2,'Visible','on')
3218    set(handles.ListPairCiv2,'Visible','on')
3219    find_netcpair_civ2(handles) % select the available netcdf files
3220else
3221    set(handles.TitleSubdirCiv2,'Visible','off')
3222    set(handles.txt_SubdirCiv2,'Visible','off')
3223    set(handles.ListSubdirCiv2,'Visible','off')
3224    set(handles.ListPairCiv2,'Visible','off')
3225end
3226%index_last=find(checkbox,1,'last');%last selected button
3227options={'Civ1','Fix1','Patch1','Civ2','Fix2','Patch2'};
3228for ilist=1:length(options)
3229    if checkbox(ilist)
3230        set(handles.(options{ilist}),'Visible','on')
3231    else
3232        set(handles.(options{ilist}),'Visible','off')
3233    end
3234end
3235
3236% end
3237
3238% set(handles.(operations(ind_selected)),'Visible','on')
3239%
3240% if state
3241%     RootName=get(handles.RootName,'String');
3242%     if isempty(RootName)
3243%          msgbox_uvmat('ERROR','No input file')
3244%         return
3245%     end
3246%     set(handles.Civ1,'Visible','on')   
3247%     set(handles.PairIndices,'Visible','on')
3248% else
3249%     set(handles.Civ1,'Visible','off') 
3250% end
3251
3252
3253%------------------------------------------------------------------------
3254function first_i_Callback(hObject, eventdata, handles)
3255%------------------------------------------------------------------------
3256% last_i_Callback(hObject, eventdata, handles)
3257first_i=str2double(get(handles.first_i,'String'));
3258% last_i=str2num(get(handles.last_i,'String'));
3259% ref_i=ceil((first_i+last_i)/2);
3260set(handles.ref_i,'String', num2str(first_i))% reference index for pair dt = first index
3261%set(handles.ref_i_civ2,'String', num2str(first_i))% reference index for pair dt = first index
3262ref_i_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3263
3264%------------------------------------------------------------------------
3265function first_j_Callback(hObject, eventdata, handles)
3266%------------------------------------------------------------------------
3267first_j=str2num(get(handles.first_j,'String'));
3268set(handles.ref_j,'String', num2str(first_j))% reference index for pair dt = first index
3269ref_j_Callback(hObject, eventdata, handles)%refresh dispaly of dt for pairs (in case of non constant dt)
3270
3271%------------------------------------------------------------------------
3272% --- Executes on button press in SearchRange: determine the search range num_Searchx,num_Searchy
3273function SearchRange_Callback(hObject, eventdata, handles)
3274%------------------------------------------------------------------------
3275%determine pair numbers
3276if strcmp(get(handles.umin,'Visible'),'off')
3277    set(handles.u_title,'Visible','on')
3278    set(handles.v_title,'Visible','on')
3279    set(handles.umin,'Visible','on')
3280    set(handles.umax,'Visible','on')
3281    set(handles.vmin,'Visible','on')
3282    set(handles.vmax,'Visible','on')
3283    set(handles.CoordUnit,'Visible','on')
3284    set(handles.TimeUnit,'Visible','on')
3285    set(handles.slash_title,'Visible','on')
3286    set(handles.min_title,'Visible','on')
3287    set(handles.max_title,'Visible','on')
3288    set(handles.unit_title,'Visible','on')
3289else
3290    get_search_range(hObject, eventdata, handles)
3291end
3292
3293%------------------------------------------------------------------------
3294% ---  determine the search range num_Searchx,num_Searchy and shift
3295function get_search_range(hObject, eventdata, handles)
3296umin=str2double(get(handles.umin,'String'));
3297umax=str2double(get(handles.umax,'String'));
3298vmin=str2double(get(handles.umin,'String'));
3299vmax=str2double(get(handles.vmax,'String'));
3300%switch min_title and max_title in case of error
3301if umax<=umin
3302    umin_old=umin;
3303    umin=umax;
3304    umax=umin_old;
3305    set(handles.umin,'String', num2str(umin))
3306    set(handles.umax,'String', num2str(umax))
3307end
3308if vmax<=vmin
3309    vmin_old=vmin;
3310    vmin=vmax;
3311    vmax=vmin_old;
3312    set(handles.vmin,'String', num2str(vmin))
3313    set(handles.vmax,'String', num2str(vmax))
3314end   
3315if ~(isnan(umin)||isnan(umax)||isnan(vmin)||isnan(vmax))
3316    list_pair=get(handles.ListPairCiv1,'String');%get the menu of image pairs
3317    index=get(handles.ListPairCiv1,'Value');
3318    displ_num=get(handles.ListPairCiv1,'UserData');
3319    time=get(handles.ImaDoc,'UserData'); %get the set of times
3320    pxcm_xy=get(handles.SearchRange,'UserData');
3321    pxcmx=pxcm_xy(1);
3322    pxcmy=pxcm_xy(2);
3323    mode_list=get(handles.ListPairMode,'String');
3324    mode_value=get(handles.ListPairMode,'Value');
3325    mode=mode_list{mode_value};
3326    if isequal (mode, 'series(Di)' )
3327        ref_i=str2double(get(handles.ref_i,'String'));
3328        num1=ref_i-floor(index/2);%  first image numbers
3329        num2=ref_i+ceil(index/2);
3330        num_a=1;
3331        num_b=1;
3332    elseif isequal (mode, 'series(Dj)')
3333        num1=1;
3334        num2=1;
3335        ref_j=str2double(get(handles.ref_j,'String'));
3336        num_a=ref_j-floor(index/2);%  first image numbers
3337        num_b=ref_j+ceil(index/2);
3338    elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3339        ref_i=str2double(get(handles.ref_i,'String'));
3340        num1=ref_i;
3341        num2=ref_i;
3342        num_a=displ_num(1,index);
3343        num_b=displ_num(2,index);
3344    end
3345    dt=time(num2,num_b)-time(num1,num_a);
3346    ibx=str2double(get(handles.num_Bx,'String'));
3347    iby=str2double(get(handles.num_By,'String'));
3348    umin=dt*pxcmx*umin;
3349    umax=dt*pxcmx*umax;
3350    vmin=dt*pxcmy*vmin;
3351    vmax=dt*pxcmy*vmax;
3352    shiftx=round((umin+umax)/2);
3353    shifty=round((vmin+vmax)/2);
3354    isx=(umax+2-shiftx)*2+ibx;
3355    isx=2*ceil(isx/2)+1;
3356    isy=(vmax+2-shifty)*2+iby;
3357    isy=2*ceil(isy/2)+1;
3358    set(handles.num_Shiftx,'String',num2str(shiftx));
3359    set(handles.num_Shifty,'String',num2str(shifty));
3360    set(handles.num_Searchx,'String',num2str(isx));
3361    set(handles.num_Searchy,'String',num2str(isy));
3362end
3363
3364%------------------------------------------------------------------------
3365% --- Executes on carriage return on the subdir checkciv1 edit window
3366function txt_SubdirCiv1_Callback(hObject, eventdata, handles)
3367%------------------------------------------------------------------------
3368subdir=get(handles.txt_SubdirCiv1,'String');
3369set(handles.txt_SubdirCiv2,'String',subdir);% set civ2 directory the same as civ1 by default
3370menu_str=get(handles.ListSubdirCiv1,'String');% read the list of subdirectories for update
3371ichoice=find(strcmp(subdir,menu_str),1);
3372if isempty(ichoice)
3373    ilist=numel(menu_str); %select 'new...' in the menu
3374else
3375    ilist=ichoice;
3376end
3377set(handles.ListSubdirCiv1,'Value',ilist)% select the selected subdir in the menu
3378if get(handles.CheckCiv1,'Value')==0
3379    find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3380end
3381
3382%------------------------------------------------------------------------
3383% --- Executes on carriage return on the subdir checkciv1 edit window
3384function txt_SubdirCiv2_Callback(hObject, eventdata, handles)
3385%------------------------------------------------------------------------
3386subdir=get(handles.txt_SubdirCiv1,'String');
3387menu_str=get(handles.ListSubdirCiv2,'String');% read the list of subdirectories for update
3388ichoice=find(strcmp(subdir,menu_str),1);
3389if isempty(ichoice)
3390    ilist=numel(menu_str); %select 'new...' in the menu
3391else
3392    ilist=ichoice;
3393end
3394set(handles.ListSubdirCiv2,'Value',ilist)% select the selected subdir in the menu
3395%update the list of available pairs from netcdf files in the new directory
3396if ~get(handles.CheckCiv2,'Value') && ~get(handles.CheckCiv1,'Value') && ~get(handles.CheckFix1,'Value') && ~get(handles.CheckPatch1,'Value')
3397    find_netcpair_civ2(hObject, eventdata, handles);
3398end
3399
3400% %------------------------------------------------------------------------
3401% % --- Executes on button press in CheckMask: select box for mask option
3402% function check_Mask_Callback(hObject, eventdata, handles)
3403% %------------------------------------------------------------------------
3404% maskval=get(handles.CheckMask,'Value');
3405% if isequal(maskval,0)
3406%     set(handles.txt_MaskName,'Visible','off')
3407% %     set(handles.txt_MaskName,'String','')
3408% else
3409%     mask_displ='no mask'; %default
3410%     filebase=get(handles.RootName,'String');
3411%     [ nbslice_mask, flag_mask]=get_mask(filebase,handles);
3412%     if isequal(flag_mask,1)
3413%         mask_displ=[num2str(nbslice_mask) 'mask'];
3414%     elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3415%         common_path=fileparts(filebase);
3416%         filebase_a=fullfile(common_path,get(handles.RootName_1,'String'));
3417%         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3418%         if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice_mask)
3419%             mask_displ='no mask';
3420%         end
3421%     end
3422%     if isequal(mask_displ,'no mask')
3423%         [FileName, PathName, filterindex] = uigetfile( ...
3424%             {'*.png', ' (*.png)';
3425%             '*.png',  '.png files '; ...
3426%             '*.*', 'All Files (*.*)'}, ...
3427%             'Pick a mask file *.png',filebase);
3428%         mask_displ=fullfile(PathName,FileName);
3429%         if exist(mask_displ,'file')
3430%             set(handles.txt_MaskName,'Visible','on')
3431%             set(handles.txt_MaskName,'String',mask_displ)
3432% %             mask_displ='no mask';
3433%         end
3434%     end
3435%     if isequal(mask_displ,'no mask')
3436%         set(handles.CheckMask,'Value',0)
3437%         set(handles.CheckMask,'Value',0)
3438%         set(handles.CheckMask,'Value',0)
3439%         set(handles.CheckMask,'Value',0)
3440%     else
3441%         set(handles.CheckMask,'Value',1)
3442%         set(handles.CheckMask,'Value',1)
3443%     end
3444%     set(handles.txt_MaskName,'String',mask_displ)
3445%     set(handles.txt_MaskName,'String',mask_displ)
3446%     set(handles.txt_Mask,'String',mask_displ)
3447%     set(handles.txt_MaskName,'String',mask_displ)
3448% end
3449% set(handles.CheckMask,'Value',maskval)%update the checkciv2 mask with the same option as checkciv1
3450
3451%------------------------------------------------------------------------
3452% --- Executes on button press in CheckMask.
3453function get_mask_fix1_Callback(hObject, eventdata, handles)
3454%------------------------------------------------------------------------
3455maskval=get(handles.CheckMask,'Value');
3456if isequal(maskval,0)
3457    set(handles.txt_MaskName,'String','')
3458else
3459    mask_displ='no mask'; %default
3460    filebase=get(handles.RootName,'String');
3461    [nbslice, flag_mask]=get_mask(filebase,handles);
3462    if isequal(flag_mask,1)
3463        mask_displ=[num2str(nbslice) 'mask'];
3464    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3465        filebase_a=get(handles.RootName_1,'String');
3466        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3467        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3468            mask_displ='no mask';
3469        end
3470    end
3471    if isequal(mask_displ,'no mask')
3472        [FileName, PathName, filterindex] = uigetfile( ...
3473            {'*.png', ' (*.png)';
3474            '*.png',  '.png files '; ...
3475            '*.*', 'All Files (*.*)'}, ...
3476            'Pick a mask file *.png',filebase);
3477        mask_displ=fullfile(PathName,FileName);
3478        if ~exist(mask_displ,'file')
3479            mask_displ='no mask';
3480        end
3481    end
3482    if isequal(mask_displ,'no mask')
3483        set(handles.CheckMask,'Value',0)
3484        set(handles.CheckMask,'Value',0)
3485        set(handles.CheckMask,'Value',0)
3486    else
3487        %set(handles.CheckMask,'Value',1)
3488        set(handles.CheckMask,'Value',1)
3489    end
3490    set(handles.txt_MaskName,'String',mask_displ)
3491    set(handles.txt_Mask,'String',mask_displ)
3492    set(handles.txt_MaskName,'String',mask_displ)
3493end
3494
3495%------------------------------------------------------------------------
3496% --- Executes on button press in CheckMask: select box for mask option
3497function get_mask_civ2_Callback(hObject, eventdata, handles)
3498%------------------------------------------------------------------------
3499maskval=get(handles.CheckMask,'Value');
3500if isequal(maskval,0)
3501    set(handles.txt_Mask,'String','')
3502else
3503    mask_displ='no mask'; %default
3504    filebase=get(handles.RootName,'String');
3505    [nbslice, flag_mask]=get_mask(filebase,handles);
3506    if isequal(flag_mask,1)
3507        mask_displ=[num2str(nbslice) 'mask'];
3508    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3509        filebase_a=get(handles.RootName_1,'String');
3510        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3511        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3512            mask_displ='no mask';
3513        end
3514    end
3515    if isequal(mask_displ,'no mask')
3516        [FileName, PathName, filterindex] = uigetfile( ...
3517            {'*.png', ' (*.png)';
3518            '*.png',  '.png files '; ...
3519            '*.*', 'All Files (*.*)'}, ...
3520            'Pick a mask file *.png',filebase);
3521        mask_displ=fullfile(PathName,FileName);
3522        if ~exist(mask_displ,'file')
3523            mask_displ='no mask';
3524        end
3525    end
3526    if isequal(mask_displ,'no mask')
3527        set(handles.CheckMask,'Value',0)
3528        set(handles.CheckMask,'Value',0)
3529    else
3530        set(handles.CheckMask,'Value',1)
3531    end
3532    set(handles.txt_Mask,'String',mask_displ)
3533    set(handles.txt_MaskName,'String',mask_displ)
3534end
3535
3536%------------------------------------------------------------------------
3537% --- Executes on button press in CheckMask.
3538function get_mask_fix2_Callback(hObject, eventdata, handles)
3539%------------------------------------------------------------------------
3540maskval=get(handles.CheckMask,'Value');
3541if isequal(maskval,0)
3542    set(handles.txt_MaskName,'String','')
3543else
3544    mask_displ='no mask'; %default
3545    filebase=get(handles.RootName,'String');
3546    [nbslice, flag_mask]=get_mask(filebase,handles);
3547    if isequal(flag_mask,1)
3548        mask_displ=[num2str(nbslice) 'mask'];
3549    elseif get(handles.ListCompareMode,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3550        filebase_a=get(handles.RootName_1,'String');
3551        [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3552        if isequal(flag_mask_a,0) || ~isequal(nbslice_a,nbslice)
3553            mask_displ='no mask';
3554        end
3555    end
3556    if isequal(mask_displ,'no mask')
3557        [FileName, PathName, filterindex] = uigetfile( ...
3558            {'*.png', ' (*.png)';
3559            '*.png',  '.png files '; ...
3560            '*.*', 'All Files (*.*)'}, ...
3561            'Pick a mask file *.png',filebase);
3562        mask_displ=fullfile(PathName,FileName);
3563        if ~exist(mask_displ,'file')
3564            mask_displ='no mask';
3565        end
3566    end
3567    if isequal(mask_displ,'no mask')
3568        set(handles.CheckMask,'Value',0)
3569    end
3570    set(handles.txt_MaskName,'String',mask_displ)
3571end
3572
3573%------------------------------------------------------------------------
3574% --- function called to look for mask files
3575function [nbslice, flag_mask]=get_mask(filebase,handles)
3576%------------------------------------------------------------------------
3577%detect mask files, images with appropriate file base
3578%[filebase '_' xx 'mask'], xx=nbslice
3579%flag_mask=1 indicates detection
3580
3581flag_mask=0;%default
3582nbslice=1;
3583
3584% subdir=get(handles.txt_SubdirCiv1,'String');
3585[Path,Name]=fileparts(filebase);
3586if ~isdir(Path)
3587    msgbox_uvmat('ERROR','no path for input files')
3588    return
3589end
3590% currentdir=pwd;
3591% cd(Path);%move in the dir of the root name filebase
3592maskfiles=dir(fullfile(Path,[Name '_*mask_*.png']));%look for mask files
3593% cd(currentdir);%come back to the current working directory
3594if ~isempty(maskfiles)
3595    %     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3596    % else
3597    flag_mask=1;
3598    maskname=maskfiles(1).name;% take the first mask file in the list
3599    [Path2,Name,ext]=fileparts(maskname);
3600    Namedouble=double(Name);
3601    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3602    ind_mask=findstr('mask',Name);
3603    i=ind_mask-1;
3604    while val(i)==0 && i>0
3605        i=i-1;
3606    end
3607    nbslice=str2double(Name(i+1:ind_mask-1));
3608    if ~isnan(nbslice) && Name(i)=='_'
3609        flag_mask=1;
3610    else
3611        msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3612        return
3613        nbslice=1;
3614    end
3615end
3616
3617%------------------------------------------------------------------------
3618% --- function called to look for grid files
3619function [nbslice, flag_mask]=get_grid(filebase,handles)
3620%------------------------------------------------------------------------
3621flag_mask=0;%default
3622nbslice=1;
3623[Path,Name]=fileparts(filebase);
3624currentdir=pwd;
3625cd(Path);%move in the dir of the root name filebase
3626maskfiles=dir([Name '_*grid_*.grid']);%look for mask files
3627cd(currentdir);%come back to the current working directory
3628if ~isempty(maskfiles)
3629    flag_mask=1;
3630    maskname=maskfiles(1).name;% take the first mask file in the list
3631    [Path2,Name,ext]=fileparts(maskname);
3632    Namedouble=double(Name);
3633    val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3634    ind_mask=findstr('grid',Name);
3635    i=ind_mask-1;
3636    while val(i)==0 && i>0
3637        i=i-1;
3638    end
3639    nbslice=str2double(Name(i+1:ind_mask-1));
3640    if ~isnan(nbslice) && Name(i)=='_'
3641        flag_mask=1;
3642    else
3643        msgbox_uvmat('ERROR',['bad grid file ' Name ext ' found in ' Path2])
3644        return
3645        nbslice=1;
3646    end
3647end
3648
3649%------------------------------------------------------------------------
3650% --- transform numbers to letters
3651function str=num2stra(num,nom_type)
3652%------------------------------------------------------------------------
3653if isempty(nom_type)
3654    str='';
3655elseif strcmp(nom_type(end),'a')
3656    str=char(96+num);
3657elseif strcmp(nom_type(end),'A')
3658    str=char(96+num);
3659elseif isempty(nom_type(2:end))%a single index
3660    str='';
3661else
3662    str=num2str(num);
3663end
3664
3665%------------------------------------------------------------------------
3666% --- Executes on button press in ListSubdirCiv1.
3667function ListSubdirCiv1_Callback(hObject, eventdata, handles)
3668%------------------------------------------------------------------------
3669list_subdir_civ1=get(handles.ListSubdirCiv1,'String');
3670val=get(handles.ListSubdirCiv1,'Value');
3671subdir=list_subdir_civ1{val};
3672if strcmp(subdir,'new...')
3673    subdir='CIV'; %default subdirectory
3674end
3675set(handles.txt_SubdirCiv1,'String',subdir);
3676find_netcpair_civ1(hObject, eventdata, handles)
3677% end
3678
3679%------------------------------------------------------------------------
3680% --- Executes on button press in ListSubdirCiv2.
3681function ListSubdirCiv2_Callback(hObject, eventdata, handles)
3682%------------------------------------------------------------------------
3683list_subdir_civ2=get(handles.ListSubdirCiv2,'String');
3684val=get(handles.ListSubdirCiv2,'Value');
3685subdir=list_subdir_civ2{val};
3686if strcmp(subdir,'new...')
3687    subdir='CIV'; %default subdirectory
3688end
3689set(handles.txt_SubdirCiv2,'String',subdir);
3690%     set(handles.ListSubdirCiv2,'Value',1);
3691
3692
3693%------------------------------------------------------------------------
3694% --- Executes on button press in CheckGrid.
3695function CheckGrid_Callback(hObject, eventdata, handles)
3696%------------------------------------------------------------------------
3697value=get(hObject,'Value');
3698hparent=get(hObject,'parent');
3699hchildren=get(hparent,'children');
3700handle_txtbox=findobj(hchildren,'tag','txt_GridName');
3701handle_dx=findobj(hchildren,'tag','num_Dx');
3702handle_dy=findobj(hchildren,'tag','num_Dy');
3703testgrid=0;
3704filegrid='';
3705if value
3706    filebase=get(handles.RootName,'String');
3707    [nbslice, flag_grid]=get_grid(filebase,handles);% look for a grid with appropriate name
3708    if isequal(flag_grid,1)
3709        filegrid=[num2str(nbslice) 'grid'];
3710        testgrid=1;
3711    else % browse for a grid
3712        filegrid=get(hObject,'UserData');%look for previous grid name stored as UserData
3713        if exist(filegrid,'file')
3714            filebase=filegrid;
3715        end
3716        [FileName, PathName, filterindex] = uigetfile( ...
3717            {'*.grid', ' (*.grid)';
3718            '*.grid',  '.grid files '; ...
3719            '*.*', 'All Files (*.*)'}, ...
3720            'Pick a file',filebase);
3721        filegrid=fullfile(PathName,FileName);
3722        set(hObject,'UserData',filegrid);%store for future use
3723        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file'))
3724            testgrid=1;
3725        end
3726    end
3727end
3728if testgrid
3729    set(handle_dx,'Visible','off');
3730    set(handle_dy,'Visible','off');
3731    set(handle_txtbox,'Visible','on')
3732    set(handle_txtbox,'String',filegrid)
3733else
3734    set(hObject,'Value',0);
3735    set(handle_dx,'Visible','on');
3736    set(handle_dy,'Visible','on');
3737    set(handle_txtbox,'Visible','off')
3738end
3739
3740%% if hObject is on the checkciv1 frame, duplicate action for checkciv2 frame
3741PanelName=get(hparent,'tag');
3742if strcmp(PanelName,'panel_Civ1')
3743    hchildren=get(handles.Civ2,'children');
3744    handle_checkbox=findobj(hchildren,'tag','check_Grid');
3745    handle_txtbox=findobj(hchildren,'tag','txt_GridName');
3746    handle_dx=findobj(hchildren,'tag','num_Dx');
3747    handle_dy=findobj(hchildren,'tag','num_Dy');
3748    set(handle_checkbox,'UserData',filegrid);%store for future use
3749    if testgrid
3750        set(handle_checkbox,'Value',1);
3751        set(handle_dx,'Visible','off');
3752        set(handle_dy,'Visible','off');
3753        set(handle_txtbox,'Visible','on')
3754        set(handle_txtbox,'String',filegrid)
3755    else
3756        set(handle_checkbox,'Value',0);
3757        set(handles.CheckGrid,'Value',0);
3758        set(handle_dx,'Visible','on');
3759        set(handle_dy,'Visible','on');
3760        set(handle_txtbox,'Visible','off')
3761    end
3762end
3763%------------------------------------------------------------------------
3764% --- Executes on button press in CheckMask.
3765function CheckMask_Callback(hObject, eventdata, handles)
3766%------------------------------------------------------------------------
3767value=get(hObject,'Value');
3768testmask=0;
3769if value
3770    filebase=get(handles.RootName,'String');
3771    [nbslice, flag_mask]=get_mask(filebase,handles);% look for a mask with appropriate name
3772    if isequal(flag_mask,1)
3773        filemask=[num2str(nbslice) 'mask'];
3774        testmask=1;
3775    else % browse for a mask
3776        filemask=get(handles.CheckMask,'UserData');%look for previous mask name stored as UserData
3777        if exist(filemask,'file')
3778            filebase=filemask;
3779        end
3780        [FileName, PathName, filterindex] = uigetfile( ...
3781            {'*.png', ' (*.png)';
3782            '*.png',  '.png files '; ...
3783            '*.*', 'All Files (*.*)'}, ...
3784            'Pick a mask file *.png',filebase);
3785        filemask=fullfile(PathName,FileName);
3786        set(handles.CheckMask,'UserData',filemask);%store for future use
3787        if ~(isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filemask,'file'))
3788            testmask=1;
3789        end
3790    end
3791end
3792if testmask
3793    set(handles.num_Dx,'Visible','off');
3794    set(handles.num_Dy,'Visible','off');
3795    set(handles.num_Dx,'Visible','off');
3796    set(handles.num_Dy,'Visible','off');
3797    set(handles.txt_MaskName,'Visible','on')
3798    set(handles.txt_MaskName,'String',filemask)
3799else
3800    set(handles.CheckMask,'Value',0);
3801    set(handles.CheckGrid,'Value',0);
3802    set(handles.num_Dx,'Visible','on');
3803    set(handles.num_Dy,'Visible','on');
3804    set(handles.num_Dx,'Visible','on');
3805    set(handles.num_Dy,'Visible','on');
3806    set(handles.txt_MaskName,'Visible','off')
3807end
3808
3809%------------------------------------------------------------------------
3810% --- Executes on button press in CheckGrid.
3811function browse_gridciv2_Callback(hObject, eventdata, handles)
3812%------------------------------------------------------------------------
3813value=get(handles.CheckGrid,'Value');
3814if value
3815    filebase=get(handles.RootName,'String');
3816    [nbslice, flag_grid]=get_grid(filebase,handles);
3817    if isequal(flag_grid,1)
3818        mask_displ=[num2str(nbslice) 'grid'];
3819        set(handles.txt_GridName,'String',mask_displ)
3820        set(handles.num_Dx,'Visible','off');
3821        set(handles.num_Dy,'Visible','off');
3822    else
3823        [FileName, PathName, filterindex] = uigetfile( ...
3824            {'*.grid', ' (*.grid)';
3825            '*.grid',  '.grid files '; ...
3826            '*.*', 'All Files (*.*)'}, ...
3827            'Pick a file',filebase);
3828        filegrid=fullfile(PathName,FileName);
3829        if isempty(FileName)||isempty(PathName)||isequal(FileName,0)||~exist(filegrid,'file')
3830            set(handles.CheckGrid,'Value',0);
3831            set(handles.txt_GridName,'string','');
3832            set(handles.num_Dx,'Visible','on');
3833            set(handles.num_Dy,'Visible','on');
3834            set(handles.txt_GridName,'string','');
3835        else
3836            set(handles.txt_GridName,'string',filegrid);
3837            set(handles.num_Dx,'Visible','off');
3838            set(handles.num_Dy,'Visible','off');
3839            set(handles.txt_GridName,'string',filegrid);
3840        end
3841    end
3842else
3843    set(handles.txt_GridName,'string','');
3844    set(handles.num_Dx,'Visible','on');
3845    set(handles.num_Dy,'Visible','on');
3846    set(handles.txt_GridName,'string','');
3847end
3848
3849% % --- Executes on button press in CheckGrid.
3850% function browse_gridciv2_Callback(hObject, eventdata, handles)
3851%
3852% filebase=get(handles.RootName,'String');
3853% [FileName, PathName, filterindex] = uigetfile( ...
3854%        {'*.grid', ' (*.grid)';
3855%         '*.grid',  '.grid files '; ...
3856%         '*.*', 'All Files (*.*)'}, ...
3857%         'Pick a file',filebase);
3858% filegrid=fullfile(PathName,FileName);
3859% set(handles.txt_GridName,'string',filegrid);
3860% set(handles.num_Dx,'String',' ');
3861% set(handles.num_Dy,'String',' ');
3862% % set(handles.grid_patch2,'string',filegrid);
3863
3864% --- Executes on button press in get_gridpatch1.
3865function get_gridpatch1_Callback(hObject, eventdata, handles)
3866% hObject    handle to get_gridpatch1 (see GCBO)
3867% eventdata  reserved - to be defined in a future version of MATLAB
3868% handles    structure with handles and user data (see GUIDATA)
3869
3870filebase=get(handles.RootName,'String');
3871[FileName, PathName, filterindex] = uigetfile( ...
3872    {'*.grid', ' (*.grid)';
3873    '*.grid',  '.grid files '; ...
3874    '*.*', 'All Files (*.*)'}, ...
3875    'Pick a file',filebase);
3876filegrid=fullfile(PathName,FileName);
3877set(handles.grid_patch1,'string',filegrid);
3878% set(handles.grid_patch2,'string',filegrid
3879
3880%------------------------------------------------------------------------
3881% --- Executes on button press in get_gridpatch2.
3882function get_gridpatch2_Callback(hObject, eventdata, handles)
3883%------------------------------------------------------------------------
3884
3885%------------------------------------------------------------------------
3886function enable_civ1(handles,state)
3887%------------------------------------------------------------------------
3888if state
3889    RootName=get(handles.RootName,'String');
3890    if isempty(RootName)
3891         msgbox_uvmat('ERROR','No input file')
3892        return
3893    end
3894    set(handles.Civ1,'Visible','on')   
3895    set(handles.PairIndices,'Visible','on')
3896%     set(handles.frame_civ1,'BackgroundColor',[1 1 0])
3897%     set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
3898%     set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
3899else
3900    set(handles.Civ1,'Visible','off') 
3901%     set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
3902%     set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
3903%     set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
3904end
3905
3906% set(handles.num_Bx,'Visible',state)
3907% set(handles.num_By,'Visible',state)
3908% set(handles.num_Searchx,'Visible',state)
3909% set(handles.num_Searchy,'Visible',state)
3910% set(handles.num_Shiftx,'Visible',state)
3911% set(handles.num_Shifty,'Visible',state)
3912% set(handles.num_Rho,'Visible',state)
3913% set(handles.num_Dx,'Visible',state)
3914% set(handles.num_Dy,'Visible',state)
3915% set(handles.SearchRange,'Visible',state)
3916% set(handles.u_title,'Visible',state)
3917% set(handles.v_title,'Visible',state)
3918% set(handles.min_title,'Visible',state)
3919% set(handles.max_title,'Visible',state)
3920% set(handles.umin,'Visible',state)
3921% set(handles.umax,'Visible',state)
3922% set(handles.vmin,'Visible',state)
3923% set(handles.vmax,'Visible',state)
3924% set(handles.txt_GridName,'Visible',state)
3925% set(handles.txt_MaskName,'Visible',state)
3926% set(handles.CheckGrid,'Visible',state)
3927% set(handles.CheckMask,'Visible',state)
3928% set(handles.parameters,'Visible',state)
3929% set(handles.grid,'Visible',state)
3930% set(handles.num_Dx,'Visible',state)
3931% set(handles.num_Dy,'Visible',state)
3932% set(handles.check_Threshold,'Visible',state)
3933% if isequal(state,'off')
3934%     set(handles.num_MinIma,'Visible','off')
3935%     set(handles.num_MaxIma,'Visible','off')
3936%     set(handles.check_Threshold,'Value',0)
3937% end
3938% set(handles.dx_civ1_title,'Visible',state)
3939% set(handles.dy_civ1_title,'Visible',state)
3940% set(handles.ImaThreshold_title,'Visible',state)
3941% set(handles.ib_title,'Visible',state)
3942% set(handles.is_title,'Visible',state)
3943% set(handles.shift_title,'Visible',state)
3944% set(handles.rho_title,'Visible',state)
3945% set(handles.TestCiv1,'Visible',state)
3946%set(handles.ListPairMode,'Visible',state)
3947
3948%------------------------------------------------------------------------
3949function enable_fix1(handles,state)
3950%------------------------------------------------------------------------
3951
3952if isequal(state,0)
3953    state='off';
3954end
3955if isequal(state,1)
3956    state='on';
3957    RootName=get(handles.RootName,'String');
3958    if isempty(RootName)
3959         msgbox_uvmat('ERROR','No input file')
3960        return
3961    end
3962end
3963if isequal(state,'on')
3964    set(handles.Fix1,'Visible','on')
3965%     set(handles.frame_fix1,'BackgroundColor',[1 1 0])
3966else
3967    set(handles.Fix1,'Visible','off')
3968    %set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
3969end
3970% % set(handles.REMOVE,'Visible',state)
3971% set(handles.CheckFmin2,'Visible',state)
3972% set(handles.CheckF2,'Visible',state)
3973% set(handles.CheckF3,'Visible',state)
3974% set(handles.num_MinCorr,'Visible',state)
3975% set(handles.thresh_vecC_title,'Visible',state)
3976% set(handles.num_MinVel,'Visible',state)
3977% set(handles.TitleMinVel,'Visible',state)
3978% set(handles.txt_MaskName,'Visible',state)
3979% set(handles.CheckMask,'Visible',state)
3980% set(handles.get_ref_fix1,'Visible',state)
3981% set(handles.ref_fix1,'Visible',state)
3982% set(handles.num_MinVel,'Visible',state)
3983% set(handles.field_ref1,'Visible',state)
3984
3985%------------------------------------------------------------------------
3986function enable_patch1(handles,state)
3987%------------------------------------------------------------------------
3988if state
3989    RootName=get(handles.RootName,'String');
3990    if isempty(RootName)
3991        msgbox_uvmat('ERROR','No input file')
3992        return
3993    end
3994    set(handles.Patch1,'Visible','on')
3995else
3996    set(handles.Patch1,'Visible','off')
3997end
3998return
3999set(handles.frame_patch1,'BackgroundColor',[1 1 0])
4000set(handles.rho_patch1,'Visible','on')
4001set(handles.rho_text1,'Visible','on')
4002if get(handles.ListPairMode,'Value')==2
4003    set(handles.thresh_patch1,'Visible','on')
4004    set(handles.thresh_text1,'Visible','on')
4005end
4006set(handles.subdomain_patch1,'Visible','on')
4007set(handles.subdomain_text1,'Visible','on')
4008set(handles.nx_patch1,'Visible','on')
4009set(handles.num_Ny,'Visible','on')
4010set(handles.nx_patch1_title,'Visible','on')
4011set(handles.ny_patch1_title,'Visible','on')
4012% if ~isempty(patch_newBin)
4013set(handles.test_interp,'Visible','off');
4014stereo_test=get(handles.ListCompareMode,'Value');
4015if stereo_test==3
4016    set(handles.test_stereo1,'Visible','on')
4017end
4018% end
4019%set(handles.get_gridpatch1,'Visible','on')
4020%set(handles.grid_patch1,'string','none');
4021%set(handles.grid_patch1,'Visible','on')
4022
4023% %------------------------------------------------------------------------
4024% function desable_patch1(handles)
4025% %------------------------------------------------------------------------
4026% return
4027% set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
4028% set(handles.rho_patch1,'Visible','off')
4029% set(handles.rho_text1,'Visible','off')
4030% set(handles.thresh_patch1,'Visible','off')
4031% set(handles.thresh_text1,'Visible','off')
4032% set(handles.subdomain_patch1,'Visible','off')
4033% set(handles.subdomain_text1,'Visible','off')
4034% set(handles.nx_patch1,'Visible','off')
4035% set(handles.num_Ny,'Visible','off')
4036% set(handles.nx_patch1_title,'Visible','off')
4037% set(handles.ny_patch1_title,'Visible','off')
4038% set(handles.test_stereo1,'Visible','off')
4039% %set(handles.test_interp,'Visible','off')
4040% %set(handles.get_gridpatch1,'Visible','off')
4041% %set(handles.grid_patch1,'Visible','off')
4042
4043%------------------------------------------------------------------------
4044function enable_civ2(handles,state)
4045%------------------------------------------------------------------------
4046if state
4047    RootName=get(handles.RootName,'String');
4048    if isempty(RootName)
4049         msgbox_uvmat('ERROR','No input file')
4050        return
4051    end
4052    set(handles.Civ2,'Visible','on')
4053    set(handles.ListSubdirCiv2,'Visible','on')
4054    set(handles.SubdirCiv2,'Visible','on')
4055    set(handles.TitleSubdirCiv2,'Visible','on')
4056else
4057    set(handles.Civ2,'Visible','off')
4058end
4059
4060%
4061% set(handles.num_Bx,'Visible',state)
4062% set(handles.num_By,'Visible',state)
4063% set(handles.CheckDecimal,'Visible',state)
4064% set(handles.CheckDeformation,'Visible',state)
4065% set(handles.num_Rho,'Visible',state)
4066% set(handles.num_Dx,'Visible',state)
4067% set(handles.num_Dy,'Visible',state)
4068% set(handles.CheckGrid,'Visible',state)
4069% set(handles.CheckMask,'Visible',state)
4070% set(handles.parameters,'Visible',state)
4071% set(handles.grid,'Visible',state)
4072% set(handles.parameters_text,'Visible',state)
4073% set(handles.grid_text,'Visible',state)
4074% set(handles.txt_GridName,'Visible',state)
4075% set(handles.txt_Mask,'Visible',state)
4076% set(handles.dx_civ2_title,'Visible',state)
4077% set(handles.dy_civ2_title,'Visible',state)
4078% set(handles.ibx_civ2_text,'Visible',state)
4079% set(handles.rho_civ2_title,'Visible',state)
4080% set(handles.ImaThreshold2,'Visible',state)
4081% set(handles.ImaThreshold_title2,'Visible',state)
4082% if isequal(state,'off')
4083%     set(handles.num_MinIma,'Visible','off')
4084%     set(handles.num_MaxIma,'Visible','off')
4085%     set(handles.ImaThreshold2,'Value',0)
4086%     if isequal(get(handles.CheckFix2,'Value'),0) & isequal(get(handles.CheckPatch2,'Value'),0)
4087%         set(handles.ListPairCiv2,'Visible','off')
4088%         set(handles.txt_SubdirCiv2,'Visible','off')
4089%         set(handles.TitleSubdirCiv2,'Visible','off')
4090%         set(handles.dt_unit_civ2,'Visible','off')
4091%         %set(handles.ref_i_civ2,'Visible','off')
4092%         set(handles.i_ref_civ2_title,'Visible','off')
4093%         set(handles.j_ref_civ2_title,'Visible','off')
4094%         set(handles.ref_j_civ2,'Visible','off')
4095%     end
4096% else
4097%     set(handles.ListPairCiv2,'Visible','on')
4098%     set(handles.txt_SubdirCiv2,'Visible','on')
4099%     set(handles.TitleSubdirCiv2,'Visible','on')
4100%     set(handles.dt_unit_civ2,'Visible','on')
4101%   %  set(handles.ref_i_civ2,'Visible','on')
4102%     set(handles.i_ref_civ2_title,'Visible','on')
4103%     set(handles.j_ref_civ2_title,'Visible','on')
4104%     set(handles.ref_j_civ2,'Visible','on')
4105% end
4106% set(handles.rho_civ2_title,'Visible',state)
4107
4108%------------------------------------------------------------------------
4109function enable_fix2(handles,state)
4110%------------------------------------------------------------------------
4111% if isequal(state,'on')
4112%     set(handles.Fix2,'Visible','on') 
4113%     
4114% %     set(handles.frame_civ2,'BackgroundColor',[1 1 0])
4115% %     set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
4116% %     set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
4117% %     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
4118% else
4119if state
4120    RootName=get(handles.RootName,'String');
4121    if isempty(RootName)
4122         msgbox_uvmat('ERROR','No input file')
4123        return
4124    end
4125    set(handles.Fix2,'Visible','on')
4126else
4127    set(handles.Fix2,'Visible','off')
4128end
4129
4130% set(handles.frame_fix2,'BackgroundColor',[1 1 0])
4131% set(handles.REMOVE2,'Visible','on')
4132% set(handles.CheckFmin2,'Visible','on')
4133% set(handles.CheckF4,'Visible','on')
4134% set(handles.CheckF3,'Visible','on')
4135% set(handles.num_MinCorr,'Visible','on')
4136% set(handles.thresh_vec2C_text,'Visible','on')
4137% set(handles.num_MinVel,'Visible','on')
4138% set(handles.TitleMinVel,'Visible','on')
4139% set(handles.txt_MaskName,'Visible','on')
4140% set(handles.CheckMask,'Visible','on')
4141% set(handles.ListPairCiv2,'Visible','on')
4142% set(handles.txt_SubdirCiv2,'Visible','on')
4143% set(handles.TitleSubdirCiv2,'Visible','on')
4144% set(handles.get_ref_fix2,'Visible','on')
4145% set(handles.ref_fix2,'Visible','on')
4146% set(handles.num_MinVel,'Visible','on')
4147% set(handles.field_ref2,'Visible','on')
4148
4149% %------------------------------------------------------------------------
4150% function desable_fix2(handles)
4151% %------------------------------------------------------------------------
4152% set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
4153% set(handles.REMOVE2,'Visible','off')
4154% set(handles.CheckFmin2,'Visible','off')
4155% set(handles.CheckF4,'Visible','off')
4156% set(handles.CheckF3,'Visible','off')
4157% set(handles.num_MinCorr,'Visible','off')
4158% set(handles.thresh_vec2C_text,'Visible','off')
4159% set(handles.num_MinVel,'Visible','off')
4160% set(handles.TitleMinVel,'Visible','off')
4161% set(handles.txt_MaskName,'Visible','off')
4162% set(handles.CheckMask,'Visible','off')
4163% set(handles.get_ref_fix2,'Visible','off')
4164% set(handles.ref_fix2,'Visible','off')
4165% set(handles.num_MinVel,'Visible','off')
4166% set(handles.field_ref2,'Visible','off')
4167% if isequal(get(handles.CheckCiv2,'Value'),0) & isequal(get(handles.CheckPatch2,'Value'),0)
4168%     set(handles.ListPairCiv2,'Visible','off')
4169%     set(handles.txt_SubdirCiv2,'Visible','off')
4170%     set(handles.TitleSubdirCiv2,'Visible','off')
4171% end
4172
4173%------------------------------------------------------------------------
4174function enable_patch2(handles,state)
4175%------------------------------------------------------------------------
4176if state
4177RootName=get(handles.RootName,'String');
4178    if isempty(RootName)
4179         msgbox_uvmat('ERROR','No input file')
4180        return
4181    end
4182    set(handles.Patch2,'Visible','on')
4183else
4184set(handles.Patch2,'Visible','off')
4185end
4186% set(handles.frame_patch2,'BackgroundColor',[1 1 0])
4187% set(handles.num_SmoothParam,'Visible','on')
4188% set(handles.num_SmoothingParam,'Visible','on')
4189% set(handles.num_MaxDiff,'Visible','on')
4190% set(handles.thresh_text2,'Visible','on')
4191% set(handles.num_SubdomainSize,'Visible','on')
4192% set(handles.subdomain_text2,'Visible','on')
4193% set(handles.num_Nx,'Visible','on')
4194% set(handles.num_Ny,'Visible','on')
4195% set(handles.nx_patch2_title,'Visible','on')
4196% set(handles.ny_patch2_title,'Visible','on')
4197% % set(handles.get_gridpatch2,'Visible','on')
4198% % set(handles.grid_patch2,'Visible','on')
4199% set(handles.ListPairCiv2,'Visible','on')
4200% set(handles.txt_SubdirCiv2,'Visible','on')
4201% set(handles.TitleSubdirCiv2,'Visible','on')
4202% stereo_test=get(handles.ListCompareMode,'Value');
4203% if stereo_test==3
4204%     set(handles.CheckStereo,'Visible','on')
4205% end
4206
4207% %------------------------------------------------------------------------
4208% function desable_patch2(handles)
4209% %------------------------------------------------------------------------
4210% %set(handles.checkpatch2,'Visible','off')
4211% return
4212% set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
4213% set(handles.num_SmoothParam,'Visible','off')
4214% set(handles.num_SmoothingParam,'Visible','off')
4215% set(handles.num_MaxDiff,'Visible','off')
4216% set(handles.thresh_text2,'Visible','off')
4217% set(handles.num_SubdomainSize,'Visible','off')
4218% set(handles.subdomain_text2,'Visible','off')
4219% set(handles.num_Nx,'Visible','off')
4220% set(handles.num_Ny,'Visible','off')
4221% set(handles.nx_patch2_title,'Visible','off')
4222% set(handles.ny_patch2_title,'Visible','off')
4223% % set(handles.get_gridpatch2,'Visible','off')
4224% % set(handles.grid_patch2,'Visible','off')
4225% if isequal(get(handles.CheckCiv2,'Value'),0) & isequal(get(handles.CheckFix2,'Value'),0)
4226%     set(handles.ListPairCiv2,'Visible','off')
4227%     set(handles.txt_SubdirCiv2,'Visible','off')
4228%     set(handles.TitleSubdirCiv2,'Visible','off')
4229% end
4230% set(handles.CheckStereo,'Visible','off')
4231%------------------------------------------------------------------------
4232function enable_pair1(handles,state)
4233%------------------------------------------------------------------------
4234set(handles.txt_SubdirCiv1,'Visible',state)
4235set(handles.ListSubdirCiv1,'Visible',state)
4236set(handles.TitleSubdirCiv1,'Visible',state)
4237%set(handles.frame_subdirciv1,'Visible',state)
4238set(handles.ListPairCiv1,'Visible',state)
4239set(handles.TitlePairCiv1,'Visible',state)
4240%set(handles.dt_unit,'Visible',state)
4241%set(handles.PAIR_frame,'Visible',state)
4242
4243%------------------------------------------------------------------------
4244% --- Read the parameters for checkciv1 on the interface
4245% function par=read_param_civ1(handles,filecell)
4246% % not deeden anymore
4247% %------------------------------------------------------------------------
4248% ibx_val=str2double(get(handles.num_Bx,'String'));
4249% par.ibx=num2str(ibx_val);
4250% iby_val=str2double(get(handles.num_By,'String'));
4251% par.iby=num2str(iby_val);
4252% isx=get(handles.num_Searchx,'String');
4253% if isnan(str2double(isx)), isx='41'; set(handles.num_Searchx,'String','41'), end; %default
4254% if str2double(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.num_Searchx,'String',num2str(ibx_val+8)); end
4255% isy=get(handles.num_Searchy,'String');
4256% if isnan(str2double(isy)), isy='41'; set(handles.num_Searchy,'String','41'), end;%default
4257% if str2double(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.num_Searchy,'String',num2str(iby_val+8)); end
4258% par.isx=get(handles.num_Searchx,'String');
4259% par.isy=get(handles.num_Searchy,'String');
4260% par.shiftx=get(handles.num_Shiftx,'String');
4261% par.shifty=get(handles.num_Shifty,'String');
4262% if isnan(str2double(par.isx))
4263%     par.isx='41';%default
4264%     set(handles.num_Searchx,'String','41');
4265% end
4266% if isnan(str2double(par.isy))
4267%     par.isy='41'; %default
4268%     set(handles.num_Searchy,'String','41');
4269% end
4270% if isnan(str2double(par.shiftx))
4271%     par.shiftx='0';%default
4272%     set(handles.num_Shiftx,'String','0');
4273% end
4274% if isnan(str2double(par.shifty))
4275%     par.shifty='0'; %default
4276%     set(handles.num_Shifty,'String','0');
4277% end
4278% par.rho=get(handles.num_Rho,'String');
4279% if isequal(get(handles.num_Rho,'Style'),'popupmenu')
4280%     index=get(handles.num_Rho,'Value');
4281%     par.rho=par.rho{index};
4282% end
4283% dummy=get(handles.num_Dx,'String');
4284% par.dx=dummy{1};
4285% dummy=get(handles.num_Dy,'String');
4286% par.dy=dummy{1};
4287% if isnan(str2double(par.dx))
4288%     if isempty(get(handles.txt_GridName,'String'));
4289%         par.dx='0'; %just read by civ program, not used
4290%     else
4291%         par.dx='20';%default
4292%         set(handles.num_Dx,'String','20');
4293%     end
4294% end
4295% if isnan(str2double(par.dy))
4296%     if isempty(get(handles.txt_GridName,'String'));
4297%         par.dy='0';%just read by civ program, not used
4298%     else
4299%         par.dy='20';%default
4300%         set(handles.dy_civ1_title,'String','20');
4301%     end
4302% end
4303% par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4304% par.pxcmy='1';
4305% % if exist('file_ima','var')
4306% A=imread(filecell.ima1.civ1{1,1});%read the first image to get the size
4307% sizim=size(A);
4308% par.npx=num2str(sizim(2));
4309% par.npy=num2str(sizim(1));
4310% %TODO : civ should not need npx and npy
4311%
4312%
4313% % end
4314% %time=get(handles.RootName,'UserData'); %get the set of times
4315% dummy=get(handles.txt_GridName,'String');
4316% par.gridname=dummy{1};
4317% par.gridflag='y';
4318% if strcmp(par.gridname,'')|| isempty(par.gridname)
4319%     par.gridname='nogrid';
4320%     par.gridflag='n';
4321% end
4322%
4323 
4324           
4325%------------------------------------------------------------------------
4326function par=read_param_civ2(handles,file_ima)
4327%------------------------------------------------------------------------
4328par.ibx=get(handles.num_Bx,'String');
4329par.iby=get(handles.num_By,'String');
4330par.rho=get(handles.num_Rho,'String');
4331par.decimal=int2str(get(handles.CheckDecimal,'Value'));
4332par.deformation=int2str(get(handles.CheckDeformation,'Value'));
4333par.dx=get(handles.num_Dx,'String');
4334par.dy=get(handles.num_Dy,'String');
4335if isnan(str2double(par.dx))
4336    if isempty(get(handles.txt_GridName,'String'));
4337        par.dx='0'; %just read by civ program, not used
4338    else
4339        par.dx='20';%default
4340        set(handles.num_Dx,'String','20');
4341    end
4342end
4343if isnan(str2double(par.dy))
4344    if isempty(get(handles.txt_GridName,'String'));
4345        par.dy='0';%just read by civ program, not used
4346    else
4347        par.dy='20';%default
4348        set(handles.num_Dy,'String','20');
4349    end
4350end
4351par.pxcmx='1';
4352par.pxcmy='1';
4353A=imread(file_ima);%read the first image to get the size
4354sizim=size(A);
4355par.npx=num2str(sizim(2));
4356par.npy=num2str(sizim(1));
4357%time=get(handles.RootName,'UserData'); %get the set of times
4358par.gridname=get(handles.txt_GridName,'String');
4359par.gridflag='y';
4360if strcmp(par.gridname,'')|| isempty(par.gridname)
4361    par.gridname='nogrid';
4362    par.gridflag='n';
4363end
4364
4365function cmd_CIV1=CIV1_CMD(filename,param)
4366%TODO : include filename in par_civ1
4367%------------------------------------------------------------------------
4368%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4369
4370%changes : filename_cmx -> filename ( no extension )
4371
4372filename=regexprep(filename,'.nc','');
4373
4374if isequal(Param.Civ1.Dt,'0')
4375    Param.Civ1.Dt='1' ;%case of 'displacement' mode
4376end
4377Param.Civ1.filename_ima_a=regexprep(Param.Civ1.filename_ima_a,'.png','');
4378Param.Civ1.filename_ima_b=regexprep(Param.Civ1.filename_ima_b,'.png','');
4379fid=fopen([filename '.civ1.cmx'],'w');
4380fprintf(fid,['##############   CMX file' '\n' ]);
4381fprintf(fid,   ['FirstImage ' regexprep(Param.Civ1.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4382fprintf(fid,   ['LastImage  ' regexprep(Param.Civ1.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4383fprintf(fid,  ['XX' '\n' ]);
4384fprintf(fid,  ['Mask ' Param.Civ1.maskflag '\n' ]);
4385fprintf(fid,  ['MaskName ' regexprep(Param.Civ1.maskname,'\\','\\\\') '\n' ]);
4386fprintf(fid,   ['ImageSize ' Param.Civ1.npx ' ' Param.Civ1.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4387fprintf(fid,   ['CorrelationBoxesSize ' Param.Civ1.ibx ' ' Param.Civ1.iby '\n' ]);
4388fprintf(fid,   ['SearchBoxeSize ' Param.Civ1.isx ' ' Param.Civ1.isy '\n' ]);
4389fprintf(fid,   ['RO ' Param.Civ1.rho '\n' ]);
4390fprintf(fid,   ['GridSpacing ' Param.Civ1.dx ' ' Param.Civ1.dy '\n' ]);
4391fprintf(fid,   ['XX 1.0' '\n' ]);
4392fprintf(fid,   ['Dt_TO ' Param.Civ1.Dt ' ' Param.Civ1.T0 '\n' ]);
4393fprintf(fid,  ['PixCmXY ' Param.Civ1.pxcmx ' ' Param.Civ1.pxcmy '\n' ]);
4394fprintf(fid,  ['XX 1' '\n' ]);
4395fprintf(fid,   ['ShiftXY ' Param.Civ1.shiftx ' '  Param.Civ1.shifty '\n' ]);
4396fprintf(fid,  ['Grid ' Param.Civ1.gridflag '\n' ]);
4397fprintf(fid,   ['GridName ' regexprep(Param.Civ1.gridname,'\\','\\\\') '\n' ]);
4398fprintf(fid,   ['XX 85' '\n' ]);
4399fprintf(fid,   ['XX 1.0' '\n' ]);
4400fprintf(fid,   ['XX 1.0' '\n' ]);
4401fprintf(fid,   ['Hart 1' '\n' ]);
4402fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
4403fprintf(fid,   ['Deformation 0' '\n' ]);
4404fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4405fprintf(fid,   ['IntensityMin 0' '\n' ]);
4406fprintf(fid,  ['SeuilImage n' '\n' ]);
4407fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4408fprintf(fid,   ['ImageToUse ' Param.Civ1.term_a ' ' Param.Civ1.term_b '\n' ]); % VERIFIER ?
4409fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
4410fclose(fid);
4411
4412% cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
4413% cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
4414% namelog=regexprep(namelog,'\\','\\\\');
4415if(isunix)
4416    cmd_CIV1=['cp -f ' filename '.civ1.cmx ' filename '.cmx'];
4417    cmd_CIV1=[cmd_CIV1 '\n'...
4418        Param.xml.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
4419    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
4420   % cmd_CIV1=[cmd_CIV1 '\n' 'mv ' filename '.cmx' ' ' filename '.checkciv1.cmx' '\n'];%rename .cmx as .checkciv1.cmx
4421else %Windows system
4422%                     flname=regexprep(flname,'\\','\\\\');
4423%                     cmd=[cmd 'copy /Y "' flname '.checkciv1.cmx" "' flname '.cmx"\n'];
4424%     filename=regexprep(filename,'\\','\\\\');
4425    cmd_CIV1=['copy /Y "' filename '.civ1.cmx" "' filename '.cmx"'];% copy the .civ1.cmx parameter file to .cmx
4426    cmd_CIV1=[cmd_CIV1 '\n "' regexprep(Param.xml.Civ1Bin,'\\','\\\\') '" -f "' filename '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4427%     namelog=regexprep(namelog,'\\','\\\\');
4428    cmd_CIV1=[cmd_CIV1 '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ1.log"']; %preserve the log file as .civ1.log
4429
4430    %  cmd_CIV1=[cmd_CIV1 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.checkciv1.cmx"'];
4431end
4432
4433%------------------------------------------------------------------------
4434% --- CheckCiv1  Unified
4435function xml_civ1_parameters=CIV1_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
4440civ1.image1=par.filename_ima_a;
4441civ1.image2=par.filename_ima_b;
4442civ1.imageSize_X=par.npx;
4443civ1.imageSize_Y=par.npy;
4444civ1.outputFileName=[filename '.nc'];
4445civ1.correlationBoxesSize_X=par.ibx;
4446civ1.correlationBoxesSize_Y=par.iby;
4447civ1.searchBoxesSize_X=par.isx;
4448civ1.searchBoxesSize_Y=par.isy;
4449civ1.globalShift_X=par.shiftx;
4450civ1.globalShift_Y=par.shifty;
4451civ1.ro=par.rho;
4452civ1.hart='y';
4453if isequal(par.gridflag,'y')
4454    civ1.grid=par.gridname;
4455else
4456    civ1.grid='n';
4457    civ1.gridSpacing_X=par.dx;
4458    civ1.gridSpacing_Y=par.dy;
4459end
4460if isequal(par.maskflag,'y')
4461    civ1.mask=par.maskname;
4462end
4463civ1.dt=par.Dt;
4464civ1.unit='pixel';
4465civ1.absolut_time_T0=par.T0;
4466civ1.pixcmx=par.pxcmx;
4467civ1.pixcmy=par.pxcmy;
4468civ1.convectFlow='n';
4469
4470xml_civ1_parameters=civ1;
4471
4472%------------------------------------------------------------------------
4473% --- CheckCiv2  Unified
4474function civ2=CIV2_CMD_Unified(filename,namelog,par)
4475%------------------------------------------------------------------------
4476%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4477%global CivBin%name of the executable for checkciv1 calculation
4478
4479filename=regexprep(filename,'.nc','');
4480
4481civ2.image1=par.filename_ima_a;
4482civ2.image2=par.filename_ima_b;
4483civ2.imageSize_X=par.npx;
4484civ2.imageSize_Y=par.npy;
4485civ2.inputFileName=[par.filename_nc1 '.nc'];
4486civ2.outputFileName=[filename '.nc'];
4487civ2.correlationBoxesSize_X=par.ibx;
4488civ2.correlationBoxesSize_Y=par.iby;
4489civ2.ro=par.rho;
4490%checkciv2.decimalShift=par.CheckDecimal;
4491%checkciv2.CheckDeformation=par.CheckDeformation;
4492if isequal(par.decimal,'1')
4493    civ2.decimalShift='y';
4494else
4495    civ2.decimalShift='n';
4496end
4497if isequal(par.deformation,'1')
4498    civ2.deformation='y';
4499else
4500    civ2.deformation='n';
4501end
4502if isequal(par.gridflag,'y')
4503    civ2.grid=par.gridname;
4504else
4505    civ2.grid='n';
4506    civ2.gridSpacing_X=par.dx;
4507    civ2.gridSpacing_Y=par.dy;
4508end
4509civ2.gridSpacing_X='10';
4510civ2.gridSpacing_Y='10';%NOTE: faut mettre gridSpacing pourque ca tourne, meme si c'est la grille qui est utilisee
4511if isequal(par.maskflag,'y')
4512    civ2.mask=par.maskname;
4513else
4514    civ2.mask='n';
4515end
4516civ2.dt=par.Dt;
4517civ2.unit='pixel';
4518civ2.absolut_time_T0=par.T0;
4519civ2.pixcmx=par.pxcmx;
4520civ2.pixcmy=par.pxcmy;
4521civ2.convectFlow='n';
4522civ2.pixcmx=par.pxcmx;
4523civ2.pixcmy=par.pxcmy;
4524civ2.convectFlow='n';
4525
4526%------------------------------------------------------------------------
4527% --- CheckCiv2  CheckCiv2  CheckCiv2 CheckCiv2
4528function cmd_CIV2=CIV2_CMD(filename,param)
4529%------------------------------------------------------------------------
4530%pixels per cm and matrix of the image times, read from the .civ file by uvmat
4531% global civ2Bin sge%name of the executable for checkciv1 calculation
4532filename=regexprep(filename,'.nc','');
4533if isequal(Param.Civ2.Dt,'0')
4534    Param.Civ2.Dt='1' ;%case of 'displacement' mode
4535end
4536Param.Civ2.filename_ima_a=regexprep(Param.Civ2.filename_ima_a,'.png','');
4537Param.Civ2.filename_ima_b=regexprep(Param.Civ2.filename_ima_b,'.png','');% bug : .png appears two times ?
4538[fid,errormsg]=fopen([filename '.civ2.cmx'],'w');
4539if isequal(fid,-1)
4540    msgbox_uvmat('ERROR',errormsg)
4541    cmd_CIV2='';
4542    return
4543end
4544fprintf(fid,['##############   CMX file' '\n' ]);
4545fprintf(fid,   ['FirstImage ' regexprep(Param.Civ2.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
4546fprintf(fid,   ['LastImage  ' regexprep(Param.Civ2.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
4547fprintf(fid,  ['XX' '\n' ]);
4548fprintf(fid, ['Mask ' Param.Civ2.MaskFlag '\n' ]);
4549fprintf(fid, ['MaskName ' regexprep(Param.Civ2.MaskName,'\\','\\\\') '\n' ]);% for windows compatibility
4550fprintf(fid, ['ImageSize ' Param.Civ2.npx ' ' Param.Civ2.npy '\n' ]);   %VERIFIER CAS GENERAL ?
4551fprintf(fid, ['CorrelationBoxesSize ' Param.Civ2.ibx ' ' Param.Civ2.iby '\n' ]);
4552fprintf(fid, ['SearchBoxeSize ' Param.Civ2.ibx ' ' Param.Civ2.iby '\n']);
4553fprintf(fid, ['RO ' Param.Civ2.rho '\n']);
4554fprintf(fid, ['GridSpacing ' Param.Civ2.dx ' ' Param.Civ2.dy '\n']);
4555fprintf(fid, ['XX 1.0' '\n' ]);
4556fprintf(fid, ['Dt_TO ' Param.Civ2.Dt ' ' Param.Civ2.T0 '\n' ]);
4557fprintf(fid, ['PixCmXY ' Param.Civ2.pxcmx ' ' Param.Civ2.pxcmy '\n' ]);
4558fprintf(fid, ['XX 1' '\n' ]);
4559fprintf(fid, 'ShiftXY 0 0\n');
4560fprintf(fid, ['Grid ' Param.Civ2.GridFlag '\n' ]);
4561fprintf(fid, ['GridName ' regexprep(Param.Civ2.GridName,'\\','\\\\') '\n']);
4562fprintf(fid, ['XX 85' '\n' ]);
4563fprintf(fid, ['XX 1.0' '\n' ]);
4564fprintf(fid, ['XX 1.0' '\n' ]);
4565fprintf(fid, ['Hart 1' '\n' ]);
4566fprintf(fid, ['DecimalShift ' Param.Civ2.decimal '\n']);
4567fprintf(fid, ['Deformation ' Param.Civ2.deformation '\n']);
4568fprintf(fid,  ['CorrelationMin 0' '\n' ]);
4569fprintf(fid,   ['IntensityMin 0' '\n' ]);
4570fprintf(fid,  ['SeuilImage n' '\n' ]);
4571fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
4572fprintf(fid,   ['ImageToUse ' Param.Civ2.term_a ' ' Param.Civ2.term_b '\n' ]); % VERIFIER ?
4573fprintf(fid, ['ImageUsedBefore ' regexprep(Param.Civ2.filename_nc1,'\\','\\\\') '\n']);
4574fclose(fid);
4575
4576if(isunix)
4577    cmd_CIV2=['cp -f ' filename '.civ2.cmx ' filename '.cmx\n'];
4578    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
4579    cmd_CIV2=[cmd_CIV2 '\n' 'mv ' filename '.log' ' ' filename '.civ2.log' '\n' 'chmod g+w ' filename '.nc'];%preserve the log file as .civ2.log
4580%    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
4581
4582else
4583    filename=regexprep(filename,'\\','\\\\');
4584    cmd_CIV2=['copy /Y "' filename '.civ2.cmx" "' filename '.cmx"'];
4585    cmd_CIV2=[cmd_CIV2 '\n "' regexprep(Param.xml.Civ2Bin,'\\','\\\\') '" -f "' filename  '.cmx" >"' filename '.log"' ]; % redirect standard output to the log file
4586    cmd_CIV2=[cmd_CIV2 '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ2.log"'];
4587 %    cmd_CIV2=[cmd_CIV2 '\n' 'copy /Y "' filename '.cmx' '" "' filename '.checkciv2.cmx"'];
4588end
4589
4590
4591%------------------------------------------------------------------------
4592%--read images and convert them to the uint16 format used for PIV
4593function A=read_image(filename,type_ima,num,movieobject)
4594%------------------------------------------------------------------------
4595%num is the view number needed for an avi movie
4596switch type_ima
4597    case 'movie'
4598        A=read(movieobject,num);
4599    case 'avi'
4600        mov=aviread(filename,num);
4601        A=frame2im(mov(1));
4602    case 'multimage'
4603        A=imread(filename,num);
4604    case 'image'
4605        A=imread(filename);
4606end
4607siz=size(A);
4608if length(siz)==3;%color images
4609    A=sum(double(A),3);
4610    A=uint16(A);
4611end
4612
4613%------------------------------------------------------------------------
4614function ref_i_Callback(hObject, eventdata, handles)
4615%------------------------------------------------------------------------
4616mode_list=get(handles.ListPairMode,'String');
4617mode_value=get(handles.ListPairMode,'Value');
4618mode=mode_list{mode_value};
4619find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4620if isequal(mode,'series(Di)') || ...% we do patch2 only
4621        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
4622    find_netcpair_civ2(hObject, eventdata, handles);
4623end
4624
4625%------------------------------------------------------------------------
4626function ref_j_Callback(hObject, eventdata, handles)
4627%------------------------------------------------------------------------
4628mode_list=get(handles.ListPairMode,'String');
4629mode_value=get(handles.ListPairMode,'Value');
4630mode=mode_list{mode_value};
4631if isequal(get(handles.CheckCiv1,'Value'),0)|| isequal(mode,'series(Dj)')
4632    find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4633end
4634if isequal(mode,'series(Dj)') || ...
4635        (get(handles.CheckCiv2,'Value')==0 && get(handles.CheckCiv1,'Value')==0 && get(handles.CheckFix1,'Value')==0 && get(handles.CheckPatch1,'Value')==0)
4636    find_netcpair_civ2(hObject, eventdata, handles);
4637end
4638
4639%------------------------------------------------------------------------
4640function ref_i_civ2_Callback(hObject, eventdata, handles)
4641%------------------------------------------------------------------------
4642mode_list=get(handles.ListPairMode,'String');
4643mode_value=get(handles.ListPairMode,'Value');
4644mode=mode_list{mode_value};
4645find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4646
4647%------------------------------------------------------------------------
4648function ref_j_civ2_Callback(hObject, eventdata, handles)
4649%------------------------------------------------------------------------
4650mode_list=get(handles.ListPairMode,'String');
4651mode_value=get(handles.ListPairMode,'Value');
4652mode=mode_list{mode_value};
4653if  isequal(mode,'series(Dj)')
4654    find_netcpair_civ2(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4655end
4656
4657%------------------------------------------------------------------------
4658% --- Executes on button press in ListCompareMode.
4659function ListCompareMode_Callback(hObject, eventdata, handles)
4660%------------------------------------------------------------------------
4661test=get(handles.ListCompareMode,'Value');
4662if test==2 || test==3 % case 'dispalcemen' or 'stereo PIV'
4663    filebase=get(handles.RootName,'String');
4664    browse=get(handlesRootName,'Userdata');
4665    browse.nom_type_ima1=browse.nom_type_ima;
4666    set(handlesRootName,'UserData',browse);
4667    set(handles.sub_txt,'Visible','on')
4668    set(handles.RootName_1,'Visible','On');%mkes the second file input window visible
4669    mode_store=get(handles.ListPairMode,'String');%get the present 'mode'
4670    set(handles.ListCompareMode,'UserData',mode_store);%store the mode display
4671    set(handles.ListPairMode,'Visible','off')
4672    if test==2
4673        set(handles.ListPairMode,'Visible','off')
4674        set(handles.ListPairMode,'Value',1) % mode 'civX' selected by default
4675    else
4676        set(handles.ListPairMode,'Visible','on')
4677        set(handles.ListPairMode,'Value',3) % mode 'Matlab' selected for stereo
4678    end
4679   
4680    %% menuopen an image file with the browser
4681    ind_opening=1;%default
4682    browse.incr_pair=[0 0]; %default
4683    oldfile=get(handles.RootName,'String');
4684     menu={'*.xml;*.civ;*.png;*.jpg;*.tif;*.avi;*.AVI;*.nc;', ' (*.xml,*.civ,*.png,*.jpg ,.tif, *.avi,*.nc)';
4685       '*.xml',  '.xml files '; ...
4686        '*.civ',  '.civ files '; ...
4687        '*.png','.png image files'; ...
4688        '*.jpg',' jpeg image files'; ...
4689        '*.tif','.tif image files'; ...
4690        '*.avi;*.AVI','.avi movie files'; ...
4691        '*.nc','.netcdf files'; ...
4692        '*.*',  'All Files (*.*)'};
4693    [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file of the second series',oldfile);
4694    fileinput=[PathName FileName];%complete file name
4695    sizf=size(fileinput);
4696    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4697    [path,name,ext]=fileparts(fileinput);
4698    [path1]=fileparts(filebase);
4699    if isunix
4700        [status,path]=system(['readlink ' path]);
4701        [status,path1]=system(['readlink ' path1]);% look for the true path in case of symbolic paths
4702    end
4703    if ~strcmp(path1,path)
4704        msgbox_uvmat('ERROR','The second image series must be in the same directory as the first one')
4705        return
4706     end
4707%     set(handles.RootName_1,'String',name);
4708    [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
4709    set(handles.RootName_1,'String',RootFile);
4710    browse=get(handlesRootName,'UserData');
4711    browse.nom_type_ima_1=nom_type;
4712    set(handlesRootName,'UserData',browse)
4713   
4714    %check image extension
4715    if ~strcmp(ext,get(handles.ImaExt,'String'))
4716        msgbox_uvmat('ERROR','The second image series must have the same extension name as the first one')
4717        return
4718    end
4719   
4720    %% check coincidence of image sizes
4721%     ref_i=get(handles.ref_i,'string');
4722%     ref_j=get(handles.ref_j,'string');
4723%     [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]);
4724%     A=imread(filecell.ima1.checkciv1{1});
4725%     A_1=imread(fileinput);
4726%     npxy=size(A);
4727%     npxy_1=size(A_1);
4728%     if ~isequal(size(A),size(A_1))
4729%         msgbox_uvmat('ERROR','The two input image series do not have the same size')
4730%         return
4731%     end
4732else
4733    set(handles.ListPairMode,'Visible','on')
4734    set(handles.RootName_1,'Visible','Off');
4735    set(handles.sub_txt,'Visible','off')
4736    set(handles.RootName_1,'String',[]);
4737    mode_store=get(handles.ListCompareMode,'UserData');
4738    set(handles.ListPairMode,'Value',1)
4739    set(handles.ListPairMode,'String',mode_store)
4740    set(handles.test_stereo1,'Value',0)
4741    set(handles.CheckStereo,'Value',0)
4742    set(handles.ListPairMode,'Value',1) % mode 'civX' selected by default
4743end
4744if test==3 && get(handles.CheckPatch1,'Value')
4745    set(handles.test_stereo1,'Visible','on')
4746else
4747    set(handles.test_stereo1,'Visible','off')
4748end
4749if test==3 && get(handles.CheckPatch2,'Value')
4750    set(handles.CheckStereo,'Visible','on')
4751else
4752    set(handles.CheckStereo,'Visible','off')
4753end
4754mode_Callback(hObject, eventdata, handles)
4755
4756%------------------------------------------------------------------------
4757% --- Executes on button press in get_ref_fix1.
4758function get_ref_fix1_Callback(hObject, eventdata, handles)
4759%------------------------------------------------------------------------
4760filebase=get(handles.RootName,'String');
4761[FileName, PathName, filterindex] = uigetfile( ...
4762    {'*.nc', ' (*.nc)';
4763    '*.nc',  'netcdf files '; ...
4764    '*.*', 'All Files (*.*)'}, ...
4765    'Pick a file',filebase);
4766
4767fileinput=[PathName FileName];
4768sizf=size(fileinput);
4769if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4770[Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4771ref.filebase=fullfile(Path,File);
4772ref.num_a=stra2num(str_a);
4773ref.num_b=stra2num(str_b);
4774ref.num1=str2double(field_count);
4775ref.num2=str2double(str2);
4776browse=[];%initialisation
4777if ~isequal(ref.ext,'.nc')
4778    msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4779    return
4780end
4781set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
4782set(handles.ref_fix1,'UserData',ref)
4783menu_field{1}='civ1';
4784Data=nc2struct(fileinput,[]);
4785if isfield(Data,'patch') && isequal(Data.patch,1)
4786    menu_field{2}='filter1';
4787end
4788if isfield(Data,'civ2') && isequal(Data.civ2,1)
4789    menu_field{3}='civ2';
4790end
4791if isfield(Data,'patch2') && isequal(Data.patch2,1)
4792    menu_field{4}='filter2';
4793end
4794set(handles.field_ref1,'String',menu_field);
4795set(handles.field_ref1,'Value',length(menu_field));
4796set(handles.num_MinVel,'Value',2);
4797set(handles.num_MinVel,'String','1');%default threshold
4798set(handles.ref_fix1,'Enable','on')
4799
4800%------------------------------------------------------------------------
4801% --- Executes on button press in get_ref_fix2.
4802function get_ref_fix2_Callback(hObject, eventdata, handles)
4803%------------------------------------------------------------------------
4804if isequal(get(handles.get_ref_fix2,'Value'),1)
4805    filebase=get(handles.RootName,'String');
4806    [FileName, PathName, filterindex] = uigetfile( ...
4807        {'*.nc', ' (*.nc)';
4808        '*.nc',  'netcdf files '; ...
4809        '*.*', 'All Files (*.*)'}, ...
4810        'Pick a file',filebase);
4811    fileinput=[PathName FileName];
4812    sizf=size(fileinput);
4813    if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4814    [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4815    ref.filebase=fullfile(Path,File);
4816    ref.num_a=stra2num(str_a);
4817    ref.num_b=stra2num(str_b);
4818    ref.num1=str2num(field_count);
4819    ref.num2=str2num(str2);
4820    browse=[];%initialisation
4821    if ~isequal(ref.ext,'.nc')
4822        msgbox_uvmat('ERROR','the reference file must be in netcdf format (*.nc)')
4823        return
4824    end
4825    set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
4826    set(handles.ref_fix2,'UserData',ref)
4827    menu_field{1}='civ1';
4828    Data=nc2struct(fileinput,[]);
4829    if isfield(Data,'patch') & isequal(Data.patch,1)
4830        menu_field{2}='filter1';
4831    end
4832    if isfield(Data,'civ2') & isequal(Data.civ2,1)
4833        menu_field{3}='civ2';
4834    end
4835    if isfield(Data,'patch2') & isequal(Data.patch2,1)
4836        menu_field{4}='filter2';
4837    end
4838    set(handles.field_ref2,'String',menu_field);
4839    set(handles.field_ref2,'Value',length(menu_field));
4840    set(handles.num_MinVel,'Value',2);
4841    set(handles.num_MinVel,'String','1');%default threshold
4842    set(handles.ref_fix2,'Enable','on')
4843    set(handles.ref_fix2,'Visible','on')
4844    set(handles.field_ref2,'Visible','on')
4845else
4846    set(handles.ref_fix2,'Visible','off')
4847    set(handles.field_ref2,'Visible','off')
4848end
4849
4850%------------------------------------------------------------------------
4851function ref_fix1_Callback(hObject, eventdata, handles)
4852%------------------------------------------------------------------------
4853set(handles.num_MinVel,'Value',1);
4854set(handles.field_ref1,'Value',1)
4855set(handles.field_ref1,'String',{' '})
4856set(handles.ref_fix1,'UserData',[]);
4857set(handles.ref_fix1,'String','');
4858set(handles.thresh_vel1,'String','0');
4859
4860%------------------------------------------------------------------------
4861function ref_fix2_Callback(hObject, eventdata, handles)
4862%------------------------------------------------------------------------
4863set(handles.num_MinVel,'Value',1);
4864set(handles.field_ref2,'Value',1)
4865set(handles.field_ref2,'String',{' '})
4866set(handles.ref_fix2,'UserData',[]);
4867set(handles.ref_fix2,'String','');
4868set(handles.num_MinVel,'String','0');
4869
4870%------------------------------------------------------------------------
4871% --- Executes on button press in test_stereo1.
4872function test_stereo1_Callback(hObject, eventdata, handles)
4873%------------------------------------------------------------------------
4874if isequal(get(handles.test_stereo1,'Value'),0)
4875    set(handles.subdomain_patch1,'Visible','on')
4876    set(handles.rho_patch1,'Visible','on')
4877else
4878    set(handles.subdomain_patch1,'Visible','off')
4879    set(handles.rho_patch1,'Visible','off')
4880end
4881
4882%------------------------------------------------------------------------
4883% --- Executes on button press in CheckStereo.
4884function StereoCheck_Callback(hObject, eventdata, handles)
4885%------------------------------------------------------------------------
4886if isequal(get(handles.CheckStereo,'Value'),0)
4887    set(handles.num_SubdomainSize,'Visible','on')
4888    set(handles.num_SmoothParam,'Visible','on')
4889else
4890    set(handles.num_SubdomainSize,'Visible','off')
4891    set(handles.num_SmoothParam,'Visible','off')
4892end
4893
4894%------------------------------------------------------------------------
4895% --- Executes on button press in check_Threshold.
4896function ImaThreshold_Callback(hObject, eventdata, handles)
4897%------------------------------------------------------------------------
4898if isequal(get(handles.check_Threshold,'Value'),1)
4899    set(handles.num_MinIma,'Visible','on')
4900    set(handles.num_MaxIma,'Visible','on')
4901else
4902    set(handles.num_MinIma,'Visible','off')
4903    set(handles.num_MaxIma,'Visible','off')
4904end
4905
4906%------------------------------------------------------------------------
4907% --- Executes on button press in ImaThreshold2.
4908function ImaThreshold2_Callback(hObject, eventdata, handles)
4909%------------------------------------------------------------------------
4910if isequal(get(handles.ImaThreshold2,'Value'),1)
4911    set(handles.num_MinIma,'Visible','on')
4912    set(handles.num_MaxIma,'Visible','on')
4913else
4914    set(handles.num_MinIma,'Visible','off')
4915    set(handles.num_MaxIma,'Visible','off')
4916end
4917
4918%------------------------------------------------------------------------
4919% --- Executes on button press in TestCiv1: display image correlation function
4920function TestCiv1_Callback(hObject, eventdata, handles)
4921%------------------------------------------------------------------------
4922set(handles.TestCiv1,'BackgroundColor',[1 1 0])
4923drawnow
4924test_civ1=get(handles.TestCiv1,'Value');
4925if test_civ1
4926    ref_i=str2double(get(handles.ref_i,'String'));
4927    if strcmp(get(handles.ref_j,'Visible'),'on')
4928        ref_j=str2double(get(handles.ref_j,'String'));
4929    else
4930        ref_j=1;%default
4931    end
4932    [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]=...
4933        set_civ_filenames(handles,ref_i,ref_j,[1 0 0 0 0 0]);
4934    Data.ListVarName={'ny','nx','A'};
4935    Data.VarDimName={'ny','nx',{'ny','nx'}};
4936    Data.A=imread(filecell.ima1.civ1{1});
4937    Data.ny=[size(Data.A,1) 1];
4938    Data.nx=[1 size(Data.A,2)];
4939    par_civ1=read_param_civ1(handles,filecell.ima1.civ1{1});
4940    par_civ1.filename_ima_a=filecell.ima1.civ1{1};
4941    par_civ1.filename_ima_b=filecell.ima2.civ1{1};
4942    par_civ1.T0=0;
4943    par_civ1.Dt=1;
4944    Param.Civ1=par_civ1;
4945    Data=civ_uvmat(Param);
4946    Data.ListVarName=[Data.ListVarName {'ny','nx','A'}];
4947    Data.VarDimName=[Data.VarDimName {'ny','nx',{'ny','nx'}}];
4948    Data.A=imread(filecell.ima1.civ1{1});
4949    Data.ny=[size(Data.A,1) 1];
4950    Data.nx=[1 size(Data.A,2)];
4951    hview_field=view_field(Data);
4952    set(0,'CurrentFigure',hview_field)
4953    hhview_field=guihandles(hview_field);
4954    set(hview_field,'CurrentAxes',hhview_field.axes3)
4955    ViewData=get(hview_field,'UserData');
4956    ViewData.CivHandle=handles.civ;% indicate the handle of the civ GUI in view_field
4957    ViewData.axes3.B=imread(filecell.ima2.civ1{1});%store the second image in the UserData of the GUI view_field
4958    ViewData.axes3.X=Data.Civ1_X; %keep the set of points in memeory
4959    ViewData.axes3.Y=Data.Civ1_Y;
4960    set(hview_field,'UserData',ViewData)
4961    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
4962    if isempty(corrfig)
4963        corrfig=figure;
4964        set(corrfig,'tag','corrfig')
4965        set(corrfig,'name','image correlation')
4966        set(corrfig,'DeleteFcn',{@closeview_field})%
4967    end
4968    set(handles.TestCiv1,'BackgroundColor',[1 0 0])
4969else
4970    corrfig=findobj(allchild(0),'tag','corrfig');% look for a current figure for image correlation display
4971    if ~isempty(corrfig)
4972        delete(corrfig)
4973    end
4974    hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
4975    if ~isempty(hview_field)
4976        delete(hview_field)
4977    end
4978end
4979
4980function closeview_field(gcbo,eventdata)
4981hview_field=findobj(allchild(0),'tag','view_field');% look for view_field   
4982    if ~isempty(hview_field)
4983        delete(hview_field)
4984    end
4985%-------------------------------------------------------------------
4986% --- Executes on button press in status.
4987function status_Callback(hObject, eventdata, handles)
4988%-------------------------------------------------------------------
4989val=get(handles.status,'Value');
4990if val==0
4991    set(handles.status,'BackgroundColor',[0 1 0])
4992    hfig=findobj(allchild(0),'name','civ_status');
4993    if ~isempty(hfig)
4994        delete(hfig)
4995    end
4996    return
4997end
4998set(handles.status,'BackgroundColor',[1 1 0])
4999drawnow
5000listtype={'civ1','fix1','patch1','civ2','fix2','patch2'};
5001Param.CheckCiv1=get(handles.CheckCiv1,'Value');
5002Param.CheckFix1=get(handles.CheckFix1,'Value');
5003Param.CheckPatch1=get(handles.CheckPatch1,'Value');
5004Param.CheckCiv2=get(handles.CheckCiv2,'Value');
5005Param.CheckFix2=get(handles.CheckFix2,'Value');
5006Param.CheckPatch2=get(handles.CheckPatch2,'Value');
5007box_test=[Param.CheckCiv1 Param.CheckFix1 Param.CheckPatch1 Param.CheckCiv2 Param.CheckFix2 Param.CheckPatch2];
5008
5009option_civ=find(box_test,1,'last');%last selected option (non-zero index of box_test)
5010filecell=get(handles.civ,'UserData');%retrieve the list of output files expected for PIV
5011test_new=0;
5012if ~isfield(filecell,'nc')
5013    test_new=1;
5014    [ref_i,ref_j,errormsg]=find_ref_indices(handles);
5015    if ~isempty(errormsg)
5016        msgbox_uvmat('ERROR',errormsg)
5017        return
5018    end
5019    filecell=set_civ_filenames(handles,ref_i,ref_j,box_test);%determine the output file expected from the GUI status
5020end
5021if ~isequal(box_test(4:6),[0 0 0])
5022    civ_files=filecell.nc.civ2;%case of civ2 operations
5023else
5024    civ_files=filecell.nc.civ1;
5025end
5026[root,filename,ext]=fileparts(civ_files{1});
5027[rootroot,subdir,extdir]=fileparts(root);
5028hfig=findobj(allchild(0),'name','civ_status');
5029if isempty(hfig)
5030    hfig=figure('DeleteFcn',@stop_status);
5031    set(hfig,'name','civ_status')
5032    hlist=uicontrol('Style','listbox','Units','normalized', 'Position',[0.05 0.09 0.9 0.71], 'Callback', @open_view_field,'tag','list');
5033    uicontrol('Style','edit','Units','normalized', 'Position', [0.05 0.87 0.9 0.1],'tag','msgbox','Max',2,'String','checking files...');
5034    uicontrol('Style','frame','Units','normalized', 'Position', [0.05 0.81 0.9 0.05]);
5035    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);
5036    BarPosition=[0.05 0.81 0.01 0.05];
5037    hwaitbar=uicontrol('Style','frame','Units','normalized', 'Position',BarPosition ,'BackgroundColor',[1 0 0],'tag','waitbar');
5038    drawnow
5039end
5040% datnum=[];
5041Tabchar={};
5042nbfiles=numel(civ_files);
5043count=0;
5044testrecent=0;
5045while count<nbfiles
5046    count=0;
5047    datnum=zeros(1,nbfiles);
5048    for ifile=1:nbfiles
5049        detect=exist(civ_files{ifile},'file'); % check the existence of the file
5050        option=0;
5051        if detect==0
5052            option_str='not created';
5053        else
5054            datfile=dir(civ_files{ifile});
5055            if isfield(datfile,'datenum')
5056                datnum(ifile)=datfile.datenum;%only available in recent matlab versions
5057                testrecent=1;
5058            end
5059            filefound(ifile)={datfile.name};
5060            lastfield='';
5061            % check the content  netcdf file
5062            Data=nc2struct(civ_files{ifile},'ListGlobalAttribute','patch2','fix2','civ2','patch','fix');
5063            if ~isempty(Data.patch2) && isequal(Data.patch2,1)
5064                option=6;
5065                option_str='patch2';
5066            elseif ~isempty(Data.fix2) && isequal(Data.fix2,1)
5067                option=5;
5068                option_str='fix2';
5069            elseif ~isempty(Data.civ2) && isequal(Data.civ2,1);
5070                option=4;
5071                option_str='civ2';
5072            elseif ~isempty(Data.patch) && isequal(Data.patch,1);
5073                option=3;
5074                option_str='patch1';
5075            elseif ~isempty(Data.fix) && isequal(Data.fix,1);
5076                option=2;
5077                option_str='fix1';
5078            else
5079                option=1;
5080                option_str='civ1';
5081            end
5082        end
5083        if option >= option_civ
5084            count=count+1;
5085        end
5086        [rr,filename,ext]=fileparts(civ_files{ifile});
5087        Tabchar{ifile,1}=[fullfile([subdir extdir],filename) ext  '...' option_str];
5088    end
5089    datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
5090    if isempty(datnum)
5091        if testrecent
5092            message='no civ result created yet';
5093        else
5094            message='';
5095        end
5096    else
5097        datnum=datnum(datnum~=0);%keep the non zero values corresponding to existing files
5098        [first,ind]=min(datnum);
5099        [last,indlast]=max(datnum);
5100        if test_new
5101            message='existing file status, no processing launched yet';
5102        else
5103        message={[num2str(count) ' file(s) done over ' num2str(nbfiles)] ;['oldest modification:  ' cell2mat(filefound(ind)) ' : ' datestr(first)];...
5104            ['latest modification:  ' cell2mat(filefound(indlast)) ' : ' datestr(last)]};
5105        end
5106    end
5107    hfig=findobj(allchild(0),'name','civ_status');
5108    if isempty(hfig)% the status list has been deleted
5109        return
5110    else
5111        hlist=findobj(hfig,'tag','list');
5112        hmsgbox=findobj(hfig,'tag','msgbox');
5113        hwaitbar=findobj(hfig,'tag','waitbar');
5114        set(hlist,'String',Tabchar)
5115        set(hmsgbox,'String', message)
5116        if count>0 && ~test_new
5117            BarPosition(3)=0.9*count/nbfiles;
5118            set(hwaitbar,'Position',BarPosition)
5119        end
5120    end
5121    set(hlist,'UserData',rootroot)
5122    pause(10)% wait 10 seconds for next check
5123end
5124
5125   
5126%------------------------------------------------------------------------   
5127% call 'view_field.fig' to display the  field selected in the list of 'status'
5128function open_view_field(hObject, eventdata)
5129%------------------------------------------------------------------------
5130list=get(hObject,'String');
5131index=get(hObject,'Value');
5132rootroot=get(hObject,'UserData');
5133filename=list{index};
5134ind_dot=findstr(filename,'...');
5135filename=filename(1:ind_dot-1);
5136filename=fullfile(rootroot,filename);
5137delete(get(hObject,'parent'))%delete the display figure to stop the check process
5138if exist(filename,'file')%visualise the vel field if it exists
5139    uvmat(filename)
5140    set(gcbo,'Value',1)
5141end
5142
5143%------------------------------------------------------------------------   
5144% launched by pressing OK on the status figure
5145function close_GUI(hObject, eventdata)
5146%------------------------------------------------------------------------
5147    delete(gcbf)
5148   
5149%------------------------------------------------------------------------   
5150% launched by deleting the status figure
5151function stop_status(hObject, eventdata)
5152%------------------------------------------------------------------------
5153hciv=findobj(allchild(0),'tag','civ');
5154hhciv=guidata(hciv);
5155set(hhciv.status,'value',0) %reset the status uicontrol in the GUI civ
5156set(hhciv.status,'BackgroundColor',[0 1 0])
5157
5158%------------------------------------------------------------------------
5159% % --- Executes on button press in ListPairMode.
5160% function CivMode_Callback(hObject, eventdata, handles)
5161% %------------------------------------------------------------------------
5162% Listprog=get(handles.ListPairMode,'String');
5163% index=get(handles.ListPairMode,'Value');
5164% prog=Listprog{index};
5165% switch prog
5166%     case 'CivX'
5167%         set(handles.thresh_patch1,'Visible','off')
5168%         set(handles.thresh_text1,'Visible','off')
5169%         set(handles.num_MaxDiff,'Visible','off')
5170%         set(handles.thresh_text2,'Visible','off')
5171%         set(handles.num_Rho,'Style','edit')
5172%         set(handles.num_Rho,'String','1')
5173%         set(handles.BATCH,'Enable','on')
5174%     case 'CivAll'
5175%         if get(handles.CheckPatch1,'Value')
5176%             set(handles.thresh_patch1,'Visible','on')
5177%             set(handles.thresh_text1,'Visible','on')
5178%         end
5179%         set(handles.num_Rho,'Style','edit')
5180%         set(handles.num_Rho,'String','1')
5181%         set(handles.BATCH,'Enable','on')
5182%     case 'CivUvmat'
5183%       
5184% end
5185
5186
5187% --------------------------------------------------------------------
5188function MenuHelp_Callback(hObject, eventdata, handles)
5189path_civ=fileparts(which ('civ'));
5190helpfile=fullfile(path_civ,'uvmat_doc','uvmat_doc.html');
5191if isempty(dir(helpfile))
5192    msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
5193else
5194    addpath (fullfile(path_civ,'uvmat_doc'))
5195    web([helpfile '#civ'])
5196end
5197
5198% --------------------------------------------------------------------
5199function CivX_Callback(hObject, eventdata, handles)
5200%set(handles.thresh_patch1,'Visible','off')
5201set(handles.thresh_text1,'Visible','off')
5202set(handles.num_MaxDiff,'Visible','off')
5203set(handles.thresh_text2,'Visible','off')
5204set(handles.num_Rho,'Style','edit')
5205set(handles.num_Rho,'String','1')
5206set(handles.BATCH,'Enable','on')
5207
5208% --------------------------------------------------------------------
5209function Matlab_Callback(hObject, eventdata, handles)
5210set(handles.Matlab,'checked','on')
5211set(handles.CivX,'checked','off')
5212if get(handles.CheckPatch1,'Value')
5213    set(handles.thresh_patch1,'Visible','on')
5214    set(handles.thresh_text1,'Visible','on')
5215end
5216if get(handles.CheckPatch2,'Value')
5217    set(handles.num_MaxDiff,'Visible','on')
5218    set(handles.thresh_text2,'Visible','on')
5219end
5220set(handles.num_Rho,'Style','popupmenu')
5221set(handles.num_Rho,'Value',1)
5222set(handles.num_Rho,'String',{'1';'2'})
5223set(handles.BATCH,'Enable','off')
5224
5225% --------------------------------------------------------------------
5226% --- read a panel with handle 'handle' producing a structure 'struct'
5227function struct=read_panel(handle)
5228hchild=get(handle,'children');
5229for ichild=1:numel(hchild)
5230    if strcmp(get(hchild(ichild),'Visible'),'on')
5231        object_style=get(hchild(ichild),'Style');
5232        tag=get(hchild(ichild),'tag');
5233        check_input=1;%default
5234        switch object_style
5235            case 'edit'
5236                switch(tag(1:4))
5237                    case 'num_'
5238                        input=str2double(get(hchild(ichild),'String'));
5239                        %deal with undefined input: retrieve the default value stored as UserData
5240                        if isnan(input)
5241                            input=get(hchild(ichild),'UserData');
5242                            set(hchild(ichild),'String',num2str(input))
5243                        end
5244                    case 'txt_'
5245                        input=get(hchild(ichild),'String');
5246                end
5247                key=tag(5:end);
5248            case 'checkbox'
5249                input=get(hchild(ichild),'Value');
5250                key=tag(7:end);
5251            otherwise
5252                check_input=0;
5253        end
5254        if check_input
5255            eval(['struct.' key '=input;'])
5256        end
5257    end
5258end
5259
5260
5261
5262function cmd=cmd_civ1(filename,Param)
5263%TODO : include filename in par_civ1
5264%------------------------------------------------------------------------
5265%pixels per cm and matrix of the image times, read from the .civ file by uvmat
5266
5267%changes : filename_cmx -> filename ( no extension )
5268
5269filename=regexprep(filename,'.nc','');
5270
5271if isequal(Param.Civ1.Dt,'0')
5272    Param.Civ1.Dt='1' ;%case of 'displacement' mode
5273end
5274Param.Civ1.filename_ima_a=regexprep(Param.Civ1.filename_ima_a,'.png','');
5275Param.Civ1.filename_ima_b=regexprep(Param.Civ1.filename_ima_b,'.png','');
5276fid=fopen([filename '.civ1.cmx'],'w');
5277fprintf(fid,['##############   CMX file' '\n' ]);
5278fprintf(fid,   ['FirstImage ' regexprep(Param.Civ1.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
5279fprintf(fid,   ['LastImage  ' regexprep(Param.Civ1.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
5280fprintf(fid,  ['XX' '\n' ]);
5281fprintf(fid,  ['Mask ' Param.Civ1.MaskFlag '\n' ]);
5282fprintf(fid,  ['MaskName ' regexprep(Param.Civ1.MaskName,'\\','\\\\') '\n' ]);
5283fprintf(fid,   ['ImageSize ' num2str(Param.Civ1.npx) ' ' num2str(Param.Civ1.npy) '\n' ]);   %VERIFIER CAS GENERAL ?
5284fprintf(fid,   ['CorrelationBoxesSize ' num2str(Param.Civ1.Bx) ' ' num2str(Param.Civ1.By) '\n' ]);
5285fprintf(fid,   ['SearchBoxeSize ' num2str(Param.Civ1.Searchx) ' ' num2str(Param.Civ1.Searchy) '\n' ]);
5286fprintf(fid,   ['RO ' num2str(Param.Civ1.Rho) '\n' ]);
5287fprintf(fid,   ['GridSpacing ' num2str(Param.Civ1.Dx) ' ' num2str(Param.Civ1.Dy) '\n' ]);
5288fprintf(fid,   ['XX 1.0' '\n' ]);
5289fprintf(fid,   ['Dt_TO ' num2str(Param.Civ1.Dt) ' ' num2str(Param.Civ1.T0) '\n' ]);
5290fprintf(fid,  ['PixCmXY ' num2str(Param.Civ1.pxcmx) ' ' num2str(Param.Civ1.pxcmy) '\n' ]);
5291fprintf(fid,  ['XX 1' '\n' ]);
5292fprintf(fid,   ['ShiftXY ' num2str(Param.Civ1.Shiftx) ' '  num2str(Param.Civ1.Shifty) '\n' ]);
5293fprintf(fid,  ['Grid ' Param.Civ1.GridFlag '\n' ]);
5294fprintf(fid,   ['GridName ' regexprep(Param.Civ1.GridName,'\\','\\\\') '\n' ]);
5295fprintf(fid,   ['XX 85' '\n' ]);
5296fprintf(fid,   ['XX 1.0' '\n' ]);
5297fprintf(fid,   ['XX 1.0' '\n' ]);
5298fprintf(fid,   ['Hart 1' '\n' ]);
5299fprintf(fid,  [ 'DecimalShift 0' '\n' ]);
5300fprintf(fid,   ['Deformation 0' '\n' ]);
5301fprintf(fid,  ['CorrelationMin 0' '\n' ]);
5302fprintf(fid,   ['IntensityMin 0' '\n' ]);
5303fprintf(fid,  ['SeuilImage n' '\n' ]);
5304fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
5305fprintf(fid,   ['ImageToUse ' Param.Civ1.term_a ' ' Param.Civ1.term_b '\n' ]); % VERIFIER ?
5306fprintf(fid,   ['ImageUsedBefore null null' '\n' ]);
5307fclose(fid);
5308
5309% cmd_CIV1=[sparam.Civ1Bin ' -f ' filename '.cmx >' filename '.log' ]; % redirect standard output to the log file
5310% cmd_CIV1=regexprep(cmd_CIV1,'\\','\\\\');
5311% namelog=regexprep(namelog,'\\','\\\\');
5312if(isunix)
5313    cmd=['cp -f ' filename '.civ1.cmx ' filename '.cmx \n '];% the cmx file gives the name to the nc file
5314    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
5315%     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
5316%    cmd=[cmd '\n' 'mv ' filename '.cmx' ' ' filename '.civ1.cmx' '\n'];%rename .cmx as .civ1.cmx
5317    cmd=[cmd 'rm ' filename '.cmx'];
5318else %Windows system
5319    filename=regexprep(filename,'\\','\\\\');
5320    cmd=['copy /Y "' filename '.civ1.cmx" "' filename '.cmx" \n '];
5321    cmd=[cmd '"' regexprep(Param.xml.Civ1Bin,'\\','\\\\')...
5322        '" -f "' filename '.cmx" >"' filename '.civ1.log" \n ' ]; % redirect standard output to the log file
5323%     namelog=regexprep(namelog,'\\','\\\\');
5324%     cmd=[cmd '\n ' 'copy /Y "' filename '.log' '" "' filename '.civ1.log"']; %preserve the log file as .civ1.log
5325    cmd=[cmd 'del "' filename '.cmx"'];
5326end
5327
5328
5329function cmd=cmd_fix(filename,Param,fixname)
5330%%
5331filename=regexprep(filename,'.nc','');
5332MaskName_string='';%default
5333if Param.(fixname).CheckMask
5334    MaskName_string=[' -maskName "' Param.(fixname).MaskName '"'];
5335end
5336MaxVel_string='';%default
5337if ~isempty(Param.(fixname).MaxVel)
5338    MaxVel_string=[' -threshV ' num2str(Param.(fixname).MaxVel)];
5339end
5340if isunix
5341    cmd=[Param.xml.FixBin ' -f ' filename '.nc -fi1 ' num2str(Param.(fixname).CheckFmin2) ...
5342        ' -fi2 ' num2str(Param.(fixname).CheckF2) ' -fi3 ' num2str(Param.(fixname).CheckF3) ...
5343        ' -threshC ' num2str(Param.(fixname).MinCorr) MaxVel_string MaskName_string...
5344        ' >' filename '.' lower(fixname) '.log 2>&1'];
5345else
5346    cmd=['"' Param.xml.FixBin '" -f "' filename '.nc" -fi1 ' num2str(Param.(fixname).CheckFmin2)...
5347        ' -fi2 ' num2str(Param.(fixname).CheckF2) ' -fi3 ' num2str(Param.(fixname).CheckF3) ...
5348        ' -threshC ' num2str(Param.(fixname).MinCorr) MaxVel_string MaskName_string...
5349        ' > "' filename '.' lower(fixname) '.log"'];
5350    cmd=regexprep(cmd,'\\','\\\\');
5351end
5352
5353
5354function cmd=cmd_patch(filename,Param,patchname)
5355%% ------------------------------------------------------------------------
5356filename=regexprep(filename,'.nc','');
5357% if test_interp==0
5358if isunix
5359    cmd=[Param.xml.PatchBin...
5360        ' -f ' filename '.nc -m ' num2str(Param.(patchname).Nx)...
5361        ' -n ' num2str(Param.(patchname).Ny) ' -ro ' num2str(Param.(patchname).SmoothingParam)...
5362        ' -nopt ' num2str(Param.(patchname).SubdomainSize) ...
5363        '  > ' filename '.' lower(patchname) '.log 2>&1']; % redirect standard output to the log file
5364else
5365    cmd=['"' Param.xml.PatchBin...
5366        '" -f "' filename '.nc" -m ' num2str(Param.(patchname).Nx)...
5367        ' -n ' num2str(Param.(patchname).Ny) ' -ro ' num2str(Param.(patchname).SmoothingParam)...
5368        ' -nopt ' num2str(Param.(patchname).SubdomainSize)...
5369        '  > "' filename '.' lower(patchname) '.log" 2>&1']; % redirect standard output to the log file
5370    cmd=regexprep(cmd,'\\','\\\\');
5371end
5372% else %nouveau programme patch
5373%     cmd=[PatchBin ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
5374%         ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
5375% end
5376
5377
5378%------------------------------------------------------------------------
5379% --- CheckCiv2  CheckCiv2  CheckCiv2 CheckCiv2
5380function cmd=cmd_civ2(filename,Param)
5381%------------------------------------------------------------------------
5382%pixels per cm and matrix of the image times, read from the .civ file by uvmat
5383% global civ2Bin sge%name of the executable for checkciv1 calculation
5384filename=regexprep(filename,'.nc','');
5385if isequal(Param.Civ2.Dt,'0')
5386    Param.Civ2.Dt='1' ;%case of 'displacement' mode
5387end
5388Param.Civ2.filename_ima_a=regexprep(Param.Civ2.filename_ima_a,'.png','');
5389Param.Civ2.filename_ima_b=regexprep(Param.Civ2.filename_ima_b,'.png','');% bug : .png appears two times ?
5390[fid,errormsg]=fopen([filename '.civ2.cmx'],'w');
5391if isequal(fid,-1)
5392    msgbox_uvmat('ERROR',errormsg)
5393    cmd='';
5394    return
5395end
5396fprintf(fid,['##############   CMX file' '\n' ]);
5397fprintf(fid,   ['FirstImage ' regexprep(Param.Civ2.filename_ima_a,'\\','\\\\') '\n' ]);% for windows compatibility
5398fprintf(fid,   ['LastImage  ' regexprep(Param.Civ2.filename_ima_b,'\\','\\\\') '\n' ]);% for windows compatibility
5399fprintf(fid,  ['XX' '\n' ]);
5400fprintf(fid, ['Mask ' Param.Civ2.MaskFlag '\n' ]);
5401fprintf(fid, ['MaskName ' regexprep(Param.Civ2.MaskName,'\\','\\\\') '\n' ]);% for windows compatibility
5402fprintf(fid, ['ImageSize ' num2str(Param.Civ2.npx) ' ' num2str(Param.Civ2.npy) '\n' ]);   %VERIFIER CAS GENERAL ?
5403fprintf(fid, ['CorrelationBoxesSize ' num2str(Param.Civ2.Bx) ' ' num2str(Param.Civ2.By) '\n' ]);
5404fprintf(fid, ['SearchBoxeSize ' num2str(Param.Civ2.Bx) ' ' num2str(Param.Civ2.By) '\n']);
5405fprintf(fid, ['RO ' num2str(Param.Civ2.Rho) '\n']);
5406fprintf(fid, ['GridSpacing ' num2str(Param.Civ2.Dx) ' ' num2str(Param.Civ2.Dy) '\n']);
5407fprintf(fid, ['XX 1.0' '\n' ]);
5408fprintf(fid, ['Dt_TO ' num2str(Param.Civ2.Dt) ' ' num2str(Param.Civ2.T0) '\n' ]);
5409fprintf(fid, ['PixCmXY ' num2str(Param.Civ2.pxcmx) ' ' num2str(Param.Civ2.pxcmy) '\n' ]);
5410fprintf(fid, ['XX 1' '\n' ]);
5411fprintf(fid, 'ShiftXY 0 0\n');
5412fprintf(fid, ['Grid ' Param.Civ2.GridFlag '\n' ]);
5413fprintf(fid, ['GridName ' regexprep(Param.Civ2.GridName,'\\','\\\\') '\n']);
5414fprintf(fid, ['XX 85' '\n' ]);
5415fprintf(fid, ['XX 1.0' '\n' ]);
5416fprintf(fid, ['XX 1.0' '\n' ]);
5417fprintf(fid, ['Hart 1' '\n' ]);
5418fprintf(fid, ['DecimalShift ' num2str(Param.Civ2.CheckDecimal) '\n']);
5419fprintf(fid, ['Deformation ' num2str(Param.Civ2.CheckDeformation) '\n']);
5420fprintf(fid,  ['CorrelationMin 0' '\n' ]);
5421fprintf(fid,   ['IntensityMin 0' '\n' ]);
5422fprintf(fid,  ['SeuilImage n' '\n' ]);
5423fprintf(fid,   ['SeuilImageValues 0 4096' '\n' ]);
5424fprintf(fid,   ['ImageToUse ' Param.Civ2.term_a ' ' Param.Civ2.term_b '\n' ]); % VERIFIER ?
5425fprintf(fid, ['ImageUsedBefore ' regexprep(Param.Civ2.filename_nc1,'\\','\\\\') '\n']);
5426fclose(fid);
5427
5428if(isunix)
5429    cmd=['cp -f ' filename '.civ2.cmx ' filename '.cmx\n'...
5430        Param.xml.Civ2Bin ' -f ' filename  '.cmx >' filename '.civ2.log \n '... % redirect standard output to the log file, the result file is named [filename '.nc'] by CIVx
5431        'rm ' filename '.cmx \n'];%rename .cmx as .checkciv2.cmx, the result file is named [filename '.nc'] by CIVx
5432else
5433    filename=regexprep(filename,'\\','\\\\');
5434    cmd=['copy /Y "' filename '.civ2.cmx" "' filename '.cmx" \n'...
5435        '"' regexprep(Param.xml.Civ2Bin,'\\','\\\\') '" -f "' filename  '.cmx" >"' filename '.civ2.log" \n'...
5436        'del "' filename '.cmx" \n'];
5437end
5438
Note: See TracBrowser for help on using the repository browser.