source: trunk/src/civ.m @ 301

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

various bug fixes in civ.m

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