source: trunk/src/civ.m @ 492

Last change on this file since 492 was 492, checked in by sommeria, 13 years ago

civ corrected to deal with windows system
proj_field improved to provject tps fields on lines

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