source: trunk/src/civ.m @ 285

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

fix1 now OK

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