skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.4k stars 211 forks source link

hadec crashes: 'int' object has no attribute 'longitude' #712

Closed ion1 closed 2 years ago

ion1 commented 2 years ago

The following code crashes with the following output:

import skyfield
from skyfield.api import Star, load
from skyfield.data import hipparcos
from skyfield.named_stars import named_star_dict

print(skyfield.__version__)

eph = load("de421.bsp")

with load.open(hipparcos.URL) as f:
    stars_df = hipparcos.load_dataframe(f)

ts = load.timescale()
t = ts.utc(2022, 1, 1, 0, 0, 0)

earth = eph["earth"]
regulus = Star.from_dataframe(stars_df.loc[named_star_dict["Regulus"]])
print(earth.at(t).observe(regulus).hadec())
1.42
Traceback (most recent call last):
  File "...\crash.py", line 18, in <module>
    print(earth.at(t).observe(regulus).hadec())
  File "...\.venv\lib\site-packages\skyfield\positionlib.py", line 302, in hadec   
    ha = self.center.longitude.radians - sublongtiude
AttributeError: 'int' object has no attribute 'longitude'
brandon-rhodes commented 2 years ago

Thanks for reporting this, I think it suggests that this error could be made clearer. But, first: could you explain what value you would like to be returned for the hour angle here? If you could share the number, then it might help me suggest the correct Skyfield code to get the value you need.

ion1 commented 2 years ago

could you explain what value you would like to be returned for the hour angle here? If you could share the number, then it might help me suggest the correct Skyfield code to get the value you need.

I'm just getting to know Skyfield and I was looking into the differences between radec, hadec and geographic_position_of when I encountered the crash. I think geographic_position_of will suit my current needs.

brandon-rhodes commented 2 years ago

I think geographic_position_of will suit my current needs.

Great, I'm glad you've found a way to move forward! In the next few days I'll see about getting that error message made more specific.

brandon-rhodes commented 2 years ago

There — I've landed a fix which should print a more interesting error message in the future:

to compute a body’s hour angle, you must observe it  from a specific latitude and longitude on Earth

Hopefully that will help make the error clearer for the next person to run into it!