source: sasmodels/sasmodels/guyou.py @ 802c412

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 802c412 was d86f0fc, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

lint reduction

  • Property mode set to 100644
File size: 11.7 KB
Line 
1# Copyright 2013-2016 Mike Bostock
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without modification,
5# are permitted provided that the following conditions are met:
6#
7# * Redistributions of source code must retain the above copyright notice, this
8#   list of conditions and the following disclaimer.
9#
10# * Redistributions in binary form must reproduce the above copyright notice,
11#   this list of conditions and the following disclaimer in the documentation
12#   and/or other materials provided with the distribution.
13#
14# * Neither the name of the author nor the names of contributors may be used to
15#   endorse or promote products derived from this software without specific prior
16#   written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# https://github.com/d3/d3-geo-projection
30# commit fd2886555e46b35163c7b898d43c7d1bcebbba7c 2016-07-02
31#
32# 2017-11-01 Paul Kienzle
33# * converted to python, with degrees rather than radians
34"""
35Convert between latitude-longitude and Guyou map coordinates.
36"""
37
38from __future__ import division, print_function
39
40import numpy as np
41from numpy import sqrt, pi, tan, cos, sin, sign, radians, degrees
42from numpy import sinh, arctan as atan
43
44# scipy version of special functions
45from scipy.special import ellipj as ellipticJ, ellipkinc as ellipticF
46
47_ = """
48# mpmath version of special functions
49import mpmath as mp
50sn, cn, dn = [mp.ellipfun(v) for v in 'sn', 'cn', 'dn']
51
52def ellipticJi(u, v, m):
53    z = u+v*1j
54    return sn(z, m), cn(z, m), dn(z, m)
55
56def ellipticJ(u, m):
57    s, c, d = sn(u, m), cn(u, m), dn(u, m)
58    phi = mp.asin(s)
59    return s, c, d, phi
60
61def ellipticFi(phi, psi, m):
62    z = phi + psi*1j
63    return mp.ellipf(z, m)
64
65def ellipticF(phi, m):
66    return mp.ellipf(phi, m)
67"""
68
69def ellipticJi(u, v, m):
70    scalar = np.isscalar(u) and np.isscalar(v) and np.isscalar(m)
71    u, v, m = np.broadcast_arrays(u, v, m)
72    result = np.empty_like([u, u, u], 'D')
73    real = (v == 0)
74    imag = (u == 0)
75    mixed = ~(real|imag)
76    result[:, real] = _ellipticJi_real(u[real], m[real])
77    result[:, imag] = _ellipticJi_imag(v[imag], m[imag])
78    result[:, mixed] = _ellipticJi(u[mixed], v[mixed], m[mixed])
79    return result[0, :] if scalar else result
80
81def _ellipticJi_real(u, m):
82    sn, cn, dn, phi = ellipticJ(u, m)
83    return sn, cn, dn
84
85def _ellipticJi_imag(v, m):
86    sn, cn, dn, phi = ellipticJ(v, 1-m)
87    return 1j*sn/cn, 1/cn, dn/cn
88
89def _ellipticJi(u, v, m):
90    # Ignoring special cases for now.
91    #   u=0: (1j*b[0]/b[1], 1/b[1], b[2]/b[1])
92    #   v=0: (a[0], a[1], a[2])
93    a = ellipticJ(u, m)
94    b = ellipticJ(v, 1 - m)
95    c = b[1]**2 + m * (a[0] * b[0])**2
96    return [
97        (a[0] * b[2] / c) + 1j*(a[1] * a[2] * b[0] * b[1] / c),
98        (a[1] * b[1] / c) + 1j*(-a[0] * a[2] * b[0] * b[2] / c),
99        (a[2] * b[1] * b[2] / c) + 1j*(-m * a[0] * a[1] * b[0] / c),
100    ]
101
102# calculate F(phi+ipsi|m).
103# see Abramowitz and Stegun, 17.4.11.
104def ellipticFi(phi, psi, m):
105    if np.any(phi == 0):
106        scalar = np.isscalar(phi) and np.isscalar(psi) and np.isscalar(m)
107        phi, psi, m = np.broadcast_arrays(phi, psi, m)
108        result = np.empty_like(phi, 'D')
109        index = (phi == 0)
110        result[index] = ellipticF(atan(sinh(abs(phi[index]))),
111                                  1-m[index]) * sign(psi[index])
112        result[~index] = ellipticFi(phi[~index], psi[~index], m[~index])
113        return result.reshape(1)[0] if scalar else result
114
115    r = abs(phi)
116    i = abs(psi)
117    sinhpsi2 = sinh(i)**2
118    cscphi2 = 1 / sin(r)**2
119    cotphi2 = 1 / tan(r)**2
120    b = -(cotphi2 + m * (sinhpsi2 * cscphi2) - 1 + m)
121    c = (m - 1) * cotphi2
122    cotlambda2 = (-b + sqrt(b * b - 4 * c)) / 2
123    re = ellipticF(atan(1 / sqrt(cotlambda2)), m) * sign(phi)
124    im = ellipticF(atan(sqrt(np.maximum(0, (cotlambda2 / cotphi2 - 1) / m))),
125                   1 - m) * sign(psi)
126    return re + 1j*im
127
128SQRT2 = sqrt(2)
129
130# [PAK] renamed k_ => cos_u, k => sin_u, k*k => sinsq_u to avoid k,K confusion
131# cos_u = 0.171572875253809902396622551580603842860656249246103853646...
132# sinsq_u = 0.970562748477140585620264690516376942836062504523376878120...
133# K = 3.165103454447431823666270142140819753058976299237578486994...
134def guyou(lam, phi):
135    """Transform from (latitude, longitude) to point (x, y)"""
136    # [PAK] wrap into [-pi/2, pi/2] radians
137    x, y = np.asarray(lam), np.asarray(phi)
138    xn, x = divmod(x+90, 180)
139    yn, y = divmod(y+90, 180)
140    xn, lam = xn*180, radians(x-90)
141    yn, phi = yn*180, radians(y-90)
142
143    # Compute constant K
144    cos_u = (SQRT2 - 1) / (SQRT2 + 1)
145    sinsq_u = 1 - cos_u**2
146    K = ellipticF(pi/2, sinsq_u)
147
148    # [PAK] simplify expressions, using the fact that f = -1
149    # Note: exp(f log(x)) => 1/x,  cos(f x) => cos(x), sin(f x) => -sin(x)
150    r = 1/(tan(pi/4 + abs(phi)/2) * sqrt(cos_u))
151    at = atan(r * (cos(lam) - 1j*sin(lam)))
152    t = ellipticFi(at.real, at.imag, sinsq_u)
153    x, y = (-t.imag, sign(phi + (phi == 0))*(0.5 * K - t.real))
154
155    # [PAK] convert to degrees, and return to original tile
156    return degrees(x)+xn, degrees(y)+yn
157
158def guyou_invert(x, y):
159    """Transform from point (x, y) on plot to (latitude, longitude)"""
160    # [PAK] wrap into [-pi/2, pi/2] radians
161    x, y = np.asarray(x), np.asarray(y)
162    xn, x = divmod(x+90, 180)
163    yn, y = divmod(y+90, 180)
164    xn, x = xn*180, radians(x-90)
165    yn, y = yn*180, radians(y-90)
166
167    # compute constant K
168    cos_u = (SQRT2 - 1) / (SQRT2 + 1)
169    sinsq_u = 1 - cos_u**2
170    K = ellipticF(pi/2, sinsq_u)
171
172    # [PAK] simplify expressions, using the fact that f = -1
173    j = ellipticJi(K/2 - y, -x, sinsq_u)
174    tn = j[0]/j[1]  # j[0], j[1] are complex
175    # Note: -atan2(im(x)/re(x)) => angle(x)
176    lam = -np.angle(tn)
177    # Note: exp(0.5/f log(a re(x)^2 + a im(x)^2)) => 1/(sqrt(a) |x|)
178    phi = 2*atan(1/sqrt(cos_u)/abs(tn)) - pi/2
179
180    # [PAK] convert to degrees, and return to original tile
181    return degrees(lam)+xn, degrees(phi)+yn
182
183def plot_grid():
184    """Plot the latitude-longitude grid for Guyou transform"""
185    import matplotlib.pyplot as plt
186    from numpy import linspace
187    lat_line = linspace(-90, 90, 400)
188    long_line = linspace(-90, 90, 400)
189
190    #scale = 1
191    limit, step, scale = 90, 10, 2
192    plt.subplot(211)
193    for lat in range(-limit, limit+1, step):
194        x, y = guyou(scale*lat_line, scale*lat)
195        plt.plot(x, y, 'g')
196
197    for long in range(-limit, limit+1, step):
198        x, y = guyou(scale*long, scale*long_line)
199        plt.plot(x, y, 'b')
200    #plt.xlabel('longitude')
201    plt.ylabel('latitude')
202
203    plt.subplot(212)
204    for lat in range(-limit, limit+1, step):
205        x, y = guyou_invert(scale*lat_line, scale*lat)
206        plt.plot(x, y, 'g')
207
208    for long in range(-limit, limit+1, step):
209        x, y = guyou_invert(scale*long, scale*long_line)
210        plt.plot(x, y, 'b')
211    plt.xlabel('longitude')
212    plt.ylabel('latitude')
213
214def main():
215    plot_grid()
216    import matplotlib.pyplot as plt
217    plt.show()
218
219if __name__ == "__main__":
220    main()
221
222_ = """
223// Javascript source for elliptic functions
224//
225// Returns [sn, cn, dn](u + iv|m).
226export function ellipticJi(u, v, m) {
227  var a, b, c;
228  if (!u) {
229    b = ellipticJ(v, 1 - m);
230    return [
231      [0, b[0] / b[1]],
232      [1 / b[1], 0],
233      [b[2] / b[1], 0]
234    ];
235  }
236  a = ellipticJ(u, m);
237  if (!v) return [[a[0], 0], [a[1], 0], [a[2], 0]];
238  b = ellipticJ(v, 1 - m);
239  c = b[1] * b[1] + m * a[0] * a[0] * b[0] * b[0];
240  return [
241    [a[0] * b[2] / c, a[1] * a[2] * b[0] * b[1] / c],
242    [a[1] * b[1] / c, -a[0] * a[2] * b[0] * b[2] / c],
243    [a[2] * b[1] * b[2] / c, -m * a[0] * a[1] * b[0] / c]
244  ];
245}
246
247// Returns [sn, cn, dn, ph](u|m).
248export function ellipticJ(u, m) {
249  var ai, b, phi, t, twon;
250  if (m < epsilon) {
251    t = sin(u);
252    b = cos(u);
253    ai = m * (u - t * b) / 4;
254    return [
255      t - ai * b,
256      b + ai * t,
257      1 - m * t * t / 2,
258      u - ai
259    ];
260  }
261  if (m >= 1 - epsilon) {
262    ai = (1 - m) / 4;
263    b = cosh(u);
264    t = tanh(u);
265    phi = 1 / b;
266    twon = b * sinh(u);
267    return [
268      t + ai * (twon - u) / (b * b),
269      phi - ai * t * phi * (twon - u),
270      phi + ai * t * phi * (twon + u),
271      2 * atan(exp(u)) - halfPi + ai * (twon - u) / b
272    ];
273  }
274
275  var a = [1, 0, 0, 0, 0, 0, 0, 0, 0],
276      c = [sqrt(m), 0, 0, 0, 0, 0, 0, 0, 0],
277      i = 0;
278  b = sqrt(1 - m);
279  twon = 1;
280
281  while (abs(c[i] / a[i]) > epsilon && i < 8) {
282    ai = a[i++];
283    c[i] = (ai - b) / 2;
284    a[i] = (ai + b) / 2;
285    b = sqrt(ai * b);
286    twon *= 2;
287  }
288
289  phi = twon * a[i] * u;
290  do {
291    t = c[i] * sin(b = phi) / a[i];
292    phi = (asin(t) + phi) / 2;
293  } while (--i);
294
295  // [PAK] Cephes uses dn = sqrt(1 - m*sin^2 phi) rather than cos(phi)/cos(phi-b)
296  // DLMF says the second version is unstable near x = K.
297  return [sin(phi), t = cos(phi), t / cos(phi - b), phi];
298}
299
300// calculate F(phi+ipsi|m).
301// see Abramowitz and Stegun, 17.4.11.
302export function ellipticFi(phi, psi, m) {
303  var r = abs(phi),
304      i = abs(psi),
305      sinhpsi = sinh(i);
306  if (r) {
307    var cscphi = 1 / sin(r),
308        cotphi2 = 1 / (tan(r) * tan(r)),
309        b = -(cotphi2 + m * (sinhpsi * sinhpsi * cscphi * cscphi) - 1 + m),
310        c = (m - 1) * cotphi2,
311        cotlambda2 = (-b + sqrt(b * b - 4 * c)) / 2;
312    return [
313      ellipticF(atan(1 / sqrt(cotlambda2)), m) * sign(phi),
314      ellipticF(atan(sqrt((cotlambda2 / cotphi2 - 1) / m)), 1 - m) * sign(psi)
315    ];
316  }
317  return [
318    0,
319    ellipticF(atan(sinhpsi), 1 - m) * sign(psi)
320  ];
321}
322
323// Calculate F(phi|m) where m = k^2 = sin(alpha)^2.
324// See Abramowitz and Stegun, 17.6.7.
325export function ellipticF(phi, m) {
326  if (!m) return phi;
327  if (m === 1) return log(tan(phi / 2 + quarterPi));
328  var a = 1,
329      b = sqrt(1 - m),
330      c = sqrt(m);
331  for (var i = 0; abs(c) > epsilon; i++) {
332    if (phi % pi) {
333      var dPhi = atan(b * tan(phi) / a);
334      if (dPhi < 0) dPhi += pi;
335      phi += dPhi + ~~(phi / pi) * pi;
336    } else phi += phi;
337    c = (a + b) / 2;
338    b = sqrt(a * b);
339    c = ((a = c) - b) / 2;
340  }
341  return phi / (pow(2, i) * a);
342
343
344export function guyouRaw(lambda, phi) {
345  var k_ = (sqrt2 - 1) / (sqrt2 + 1),
346      k = sqrt(1 - k_ * k_),
347      K = ellipticF(halfPi, k * k),
348      f = -1,
349      psi = log(tan(pi / 4 + abs(phi) / 2)),
350      r = exp(f * psi) / sqrt(k_),
351      at = guyouComplexAtan(r * cos(f * lambda), r * sin(f * lambda)),
352      t = ellipticFi(at[0], at[1], k * k);
353  return [-t[1], (phi >= 0 ? 1 : -1) * (0.5 * K - t[0])];
354}
355
356function guyouComplexAtan(x, y) {
357  var x2 = x * x,
358      y_1 = y + 1,
359      t = 1 - x2 - y * y;
360  return [
361   0.5 * ((x >= 0 ? halfPi : -halfPi) - atan2(t, 2 * x)),
362    -0.25 * log(t * t + 4 * x2) +0.5 * log(y_1 * y_1 + x2)
363  ];
364}
365
366function guyouComplexDivide(a, b) {
367  var denominator = b[0] * b[0] + b[1] * b[1];
368  return [
369    (a[0] * b[0] + a[1] * b[1]) / denominator,
370    (a[1] * b[0] - a[0] * b[1]) / denominator
371  ];
372}
373
374guyouRaw.invert = function(x, y) {
375  var k_ = (sqrt2 - 1) / (sqrt2 + 1),
376      k = sqrt(1 - k_ * k_),
377      K = ellipticF(halfPi, k * k),
378      f = -1,
379      j = ellipticJi(0.5 * K - y, -x, k * k),
380      tn = guyouComplexDivide(j[0], j[1]),
381      lambda = atan2(tn[1], tn[0]) / f;
382  return [
383    lambda,
384    2 * atan(exp(0.5 / f * log(k_ * tn[0] * tn[0] + k_ * tn[1] * tn[1]))) - halfPi
385  ];
386};
387"""
Note: See TracBrowser for help on using the repository browser.