skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.43k stars 213 forks source link

How does load.tle Reload function work? #169

Closed tmamedzadeh closed 6 years ago

tmamedzadeh commented 6 years ago

In the documentation it's stated, that the following script will force to download new elements even if the file is already on disk:

from skyfield.api import Topos, load

stations_url = 'http://celestrak.com/NORAD/elements/stations.txt'

satellites = load.tle(stations_url, reload=True)
satellite = satellites['ISS (ZARYA)']
print(satellite.epoch)

However, there is no input for the date. For instance, I'm analyzing a 1 year ago date, how could I achieve that TLE?

BTW, I got an error:

OSError: error renaming .\stations.txt.download to .\stations.txt - [WinError 183] Cannot create a file when that file already exists: '.\\stations.txt.download' -> '.\\stations.txt'
brandon-rhodes commented 6 years ago

Thank you, I didn't know about that WinError (I don't use Windows so I don't see those myself), so thanks for pasting in the error message so I can investigate it!

I'm not sure where Celestrak puts old TLE files. Clicking around its web site, have you found any archives where old orbital sets are preserved? If they exist, and if their URLs are predictable, then we could try adding a function to Skyfield to fetch old satellite elements. I think many users would be very interested in that!

tmamedzadeh commented 6 years ago

I didn't find it in celestrak. But it's in www.space-track.org. You can choose a time interval and fetch the TLE's for any satellite. There is also an API functionality. You have to log in to the site first.

brandon-rhodes commented 6 years ago

Happily, it looks like there's a Python package that supports downloading TLEs from space-track:

https://pypi.python.org/pypi/spacetrack

Could you test whether the TLEs it downloads can then be used with Skyfield successfully?

dsholes commented 6 years ago

Regarding the WinError, in iokit.download(), changing line 478 from os.rename(tempname, path) to os.replace(tempname, path) should fix for Windows OS. See os documentation for os.rename and os.replace.

if lockf is None:
        # On Windows, rename here because the file needs to be closed first.
     try:
          #os.rename(tempname, path)
           os.replace(tempname,path)
     except Exception as e:
           raise IOError('error renaming {0} to {1} - {2}'.format(
                tempname, path, e))
brandon-rhodes commented 6 years ago

Alas, os.replace() is only available on Python 3.3 and up, so it won't help Windows users of older Python versions — but I'll go ahead and have it use the function if it exists, and hopefully the number of 2.7 users who might run into this problem will keep falling. Thanks for the idea, @dsholes!