source: trunk/src/civ.m @ 307

Last change on this file since 307 was 307, checked in by sommeria, 12 years ago

bug fixed in the browser of civ + bug fix for the plot of objects

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