source: trunk/src/civ.m @ 416

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

bugs corrected in civ: undefined pxcm_search

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