m_spherdist.F90 894 B

123456789101112131415161718192021222324252627282930
  1. module m_spherdist
  2. contains
  3. ! Computes the distance between geo. pos. lon1, lat1 and lon2, lat2.
  4. ! http://en.wikipedia.org/wiki/Great-circle_distance
  5. !
  6. ! Input is in degrees, output in meters
  7. !
  8. !
  9. !FC: 29/02/12 add min max to avoid NaN from acos
  10. real function spherdist(lon1, lat1, lon2, lat2)
  11. implicit none
  12. real(8), intent(in) :: lon1, lat1, lon2, lat2 ! pos. in degrees
  13. real(8), parameter :: INVRAD = 3.14159265358979323846d0 / 180.0d0
  14. real, parameter :: REARTH = 6371000.0d0
  15. real :: rlon1, rlat1, rlon2, rlat2 ! pos. in radians
  16. rlon1 = lon1 * INVRAD !lon1 in rad
  17. rlat1 = lat1 * INVRAD !90-lat1 in rad
  18. rlon2 = lon2 * INVRAD ! lon2 in rad
  19. rlat2 = lat2 * INVRAD !90 - lat2 in rad
  20. spherdist = REARTH * acos(min(max(sin(rlat1) * sin(rlat2)&
  21. + cos(rlat1) * cos(rlat2) * cos(rlon1 - rlon2),-1.),1.))
  22. end function spherdist
  23. end module m_spherdist