Search code examples
pythonstringastropy

Parsing string as astronomical coordinates


I'm trying to load in a set of coordinates from a csv file. The file contains coordinates of galaxies which is loaded in a Pandas dataframe as in this snippet:

enter image description here

I'm trying to extract the coordinates from the columns _RAJ2000 and _DEJ2000 so that I can cross-match them with another file. My code:

import numpy as np
from astropy import units as u
from astropy.coordinates import SkyCoord as coord
import quasar_functions as qf

# c1 = coord('5h23m34.5s', '-69d45m22s', distance = 70*u.kpc, frame = 'icrs')
# c2 = coord('0h52m44.8s', '-72d49m43s', distance = 80*u.kpc, frame = 'fk5')

# sep = c1.separation(c2)
# sep_3d = c1.separation_3d(c2)

data = ...# here's where I call my loading function
ra1, dec1 = data['_RAJ2000'], data['_DEJ2000']
ooi1_ra, ooi1_dec = ra1[22], ra1[60]
object1_coords = coord(ra1[22]*u.hour, dec1[22]*u.degree)
object2_coords = coord(ra1[60]*u.hour, dec1[60]*u.degree)

but I'm getting an error:

ValueError: '01 24 45.98328' did not parse as unit: Syntax error parsing unit '01 24 45.98328' If
this is meant to be a custom unit, define it with 'u.def_unit'. To have it recognized inside a file
reader or other code, enable it with 'u.add_enabled_units'. For details, see
https://docs.astropy.org/en/latest/units/combining_and_defining.html

I don't want to define it as a custom unit; I'd rather have Astropy read it natively (if that's possible), or modify the string so that Astropy can handle the coordinates. I think RA is in h/m/s and DE is in degrees.


Solution

  • What you're doing is equivalent to:

    >>> '01 24 45.98328' * u.hour
    

    which means trying to convert the string '01 24 45.98328' to a quantity in literally the time unit of hours. Ditto the second but in degrees. I can see the logic behind that and maybe it should be supported. But raw units don't know how to deal with different coordinate system representations, per the docs:

    astropy.units does not know spherical geometry or sexagesimal (hours, min, sec): if you want to deal with celestial coordinates, see the astropy.coordinates package.

    TL;DR SkyCoord itself deals with parsing coordinate formats, and you specify the units you want the coordinates in directly to SkyCoord:

    >>> SkyCoord(ra='01 24 45.98328', dec='+22 30 04.3700', unit=(u.hourangle, u.deg))
    <SkyCoord (ICRS): (ra, dec) in deg
        (21.191597, 22.50121389)>