python-astrodynamics / spacetrack

Python client for space-track.org
MIT License
74 stars 15 forks source link

help downloading multiple years STE to minimize api calls to ste #66

Closed probinso closed 1 year ago

probinso commented 1 year ago

I am looking to download historical data for multiple years and multiple satellites. The spacetrack api states that you should not exceed one call per hour-,TLE,-1%C2%A0/%C2%A0hour) for tle. Because of this i'm trying to request st.tle data in chunks of 400 days.

st = SpaceTrackClient(
    identity=creds.username, password=creds.password
)
...

def get_satellite_orb(
    st: SpaceTrackClient,
    norad_id: int,
    date: datetime,
    *,
    buffer: timedelta(days=400),
    name: str = 'satellite',
) -> ephem.EarthSatellite:
    ir_date_1 = date.date()
    ir_date_2 = (date + buffer).date()

    decay_epoch = op.inclusive_range(ir_date_1, ir_date_2)

    tles = st.tle(
        norad_cat_id=norad_id,
        orderby='epoch desc',
        limit=3,
        format='tle',
        epoch=decay_epoch,
    )
    if not tles:
        raise requests.exceptions.HTTPError(
            "I guess SpaceTrack doesn't throw errors and instead returns none in some cases?"
        )

    tle = tles.split('\n')
    line1, line2 = tle[0], tle[1]
    orb = ephem.readtle(name, line1, line2)
    return orb

however after the call, when using the tle data I am receiving this error.

def get_positions_df(orb: ephem.EarthSatellite, date: datetime) -> pd.DataFrame:
    thedatetimes = (date + timedelta(0, i) for i in range(24 * 60 * 60))
    lats = []
    lons = []
    times = []
    altitudes = []

    for t in thedatetimes:
        orb.compute(t.strftime('%Y/%m/%d %H:%M:%S'))
        lon = ephem.degrees(orb.sublong) * 180 / pi
        lat = ephem.degrees(orb.sublat) * 180 / pi
        altitude = orb.elevation
        times.append(t)
        lats.append(lat)
        lons.append(lon)
        altitudes.append(altitude)

    df = pd.DataFrame(
        list(zip(times, lons, lats, altitudes)),
        columns=['time', 'lon', 'lat', 'altitude'],
    )
    return df
ValueError: TLE elements are valid for a few weeks around their epoch, but you are asking about a date 396 days from the epoch

Firstly, I couldn't find in the documentation what only valid for a few weeks comes out to, so I don't know how long my largest tle op.includive_range should be. Secondly, I'm looking to set this up for multiple satellites that may not have overlapping operating dates, so I seem to need to request st.tle( separately per satelite and so will have to make many requests. Do you have any guidance on using this api for downloading entire multiple years of st.tle data of this form?

RazerM commented 1 year ago

The TLEs you get back from Space-Track.org are the same regardless of how many you query at once.

Your query doesn't make much sense, as you are querying within a 400 day window and returning the 3 nearest the end of that window, then you only read the first (i.e. latest) TLE.

The actual error you're getting appears to be from the pyephem library. It's refusing to propagate a TLE past a given point. For example, the TLE you've used might have an epoch of 2020-01-01 but you're trying to propagate it to 2022-01-01 or something, and pyephem refuses to do so. You should instead be finding the closest TLE to the epoch you wish to propagate to. This is off topic as far as spacetrack is concerned, so I'm closing this.