Search code examples
pythonastropy

Converting equatorial to alt-az coordinates is very slow


I'm trying to convert the equatorial coordinates of an object to alt-az coordinates for a given time & location in (hopefully) <5 sec runtime, or more ideally, <1 sec runtime.

Following the astropy tutorial for coordinate transformations, I set up the following code:

from astropy import units as u
from astropy.coordinates import SkyCoord,EarthLocation, AltAz
from astropy.time import Time

target = SkyCoord(9.81625*u.deg, 0.88806*u.deg, frame='icrs')

location = EarthLocation(lat='31d57.5m', lon='-111d35.8m', height=0*u.m)
obs_time = Time('2010-12-21 1:00')

alt_az_frame = AltAz(location=location, obstime=obs_time)
target_alt_az = target.transform_to(alt_az_frame)

print(target_alt_az.alt, target_alt_az.az)

This code takes 20 seconds to run, almost all of which comes from the target.transform_to(alt_az_frame) line.

Is there a more appropriate way to use the transform_to function to speed up the code, or should I abandon using astropy altogether and write code from scratch? I understand there's a lot of extra functionality built into SkyCoord objects, much of which I likely don't need -- it's just convenient to use pre-built, standardized code.


Solution

  • After some debugging, it seems that it happens because astropy.utils.iers.iers's conf.iers_auto_url is a bad url. Here is my simple solution for your problem.

    from astropy.utils.iers.iers import conf
    # The desired url may change in the future
    conf.iers_auto_url = 'ftp://cddis.gsfc.nasa.gov/pub/products/iers/finals2000A.all'
    
    # Run your code here
    

    Alternatively, you can do things like conf.remote_timeout = 0.1 or conf.auto_download = False(use backup data).

    I couldn't find a document directly relevant to this issue but reading this page may help. This problem was discussed in the issue. I think this problem happens of only certain versions of the library, so updating the library to the latest version could be a solution.