!****************************************************************
SUBROUTINE radar_profs(plat,plon,kobtype,kprof,plat_2d,plon_2d,kerror)

! Routine to calculate a series of points to define the neighboring
! box for a radar inversion of the reflectivity column, given the latitude
! and longitude
! of the observation point, the number of the profiles needed kprof and
! the resolution of the box zdist

! Eric Wattrelot, 10 November 2006
!-------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK    ,DR_HOOK
USE YOMCST   , ONLY : RPI      ,RA
USE YOMLUN   , ONLY : NULOUT

IMPLICIT NONE

REAL(KIND=JPRB),    INTENT(IN)  :: plat           ! Lat of sub-tangent pt [deg]
REAL(KIND=JPRB),    INTENT(IN)  :: plon           ! Lon of sub-tangent pt [deg]
INTEGER(KIND=JPIM), INTENT(IN)  :: kobtype        ! Number of points
INTEGER(KIND=JPIM), INTENT(IN)  :: kprof          ! Number of points                 
REAL(KIND=JPRB),    INTENT(OUT) :: plat_2d(kprof) ! Lats of points [deg] 
REAL(KIND=JPRB),    INTENT(OUT) :: plon_2d(kprof) ! Lons of points [deg]
INTEGER(KIND=JPIM), INTENT(OUT) :: kerror         ! Error code

! local

REAL(KIND=JPRB)          :: zdist
INTEGER(KIND=JPIM)       :: i,j,iiprof,IINTPROF
REAL(KIND=JPRB) :: ZHOOK_HANDLE


!-------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('RADAR_PROFS',0,ZHOOK_HANDLE)


! set the initial interval number value


IINTPROF = INT((sqrt(real(kprof)) - 1)/2.0_JPRB)  ! interval number for loop

! set the value of distance where we want to find a profile

zdist = 100000.0_JPRB ! 100 kms pour commencer

! boucle sur les interdistances pour obtenir tous les lat,lon des profiles
! premiers profiles centraux

plat_2d(1) = plat ! premier profile, obs elle-meme
plon_2d(1) = plon ! premier profile, obs elle-meme

iiprof = 1
DO i = 1, IINTPROF

 iiprof = iiprof + 1 
 plat_2d(iiprof) = plat + (REAL(i)*zdist)/(REAL(IINTPROF)*RA)  
 plon_2d(iiprof) = plon

 iiprof = iiprof + 1
 plat_2d(iiprof) = plat - (REAL(i)*zdist)/(REAL(IINTPROF)*RA)
 plon_2d(iiprof) = plon

 iiprof = iiprof + 1 
 plat_2d(iiprof) = plat 
 plon_2d(iiprof) = plon + (REAL(i)*zdist)/(REAL(IINTPROF)*RA &
  &                *COS(plat_2d(iiprof)))
   
 iiprof = iiprof + 1 
 plat_2d(iiprof) = plat
 plon_2d(iiprof) = plon -(REAL(i)*zdist)/(REAL(IINTPROF)*RA &
  &                *COS(plat_2d(iiprof)))
ENDDO 

iiprof = nint(2*(sqrt(real(kprof))))-1
DO i=1, IINTPROF ! boucle sur les intervalles, nombre de latitudes
  DO j=1, IINTPROF ! boucle sur les longitudes, nombre de longitudes par
!latitude
    iiprof = iiprof + 1  ! compteur du nombre de profils
    plat_2d(iiprof) = plat + (REAL(i)*zdist)/(REAL(IINTPROF)*RA)
! condition on latitude
    if (COS(plat_2d(iiprof))<0._JPRB) THEN 
      if (plat_2d(iiprof)>RPI/2.) THEN
        plat_2d(iiprof)=RPI-plat_2d(iiprof)
      ELSEIF (plat_2d(iiprof)<-RPI/2.) THEN
        plat_2d(iiprof)=-RPI-plat_2d(iiprof)
      ENDIF        
    ENDIF   
! end of the condition
    plon_2d(iiprof) = plon + (REAL(j)*zdist)/(REAL(IINTPROF)*RA &
     &               *COS(plat_2d(iiprof)))

    iiprof = iiprof + 1 
    plat_2d(iiprof) = plat_2d(iiprof-1)
    plon_2d(iiprof) = plon - (REAL(j)*zdist)/(REAL(IINTPROF)*RA &
     &                *COS(plat_2d(iiprof)))

    iiprof = iiprof + 1 
    plat_2d(iiprof) = plat - (REAL(i)*zdist)/(REAL(IINTPROF)*RA)
    plon_2d(iiprof) = plon + (REAL(j)*zdist)/(REAL(IINTPROF)*RA &
     &                *COS(plat_2d(iiprof)))
 
    iiprof = iiprof + 1 
    plat_2d(iiprof) = plat_2d(iiprof-1)
    plon_2d(iiprof) = plon - (REAL(j)*zdist)/(REAL(IINTPROF)*RA &
     &                *COS(plat_2d(iiprof)))

  ENDDO  !  end loop  longitude interval 
ENDDO ! end loop  latitude interval
! loop for verification of my vicinity
DO i = 1, kprof
  WRITE(NULOUT,*)"lat et lon du profile numero",i,"sont",plat_2d(i),plon_2d(i)
ENDDO

IF (LHOOK) CALL DR_HOOK('RADAR_PROFS',1,ZHOOK_HANDLE)

END SUBROUTINE radar_profs
