source: trunk/src/civ.m @ 298

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

improve the input to the civ GUI

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