pythonpyephem

pyephem problem with alt/az to ra/dec and back


i generate random positions above the horizon(az=0-360,alt=0-90)in az/alt and calculate them withradec_to() to RA and DEC. to check the result i retransform them.

so what i don't understand is, why i get around half of coordinates back under the horizon?

import ephem
from datetime import datetime
import random

home = ephem.Observer()

home.lon = '-70.4'  # +E
home.lat = '-24.62'  # +N
home.elevation = 1189  # meters
home.date = datetime.utcnow()
RA = []
DEC = []
for n in range(100):
    # print(random.uniform(0, 360), random.uniform(0, 90))
    ra, dec = home.radec_of(az=random.uniform(0, 360), alt=random.uniform(0, 90))
    # print('%s %s' % (ra, dec))
    RA.append(ra)
    DEC.append(dec)

dummy = 0
for coordinate in RA:
    body = ephem.FixedBody()
    body._epoch = ephem.J2000
    body._ra = ephem.degrees(RA[dummy])
    body._dec = ephem.degrees(DEC[dummy])
    body.compute(home)
    dummy += 1
    print(body.az, body.alt)

the aim of this program is to generate random artifical stars over ans observer that can be tracked with pyephem.


Solution

  • You are providing floating point numbers to radec_of(), and PyEphem interprets floating point numbers as radians, not degrees. Only when numbers are supplied as strings does it interpret them as degrees. So you could try either:

        ra, dec = home.radec_of(az=str(random.uniform(0, 360)),
                                alt=str(random.uniform(0, pi)))
    

    Or you could generate radians for the angles in the first place:

    from math import pi
    
    ra, dec = home.radec_of(az=random.uniform(0, 2*pi),
                            alt=random.uniform(0, pi))
    

    Either way, you should then find that all the resulting stars are above the horizon instead of below it.

    I’ll see if I can update the PyEphem Quick Reference to explain the types expected for the arguments to radec_of(), instead of relying on conventions that are explained elsewhere in the docs where they’re hard to find.