!-*- F90 -*-

!
! Structure containing the geopotential height and derivatives
! of a model profile.
!
! Functions:
!      Construct the geop hgt cached profiles.
!
! Josep M. Aparicio, 2003-2012.
! ARMA/ASTD
! Environment Canada
!
! Revisions:
!        J.M. Aparicio - ARMA Feb 2012
!          Improved compressibility
!

module modgps07geostruct 2,12
  use modgps00base     , only : i4, dp, ngpssize
  use modgps02wgs84const,only : WGS_a, WGS_OmegaPrime
  use modgps02wgs84grav ,only : gpsgravityalt
  use modgps03diff
  use modgps04profile  , only : gpsprofile
  use modgps05refstruct, only : gpscmp, gpsden
  

  implicit none
  
contains
  

  subroutine gpsbvf(prf, bvf) 2,4
    type(gpsprofile)              :: prf
    type(gpsdiff)   , intent(out) :: bvf(ngpssize)

    type(gpsdiff)                 :: den(ngpssize), dddz(ngpssize)
    integer(i4)                   :: i, ngpslev, im, ip
    real(dp)                      :: g, sLat

    call gpsden(prf, den)

    ngpslev = prf%ngpslev
    sLat=sin(prf%rLat)
    do i = 1, ngpslev
       ip=i+1
       im=i-1
       if (i==1)       im=1
       if (i==ngpslev) ip=ngpslev
       dddz(i)=den(i)*(log(den(ip))-log(den(im)))/(prf%gst(ip)-prf%gst(im))
       g=gpsgravityalt(sLat, prf%gst(i)%Var)
       bvf(i)=sqrt((-g)/den(i)*dddz(i))
    enddo
  end subroutine gpsbvf

end module modgps07geostruct