source: trunk/src/civ.m @ 378

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

sge option for ENS Lyon psmn added

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