source: trunk/src/civ.m @ 273

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

GUI civ renovated with panels

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