# Converting Between Decimal Degrees and Hours, Minutes, Seconds

Here's a quick Python snippet I wrote to convert right ascension in decimal degrees to hours, minutes, seconds and declination to (+/-)degrees, minutes, seconds.

```def deg2HMS(ra='', dec='', round=False):   RA, DEC, rs, ds = '', '', '', ''   if dec:     if str(dec) == '-':       ds, dec = '-', abs(dec)     deg = int(dec)     decM = abs(int((dec-deg)*60))     if round:       decS = int((abs((dec-deg)*60)-decM)*60)     else:       decS = (abs((dec-deg)*60)-decM)*60     DEC = '{0}{1} {2} {3}'.format(ds, deg, decM, decS)      if ra:     if str(ra) == '-':       rs, ra = '-', abs(ra)     raH = int(ra/15)     raM = int(((ra/15)-raH)*60)     if round:       raS = int(((((ra/15)-raH)*60)-raM)*60)     else:       raS = ((((ra/15)-raH)*60)-raM)*60     RA = '{0}{1} {2} {3}'.format(rs, raH, raM, raS)      if ra and dec:     return (RA, DEC)   else:     return RA or DEC```

For example:
```In : f.deg2HMS(ra=66.918277) Out: '4 27 40.386'```

Or even:
```In : f.deg2HMS(dec=24.622590) Out: '+24 37 21.324'```

Or if you want to round the seconds, just do:
```In : f.deg2HMS(dec=24.622590,round=True) Out: '+24 37 21'```

And to convert hours, minutes and seconds into decimal degrees, we have:

```def HMS2deg(ra='', dec=''):   RA, DEC, rs, ds = '', '', 1, 1   if dec:     D, M, S = [float(i) for i in dec.split()]     if str(D) == '-':       ds, D = -1, abs(D)     deg = D + (M/60) + (S/3600)     DEC = '{0}'.format(deg*ds)      if ra:     H, M, S = [float(i) for i in ra.split()]     if str(H) == '-':       rs, H = -1, abs(H)     deg = (H*15) + (M/4) + (S/240)     RA = '{0}'.format(deg*rs)      if ra and dec:     return (RA, DEC)   else:     return RA or DEC```

So we can get back our decimal degrees with:

```In : f.HMS2deg(ra='4 27 40.386', dec='+24 37 21.324') Out: (66.918, 24.622)```

## 8 thoughts on “Converting Between Decimal Degrees and Hours, Minutes, Seconds”

1. Adric Riedel on said:

This is great!
You can also simplify the code to just the DEC case, and run RA/15 through it if you get an RA (although formatting will have to be done elsewhere).

2. Henrietta Leavitt on said:

Sorry, but this is dangerous and sloppy code! Telescopes like Hubble have pointed at the wrong place because of sloppiness like this. Always treat declinations as positive numbers and save the sign separately. Otherwise things break badly:

n : aaa.HMS2deg(dec='-00 30 01.0')
Out: '0.500277777778'

In : aaa.deg2HMS(dec=-0.51)
Out: '+0 30 36.0'

• Joe Filippazzo on said:

I don’t know if a sign error is “sloppy” or “dangerous” but thanks for the comment. It’s a good point and it’s fixed now!

Also, if you get Hubble time don’t grab your code off a website.

Also, didn’t you die in 1921?

3. Gabrielle on said:

Hi there,

Thanks alot for this great code. I'm trying to use it to transform an array of values (in dec. degrees) into HMS. Any tips on how to imput this into python and get it to give me a new array in HMS?

4. kelle on said:

I would suggest *not* using this code and instead using the much more powerful and supported astropy.coordinates package: http://docs.astropy.org/en/latest/coordinates/index.html

• Gabrielle on said:

This looks really helpful. Thank you. But for some reason my ipython returns an "str" not available message when I attempt to import the necessary modules. Would you happen to have any helpful tips for dealing with this as well?

• Peter Jonasen on said:

Brilliant. Thank you.