source: trunk/src/civ.m @ 35

Last change on this file since 35 was 16, checked in by gostiaux, 15 years ago

In windows, the fprintf command has troubles with / characters; the / needs to be replaced by , this is done now in CIV1_CALLBACK, needs to be done in other routines if it is OK

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