|
| 1 | +! haversine_distmat.f90 |
| 2 | +! |
| 3 | +! =========================================================================== |
| 4 | +! This file is part of py-eddy-tracker. |
| 5 | +! |
| 6 | +! py-eddy-tracker is free software: you can redistribute it and/or modify |
| 7 | +! it under the terms of the GNU General Public License as published by |
| 8 | +! the Free Software Foundation, either version 3 of the License, or |
| 9 | +! (at your option) any later version. |
| 10 | +! |
| 11 | +! py-eddy-tracker is distributed in the hope that it will be useful, |
| 12 | +! but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +! GNU General Public License for more details. |
| 15 | +! |
| 16 | +! You should have received a copy of the GNU General Public License |
| 17 | +! along with py-eddy-tracker. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +! |
| 19 | +! Copyright (c) 2014 by Evan Mason |
| 20 | + |
| 21 | +! =========================================================================== |
| 22 | +! |
| 23 | +! To compile for f2py do following in terminal: |
| 24 | +! f2py -m haversine_distmat -h haversine_distmat.pyf haversine_distmat.f90 --overwrite-signature |
| 25 | +! f2py -c --fcompiler=gfortran haversine_distmat.pyf haversine_distmat.f90 |
| 26 | +! |
| 27 | +! Version 1.0.1 |
| 28 | +! |
| 29 | +!=========================================================================== |
| 30 | + |
| 31 | + |
| 32 | +!------------------------------------------------------------------------------ |
| 33 | + |
| 34 | + subroutine haversine_distmat(xa, xb, dist) |
| 35 | +!------------------------------------------------------------------------------ |
| 36 | + implicit none |
| 37 | + real(kind=8), dimension(:,:), intent(in) :: xa, xb |
| 38 | + real(kind=8), allocatable, dimension(:) :: xa1, xa2, xb1, xb2 |
| 39 | + integer(kind=8) :: i, j, m, n |
| 40 | + real(kind=8) :: thedist, d2r |
| 41 | + real(kind=8), parameter :: erad = 6371315.0 |
| 42 | + real(kind=8), dimension(:,:), intent(inout) :: dist |
| 43 | + external :: haversine |
| 44 | + |
| 45 | + d2r = atan2(0.,-1.)/ 180. ! atan2(0.,-1.) == pi |
| 46 | + m = size(xa, 1) |
| 47 | + n = size(xb, 1) |
| 48 | + |
| 49 | + xa1 = xa(:,1) |
| 50 | + xa2 = xa(:,2) |
| 51 | + xb1 = xb(:,1) |
| 52 | + xb2 = xb(:,2) |
| 53 | + |
| 54 | +! Loop over empty dist matrix and fill |
| 55 | + do j = 1, m |
| 56 | + do i = 1, n |
| 57 | + call haversine(xa1(j), xa2(j), xb1(i), xb2(i), d2r, thedist) |
| 58 | + dist(j,i) = thedist |
| 59 | + enddo |
| 60 | + enddo |
| 61 | + dist = dist * erad |
| 62 | + end subroutine haversine_distmat |
| 63 | + |
| 64 | + |
| 65 | +!------------------------------------------------------------------------------ |
| 66 | + subroutine haversine_distvec(lon1, lat1, lon2, lat2, dist) |
| 67 | +!------------------------------------------------------------------------------ |
| 68 | + |
| 69 | + implicit none |
| 70 | + real(kind=8), dimension(:), intent(in) :: lon1, lat1, lon2, lat2 |
| 71 | + integer(kind=8) :: i, m |
| 72 | + real(kind=8) :: thedist, d2r |
| 73 | + real(kind=8), parameter :: erad = 6371315.0 |
| 74 | + real(kind=8), dimension(:), intent(inout) :: dist |
| 75 | + external :: haversine |
| 76 | + |
| 77 | + d2r = atan2(0.,-1.)/ 180. ! atan2(0.,-1.) == pi |
| 78 | + m = size(lon1) |
| 79 | + |
| 80 | +! Loop over empty dist matrix and fill |
| 81 | + do i = 1, m |
| 82 | + call haversine(lon1(i), lat1(i), lon2(i), lat2(i), d2r, thedist) |
| 83 | + dist(i) = thedist |
| 84 | + enddo |
| 85 | + dist = dist * erad |
| 86 | + |
| 87 | + end subroutine haversine_distvec |
| 88 | + |
| 89 | + |
| 90 | +!------------------------------------------------------------------------------ |
| 91 | + subroutine haversine_dist(lon1, lat1, lon2, lat2, thedist) |
| 92 | +!------------------------------------------------------------------------------ |
| 93 | + |
| 94 | + implicit none |
| 95 | + real(kind=8), intent(in) :: lon1, lat1, lon2, lat2 |
| 96 | + real(kind=8) :: d2r |
| 97 | + real(kind=8), parameter :: erad = 6371315.0 |
| 98 | + real(kind=8), intent(out) :: thedist |
| 99 | + external :: haversine |
| 100 | + |
| 101 | + d2r = atan2(0.,-1.)/ 180. ! atan2(0.,-1.) == pi |
| 102 | + call haversine(lon1, lat1, lon2, lat2, d2r, thedist) |
| 103 | + thedist = thedist * erad |
| 104 | + |
| 105 | + end subroutine haversine_dist |
| 106 | + |
| 107 | + |
| 108 | +!------------------------------------------------------------------------------ |
| 109 | + subroutine haversine(lon1, lat1, lon2, lat2, d2r, thedist) |
| 110 | +!------------------------------------------------------------------------------ |
| 111 | +! |
| 112 | + implicit none |
| 113 | + real(kind=8), intent(in) :: lon1, lat1, lon2, lat2, d2r |
| 114 | + real(kind=8) :: lt1, lt2, dlat, dlon |
| 115 | + real(kind=8) :: a |
| 116 | + real(kind=8), intent(out) :: thedist |
| 117 | +! |
| 118 | + dlat = d2r * (lat2 - lat1) |
| 119 | + dlon = d2r * (lon2 - lon1) |
| 120 | + lt1 = d2r * lat1 |
| 121 | + lt2 = d2r * lat2 |
| 122 | +! |
| 123 | + a = sin(0.5 * dlon) * sin(0.5 * dlon) |
| 124 | + a = a * cos(lt1) * cos(lt2) |
| 125 | + a = a + (sin(0.5 * dlat) * sin(0.5 * dlat)) |
| 126 | + thedist = 2 * atan2(sqrt(a), sqrt(1 - a)) |
| 127 | +! |
| 128 | + end subroutine haversine |
| 129 | + |
| 130 | + |
| 131 | +!------------------------------------------------------------------------------ |
| 132 | + subroutine waypoint_vec(lonin, latin, anglein, distin, lon, lat) |
| 133 | +!------------------------------------------------------------------------------ |
| 134 | + |
| 135 | + implicit none |
| 136 | + real(kind=8), dimension(:), intent(in) :: lonin, latin, anglein, distin |
| 137 | + integer(kind=8) :: i, m |
| 138 | + real(kind=8) :: thelon, thelat, d2r |
| 139 | + real(kind=8), dimension(:), intent(inout) :: lon, lat |
| 140 | + external :: waypoint |
| 141 | + |
| 142 | + d2r = atan2(0.,-1.)/ 180. ! atan2(0.,-1.) == pi |
| 143 | + m = size(lonin) |
| 144 | + |
| 145 | +! Loop over empty dist matrix and fill |
| 146 | + do i = 1, m |
| 147 | + call waypoint(lonin(i), latin(i), anglein(i), distin(i), d2r, thelon, thelat) |
| 148 | + lon(i) = thelon |
| 149 | + lat(i) = thelat |
| 150 | + enddo |
| 151 | + |
| 152 | + end subroutine waypoint_vec |
| 153 | + |
| 154 | + |
| 155 | +!------------------------------------------------------------------------------ |
| 156 | + subroutine waypoint(lonin, latin, anglein, distin, d2r, thelon, thelat) |
| 157 | +!------------------------------------------------------------------------------ |
| 158 | +! |
| 159 | + implicit none |
| 160 | + real(kind=8), intent(in) :: lonin, latin, anglein, distin, d2r |
| 161 | + real(kind=8) :: ln1, lt1, angle, d_r |
| 162 | + real(kind=8), parameter :: erad = 6371315.0 |
| 163 | + real(kind=8), intent(out) :: thelon, thelat |
| 164 | +! |
| 165 | + d_r = distin / erad ! angular distance |
| 166 | + thelon = d2r * lonin |
| 167 | + thelat = d2r * latin |
| 168 | + angle = d2r * anglein |
| 169 | + |
| 170 | + lt1 = asin(sin(thelat) * cos(d_r) + cos(thelat) * sin(d_r) * cos(angle)) |
| 171 | + ln1 = atan2(sin(angle) * sin(d_r) * cos(thelat), cos(d_r) - sin(thelat) * sin(lt1)) |
| 172 | + ln1 = ln1 + thelon |
| 173 | + thelat = lt1 / d2r |
| 174 | + thelon = ln1 / d2r |
| 175 | +! |
| 176 | + end subroutine waypoint |
| 177 | + |
| 178 | + |
| 179 | +! |
| 180 | +! !------------------------------------------------------------------------------ |
| 181 | +! subroutine concat_arrays(a, b) |
| 182 | +! !------------------------------------------------------------------------------ |
| 183 | +! implicit none |
| 184 | +! real(kind=8), dimension(:) :: a |
| 185 | +! real(kind=8), dimension(:) :: b |
| 186 | +! real(kind=8), dimension(:), allocatable :: c |
| 187 | +! ! |
| 188 | +! allocate(c(size(a)+size(b))) |
| 189 | +! c(1:size(a)) = a |
| 190 | +! c(size(a)+1:size(a)+size(b)) = b |
| 191 | +! |
| 192 | +! end subroutine concat_arrays |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
0 commit comments