skyfielders / python-skyfield

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

Loader(dir).tle(file) does not honor directory #193

Closed jkrage closed 6 years ago

jkrage commented 6 years ago

As described in the documentation (http://rhodesmill.org/skyfield/files.html) and API (http://rhodesmill.org/skyfield/api-iokit.html), a customer Loader using a data directory, e.g., Loader(cache) should keep data files in that directory. The open() method, also called by tle(), does not honor the custom Loader directory. Related to discussion in closed issue #89.

My intended use case is to be able to make use of multiple (already local) TLE files from past times for point-in-time retrospectives. One path of exploration would use multiple Loader() objects.

Workaround is to prepend the data directory manually prior to open() or tle() calls. Easy enough in the short term.

With d5b70dab6018edd4027b44c4dd7ce4ef1592a101 open() was updated to permit specifying local filenames (yay!). Prior to the commit, using a relative path (e.g., './tle.txt') within the custom Loader() sufficed as a kludge.

Sample poc.py:

import skyfield.api

load = skyfield.api.Loader('cache')

earth_satellites = load.tle('cache/3le.txt')
print(f'Loaded {len(earth_satellites)} object identifiers')

print('\n-----\n')

earth_satellites = load.tle('./3le.txt')
print(f'Loaded {len(earth_satellites)} object identifiers')

Traceback:

$ python poc.py
Loaded 2 object identifiers

-----

Traceback (most recent call last):
  File "poc.py", line 12, in <module>
    earth_satellites = load.tle('./3le.txt')
  File "~/.local/virtualenvs/DPXkZ7s8/lib/python3.7/site-packages/skyfield/iokit.py", line 213, in tle
    with self.open(url, reload=reload, filename=filename) as f:
  File "~/.local/virtualenvs/DPXkZ7s8/lib/python3.7/site-packages/skyfield/iokit.py", line 240, in open
    return open(url, mode)
FileNotFoundError: [Errno 2] No such file or directory: './3le.txt'

File structure:

$ ls poc.py cache
poc.py

cache:
3le.txt

My sample 3le.txt only has two entries, and existed prior to invocation.

jkrage commented 6 years ago

I forgot to point out this is a similar issue as initially reported in #174, though the discussion on that has moved into a different direction.

brandon-rhodes commented 6 years ago

Thanks for pointing out that problem! I have added tests, so hopefully this won't break from now on.