Skip to content

Instantly share code, notes, and snippets.

@marksands
Created April 29, 2009 00:28
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 marksands/103498 to your computer and use it in GitHub Desktop.
Save marksands/103498 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: ./ToDecRa.py DEC RA
#
# test data: 63.36130383 256.99567542
import math
import sys
import string
inp=sys.argv[0:] # the bug exists here
imp=sys.argv[1:] # and here with the array endings
del inp[0]
del imp[0]
if len(inp)==0:
print" Program to convert a float into a declination"
print" of the form hh:mm:ss.ssssssss\n"
print" Type 'deg2dec.py' followed by a list of floats"
print" to convert them into declinations."
if len(imp)==0:
print" Program to convert a float into a right ascension"
print" of the form hh:mm:ss.ssssssss\n"
print" Type 'deg2ra.py' followed by a list of floats"
print" to convert them into right ascensions."
def dec(inp):
for x in inp:
deg=float(x)
sign="+"
# test whether the input numbers are sane:
# if negative, store "-" in sign and continue calulation
# with positive value
if deg < 0:
sign="-"
deg=deg*(-1)
if deg > 180:
print `deg`+": inputs may not exceed 180!\n"
continue
if 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
if sign=="-":
print str(deg+90)+":"
else:
print `deg`+":"
print '\t'+sign+string.zfill(`hh`,2)+':'+string.zfill(`mm`,2)+':'+'%10.8f' % ss
print '\t'+sign+string.zfill(`hh`,2)+' '+string.zfill(`mm`,2)+' '+'%10.8f' % ss
print '\t'+sign+string.zfill(`hh`,2)+'h'+string.zfill(`mm`,2)+'m'+'%10.8fs\n' % ss
def ra(imp):
for x in imp:
deg=float(x)
# test whether the input numbers are sane:
if deg < 0:
deg=deg+360
if deg > 360:
print `deg`+": inputs may not exceed 360!\n"
continue
hh=int(deg/15)
mm=int((deg-15*hh)*4)
ss=(4*deg-60*hh-mm)*60
print `deg`+":"
print '\t'+string.zfill(`hh`,2)+':'+string.zfill(`mm`,2)+':'+'%10.8f' % ss
print '\t'+string.zfill(`hh`,2)+' '+string.zfill(`mm`,2)+' '+'%10.8f' % ss
print '\t'+string.zfill(`hh`,2)+'h'+string.zfill(`mm`,2)+'m'+'%10.8fs\n' % ss
def main():
print" Declination:\n"
dec(inp)
print" Right Ascension:\n"
ra(imp)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment