source: trunk/src/civ.m @ 297

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

oar-dispatch mode debuggued

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