# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # This notebook runs a covariance based Monte Carlo propagation in ADAM

from adam import PropagationParams
from adam import OpmParams
from adam import ConfigManager
from adam import ProjectsClient
from adam import AdamProcessingService
from adam import MonteCarloResults
from adam import OrbitEventType
import matplotlib.pyplot as plt

# This sets up authenticated access to the server. It needs to be done before pretty much everything you want to do with ADAM.

# ConfigManager loads the config set up via adamctl.
# See the README at https://github.com/B612-Asteroid-Institute/adam_home/blob/master/README.md
config = ConfigManager().get_config('dev')
project = ProjectsClient().get_project_from_config(config)
aps = AdamProcessingService()

# ## Example Inputs

# The PropagationParameters sent to the propagation API will operate on either Keplerian or Cartesian elements.

# +
# from https://newton.spacedys.com/neodys/index.php?pc=1.1.1.1&n=2000SG344
# true_anomaly_deg is also available instead of mean_anomaly_deg
keplerian_elements = {
    'semi_major_axis_km': 146222900,
    'eccentricity': 0.066958,
    'inclination_deg': 0.112,
    'ra_of_asc_node_deg' : 191.912,
    'arg_of_pericenter_deg' :  275.347,
    'mean_anomaly_deg' : 35.681,
    'gm' : 132712440041.93938 #JPL Horizons GM
}

# Lower triangular covariance matrix (21 elements in a list)
covariance = [3.94346903514E+03, + \
             -1.40266786788E-04, 5.00812620000E-12, + \
             -2.91357694324E-04, 1.06017205000E-11, 3.15658331000E-11, + \
             -3.83826656095E-03, 1.40431472000E-10, 2.32155752000E-09, 8.81161492000E-07, + \
             -1.09220523817E-02, 3.62452521000E-10, -1.53067748000E-09, -8.70304198000E-07, 9.42413982000E-07, + \
             -2.96713683611E-01, 1.05830167000E-08, 2.23110293000E-08, 2.93564832000E-07, 7.81029359000E-07, 2.23721205000E-05]
# -

# ### Set Parameters
#
# Commented parameters are optional. Uncomment to use.

# +
propagation_params = PropagationParams({
    'start_time': '2017-10-04T00:00:00Z',   # propagation start time in ISO format
    'end_time': '2017-10-11T00:00:00Z',     # propagation end time in ISO format

    'project_uuid': config['workspace'],
    'description': 'Jupyter Keplerian Covariance Monte Carlo Example',
    'monteCarloDraws': 10,
    'propagationType': 'MONTE_CARLO',
    'stopOnImpact': True,
    'step_size': 86400,
    'stopOnCloseApproach': False,
    'stopOnImpactAltitudeMeters': 500000,
    'closeApproachRadiusFromTargetMeters': 7000000000
})

opm_params = OpmParams({
    'epoch': '2017-10-04T00:00:00Z',
    'keplerian_elements': keplerian_elements,
    'keplerian_covariance': covariance,   # object covariance
            
#     'mass': 500.5,              # object mass
#     'solar_rad_area': 25.2,     # object solar radiation area (m^2)
#     'solar_rad_coeff': 1.2,     # object solar radiation coefficient
#     'drag_area': 33.3,          # object drag area (m^2)
#     'drag_coeff': 2.5,          # object drag coefficient
    
})
# -

# ### Submit and Run Propagation

batch_run = aps.execute_batch_propagation(
    project, 
    propagation_params, 
    opm_params, 
    object_id="KeplerianCovarianceObject01",
    user_defined_id="KeplerianCovarianceWorkbook"
)

# ### Get Status

# See example notebook on how to search the ADAM system for previous submitted jobs
print(batch_run.check_status())
batch_run.wait_for_complete(max_wait_sec=500, print_waiting = True)
print()
print(batch_run.check_status())

# ### Get Summary Statistics

stats = batch_run.get_summary()
print(stats)
print(stats.get_misses())

# ### Get Ephemeris for specific run

eph = batch_run.get_ephemeris_as_dataframe(2)
print(eph)
eph.plot(x='Epoch', y=['X','Y','Z'])
eph.plot(x='Epoch', y=['Vx','Vy','Vz'])

ephem_raw_data = batch_run.get_ephemeris_content(2)
print(ephem_raw_data)

# ### Get ending state vector

# +
close_approach_states = batch_run.get_states_dataframe(OrbitEventType.CLOSE_APPROACH)
print("\nClose State Vectors")
if not close_approach_states.empty:
    print(f'First close approach state:\n{close_approach_states.loc[0]}')
else:
    print("None")


impact_states = batch_run.get_states_dataframe(OrbitEventType.IMPACT)
print("\nImpact State Vectors")
if not impact_states.empty:
    print(f'First impact end state:\n{impact_states.loc[0]}')
else:
    print("None")


miss_states = batch_run.get_states_dataframe(OrbitEventType.MISS)
print("\nMiss State Vectors")
if not miss_states.empty:
    print(f'First miss end state:\n{miss_states.loc[0]}')
else:
    print("None")

# -



StarcoderdataJupyterScriptsDedupFiltered