NASA-Planetary-Science / sbpy

A Python package for small bodies research
https://sbpy.org/
Other
66 stars 34 forks source link

Manually add orbits to Orbit() #348

Open jrob93 opened 2 years ago

jrob93 commented 2 years ago

This is a request for

The requested changes will be implemented by

High-level concept Add the ability to initialise an Orbit() object from a table/dataframe containing orbital elements.

Explain the relevance to sbpy sbpy Orbit() documentation only references from_mpc and from_horizons as the way to set up an orbit (unless I've missed something).

Proposal details It is possible to create an empty Orbit() instance and fill the fields manually, this would probably just need a wrapper function. This functionality would be handy for dealing with simulated orbits, e.g. the LINCC Simulated Solar System Products Data Base

Example (pseudo-)code e.g. from dataframe

df_orb = ... # pandas dataframe with orbital element fields ["a","e","incl","Omega","w","M"]

elem = Orbit() # empty orbit instance

Fill fields (remember astropy units):

orb_col = list(df_orb)]
orb_unit = [u.au,u.dimensionless_unscaled,u.deg,u.deg,u.deg,u.deg]
for i in range(len(orb_col)):
    elem[orb_col[i]] = df_orb[orb_col[i]] * orb_unit[i]

Add extra required fields:

elem["epoch"] = Time(...)
elem["targetname"] = ...
elem["H"] = ... * u.mag
elem["G"] = ... * u.dimensionless_unscaled
mkelley commented 2 years ago

Thanks, @jrob93 . I actually have an active pull request that aims to improve the documentation and this could be a good example. Or maybe for our tutorial repository. (We definitely need this in the SSSC technical documentation.)

I think the main problem with the pandas dataframe is that it lacks (astropy) units, right? But we can go through an astropy QTable. How about something like this:

from astropy.table import QTable
tab = QTable.from_pandas(df_orb, units={'a': u.au, 'incl': u.deg, 'Omega': u.deg, 'w': u.deg, 'M': u.deg})
orbits = Orbit.from_table(tab)

QTable.from_pandas can handle time objects, too, but I don't understand dataframes well enough to experiment with it right now: https://docs.astropy.org/en/stable/api/astropy.table.Table.html#astropy.table.Table.from_pandas

jrob93 commented 2 years ago

Ah cool, that's much better using QTable. I've never really used tables before because I was more familiar with pandas when I started learning astropy type things, but yes the units are easier with tables. I'm nearly finished the notebook you requested for the SSSC technical docs so I'll use those lines in it.