source: trunk/src/civ.m @ 311

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

oar menu for different modes added

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