! find_peak interpolation of correlation function and optimization
! Copyright (C) 1999,2010 Gauthier Delerce
!
! This program is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation; either version 2, or (at your option)
! any later version.
!
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program; if not, write to the Free Software Foundation,
! Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
!
! Gauthier Delerce <gauthier@delerce.fr>
subroutine find_peak
use modvsv
use modcorr
use modresultats
use peakopt
use modgrille
use modimsls
!use modnetcdf
implicit none
real,dimension(3)::tottime,tttt
logical::instable
integer::i,j,k,cptout,cpterror
real,dimension(:,:),pointer::corr
logical::verbose
integer::i1,j1
verbose=.false.
allocate(corr(maxval(ncor(:,1)),maxval(ncor(:,2))))
allocate(u(nbnodes),v(nbnodes),corrval(nbnodes))
tottime=0.
cptout=0
errorcpt=0
print*,"find_peak,smooth_coeff is",roi
do i=1,nbnodes
if((floor(grille(i,3))==3).or.(grille(i,3)==0.)) then
grille(i,4)=0.
grille(i,5)=0.
goto 2
endif
corr=corr_tab(i,:,:)
! call sfit1(ncor(i,1),ncor(i,2),corr,u(i),v(i),corrval(i),0.,0.,roi,small)
tttt=0.
cpterror=sum(errorcpt(1:8))
! DISPLAY CORRELATION TAB
goto 111
do j1=1,ncor(i,2)
write(*,*)(corr(i1,j1),i1=1,ncor(i,1))
enddo
111 continue
call sfit1_imsl(ncor(i,1),ncor(i,2),corr,u(i),v(i),corrval(i),0.,0.,roi,small,instable)
! print*,"u(i),v(i)",u(i),v(i),ncor(i,1),ncor(i,2)
if(instable) then
grille(i,3)=3.3
goto 2
endif
grille(i,6)=corrval(i)
if(small) then
! print*,' Search area too small, increasing .... ',i
! grille(i,4)=-((is(i,1)-ib(i,1))/2.+1-u(i) -nint(grille(i,4)))
! grille(i,5)=((is(i,2)-ib(i,2))/2.+1-v(i) +nint(grille(i,5)))
!! print*,grille(i,:)
grille(i,3)=-2.
goto 2
endif
!print*,u(i),grille(i,4),u(i)-grille(i,4)
!print*,(float(sizexb-sizexa)/2.+1.),u(i),float(nint(grille(i,4)))
if(corrdec) then
! pastis(i,1)=pastis(i,1)+grille(i,4)
! pastis(i,2)=-(pastis(i,2)-grille(i,5))
grille(i,4)=((u(i)-(float(sizexb-sizexa)/2.+1.))+grille(i,4))!+2*float(nint(grille(i,4))))
grille(i,5)=((v(i)-(float(sizeyb-sizeya)/2.+1.))+grille(i,5)) !-float(nint(grille(i,5)))
! print*,"sizeyb,sizeya",sizeyb,sizeya,"float(sizeyb-sizeya)/2.+1.",float(sizeyb-sizeya)/2.+1.,grille(i,5),grille0(i,4)
! Print*,'XCORR ',grille(i,4),grille(i,5)
! Print*,'PASTIS',pastis(i,1),pastis(i,2)
else
! print*,"nint(grille(i,5))",nint(grille(i,5))
! print*,"sizexb,sizexa",sizexb,sizexa,"float(sizexb-sizexa)/2.+1.",float(sizexb-sizexa)/2.+1.,grille(i,4),grille0(i,3)
grille(i,4)=((u(i)-(float(sizexb-sizexa)/2.+1.))+float(nint(grille(i,4))))!+2*float(nint(grille(i,4))))
grille(i,5)=((v(i)-(float(sizeyb-sizeya)/2.+1.))+float(nint(grille(i,5)))) !-float(nint(grille(i,5)))
endif
grille(i,3)=1.
! print*,(grille(i,4))-(grille0(i,3)),((grille(i,5)+grille0(i,4)))
! if( (nint(grille(i,4))/=nint(grille0(i,3))).or.(nint(grille(i,5))/=nint(-1.*grille0(i,4)))) then
!goto 456
!goto 457
if( ( abs(grille(i,4)-grille0(i,3)) >=1.).or.(abs(grille(i,5)-grille0(i,4))>=1.)) then
cptout=cptout+1
grille(i,3)=4
print*,"Difference entre l'estimateur et le resultat"
print*,grille(i,4),grille0(i,3)," ",grille(i,5),grille0(i,4)
goto 2
endif
!457 continue
if ((cpterror/=sum(errorcpt(1:8)))) then
cptout=cptout+1
! print*,'HUMMMMMM ca change par rapport au calcul precedent au noeud:',i
! print*,(grille(i,4)),(grille0(i,3)),grille(i,5),grille0(i,4)
! print*,(grille(i,4))-(grille0(i,3)),(grille(i,5))+(grille0(i,4))
! print*,''
! if(hole) then
! grille(i,3)=2.
! else
grille(i,3)=4.1
! endif
! grille(i,4)=grille0(i,3)
! grille(i,5)=-1*grille0(i,4)
goto 2
endif
!456 continue
! grille(i,4)=((is(i,1)-ib(i,1))/2.+1-u(i))-nint(grille(i,4))
! grille(i,5)=((is(i,2)-ib(i,2))/2.+1-v(i))/float(ratio)+nint(grille(i,5))
! print*,i,grille(i,4:5),u(i),v(i)
! print*,''
grille(i,3)=1.
! print*,grille(i,4:5),grille0(i,3),-1*grille0(i,4)
2 continue
! grille(i,4)=grille0(i,3)
! grille(i,5)=-1*grille0(i,4)
enddo
!print*,'tot time ',tottime,'/',nbnodes
if(verbose) Print*,'nb de vecteurs differents',cptout,'/',nbnodes
!Print*,errorcpt
if(errorcpt(1)/=0) then
print*,'Both the actual and predicted relative reductions in the function are less'
print*,'than or equal to the relative function convergence tolerance.'
Print*,errorcpt(1),'/',nbnodes
print*,''
endif
if(errorcpt(2)/=0) then
print*,'The iterates appear to be converging to a noncritical point.'
Print*,errorcpt(2),'/',nbnodes
print*,''
endif
if(errorcpt(3)/=0) then
print*,'Maximum number of iterations exceeded.'
Print*,errorcpt(3),'/',nbnodes
print*,''
endif
if(errorcpt(4)/=0) then
print*,'Maximum number of function evaluations exceeded.'
Print*,errorcpt(4),'/',nbnodes
print*,''
endif
if(errorcpt(5)/=0) then
print*,'Maximum number of gradient evaluations exceeded.'
Print*,errorcpt(5),'/',nbnodes
print*,''
endif
if(errorcpt(6)/=0) then
print*,'Five consecutive steps have been taken with the maximum step length.'
Print*,errorcpt(6),'/',nbnodes
print*,''
endif
if(errorcpt(7)/=0) then
print*,'Scaled step tolerance satisfied; the current point may be an approximate local solution,'
print*,'or the algorithm is making very slow progress and is not near a solution, or STEPTL is too big.'
Print*,errorcpt(7),'/',nbnodes
print*,''
endif
if(errorcpt(8)/=0) then
print*,'The last global step failed to locate a lower point than the current X value.'
Print*,errorcpt(8),'/',nbnodes
print*,''
endif
!Print*,'*******************************'
return
end