source: trunk/src/civ.m @ 308

Last change on this file since 308 was 308, checked in by gostiaux, 12 years ago

batch for LEGI debugging

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