Skip to content

Instantly share code, notes, and snippets.

@jarodl
Forked from marksands/gist:103498
Created April 29, 2009 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jarodl/103499 to your computer and use it in GitHub Desktop.
Save jarodl/103499 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Program to convert a number into the format
# hh mm ss.sssss
# Input format: ./converter.py DEC RA
import math
import sys
import string
def to_dec(inp):
deg = float(inp)
sign = "+"
if deg < 0:
sign="-"
deg=deg*(-1)
elif deg > 180:
print `deg`+": inputs may not exceed 180!\n"
elif deg > 90:
print `deg`+" exceeds 90, will convert it to negative dec\n"
deg=deg-90
sign="-"
hh=int(deg)
mm=int((deg-int(deg))*60)
ss=((deg-int(deg))*60-mm)*60
print '\t'+sign+string.zfill(`hh`,2)+'h'+string.zfill(`mm`,2)+'m'+'%10.8fs' % ss
def to_ra(imp):
deg = float(imp)
if deg < 0:
deg=deg+360
if deg > 360:
print `deg`+": inputs may not exceed 360!\n"
hh=int(deg/15)
mm=int((deg-15*hh)*4)
ss=(4*deg-60*hh-mm)*60
print '\t'+string.zfill(`hh`,2)+'h'+string.zfill(`mm`,2)+'m'+'%10.8fs' % ss
def main():
if len(sys.argv[:]) != 3:
print "Usage: python converter.py DEC RA"
else:
print"Declination:"
to_dec(sys.argv[1])
print"Right Ascension:"
to_ra(sys.argv[-1])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment