m_spherdist.f90 1.1 KB

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