Home > . > civ.m

civ

PURPOSE ^

'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)

SYNOPSIS ^

function varargout = civ(varargin)

DESCRIPTION ^

'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)   
------------------------------------------------------------------------
  provides an interface for the software CIVx
 function varargout = civ(varargin)
 provides an interface for the software CIVx

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     This file is part of the toolbox UVMAT. 
 
     UVMAT is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
     UVMAT is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License (file UVMAT/COPYING.txt) for more details.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 %'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)
0002 %------------------------------------------------------------------------
0003 %  provides an interface for the software CIVx
0004 % function varargout = civ(varargin)
0005 % provides an interface for the software CIVx
0006 %
0007 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0008 %  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
0009 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0010 %     This file is part of the toolbox UVMAT.
0011 %
0012 %     UVMAT is free software; you can redistribute it and/or modify
0013 %     it under the terms of the GNU General Public License as published by
0014 %     the Free Software Foundation; either version 2 of the License, or
0015 %     (at your option) any later version.
0016 %
0017 %     UVMAT is distributed in the hope that it will be useful,
0018 %     but WITHOUT ANY WARRANTY; without even the implied warranty of
0019 %     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0020 %     GNU General Public License (file UVMAT/COPYING.txt) for more details.
0021 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0022 function varargout = civ(varargin)
0023 
0024 % Last Modified by GUIDE v2.5 29-Oct-2009 19:05:39
0025 % Begin initialization code - DO NOT EDIT
0026 gui_Singleton = 1;
0027 gui_State = struct('gui_Name',       mfilename, ...
0028                    'gui_Singleton',  gui_Singleton, ...
0029                    'gui_OpeningFcn', @civ_OpeningFcn, ...
0030                    'gui_OutputFcn',  @civ_OutputFcn, ...
0031                    'gui_LayoutFcn',  [] , ...
0032                    'gui_Callback',   []);
0033 if nargin && ischar(varargin{1})
0034     gui_State.gui_Callback = str2func(varargin{1});
0035 end
0036 
0037 if nargout
0038     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0039 else
0040     gui_mainfcn(gui_State, varargin{:});
0041 end
0042 % End initialization code - DO NOT EDIT
0043 
0044 %--------------------------------------------------------------------------
0045 % --- Executes just before civ is made visible.
0046 %--------------------------------------------------------------------------
0047 function civ_OpeningFcn(hObject, eventdata, handles, varargin)
0048 % This function has no output args, see OutputFcn.
0049 % hObject    handle to figure
0050 % eventdata  reserved - to be defined in a future version of MATLAB
0051 % handles    structure with handles and user data (see GUIDATA)
0052 % varargin   command line arguments to civ (see VARARGIN)
0053 global test_batch patch_new_exe%=1 if patch processing available
0054 %filebase: root name
0055 %nom_type: nomencalture used ('png_old','_i_j'...)
0056 %list of field numbers to process
0057 %subdir: subdirectory of the opened netcdf file
0058 %ind_opening: operation number advised for beginning (1=civ1,2=fix1,3=patch1,4=civ2,5=fix2,6=patch2),
0059 %ind_a_opening ind_b_opening chosen pair from the opened netcdf file
0060 % Choose default command line output for civ
0061 handles.output = hObject;
0062 % Update handles structure
0063 guidata(hObject, handles);
0064 
0065 %default initial parameters
0066 filebase=''; % root file name ('filebase'.civ)
0067 nom_type=[]; % nomenclature type
0068 ext=[];
0069 testall=0; 
0070 browse=[];
0071 
0072 %load the initial parameters if the interface is started from uvmat
0073 if ~isempty(varargin)% the interface is opened from uvmat
0074     varcell=varargin{1};
0075     filebase=varcell{1};
0076     nom_type_read=varcell{2};
0077     num1=varcell{3};
0078     num2=varcell{4};
0079     num_a=varcell{5};
0080     num_b=varcell{6};
0081     subdir=varcell{7};
0082     ind_opening=varcell{8};
0083     ext=varcell{11};
0084 else
0085     num1=1; % set of field i numbers
0086     num2=2; % set of field i numbers
0087     num_a=1; % set of field j numbers (fields a)
0088     num_b=1; % second set of field j numbers (fields b)
0089     subdir='A'; % subdir for the netcdf result files
0090     ind_opening=1; % proposed operation number (1=civ1,2=fix1,3=patch1,4=civ2,5=fix2,6=patch2)
0091 end
0092 
0093 if exist('ext','var') && length(ext)>1 && (~isempty(imformats(ext([2:end])))|...
0094                        isequal(ext,'.avi')||isequal(ext,'.AVI'));%if an image file has been opened by uvmat
0095         set(handles.ext_ima,'String',ext)
0096         if exist('nom_type_read','var')
0097             browse.nom_type_ima=nom_type_read; % the image nomenclature is stored
0098         end
0099 elseif isequal(ext,'.nc')
0100     if exist('nom_type_read','var')
0101         browse.nom_type_nc=nom_type_read;% the netcdf  nomenclature is stored
0102     end
0103 end
0104 set(handles.displ_filebase,'String',filebase);
0105 set(handles.ImaDoc,'UserData',testall);
0106 set(handles.ImaDoc,'String',ext)
0107 
0108 
0109 % set(handles.ImaDoc,'String',ext)
0110   
0111 %read names of the .exe file to adjust the interface according to
0112 %available prog
0113 %read names of the .exe file
0114 path_uvmat=which('uvmat');% check the path detected for source file uvmat
0115 path_UVMAT=fileparts(path_uvmat); %path to UVMAT
0116 if isunix
0117     syst='LINUX'
0118     %fid = fopen(fullfile(path_UVMAT,'PARAM_LINUX.txt'),'r');%open the file with civ binary names
0119     xmlfile=fullfile(path_UVMAT,'PARAM_LINUX.xml');
0120     display(xmlfile)
0121     if exist(xmlfile,'file')
0122         t=xmltree(xmlfile);
0123         sparam=convert(t);
0124     end
0125 else
0126     syst='WIN'
0127     %fid = fopen(fullfile(path_UVMAT,'PARAM_WIN.txt'),'r');%open the file with civ binary names
0128     xmlfile=fullfile(path_UVMAT,'PARAM_WIN.xml');
0129     if exist(xmlfile,'file')
0130         t=xmltree(xmlfile);
0131         sparam=convert(t);
0132     end
0133 end
0134    
0135 patch_new_exe='';
0136 % todo_patch='';
0137 sge=0;
0138 
0139 if isfield(sparam,'PatchNew_exe')
0140     patch_new_exe=sparam.PatchNew_exe;
0141 end
0142 if isfield(sparam,'Todo_path')
0143     todo_path=sparam.Todo_path
0144 end
0145 if isfield(sparam,'SGE')
0146     sge=str2num(sparam.SGE);
0147 end   
0148 name_todo=fullfile(todo_path,'TODO.txt');
0149 display(name_todo)
0150 test_batch=1;
0151 if ~sge
0152 if isequal(todo_path,'') || isequal(todo_path,[])
0153     display(['no batch distributed processing available:file path TODO.txt not defined in UVMAT/PARAM_' syst])
0154     test_batch=0;
0155 end
0156 if exist(name_todo,'file')~=2 
0157     msgbox_uvmat('WARNING',['no batch distributed processing available, queue file ' name_todo ' absent']);
0158   %  test_batch=0;  % Problems to detect file on linux/nfs filesystems
0159 end
0160 end
0161 
0162 
0163 if test_batch==0
0164     set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])% put the BATCH button in grey (unactivated)
0165 end
0166 
0167 set(handles.subdir_civ1,'String',subdir)%default subdir on which uvmat was working
0168 set(handles.subdir_civ2,'String',subdir)%default subdir on which uvmat was working
0169 
0170 %initiate advised operations
0171 if isequal(ind_opening,[])
0172     ind_opening=1; % default
0173 end
0174 % set default operation options
0175     enable_civ1(handles,'off')
0176     enable_civ2(handles,'off')
0177     enable_pair1(handles,'on')
0178     enable_fix1(handles,'off')
0179     desable_patch1(handles)
0180     desable_fix2(handles)
0181     desable_patch2(handles)
0182     set(handles.CIV1,'Value',0)
0183     set(handles.FIX1,'Value',0)
0184     set(handles.PATCH1,'Value',0)
0185     set(handles.CIV2,'Value',0)
0186     set(handles.FIX2,'Value',0)
0187     set(handles.PATCH2,'Value',0)
0188     set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
0189 if isequal(ind_opening,1)
0190     set(handles.CIV1,'Value',1)
0191     enable_civ1(handles,'on')
0192 elseif isequal(ind_opening,2)
0193     set(handles.FIX1,'Value',1)
0194     enable_fix1(handles,'on')
0195 elseif isequal(ind_opening,3)
0196     set(handles.PATCH1,'Value',1)
0197     enable_patch1(handles)
0198 elseif isequal(ind_opening,4)
0199     set(handles.CIV2,'Value',1)
0200     enable_civ2(handles,1)
0201 elseif isequal(ind_opening,5)
0202     set(handles.FIX2,'Value',1)
0203     enable_fix2(handles)
0204      set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
0205      set(handles.list_pair_civ2,'Enable','On')
0206      set(handles.list_pair_civ2,'Enable','On')
0207      enable_pair1(handles,'off')
0208 elseif isequal(ind_opening,6)
0209     set(handles.PATCH2,'Value',1)
0210     enable_patch2(handles)
0211     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
0212     set(handles.list_pair_civ2,'Enable','On')
0213     enable_pair1(handles,'off')
0214 end
0215 
0216 % set the range of fields (1:1 by default) and selected pair
0217 if isempty(num2)|isequal(num2,num1)
0218     num_ref_i=num1;
0219 else
0220     num_ref_i=floor((num1+num2)/2);
0221     browse.incr_pair(1)=num2-num1;
0222     browse.incr_pair(2)=0;
0223 end
0224 if isempty(num_b)|isequal(num_a,num_b)
0225     if isempty(num_a)
0226         num_ref_j=1;
0227     else
0228         num_ref_j=num_a;
0229     end
0230 else
0231     num_ref_j=floor((num_a+num_b)/2);
0232     browse.incr_pair(2)=num_b-num_a; 
0233 end
0234 set(handles.first_i,'String',num2str(num_ref_i));
0235 set(handles.last_i,'String',num2str(num_ref_i));
0236 set(handles.first_j,'String',num2str(num_ref_j));
0237 set(handles.last_j,'String',num2str(num_ref_j));
0238 set(handles.ref_i,'String',num2str(num_ref_i));
0239 set(handles.ref_j,'String',num2str(num_ref_j));
0240 set(handles.browse_root,'UserData',browse);
0241 if ~isempty(varargin)% the interface is opened from uvmat
0242     displ_filebase_Callback(hObject, eventdata, handles); 
0243 end
0244 
0245 set(handles.waitbar_1,'Position',[0.946 0.877 0.03 0.001])
0246 set(handles.waitbar_patch1,'Position',[0.946 0.626 0.03 0.001])
0247 set(handles.waitbar_civ2,'Position',[0.946 0.406 0.03 0.001])
0248 set(handles.waitbar_patch2,'Position',[0.946 0.187 0.03 0.001])
0249 
0250 
0251 %--------------------------------------------------------------
0252 % --- Outputs from this function are returned to the command line.
0253 %-----------------------------------------------------------------
0254 function varargout = civ_OutputFcn(hObject, eventdata, handles)
0255 % varargout  cell array for returning output args (see VARARGOUT);
0256 % hObject    handle to figure
0257 % eventdata  reserved - to be defined in a future version of MATLAB
0258 % handles    structure with handles and user data (see GUIDATA)
0259 % Get default command line output from handles structure
0260 varargout{1} = handles.output;
0261 
0262 %------------------------------------------------------------------
0263 % --- Executes on button press in browse_root.
0264 function browse_root_Callback(hObject, eventdata, handles)
0265 %get the input file properties
0266 filebase=get(handles.displ_filebase,'String');
0267 oldfile=''; %default
0268 if isempty(filebase)|isequal(filebase,'')%loads the previously stored file name and set it as default in the file_input box
0269      dir_perso=prefdir;
0270      profil_perso=fullfile(dir_perso,'uvmat_perso.mat');
0271      display(profil_perso)
0272      if exist(profil_perso,'file')
0273           h=load (profil_perso);
0274          if isfield(h,'filebase')&& ischar(h.filebase)
0275                  oldfile=h.filebase;
0276          end
0277          if isfield(h,'RootPath') && ischar(h.RootPath)
0278                  oldfile=h.RootPath;
0279          end
0280      end
0281  else
0282      oldfile=filebase;
0283  end
0284 testall=get(handles.ImaDoc,'UserData');
0285 ind_opening=1;%default
0286 browse.incr_pair=[0 0]; %default
0287 if testall 
0288     menu={'*.*', 'All Files (*.*)'; '*.xml; *.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
0289         '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc','.nc files'};
0290 else % menu selecting only .civ or .avi files
0291 menu={'*.xml;*.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
0292         '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc', '.nc files';...
0293         '*.*', 'All Files (*.*)'};
0294 end
0295 [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
0296 fileinput=[PathName FileName];%complete file name
0297 sizf=size(fileinput);
0298 if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
0299 [path,name,ext]=fileparts(fileinput);
0300 testeditxml=0;
0301 if isequal(ext,'.xml')
0302     testeditxml=1;
0303     t_browse=xmltree(fileinput);
0304     head_element=get(t_browse,1);
0305     if isfield(head_element,'name')& isequal(head_element.name,'ImaDoc')
0306         testeditxml=0;
0307     end
0308 end
0309 if testeditxml==1 | isequal(ext,'.xls')
0310    heditxml=editxml({fileinput});
0311    set(heditxml,'Tag','browser')
0312    waitfor(heditxml,'Tag','idle')
0313    if ~ishandle(heditxml)
0314        return
0315    end
0316    attr=findobj(get(heditxml,'children'),'Tag','CurrentAttributes');
0317    set(handles.browse,'UserData',fileinput)% store for future opening with browser
0318    fileinput=get(attr,'UserData');
0319    if ~exist(fileinput,'file')
0320        return
0321    end
0322 end
0323 
0324 [RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileinput);
0325 filebase=fullfile(RootPath,RootFile);
0326 if isequal(get(handles.compare,'Value'),1)
0327     browse=[];%initialisation
0328 else
0329     browse=get(handles.browse_root,'UserData');
0330 end
0331 if length(ext)>1 & (~isempty(imformats(ext([2:end])))|...
0332                        isequal(ext,'.avi')|isequal(ext,'.AVI'));%if an image file has been opened by uvmat
0333     set(handles.ext_ima,'String',ext)
0334     browse.nom_type_ima=nom_type;
0335     browse.field_count=str2num(field_count);
0336     A=imread(fileinput);
0337     npxy=size(A)
0338     set(handles.ext_ima,'UserData',npxy)
0339 end
0340 set(handles.ImaDoc,'String',ext);
0341 
0342 %%%%% read the state of the selected netcdf file to advise default operation
0343 if isequal(ext,'.nc')
0344     browse.nom_type_nc=nom_type;
0345     ind_opening=2;% propose 'fix' as the default option
0346     Data=nc2struct(fileinput,[]);
0347     if isfield(Data,'absolut_time_T0')%test for civx files
0348         if isfield(Data,'fix') && isequal(Data.fix,1)
0349             ind_opening=3;
0350         end
0351         if isfield(Data,'patch') && isequal(Data.patch,1)
0352             ind_opening=4;
0353         end
0354         if isfield(Data,'civ2') && isequal(Data.civ2,1)
0355             ind_opening=5;
0356         end
0357         if isfield(Data,'fix2') && isequal(Data.fix2,1)
0358             ind_opening=6;
0359         end
0360         testciv=1; %TO SUPPRESS WITH NEW VERSION OF CIVX
0361     else
0362         ind_opening=3; %GUI used only for patch
0363         testciv=0;
0364     end
0365     set(handles.subdir_civ1,'String',subdir);%set the default subdir directories for installing the .nc results
0366     set(handles.subdir_civ2,'String',subdir);
0367     browse.testciv=testciv;
0368     browse.ind_opening=ind_opening;
0369 end
0370 set(handles.displ_filebase,'String',filebase);
0371 set(handles.ImaDoc,'String',ext);
0372 if ~isempty(str2num(field_count))
0373     ref_i=str2num(field_count);
0374     if ~isempty(str2num(str2))
0375         ref_i=floor((ref_i+str2num(str2))/2);% reference image number corresponding to the file
0376         browse.incr_pair(1)=str2num(str2)-str2num(field_count);
0377         browse.incr_pair(2)=0;
0378     end
0379     set(handles.first_i,'String',num2str(ref_i));
0380     set(handles.last_i,'String',num2str(ref_i));
0381     set(handles.ref_i,'String',num2str(ref_i)); 
0382 end
0383 if isempty(str2num(str_a))
0384     set(handles.ref_j,'String','1');
0385 else
0386     ref_j=str2num(str_a);
0387     if ~isempty(str2num(str_b))
0388         ref_j=floor((str2num(str_a)+str2num(str_b))/2);
0389         browse.incr_pair(2)=str2num(str_b)-str2num(str_a); 
0390     end
0391     set(handles.first_j,'String',num2str(ref_j));
0392     set(handles.last_j,'String',num2str(ref_j));
0393     set(handles.ref_j,'String',num2str(ref_j)); 
0394 end
0395 
0396 % set default operation options
0397 enable_civ1(handles,'off')
0398 enable_civ2(handles,'off')
0399 enable_pair1(handles,'on')
0400 enable_fix1(handles,'off')
0401 desable_patch1(handles)
0402 desable_fix2(handles)
0403 desable_patch2(handles)
0404 set(handles.CIV1,'Value',0)
0405 set(handles.FIX1,'Value',0)
0406 set(handles.PATCH1,'Value',0)
0407 set(handles.CIV2,'Value',0)
0408 set(handles.FIX2,'Value',0)
0409 set(handles.PATCH2,'Value',0)
0410 set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
0411 if isequal(ind_opening,1)
0412     set(handles.CIV1,'Value',1)
0413     enable_civ1(handles,'on')
0414 elseif isequal(ind_opening,2)
0415     set(handles.FIX1,'Value',1)
0416     enable_fix1(handles,'on')
0417 elseif isequal(ind_opening,3)
0418     set(handles.PATCH1,'Value',1)
0419     enable_patch1(handles)
0420 elseif isequal(ind_opening,4)
0421     set(handles.CIV2,'Value',1)
0422     enable_civ2(handles,1)
0423 elseif isequal(ind_opening,5)
0424     enable_pair1(handles,'off')
0425     set(handles.FIX2,'Value',1)
0426     enable_fix2(handles)
0427      set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
0428      set(handles.list_pair_civ2,'Enable','On')
0429      set(handles.list_pair_civ2,'Enable','On')
0430 elseif isequal(ind_opening,6)
0431     enable_pair1(handles,'off')
0432     set(handles.PATCH2,'Value',1)
0433     enable_patch2(handles)
0434     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
0435     set(handles.list_pair_civ2,'Enable','On')
0436 end
0437 set(handles.browse_root,'UserData',browse);% store information from browser
0438 testall=isequal(menu(filtindex,1),{'*.*'});
0439 set(handles.ImaDoc,'UserData',testall);
0440 
0441 displ_filebase_Callback(hObject, eventdata, handles);
0442 
0443 %------------------------------------------------
0444 
0445 function ImaDoc_Callback(hObject, eventdata, handles)
0446 displ_filebase_Callback(hObject, eventdata, handles)
0447 
0448 %--------------------------------------------------------------
0449 %function activated when a new filebase (image series) is introduced
0450 %------------------------------------------------------------
0451 function displ_filebase_Callback(hObject, eventdata, handles)
0452 
0453 global test_batch
0454 %set(gcf,'Pointer','watch')
0455 set(handles.compare,'Visible','on')
0456 ext_ima=get(handles.ext_ima,'String'); 
0457 nom_type_ima=[];%default
0458 field_count=1;%default
0459 nom_type_nc=[];
0460 npx=[];%default
0461 npy=[];
0462 time=[];
0463 TimeUnit=[]; %default
0464 CoordUnit=[];%default
0465 pxcmx_search=[];%default
0466 pxcmy_search=[];%default
0467 filebase=get(handles.displ_filebase,'String');
0468 
0469 ext=get(handles.ImaDoc,'String');
0470 browse=get(handles.browse_root,'UserData');%default
0471 if ~isempty(browse) 
0472     if isfield(browse,'nom_type_ima')
0473         nom_type_ima=browse.nom_type_ima;
0474     end
0475     if isfield(browse,'field_count')
0476         field_count=browse.field_count;
0477     end
0478 end
0479 
0480 %default first_i and j and increments
0481 first_i=str2num(get(handles.first_i,'String'));%value possibly set by uvmat_Opening
0482 if isempty(first_i)| first_i < 1
0483     first_i=1; %default first_i
0484 end
0485 last_i=str2num(get(handles.last_i,'String'));
0486 if isempty(last_i)| last_i < first_i
0487     last_i=first_i;  %default last_i
0488 end
0489 first_j=str2num(get(handles.first_j,'String'));
0490 if isempty(first_j)| first_j < 1
0491     first_j=1; %default first_j
0492 end
0493 last_j=str2num(get(handles.last_j,'String'));
0494 if isempty(last_j)| last_j < first_j
0495     last_j=first_j; %default last_j
0496 end
0497 incr_i=str2num(get(handles.incr_i,'String'));
0498 if isempty(incr_i) | incr_i < 1;
0499     set(handles.incr_i,'String','1') %default incr_i
0500 end
0501 incr_j=str2num(get(handles.incr_j,'String'));
0502 if isempty(incr_j) | incr_j < 1;
0503     set(handles.incr_j,'String','1') %default incr_j
0504 end
0505 dt=[];%default
0506 testmode=0;%default
0507 nbfield=[]; %default
0508 nburst=[];%default
0509 pxcmx=1;
0510 pxcmy=1; 
0511 
0512  %look for an image documentation file
0513 if ~isequal(ext,'.xml') && ~ isequal(ext,'.civ')&& ~ isequal(ext,'.avi')&&~ isequal(ext,'.AVI')
0514     if exist([filebase '.xml'],'file')
0515          ext='.xml';
0516     elseif exist([filebase '.civxml'],'file')
0517          ext='.civxml';
0518     elseif exist([filebase '.civ'],'file')
0519          ext='.civ';
0520     elseif exist([filebase '.avi'],'file') 
0521          ext='.avi';
0522     elseif exist([filebase '.AVI'],'file')
0523          ext='.AVI';
0524     end
0525     set(handles.ImaDoc,'String',ext)
0526 end
0527 
0528 %%%%%%%%   read image documentation file  %%%%%%%%%%%%%%%%%%%%%%%%%%%
0529     mode=''; %default
0530     %read the image documentation file if found
0531 if isequal(ext,'.civxml') | isequal(ext,'.xml')| isequal(ext,'.civ')
0532     set(handles.ref_i,'Visible','On')%use a reference index
0533     set(handles.ref_j,'Visible','On')
0534     set(handles.dt,'Visible','Off')
0535     set(handles.dt_text,'String','ref. ind.')
0536 elseif isequal(ext,'.avi') | isequal(ext,'.AVI')
0537     set(handles.ref_j,'Visible','Off')
0538     set(handles.dt,'Visible','Off')
0539     set(handles.dt_text,'String','ref. ind.')
0540 else
0541     set(handles.ref_i,'Visible','Off')
0542     set(handles.ref_j,'Visible','Off')
0543     set(handles.dt,'Visible','On')
0544     set(handles.dt_text,'String','dt(ms)=')
0545 end
0546 if isequal(ext,'.civxml')%TO ABANDON
0547     [nbfield,nburst,time]=read_civxml([filebase '.civxml']);
0548     mode='pair j1-j2';
0549     if isempty(nom_type_ima)% dtermine types by default if not already selected by browser or uvmat
0550             nom_type_ima='_i_j';
0551     end
0552 elseif isequal(ext,'.xml')
0553     [XmlData,warntext]=imadoc2struct([filebase '.xml']);
0554     if isfield(XmlData,'Time')
0555         time=XmlData.Time;
0556         nbfield=size(XmlData.Time,1);
0557         nburst=size(XmlData.Time,2);
0558     end
0559     ext_ima_read=[];
0560     nom_type_read=[];
0561     if isfield(XmlData,'Heading')&&isfield(XmlData.Heading','ImageName')
0562         [PP,FF,fc,str2,str_a,str_b,ext_ima_read,nom_type_read]=name2display(XmlData.Heading.ImageName);
0563     end
0564     if isfield(XmlData,'Camera')
0565         if isfield(XmlData.Camera,'TimeUnit')
0566             TimeUnit=XmlData.Camera.TimeUnit;
0567         end
0568         if isfield(XmlData.Camera,'ImageSize')
0569            ImageSize=XmlData.Camera.ImageSize;
0570            if ~isempty(ImageSize)&& ~isempty(ImageSize)
0571                xindex=findstr(ImageSize,'x');
0572                if length(xindex)>=2
0573                     npx=str2num(ImageSize(1:xindex(1)-1));
0574                     npy=str2num(ImageSize(xindex(1)+1:xindex(2)-1));
0575                end
0576            end
0577        end
0578     end
0579     pxcmx_search=1;
0580     pxcmy_search=1;
0581     if isfield(XmlData,'GeometryCalib')
0582         tsai=XmlData.GeometryCalib;
0583         if isfield(tsai,'f') & isfield(tsai,'Tz') & isfield(tsai,'dpx') & isfield(tsai,'dpy')& isfield(tsai,'R') 
0584              rot2D=tsai.R([1:2],[1,2]);
0585              pxcmx_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpx);
0586              pxcmy_search=tsai.f * sqrt(det(rot2D))/(tsai.Tz*tsai.dpy);           
0587         end
0588         if isfield(tsai,'CoordUnit') 
0589              CoordUnit=tsai.CoordUnit;
0590         end
0591     end                
0592     if ~isempty(ext_ima_read) && ~isempty(nom_type_read)
0593         if isempty(ext_ima)
0594              ext_ima=ext_ima_read;% define image extension from the xml file if an image has not been opened previously
0595         else   %keep the image extension
0596             if  ~strcmp(ext_ima_read,ext_ima)
0597               msgbox_uvmat('WARNING',['FirtsImage extension ' ext_ima_read ' announced in the xml file inconsistent with the selected image'])
0598             end
0599         end
0600         nom_type_ima=nom_type_read;
0601     end
0602 elseif isequal(ext,'.civ')% case of .civ image documentation file
0603         [error,time,TimeUnit,mode,npx,npy]=read_imatext([filebase '.civ']);
0604         if error==2, warndlg(['no file ' filebase '.civ']);
0605         elseif error==1, warndlg('inconsistent number of fields in the .civ file');
0606         end
0607 %         ext_ima='.png';
0608         nom_type_ima='png_old';       
0609 elseif isequal(lower(ext),'.avi') 
0610         nom_type_ima='*';
0611         ext_ima=ext;
0612         set(handles.mode,'String',{'series(Di)'})
0613         dt=0.04;%default
0614         if exist([filebase ext],'file')==2
0615             info=aviinfo([filebase ext]);%read infos on the avi movie
0616             dt=1/info.FramesPerSecond;%time interval between successive frames
0617             nbfield=info.NumFrames;%number of frames
0618         end
0619         time=(dt*[0:nbfield-1])';%list of image times
0620         set(handles.dt,'String',num2str(dt*1000));%store the time interval between successive images
0621 end
0622 if isempty(time)
0623     set(handles.ImaDoc,'String',''); %xml file not used for timing
0624 end
0625      % no image documentation file found: look for a series of existing images or .nc files
0626 if isempty(time) && ~isequal(ext,'.nc') 
0627         subdir=get(handles.subdir_civ1,'String');
0628         incr_pair=[0 0];%default
0629         if isfield(browse,'incr_pair')
0630                 incr_pair=browse.incr_pair;
0631         end
0632         nbdetect=0;%test of detected images
0633         field_i=field_count;
0634         idetect=1;
0635         while idetect==1 %look for the maximum file number in the series
0636                 field_i=field_i+1;
0637                 imagename=name_generator(filebase,field_i,1,ext_ima,nom_type_ima);
0638                 if strcmp(nom_type_ima,'none')||strcmp(nom_type_ima,'')
0639                    idetect=0; %stop if the same image is repeated (if nom_type='none')
0640                    nbdetect=1;
0641                 else
0642                     idetect=(exist(imagename,'file')==2);
0643                 end
0644                 %SEE CASE OF NETCDF FILES
0645                 nbdetect=nbdetect+(exist(imagename,'file')==2);
0646         end
0647         nb_field=field_i;% last detected field number
0648         field_i=field_count;%look for the minimum file number in the series
0649         idetect=1;
0650         while idetect==1 
0651                     field_i=field_i-1;
0652                     imagename=name_generator(filebase,field_i,1,ext_ima,nom_type_ima);
0653                     if isequal(nom_type_ima,'none')||strcmp(nom_type_ima,'')
0654                         idetect=0; %stop if the same image is repeted (if nom_type='none')
0655                         nbdetect=1;
0656                     else
0657                         idetect=(exist(imagename,'file')==2);
0658                     end
0659                     nbdetect=nbdetect+idetect;
0660         end
0661         first_i=max(field_i+1,1);
0662             %determine the set of times and possible intervals for CIV
0663         dt=(1/1000)*str2num(get(handles.dt,'String'));
0664         time=(dt*[0:nb_field-1])';
0665         set(handles.mode,'String',{'series(Di)'})
0666 end
0667 if isequal(nom_type_ima,'none')% no file numbering used
0668   first_i=1; 
0669   last_i=1;
0670    first_j=1;
0671   last_j=1;
0672 end
0673 if exist('time','var')
0674     if size(time,1)+size(time,2)>=3 % if there are at least two time values to define dt
0675         nbfield=size(time,1);
0676         nbfield2=size(time,2);
0677         set(handles.displ_filebase,'UserData',time); %store the set of times
0678         if isempty(TimeUnit)
0679             set(handles.dt_unit,'String','e-03');
0680             set(handles.TimeUnit,'String','');
0681         else
0682             set(handles.dt_unit,'String',['m' TimeUnit]);
0683             set(handles.TimeUnit,'String',TimeUnit);
0684         end
0685         set(handles.nb_field,'String',num2str(nbfield));
0686         set(handles.nb_field2,'String',num2str(nbfield2));
0687     end
0688 end
0689 set(handles.CoordUnit,'String',[CoordUnit '/'])
0690 set(handles.calcul_search,'UserData',[pxcmx_search pxcmy_search]);
0691 % npxy=[npy npx];
0692 set(handles.ext_ima,'String',ext_ima)
0693 set(handles.first_i,'String',num2str(first_i));
0694 set(handles.last_i,'String',num2str(last_i));%
0695 set(handles.first_j,'String',num2str(first_j));
0696 set(handles.last_j,'String',num2str(last_j));%
0697 browse.nom_type_ima=nom_type_ima;
0698 set(handles.browse_root,'UserData',browse)% store the nomenclature type
0699 
0700         %%%%%%%%%%%  set the menus of image pairs and default selection for civ   %%%%%%%%%%%%%%%%%%%
0701 if isequal(nom_type_ima,'_i')| isequal(nom_type_nc,'_i1-i2')|~exist('nbfield2','var')|(nbfield2==1)
0702     set(handles.mode,'String',{'series(Di)'})
0703     set(handles.mode,'Value',1)
0704 elseif isequal(nom_type_ima,'png_old')|isequal(nom_type_nc,'netc_old')
0705     set(handles.mode,'String',{'pair j1-j2'})
0706     set(handles.mode,'Value',1)
0707 elseif (nbfield==1)% simple series in j
0708     set(handles.mode,'String',{'series(Dj)'})
0709     set(handles.mode,'Value',1)
0710 else
0711     set(handles.mode,'String',{'pair j1-j2';'series(Dj)';'series(Di)'})%multiple choice
0712     if isequal(mode,'volume')
0713         set(handles.mode,'Value',3)
0714     elseif nbfield2 <= 5
0715         set(handles.mode,'Value',1)% advice 'pair j1-j2' for small bursts
0716     else
0717         set(handles.mode,'Value',2)% advice series Dj for long bursts, not volume
0718     end
0719 end
0720 
0721 %update the subdir
0722 pathdir=fileparts(filebase);%path to the current xml file
0723 listot=dir(pathdir);
0724 idir=0;
0725 listdir={''};%default
0726 for ilist=1:length(listot)
0727     if listot(ilist).isdir
0728         name=listot(ilist).name;
0729         if ~isequal(name,'.') && ~isequal(name,'..')
0730             idir=idir+1;
0731             listdir{idir,1}=listot(ilist).name;
0732         end
0733     end
0734 end
0735 set(handles.list_subdir_civ1,'Value',1) 
0736 set(handles.list_subdir_civ2,'Value',1)
0737 set(handles.list_subdir_civ1,'String',['browse...';listdir])
0738 set(handles.list_subdir_civ2,'String',['browse...';listdir])
0739 %check wether the current subdir exists:
0740 subdir_civ1=get(handles.subdir_civ1,'String');
0741 subdir_civ2=get(handles.subdir_civ2,'String');
0742 
0743 mode_Callback(hObject, eventdata, handles)  
0744 
0745 %%%%%% initialize waitbars and RUN button
0746 set(handles.waitbar_1,'Position',[0.946 0.876 0.03 0.001])
0747 set(handles.waitbar_patch1,'Position',[0.946 0.439 0.03 0.001])
0748 set(handles.waitbar_civ2,'Position',[0.946 0.219 0.03 0.001])
0749 set(handles.waitbar_patch2,'Position',[0.946 0.0 0.03 0.001])
0750 set(handles.RUN, 'Enable','On')
0751 set(handles.RUN,'BackgroundColor',[1 0 0])
0752 if isequal(test_batch,1)%if batch installation is available
0753     set(handles.BATCH, 'Enable','On')
0754     set(handles.BATCH,'BackgroundColor',[1 0 0])
0755 end
0756     
0757 %%%%% store the root input filename for future opening
0758 dir_perso=prefdir;
0759 profil_perso=fullfile(prefdir,'uvmat_perso.mat');
0760 RootPath=fileparts(filebase);
0761 if exist(profil_perso,'file')
0762     save (profil_perso,'RootPath','-append'); %store the root name for future opening of uvmat
0763 else
0764     txt=ver;
0765     Release=txt(1).Release;
0766     relnumb=str2num(Release(3:4));
0767     if relnumb >= 14
0768         save (profil_perso,'RootPath','-V6'); %store the root name for future opening of uvmat
0769     else
0770         save (profil_perso,'RootPath'); %store the root name for future opening of uvmat
0771     end
0772 end
0773 % save(profil_perso, 'filebase'); %store the root name for future opening of uvmat
0774 set(gcf,'Pointer','arrow')
0775 
0776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%????????????
0777 % --- Executes on button press in mode.
0778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0779 function mode_Callback(hObject, eventdata, handles)
0780 browse=get(handles.browse_root,'UserData');
0781 
0782 compare_list=get(handles.compare,'String');
0783 val=get(handles.compare,'Value');
0784 compare=compare_list{val};
0785 if strcmp(compare,'displacement')
0786     mode='displacement';
0787 else
0788     mode_list=get(handles.mode,'String');
0789     mode_value=get(handles.mode,'Value');
0790     mode=mode_list{mode_value};
0791 end
0792 displ_num=[];%default
0793 first_i=str2num(get(handles.first_i,'String'));
0794 % last_i=str2num(get(handles.last_i,'String'));
0795 time=get(handles.displ_filebase,'UserData'); %get the set of times
0796 siztime=size(time);
0797 nbfield=siztime(1);
0798 nbfield2=siztime(2);
0799 indchosen=1;  %%first pair selected by default
0800 if isequal(mode,'pair j1-j2')%| isequal(mode,'st_pair j1-j2')
0801     dt=1;
0802     displ='';
0803     index=0;
0804     numlist_a=[];
0805     numlist_B=[];
0806     %get all the time intervals in bursts
0807     displ_dt=1;%default
0808     nbfield2=min(nbfield2,10);%limitate the number of pairs to 10x10
0809 %     if nbfield2<2
0810 %         nbfield2=2,
0811 %     end
0812     for numod_a=1:nbfield2-1 %nbfield2 always >=2 for 'pair j1-j2' mode
0813         for numod_b=(numod_a+1):nbfield2
0814              index=index+1;
0815              numlist_a(index)=numod_a;
0816              numlist_b(index)=numod_b;
0817              if ~isempty(time)
0818                 dt(numod_a,numod_b)=time(first_i,numod_b)-time(first_i,numod_a);%first time interval dt
0819                 displ_dt(index)=dt(numod_a,numod_b);
0820              else
0821                  displ_dt(index)=1;
0822              end
0823          end
0824      end
0825      [dtsort,indsort]=sort(displ_dt);
0826      if ~isempty(numlist_a)
0827      displ_num(1,:)=numlist_a(indsort);
0828      displ_num(2,:)=numlist_b(indsort);
0829      end
0830      displ_num(3,:)=0;
0831      displ_num(4,:)=0;
0832      set(handles.jtext,'Visible','Off')
0833     set(handles.first_j,'Visible','Off')
0834     set(handles.last_j,'Visible','Off')
0835     set(handles.incr_j,'Visible','Off')
0836     set(handles.nb_field2,'Visible','Off')
0837     set(handles.ref_j,'Visible','Off')
0838 elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)')
0839      for index=1:min(nbfield2-1,200)
0840          displ_num(1,index)=-floor(index/2);
0841          displ_num(2,index)=ceil(index/2);
0842          displ_num(3,index)=0;
0843          displ_num(4,index)=0;
0844      end       
0845      set(handles.jtext,'Visible','On')
0846      set(handles.first_j,'Visible','On')
0847      set(handles.last_j,'Visible','On')
0848      set(handles.incr_j,'Visible','On')
0849      set(handles.nb_field2,'Visible','On')
0850      set(handles.ref_j,'Visible','On')
0851      if nbfield > 1
0852         set(handles.itext,'Visible','On')
0853         set(handles.first_i,'Visible','On')
0854         set(handles.last_i,'Visible','On')
0855         set(handles.incr_i,'Visible','On')
0856         set(handles.nb_field,'Visible','On')
0857         set(handles.ref_i,'Visible','On')
0858     else
0859         set(handles.itext,'Visible','Off')
0860         set(handles.first_i,'Visible','Off')
0861         set(handles.last_i,'Visible','Off')
0862         set(handles.incr_i,'Visible','Off')
0863         set(handles.nb_field,'Visible','Off')
0864         set(handles.ref_i,'Visible','Off')
0865     end
0866 elseif isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
0867      for index=1:200%min(nbfield-1,200)
0868          displ_num(1,index)=0;
0869          displ_num(2,index)=0;
0870          displ_num(3,index)=-floor(index/2);
0871          displ_num(4,index)=ceil(index/2);
0872      end
0873      set(handles.itext,'Visible','On')
0874      set(handles.first_i,'Visible','On')
0875      set(handles.last_i,'Visible','On')
0876      set(handles.incr_i,'Visible','On')
0877      set(handles.nb_field,'Visible','On')
0878      set(handles.ref_i,'Visible','On')
0879      if nbfield2 > 1
0880         set(handles.jtext,'Visible','On')
0881         set(handles.first_j,'Visible','On')
0882         set(handles.last_j,'Visible','On')
0883         set(handles.incr_j,'Visible','On')
0884         set(handles.nb_field2,'Visible','On')
0885         set(handles.ref_j,'Visible','On')
0886      else
0887         set(handles.jtext,'Visible','Off')
0888         set(handles.first_j,'Visible','Off')
0889         set(handles.last_j,'Visible','Off')
0890         set(handles.incr_j,'Visible','Off')
0891         set(handles.nb_field2,'Visible','Off')
0892         set(handles.ref_j,'Visible','Off')
0893     end
0894 elseif isequal(mode,'displacement')%the pairs have the same indices
0895      displ_num(1,1)=0; 
0896      displ_num(2,1)=0;
0897      displ_num(3,1)=0;
0898      displ_num(4,1)=0;
0899      if nbfield > 1
0900         set(handles.itext,'Visible','On')
0901         set(handles.first_i,'Visible','On')
0902         set(handles.last_i,'Visible','On')
0903         set(handles.incr_i,'Visible','On')
0904         set(handles.nb_field,'Visible','On')
0905         set(handles.ref_i,'Visible','On')
0906     else
0907         set(handles.itext,'Visible','Off')
0908         set(handles.first_i,'Visible','Off')
0909         set(handles.last_i,'Visible','Off')
0910         set(handles.incr_i,'Visible','Off')
0911         set(handles.nb_field,'Visible','Off')
0912         set(handles.ref_i,'Visible','Off')
0913     end
0914     if nbfield2 > 1
0915       set(handles.jtext,'Visible','On')
0916      set(handles.first_j,'Visible','On')
0917      set(handles.last_j,'Visible','On')
0918      set(handles.incr_j,'Visible','On')
0919      set(handles.nb_field2,'Visible','On')
0920      set(handles.ref_j,'Visible','On')
0921     else
0922         set(handles.jtext,'Visible','Off')
0923         set(handles.first_j,'Visible','Off')
0924         set(handles.last_j,'Visible','Off')
0925         set(handles.incr_j,'Visible','Off')
0926         set(handles.nb_field2,'Visible','Off')
0927         set(handles.ref_j,'Visible','Off')
0928     end
0929 end  
0930 set(handles.list_pair_civ1,'UserData',displ_num);
0931 find_netcpair_civ1(hObject, eventdata, handles)
0932 find_netcpair_civ2(hObject, eventdata, handles)
0933 
0934 %--------------------------------------------------------------
0935 % determine the menu for civ1 pairs depending on existing netcdf file at the middle of
0936 % the field series set by first_i, incr, last_i
0937 %----------------------------------------------------------------
0938 function find_netcpair_civ1(hObject, eventdata, handles)
0939 set(gcf,'Pointer','watch')
0940 %nomenclature types
0941 filebase=get(handles.displ_filebase,'String');
0942 [filepath,Nme,ext_dir]=fileparts(filebase);
0943 browse=get(handles.browse_root,'UserData');
0944 compare_list=get(handles.compare,'String');
0945 val=get(handles.compare,'Value');
0946 compare=compare_list{val};
0947 if strcmp(compare,'displacement')
0948     mode='displacement';
0949 else
0950     mode_list=get(handles.mode,'String');
0951     mode_value=get(handles.mode,'Value');
0952     mode=mode_list{mode_value};
0953 end
0954 
0955 % nomenclature type of the .nc files
0956 nom_type_ima=[];%default
0957 if isfield(browse,'nom_type_ima')
0958     nom_type_ima=browse.nom_type_ima;
0959 end
0960 
0961 %determine nom_type_nc:
0962 nom_type_nc=[];%default
0963 if isfield(browse,'nom_type_nc')
0964     nom_type_nc=browse.nom_type_nc;
0965 end
0966 if isempty(nom_type_nc)
0967      [nom_type_nc]=nomtype2pair(nom_type_ima,isequal(mode,'series(Di)'),isequal(mode,'series(Dj)'));
0968 end
0969 browse.nom_type_nc=nom_type_nc;
0970 set(handles.browse_root,'UserData',browse)
0971 
0972 %reads .nc subdirectoy and image numbers from the interface
0973 subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
0974 first_i=str2num(get(handles.first_i,'String'));
0975 last_i=str2num(get(handles.last_i,'String'));
0976 incr=str2num(get(handles.incr_i,'String'));
0977 num1=first_i:incr:last_i;
0978 if isempty(num1)
0979     set(handles.list_pair_civ1,'String',{''});
0980     return
0981 end
0982 ref_i=str2num(get(handles.ref_i,'String'));
0983 if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
0984     ref_j=0;
0985 else
0986     ref_j=str2num(get(handles.ref_j,'String'));
0987 end
0988 if isequal(get(handles.dt_text,'String'),'dt(ms)=')%simple series(Di) with equal interval
0989     ref_i=floor((first_i+last_i)/2);
0990     ref_j=1;
0991 end
0992 time=get(handles.displ_filebase,'UserData');%get the set of times
0993 if isempty(time)
0994     time=[0 1];
0995 end 
0996 dt_unit=str2num(get(handles.dt,'String'));% used when there is no image documentation file
0997 displ_num=get(handles.list_pair_civ1,'UserData');
0998 
0999 %eliminate the first pairs inconsistent with the position
1000  if isempty(displ_num)
1001      nbpair=0;
1002  else
1003     nbpair=length(displ_num(1,:));%nbre of displayed pairs
1004     if  isequal(mode,'series(Di)')  %| isequal(mode,'st_series(Di)')
1005         nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
1006     elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1007         nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
1008     end
1009  end
1010 nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
1011 
1012 %look for existing processed pairs involving the field at the middle of the series if civ1 will not
1013 % be performed, while the result is needed for next steps.
1014 displ_pair={''};
1015 select=ones(size(1:nbpair));%default =1 for numbers of displayed pairs
1016 testpair=0;
1017 if get(handles.CIV1,'Value')==0 %
1018     dirname=fullfile(filepath,subdir_civ1,ext_dir);
1019     if ~exist(fullfile(filepath,subdir_civ1,ext_dir),'dir') 
1020          msgbox_uvmat('ERROR',['no civ1 file available: subdirectory ' subdir_civ1 ' does not exist']);
1021          set(handles.list_pair_civ1,'String',{});
1022          return
1023     end
1024     for ipair=1:nbpair   
1025         filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1026         ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1027         select(ipair)=exist(filename,'file')==2;
1028     end
1029     if ~exist('select','var') || isequal(select,zeros(size(1:nbpair)))
1030         if isfield(browse,'incr_pair') 
1031             num_i1=ref_i-floor(browse.incr_pair(1)/2);
1032             num_i2=ref_i+ceil(browse.incr_pair(1)/2);
1033             num_j1=ref_j-floor(browse.incr_pair(2)/2);
1034             num_j2=ref_j+ceil(browse.incr_pair(2)/2);
1035             filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ1);
1036             select(1)=exist(filename,'file')==2;
1037             testpair=1;
1038         else
1039             if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1040                 msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ1]);
1041             else
1042                 msgbox_uvmat('ERROR',['no civ1 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ1]);
1043             end
1044              set(handles.list_pair_civ1,'String',{''});
1045              %COMPLETER CAS STEREO
1046             return
1047         end
1048     end
1049 end
1050 if isequal(mode,'series(Di)') %| isequal(mode,'st_series(Di)')
1051     if testpair
1052               displ_pair{1}=['Di= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))];        
1053     elseif ~isequal(get(handles.dt_text,'String'),'dt(ms)=') 
1054        for ipair=1:nbpair
1055           if select(ipair)  
1056               if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1057               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
1058               displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1059               end
1060           else 
1061              displ_pair{ipair}='...'; %pair not displayed in the menu
1062           end
1063        end
1064     else
1065        for ipair=1:nbpair
1066          if select(ipair)
1067             displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt_unit*ipair)];
1068          else 
1069             displ_pair{ipair}='...'; %pair not displayed in the menu
1070          end
1071        end
1072     end
1073 elseif isequal(mode,'series(Dj)')%|isequal(mode,'st_series(Dj)')% series on the j index
1074     if testpair
1075          displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(1)/2)) '|' num2str(ceil(browse.incr_pair(1)/2))]; 
1076     else
1077        for ipair=1:nbpair
1078           if select(ipair)
1079               if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1080               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
1081               displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1082               end
1083            elseif testpair
1084               displ_pair{1}=['Dj= ' num2str(-floor(browse.incr_pair(2)/2)) '|' num2str(ceil(browse.incr_pair(2)/2))];
1085           else 
1086              displ_pair{ipair}='...'; %pair not displayed in the menu
1087           end
1088        end
1089    end
1090 elseif isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')%case of pairs
1091     for ipair=1:nbpair
1092         if select(ipair)
1093            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
1094            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1095                         ' :dt= ' num2str(dt*1000)];
1096        else 
1097           displ_pair{ipair}='...'; %pair not displayed in the menu
1098         end   
1099    end
1100 elseif isequal(mode,'displacement')
1101     displ_pair={'Di=Dj=0'};  
1102 end   
1103 set(handles.list_pair_civ1,'String',displ_pair');
1104 ichoice=min(find(select));
1105 if (isempty(ichoice) | ichoice < 1); ichoice=1; end;
1106 initial=get(handles.list_pair_civ1,'Value');
1107 if initial>nbpair |~isequal(select(initial),1)
1108     set(handles.list_pair_civ1,'Value',ichoice);% first valid pair proposed by default in the menu
1109 end
1110 %set(handles.list_pair_civ2,'String',displ_pair');
1111 initial=get(handles.list_pair_civ2,'Value');
1112 if initial>length(displ_pair')%|~isequal(select(initial),1)
1113     if ichoice <= length(displ_pair')
1114         set(handles.list_pair_civ2,'Value',ichoice);% same pair proposed by default for civ2
1115     else
1116         set(handles.list_pair_civ2,'Value',1);% same pair proposed by default for civ2
1117     end
1118 end
1119 set(handles.list_pair_civ2,'String',displ_pair');
1120 set(gcf,'Pointer','arrow')
1121 %--------------------------------------------------------------
1122 % determine the menu for civ2 pairs depending on the existing netcdf file at the
1123 %middle of the series set by first_i, incr, last_i
1124 %--------------------------------------------------------------
1125 function find_netcpair_civ2(hObject, eventdata, handles)
1126 set(gcf,'Pointer','watch')
1127 %nomenclature types
1128 filebase=get(handles.displ_filebase,'String');
1129 [filepath,Nme,ext_dir]=fileparts(filebase);
1130 browse=get(handles.browse_root,'UserData');
1131 compare_list=get(handles.compare,'String');
1132 val=get(handles.compare,'Value');
1133 compare=compare_list{val};
1134 if strcmp(compare,'displacement')
1135     mode='displacement';
1136 else
1137     mode_list=get(handles.mode,'String');
1138     mode_value=get(handles.mode,'Value');
1139     mode=mode_list{mode_value};
1140 end
1141 
1142 % nomenclature type of the .nc files
1143 nom_type_ima='ima_num';%default
1144 if isfield(browse,'nom_type_ima')
1145     nom_type_ima=browse.nom_type_ima;
1146 end
1147 nom_type_nc='_i1-i2';%default
1148 if isfield(browse,'nom_type_nc')
1149     nom_type_nc=browse.nom_type_nc;
1150 end
1151 if isequal(nom_type_ima,'png_old') | isequal(nom_type_ima,'netc_old')| isequal(nom_type_ima,'raw_SMD')| isequal(nom_type_nc,'netc_old')
1152     nom_type_nc='netc_old';%nom_type for the netcdf files
1153 elseif isequal(nom_type_ima,'none')|isequal(nom_type_nc,'none')
1154     nom_type_nc='none';
1155 elseif isequal(nom_type_ima,'avi')|isequal(nom_type_ima,'_i')|isequal(nom_type_ima,'ima_num')|isequal(nom_type_nc,'_i1-i2')
1156      nom_type_nc='_i1-i2';
1157 else
1158     if  isequal(mode,'series(Di)')%|isequal(mode,'st_series(Di)')
1159         nom_type_nc='_i1-i2_j'; % PIV in volume
1160     else
1161         nom_type_nc='_i_j1-j2';
1162     end    
1163 end
1164 browse.nom_type_nc=nom_type_nc;
1165 set(handles.browse_root,'UserData',browse)
1166 
1167 %reads .nc subdirectory and image numbers from the interface
1168 subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf data
1169 subdir_civ2=get(handles.subdir_civ2,'String');%subdirectory subdir_civ2 for the netcdf data
1170 first_i=str2num(get(handles.first_i,'String'));
1171 last_i=str2num(get(handles.last_i,'String'));
1172 incr=str2num(get(handles.incr_i,'String'));
1173 num1=first_i:incr:last_i;
1174 if isempty(num1)
1175     set(handles.list_pair_civ2,'Value',1);
1176     set(handles.list_pair_civ2,'String',{''});
1177     return
1178 end
1179 ref_i=str2num(get(handles.ref_i,'String'));
1180 if isequal(mode,'pair j1-j2')%|isequal(mode,'st_pair j1-j2')
1181     ref_j=0;
1182 else
1183     ref_j=str2num(get(handles.ref_j,'String'));
1184 end
1185 if isequal(get(handles.dt_text,'String'),'dt(ms)=')%simple series(Di) with equal interval
1186     ref_i=ceil((first_i+last_i)/2);
1187     ref_j=1;
1188 end
1189 %     ref_i=browse.num_ref;%field number initially selected by the browser
1190 time=get(handles.displ_filebase,'UserData'); %get the set of times
1191 if isempty(time)
1192     time=[0 1];%default
1193 end
1194 dt_unit=str2num(get(handles.dt,'String'));% used when there is no image documentation file
1195 displ_num=get(handles.list_pair_civ1,'UserData');
1196 
1197 
1198 %eliminate the first pairs inconsistent with the position
1199 if isempty(displ_num)
1200     nbpair=0;
1201 else
1202     nbpair=length(displ_num(1,:));%nbre of displayed pairs
1203     if  isequal(mode,'series(Di)')% | isequal(mode,'st_series(Di)')
1204         nbpair=min(2*ref_i-1,nbpair);%limit the number of pairs with positive first index
1205     elseif  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1206         nbpair=min(2*ref_j-1,nbpair);%limit the number of pairs with positive first index
1207     end
1208 end
1209 nbpair=min(200,nbpair);%limit the number of displayed pairs to 200
1210 
1211 %look for existing processed pairs involving the field at the middle of the series if civ1 will not
1212 % be performed, while the result is needed for next steps.
1213 displ_pair={''}; %default
1214 select=ones(size(1:nbpair));%default =1 for numbers of displayed pairs
1215 if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0%&...
1216     if ~exist(fullfile(filepath,subdir_civ2,ext_dir),'dir') 
1217          errordlg(['no civ2 file available: subdirectory ' subdir_civ2 ' does not exist'])
1218          set(handles.list_pair_civ2,'Value',1);
1219          set(handles.list_pair_civ2,'String',{''});
1220          return
1221     end
1222     for ipair=1:nbpair       
1223         filename=name_generator(filebase,ref_i+displ_num(3,ipair),ref_j+displ_num(1,ipair),'.nc',nom_type_nc,1,...
1224         ref_i+displ_num(4,ipair),ref_j+displ_num(2,ipair),subdir_civ1);
1225         select(ipair)=exist(filename,'file')==2;
1226     end
1227     if  isequal(select,zeros(size(1:nbpair)))
1228         if isfield(browse,'incr_pair') 
1229             num_i1=ref_i-floor(browse.incr_pair(1)/2);
1230             num_i2=ref_i+floor((browse.incr_pair(1)+1)/2);
1231             num_j1=ref_j-floor(browse.incr_pair(2)/2);
1232             num_j2=ref_j+floor((browse.incr_pair(2)+1)/2);
1233             filename=name_generator(filebase,num_i1,num_j1,'.nc',nom_type_nc,1,num_i2,num_j2,subdir_civ2);
1234             select(1)=exist(filename,'file')==2;
1235         else
1236             if  isequal(mode,'series(Dj)')% | isequal(mode,'st_series(Dj)')
1237                 errordlg(['no civ2 file available for the selected reference index j=' num2str(ref_j) ' and subdirectory ' subdir_civ2])
1238             else
1239                 errordlg(['no civ2 file available for the selected reference index i=' num2str(ref_i) ' and subdirectory ' subdir_civ2])
1240             end
1241              set(handles.list_pair_civ2,'Value',1);
1242              set(handles.list_pair_civ2,'String',{''});
1243             return
1244         end
1245     end
1246 end
1247 if isequal(mode,'series(Di)') % | isequal(mode,'st_series(Di)')
1248 %     if  ~isequal(get(handles.dt_text,'String'),'dt(ms)=')
1249        for ipair=1:nbpair
1250           if select(ipair)
1251               if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1252                 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
1253                 displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1254               end
1255           else 
1256              displ_pair{ipair}='...'; %pair not displayed in the menu
1257           end
1258        end
1259 %    else
1260 %        for ipair=1:nbpair
1261 %          if select(ipair)
1262 %             displ_pair{ipair}=['Di= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt_unit*ipair)];
1263 %          else
1264 %             displ_pair{ipair}='...'; %pair not displayed in the menu
1265 %          end
1266 %        end
1267 %     end
1268 elseif isequal(mode,'series(Dj)') %| isequal(mode,'st_series(Dj)') % series on the j index
1269        for ipair=1:nbpair
1270           if select(ipair)
1271               if size(time,1)>=ref_i+displ_num(4,ipair) && size(time,2)>=ref_j+displ_num(2,ipair)
1272               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
1273               displ_pair{ipair}=['Dj= ' num2str(-floor(ipair/2)) '|' num2str(ceil(ipair/2)) ' :dt= ' num2str(dt*1000)];
1274               end
1275           else 
1276              displ_pair{ipair}='...'; %pair not displayed in the menu
1277           end
1278        end
1279 elseif isequal(mode,'pair j1-j2')% | isequal(mode,'st_pair j1-j2') %case of pairs
1280     for ipair=1:nbpair
1281         if select(ipair)
1282            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
1283            displ_pair{ipair}=['j= ' num2stra(displ_num(1,ipair),nom_type_ima) '-' num2stra(displ_num(2,ipair),nom_type_ima) ...
1284                         ' :dt= ' num2str(dt*1000)];
1285        else 
1286           displ_pair{ipair}='...'; %pair not displayed in the menu
1287        end   
1288    end
1289 elseif isequal(mode,'displacement')
1290     displ_pair={'Di=Dj=0'};  
1291 end   
1292 val=get(handles.list_pair_civ2,'Value');
1293 ichoice=min(find(select));
1294 if (isempty(ichoice) | ichoice < 1); ichoice=1; end;
1295 if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
1296     val=ichoice;% first valid pair proposed by default in the menu
1297 end
1298 if val>length(displ_pair')
1299     set(handles.list_pair_civ2,'Value',1);% first valid pair proposed by default in the menu
1300 else
1301     set(handles.list_pair_civ2,'Value',val);
1302 end
1303 set(handles.list_pair_civ2,'String',displ_pair');
1304 
1305 
1306 set(gcf,'Pointer','arrow')
1307 %----------------------------------------------------
1308 %  determine the list of index pairs of processing file
1309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1310 function [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
1311     find_pair_indices(handles,mode)
1312 first_i=str2num(get(handles.first_i,'String'));
1313 last_i=str2num(get(handles.last_i,'String'));
1314 incr=str2num(get(handles.incr_i,'String'));
1315 num1=[first_i:incr:last_i];
1316 if isequal(get(handles.first_j,'Visible'),'on')
1317     first_j=str2num(get(handles.first_j,'String'));
1318     last_j=str2num(get(handles.last_j,'String'));
1319     incr_j=str2num(get(handles.incr_j,'String'));
1320 else
1321     first_j=1;
1322     last_j=1;
1323     incr_j=1;
1324 end
1325 num_j=[first_j:incr_j:last_j];
1326 list_civ1=get(handles.list_pair_civ1,'String');
1327 index_civ1=get(handles.list_pair_civ1,'Value');
1328 str_civ1=list_civ1{index_civ1};
1329 if isempty(str_civ1)||isequal(str_civ1,'')
1330     msgbox_uvmat('ERROR','no image pair selected for civ1')
1331     return
1332 end
1333 list_civ2=get(handles.list_pair_civ2,'String');
1334 index_civ2=get(handles.list_pair_civ2,'Value');
1335 if index_civ2>length(list_civ2)
1336     list_civ2=list_civ1;
1337     index_civ2=index_civ1;
1338 end
1339 str_civ2=list_civ2{index_civ2};
1340 if isequal(first_i,[])|isequal(first_j,[]), errordlg('first field number not defined'),...
1341     set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1342 if isequal(last_i,[])| isequal(last_j,[]),errordlg('last field number not defined'),...
1343     set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1344 if isequal(incr,[])| isequal(incr_j,[]),errordlg('increment in field number not defined'),...
1345     set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1346 if last_i < first_i | last_j < first_j , errordlg('last field number must be larger than the first one'),...
1347     set(handles.RUN, 'Enable','On'), set(handles.RUN,'BackgroundColor',[1 0 0]),return,end;
1348 if isequal (mode,'series(Di)')% |isequal(mode,'st_series(Di)')
1349      %recognize the pair civ1 from the display
1350     indsel=find((double(str_civ1)<48)|(double(str_civ1)>57));% character indices of non numerical characters
1351     str_raw=str_civ1(indsel);
1352     indsepar=find(str_raw=='|'); %character index of the separator
1353     d1=str2num(str_civ1([indsel(indsepar-1)+1:indsel(indsepar)-1]));
1354     if indsepar==length(str_raw)
1355         d2=str2num(str_civ1([indsel(indsepar)+1:end]));
1356     else
1357         d2=str2num(str_civ1([indsel(indsepar)+1:indsel(indsepar+1)-1]));
1358     end   
1359     num1_civ1=num1-d1;% set of first image numbers
1360     num2_civ1=num1+d2;
1361     num_a_civ1=num_j;
1362     num_b_civ1=num_j;
1363     
1364     %recognize the pair civ2 from the display
1365     indsel=find((double(str_civ2)<48)|(double(str_civ2)>57));% character indices of non numerical characters
1366     str_raw=str_civ2(indsel);
1367     indsepar=find(str_raw=='|'); %character index of the separator
1368     d1=str2num(str_civ2([indsel(indsepar-1)+1:indsel(indsepar)-1]));
1369     if indsepar==length(str_raw)
1370         d2=str2num(str_civ2([indsel(indsepar)+1:end]));
1371     else
1372         d2=str2num(str_civ2([indsel(indsepar)+1:indsel(indsepar+1)-1]));
1373     end   
1374     if isempty(d1)
1375         num1_civ2=num1;
1376     else
1377         num1_civ2=num1-d1;% set of first image numbers
1378     end
1379     if isempty(d2)
1380         num2_civ2=num1;
1381     else
1382         num2_civ2=num1+d2;
1383     end
1384     num_a_civ2=num_j;
1385     num_b_civ2=num_j;
1386     
1387     % adjust the first and last field number
1388     lastfield=str2num(get(handles.nb_field,'String'));
1389     if isequal(lastfield,[])
1390         indsel=find((num1_civ1 >= 1)&(num1_civ2 >= 1));
1391     else
1392         indsel=find((num2_civ1 <= lastfield)&(num2_civ2 <= lastfield)&(num1_civ1 >= 1)&(num1_civ2 >= 1));
1393     end
1394     if length(indsel)>=1
1395         firstind=indsel(1);
1396         lastind=indsel(end);
1397         set(handles.first_i,'String',num2str(num1(firstind)))%update the display of first and last fields
1398         set(handles.last_i,'String',num2str(num1(lastind)))
1399         num1=num1(indsel);
1400         num1_civ1=num1_civ1(indsel);
1401         num1_civ2=num1_civ2(indsel);
1402         num2_civ1=num2_civ1(indsel);
1403         num2_civ2=num2_civ2(indsel);
1404     end
1405 elseif isequal (mode,'series(Dj)')%|isequal (mode,'st_series(Dj)')
1406     lastfield_j=str2num(get(handles.nb_field2,'String'));
1407     num1_civ1=num1;% set of first image numbers
1408     num2_civ1=num1;
1409     num_a_civ1=num_j-floor(index_civ1/2)*ones(size(num_j));
1410     num_b_civ1=num_j+ceil(index_civ1/2)*ones(size(num_j));
1411     num1_civ2=num1;
1412     num2_civ2=num1;
1413     num_a_civ2=num_j-floor(index_civ2/2)*ones(size(num_j));
1414     num_b_civ2=num_j+ceil(index_civ2/2)*ones(size(num_j));
1415     % adjust the first and last field number
1416     if isequal(lastfield_j,[])
1417         indsel=find((num_a_civ1 >= 1)&(num_a_civ2 >= 1));
1418     else
1419         indsel=find((num_b_civ1 <= lastfield_j)&(num_b_civ2 <= lastfield_j)&(num_a_civ1 >= 1)&(num_a_civ2 >= 1));
1420     end
1421     if length(indsel)>=1
1422         firstind=indsel(1);
1423         lastind=indsel(end);
1424         set(handles.first_j,'String',num2str(num_j(firstind)))%update the display of first and last fields
1425         set(handles.last_j,'String',num2str(num_j(lastind)))
1426         num_j=num_j(indsel);
1427         num_a_civ1=num_a_civ1(indsel);
1428         num_a_civ2=num_a_civ2(indsel);
1429         num_b_civ1=num_b_civ1(indsel);
1430         num_b_civ2=num_b_civ2(indsel);
1431     end
1432 elseif isequal(mode,'pair j1-j2')% | isequal(mode,'st_pair j1-j2') %case of bursts (png_old or png_2D)
1433     num1_civ1=num1;
1434     num1_civ2=num1;
1435     displ_num=get(handles.list_pair_civ1,'UserData');
1436     num2_civ1=num1;
1437     num_a_civ1=displ_num(1,index_civ1);
1438     num_b_civ1=displ_num(2,index_civ1);
1439     num2_civ2=num1;
1440     num_a_civ2=displ_num(1,index_civ2);
1441     num_b_civ2=displ_num(2,index_civ2);
1442 elseif isequal(mode,'displacement')
1443     num1_civ1=num1;
1444     num2_civ1=num1;
1445     num_a_civ1=num_j;
1446     num_b_civ1=num_j;
1447     num1_civ2=num1;
1448     num2_civ2=num1;
1449     num_a_civ2=num_j;
1450     num_b_civ2=num_j;
1451 end
1452 
1453 
1454 %-------------------------------------------------------------
1455 % --- Executes on selection change in list_pair_civ1.
1456 function list_pair_civ1_Callback(hObject, eventdata, handles)
1457 %------------------------------------------------------------
1458 %reproduce by default the chosen pair in the civ2 menu
1459 list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
1460 index_pair=get(handles.list_pair_civ1,'Value');
1461 displ_num=get(handles.list_pair_civ1,'UserData');
1462 num_a=displ_num(1,index_pair);
1463 num_b=displ_num(2,index_pair);
1464 list_pair2=get(handles.list_pair_civ2,'String');%get the menu of image pairs
1465 if index_pair<=length(list_pair2)
1466     set(handles.list_pair_civ2,'Value',index_pair);
1467 end
1468 
1469 %update first_i and last_i according to the chosen image pairs
1470 mode_list=get(handles.mode,'String');
1471 mode_value=get(handles.mode,'Value');
1472 mode=mode_list{mode_value};
1473 if isequal(mode,'series(Di)')
1474     first_i=str2num(get(handles.first_i,'String'));
1475     last_i=str2num(get(handles.last_i,'String'));
1476     incr_i=str2num(get(handles.incr_i,'String'));
1477     num1=first_i:incr_i:last_i;
1478     lastfield=str2num(get(handles.nb_field,'String'));
1479     if ~isequal(lastfield,[])
1480         ind=find((num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1481              (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield));
1482         num1=num1(ind);
1483     end
1484     set(handles.first_i,'String',num2str(num1(1)));
1485     set(handles.last_i,'String',num2str(num1(end)));
1486 elseif isequal(mode,'series(Dj)')
1487     first_j=str2num(get(handles.first_j,'String'));
1488     last_j=str2num(get(handles.last_j,'String'));
1489     incr_j=str2num(get(handles.incr_j,'String'));
1490     num_j=first_j:incr_j:last_j;
1491     lastfield2=str2num(get(handles.nb_field2,'String'));
1492     if ~isequal(lastfield2,[])
1493         ind=find((num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1494              (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2));
1495         num1=num_j(ind);
1496     end
1497     set(handles.first_j,'String',num2str(num1(1)));
1498     set(handles.last_j,'String',num2str(num1(end)));
1499 end 
1500 
1501 %------------------------------------------------------------------
1502 % --- Executes on selection change in list_pair_civ2.
1503 function list_pair_civ2_Callback(hObject, eventdata, handles)
1504 
1505 index_pair=get(handles.list_pair_civ2,'Value');%get the selected position index in the menu
1506 
1507 %update first_i and last_i according to the chosen image pairs
1508 mode_list=get(handles.mode,'String');
1509 mode_value=get(handles.mode,'Value');
1510 mode=mode_list{mode_value};
1511 if isequal(mode,'series(Di)')
1512     first_i=str2num(get(handles.first_i,'String'));
1513     last_i=str2num(get(handles.last_i,'String'));
1514     incr_i=str2num(get(handles.incr_i,'String'));
1515     num1=first_i:incr_i:last_i;
1516     lastfield=str2num(get(handles.nb_field,'String'));
1517     if ~isequal(lastfield,[])
1518         ind=find((num1-floor(index_pair/2)*ones(size(num1))>0)& ...
1519              (num1+ceil(index_pair/2)*ones(size(num1))<=lastfield));
1520         num1=num1(ind);
1521     end
1522     set(handles.first_i,'String',num2str(num1(1)));
1523     set(handles.last_i,'String',num2str(num1(end)));
1524 elseif isequal(mode,'series(Dj)')
1525     first_j=str2num(get(handles.first_j,'String'));
1526     last_j=str2num(get(handles.last_j,'String'));
1527     incr_j=str2num(get(handles.incr_j,'String'));
1528     num_j=first_j:incr_j:last_j;
1529     lastfield2=str2num(get(handles.nb_field2,'String'));
1530     if ~isequal(lastfield2,[])
1531         ind=find((num_j-floor(index_pair/2)*ones(size(num_j))>0)& ...
1532              (num_j+ceil(index_pair/2)*ones(size(num_j))<=lastfield2));
1533         num1=num_j(ind);
1534     end
1535     set(handles.first_j,'String',num2str(num1(1)));
1536     set(handles.last_j,'String',num2str(num1(end)));
1537 end 
1538 
1539 %---------------------------------------------------
1540 % --- Executes on button press in RUN.
1541 %------------------------------------------------------
1542 function RUN_Callback(hObject, eventdata, handles)
1543 global civ1_exe civ2_exe patch_exe patch_new_exe sge
1544 
1545 compare=get(handles.compare,'Value');%test for usual PIV (compare=1) or displacement (=2) or stereo PIV (=3)
1546 %check the list of operations:
1547 operations={'CIV1','FIX1','PATCH1','CIV2','FIX2','PATCH2'};
1548 run_flag=1;
1549 box_test(1)=get(handles.CIV1,'Value');
1550 box_test(2)=get(handles.FIX1,'Value');
1551 box_test(3)=get(handles.PATCH1,'Value');
1552 box_test(4)=get(handles.CIV2,'Value');
1553 box_test(5)=get(handles.FIX2,'Value');
1554 box_test(6)=get(handles.PATCH2,'Value');
1555 index=find(box_test==1);
1556 if isempty(index)
1557     errordlg('no selected operation')
1558     set(handles.RUN, 'Enable','On')
1559     set(handles.RUN,'BackgroundColor',[1 0 0])
1560     return
1561 end
1562 index_first=min(index);
1563 index_last=max(index);
1564 box_used=box_test([index_first : index_last]);
1565 [box_missing,ind_missing]=min(box_used);
1566 if isequal(box_missing,0)
1567     errordlg(['missing' cell2mat(operations(ind_missing))]);
1568     set(handles.RUN, 'Enable','On')
1569     set(handles.RUN,'BackgroundColor',[1 0 0])
1570     return
1571 end
1572 
1573 %root name
1574 filebase=get(handles.displ_filebase,'String');
1575 if isempty(filebase)||isequal(filebase,'')
1576     msgbox_uvmat('ERROR','no input files')
1577     return
1578 end
1579 %check mask if selecetd
1580 if isequal(get(handles.get_mask_civ1,'Value'),1)
1581      get_mask_civ1_Callback(hObject, eventdata, handles);
1582 end
1583 if isequal(get(handles.get_mask_fix1,'Value'),1)
1584      get_mask_fix1_Callback(hObject, eventdata, handles);
1585 end
1586 if isequal(get(handles.get_mask_civ2,'Value'),1)
1587      get_mask_civ2_Callback(hObject, eventdata, handles);
1588 end
1589 if isequal(get(handles.get_mask_fix2,'Value'),1)
1590      get_mask_fix2_Callback(hObject, eventdata, handles);
1591 end
1592 
1593 %read names of the .exe file
1594 if box_test(1)==1 | box_test(3)==1 | box_test(4)==1 | box_test(6)==1 
1595     path_uvmat=which('uvmat');% check the path detected for source file uvmat
1596     path_UVMAT=fileparts(path_uvmat); %path to UVMAT
1597     if isunix
1598         %fid = fopen(fullfile(path_UVMAT,'PARAM_LINUX.txt'),'r');%open the file with civ binary names
1599         xmlfile=fullfile(path_UVMAT,'PARAM_LINUX.xml')
1600         if exist(xmlfile,'file')
1601             t=xmltree(xmlfile);
1602             sparam=convert(t);
1603         end
1604     else
1605         %fid = fopen(fullfile(path_UVMAT,'PARAM_WIN.txt'),'r');%open the file with civ binary names
1606         xmlfile=fullfile(path_UVMAT,'PARAM_WIN.xml')
1607         if exist(xmlfile,'file')
1608             t=xmltree(xmlfile);
1609             sparam=convert(t);
1610         end
1611     end 
1612     if isfield(sparam,'Civ1_exe')
1613         civ1_exe=sparam.Civ1_exe;
1614     end
1615     if isfield(sparam,'Civ2_exe')
1616         civ2_exe=sparam.Civ2_exe;
1617     end
1618     if isfield(sparam,'Patch_exe')
1619         patch_exe=sparam.Patch_exe;
1620     end
1621     if isfield(sparam,'Stinterp_exe')
1622         stinterp_exe=sparam.Stinterp_exe;
1623     end
1624     if isfield(sparam,'SGE')
1625         sge=str2num(sparam.SGE);
1626     end 
1627     if ~isunix % for windows system, check whether the Matlab working dir is a UBC name
1628         dircur=pwd;
1629         if ~isequal(dircur([2 3]),':\')
1630             errordlg('move to a Matlab current working directory with a Windows name, e.g. m:/...')
1631             set(handles.RUN, 'Enable','On')
1632             set(handles.RUN,'BackgroundColor',[1 0 0])
1633             return;
1634         end
1635     end
1636 end
1637 
1638 %initialize the waitbars
1639 set(handles.waitbar_1,'Position',[0.946 0.877 0.03 0.001])
1640 set(handles.waitbar_patch1,'Position',[0.946 0.626 0.03 0.001])
1641 set(handles.waitbar_civ2,'Position',[0.946 0.406 0.03 0.001])
1642 set(handles.waitbar_patch2,'Position',[0.946 0.187 0.03 0.001])
1643 set(handles.RUN,'BackgroundColor',[0.831 0.816 0.784])
1644 set(handles.RUN, 'Enable','Off')
1645 drawnow
1646 
1647 [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]=...
1648        set_civ_filenames(handles,compare,box_test);
1649   
1650 nbfield=size(num1_civ1,2);
1651 nbslice=size(num1_civ1,1);
1652 
1653 %RUN CIV1
1654 if box_test(1)==1
1655       RUN_CIV1(handles,filecell.ima1.civ1,filecell.ima2.civ1,filecell.nc.civ1,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,nom_type_nc)
1656       if compare==3
1657             RUN_CIV1(handles,filecell.imaA1.civ1,filecell.imaA2.civ1,filecell.ncA.civ1,num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,nom_type_nc)
1658      end
1659 end
1660 
1661 %RUN FIX1
1662 if box_test(2)==1 
1663     filebase=get(handles.displ_filebase,'String');
1664     %names of the civ1 fields
1665     field1.vel_type='civ1';
1666     field1.nb='nb_vectors';
1667     field1.X='vec_X';
1668     field1.Y='vec_Y';
1669     field1.U='vec_U';
1670     field1.V='vec_V';
1671     field1.fixflag='vec_FixFlag';
1672     flagindex(1)=get(handles.vec_Fmin2, 'Value');
1673     flagindex(2)=get(handles.vec_F3, 'Value');
1674     flagindex(3)=get(handles.vec_F2, 'Value');
1675     thresh_vecC=str2num(get(handles.thresh_vecC,'String'));%threshold on image correlation vec_C
1676     thresh_vel=str2num(get(handles.thresh_vel,'String'));%threshold on velocity modulus
1677     inf_sup=get(handles.inf_sup1,'Value');
1678     menu=get(handles.field_ref1,'String');
1679     index=get(handles.field_ref1,'Value');
1680     if isempty(menu)
1681         fieldchoice='';
1682     else
1683         fieldchoice=menu{index};
1684     end   
1685     h = waitbar(0,['removing velocity vectors, fix1']);% display a wait bar
1686     test_mask=get(handles.get_mask_fix1,'Value');
1687     maskdispl=get(handles.mask_fix1,'String');
1688     maskbase=[get(handles.displ_filebase,'String') '_' maskdispl]; % mask root name
1689     for ifile=1:nbfield
1690         waitbar(ifile/nbfield); %update the waitbar
1691         for j=1:nbslice
1692             if test_mask==0
1693                 maskname='noFile use default';
1694                 maskflag=0;
1695             else
1696                 maskname=name_generator(maskbase,num1_civ1(ifile),1,'.png','_i');% mask corresponding to the first image of the pair
1697                 maskflag= exist(maskname,'file')==2;
1698                 if ~maskflag; 
1699                     maskname='noFile use default';
1700                 end              
1701             end
1702             if ~isempty(file_ref_fix1)
1703                 file_ref=file_ref_fix1{ifile,j};
1704             else
1705                 file_ref='';
1706             end
1707             error=RUN_FIX(filecell.nc.civ1{ifile,j},field1,flagindex,1,thresh_vecC,maskflag,maskname,...
1708                                     thresh_vel,inf_sup,file_ref,fieldchoice)
1709 
1710             if compare==3 &&(isequal(mode,'pair j1-j2') || isequal(mode,'series(Dj)') || isequal(mode,'series(Di)'))
1711                  if test_mask==0  
1712                       maskname='noFile use default';
1713                       maskflag=0;
1714                  else
1715                      maskbase=[get(handles.displ_filebase2,'String') '_' maskdispl]; % mask root name
1716                      maskname=name_generator(maskbase,num1_civ1(ifile),1,'.png','_i');
1717                      maskflag= exist(maskname,'file')==2;
1718                      if ~maskflag; 
1719                         maskname='noFile use default';
1720                      end
1721                  end
1722                  error=RUN_FIX(filecell.ncA.civ1{ifile,j},field1,flagindex,1,thresh_vecC,maskflag,maskname,...
1723                                     thresh_vel,inf_sup,file_ref,fieldchoice)
1724                  if ~isempty(error)
1725                     errordlg(error)
1726                     return
1727                  end             
1728             end
1729         end
1730     end
1731     close(h)
1732 end
1733 
1734 % RUN PATCH1
1735 if box_test(3)==1
1736     rho_patch1=str2num(get(handles.rho_patch1,'String'));
1737     if isempty(rho_patch1)
1738         rho_patch1='1000';
1739         set(handles.rho_patch1,'String','1')
1740     else
1741         rho_patch1=num2str(1000*rho_patch1);
1742     end
1743     nx_patch1=get(handles.nx_patch1,'String');
1744     if isequal(str2num(nx_patch1),[])
1745          nx_patch1='50' ;%default
1746          set(handles.nx_patch1,'String','50');
1747     end
1748     ny_patch1=get(handles.ny_patch1,'String');
1749     if isequal(str2num(ny_patch1),[])
1750          ny_patch1='50' ;%default
1751          set(handles.ny_patch1,'String','50');
1752     end
1753     subdomain_patch1=get(handles.subdomain_patch1,'String');
1754     thresh_patch1=get(handles.thresh_patch1,'String');
1755     test_interp=get(handles.test_interp,'Value'); 
1756     icount=0;
1757      for ifile=1:nbfield
1758 %             barlength=0.188*ifile/nbfield;
1759 %             set(handles.waitbar_patch1,'Position',[0.946 0.627-barlength 0.03 barlength])
1760             for j=1:nbslice
1761                 icount=icount+1;
1762                 barlength=0.188*icount/(nbfield*nbslice);
1763                 set(handles.waitbar_patch1,'Position',[0.946 0.627-barlength 0.03 barlength])
1764                 drawnow
1765                 if  isequal(get(handles.test_stereo1,'Value'),0)
1766                     cmd=RUN_PATCH(filecell.nc.civ1{ifile,j},nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,test_interp);
1767                     s=-1;
1768                     if sge
1769                         [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1770                     end
1771                     if s~=0
1772                         eval(['!' cmd]);
1773                     end
1774                     
1775                     %stereo case:
1776                 elseif  isequal(get(handles.test_stereo1,'Value'),1)
1777                     if exist('stinterp_exe','var')%Prog Gauthier
1778                     cmd=RUN_STINTERP(stinterp_exe,filecell.ncA.civ1{ifile,j},filecell.nc.civ1{ifile,j},filecell.st{ifile,j},...
1779                     nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,[filebase_A '.xml'],[filebase_B '.xml']);
1780                     eval(['!' cmd]);
1781                     else
1782                     RUN_STLIN(filecell.ncA.civ1{ifile,j},filecell.nc.civ1{ifile,j},'civ1',filecell.st{ifile,j},...
1783                     str2num(nx_patch1),str2num(ny_patch1),str2num(thresh_patch1),[filebase_A '.xml'],[filebase_B '.xml']);
1784                     end
1785                 end
1786                 if compare==3 && isequal(get(handles.test_stereo1,'Value'),0)
1787                     cmd=RUN_PATCH(filecell.ncA.civ1{ifile,j},nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,test_interp);
1788                     s=-1;
1789                     if sge
1790                         [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1791                     end
1792                     if s~=0
1793                         eval(['!' cmd]);
1794                     end
1795                 end
1796             end
1797         end
1798 end
1799 %     close(h)
1800 
1801 % CIV2
1802 if box_test(4)==1 
1803     RUN_CIV2(handles,filecell.ima1.civ2,filecell.ima2.civ2,filecell.nc.civ1,filecell.nc.civ2,num1_civ2,num2_civ2,...
1804         num_a_civ2,num_b_civ2,nom_type_nc)
1805 end
1806 
1807 % FIX2
1808 if box_test(5)==1 
1809     %names of the civ2 fields
1810     field2.vel_type='civ2';
1811     field2.nb='nb_vectors2';
1812     field2.X='vec2_X';
1813     field2.Y='vec2_Y';
1814     field2.U='vec2_U';
1815     field2.V='vec2_V';
1816     field2.fixflag='vec2_FixFlag';
1817     flagindex(1)=get(handles.vec_Fmin2_2, 'Value');
1818     flagindex(2)=get(handles.vec_F3_2, 'Value');
1819     flagindex(3)=get(handles.vec_F4, 'Value');
1820     thresh_vec2C=str2num(get(handles.thresh_vec2C,'String'));%threshold on image correlation vec_C
1821     thresh_vel2=str2num(get(handles.thresh_vel2,'String'));%threshold on velocity modulus
1822     inf_sup=get(handles.inf_sup2,'Value');
1823     menu=get(handles.field_ref2,'String');
1824     index=get(handles.field_ref2,'Value');
1825     if isempty(menu)
1826         fieldchoice='';
1827     else
1828         fieldchoice=menu{index};
1829     end   
1830 
1831     h = waitbar(0,['removing velocity vectors, fix2']);% display a wait bar
1832     test_mask=get(handles.get_mask_fix2,'Value');
1833     maskdispl=get(handles.mask_fix2,'String');
1834     maskbase=[get(handles.displ_filebase,'String') '_' maskdispl]; % mask root name
1835     for ifile=1:nbfield
1836         waitbar(ifile/nbfield); %update the waitbar
1837         for j=1:nbslice       
1838            if test_mask==0 
1839                 maskname='noFile use default';
1840                 maskflag=0;
1841            else
1842                 maskname=name_generator(maskbase,num1_civ2(ifile),1,'.png','_i');
1843                 maskflag= exist(maskname,'file')==2;
1844                 if ~maskflag; 
1845                       maskname='noFile use default';
1846                 end
1847             end
1848             if ~isempty(file_ref_fix2)
1849                 file_ref=file_ref_fix2{ifile,j};
1850             else
1851                 file_ref='';
1852             end
1853             'TESTfix'
1854             filecell.nc.civ2{ifile,j}
1855             error=RUN_FIX(filecell.nc.civ2{ifile,j},field2,flagindex,2,thresh_vec2C,maskflag,maskname,...
1856                                     thresh_vel2,inf_sup,file_ref,fieldchoice); 
1857             if ~isempty(error)
1858                 msgbox_uvmat('ERROR',error)
1859                 return
1860             end  
1861             if compare==3
1862                  if test_mask==0  
1863                       maskname='noFile use default';
1864                       maskflag=0;
1865                  else
1866                       maskbase=[get(handles.displ_filebase2,'String') '_' maskdispl]; % mask root name
1867                       maskname=name_generator(maskbase,num1_civ2(ifile),1,'.png','_i');
1868                       maskflag= exist(maskname,'file')==2;
1869                       if ~maskflag; 
1870                          maskname='noFile use default';
1871                       end
1872                  end
1873                  error=RUN_FIX(filecell.ncA.civ2{ifile,j},field2,flagindex,1,thresh_vec2C,maskflag,maskname,...
1874                                     thresh_vel2,inf_sup,file_ref,fieldchoice);
1875                  if ~isempty(error)
1876                     msgbox_uvmat('ERROR',error)
1877                     return
1878                  end  
1879             end
1880         end
1881     end
1882     close(h)
1883 end
1884 
1885 %PATCH 2
1886 if box_test(6)==1
1887     rho_patch2=str2num(get(handles.rho_patch2,'String'));
1888     if isempty(rho_patch2)
1889         rho_patch2='1000';
1890         set(handles.rho_patch2,'String','1')
1891     else
1892         rho_patch2=num2str(1000*rho_patch2);
1893     end
1894     nx_patch2=get(handles.nx_patch2,'String');
1895     ny_patch2=get(handles.ny_patch2,'String');
1896     thresh_patch2=get(handles.thresh_patch2,'String');
1897     if isequal(str2num(nx_patch2),[])
1898          nx_patch2='50' ;%default
1899          set(handles.nx_patch2,'String','50');
1900     end
1901     if isequal(str2num(ny_patch2),[])
1902          ny_patch2='50' ;%default
1903          set(handles.ny_patch2,'String','50');
1904     end
1905     subdomain_patch2=get(handles.subdomain_patch2,'String');
1906     icount=0;
1907     for ifile=1:nbfield
1908 %             barlength=0.188*ifile/nbfield;
1909 %             set(handles.waitbar_patch2,'Position',[0.946 0.188-barlength 0.03 barlength])
1910             for j=1:nbslice
1911                 icount=icount+1;
1912                 barlength=0.188*icount/(nbfield*nbslice);
1913                 set(handles.waitbar_patch2,'Position',[0.946 0.188-barlength 0.03 barlength])
1914                 drawnow
1915                 if compare~=3 | isequal(get(handles.test_stereo2,'Value'),0)
1916                     cmd=RUN_PATCH(filecell.nc.civ2{ifile,j},nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,1,0);
1917                     s=-1;
1918                     if sge
1919                         [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1920                     end
1921                     if s~=0
1922                         eval(['!' cmd]);
1923                     end
1924                 elseif compare==3 &&  isequal(get(handles.test_stereo2,'Value'),1)
1925                    if exist('stinterp_exe','var')%Prog Gauthier
1926                        cmd=RUN_STINTERP(stinterp_exe,filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},filecell.st{ifile,j},...
1927                        nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,thresh_patch2,[filebase_A '.xml'],[filebase_B '.xml']);
1928                        eval(['!' cmd])
1929                    else
1930                        RUN_STLIN(filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},'civ2',filecell.st{ifile,j},...
1931                        str2num(nx_patch2),str2num(ny_patch2),str2num(thresh_patch2),[filebase_A '.xml'],[filebase_B '.xml'])
1932                    end
1933                 end    
1934                 if compare==3 && isequal(get(handles.test_stereo2,'Value'),0)
1935                     cmd=RUN_PATCH(filecell.ncA.civ2{ifile,j},nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,1,0);
1936                     s=-1;
1937                     if sge
1938                         [s,w]=unix(['qrsh -q -fast.q ' cmd]);
1939                     end
1940                     if s~=0
1941                         eval(['!' cmd]);
1942                     end
1943                 end
1944             end
1945     end
1946 end
1947 
1948 set(handles.RUN, 'Enable','On')
1949 set(handles.RUN,'BackgroundColor',[1 0 0])
1950 
1951 %save the current interface setting as figure namefig, append .0 to the name if it already exists
1952 if isfield(filecell,'st')
1953     fileresu=filecell.st{1,1};
1954 elseif isfield(filecell,'nc')
1955     if isfield(filecell.nc,'civ2')
1956         fileresu=filecell.nc.civ2{1,1};
1957     else
1958         fileresu=filecell.nc.civ1{1,1};
1959     end
1960 end
1961 [RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
1962 namedoc=fullfile(RootPath,subdir,RootFile);
1963 detect=1; 
1964 while detect==1
1965     namefigfull=[namedoc '.fig'];
1966     hh=dir(namefigfull);
1967     if ~isempty(hh)
1968         detect=1;
1969         namedoc=[namedoc '.0'];
1970     else
1971         detect=0;
1972     end
1973 end
1974 saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
1975 
1976 
1977 %-----------------------------------------------------------
1978 % --- Executes on button press in BATCH: remote processing
1979 %-----------------------------------------------------------
1980 function BATCH_Callback(hObject, eventdata, handles)
1981 global civ1_exe civ2_exe patch_exe patch_new_exe fix_exe todo_path sge Civ_exe
1982 compare=get(handles.compare,'Value');%test for usual PIV (compare=1) or displacement (=2) or stereo PIV (=3)
1983 
1984 %check the list of operations:
1985 operations={'CIV1','FIX1','PATCH1','CIV2','FIX2','PATCH2'};
1986 run_flag=1;
1987 box_test(1)=get(handles.CIV1,'Value');
1988 box_test(2)=get(handles.FIX1,'Value');
1989 box_test(3)=get(handles.PATCH1,'Value');
1990 box_test(4)=get(handles.CIV2,'Value');
1991 box_test(5)=get(handles.FIX2,'Value');
1992 box_test(6)=get(handles.PATCH2,'Value');
1993 index=find(box_test==1);
1994 if isempty(index)
1995     errordlg('no selected operation')
1996     set(handles.BATCH, 'Enable','On')
1997     set(handles.BATCH,'BackgroundColor',[1 0 0])
1998     return
1999 end
2000 index_first=min(index);
2001 index_last=max(index);
2002 box_used=box_test([index_first : index_last]);
2003 [box_missing,ind_missing]=min(box_used);
2004 if isequal(box_missing,0)
2005     errordlg(['missing' cell2mat(operations(ind_missing))]);
2006     set(handles.BATCH, 'Enable','On')
2007     set(handles.BATCH,'BackgroundColor',[1 0 0])
2008     return
2009 end
2010 
2011 %root name
2012 filebase=get(handles.displ_filebase,'String');
2013 if isempty(filebase)||isequal(filebase,'')
2014     msgbox_uvmat('ERROR','no input files')
2015     return
2016 end
2017     
2018 %check mask if selecetd
2019 if isequal(get(handles.get_mask_civ1,'Value'),1)
2020      get_mask_civ1_Callback(hObject, eventdata, handles);
2021 end
2022 if isequal(get(handles.get_mask_fix1,'Value'),1)
2023      get_mask_fix1_Callback(hObject, eventdata, handles);
2024 end
2025 if isequal(get(handles.get_mask_civ2,'Value'),1)
2026      get_mask_civ2_Callback(hObject, eventdata, handles);
2027 end
2028 if isequal(get(handles.get_mask_fix2,'Value'),1)
2029      get_mask_fix2_Callback(hObject, eventdata, handles);
2030 end
2031 
2032 %read names of the .exe file
2033 path_uvmat=which('uvmat');% check the path detected for source file uvmat
2034 path_UVMAT=fileparts(path_uvmat); %path to UVMAT
2035 if isunix
2036     xmlfile=fullfile(path_UVMAT,'PARAM_LINUX.xml');
2037     if exist(xmlfile,'file')
2038         t=xmltree(xmlfile);
2039         sparam=convert(t);
2040     end
2041 else
2042     xmlfile=fullfile(path_UVMAT,'PARAM_WIN.xml');
2043     if exist(xmlfile,'file')
2044         t=xmltree(xmlfile);
2045         sparam=convert(t);
2046     end
2047 end
2048  sge=0;
2049     if isfield(sparam,'Civ_exe')
2050         Civ_exe=sparam.Civ_exe
2051     end
2052     if isfield(sparam,'Civ1_exe')
2053         civ1_exe=sparam.Civ1_exe
2054     end
2055     if isfield(sparam,'Civ2_exe')
2056         civ2_exe=sparam.Civ2_exe
2057     end
2058     if isfield(sparam,'Patch_exe')
2059         patch_exe=sparam.Patch_exe
2060     end
2061      if isfield(sparam,'PatchNew_exe')
2062         patch_new_exe=sparam.PatchNew_exe
2063      end
2064      if isfield(sparam,'Fix_exe')
2065         fix_exe=sparam.Fix_exe
2066      end
2067      if isfield(sparam,'Todo_path')
2068         todo_path=sparam.Todo_path
2069      end
2070     if isfield(sparam,'SGE')
2071         sge=str2num(sparam.SGE)
2072     end 
2073     
2074 %choice of batch priority
2075 ind_answer=2;
2076 if sge
2077     [s,w]=unix('qstat -q civ.q|grep job_| wc -l'); %check the waiting list (command unix)
2078     if isequal(s,0)
2079         w(end)=[];
2080         str_displ={[w ' jobs in the waiting list'];'Select a priority:'};
2081         str={'urgent';'normal';'low'};
2082         [ind_answer,v] = listdlg('PromptString',str_displ,...
2083                 'SelectionMode','single',...
2084                 'ListString',str,'ListSize',[200 200],'Name','job priority','InitialValue',3);
2085         if isequal(v,0) % to handle Cancel button and figure close,
2086            return % a better way should be create
2087         end
2088     else
2089         warndlg_uvmat('batch system not available','ERROR')
2090         return
2091     end
2092 end
2093 
2094 %initialize the waitbars
2095 set(handles.waitbar_1,'Position',[0.946 0.876 0.03 0.001])
2096 set(handles.waitbar_patch1,'Position',[0.946 0.439 0.03 0.001])
2097 set(handles.waitbar_civ2,'Position',[0.946 0.219 0.03 0.001])
2098 set(handles.waitbar_patch2,'Position',[0.946 0.0 0.03 0.001])
2099 set(handles.BATCH, 'Enable','Off')
2100 set(handles.BATCH,'BackgroundColor',[0.831 0.816 0.784])
2101 
2102 %get the filename root, nomenclature and numbers
2103 
2104 % for Windows system find the UBC path name if needed
2105 if ~isunix & isequal(todo_path(1:2),'\\') & isequal(filebase(2:3),':\')
2106     cur_dir=pwd;
2107     if ~isequal(cur_dir(2:3),':\')
2108         cd(matlabroot); %move to the Matlab root directory if the current Matlab dir does not allow the dos command or is M:
2109     end
2110     [ss,ww]=dos(['net use ' filebase(1:2)]);
2111     if isequal(ss,0)
2112         rankpath=findstr(ww,'\\');
2113         if ~isempty(rankpath)
2114             wwrest=ww(rankpath:end);
2115             rankend=min(find(double(wwrest)==10))-1;
2116             filebase=[wwrest(1:rankend) filebase(3:end)];
2117             set(handles.displ_filebase,'String',filebase);
2118         end
2119     else
2120          warndlg_uvmat('for BATCH option, UBC file names, beginning by \\, are needed','ERROR');
2121          set(handles.BATCH, 'Enable','On')
2122          set(handles.BATCH,'BackgroundColor',[1 0 0])
2123          return
2124     end
2125 end
2126 
2127 [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]=...
2128        set_civ_filenames(handles,compare,box_test);
2129   
2130 nbfield=size(num1_civ1,2);
2131 nbslice=size(num1_civ1,1);
2132 
2133 
2134 %GET PARAMETERS:
2135   %get civ parameters
2136 if box_test(1)==1
2137     par_civ1=read_param_civ1(handles,filecell.ima1.civ1{1,1});
2138 end 
2139 
2140 %get fix1 parameters
2141 if box_test(2)==1 
2142     flagindex1(1)=get(handles.vec_Fmin2, 'Value');
2143     flagindex1(2)=get(handles.vec_F3, 'Value');
2144     flagindex1(3)=get(handles.vec_F2, 'Value');
2145     thresh_vecC1=str2num(get(handles.thresh_vecC,'String'));%threshold on image correlation vec_C
2146     thresh_vel1=str2num(get(handles.thresh_vel,'String'));%threshold on velocity modulus
2147     test_mask=get(handles.get_mask_fix1,'Value');
2148     
2149     nbslice_mask=get(handles.mask_fix1,'UserData'); % get the number of slices (= number of masks)
2150     %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX
2151 %     inf_sup=get(handles.inf_sup1,'Value');
2152 %     fileref=get(handles.ref_fix1,'String');
2153 %     refpath=get(handles.ref_fix1,'UserData');
2154 %     fileref=fullfile(refpath,fileref);
2155     menu=get(handles.field_ref1,'String');
2156     index=get(handles.field_ref1,'Value');
2157     if isempty(menu)
2158         fieldchoice='';
2159     else
2160         fieldchoice=menu{index};
2161          warndlg('reference field is not used presently with batch, use RUN option')
2162     end   
2163 end
2164  
2165  %get patch1 parameters
2166  if box_test(3)==1
2167         rho_patch1=str2num(get(handles.rho_patch1,'String'));
2168         if isempty(rho_patch1)
2169             rho_patch1='1000';
2170             set(handles.rho_patch1,'String','1')
2171         else
2172             rho_patch1=num2str(1000*rho_patch1);
2173         end
2174         nx_patch1=get(handles.nx_patch1,'String');
2175         ny_patch1=get(handles.ny_patch1,'String');
2176         if isequal(str2num(nx_patch1),[])
2177             nx_patch1='50' ;%default
2178             set(handles.nx_patch1,'String','50');
2179         end
2180         if isequal(str2num(ny_patch1),[])
2181             ny_patch1='50' ;%default
2182             set(handles.ny_patch1,'String','50');
2183         end
2184         subdomain_patch1=get(handles.subdomain_patch1,'String');
2185         thresh_patch1=get(handles.thresh_patch1,'String');
2186         test_interp=get(handles.test_interp,'Value');
2187  end
2188  
2189  %get civ2 parameters
2190 if box_test(4)==1
2191     par_civ2=read_param_civ2(handles,cell2mat(filecell.ima1.civ2(1,1)));
2192 end
2193 
2194 %get fix2 parameters
2195 if box_test(5)==1 
2196     flagindex2(1)=get(handles.vec_Fmin2_2, 'Value');
2197     flagindex2(2)=get(handles.vec_F3_2, 'Value');
2198     flagindex2(3)=get(handles.vec_F4, 'Value');
2199     thresh_vec2C=str2num(get(handles.thresh_vec2C,'String'));%threshold on image correlation vec_C
2200     thresh_vel2=str2num(get(handles.thresh_vel2,'String'));%threshold on velocity modulus
2201     test_mask=get(handles.get_mask_fix2,'Value');
2202     nbslice_mask=get(handles.mask_fix2,'UserData'); % get the number of slices (= number of masks)
2203         %%%%%%%%%%%%%COMPLETER LE PROGRAMME FIX AVEC REF FILE ET OPTION inf_sup=2
2204 %     inf_sup=get(handles.inf_sup2,'Value');
2205 %     ref=get(handles.ref_fix2,'UserData');
2206 
2207     %%%%%%%%%%%%%%%%%%%
2208 end
2209 
2210 
2211 %get patch2 parameters
2212 if box_test(6)==1
2213     rho_patch2=str2num(get(handles.rho_patch2,'String'));
2214     if isempty(rho_patch2)
2215         rho_patch2='1000';
2216         set(handles.rho_patch2,'String','1')
2217     else
2218         rho_patch2=num2str(1000*rho_patch2);
2219     end
2220     nx_patch2=get(handles.nx_patch2,'String');
2221     ny_patch2=get(handles.ny_patch2,'String');
2222     if isequal(str2num(nx_patch2),[])
2223         nx_patch2='50' ;%default
2224         set(handles.nx_patch2,'String','50');
2225     end
2226     if isequal(str2num(ny_patch2),[])
2227         ny_patch2='50' ;%default
2228         set(handles.ny_patch2,'String','50');
2229     end
2230     subdomain_patch2=get(handles.subdomain_patch2,'String');
2231     thresh_patch2=get(handles.thresh_patch2,'String');
2232     %         test_interp=get(handles.test_interp,'Value');
2233 end
2234 
2235 if ~sge
2236        
2237     %OPEN THE WAIT LIST FOR BATCH PROCESSES
2238     name_lock=fullfile(todo_path,'lock'); %lock file
2239     iwait=0;
2240     while(exist(name_lock) & iwait<15)
2241         pause(1); %wait 1 second
2242         iwait=iwait+1;
2243     end
2244     if iwait==15
2245         errordlg(['I''m tired to wait for the lock file, please delete it then click again on BATCH' name_lock ])
2246         set(handles.BATCH, 'Enable','On')
2247         set(handles.BATCH,'BackgroundColor',[1 0 0])
2248         return
2249     end
2250     p0=fopen(name_lock,'w'); %create the file name_lock: prevents other users to interfere
2251     name_todo=fullfile(todo_path,'TODO.txt');
2252     p1=fopen(name_todo,'a');
2253     if (p1<0)
2254         errordlg(['error in opening ' name_todo])
2255         set(handles.BATCH, 'Enable','On')
2256         set(handles.BATCH,'BackgroundColor',[1 0 0])
2257         return;
2258     end
2259 end
2260 
2261 %MAIN LOOP
2262 % for ifile=1:nbfield
2263 p1text=[];%initiate command text
2264 time=get(handles.displ_filebase,'UserData'); %get the set of times
2265 civAll=get(handles.Experimental,'Value'); % Boolean for new civ excution method
2266 if isequal(civAll,1)
2267     civAllxml=xmltree;% xml contents,  all parameters
2268     civAllCmd=[];
2269    civAllxml=set(civAllxml,1,'name','CivDoc');
2270 end 
2271 for ifile=1:nbfield
2272     for j=1:nbslice
2273         i_cmd=0; 
2274         cmd='';
2275         if sge
2276            cmd='#!/bin/bash';
2277            cmd=char({cmd;'#$ -cwd'});
2278            cmd=char({cmd;'hostname && date'});
2279         end
2280             
2281         filename_cmx=cell2mat(filecell.nc.civ1(ifile,j));%output netcdf file
2282         filename_cmx([end-1:end])='cm';%name of cmx file
2283         filename_cmx=[filename_cmx 'x'];
2284         
2285    %CIV1
2286         if box_test(1)==1
2287             par_civ1.filename_ima_a=cell2mat(filecell.ima1.civ1(ifile,j));
2288            % par_civ1.filename_ima_a([end-3:end])=[];%remove .png extension
2289             par_civ1.filename_ima_b=cell2mat(filecell.ima2.civ1(ifile,j));
2290            % par_civ1.filename_ima_b([end-3:end])=[];%remove .png extension
2291          
2292             namelog=[filename_cmx([1:end-3]) 'log'];
2293             par_civ1.Dt=num2str(time(num2_civ1(ifile),num_b_civ1(j))-time(num1_civ1(ifile),num_a_civ1(j)));
2294             par_civ1.T0=num2str((time(num2_civ1(ifile),num_b_civ1(j))+time(num1_civ1(ifile),num_a_civ1(j)))/2); 
2295             par_civ1.term_a=num2stra(num_a_civ1(j),nom_type_nc);%UTILITE?
2296             par_civ1.term_b=num2stra(num_b_civ1(j),nom_type_nc);%
2297             test_mask=get(handles.get_mask_civ1,'Value');
2298             if test_mask==0  
2299                 par_civ1.maskname='noFile use default';
2300                 par_civ1.maskflag='n';
2301             else
2302                 maskdispl=get(handles.mask_civ1,'String');
2303                 maskbase=[filebase '_' maskdispl]; %
2304                 par_civ1.maskname=name_generator(maskbase,num1_civ1(ifile),1,'.png','_i');
2305                 if exist(par_civ1.maskname,'file')
2306                     par_civ1.maskflag='y'; 
2307                 else
2308                     par_civ1.maskname='noFile use default';
2309                     par_civ1.maskflag='n'; 
2310                 end
2311             end
2312             i_cmd=i_cmd+1;
2313             if isequal(civAll,0)
2314                 cmd=char({cmd;BATCH_CIV1(filename_cmx([1:end-4]),namelog,par_civ1,handles)});
2315             else
2316                  civAllCmd=[civAllCmd ' civ1 '];
2317                  str=BATCH_CIV1_Unified(filename_cmx([1:end-4]),namelog,par_civ1,handles);
2318                  fieldnames=fields(str);
2319                 [civAllxml,uid_civ1]=add(civAllxml,1,'element','civ1');
2320                 for ilist=1:length(fieldnames)
2321                   val=eval(['str.' fieldnames{ilist}]);
2322                   if ischar(val)
2323                     [civAllxml,uid_t]=add(civAllxml,uid_civ1,'element',fieldnames{ilist});
2324                     [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2325                    end
2326                 end   
2327             end
2328         end
2329         
2330    % FIX1
2331        if box_test(2)==1
2332            test_mask=get(handles.get_mask_fix1,'Value');
2333            if test_mask==0
2334                 maskname='';
2335            else
2336                 maskdispl=get(handles.mask_fix1,'String');
2337                 maskbase=[filebase '_' maskdispl]; %
2338                 maskname=name_generator(maskbase,num1_civ1(ifile),1,'.png','_i');
2339            end
2340            if isequal(civAll,0)
2341            cmd_FIX=[fix_exe ' -f ' filecell.nc.civ1{ifile,j} ' -fi1 ' num2str(flagindex1(1)) ...
2342                    ' -fi2 ' num2str(flagindex1(2)) ' -fi3 ' num2str(flagindex1(3)) ...
2343                    ' -threshC ' num2str(thresh_vecC1) ' -threshV ' num2str(thresh_vel1) ' -maskName ' maskname];
2344            cmd=char({cmd ;cmd_FIX});
2345            else
2346                 fix1.inputFileName=filecell.nc.civ1{ifile,j} ;
2347                 fix1.fi1=num2str(flagindex1(1));
2348                 fix1.fi2=num2str(flagindex1(2));
2349                 fix1.fi3=num2str(flagindex1(3));
2350                 fix1.threshC=num2str(thresh_vecC1);
2351                 fix1.threshV=num2str(thresh_vel1);                 
2352                 fieldnames=fields(fix1);
2353                 [civAllxml,uid_fix1]=add(civAllxml,1,'element','fix1');
2354                 for ilist=1:length(fieldnames)
2355                   val=eval(['fix1.' fieldnames{ilist}]);
2356                   if ischar(val)
2357                     [civAllxml,uid_t]=add(civAllxml,uid_fix1,'element',fieldnames{ilist});
2358                     [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2359                    end
2360                 end 
2361              civAllCmd=[civAllCmd ' fix1 '];           
2362            end          
2363        end
2364    
2365     %PATCH1
2366     if box_test(3)==1
2367         if isequal(civAll,0)
2368         cmd_PATCH=RUN_PATCH(filecell.nc.civ1{ifile,j},nx_patch1,ny_patch1,rho_patch1,subdomain_patch1,thresh_patch1,test_interp);
2369         cmd=char({cmd ;cmd_PATCH});
2370         else
2371             patch1.inputFileName=filecell_nc1{ifile,j} ;
2372             patch1.nopt=subdomain_patch1;
2373             patch1.maxdiff=thresh_patch1;
2374             patch1.ro=rho_patch1;
2375             patch1.m=nx_patch1;
2376             patch1.n=ny_patch1;
2377             grid_tmp=get(handles.grid_patch1,'String');
2378             if ~isequal(grid_tmp,'none')
2379                 patch1.gridPatch=grid_tmp;
2380             end
2381             patch1.convectFlow='n';
2382                fieldnames=fields(patch1);
2383                 [civAllxml,uid_patch1]=add(civAllxml,1,'element','patch1');
2384                 for ilist=1:length(fieldnames)
2385                   val=eval(['patch1.' fieldnames{ilist}]);
2386                   if ischar(val)
2387                     [civAllxml,uid_t]=add(civAllxml,uid_patch1,'element',fieldnames{ilist});
2388                     [civAllxml,uid_t2]=add(civAllxml,uid_t,'chardata',val);
2389                    end
2390                 end 
2391                
2392             civAllCmd=[civAllCmd ' patch1 '];
2393         end
2394     end
2395     
2396     if box_test(4)==1 | box_test(5)==1 | box_test(6)==1
2397         filename_cmx=filecell.nc.civ2{ifile,j};%output netcdf file
2398         filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
2399         filename_cmx=[filename_cmx 'x'];
2400     end
2401     
2402     if box_test(4)==1
2403             par_civ2.filename_ima_a=filecell.ima1.civ2{ifile,j};
2404             par_civ2.filename_ima_a([end-3:end])=[];%remove .png extension
2405             par_civ2.filename_ima_b=filecell.ima2.civ2{ifile,j};
2406             par_civ2.filename_ima_b([end-3:end])=[];%remove .png extension
2407             namelog=[filename_cmx([1:end-3]) 'log'];
2408             par_civ2.Dt=num2str(time(num2_civ2(ifile),num_b_civ2(j))-time(num1_civ2(ifile),num_a_civ2(j)));
2409             par_civ2.T0=num2str((time(num2_civ1(ifile),num_b_civ2(j))+time(num1_civ2(ifile),num_a_civ2(j)))/2); 
2410             par_civ2.term_a=num2stra(num_a_civ2(j),nom_type_nc);
2411             par_civ2.term_b=num2stra(num_b_civ2(j),nom_type_nc);         
2412             par_civ2.filename_nc1=filecell.nc.civ1{ifile,j};
2413             par_civ2.filename_nc1([end-2:end])=[]; % remove '.nc'
2414             test_mask=get(handles.get_mask_civ2,'Value');
2415             if test_mask==0 
2416                 par_civ2.maskname='noFile use default';
2417                 par_civ2.maskflag='n';
2418             else
2419                 maskdispl=get(handles.mask_civ2,'String');
2420                 maskbase=[filebase '_' maskdispl]; %
2421                 par_civ2.maskname=name_generator(maskbase,num1_civ2(ifile),1,'.png','_i');
2422                 if exist(par_civ2.maskname,'file')
2423                     par_civ2.maskflag='y'; 
2424                 else
2425                     par_civ2.maskname='noFile use default';
2426                     par_civ2.maskflag='n'; 
2427                 end
2428             end
2429             i_cmd=i_cmd+1;
2430             cmd_CIV2=BATCH_CIV2(filename_cmx,namelog,par_civ2);
2431     if(isunix)
2432         cmd=char({cmd ; ['cp -f ' filename_cmx '2 ' filename_cmx]; cmd_CIV2});
2433     else
2434         cmd=char({cmd ; ['copy /Y ' filename_cmx '2 ' filename_cmx]; cmd_CIV2});
2435     end
2436      end
2437      
2438      % FIX2
2439        if box_test(5)==1
2440            test_mask=get(handles.get_mask_fix2,'Value');    
2441             if test_mask==0  
2442                 maskname=''; %no mask used
2443             else
2444                 maskdispl=get(handles.mask_fix2,'String');
2445                 maskbase=[filebase '_' maskdispl]; %
2446                 maskname =name_generator(maskbase,num1_civ2(ifile),1,'.png','_i');
2447             end
2448            cmd_FIX=[fix_exe ' -f ' filecell.nc.civ2{ifile,j} ' -fi1 ' num2str(flagindex2(1)) ...
2449                    ' -fi2 ' num2str(flagindex2(2)) ' -fi3 ' num2str(flagindex2(3)) ...
2450                    ' -threshC ' num2str(thresh_vec2C) ' -threshV ' num2str(thresh_vel2) ' -maskName ' maskname];
2451            cmd=char({cmd ;cmd_FIX});
2452        end
2453        
2454      %PATCH2
2455     if box_test(6)==1
2456         cmd_PATCH=RUN_PATCH(filecell.nc.civ2{ifile,j},nx_patch2,ny_patch2,rho_patch2,subdomain_patch2,thresh_patch2,0);
2457         cmd=char({cmd; cmd_PATCH});
2458     end
2459      
2460      if isequal(civAll,1)
2461         save(civAllxml,[filename_cmx([1:end-4]) '.xml']);
2462         cmd=char({cmd;[Civ_exe ' -f ' [filename_cmx([1:end-4]) '.xml'] ' ' civAllCmd]});
2463         
2464          %save(civAllxml,'/tmp/test.xml');
2465      end
2466               
2467   % create the .bat file:
2468     if sge
2469             [Rootbat,Filebat,extbat]=fileparts(filename_cmx);
2470             filename_bat=fullfile(Rootbat,['job_' Filebat extbat]);
2471     else
2472           filename_bat=filename_cmx;
2473     end
2474   filename_bat(end-2:end)='bat';
2475 
2476     dlmwrite(filename_bat,cmd,'');%write commands in filename_bat
2477     if sge
2478         pvalue=num2str((1-ind_answer)*500);
2479         namelog=[filename_bat '.patch.log'];
2480         ['!qsub -p ' pvalue ' -q civ.q -e ' filename_cmx(1:end-4) '.errors -o ' filename_cmx(1:end-4) '.log' ' ' filename_bat];
2481         eval(  ['!qsub -p ' pvalue ' -q civ.q -e ' filename_cmx(1:end-4) '.errors -o ' filename_cmx(1:end-4) '.log' ' ' filename_bat]);
2482     else
2483         if(isunix)
2484           cmdtodo=['. ' filename_bat ];%removed for Mathieu tests %' && rm -f ' filename_bat] ;
2485         else
2486            cmdtodo=[filename_bat];%removed for Mathieu tests %' && del /F /Q ' filename_bat' ;
2487         end
2488         count= fprintf(p1,'%s\n', cmdtodo);
2489     end
2490  end
2491 end
2492 if ~sge
2493     fclose(p1);
2494     fclose(p0);
2495     delete(name_lock);
2496 end
2497 
2498 set(handles.BATCH, 'Enable','On')
2499 set(handles.BATCH,'BackgroundColor',[1 0 0])
2500 
2501 %save interface state
2502 if isfield(filecell,'nc')
2503     if isfield(filecell.nc,'civ2')
2504         fileresu=filecell.nc.civ2{1,1};
2505     else
2506         fileresu=filecell.nc.civ1{1,1};
2507     end
2508 end
2509 [RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileresu);
2510 namedoc=fullfile(RootPath,subdir,RootFile);
2511 detect=1; 
2512 while detect==1
2513     namefigfull=[namedoc '.fig'];
2514     hh=dir(namefigfull);
2515     if ~isempty(hh)
2516         detect=1;
2517         namedoc=[namedoc '.0'];
2518     else
2519         detect=0;
2520     end
2521 end
2522 saveas(gcbf,namefigfull);%save the interface with name namefigfull (A CHANGER EN FICHIER  .xml)
2523 
2524 
2525 %----------------------------------------------------------
2526 %OUTPUT:
2527 % filecell: structure of input and output files
2528 %         .ima1.civ1{i,j}, .ima1.civ2{i,j}; set of first image names for  correlations, for civ1 and civ2
2529 %         .ima2.civ1{i,j}, .ima2.civ2{i,j} ; set of second image names for correlations
2530 %         .imaA1.civ1{i,j}, .ima1.civ2{i,j}; set of first image names for correlations, for civ1 and civ2, with camA in stereo case (then .ima1 corresponds to camB)
2531 %         .imaA2.civ1{i,j}, .ima2.civ2{i,j} ; set of second image names for  correlations, with camA in stereo case (then .ima1 corresponds to camB)
2532 %         .nc.civ1{i,j}, .nc.civ2{i,j}; set of nc files for PIV results
2533 %         .ncA.civ1{i,j}, .ncA.civ2{i,j}; set of nc files for PIV results with camA (then .nc corresponds to camB)
2534 %         .st{i,j};   set of nc files for the combined stereo fields
2535 
2536 function [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]=...
2537     set_civ_filenames(handles,compare,box_test)
2538 
2539 %get the filename root, nomenclature and numbers
2540 filebase=get(handles.displ_filebase,'String');
2541 browse=get(handles.browse_root,'UserData');
2542 compare_list=get(handles.compare,'String');
2543 val=get(handles.compare,'Value');
2544 compare=compare_list{val};
2545 if strcmp(compare,'displacement')
2546     mode='displacement';
2547 else
2548     mode_list=get(handles.mode,'String');
2549     mode_value=get(handles.mode,'Value');
2550     mode=mode_list{mode_value};
2551 end
2552 time=get(handles.displ_filebase,'UserData'); %get the set of times
2553 ext_ima=get(handles.ext_ima,'String');
2554 nom_type_nc=browse.nom_type_nc;
2555 nom_type_ima2=browse.nom_type_ima;
2556 if isequal(nom_type_ima2,[]),nom_type_ima2='ima_num';end; %default
2557 if isequal(nom_type_nc,[]),nom_type_nc='_i1-i2';end; %default
2558 [num1_civ1,num2_civ1,num_a_civ1,num_b_civ1,num1_civ2,num2_civ2,num_a_civ2,num_b_civ2]=...
2559     find_pair_indices(handles,mode);
2560 
2561 %determine the new filebase for 'displacement' mode (comparison of two series)
2562 filebase_B=filebase;% root name of the second field series for stereo
2563 if strcmp(compare,'displacement') || strcmp(compare,'stereo PIV')
2564     test_disp=1;   
2565     nom_type_ima1=browse.nom_type_ima_1; %nomenclature type of the second file series
2566     [Path2,Name2]=fileparts(filebase_B);
2567     Path1=Path2;
2568     Name1=get(handles.displ_filebase2,'String');% root name of the first field series for stereo
2569     filebase_A=fullfile(Path1,Name1);
2570     if length(Name1)>6
2571         Name1=Name1(end-5:end);
2572     end
2573     if length(Name2)>6
2574         Name2=Name2(end-5:end);
2575     end
2576     filebase_AB=fullfile(Path2,[Name2 '-' Name1]);
2577 else
2578     test_disp=0;
2579     filebase_A=filebase;
2580     nom_type_ima1=nom_type_ima2;
2581     filebase_AB=filebase;
2582 end
2583 if strcmp(compare,'displacement')
2584         filebase_ima1=filebase_A;
2585         filebase_ima2=filebase_B;
2586         filebase_nc=filebase_AB; %root name for the result of civ2
2587 else
2588         filebase_ima1=filebase_B;
2589         filebase_ima2=filebase_B;
2590         filebase_nc=filebase_B;
2591 end    
2592 
2593 %determine reference files for fix:
2594 file_ref_fix1={};%default
2595 file_ref_fix2={};
2596 nbfield=length(num1_civ1);
2597 nbslice=length(num_a_civ1);
2598 if box_test(2)==1% fix1 performed
2599     ref=get(handles.ref_fix1,'UserData');%read data on the ref file stored by get_ref_fix1_Callback
2600     if ~isempty(ref)
2601         first_i=str2num(get(handles.first_i,'String'));
2602         last_i=str2num(get(handles.last_i,'String'));
2603         incr_i=str2num(get(handles.incr_i,'String'));
2604         first_j=str2num(get(handles.first_j,'String'));
2605         last_j=str2num(get(handles.last_j,'String'));
2606         incr_j=str2num(get(handles.incr_j,'String'));
2607         num_i_ref=[first_i:incr_i:last_i];
2608         num_j_ref=[first_j:incr_j:last_j];
2609         if isequal(mode,'displacement')
2610             num_i1=num_i_ref;
2611             num_i2=num_i_ref;
2612             num_j1=num_j_ref;
2613             num_j2=num_j_ref;
2614         elseif isequal(mode,'pair j1-j2')% isequal(mode,'st_pair j1-j2')
2615             num_i1=num_i_ref;
2616             num_i2=num_i1;
2617             num_j1=ref.num_a*ones(size(num_i_ref));
2618             num_j2=ref.num_b*ones(size(num_i_ref));
2619         elseif isequal(mode,'series(Di)') % isequal(mode,'st_series(Di)')
2620             delta1=floor((ref.num2-ref.num1)/2);
2621             delta2=ceil((ref.num2-ref.num1)/2);
2622             num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2623             num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2624             if isempty(ref.num_a)
2625                 ref.num_a=1;
2626             end
2627             num_j1=ref.num_a*ones(size(num_i1));
2628             num_j2=num_j1;
2629         elseif isequal(mode,'series(Dj)')%| isequal(mode,'st_series(Dj)')
2630             delta1=floor((ref.num_b-ref.num_a)/2);
2631             delta2=ceil((ref.num_b-ref.num_a)/2);
2632             num_i1=ref.num1*ones(size(num_i_ref));
2633             num_i2=num_i1;
2634             num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2635             num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2636         end
2637         for ifile=1:nbfield
2638             for j=1:nbslice
2639                 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);%
2640                 file_ref_fix1(ifile,j)={file_ref};
2641                 if ~exist(file_ref,'file')
2642                     warndlg_uvmat(['reference file ' file_ref ' not found for fix1'],'ERROR')
2643                     set(handles.RUN, 'Enable','On')
2644                     set(handles.RUN,'BackgroundColor',[1 0 0])
2645                     set(handles.BATCH, 'Enable','On')
2646                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2647                     return
2648                 end
2649             end
2650         end
2651     end
2652 end
2653 
2654 %determine reference files for fix2:
2655 if box_test(5)==1% fix2 performed
2656     ref=get(handles.ref_fix2,'UserData');
2657     if ~isempty(ref)
2658         first_i=str2num(get(handles.first_i,'String'));
2659         last_i=str2num(get(handles.last_i,'String'));
2660         incr_i=str2num(get(handles.incr_i,'String'));
2661         first_j=str2num(get(handles.first_j,'String'));
2662         last_j=str2num(get(handles.last_j,'String'));
2663         incr_j=str2num(get(handles.incr_j,'String'));
2664         num_i_ref=[first_i:incr_i:last_i];
2665         num_j_ref=[first_j:incr_j:last_j];
2666         if isequal(mode,'displacement')
2667             num_i1=num_i_ref;
2668             num_i2=num_i_ref;
2669             num_j1=num_j_ref;
2670             num_j2=num_j_ref;
2671         elseif isequal(mode,'pair j1-j2')
2672             num_i1=num_i_ref;
2673             num_i2=num_i1;
2674             num_j1=ref.num_a;
2675             num_j2=ref.num_b;
2676         elseif isequal(mode,'series(Di)')
2677             delta1=floor((ref.num2-ref.num1)/2);
2678             delta2=ceil((ref.num2-ref.num1)/2);
2679             num_i1=num_i_ref-delta1*ones(size(num_i_ref));
2680             num_i2=num_i_ref+delta2*ones(size(num_i_ref));
2681             num_j1=ref.num_a*ones(size(num_i1));
2682             num_j2=num_j1;
2683         elseif isequal(mode,'series(Dj)')
2684             delta1=floor((ref.num_b-ref.num_a)/2);
2685             delta2=ceil((ref.num_b-ref.num_a)/2);
2686             num_i1=ref.num1*ones(size(num_i_ref));
2687             num_i2=num_i1;
2688             num_j1=num_j_ref-delta1*ones(size(num_j_ref));
2689             num_j2=num_j_ref+delta2*ones(size(num_j_ref));
2690         end
2691         for ifile=1:nbfield
2692             for j=1:nbslice
2693                 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);%
2694                 file_ref_fix2(ifile,j)={file_ref};
2695                 if ~exist(file_ref,'file')
2696                     warndlg_uvmat(['reference file ' file_ref ' not found for fix2'],'ERROR')
2697                     set(handles.RUN, 'Enable','On')
2698                     set(handles.RUN,'BackgroundColor',[1 0 0])
2699                            set(handles.BATCH, 'Enable','On')
2700                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2701                     return
2702                 end
2703             end
2704         end
2705     end
2706 end
2707 
2708 %check dir
2709 subdir_civ1=get(handles.subdir_civ1,'String');%subdirectory subdir_civ1 for the netcdf output data
2710 subdir_civ2=get(handles.subdir_civ2,'String');
2711 if isequal(subdir_civ1,''),subdir_civ1='A'; end% put default subdir
2712 if isequal(subdir_civ2,''),subdir_civ2=subdir_civ1; end% put default subdir
2713 currentdir=pwd;%store the current working directory
2714 [Path_ima,Name]=fileparts(filebase);%Path of the image files (.civ)
2715 if ~exist(Path_ima,'dir')
2716     warndlg_uvmat(['path to images ' Path_ima ' not found'],'ERROR')
2717     return
2718 end
2719 cd(Path_ima);%move to the directory of the images: needed to create the result dir by 'mkdir'
2720 dircur=pwd; %current working directory
2721 m2='';
2722 [erread,message]=fileattrib(Path_ima);
2723 if ~isempty(message) & ~isequal(message.UserWrite,1)
2724       errordlg(['No writting access to ' Path_ima])
2725       cd(currentdir);
2726       return
2727 end
2728 
2729 %check the existence of the netcdf and image files involved
2730 % %%%%%%%%%%%%  case CIV1 activated   %%%%%%%%%%%%%
2731 if box_test(1)==1;
2732      detect=1;     
2733      while detect==1 %create a new subdir if the netcdf files already exist
2734           for ifile=1:nbfield;
2735               for j=1:nbslice
2736                   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);
2737                   detect=exist(filename,'file')==2;
2738                   if detect% if a netcdf file already exists
2739                      subdir_civ1=[subdir_civ1 '.0'];
2740                      subdir_civ2=subdir_civ1;
2741                      break
2742                   end
2743                   filecell.nc.civ1(ifile,j)={filename};
2744               end
2745               if detect% if a netcdf file already exists
2746                   break
2747               end
2748           end
2749            %create the new subdir_civ1
2750           if ~exist(fullfile(Path_ima,subdir_civ1),'dir')
2751               [m1,m2,m3]=mkdir(subdir_civ1)
2752                 if ~isequal(m2,'')
2753                    msgbox_uvmat('ERROR', m2)
2754                    %msgbox(m2);%error message for directory creation
2755               end
2756           end
2757           if strcmp(compare,'stereo PIV')&&(strcmp(mode,'pair j1-j2')||strcmp(mode,'series(Dj)')||strcmp(mode,'series(Di)'))%check second nc series
2758                 for ifile=1:nbfield
2759                      for j=1:nbslice
2760                          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);%
2761                           detect=exist(filename,'file')==2;
2762                           if detect% if a netcdf file already exists
2763                               subdir_civ1=[subdir_civ1 '.0'];
2764                               subdir_civ2=subdir_civ1;
2765                               break
2766                           end
2767                           filecell.ncA.civ1(ifile,j)={filename};
2768                      end
2769                     if detect% if a netcdf file already exists
2770                          break
2771                     end
2772               end
2773            %create the new subdir_civ1
2774                if ~exist(fullfile(Path_ima,subdir_civ1),'dir')
2775                       [m1,m2,m3]=mkdir(subdir_civ1);
2776                        if ~isequal(m2,'')
2777                             msgbox_uvmat('ERROR', m2)
2778                            % msgbox(m2);%error message for directory creation
2779                       end
2780                end
2781           end
2782      end        
2783     % get image names
2784     for ifile=1:nbfield
2785          for j=1:nbslice
2786             filename=name_generator(filebase_ima1, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2787             idetect(j)=exist(filename,'file')==2;
2788             filecell.ima1.civ1(ifile,j)={filename}; %first image
2789             filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2790             idetect_1(j)=exist(filename,'file')==2;
2791             filecell.ima2.civ1(ifile,j)={filename};%second image
2792          end
2793             [idetectmin,indexj]=min(idetect);
2794           if idetectmin==0,
2795                 warndlg_uvmat([filecell.ima1.civ1{ifile,indexj} ' not found'],'ERROR')
2796                 set(handles.RUN, 'Enable','On')
2797                 set(handles.RUN,'BackgroundColor',[1 0 0])
2798                        set(handles.BATCH, 'Enable','On')
2799                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2800                 cd(currentdir)
2801                 return
2802           end
2803             [idetectmin,indexj]=min(idetect_1);
2804           if idetectmin==0,
2805                 warndlg_uvmat([filecell.ima2.civ1{ifile,indexj} ' not found'],'ERROR')
2806                 set(handles.RUN, 'Enable','On')
2807                 set(handles.RUN,'BackgroundColor',[1 0 0])
2808                        set(handles.BATCH, 'Enable','On')
2809                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2810                 cd(currentdir)
2811                 return
2812           end
2813     end
2814     if strcmp(compare,'stereo PIV') && (strcmp(mode,'pair j1-j2') || strcmp(mode,'series(Dj)') || strcmp(mode,'series(Di)'))
2815          for ifile=1:nbfield
2816              for j=1:nbslice
2817                   filename=name_generator(filebase_A, num1_civ1(ifile),num_a_civ1(j),ext_ima,nom_type_ima1);
2818                   idetect(j)=exist(filename,'file')==2;
2819                   filecell.imaA1.civ1(ifile,j)={filename} ;%first image
2820                   filename=name_generator(filebase_A, num2_civ1(ifile),num_b_civ1(j),ext_ima,nom_type_ima2);
2821                   idetect_1(j)=exist(filename,'file')==2;
2822                    filecell.imaA2.civ1(ifile,j)={filename};%second image
2823              end
2824               [idetectmin,indexj]=min(idetect);
2825               if idetectmin==0,
2826                      warndlg_uvmat([filecell.imaA1.civ1{ifile,indexj} ' not found'],'ERROR')
2827                      set(handles.RUN, 'Enable','On')
2828                       set(handles.RUN,'BackgroundColor',[1 0 0])
2829                              set(handles.BATCH, 'Enable','On')
2830                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2831                       cd(currentdir)
2832                       return
2833               end
2834                [idetectmin,indexj]=min(idetect_1);
2835                if idetectmin==0,
2836                     warndlg_uvmat([filecell.imaA2.civ1{ifile,indexj} ' not found'],'ERROR')
2837                     set(handles.RUN, 'Enable','On')
2838                     set(handles.RUN,'BackgroundColor',[1 0 0])
2839                            set(handles.BATCH, 'Enable','On')
2840                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2841                     cd(currentdir)
2842                     return
2843                end
2844          end
2845     end
2846         
2847  %%%%%%%%%%%%%  fix1 or patch1 activated but no civ1   %%%%%%%%%%%%%
2848 elseif (box_test(2)==1 || box_test(3)==1);  
2849     for ifile=1:nbfield
2850         for j=1:nbslice
2851             filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2852                     nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2853             detect=exist(filename,'file')==2;   
2854             filecell.nc.civ1(ifile,j)={filename};
2855         end
2856     end
2857     if strcmp(compare,'stereo PIV')
2858             for ifile=1:nbfield
2859                 for j=1:nbslice
2860                     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);%
2861                     filecell.ncA.civ1(ifile,j)={filename};
2862                     if ~exist(filename,'file')
2863                         warndlg_uvmat(['input file ' filename ' not found'],'ERROR')
2864                         set(handles.RUN, 'Enable','On')
2865                         set(handles.RUN,'BackgroundColor',[1 0 0])
2866                                set(handles.BATCH, 'Enable','On')
2867                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2868                         cd(currentdir)
2869                         return
2870                     end
2871                 end
2872             end
2873     end
2874 end
2875 %%%%%%%%%%%%%  if civ2 performed with pairs different than civ1  %%%%%%%%%%%%%
2876 if (box_test(4)==1)&...
2877         ((get(handles.list_pair_civ1,'Value')~=get(handles.list_pair_civ2,'Value'))|~isequal(subdir_civ2,subdir_civ1)) 
2878     detect=1; 
2879     while detect==1 %create a new subdir if the netcdf files already exist
2880           for ifile=1:nbfield
2881               for j=1:nbslice
2882                   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);%
2883                   detect=exist(filename,'file')==2;
2884                   if detect% if a netcdf file already exists
2885                      subdir_civ2=[subdir_civ2 '.0'];
2886                      break
2887                   end
2888                   filecell.nc.civ2(ifile,j)={filename};
2889               end
2890               if detect% if a netcdf file already exists
2891                   break
2892               end
2893           end
2894            %create the new subdir_civ2
2895           if ~exist(fullfile(Path_ima,subdir_civ2),'dir')
2896               [m1,m2,m3]=mkdir(subdir_civ2);
2897                 if ~isequal(m2,'')
2898                    msgbox_uvmat('ERROR', m2)
2899                    %msgbox(m2);%error message for directory creation
2900               end
2901           end
2902            if strcmp(compare,'stereo PIV')%check second nc series
2903                 for ifile=1:nbfield
2904                      for j=1:nbslice
2905                          filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2906                           nom_type_nc,1,num2_civ2(ifile),num_b_civ1(j),subdir_civ2);%
2907                           detect=exist(filename,'file')==2;
2908                           if detect% if a netcdf file already exists
2909                               subdir_civ2=[subdir_civ2 '.0'];
2910                               break
2911                           end
2912                           filecell.ncA.civ2(ifile,j)={filename};
2913                      end
2914                     if detect% if a netcdf file already exists
2915                          break
2916                     end
2917               end
2918            %create the new subdir_civ1
2919                if ~exist(fullfile(Path_ima,subdir_civ2),'dir')
2920                       [m1,m2,m3]=mkdir(subdir_civ2);
2921                         if ~isequal(m2,'')
2922                             msgbox_uvmat('ERROR', m2)
2923                             %msgbox(m2);%error message for directory creation
2924                       end
2925                end
2926           end
2927     end  
2928 end
2929     cd(currentdir);%come back to the current working directory
2930     
2931 
2932 %%%%%%%%%%%%%  if civ2 results are obtained or used  %%%%%%%%%%%%%
2933 if box_test(4)==1 | box_test(5)==1 | box_test(6)==1 %civ2
2934     %check source netcdf file of civ1 estimates
2935     if box_test(1)==0; %no civ1 performed
2936         for ifile=1:nbfield
2937             for j=1:nbslice
2938                 filename=name_generator(filebase_nc,num1_civ1(ifile),num_a_civ1(j),'.nc',...
2939                     nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
2940                 filecell.nc.civ1(ifile,j)={filename};
2941                 if ~exist(filename,'file')
2942                     warndlg_uvmat(['input file ' filename ' not found'],'ERROR')
2943                     set(handles.RUN, 'Enable','On')
2944                     set(handles.RUN,'BackgroundColor',[1 0 0])
2945                            set(handles.BATCH, 'Enable','On')
2946                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2947                     return
2948                 elseif box_test(3)==0; %check the existence of patch if it is not calculated
2949 %                     [cte_detect,vdt,cte_read]=read_netcdf(filename,{'patch'});
2950                     Data=nc2struct(filename,[]);
2951                     test_patch=0;
2952                     if isfield(Data,'patch') & isequal(Data.patch,1)
2953                        test_patch=1;
2954                     end
2955                     if isequal(test_patch,0)
2956                         errordlg(['no patch data in ' filename])
2957                         set(handles.RUN, 'Enable','On')
2958                         set(handles.RUN,'BackgroundColor',[1 0 0])
2959                                set(handles.BATCH, 'Enable','On')
2960                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2961                         return
2962                     end
2963                 end          
2964             end
2965         end
2966         if strcmp(compare,'stereo PIV')
2967             for ifile=1:nbfield
2968                 for j=1:nbslice
2969                     filename=name_generator(filebase_A,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2970                         nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
2971                     filecell.ncA.civ2(ifile,j)={filename};
2972                     if ~exist(filename,'file')
2973                         warndlg_uvmat(['input file ' filename ' not found'],'ERROR')
2974                         set(handles.RUN, 'Enable','On')
2975                         set(handles.RUN,'BackgroundColor',[1 0 0])
2976                                set(handles.BATCH, 'Enable','On')
2977                     set(handles.BATCH,'BackgroundColor',[1 0 0])
2978                         cd(currentdir)
2979                         return
2980                     end
2981                 end
2982             end
2983         end
2984     end
2985 
2986     detect=1; 
2987 %     while detect==1%creates a new subdir if the netcdf files already contain civ2 data
2988         for ifile=1:nbfield
2989             for j=1:nbslice
2990                 filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
2991                 nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);
2992                 detect=exist(filename,'file')==2;
2993                 filecell.nc.civ2(ifile,j)={filename};
2994             end
2995         end
2996     %get first image names for civ2
2997     if box_test(1)==1 & isequal(num1_civ1,num1_civ2) & isequal(num_a_civ1,num_a_civ2)
2998         filecell.ima1.civ2=filecell.ima1.civ1;
2999     elseif box_test(4)==1
3000         for ifile=1:nbfield
3001             for j=1:nbslice
3002                 filename=name_generator(filebase_ima1, num1_civ2(ifile),num_a_civ2(j),ext_ima,nom_type_ima1);
3003                 idetect_2(j)=exist(filename,'file')==2;
3004                 filecell.ima1.civ2(ifile,j)={filename};%first image
3005             end
3006                 [idetectmin,indexj]=min(idetect_2);
3007             if idetectmin==0,
3008                     warndlg_uvmat(['input image ' filecell.ima1.civ2{ifile,indexj} ' not found'],'ERROR')
3009                     set(handles.RUN, 'Enable','On')
3010                     set(handles.RUN,'BackgroundColor',[1 0 0])
3011                            set(handles.BATCH, 'Enable','On')
3012                     set(handles.BATCH,'BackgroundColor',[1 0 0])
3013                     return
3014             end
3015         end
3016     end
3017     %get second image names for civ2
3018     if box_test(1)==1 & isequal(num2_civ1,num2_civ2) & isequal(num_b_civ1,num_b_civ2)
3019         filecell.ima2.civ2=filecell.ima2.civ1;
3020     elseif box_test(4)==1
3021         for ifile=1:nbfield
3022             for j=1:nbslice
3023                 filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),ext_ima,nom_type_ima2);
3024                 idetect_3(j)=exist(filename,'file')==2;
3025                 filecell.ima2.civ2(ifile,j)={filename};%first image
3026             end
3027 %             if isequal(ext_title,'.png')
3028                 [idetectmin,indexj]=min(idetect_3);
3029             if idetectmin==0,
3030                     warndl_uvmat(['input image ' filecell.ima2.civ2{ifile,indexj} ' not found'],'ERROR')
3031                     set(handles.RUN, 'Enable','On')
3032                     set(handles.RUN,'BackgroundColor',[1 0 0])
3033                            set(handles.BATCH, 'Enable','On')
3034                     set(handles.BATCH,'BackgroundColor',[1 0 0])
3035 %                     close(hsuper)
3036                     return
3037             end
3038         end
3039     end
3040 end
3041 if (box_test(5)==1 | box_test(6)==1 )& box_test(4)==0  % need to read an existing netcdf civ2 file
3042     for ifile=1:nbfield
3043         for j=1:nbslice
3044             filename=name_generator(filebase_nc,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3045                     nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3046             filecell.nc.civ2(ifile,j)={filename};
3047             if ~exist(filename,'file')
3048                 warndlg_uvmat(['input file ' filename ' not found'],'ERROR')
3049                 set(handles.RUN, 'Enable','On')
3050                 set(handles.RUN,'BackgroundColor',[1 0 0])
3051                        set(handles.BATCH, 'Enable','On')
3052                     set(handles.BATCH,'BackgroundColor',[1 0 0])
3053 %                 close(hsuper)
3054                 return
3055             else
3056 %                 [cte_detect,vdt,cte_read]=read_netcdf(filename,{'civ2'});
3057                 Data=nc2struct(filename,[]);
3058                  test_civ2=0;
3059                 if isfield(Data,'civ2') & isequal(Data.civ2,1)
3060                    test_civ2=1;
3061                 end
3062                 if isequal(test_civ2,0)
3063                     errordlg(['no civ2 data in ' filename])
3064                     set(handles.RUN, 'Enable','On')
3065                     set(handles.RUN,'BackgroundColor',[1 0 0])
3066                            set(handles.BATCH, 'Enable','On')
3067                     set(handles.BATCH,'BackgroundColor',[1 0 0])
3068 %                     close(hsuper)
3069                     return
3070                 end
3071             end
3072         end
3073     end
3074 end
3075 %%%%%%%%%%%%%  if stereo fields are calculated by PATCH %%%%%%%%%%%%%
3076 if strcmp(compare,'stereo PIV')
3077     if  box_test(3)==1 & isequal(get(handles.test_stereo1,'Value'),1) 
3078         for ifile=1:nbfield
3079             for j=1:nbslice
3080                 filename=name_generator(filebase_AB,num1_civ1(ifile),num_a_civ1(j),'.nc',...
3081                     nom_type_nc,1,num2_civ1(ifile),num_b_civ1(j),subdir_civ1);%
3082                 filecell.st(ifile,j)={filename};
3083             end
3084         end
3085     end
3086      if  box_test(6)==1 & isequal(get(handles.test_stereo2,'Value'),1) 
3087         for ifile=1:nbfield
3088             for j=1:nbslice
3089                 filename=name_generator(filebase_AB,num1_civ2(ifile),num_a_civ2(j),'.nc',...
3090                     nom_type_nc,1,num2_civ2(ifile),num_b_civ2(j),subdir_civ2);%
3091                 filecell.st(ifile,j)={filename};
3092             end
3093         end
3094      end
3095 end
3096 set(handles.subdir_civ1,'String',subdir_civ1);%update the edit box
3097 set(handles.subdir_civ2,'String',subdir_civ2);%update the edit box
3098 browse.nom_type_nc=nom_type_nc;
3099 set(handles.browse_root,'UserData',browse); %update the nomenclature type for uvmat
3100 
3101 
3102 %COPY IMAGES TO THE FORMAT .png IF NEEDED
3103 if isequal(nom_type_ima1,'*')
3104     nom_type_imanew='_i';
3105 else
3106     nom_type_imanew1=nom_type_ima1;
3107 end
3108 if isequal(nom_type_ima2,'*')
3109     nom_type_imanew='_i';
3110 else
3111     nom_type_imanew2=nom_type_ima2;
3112 end
3113 if ~isequal(ext_ima,'.png')
3114     npxy=get(handles.ext_ima,'UserData');
3115     if numel(npxy)<2
3116         filename=name_generator(filebase_ima1,num1_civ1(1),num_a_civ1(1),ext_ima,nom_type_ima1);
3117         A=imread(filename);
3118         npxy=size(A);
3119     end
3120     npy=npxy(1);
3121     npx=npxy(2);
3122     if box_test(1)==1 %if civ1 is performed
3123        h = waitbar(0,['copy images to the .png format for civ1']);% display a wait bar
3124        for ifile=1:nbfield
3125             waitbar(ifile/nbfield);
3126             for j=1:nbslice
3127                     filename=name_generator(filebase_ima1,num1_civ1(ifile),num_a_civ1(j),'.png',nom_type_imanew1);
3128                     if ~exist(filename,'file')
3129                         A=read_image(filecell.ima1.civ1{ifile,j},nom_type_ima2,npx,npy,num1_civ1(ifile));
3130                         imwrite(A,filename,'BitDepth',16); 
3131                     end
3132                     filecell.ima1.civ1(ifile,j)={filename};
3133                     filename=name_generator(filebase_ima2, num2_civ1(ifile),num_b_civ1(j),'.png',nom_type_imanew2);
3134                     if ~exist(filename,'file')
3135                         A=read_image(filecell.ima2.civ1{ifile,j},nom_type_ima2,npx,npy,num2_civ1(ifile));
3136                         imwrite(A,filename,'BitDepth',16);
3137                     end
3138                     filecell.ima2.civ1(ifile,j)={filename};
3139             end
3140         end
3141         close(h)
3142     end
3143     if box_test(4)==1 %if civ2 is performed
3144         h = waitbar(0,['copy images to the .png format for civ2']);% display a wait bar
3145         for ifile=1:nbfield
3146             waitbar(ifile/nbfield);
3147             for j=1:nbslice
3148                     filename=name_generator(filebase_ima1,num1_civ2(ifile),num_a_civ2(j),'.png',nom_type_imanew1);
3149                     if ~exist(filename,'file')
3150                         A=read_image(cell2mat(filecell.ima1.civ2(ifile,j)),nom_type_ima2,npx,npy,num1_civ2(ifile));
3151                         imwrite(A,filename,'BitDepth',16); 
3152                     end
3153                     filecell.ima1.civ2(ifile,j)={filename};
3154                     filename=name_generator(filebase_ima2, num2_civ2(ifile),num_b_civ2(j),'.png',nom_type_imanew2);
3155                     if ~exist(filename,'file')
3156                         A=read_image(cell2mat(filecell.ima2.civ2(ifile,j)),nom_type_ima2,npx,npy,num2.civ1(ifile));
3157                         imwrite(A,filename,'BitDepth',16);
3158                     end
3159                     filecell.ima2.civ2(ifile,j)={filename};
3160             end
3161         end
3162         close(h);
3163     end
3164 end
3165 
3166 %---------------------------------------------------------
3167 %CIV1  CIV1  CIV1 CIV1
3168 %----------------------------------------------------------
3169 function RUN_CIV1(handles,filecell,filecell_1,filecell_nc1,num1,num2,num_a,num_b,nom_type_nc)
3170 %pixels per cm and matrix of the image times, read from the .civ file by uvmat
3171 global civ1_exe sge%name of the executable for civ1 calculation
3172 
3173 %get civ parameters
3174 ibx_val=str2num(get(handles.ibx,'String'));
3175 if isempty(ibx_val)
3176     ibx='21'; set(handles.ibx,'String','21')
3177 else 
3178     ibx=num2str(ibx_val);
3179 end
3180 iby_val=str2num(get(handles.iby,'String'));
3181 if isempty(iby_val)
3182     iby='21'; set(handles.iby,'String','21')
3183 else 
3184     iby=num2str(iby_val);
3185 end
3186 isx=get(handles.isx,'String');
3187 if isempty(str2num(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
3188 if str2num(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
3189 isy=get(handles.isy,'String');
3190 if isempty(str2num(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
3191 if str2num(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
3192 shiftx=get(handles.shiftx,'String');
3193 if isempty(str2num(shiftx)), shiftx='0'; set(handles.shiftx,'String','0'), end;%default
3194 shifty=get(handles.shifty,'String');
3195 if isempty(str2num(shifty)), shifty='0'; set(handles.shifty,'String','0'), end;%default
3196 rho=get(handles.rho,'String');
3197 dx=get(handles.dx_civ1,'String');
3198 if isequal(str2num(dx),[]), dx='20'; set(handles.dx_civ1,'String','20'); end%default
3199 dy=get(handles.dy_civ1,'String');
3200 if isequal(str2num(dy),[]), dy='20'; set(handles.dy_civ1,'String','20');end %default
3201 if isequal(str2num(dy),[]), dy='20'; end%default
3202 pxcmx='1'; %velocity fields are expressed in pixel displacement
3203 pxcmy='1';
3204 image_first=cell2mat(filecell(1,1));
3205 if ~exist(image_first,'file')
3206     warndlg_uvmat(['image ' image_first 'not found'],'ERROR')
3207     set(handles.RUN, 'Enable','On')
3208     set(handles.RUN,'BackgroundColor',[1 0 0])
3209     return
3210 end
3211 A=imread(cell2mat(filecell(1,1)));%read the first image to get the size
3212 sizim=size(A);
3213 npx=num2str(sizim(2));
3214 npy=num2str(sizim(1));
3215 time=get(handles.displ_filebase,'UserData'); %get the set of times
3216 gridname=get(handles.grid_civ1,'String');
3217 gridflag='y';
3218 if isequal(gridname,'')| isempty(gridname)
3219         gridname='nogrid';
3220         gridflag='n';
3221 end
3222 if isequal(get(handles.ImaThreshold,'Value'),1)
3223     threshflag='y';
3224     min_ima=get(handles.MinIma,'String');
3225     max_ima=get(handles.MaxIma,'String');
3226 else
3227     threshflag='n';
3228     min_ima='0';
3229     max_ima='4096';
3230 end 
3231  %main loop
3232 filebase=get(handles.displ_filebase,'String');
3233 sizcell=size(filecell);
3234 nbfield=sizcell(1);
3235 nbslice=sizcell(2);
3236 icount=0;
3237 for ifile=1:nbfield
3238 %  barlength=0.188*ifile/nbfield;
3239 %     set(handles.waitbar_1,'Position',[0.946 0.877-barlength 0.03 barlength])
3240     for j=1:nbslice
3241         icount=icount+1;
3242         barlength=0.188*icount/(nbfield*nbslice);
3243         set(handles.waitbar_1,'Position',[0.946 0.877-barlength 0.03 barlength])
3244         drawnow
3245         filename_ima=cell2mat(filecell(ifile,j));
3246         filename_ima([end-3:end])=[];%remove .png extension
3247         filename_ima_1=cell2mat(filecell_1(ifile,j));
3248         filename_ima_1([end-3:end])=[];%remove .png extension
3249         filename_cmx=cell2mat(filecell_nc1(ifile,j));%output netcdf file
3250         filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
3251         filename_cmx=[filename_cmx 'x'];
3252         namelog=[filename_cmx([1:end-3]) 'log'];       
3253         if size(time,1)>=num2(ifile) &  size(time,2)>=num_b(j)
3254             Dt=num2str(time(num2(ifile),num_b(j))-time(num1(ifile),num_a(j)));
3255             if isequal(Dt,'0')
3256                 Dt='1' ;%case of 'displacement' mode
3257             end
3258             T0=num2str((time(num2(ifile),num_b(j))+time(num1(ifile),num_a(j)))/2);
3259         else
3260             Dt='1';
3261             T0='0';
3262         end 
3263         term_a=num2stra(num_a(j),nom_type_nc);%UTILITE?
3264         term_b=num2stra(num_b(j),nom_type_nc);%
3265         test_mask=get(handles.get_mask_civ1,'Value');
3266         if test_mask==0
3267             maskname='noFile use default';
3268             maskflag='n';
3269         else
3270             maskdispl=get(handles.mask_civ1,'String');%look for mask name in edit box
3271             maskbase=[filebase '_' maskdispl];%
3272             maskname=name_generator(maskbase,num1(ifile),1,'.png','_i');
3273             if exist(maskname,'file')
3274                 maskflag='y'; 
3275             else
3276                 maskname='noFile use default';
3277                 maskflag='n'; 
3278             end
3279         end
3280         textcmx={'##############   CMX file';...
3281         ['FirstImage ' filename_ima];...
3282         ['LastImage  ' filename_ima_1];...
3283         'XX' ;...
3284         ['Mask ' maskflag] ;...
3285         ['MaskName ' maskname];...
3286         ['ImageSize ' npx ' ' npy];...   %VERIFIER CAS GENERAL ?
3287         ['CorrelationBoxesSize ' ibx ' ' iby];...
3288         ['SearchBoxeSize ' isx ' ' isy];...
3289         ['RO ' rho];...
3290         ['GridSpacing ' dx ' ' dy];...
3291         'XX 1.0';...
3292         ['Dt_TO ' Dt ' ' T0];...
3293         ['PixCmXY ' pxcmx ' ' pxcmy];...
3294         'XX 1';...
3295         ['ShiftXY ' shiftx ' ' shifty];...
3296         ['Grid ' gridflag];...
3297         ['GridName ' gridname] ;...
3298         'XX 85';...
3299         'XX 1.0';...
3300         'XX 1.0';...
3301         'Hart 1';...
3302         'DecimalShift 0';...
3303         'Deformation 0';...
3304         'CorrelationMin 0';...
3305         'IntensityMin 0';...
3306         ['SeuilImage ' threshflag];...
3307         ['SeuilImageValues ' min_ima ' ' max_ima];...
3308         ['ImageToUse ' term_a ' ' term_b];... % VERIFIER ?
3309         'ImageUsedBefore null null'};
3310         textout=char(textcmx);
3311         dlmwrite(filename_cmx,textout,'');
3312         s=-1; 
3313          ['!' civ1_exe ' -f ' filename_cmx ]
3314              eval(['!' civ1_exe ' -f ' filename_cmx ' > ' namelog]);
3315 %         if sge%dispatch computation on the cluster using interactive queue
3316 %           %  [s,w] = unix(['qrsh -q fast.q ' civ1_exe ' -f ' filename_cmx ' > ' namelog ' 2>&1' ]);
3317 %        end
3318 %         if s~=0
3319 %            %  ['!' civ1_exe ' -f ' filename_cmx ' > ' namelog]
3320 %            % eval(['!' civ1_exe ' -f ' filename_cmx ' > ' namelog]);
3321 %         end
3322     end
3323 end
3324 %  close(h)
3325 
3326 
3327 %---------------------------------------------------------------
3328 % RUN CIV2   CIV2    CIV2   CIV2
3329 %-----------------------------------------------------------
3330 function RUN_CIV2(handles,filecell_2,filecell_3,filecell_nc1,filecell_nc2,num1,num2,num_a,num_b,nom_type_nc)
3331 %filecell_2: names of first image
3332 %filecell_3: names of second images
3333 global civ2_exe sge
3334 
3335 %names of the civ2 fields
3336 field.vel_type='civ2';
3337 field.nb='nb_vectors2';
3338 field.X='vec2_X';
3339 field.Y='vec2_Y';
3340 field.U='vec2_U';
3341 field.V='vec2_V';
3342  
3343 %get civ parameters
3344 ibx=get(handles.ibx_civ2,'String');
3345 iby=get(handles.iby_civ2,'String');
3346 rho=get(handles.rho_civ2,'String');
3347 decimal=int2str(get(handles.decimal,'Value'));
3348 deformation=int2str(get(handles.deformation,'Value'));
3349 dx=get(handles.dx_civ2,'String');
3350 dy=get(handles.dy_civ2,'String');
3351 if isequal(str2num(dx),[])
3352     dx='20';%default
3353 end
3354 if isequal(str2num(dy),[])
3355     dy='20';%default
3356 end
3357     pxcmx='1';%velocity fields are expressed in pixel displacement
3358     pxcmy='1';
3359 A=imread(cell2mat(filecell_2(1,1)));%read the first image to get the size
3360 sizim=size(A);
3361 npx=num2str(sizim(2));
3362 npy=num2str(sizim(1));
3363 time=get(handles.displ_filebase,'UserData'); %get the set of times
3364 filebase=get(handles.displ_filebase,'String');
3365 gridname=get(handles.grid_civ2,'String');
3366 gridflag='y';
3367 if isequal(gridname,'')| isempty(gridname)
3368         gridname='nogrid';
3369         gridflag='n';
3370 end
3371 sizcell=size(filecell_2);
3372 nbfield=sizcell(1);
3373 nbslice=sizcell(2);
3374   
3375 %main loop
3376 % h = waitbar(0,['CIV2 processing, ' namefig]);% display a wait bar
3377 % nbfield=length(filecell_2);
3378 icount=0;
3379 for ifile=1:nbfield
3380 %     barlength=0.188*ifile/nbfield;
3381 %     set(handles.waitbar_civ2,'Position',[0.946 0.407-barlength 0.03 barlength])
3382     for j=1:nbslice
3383         icount=icount+1;
3384         barlength=0.188*icount/(nbfield*nbslice);
3385         set(handles.waitbar_civ2,'Position',[0.946 0.407-barlength 0.03 barlength])
3386         drawnow
3387         filename_ima_2=cell2mat(filecell_2(ifile,j));
3388         filename_ima_2([end-3:end])=[];%remove .png extension
3389         filename_ima_3=cell2mat(filecell_3(ifile,j));
3390         filename_ima_3([end-3:end])=[];%remove .png extension
3391         filename_cmx=cell2mat(filecell_nc2(ifile,j));%output netcdf file
3392         filename_cmx([end-1:end])=[ 'cm'];%name of cmx file
3393         filename_cmx=[filename_cmx 'x'];
3394         namelog=[filename_cmx([1:end-3]) 'log']; 
3395         if size(time,1)>=num2(ifile) &  size(time,2)>=num_b(j)
3396             Dt=num2str(time(num2(ifile),num_b(j))-time(num1(ifile),num_a(j)));
3397             if isequal(Dt,'0')
3398                 Dt='1' %case of 'displacement' mode
3399             end
3400             T0=num2str((time(num2(ifile),num_b(j))+time(num1(ifile),num_a(j)))/2);
3401         else
3402             Dt='1';
3403             T0='0';
3404         end 
3405         term_a=num2stra(num_a(j),nom_type_nc);
3406         term_b=num2stra(num_b(j),nom_type_nc);
3407         filename_nc1=cell2mat(filecell_nc1(ifile,j));
3408         filename_nc1([end-2:end])=[]; % remove '.nc'
3409         test_mask=get(handles.get_mask_civ2,'Value');
3410         if test_mask==0  
3411             maskname='noFile use default';
3412             maskflag='n';
3413         else
3414             maskdispl=get(handles.mask_civ2,'String');
3415             maskbase=[filebase '_' maskdispl]; %
3416             maskname =name_generator(maskbase,num1(ifile),1,'.png','_i');
3417             if ~exist(maskname,'file')
3418                 maskflag='y'; 
3419             else
3420                 maskname='noFile use default';
3421                 maskflag='n'; 
3422             end
3423         end
3424     
3425         textcmx={'##############   CMX file';...
3426         ['FirstImage ' filename_ima_2];...
3427         ['LastImage  ' filename_ima_3];...
3428         'XX' ;...
3429         ['Mask ' maskflag];...
3430         ['MaskName ' maskname];...
3431         ['ImageSize ' npx ' ' npy];...   
3432         ['CorrelationBoxesSize ' ibx ' ' iby];...
3433         ['SearchBoxeSize ' ibx ' ' iby];...
3434         ['RO ' rho];...
3435         ['GridSpacing ' dx ' ' dy];...
3436         'XX 1.0';...
3437         ['Dt_TO ' Dt ' ' T0];...
3438         ['PixCmXY ' pxcmx ' ' pxcmy];...
3439         'XX 1';...
3440         ['ShiftXY 0 0'];...
3441         ['Grid ' gridflag];...
3442         ['GridName ' gridname];...
3443         'XX 85';...
3444         'XX 1.0';...
3445         'XX 1.0';...
3446         'Hart 1';...
3447         ['DecimalShift ' decimal];...
3448         ['Deformation ' deformation];...
3449         'CorrelationMin 0';...
3450         'IntensityMin 0';...
3451         'SeuilImage n';...
3452         'SeuilImageValues 0 4096';...
3453         ['ImageToUse ' term_a ' ' term_b];... % VERIFIER ?
3454         ['ImageUsedBefore ' filename_nc1]};
3455         textout=char(textcmx);
3456         dlmwrite(filename_cmx,textout,'');     
3457         s=-1;  
3458            ['!' civ2_exe ' -f ' filename_cmx ' > ' namelog]
3459            eval(['!' civ2_exe ' -f ' filename_cmx ' > ' namelog]);    
3460 %         if sge%dispatch computation on the cluster using interactive queue
3461 %             [s,w] = unix(['qrsh -q fast.q ' civ2_exe ' -f ' filename_cmx ' > ' namelog ' 2>&1']);
3462 %         end
3463 %         if s~=0
3464 %             eval(['!' civ2_exe ' -f ' filename_cmx ' > ' namelog]);
3465 %             ['!' civ2_exe ' -f ' filename_cmx ' > ' namelog]
3466 %         end
3467     end
3468 end
3469 % close(h)
3470 
3471           
3472 %----------------------------------------
3473 %PATCH
3474 %---------------------------------------
3475 function cmd_PATCH=RUN_PATCH(filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,test_interp)
3476 global patch_exe patch_new_exe
3477         namelog=[filename_nc([1:end-3]) '_patch.log'];
3478         if test_interp==0
3479             cmd_PATCH=[patch_exe ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ' -nopt ' subdomain_patch ...
3480             '  > ' namelog ' 2>&1'] % redirect standard output to the log file
3481          else %nouveau programme patch
3482              cmd_PATCH=[patch_new_exe ' -f ' filename_nc ' -m ' nx_patch  ' -n ' ny_patch ' -ro ' rho_patch ...
3483                 ' -max ' thresh_value ' -nopt ' subdomain_patch  '  > ' namelog ' 2>&1']; % redirect standard output to the log file
3484         end
3485               
3486 %----------------------------------------
3487 %STEREO Interp
3488 %---------------------------------------
3489 function cmd=RUN_STINTERP(stinterp_exe,filename_A_nc,filename_B_nc,filename_nc,nx_patch,ny_patch,rho_patch,subdomain_patch,thresh_value,xmlA,xmlB)
3490 namelog=[filename_nc([1:end-3]) '_stinterp.log'];
3491 cmd=[stinterp_exe ' -f1 ' filename_A_nc  ' -f2 ' filename_B_nc ' -f  ' filename_nc ...
3492     ' -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
3493   
3494 %----------------------------------------------------
3495 function first_j_Callback(hObject, eventdata, handles)
3496 last_j_Callback(hObject, eventdata, handles)
3497 
3498 %---------------------------------------------------------
3499 % --- Executes on button press in CIV1.
3500 function CIV1_Callback(hObject, eventdata, handles)
3501 val=get(handles.CIV1,'Value');
3502 if isequal(val,1)
3503     enable_civ1(handles,'on')
3504     enable_pair1(handles,'on')
3505 else
3506     enable_civ1(handles,'off')
3507 end
3508 find_netcpair_civ1(hObject, eventdata, handles);
3509 
3510 %------------------------------------------------------
3511 % --- Executes on button press in FIX1.
3512 function FIX1_Callback(hObject, eventdata, handles)
3513 
3514 enable_fix1(handles,get(handles.FIX1,'Value'))
3515 
3516 
3517 %----------------------------------------------------------------
3518 % --- Executes on button press in PATCH1.
3519 function PATCH1_Callback(hObject, eventdata, handles)
3520 
3521 if get(handles.PATCH1,'Value')==1
3522 enable_patch1(handles)
3523 else
3524 desable_patch1(handles)
3525 end
3526 
3527 %----------------------------------------------------------
3528 % --- Executes on button press in CIV2.
3529 function CIV2_Callback(hObject, eventdata, handles)
3530 state=get(handles.CIV2,'Value');
3531 enable_civ2(handles,state)
3532 if state
3533     find_netcpair_civ2(hObject, eventdata, handles)
3534     enable_pair1(handles,'on')
3535 end
3536 
3537 %---------------------------------------------------
3538 % --- Executes on button press in FIX2.
3539 function FIX2_Callback(hObject, eventdata, handles)
3540 if get(handles.FIX2,'Value')==1
3541     enable_fix2(handles)
3542     if get(handles.CIV2,'Value')==0
3543         find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3544     end
3545 else
3546     desable_fix2(handles)
3547 end
3548 
3549 
3550 %-------------------------------------------------------
3551 % --- Executes on button press in PATCH2.
3552 function PATCH2_Callback(hObject, eventdata, handles)
3553 %--------------------------------------------------------
3554 if get(handles.PATCH2,'Value')==1
3555     enable_patch2(handles)
3556     if get(handles.CIV2,'Value')==0
3557         find_netcpair_civ2(hObject, eventdata, handles) % select the available netcdf files
3558     end
3559 else
3560     desable_patch2(handles)
3561 end
3562 
3563 
3564 
3565 %-----------------------------------------------------------
3566 function first_i_Callback(hObject, eventdata, handles)
3567 %------------------------------------------------------
3568 last_i_Callback(hObject, eventdata, handles)
3569 
3570 %-----------------------------------------------------------
3571 % --- Executes on button press in calcul_search: determine the search range isx,isy
3572 %--------------------------------------------------------
3573 function calcul_search_Callback(hObject, eventdata, handles)
3574 
3575 %determine pair numbers
3576 list_pair=get(handles.list_pair_civ1,'String');%get the menu of image pairs
3577 index=get(handles.list_pair_civ1,'Value');
3578 displ_num=get(handles.list_pair_civ1,'UserData');
3579 time=get(handles.displ_filebase,'UserData'); %get the set of times
3580 pxcm_xy=get(handles.calcul_search,'UserData');
3581 pxcmx=pxcm_xy(1);
3582 pxcmy=pxcm_xy(2);
3583 mode_list=get(handles.mode,'String');
3584 mode_value=get(handles.mode,'Value');
3585 mode=mode_list{mode_value};
3586 if isequal (mode, 'series(Di)' )
3587     ref_i=str2num(get(handles.ref_i,'String'));
3588     num1=ref_i-floor(index/2);%  first image numbers
3589     num2=ref_i+ceil(index/2);
3590     num_a=1;
3591     num_b=1;
3592 elseif isequal (mode, 'series(Dj)') 
3593     num1=1;
3594     num2=1;
3595     ref_j=str2num(get(handles.ref_j,'String'));
3596     num_a=ref_j-floor(index/2);%  first image numbers
3597     num_b=ref_j+ceil(index/2);
3598 elseif isequal(mode,'pair j1-j2') %case of bursts (png_old or png_2D)
3599     ref_i=str2num(get(handles.ref_i,'String'));
3600     num1=ref_i;
3601     num2=ref_i;
3602     num_a=displ_num(1,index);
3603     num_b=displ_num(2,index);
3604 end
3605 dt=time(num2,num_b)-time(num1,num_a);
3606 ibx=str2num(get(handles.ibx,'String'));
3607 iby=str2num(get(handles.iby,'String'));
3608 umin=dt*pxcmx*str2num(get(handles.umin,'String'));
3609 umax=dt*pxcmx*str2num(get(handles.umax,'String'));
3610 vmin=dt*pxcmy*str2num(get(handles.vmin,'String'));
3611 vmax=dt*pxcmy*str2num(get(handles.vmax,'String'));
3612 shiftx=round((umin+umax)/2);
3613 shifty=round((vmin+vmax)/2);
3614 isx=(umax+2-shiftx)*2+ibx;
3615 isx=2*ceil(isx/2)+1;
3616 isy=(vmax+2-shifty)*2+iby;
3617 isy=2*ceil(isy/2)+1;
3618 set(handles.shiftx,'String',num2str(shiftx));
3619 set(handles.shifty,'String',num2str(shifty));
3620 set(handles.isx,'String',num2str(isx));
3621 set(handles.isy,'String',num2str(isy));
3622 
3623 
3624 %---------------------------------------------------------
3625 % Executes on carriage return on the subdir civ1 edit window
3626 %--------------------------------------------------------
3627 function subdir_civ1_Callback(hObject, eventdata, handles)
3628 subdir=get(handles.subdir_civ1,'String');
3629 set(handles.subdir_civ2,'String',subdir);
3630 if get(handles.CIV1,'Value')==0 
3631     find_netcpair_civ1(hObject, eventdata, handles); %update the list of available pairs from netcdf files in the new directory
3632 end
3633 
3634 %---------------------------------------------------------
3635 % Executes on carriage return on the subdir civ1 edit window
3636 %---------------------------------------------------------
3637 function subdir_civ2_Callback(hObject, eventdata, handles)
3638 %update the list of available pairs from netcdf files in the new directory
3639 if get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0
3640     find_netcpair_civ2(hObject, eventdata, handles);
3641 end
3642 
3643 %------------------------------------------------------
3644 % --- Executes on button press in get_mask_civ1.
3645 %------------------------------------------------------
3646 function get_mask_civ1_Callback(hObject, eventdata, handles)
3647 maskval=get(handles.get_mask_civ1,'Value');
3648 if isequal(maskval,0)
3649     set(handles.mask_civ1,'String','')
3650 else
3651 mask_displ='no mask'; %default
3652 filebase=get(handles.displ_filebase,'String');
3653 [ nbslice, flag_mask]=get_mask(filebase,handles);
3654 if isequal(flag_mask,1)
3655       mask_displ=[num2str(nbslice) 'mask'];
3656 elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3657         common_path=fileparts(filebase);
3658         filebase_a=fullfile(common_path,get(handles.displ_filebase2,'String'));
3659         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3660         if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3661             mask_displ='no mask';
3662         end
3663 end
3664 if isequal(mask_displ,'no mask')
3665     set(handles.get_mask_civ1,'Value',0)
3666     set(handles.get_mask_fix1,'Value',0)
3667     set(handles.get_mask_civ2,'Value',0)
3668     set(handles.get_mask_fix2,'Value',0)
3669 else
3670     set(handles.get_mask_fix1,'Value',1)
3671     set(handles.get_mask_civ2,'Value',1)
3672     set(handles.get_mask_fix2,'Value',1)
3673 end
3674 set(handles.mask_civ1,'String',mask_displ)
3675 set(handles.mask_fix1,'String',mask_displ)
3676 set(handles.mask_civ2,'String',mask_displ)
3677 set(handles.mask_fix2,'String',mask_displ)
3678 end
3679 %--------------------------------------------------------------
3680 % --- Executes on button press in get_mask_fix1.
3681 function get_mask_fix1_Callback(hObject, eventdata, handles)
3682 maskval=get(handles.get_mask_fix1,'Value');
3683 if isequal(maskval,0)
3684     set(handles.mask_fix1,'String','')
3685 else
3686 mask_displ='no mask'; %default
3687 filebase=get(handles.displ_filebase,'String');
3688 [nbslice, flag_mask]=get_mask(filebase,handles);
3689 if isequal(flag_mask,1)
3690       mask_displ=[num2str(nbslice) 'mask'];
3691 elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3692         filebase_a=get(handles.displ_filebase2,'String');
3693         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3694         if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3695             mask_displ='no mask';
3696         end
3697 end
3698 if isequal(mask_displ,'no mask')
3699     set(handles.get_mask_fix1,'Value',0)
3700     set(handles.get_mask_civ2,'Value',0)
3701     set(handles.get_mask_fix2,'Value',0)
3702 else
3703     set(handles.get_mask_civ2,'Value',1)
3704     set(handles.get_mask_fix2,'Value',1)
3705 end 
3706 set(handles.mask_fix1,'String',mask_displ)
3707 set(handles.mask_civ2,'String',mask_displ)
3708 set(handles.mask_fix2,'String',mask_displ)
3709 end
3710 %-----------------------------------------
3711 % --- Executes on button press in get_mask_civ2.
3712 function get_mask_civ2_Callback(hObject, eventdata, handles)
3713 maskval=get(handles.get_mask_civ2,'Value');
3714 if isequal(maskval,0)
3715     set(handles.mask_civ2,'String','')
3716 else
3717 mask_displ='no mask'; %default
3718 filebase=get(handles.displ_filebase,'String');
3719 [nbslice, flag_mask]=get_mask(filebase,handles);
3720 if isequal(flag_mask,1)
3721       mask_displ=[num2str(nbslice) 'mask'];
3722 elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3723         filebase_a=get(handles.displ_filebase2,'String');
3724         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3725         if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3726             mask_displ='no mask';
3727         end
3728 end
3729 if isequal(mask_displ,'no mask')
3730     set(handles.get_mask_civ2,'Value',0)
3731     set(handles.get_mask_fix2,'Value',0)
3732 else
3733     set(handles.get_mask_fix2,'Value',1)
3734 end 
3735 set(handles.mask_civ2,'String',mask_displ)
3736 set(handles.mask_fix2,'String',mask_displ)
3737 end
3738 %-------------------------------------
3739 % --- Executes on button press in get_mask_fix2.
3740 function get_mask_fix2_Callback(hObject, eventdata, handles)
3741 maskval=get(handles.get_mask_fix2,'Value');
3742 if isequal(maskval,0)
3743     set(handles.mask_fix2,'String','')
3744 else
3745 mask_displ='no mask'; %default
3746 filebase=get(handles.displ_filebase,'String');
3747 [nbslice, flag_mask]=get_mask(filebase,handles);
3748 if isequal(flag_mask,1)
3749       mask_displ=[num2str(nbslice) 'mask'];
3750 elseif get(handles.compare,'Value')>1 & ~isequal(mask_displ,'no mask')% look for the second mask series
3751         filebase_a=get(handles.displ_filebase2,'String');
3752         [nbslice_a, flag_mask_a]=get_mask(filebase_a,handles);
3753         if isequal(flag_mask_a,0) | ~isequal(nbslice_a,nbslice)
3754             mask_displ='no mask';
3755         end
3756 end
3757 if isequal(mask_displ,'no mask')
3758     set(handles.get_mask_fix2,'Value',0)
3759 end 
3760 set(handles.mask_fix2,'String',mask_displ)
3761 end
3762 
3763 %---------------------------------------
3764 function [nbslice, flag_mask]=get_mask(filebase,handles)
3765 %detect mask files, images with appropriate file base
3766 %[filebase '_' xx 'mask'], xx=nbslice
3767 %flag_mask=1 indicates detection
3768 
3769 flag_mask=0;%default
3770 nbslice=1;
3771 
3772 % subdir=get(handles.subdir_civ1,'String');
3773 [Path,Name]=fileparts(filebase);
3774 currentdir=pwd;
3775 cd(Path);%move in the dir of the root name filebase
3776 maskfiles=dir([Name '_*mask_*.png']);%look for mask files
3777 cd(currentdir);%come back to the current working directory
3778 if isempty(maskfiles)
3779     msgbox_uvmat('ERROR','no mask available, to create it use Tools/Make mask in the upper menu bar of uvmat')
3780 else
3781     flag_mask=1;
3782     maskname=maskfiles(1).name;% take the first mask file in the list
3783     [Path2,Name,ext]=fileparts(maskname);
3784     Namedouble=double(Name);
3785     val=(48>Namedouble)|(Namedouble>57);% select the non-numerical characters
3786     ind_mask=findstr('mask',Name);
3787     i=ind_mask-1;
3788     while val(i)==0 & i>0
3789        i=i-1;
3790     end
3791     nbslice=str2num(Name(i+1:ind_mask-1));
3792     if ~isequal(nbslice,[]) & Name(i)=='_'
3793           flag_mask=1;
3794     else
3795           msgbox_uvmat('ERROR',['bad mask file ' Name ext ' found in ' Path2])
3796           return
3797           nbslice=1;
3798     end
3799 end    
3800 %------------------------------
3801 
3802 
3803 function grid_civ1_Callback(hObject, eventdata, handles)
3804 % hObject    handle to grid_civ1 (see GCBO)
3805 % eventdata  reserved - to be defined in a future version of MATLAB
3806 % handles    structure with handles and user data (see GUIDATA)
3807 
3808 % Hints: get(hObject,'String') returns contents of grid_civ1 as text
3809 %        str2double(get(hObject,'String')) returns contents of grid_civ1 as a double
3810 
3811 
3812 %-----------------------------------------------------------
3813 % transform numbers to letters
3814 %--------------------------------------------
3815 function str=num2stra(num,nom_type);
3816 if isequal(nom_type,'png_old') | isequal(nom_type,'netc_old') |isequal(nom_type,'raw_SMD')
3817     str=char(96+num);
3818 elseif isequal(nom_type,'_i')|isequal(nom_type,'_i1-i2')...
3819         |isequal(nom_type,'ima_num')| isequal(nom_type,'avi')| isequal(nom_type,'none')
3820     str='';
3821 else
3822     str=num2str(num);
3823 end
3824 %---------------------------------------------------
3825 function mask_civ1_Callback(hObject, eventdata, handles)
3826 set(handles.mask_civ1,'UserData',[])
3827 set(handles.mask_civ1,'String','')
3828 %----------------------------------------------------
3829 function mask_civ2_Callback(hObject, eventdata, handles)
3830 set(handles.mask_civ2,'UserData',[])
3831 set(handles.mask_civ2,'String','')
3832 %----------------------------------------------------
3833 function mask_fix1_Callback(hObject, eventdata, handles)
3834 set(handles.mask_fix1,'UserData',[])
3835 set(handles.mask_fix1,'String','')
3836 %----------------------------------------------------
3837 function mask_fix2_Callback(hObject, eventdata, handles)
3838 set(handles.mask_fix2,'UserData',[])
3839 set(handles.mask_fix2,'String','')
3840 
3841 %--------------------------------------------------------------------------
3842 % --- Executes on button press in list_subdir_civ1.
3843 function list_subdir_civ1_Callback(hObject, eventdata, handles)
3844 list_subdir_civ1=get(handles.list_subdir_civ1,'String');
3845 val=get(handles.list_subdir_civ1,'Value');
3846 if val>1
3847     subdir=list_subdir_civ1{val};
3848     set(handles.subdir_civ1,'String',subdir);
3849     set(handles.list_subdir_civ1,'Value',1);
3850 end
3851 
3852 
3853 %--------------------------------------------------------------------------
3854 % --- Executes on button press in list_subdir_civ2.
3855 function list_subdir_civ2_Callback(hObject, eventdata, handles)
3856 list_subdir_civ2=get(handles.list_subdir_civ2,'String');
3857 val=get(handles.list_subdir_civ2,'Value');
3858 if val>1
3859     subdir=list_subdir_civ2{val};
3860     set(handles.subdir_civ2,'String',subdir);
3861     set(handles.list_subdir_civ2,'Value',1);
3862 end
3863 
3864 
3865 %----------------------------------------------
3866 function last_i_Callback(hObject, eventdata, handles)
3867 first_i=str2num(get(handles.first_i,'String'));
3868 last_i=str2num(get(handles.last_i,'String'));
3869 ref_i=ceil((first_i+last_i)/2);
3870 set(handles.ref_i,'String', num2str(ref_i))
3871 ref_i_Callback(hObject, eventdata, handles)
3872 
3873 %-------------------------------------------------------
3874 function last_j_Callback(hObject, eventdata, handles)
3875 first_j=str2num(get(handles.first_j,'String'));
3876 last_j=str2num(get(handles.last_j,'String'));
3877 ref_j=ceil((first_j+last_j)/2);
3878 set(handles.ref_j,'String', num2str(ref_j))
3879 ref_j_Callback(hObject, eventdata, handles)
3880 
3881 %--------------------------------------------------------------------------
3882 % --- Executes on button press in browse_gridciv1.
3883 function browse_gridciv1_Callback(hObject, eventdata, handles)
3884 value=get(handles.browse_gridciv1,'Value');
3885 if value
3886     filebase=get(handles.displ_filebase,'String');
3887     [FileName, PathName, filterindex] = uigetfile( ...
3888            {'*.grid', ' (*.grid)';
3889             '*.grid',  '.grid files '; ...
3890             '*.*', 'All Files (*.*)'}, ...
3891             'Pick a file',filebase);
3892     filegrid=fullfile(PathName,FileName);
3893     if isempty(FileName)|isempty(PathName)|isequal(FileName,0)|~exist(filegrid,'file')
3894         set(handles.browse_gridciv1,'Value',0);
3895         set(handles.grid_civ1,'string','');
3896         set(handles.dx_civ1,'Visible','on');
3897         set(handles.dy_civ1,'Visible','on');
3898         set(handles.grid_civ2,'string','');
3899         if get(handles.CIV2,'Value')
3900             set(handles.dx_civ2,'Visible','on');
3901             set(handles.dy_civ2,'Visible','on');
3902         end
3903     else
3904         set(handles.grid_civ1,'string',filegrid);
3905         set(handles.dx_civ1,'Visible','off');
3906         set(handles.dy_civ1,'Visible','off');
3907         set(handles.grid_civ2,'string',filegrid);
3908         set(handles.dx_civ2,'Visible','off');
3909         set(handles.dy_civ2,'Visible','off');
3910 % set(handles.grid_patch1,'string',filegrid);
3911 % set(handles.grid_patch2,'string',filegrid);
3912     end
3913 else
3914     set(handles.grid_civ1,'string','');
3915     set(handles.dx_civ1,'Visible','on');
3916     set(handles.dy_civ1,'Visible','on');
3917     set(handles.grid_civ2,'string','');
3918     if get(handles.CIV2,'Value')
3919         set(handles.dx_civ2,'Visible','on');
3920         set(handles.dy_civ2,'Visible','on');
3921     end
3922 end
3923 
3924 
3925 % --- Executes on button press in browse_gridciv2.
3926 function browse_gridciv2_Callback(hObject, eventdata, handles)
3927 
3928 filebase=get(handles.displ_filebase,'String');
3929 [FileName, PathName, filterindex] = uigetfile( ...
3930        {'*.grid', ' (*.grid)';
3931         '*.grid',  '.grid files '; ...
3932         '*.*', 'All Files (*.*)'}, ...
3933         'Pick a file',filebase);
3934 filegrid=fullfile(PathName,FileName);
3935 set(handles.grid_civ2,'string',filegrid);
3936 set(handles.dx_civ2,'String',' ');
3937 set(handles.dy_civ2,'String',' ');
3938 % set(handles.grid_patch2,'string',filegrid);
3939 
3940 % --- Executes on button press in get_gridpatch1.
3941 function get_gridpatch1_Callback(hObject, eventdata, handles)
3942 % hObject    handle to get_gridpatch1 (see GCBO)
3943 % eventdata  reserved - to be defined in a future version of MATLAB
3944 % handles    structure with handles and user data (see GUIDATA)
3945 
3946 filebase=get(handles.displ_filebase,'String');
3947 [FileName, PathName, filterindex] = uigetfile( ...
3948        {'*.grid', ' (*.grid)';
3949         '*.grid',  '.grid files '; ...
3950         '*.*', 'All Files (*.*)'}, ...
3951         'Pick a file',filebase);
3952 filegrid=fullfile(PathName,FileName);
3953 set(handles.grid_patch1,'string',filegrid);
3954 % set(handles.grid_patch2,'string',filegrid
3955 
3956 %-----------------------------------------------------------------
3957 % --- Executes on button press in get_gridpatch2.
3958 function get_gridpatch2_Callback(hObject, eventdata, handles)
3959 
3960 
3961 %----------------------------------------------------------
3962 function enable_civ1(handles,state)
3963 if isequal(state,0)
3964     state='off';
3965 end
3966 if isequal(state,1)
3967     state='on';
3968 end
3969 if isequal(state,'on')
3970     set(handles.frame_civ1,'BackgroundColor',[1 1 0])
3971     set(handles.frame_para_civ1,'BackgroundColor',[1 1 0])
3972     set(handles.frame_grid_civ1,'BackgroundColor',[1 1 0])
3973 else
3974     set(handles.frame_civ1,'BackgroundColor',[0.831 0.816 0.784])
3975     set(handles.frame_para_civ1,'BackgroundColor',[0.831 0.816 0.784])
3976     set(handles.frame_grid_civ1,'BackgroundColor',[0.831 0.816 0.784])
3977 end
3978 set(handles.ibx,'Visible',state)
3979 set(handles.iby,'Visible',state)
3980 set(handles.isx,'Visible',state)
3981 set(handles.isy,'Visible',state)
3982 set(handles.shiftx,'Visible',state)
3983 set(handles.shifty,'Visible',state)
3984 set(handles.rho,'Visible',state)
3985 set(handles.dx_civ1,'Visible',state)
3986 set(handles.dy_civ1,'Visible',state)
3987 set(handles.calcul_search,'Visible',state)
3988 set(handles.u_text,'Visible',state)
3989 set(handles.v_text,'Visible',state)
3990 set(handles.min,'Visible',state)
3991 set(handles.max,'Visible',state)
3992 set(handles.umin,'Visible',state)
3993 set(handles.umax,'Visible',state)
3994 set(handles.vmin,'Visible',state)
3995 set(handles.vmax,'Visible',state)
3996 set(handles.grid_civ1,'Visible',state)
3997 set(handles.mask_civ1,'Visible',state)
3998 set(handles.browse_gridciv1,'Visible',state)
3999 set(handles.get_mask_civ1,'Visible',state)
4000 set(handles.parameters,'Visible',state)
4001 set(handles.grid,'Visible',state)
4002 set(handles.dx_civ1,'Visible',state)
4003 set(handles.dy_civ1,'Visible',state)
4004 set(handles.ImaThreshold,'Visible',state)
4005 if isequal(state,'off')
4006     set(handles.MinIma,'Visible','off')
4007     set(handles.MaxIma,'Visible','off')
4008     set(handles.ImaThreshold,'Value',0)
4009 end
4010 set(handles.dx_civ1_title,'Visible',state)
4011 set(handles.dy_civ1_title,'Visible',state)
4012 set(handles.ImaThreshold_title,'Visible',state)
4013 set(handles.ib_title,'Visible',state)
4014 set(handles.is_title,'Visible',state)
4015 set(handles.shift_title,'Visible',state)
4016 set(handles.rho_title,'Visible',state)
4017 
4018 %----------------------------------------------------------
4019 function enable_fix1(handles,state)
4020 if isequal(state,0)
4021     state='off';
4022 end
4023 if isequal(state,1)
4024     state='on';
4025 end
4026 if isequal(state,'on')
4027     set(handles.frame_fix1,'BackgroundColor',[1 1 0])
4028 else
4029     set(handles.frame_fix1,'BackgroundColor',[0.7 0.7 0.7])
4030 end
4031 set(handles.REMOVE,'Visible',state)
4032 set(handles.vec_Fmin2,'Visible',state)
4033 set(handles.vec_F2,'Visible',state)
4034 set(handles.vec_F3,'Visible',state)
4035 set(handles.thresh_vecC,'Visible',state)
4036 set(handles.thresh_vecC_title,'Visible',state)
4037 set(handles.thresh_vel,'Visible',state)
4038 set(handles.thresh_vel_text,'Visible',state)
4039 set(handles.mask_fix1,'Visible',state)
4040 set(handles.get_mask_fix1,'Visible',state)
4041 set(handles.get_ref_fix1,'Visible',state)
4042 set(handles.ref_fix1,'Visible',state)
4043 set(handles.inf_sup1,'Visible',state)
4044 set(handles.field_ref1,'Visible',state)
4045 
4046 
4047 %--------------------------------------------------------------
4048 function enable_patch1(handles)
4049 global patch_new_exe
4050 set(handles.frame_patch1,'BackgroundColor',[1 1 0])
4051 set(handles.rho_patch1,'Visible','on')
4052 set(handles.rho_text1,'Visible','on')
4053 set(handles.thresh_patch1,'Visible','on')
4054 set(handles.thresh_text1,'Visible','on')
4055 set(handles.subdomain_patch1,'Visible','on')
4056 set(handles.subdomain_text1,'Visible','on')
4057 set(handles.nx_patch1,'Visible','on')
4058 set(handles.ny_patch1,'Visible','on')
4059 set(handles.nx_patch1_title,'Visible','on')
4060 set(handles.ny_patch1_title,'Visible','on')
4061 if (~isequal(patch_new_exe,[]) & ~isequal(patch_new_exe,[]))
4062     set(handles.test_interp,'Visible','on');
4063 end
4064 set(handles.get_gridpatch1,'Visible','on')
4065 set(handles.grid_patch1,'string','none');
4066 set(handles.grid_patch1,'Visible','on')
4067 
4068 %--------------------------------------------------------------
4069 function desable_patch1(handles)
4070 set(handles.frame_patch1,'BackgroundColor',[0.831 0.816 0.784])
4071 set(handles.rho_patch1,'Visible','off')
4072 set(handles.rho_text1,'Visible','off')
4073 set(handles.thresh_patch1,'Visible','off')
4074 set(handles.thresh_text1,'Visible','off')
4075 set(handles.subdomain_patch1,'Visible','off')
4076 set(handles.subdomain_text1,'Visible','off')
4077 set(handles.nx_patch1,'Visible','off')
4078 set(handles.ny_patch1,'Visible','off')
4079 set(handles.nx_patch1_title,'Visible','off')
4080 set(handles.ny_patch1_title,'Visible','off')
4081 set(handles.test_interp,'Visible','off')
4082 set(handles.get_gridpatch1,'Visible','off')
4083 set(handles.grid_patch1,'Visible','off')
4084 
4085 %----------------------------------------------------------
4086 function enable_civ2(handles,state)
4087 if isequal(state,0)
4088     state='off';
4089 end
4090 if isequal(state,1)
4091     state='on';
4092 end
4093 if isequal(state,'on')
4094     set(handles.frame_civ2,'BackgroundColor',[1 1 0])
4095     set(handles.frame_para_civ2,'BackgroundColor',[1 1 0])
4096     set(handles.frame_grid_civ2,'BackgroundColor',[1 1 0])
4097     set(handles.frame_subdirciv2,'BackgroundColor',[1 1 0])
4098 else
4099     set(handles.frame_civ2,'BackgroundColor',[0.831 0.816 0.784])
4100     set(handles.frame_para_civ2,'BackgroundColor',[0.831 0.816 0.784])
4101     set(handles.frame_grid_civ2,'BackgroundColor',[0.831 0.816 0.784])
4102     set(handles.frame_subdirciv2,'BackgroundColor',[0.831 0.816 0.784])
4103 end
4104 set(handles.ibx_civ2,'Visible',state)
4105 set(handles.iby_civ2,'Visible',state)
4106 set(handles.decimal,'Visible',state)
4107 set(handles.deformation,'Visible',state)
4108 set(handles.rho_civ2,'Visible',state)
4109 set(handles.dx_civ2,'Visible',state)
4110 set(handles.dy_civ2,'Visible',state)
4111 set(handles.browse_gridciv2,'Visible',state)
4112 set(handles.get_mask_civ2,'Visible',state)
4113 set(handles.parameters,'Visible',state)
4114 set(handles.grid,'Visible',state)
4115 set(handles.parameters_text,'Visible',state)
4116 set(handles.grid_text,'Visible',state)
4117 set(handles.grid_civ2,'Visible',state)
4118 set(handles.mask_civ2,'Visible',state)
4119 set(handles.dx_civ2_title,'Visible',state)
4120 set(handles.dy_civ2_title,'Visible',state)
4121 set(handles.ibx_civ2_text,'Visible',state)
4122 set(handles.rho_civ2_title,'Visible',state)
4123 set(handles.ImaThreshold2,'Visible',state)
4124 set(handles.ImaThreshold_title2,'Visible',state)
4125 if isequal(state,'off')
4126     set(handles.MinIma2,'Visible','off')
4127     set(handles.MaxIma2,'Visible','off')
4128     set(handles.ImaThreshold2,'Value',0)
4129     if isequal(get(handles.FIX2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0) 
4130         set(handles.list_pair_civ2,'Visible','off')
4131         set(handles.subdir_civ2,'Visible','off')
4132         set(handles.subdir_civ2_text,'Visible','off')
4133     end
4134 else
4135     set(handles.list_pair_civ2,'Visible','on')
4136     set(handles.subdir_civ2,'Visible','on')
4137     set(handles.subdir_civ2_text,'Visible','on')
4138 end
4139 
4140 
4141 %----------------------------------------------------------
4142 function enable_fix2(handles)
4143 set(handles.frame_fix2,'BackgroundColor',[1 1 0])
4144 set(handles.REMOVE2,'Visible','on')
4145 set(handles.vec_Fmin2_2,'Visible','on')
4146 set(handles.vec_F4,'Visible','on')
4147 set(handles.vec_F3_2,'Visible','on')
4148 set(handles.thresh_vec2C,'Visible','on')
4149 set(handles.thresh_vec2C_text,'Visible','on')
4150 set(handles.thresh_vel2,'Visible','on')
4151 set(handles.thresh_vel2_text,'Visible','on')
4152 set(handles.mask_fix2,'Visible','on')
4153 set(handles.get_mask_fix2,'Visible','on')
4154 set(handles.list_pair_civ2,'Visible','on')
4155 set(handles.subdir_civ2,'Visible','on')
4156 set(handles.subdir_civ2_text,'Visible','on')
4157 set(handles.get_ref_fix2,'Visible','on')
4158 set(handles.ref_fix2,'Visible','on')
4159 set(handles.inf_sup2,'Visible','on')
4160 set(handles.field_ref2,'Visible','on')
4161 
4162 %----------------------------------------------------------
4163 function desable_fix2(handles)
4164 set(handles.frame_fix2,'BackgroundColor',[0.831 0.816 0.784])
4165 set(handles.REMOVE2,'Visible','off')
4166 set(handles.vec_Fmin2_2,'Visible','off')
4167 set(handles.vec_F4,'Visible','off')
4168 set(handles.vec_F3_2,'Visible','off')
4169 set(handles.thresh_vec2C,'Visible','off')
4170 set(handles.thresh_vec2C_text,'Visible','off')
4171 set(handles.thresh_vel2,'Visible','off')
4172 set(handles.thresh_vel2_text,'Visible','off')
4173 set(handles.mask_fix2,'Visible','off')
4174 set(handles.get_mask_fix2,'Visible','off')
4175 set(handles.get_ref_fix2,'Visible','off')
4176 set(handles.ref_fix2,'Visible','off')
4177 set(handles.inf_sup2,'Visible','off')
4178 set(handles.field_ref2,'Visible','off')
4179 if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.PATCH2,'Value'),0) 
4180     set(handles.list_pair_civ2,'Visible','off')
4181     set(handles.subdir_civ2,'Visible','off')
4182     set(handles.subdir_civ2_text,'Visible','off')
4183 end
4184 
4185 %--------------------------------------------------------------
4186 function enable_patch2(handles)
4187 set(handles.frame_patch2,'BackgroundColor',[1 1 0])
4188 %set(handles.rho_patch2,'Visible','on')
4189 set(handles.rho_text2,'Visible','on')
4190 set(handles.thresh_patch2,'Visible','on')
4191 set(handles.thresh_text2,'Visible','on')
4192 set(handles.subdomain_patch2,'Visible','on')
4193 set(handles.subdomain_text2,'Visible','on')
4194 set(handles.nx_patch2,'Visible','on')
4195 set(handles.ny_patch2,'Visible','on')
4196 set(handles.nx_patch2_title,'Visible','on')
4197 set(handles.ny_patch2_title,'Visible','on')
4198 set(handles.get_gridpatch2,'Visible','on')
4199 set(handles.grid_patch2,'Visible','on')
4200 set(handles.list_pair_civ2,'Visible','on')
4201 set(handles.subdir_civ2,'Visible','on')
4202 set(handles.subdir_civ2_text,'Visible','on')
4203 
4204 %--------------------------------------------------------------
4205 function desable_patch2(handles)
4206 set(handles.frame_patch2,'BackgroundColor',[0.831 0.816 0.784])
4207 set(handles.rho_patch2,'Visible','off')
4208 set(handles.rho_text2,'Visible','off')
4209 set(handles.thresh_patch2,'Visible','off')
4210 set(handles.thresh_text2,'Visible','off')
4211 set(handles.subdomain_patch2,'Visible','off')
4212 set(handles.subdomain_text2,'Visible','off')
4213 set(handles.nx_patch2,'Visible','off')
4214 set(handles.ny_patch2,'Visible','off')
4215 set(handles.nx_patch2_title,'Visible','off')
4216 set(handles.ny_patch2_title,'Visible','off')
4217 set(handles.get_gridpatch2,'Visible','off')
4218 set(handles.grid_patch2,'Visible','off')
4219 if isequal(get(handles.CIV2,'Value'),0) & isequal(get(handles.FIX2,'Value'),0) 
4220     set(handles.list_pair_civ2,'Visible','off')
4221     set(handles.subdir_civ2,'Visible','off')
4222     set(handles.subdir_civ2_text,'Visible','off')
4223 end
4224 
4225 %--------------------------------------------------------------
4226 function enable_pair1(handles,state)
4227 set(handles.subdir_civ1,'Visible',state)
4228 set(handles.list_subdir_civ1,'Visible',state)
4229 set(handles.SUBDIR_CIV1_txt,'Visible',state)
4230 set(handles.frame_subdirciv1,'Visible',state)
4231 set(handles.list_pair_civ1,'Visible',state)
4232 set(handles.PAIR_txt,'Visible',state)
4233 set(handles.dt_display_txt,'Visible',state)
4234 set(handles.dt_unit,'Visible',state)
4235 set(handles.PAIR_frame,'Visible',state)
4236 
4237 % --- Executes on button press in test_interp.
4238 function test_interp_Callback(hObject, eventdata, handles)
4239 % hObject    handle to test_interp (see GCBO)
4240 % eventdata  reserved - to be defined in a future version of MATLAB
4241 % handles    structure with handles and user data (see GUIDATA)
4242 
4243 % Hint: get(hObject,'Value') returns toggle state of test_interp
4244 
4245 
4246 
4247 
4248 
4249 
4250 %------------------------------------------------
4251 %Read the parameters for civ1 on the interface
4252 %--------------------------------------------------
4253 function par=read_param_civ1(handles,file_ima)
4254 
4255 ibx_val=str2num(get(handles.ibx,'String'));
4256 par.ibx=num2str(ibx_val);
4257 iby_val=str2num(get(handles.iby,'String'));
4258 par.iby=num2str(iby_val);
4259 isx=get(handles.isx,'String');
4260 if isempty(str2num(isx)), isx='41'; set(handles.isx,'String','41'), end; %default
4261 if str2num(isx)<ibx_val+8,isx=num2str(ibx_val+8); set(handles.isx,'String',num2str(ibx_val+8)); end
4262 isy=get(handles.isy,'String');
4263 if isempty(str2num(isy)), isy='41'; set(handles.isy,'String','41'), end;%default
4264 if str2num(isy)<iby_val+8,isy=num2str(iby_val+8); set(handles.isy,'String',num2str(iby_val+8)); end
4265 par.isx=get(handles.isx,'String');
4266 par.isy=get(handles.isy,'String');
4267 par.shiftx=get(handles.shiftx,'String');
4268 par.shifty=get(handles.shifty,'String');
4269 if isempty(str2num(par.isx))
4270          par.isx='41';%default
4271          set(handles.isx,'String','41');
4272 end
4273 if isempty(str2num(par.isy))
4274          par.isy='41'; %default
4275          set(handles.isy,'String','41');
4276 end;
4277 if isempty(str2num(par.shiftx))
4278          par.shiftx='0';%default
4279          set(handles.shiftx,'String','0');
4280 end; 
4281 if isempty(str2num(par.shifty))
4282          par.shifty='0'; %default
4283          set(handles.shifty,'String','0');
4284 end;
4285      par.rho=get(handles.rho,'String');
4286      par.dx=get(handles.dx_civ1,'String');
4287      par.dy=get(handles.dy_civ1,'String');
4288      if isequal(str2num(par.dx),[]) 
4289          if isempty(get(handles.grid_civ1,'String'));
4290              par.dx='0'; %just read by civ program, not used
4291          else
4292             par.dx='20';%default
4293             set(handles.dx_civ1,'String','20');
4294          end
4295      end
4296      if isequal(str2num(par.dy),[])
4297          if isempty(get(handles.grid_civ1,'String'));
4298              par.dy='0';%just read by civ program, not used
4299          else
4300             par.dy='20';%default
4301             set(handles.dy_civ1_title,'String','20');
4302          end
4303      end
4304         par.pxcmx='1'; %velocities are expressed in pixel dispalcement
4305          par.pxcmy='1';
4306 %      end
4307      A=imread(file_ima);%read the first image to get the size
4308      sizim=size(A);
4309      par.npx=num2str(sizim(2));
4310      par.npy=num2str(sizim(1));
4311      time=get(handles.displ_filebase,'UserData'); %get the set of times
4312      par.gridname=get(handles.grid_civ1,'String');
4313      par.gridflag='y';
4314      if isequal(par.gridname,'')| isempty(par.gridname)
4315          par.gridname='nogrid';
4316          par.gridflag='n';
4317      end
4318      
4319 %----------------------------------------------------------------
4320 function par=read_param_civ2(handles,file_ima)
4321     par.ibx=get(handles.ibx_civ2,'String');
4322     par.iby=get(handles.iby_civ2,'String');
4323     par.rho=get(handles.rho_civ2,'String');
4324     par.decimal=int2str(get(handles.decimal,'Value'));
4325     par.deformation=int2str(get(handles.deformation,'Value'));
4326     par.dx=get(handles.dx_civ2,'String');
4327     par.dy=get(handles.dy_civ2,'String');
4328     if isequal(str2num(par.dx),[]) 
4329          if isempty(get(handles.grid_civ2,'String'));
4330              par.dx='0'; %just read by civ program, not used
4331          else
4332             par.dx='20';%default
4333             set(handles.dx_civ2,'String','20');
4334          end
4335      end
4336      if isequal(str2num(par.dy),[])
4337          if isempty(get(handles.grid_civ2,'String'));
4338              par.dy='0';%just read by civ program, not used
4339          else
4340             par.dy='20';%default
4341             set(handles.dy_civ2,'String','20');
4342          end
4343      end
4344      par.pxcmx='1';
4345      par.pxcmy='1';
4346     A=imread(file_ima);%read the first image to get the size
4347     sizim=size(A);
4348     par.npx=num2str(sizim(2));
4349     par.npy=num2str(sizim(1));
4350     time=get(handles.displ_filebase,'UserData'); %get the set of times
4351     par.gridname=get(handles.grid_civ2,'String');
4352     par.gridflag='y';
4353     if isequal(par.gridname,'')| isempty(par.gridname)
4354         par.gridname='nogrid';
4355         par.gridflag='n';
4356     end
4357 
4358 
4359 %---------------------------------------------------------
4360 %CIV1  CIV1  CIV1 CIV1
4361 %----------------------------------------------------------
4362 function cmd_CIV1=BATCH_CIV1(filename,namelog,par,handles)
4363 %pixels per cm and matrix of the image times, read from the .civ file by uvmat
4364 global civ1_exe Civ_exe sge%name of the executable for civ1 calculation
4365 
4366 %changes : filename_cmx -> filename ( no extension )
4367 
4368             if isequal(par.Dt,'0')
4369                 par.Dt='1' ;%case of 'displacement' mode
4370             end         
4371  
4372     textcmx={'##############   CMX file';...
4373     ['FirstImage ' par.filename_ima_a];...
4374     ['LastImage  ' par.filename_ima_b];...
4375     'XX' ;...
4376     ['Mask ' par.maskflag] ;...
4377     ['MaskName ' par.maskname];...
4378     ['ImageSize ' par.npx ' ' par.npy];...   %VERIFIER CAS GENERAL ?
4379     ['CorrelationBoxesSize ' par.ibx ' ' par.iby];...
4380     ['SearchBoxeSize ' par.isx ' ' par.isy];...
4381     ['RO ' par.rho];...
4382     ['GridSpacing ' par.dx ' ' par.dy];...
4383     'XX 1.0';...
4384     ['Dt_TO ' par.Dt ' ' par.T0];...
4385     ['PixCmXY ' par.pxcmx ' ' par.pxcmy];...
4386     'XX 1';...
4387     ['ShiftXY ' par.shiftx ' '  par.shifty];...
4388     ['Grid ' par.gridflag];...
4389     ['GridName ' par.gridname] ;...
4390     'XX 85';...
4391     'XX 1.0';...
4392     'XX 1.0';...
4393     'Hart 1';...
4394     'DecimalShift 0';...
4395     'Deformation 0';...
4396     'CorrelationMin 0';...
4397     'IntensityMin 0';...
4398     'SeuilImage n';...
4399     'SeuilImageValues 0 4096';...
4400     ['ImageToUse ' par.term_a ' ' par.term_b];... % VERIFIER ?
4401     'ImageUsedBefore null null'};
4402 
4403             textout=char(textcmx);
4404     %         timeL1=clock;
4405             dlmwrite([filename '.cmx'],textout,'');
4406     %             timeL2=clock;
4407     %     timciv1=etime(timeL2,timeL1)
4408           if sge  
4409           cmd_CIV1=[civ1_exe ' -f ' filename '.cmx' ]; % redirect standard output to the log file
4410           else
4411               cmd_CIV1=[civ1_exe ' -f ' filename_cmx ' > ' namelog ' 2>&1']; % redirect standard output to the log file
4412           end
4413     if(isunix)
4414         [Rootbat,Filebat,extbat]=fileparts(namelog);
4415         ncName=fullfile(Rootbat,[ Filebat '.nc']);
4416         cmd_CIV1=char({cmd_CIV1 ; ['mv ' namelog  ' ' namelog '.civ1.log'];['chmod g+w ' ncName]});
4417     else
4418         cmd_CIV1=char({cmd_CIV1 ; ['copy /Y ' namelog ' ' namelog '.civ1.log']});
4419     end
4420 
4421 %---------------------------------------------------------
4422 %CIV1  Unified
4423 %----------------------------------------------------------
4424 function xml_civ1_parameters=BATCH_CIV1_Unified(filename,namelog,par,handles)
4425 %pixels per cm and matrix of the image times, read from the .civ file by uvmat
4426 global civ1_exe Civ_exe%name of the executable for civ1 calculation
4427 
4428     civ1.image1=par.filename_ima_a;
4429     civ1.image2=par.filename_ima_b;
4430     civ1.imageSize_X=par.npx; 
4431     civ1.imageSize_Y=par.npy;
4432     civ1.outputFileName=[filename '.nc'];
4433     civ1.correlationBoxesSize_X=par.ibx;
4434     civ1.correlationBoxesSize_Y=par.iby;
4435     civ1.searchBoxesSize_X=par.isx;
4436     civ1.searchBoxesSize_Y=par.isy;
4437     civ1.globalShift_X=par.shiftx;
4438     civ1.globalShift_Y=par.shifty;
4439     civ1.ro=par.rho;
4440     civ1.hart='y';
4441     if isequal(par.gridflag,'y')
4442         civ1.grid=par.gridname;
4443     end
4444     civ1.gridSpacing_X=par.dx;
4445     civ1.gridSpacing_Y=par.dy;
4446     if isequal(par.maskflag,'y')
4447         civ1.mask=par.maskname;
4448     end
4449     civ1.dt=par.Dt;
4450     civ1.unit='pixel';
4451     civ1.absolut_time_T0=par.T0;
4452     civ1.pixcmx=par.pxcmx;
4453     civ1.pixcmy=par.pxcmy;
4454     civ1.convectFlow='n';
4455   
4456     xml_civ1_parameters=civ1;
4457    
4458 %---------------------------------------------------------
4459 %CIV2  CIV2  CIV2 CIV2
4460 %----------------------------------------------------------
4461 function cmd_CIV2=BATCH_CIV2(filename_cmx,namelog,par)
4462 %pixels per cm and matrix of the image times, read from the .civ file by uvmat
4463 global civ2_exe sge%name of the executable for civ1 calculation
4464    if isequal(par.Dt,'0')
4465                 par.Dt='1' ;%case of 'displacement' mode
4466   end 
4467 textcmx={'##############   CMX file';...
4468 ['FirstImage ' par.filename_ima_a];...
4469 ['LastImage  ' par.filename_ima_b];...
4470 'XX' ;...
4471 ['Mask ' par.maskflag];...
4472 ['MaskName ' par.maskname];...
4473 ['ImageSize ' par.npx ' ' par.npy];...   
4474 ['CorrelationBoxesSize ' par.ibx ' ' par.iby];...
4475 ['SearchBoxeSize ' par.ibx ' ' par.iby];...
4476 ['RO ' par.rho];...
4477 ['GridSpacing ' par.dx ' ' par.dy];...
4478 'XX 1.0';...
4479 ['Dt_TO ' par.Dt ' ' par.T0];...
4480 ['PixCmXY ' par.pxcmx ' ' par.pxcmy];...
4481 'XX 1';...
4482 ['ShiftXY 0 0'];...
4483 ['Grid ' par.gridflag];...
4484 ['GridName ' par.gridname];...
4485 'XX 85';...
4486 'XX 1.0';...
4487 'XX 1.0';...
4488 'Hart 1';...
4489 ['DecimalShift ' par.decimal];...
4490 ['Deformation ' par.deformation];...
4491 'CorrelationMin 0';...
4492 'IntensityMin 0';...
4493 'SeuilImage n';...
4494 'SeuilImageValues 0 4096';...
4495 ['ImageToUse ' par.term_a ' ' par.term_b];... % VERIFIER ?
4496 ['ImageUsedBefore ' par.filename_nc1]};
4497         textout=char(textcmx);
4498         dlmwrite([filename_cmx '2'] ,textout,'');
4499         if sge
4500         cmd_CIV2=[civ2_exe ' -f ' filename_cmx ]; % redirect standard output to the log file
4501         else
4502           cmd_CIV2=[civ2_exe ' -f ' filename_cmx ' > ' namelog ' 2>&1']; % redirect standard output to the log file
4503       end
4504 
4505 
4506 % --- Executes on button press in HELP.
4507 function HELP_Callback(hObject, eventdata, handles)
4508 path_to_uvmat=which ('uvmat');% check the path of uvmat
4509 pathelp=fileparts(path_to_uvmat);
4510 helpfile=fullfile(pathelp,'UVMAT_DOC','uvmat_doc.html');
4511 if isempty(dir(helpfile)), errordlg('Please put the help file uvmat_doc.html in the directory UVMAT/UVMAT_DOC')
4512 else
4513 web([helpfile '#civ'])    
4514 end
4515 
4516 
4517 %----------------------------------------------------------
4518 %--read images and convert them to the uint16 format used for PIV
4519 function A=read_image(filename,nom_type,npx,npy,num);
4520 %npx, npy are the dimensions needed for the raw SMD images
4521 %num is the view number needed for an avi movie
4522 if isequal(nom_type,'avi')
4523     mov=aviread(filename,num);
4524     A=frame2im(mov(1));
4525     A=sum(double(A),3);
4526     A=uint16(A);
4527 elseif isequal(nom_type,'raw_SMD')
4528     [fid,message]=fopen(filename,'r');    
4529     B=fread(fid,Inf,'int16',0,'ieee-le');%read 16 bit binary file
4530     A=(reshape(B,npx,npy))'; %remplissage ligne par ligne avec une matrice colonne ? transposer(uB) pour avoir une matrice ligne
4531     A=uint16(A);
4532     fclose(fid);
4533 else
4534     A=imread(filename);
4535     siz=size(A);
4536     if length(siz)==3;%color images
4537         A=sum(double(A),3);
4538     end
4539     A=uint16(A);
4540 end
4541         
4542 %----------------------------------------------------------------
4543 %Executes on carriage return on the time interval dt
4544 %----------------------------------------------------------------
4545 function dt_Callback(hObject, eventdata, handles)
4546 %determine the set of times and possible intervals for CIV
4547 %                 answer=inputdlg('time interval between images?');
4548                 dt=(1/1000)*str2num(get(handles.dt,'String'));
4549                 nbfield=str2num(get(handles.nb_field,'String')); %last image number selected in the processing series
4550                 time=(dt*[0:nbfield-1])';
4551 %                 set(handles.incr_i,'UserData',dt);%store the time interval between successive images
4552                 set(handles.displ_filebase,'UserData',time); %store the set of times
4553                 for index=1:min(nbfield-1,200)
4554                     displ_num(1,index)=1;
4555                     displ_num(2,index)=1;
4556                     displ_num(3,index)=-floor(index/2);
4557                     displ_num(4,index)=ceil(index/2);
4558                 end
4559 set(handles.list_pair_civ1,'Value',1);
4560 set(handles.list_pair_civ1,'UserData',displ_num);
4561 set(handles.list_pair_civ2,'Value',1);
4562 %update the list of time intervals
4563 find_netcpair_civ1(hObject, eventdata, handles)
4564 find_netcpair_civ2(hObject, eventdata, handles)
4565 
4566 %-------------------------------------------------------
4567 function ref_i_Callback(hObject, eventdata, handles)
4568 mode_list=get(handles.mode,'String');
4569 mode_value=get(handles.mode,'Value');
4570 mode=mode_list{mode_value};
4571 if isequal(get(handles.CIV1,'Value'),0)| isequal(mode,'series(Di)') 
4572     find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4573 end
4574 if isequal(mode,'series(Di)') | ...% we do patch2 only
4575    (get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0)
4576     find_netcpair_civ2(hObject, eventdata, handles);
4577 end
4578 
4579 %----------------------------------------------------
4580 function ref_j_Callback(hObject, eventdata, handles)
4581 mode_list=get(handles.mode,'String');
4582 mode_value=get(handles.mode,'Value');
4583 mode=mode_list{mode_value};
4584 if isequal(get(handles.CIV1,'Value'),0)| isequal(mode,'series(Dj)') 
4585     find_netcpair_civ1(hObject, eventdata, handles);% update the menu of pairs depending on the available netcdf files
4586 end
4587 if isequal(mode,'series(Dj)') | ...
4588    (get(handles.CIV2,'Value')==0 & get(handles.CIV1,'Value')==0 & get(handles.FIX1,'Value')==0 & get(handles.PATCH1,'Value')==0)
4589     find_netcpair_civ2(hObject, eventdata, handles);
4590 end
4591 %----------------------------------------------------
4592 % --- Executes on button press in compare.
4593 function compare_Callback(hObject, eventdata, handles)
4594 test=get(handles.compare,'Value');
4595 if test==2 || test==3    
4596     filebase=get(handles.displ_filebase,'String');
4597     browse=get(handles.browse_root,'Userdata');
4598     browse.nom_type_ima1=browse.nom_type_ima;
4599     set(handles.browse_root,'UserData',browse);
4600     set(handles.sub_txt,'Visible','on')
4601     set(handles.displ_filebase2,'Visible','On');%mkes the second file input window visible
4602     mode_store=get(handles.mode,'String');%get the present 'mode'
4603     set(handles.compare,'UserData',mode_store);%store the mode display
4604     set(handles.mode,'Visible','off')
4605     if test==2
4606         set(handles.mode,'Visible','off')       
4607     else
4608         set(handles.mode,'Visible','on')
4609     end
4610     
4611     % open an image file with the browser
4612     ind_opening=1;%default
4613     browse.incr_pair=[0 0]; %default
4614     oldfile=get(handles.displ_filebase,'String');
4615     menu={'*.xml;*.avi;*.AVI;*.nc','(*.xml,*.avi,*.nc)'; ...
4616             '*.xml', '.xml files';'*.avi;*.AVI', '.avi files';'*.nc', '.nc files';...
4617             '*.*', 'All Files (*.*)'};
4618     [FileName, PathName, filtindex] = uigetfile( menu, 'Pick a file',oldfile);
4619     fileinput=[PathName FileName];%complete file name
4620     sizf=size(fileinput);
4621     if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4622     [path,name,ext]=fileparts(fileinput);
4623     [path1]=fileparts(filebase);
4624     if ~strcmp(path1,path)
4625          msgbox_uvmat('ERROR','The two  input image series must be in the same directory')
4626         return
4627     end
4628     set(handles.displ_filebase2,'String',name);
4629     [RootPath,RootFile,field_count,str2,str_a,str_b,xx,nom_type,subdir]=name2display(name);
4630     browse=get(handles.browse_root,'UserData');
4631     browse.nom_type_ima_1=nom_type;
4632     set(handles.browse_root,'UserData',browse)  
4633     
4634     %check image extension
4635     if ~strcmp(ext,get(handles.ext_ima,'String'))
4636         msgbox_uvmat('ERROR','The two  input image series must have the same extenion name')
4637         return
4638     end
4639     
4640     %check image size
4641     A=imread(fileinput);
4642     npxy=get(handles.ext_ima,'UserData');
4643     if ~isequal(npxy(1),size(A,1))|| ~isequal(npxy(2),size(A,2))
4644         msgbox_uvmat('ERROR','The two input image series must have the same size')
4645         return
4646     end
4647 else
4648     set(handles.mode,'Visible','on')
4649     set(handles.displ_filebase2,'Visible','Off');
4650     set(handles.sub_txt,'Visible','off')
4651     set(handles.displ_filebase2,'String',[]);
4652     mode_store=get(handles.compare,'UserData');
4653     set(handles.mode,'String',mode_store)
4654     set(handles.test_stereo1,'Value',0)
4655     set(handles.test_stereo2,'Value',0)
4656 end
4657 mode_Callback(hObject, eventdata, handles)
4658 
4659 %-----------------------------------------------------------
4660 % --- Executes on button press in get_ref_fix1.
4661 function get_ref_fix1_Callback(hObject, eventdata, handles)
4662 filebase=get(handles.displ_filebase,'String');
4663 [FileName, PathName, filterindex] = uigetfile( ...
4664        {'*.nc', ' (*.nc)';
4665         '*.nc',  'netcdf files '; ...
4666         '*.*', 'All Files (*.*)'}, ...
4667         'Pick a file',filebase);
4668     
4669 fileinput=[PathName FileName];
4670 sizf=size(fileinput);
4671 if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4672 %[filebasesub,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4673 [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4674 %filebase=fullfile(RootPath,RootFile);
4675 % [Pth,FileN]=fileparts(filebasesub);
4676 % Pth=fileparts(Pth);
4677 ref.filebase=fullfile(Path,File);
4678 ref.num_a=stra2num(str_a);
4679 ref.num_b=stra2num(str_b);
4680 ref.num1=str2num(field_count);
4681 ref.num2=str2num(str2);
4682 browse=[];%initialisation
4683 if ~isequal(ref.ext,'.nc')
4684     errordlg('the reference file must be in netcdf format (*.nc)')
4685     return
4686 end
4687 % [path,name]=fileparts(ref.filebase);
4688 set(handles.ref_fix1,'String',[fullfile(ref.subdir,File) '....nc']);
4689 set(handles.ref_fix1,'UserData',ref)
4690 menu_field{1}='civ1';
4691 Data=nc2struct(fileinput,[]);
4692 if isfield(Data,'patch') & isequal(Data.patch,1)
4693     menu_field{2}='filter1';
4694 end
4695 if isfield(Data,'civ2') & isequal(Data.civ2,1)
4696     menu_field{3}='civ2';
4697 end
4698 if isfield(Data,'patch2') & isequal(Data.patch2,1)
4699     menu_field{4}='filter2';
4700 end
4701 % [cte_detect,vdt,cte_read]=read_netcdf(fileinput,{'patch','civ2','patch2'});
4702 % if isequal(cte_detect(1),1) & isequal(cte_read(1),1)
4703 %          menu_field{2}='filter1';
4704 % end
4705 % if isequal(cte_detect(2),1) & isequal(cte_read(2),1)
4706 %          menu_field{3}='civ2';
4707 % end
4708 % if isequal(cte_detect(3),1) & isequal(cte_read(3),1)
4709 %          menu_field{4}='filter2';
4710 % end
4711 set(handles.field_ref1,'String',menu_field);
4712 set(handles.field_ref1,'Value',length(menu_field));
4713 set(handles.inf_sup1,'Value',2);
4714 set(handles.thresh_vel,'String','1');%default threshold
4715 set(handles.ref_fix1,'Enable','on')
4716  
4717 %---------------------------------------------------------------
4718 % --- Executes on button press in get_ref_fix2.
4719 function get_ref_fix2_Callback(hObject, eventdata, handles)
4720 if isequal(get(handles.get_ref_fix2,'Value'),1)
4721     filebase=get(handles.displ_filebase,'String');
4722     [FileName, PathName, filterindex] = uigetfile( ...
4723            {'*.nc', ' (*.nc)';
4724             '*.nc',  'netcdf files '; ...
4725             '*.*', 'All Files (*.*)'}, ...
4726             'Pick a file',filebase);
4727     fileinput=[PathName FileName];
4728     sizf=size(fileinput);
4729     if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end %stop if fileinput not a character string
4730     %[filebasesub,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4731     [Path,File,field_count,str2,str_a,str_b,ref.ext,ref.nom_type,ref.subdir]=name2display(fileinput);
4732 %     [Pth,FileN]=fileparts(filebasesub);
4733 %     Pth=fileparts(Pth);
4734     ref.filebase=fullfile(Path,File);
4735     ref.num_a=stra2num(str_a);
4736     ref.num_b=stra2num(str_b);
4737     ref.num1=str2num(field_count);
4738     ref.num2=str2num(str2);
4739     browse=[];%initialisation
4740     if ~isequal(ref.ext,'.nc')
4741         errordlg('the reference file must be in netcdf format (*.nc)')
4742         return
4743     end
4744 %     [path,name]=fileparts(ref.filebase);
4745     set(handles.ref_fix2,'String',[fullfile(ref.subdir,File) '....nc']);
4746     set(handles.ref_fix2,'UserData',ref)    
4747     menu_field{1}='civ1';
4748 %     [cte_detect,vdt,cte_read]=read_netcdf(fileinput,{'patch','civ2','patch2'});
4749     Data=nc2struct(fileinput,[]);
4750     if isfield(Data,'patch') & isequal(Data.patch,1)
4751         menu_field{2}='filter1';
4752     end
4753     if isfield(Data,'civ2') & isequal(Data.civ2,1)
4754         menu_field{3}='civ2';
4755     end
4756     if isfield(Data,'patch2') & isequal(Data.patch2,1)
4757         menu_field{4}='filter2';
4758     end
4759 
4760 %     if isequal(cte_detect(1),1) & isequal(cte_read(1),1)
4761 %              menu_field{2}='filter1';
4762 %     end
4763 %     if isequal(cte_detect(2),1) & isequal(cte_read(2),1)
4764 %              menu_field{3}='civ2';
4765 %     end
4766 %     if isequal(cte_detect(3),1) & isequal(cte_read(3),1)
4767 %              menu_field{4}='filter2';
4768 %     end
4769     set(handles.field_ref2,'String',menu_field);
4770     set(handles.field_ref2,'Value',length(menu_field));
4771     set(handles.inf_sup2,'Value',2);
4772     set(handles.thresh_vel2,'String','1');%default threshold
4773     set(handles.ref_fix2,'Enable','on')
4774     set(handles.ref_fix2,'Visible','on')
4775     set(handles.field_ref2,'Visible','on')
4776 else
4777     set(handles.ref_fix2,'Visible','off')
4778     set(handles.field_ref2,'Visible','off')
4779 end
4780 %-------------------------------------------------------
4781 
4782 function ref_fix1_Callback(hObject, eventdata, handles)
4783     set(handles.inf_sup1,'Value',1);
4784     set(handles.field_ref1,'Value',1)
4785     set(handles.field_ref1,'String',{' '})
4786     set(handles.ref_fix1,'UserData',[]);
4787     set(handles.ref_fix1,'String','');
4788     set(handles.thresh_vel1,'String','0');
4789  
4790 
4791 %------------------------------------------------------
4792 
4793 function ref_fix2_Callback(hObject, eventdata, handles)
4794     set(handles.inf_sup2,'Value',1);
4795     set(handles.field_ref2,'Value',1)
4796     set(handles.field_ref2,'String',{' '})
4797     set(handles.ref_fix2,'UserData',[]);
4798     set(handles.ref_fix2,'String','');
4799     set(handles.thresh_vel2,'String','0');
4800 
4801 %--------------------------------------------------------
4802 % --- Executes on selection change in inf_sup1.
4803 function inf_sup1_Callback(hObject, eventdata, handles)
4804 
4805 %--------------------------------------------------------------------------
4806 
4807 
4808 % --- Executes on selection change in field_ref.
4809 function field_ref_Callback(hObject, eventdata, handles)
4810 
4811 %-------------------------------------------------------------------------
4812 
4813 % --- Executes on selection change in field_ref2.
4814 function field_ref2_Callback(hObject, eventdata, handles)
4815 
4816 % -----------------------------------------------------------
4817 % transform letters to numbers
4818 %--------------------------------------------
4819 function numres=stra2num(str)
4820 numres=double(str)-96;
4821 if double(str) >= 48 & double(str) <= 57 % = 1 for numbers
4822     numres=str2num(str);
4823 end
4824 
4825 
4826 % --- Executes on button press in test_stereo1.
4827 function test_stereo1_Callback(hObject, eventdata, handles)
4828 if isequal(get(handles.test_stereo1,'Value'),0)
4829     set(handles.subdomain_patch1,'Visible','on')
4830     set(handles.rho_patch1,'Visible','on')
4831 else
4832     set(handles.subdomain_patch1,'Visible','off')
4833     set(handles.rho_patch1,'Visible','off')
4834 end
4835 
4836 % --- Executes on button press in test_stereo2.
4837 function test_stereo2_Callback(hObject, eventdata, handles)
4838 if isequal(get(handles.test_stereo2,'Value'),0)
4839     set(handles.subdomain_patch2,'Visible','on')
4840     set(handles.rho_patch2,'Visible','on')
4841 else
4842     set(handles.subdomain_patch2,'Visible','off')
4843     set(handles.rho_patch2,'Visible','off')
4844 end
4845 
4846 % --- Executes on button press in ImaThreshold.
4847 function ImaThreshold_Callback(hObject, eventdata, handles)
4848 if isequal(get(handles.ImaThreshold,'Value'),1)
4849     set(handles.MinIma,'Visible','on')
4850     set(handles.MaxIma,'Visible','on')
4851 else
4852     set(handles.MinIma,'Visible','off')
4853     set(handles.MaxIma,'Visible','off')
4854 end
4855 
4856 
4857 % --- Executes on button press in ImaThreshold2.
4858 function ImaThreshold2_Callback(hObject, eventdata, handles)
4859 if isequal(get(handles.ImaThreshold2,'Value'),1)
4860     set(handles.MinIma2,'Visible','on')
4861     set(handles.MaxIma2,'Visible','on')
4862 else
4863     set(handles.MinIma2,'Visible','off')
4864     set(handles.MaxIma2,'Visible','off')
4865 end
4866 
4867 
4868 % --- Executes on button press in Experimental.
4869 function Experimental_Callback(hObject, eventdata, handles)
4870 
4871 
4872 
4873 function ext_ima_1_Callback(hObject, eventdata, handles)
4874 % hObject    handle to ext_ima_1 (see GCBO)
4875 % eventdata  reserved - to be defined in a future version of MATLAB
4876 % handles    structure with handles and user data (see GUIDATA)
4877 
4878 % Hints: get(hObject,'String') returns contents of ext_ima_1 as text
4879 %        str2double(get(hObject,'String')) returns contents of ext_ima_1 as a double
4880 
4881 
4882 % --- Executes during object creation, after setting all properties.
4883 function ext_ima_1_CreateFcn(hObject, eventdata, handles)
4884 % hObject    handle to ext_ima_1 (see GCBO)
4885 % eventdata  reserved - to be defined in a future version of MATLAB
4886 % handles    empty - handles not created until after all CreateFcns called
4887 
4888 % Hint: edit controls usually have a white background on Windows.
4889 %       See ISPC and COMPUTER.
4890 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
4891     set(hObject,'BackgroundColor','white');
4892 end
4893 
4894 
4895 
4896 function ext_ima_Callback(hObject, eventdata, handles)
4897 % hObject    handle to ext_ima (see GCBO)
4898 % eventdata  reserved - to be defined in a future version of MATLAB
4899 % handles    structure with handles and user data (see GUIDATA)
4900 
4901 % Hints: get(hObject,'String') returns contents of ext_ima as text
4902 %        str2double(get(hObject,'String')) returns contents of ext_ima as a double
4903 
4904 
4905 % --- Executes during object creation, after setting all properties.
4906 function ext_ima_CreateFcn(hObject, eventdata, handles)
4907 % hObject    handle to ext_ima (see GCBO)
4908 % eventdata  reserved - to be defined in a future version of MATLAB
4909 % handles    empty - handles not created until after all CreateFcns called
4910 
4911 % Hint: edit controls usually have a white background on Windows.
4912 %       See ISPC and COMPUTER.
4913 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
4914     set(hObject,'BackgroundColor','white');
4915 end
4916 
4917

Generated on Fri 13-Nov-2009 11:17:03 by m2html © 2003