source: trunk/src/civ.m @ 246

Last change on this file since 246 was 246, checked in by sommeria, 13 years ago

thin plate shell (patch) introduced

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