Skip to content

Commit

Permalink
introduce function sc_distance()
Browse files Browse the repository at this point in the history
  • Loading branch information
jgieseler committed Aug 15, 2023
1 parent 3cfebe2 commit a451852
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
53 changes: 53 additions & 0 deletions solarmach/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from matplotlib.legend_handler import HandlerPatch
from sunpy import log
from sunpy.coordinates import frames, get_horizons_coord
from sunpy.time import parse_time

from solarmach.pfss_utilities import calculate_pfss_solution, get_field_line_coords, get_gong_map, multicolorline, sphere, spheric2cartesian, vary_flines

Expand Down Expand Up @@ -1531,6 +1532,58 @@ def pfss_3d(self, active_area=(None, None, None, None), color_code='object'):
return


def sc_distance(sc1, sc2, dtime):
"""
Obtain absolute distance between two bodies in 3d for a given datetime.
Parameters
----------
sc1 : str
Name of body 1, e.g., planet or spacecraft
sc2 : str
Name of body 2, e.g., planet or spacecraft
dtime : datetime object or datetime-compatible str
Date (and time) of distance determination
Returns
-------
astropy units
Absolute distance between body 1 and 2 in AU.
"""
# parse datetime:
if type(dtime) == str:
try:
obstime = parse_time(dtime)
except ValueError:
print(f"Unable to extract datetime from '{dtime}'. Please try a different format.")
return np.nan*u.AU

# standardize body names (e.g. 'PSP' => 'Parker Solar Probe')
try:
sc1 = body_dict[sc1][1]
except KeyError:
pass
#
try:
sc2 = body_dict[sc2][1]
except KeyError:
pass

try:
sc1_coord = get_horizons_coord(sc1, obstime, None)
except ValueError:
print(f"Unable to obtain position for '{sc1}' at {dtime}. Please try a different name or date.")
return np.nan*u.AU
#
try:
sc2_coord = get_horizons_coord(sc2, obstime, None)
except ValueError:
print(f"Unable to obtain position for '{sc2}' at {dtime}. Please try a different name or date.")
return np.nan*u.AU

return sc1_coord.separation_3d(sc2_coord)


def _isstreamlit():
"""
Function to check whether python code is run within streamlit
Expand Down
12 changes: 11 additions & 1 deletion solarmach/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import pandas
import pfsspy
from solarmach import SolarMACH, print_body_list, get_gong_map, calculate_pfss_solution
from solarmach import SolarMACH, print_body_list, get_gong_map, calculate_pfss_solution, sc_distance


def test_print_body_list():
Expand Down Expand Up @@ -87,3 +87,13 @@ def test_solarmach_pfss():
numbered_markers=True, long_sector=[290, 328], long_sector_vsw=[400, 600],
long_sector_color='red', reference_vsw=400.0)
assert isinstance(fig, matplotlib.figure.Figure)


def test_sc_distance():
distance = sc_distance('SolO', 'PSP', "2020/12/12")
assert np.round(distance.value, 8) == 1.45237361
assert distance.unit == u.AU
#
distance = sc_distance('SolO', 'PSP', "2000/12/12")
assert np.isnan(distance.value)
assert distance.unit == u.AU

0 comments on commit a451852

Please sign in to comment.