Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: adding array broadcast to non scalar outputs #63

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/apexpy/apex.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ def convert(self, lat, lon, source, dest, height=0, datetime=None,
estr = 'Unknown coordinate transformation: '
estr += '{} -> {}'.format(source, dest)
raise NotImplementedError(estr)

return lat, lon
if np.isscalar(lat) and np.isscalar(lon):
return lat, lon
return np.broadcast_arrays(lat, lon)

def geo2apex(self, glat, glon, height):
"""Converts geodetic to modified apex coordinates.
Expand Down Expand Up @@ -252,7 +253,9 @@ def geo2apex(self, glat, glon, height):
alat[alat == -9999] = np.nan

# if array is returned, dtype is object, so convert to float
return np.float64(alat), np.float64(alon)
if np.isscalar(alat) and np.isscalar(alon):
return alat, alon
return np.broadcast_arrays(np.float64(alat), np.float64(alon))

def apex2geo(self, alat, alon, height, precision=1e-10):
"""Converts modified apex to geodetic coordinates.
Expand Down Expand Up @@ -290,8 +293,9 @@ def apex2geo(self, alat, alon, height, precision=1e-10):

qlat, qlon = self.apex2qd(alat, alon, height=height)
glat, glon, error = self.qd2geo(qlat, qlon, height, precision=precision)

return glat, glon, error
if np.isscalar(glat) and np.isscalar(glon):
return glat, glon, error
return np.broadcast_arrays(glat, glon, error)

def geo2qd(self, glat, glon, height):
"""Converts geodetic to quasi-dipole coordinates.
Expand All @@ -317,9 +321,10 @@ def geo2qd(self, glat, glon, height):
glat = helpers.checklat(glat, name='glat')

qlat, qlon = self._geo2qd(glat, glon, height)

if np.isscalar(qlat) and np.isscalar(qlon):
return qlat, qlon
# if array is returned, dtype is object, so convert to float
return np.float64(qlat), np.float64(qlon)
return np.broadcast_arrays(np.float64(qlat), np.float64(qlon))

def qd2geo(self, qlat, qlon, height, precision=1e-10):
"""Converts quasi-dipole to geodetic coordinates.
Expand Down Expand Up @@ -356,9 +361,10 @@ def qd2geo(self, qlat, qlon, height, precision=1e-10):
qlat = helpers.checklat(qlat, name='qlat')

glat, glon, error = self._qd2geo(qlat, qlon, height, precision)

if np.isscalar(glat) and np.isscalar(glon):
return glat, glon, error
# if array is returned, dtype is object, so convert to float
return np.float64(glat), np.float64(glon), np.float64(error)
return np.broadcast_arrays(np.float64(glat), np.float64(glon), np.float64(error))

def _apex2qd_nonvectorized(self, alat, alon, height):
"""Convert from apex to quasi-dipole (not-vectorised)
Expand Down Expand Up @@ -430,9 +436,10 @@ def apex2qd(self, alat, alon, height):
"""

qlat, qlon = self._apex2qd(alat, alon, height)

if np.isscalar(qlat) and np.isscalar(qlon):
return qlat, qlon
# if array is returned, the dtype is object, so convert to float
return np.float64(qlat), np.float64(qlon)
return np.broadcast_arrays(np.float64(qlat), np.float64(qlon))

def _qd2apex_nonvectorized(self, qlat, qlon, height):

Expand Down Expand Up @@ -483,9 +490,10 @@ def qd2apex(self, qlat, qlon, height):
"""

alat, alon = self._qd2apex(qlat, qlon, height)

if np.isscalar(alat) and np.isscalar(alon):
return alat, alon
# if array is returned, the dtype is object, so convert to float
return np.float64(alat), np.float64(alon)
return np.broadcast_arrays(np.float64(alat), np.float64(alon))

def mlon2mlt(self, mlon, datetime, ssheight=50 * 6371):
"""Computes the magnetic local time at the specified magnetic longitude
Expand Down