source: trunk/src/civ.m @ 276

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

further cleaning after new GUI civ: rationalisation of check_Grid and txt_gridName
records the path of uvmat, series, civ, by addpath

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