Skip to content

Commit

Permalink
updating apex.py, helpers.py and __main__.py to be pep8 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
gregstarr committed Feb 4, 2021
1 parent f85fa24 commit 7eeba12
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/apexpy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ def main():
args.height, datetime=datetime)
np.savetxt(args.file_out, np.column_stack((lats, lons)), fmt='%.8f')


if __name__ == '__main__':
sys.exit(main())
46 changes: 15 additions & 31 deletions src/apexpy/apex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
# below try..catch required for autodoc to work on readthedocs
try:
from . import fortranapex as fa
except:
except ImportError as e:
print("ERROR: fortranapex module could not be imported, so apexpy probably"
" won't work. Make sure you have a gfortran compiler. Wheels "
"installation assumes your compiler lives in /opt/local/bin")
raise e

import ctypes

Expand Down Expand Up @@ -76,15 +77,15 @@ def __init__(self, date=None, refh=0, datafile=None, fortranlib=None):
fortranlib = fa.__file__

self.RE = 6371.009 # mean Earth radius
self.set_refh(refh) # reference height
self.set_refh(refh) # reference height

if date is None:
self.year = helpers.toYearFraction(dt.datetime.now())
else:
try:
# convert date/datetime object to decimal year
self.year = helpers.toYearFraction(date)
except:
except AttributeError:
# failed so date is probably int/float, use directly
self.year = date

Expand Down Expand Up @@ -124,7 +125,6 @@ def __init__(self, date=None, refh=0, datafile=None, fortranlib=None):
self._qd2apex = np.frompyfunc(self._qd2apex_nonvectorized, 3, 2)
self._get_babs = np.frompyfunc(self._get_babs_nonvectorized, 3, 1)


def convert(self, lat, lon, source, dest, height=0, datetime=None,
precision=1e-10, ssheight=50*6371):
"""Converts between geodetic, modified apex, quasi-dipole and MLT.
Expand Down Expand Up @@ -431,7 +431,7 @@ def _qd2apex_nonvectorized(self, qlat, qlon, height):
qlat = helpers.checklat(qlat, name='qlat')

alon = qlon
hA = self.get_apex(qlat, height) # apex height
hA = self.get_apex(qlat, height) # apex height

if hA < self.refh:
if np.isclose(hA, self.refh, rtol=0, atol=1e-5):
Expand Down Expand Up @@ -617,8 +617,8 @@ def _map_EV_to_height(self, alat, alon, height, newheight, X, EV):
raise ValueError(EV + ' must be (3, N) or (3,) ndarray')
X = np.reshape(X, (3, np.size(X)//3))

_, _, _, _, _, _, d1, d2, _, e1, e2, _ = self.basevectors_apex(alat, \
alon, height, coords='apex')
_, _, _, _, _, _, d1, d2, _, e1, e2, _ = self.basevectors_apex(alat,
alon, height, coords='apex')

if EV == 'E':
v1 = e1
Expand All @@ -634,8 +634,8 @@ def _map_EV_to_height(self, alat, alon, height, newheight, X, EV):
X1 = np.sum(X*v1, axis=0) # E dot e1 or V dot d1
X2 = np.sum(X*v2, axis=0) # E dot e2 or V dot d2

_, _, _, _, _, _, d1, d2, _, e1, e2, _ = self.basevectors_apex(alat, \
alon, newheight, coords='apex')
_, _, _, _, _, _, d1, d2, _, e1, e2, _ = self.basevectors_apex(alat,
alon, newheight, coords='apex')

if EV == 'E':
v1 = d1
Expand Down Expand Up @@ -678,7 +678,6 @@ def map_E_to_height(self, alat, alon, height, newheight, E):
components)
"""

return self._map_EV_to_height(alat, alon, height, newheight, E, 'E')

def map_V_to_height(self, alat, alon, height, newheight, V):
Expand Down Expand Up @@ -787,11 +786,6 @@ def basevectors_apex(self, lat, lon, height, coords='geo', precision=1e-10):
Altitude in km
coords : {'geo', 'apex', 'qd'}, optional
Input coordinate system
return_all : bool, optional
Will also return f3, g1, g2, and g3, and f1 and f2 have 3 components
(the last component is zero). Requires `lat`, `lon`, and `height`
to be broadcast to 1D (at least one of the parameters must be 1D
and the other two parameters must be 1D or 0D).
precision : float, optional
Precision of output (degrees) when converting to geo. A negative
value of this argument produces a low-precision calculation of
Expand Down Expand Up @@ -872,11 +866,9 @@ def basevectors_apex(self, lat, lon, height, coords='geo', precision=1e-10):
F = np.cross(F1.T, F2.T).T[-1]
cosI = helpers.getcosIm(alat)
k = np.array([0, 0, 1], dtype=np.float64).reshape((3, 1))
g1 = ((self.RE + np.float64(height)) / (self.RE + self.refh))**(3/2) \
* d1 / F
g2 = -1.0 / (2.0 * F * np.tan(np.radians(qlat))) * \
(k + ((self.RE + np.float64(height)) / (self.RE + self.refh))
* d2 / cosI)
g1 = ((self.RE + np.float64(height)) / (self.RE + self.refh))**(3/2) * d1 / F
g2 = -1.0 / (2.0 * F * np.tan(np.radians(qlat))) * (k + ((self.RE + np.float64(height)) /
(self.RE + self.refh)) * d2 / cosI)
g3 = k*F
f3 = np.cross(g1.T, g2.T).T

Expand Down Expand Up @@ -932,13 +924,9 @@ def set_epoch(self, year):
Decimal year
"""

# f2py
fa.loadapxsh(self.datafile, np.float(year))
# ctypes
date = ctypes.c_float(year)
fa.cofrm(year)

self.year = year

def set_refh(self, refh):
Expand All @@ -955,7 +943,6 @@ def set_refh(self, refh):
and is only relevant for conversions involving apex (not quasi-dipole).
"""

self.refh = refh

def _get_babs_nonvectorized(self, glat, glon, height):
Expand Down Expand Up @@ -1043,17 +1030,14 @@ def bvectors_apex(self, lat, lon, height, coords='geo', precision=1e-10):
J. Geophys. Res., 115(A8), A08322, :doi:`10.1029/2010JA015326`.
"""


glat, glon = self.convert(lat, lon, coords, 'geo', height=height,
precision=precision)

babs = self.get_babs(glat, glon, height)

_, _, _, _, _, _, d1, d2, d3, _, _, e3 = self.basevectors_apex(glat, \
glon, height, coords='geo')
d1_cross_d2 = np.cross(d1.T,d2.T).T
D = np.sqrt(np.sum(d1_cross_d2**2,axis=0))
_, _, _, _, _, _, d1, d2, d3, _, _, e3 = self.basevectors_apex(glat, glon, height, coords='geo')
d1_cross_d2 = np.cross(d1.T, d2.T).T
D = np.sqrt(np.sum(d1_cross_d2**2, axis=0))

Be3 = babs / D
Bd3 = babs * D
Expand Down
1 change: 1 addition & 0 deletions src/apexpy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import datetime as dt
import numpy as np


def checklat(lat, name='lat'):
"""Makes sure the latitude is inside [-90, 90], clipping close values
(tolerance 1e-4).
Expand Down

0 comments on commit 7eeba12

Please sign in to comment.