source: trunk/src/civ.m @ 36

Last change on this file since 36 was 36, checked in by sommeria, 15 years ago

-get_field: functions called by ACTION in the GUI get_field put in the subdirectory get_field (like for series)
-help put in the subdir uvmat_doc, help call in uvmat, civ, geometry calib modified accordingly

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