source: trunk/src/civ.m @ 282

Last change on this file since 282 was 282, checked in by sommeria, 12 years ago

further cleaning after new GUI civ. A general function read_GUI to read parameters from a GUI is used in civ

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