source: trunk/src/civ.m @ 344

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

small update in civ

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