Skip to content

Instantly share code, notes, and snippets.

@samuelschumacher
Created January 21, 2015 03:02
Show Gist options
  • Save samuelschumacher/e47c8c86d50017649f42 to your computer and use it in GitHub Desktop.
Save samuelschumacher/e47c8c86d50017649f42 to your computer and use it in GitHub Desktop.
Geocomputing - Lab 4
import os
#The only thing new about this program is the fact that instead of calling getvar directly,
#the function cuantos is called. cuantos() prompts the user for the number of conversions
#they would like to make and utilizes a while loop that adds 1 to the count variable 'a'.
#Once this variable is equal to the repetititon number (rep), the program exits the while
#loop. In case of failure, the entire cuantos() function is within a while loop, which is
#by default True. This means that it will loop itself infinitely if no break is encountered.
#A break is only encountered in the event of the successful run of getvar for the number of
#iterations specified by rep, so any failure would be greeted with an error prompt and restart.
def cuantos():
while True:
rep=raw_input('How many conversions would you like to make? ')
a=0
try:
while a<rep:
getvar()
a+=1
break
except:
print 'You need to enter a number, try again.'
def getvar():
while True:
var = raw_input('''
----------------------------------------------------------
Please use format 'XX, XX, XX, X' to describe the latitude
or longitude, in degrees, minutes, seconds, and
hemisphere. Alternatively, you may enter a coordinate in
decimal degrees, using format 'XX.XXXXXXX, X', indicating
first the degree and then the hemisphere, N, S, E or W.
Be aware that you must follow these protocols for the
program to function as you desire it to, and that numbers
will be treated as positive regardless of sign: ''')
var=var.replace(" ","")
print var
try:
DMS=var.split(',')
if DMS[0].find('.'):
DMS[0],DMS[1],DMS[2]=abs(float(DMS[0])), abs(float(DMS[1])), abs(float(DMS[2]))
DMS [3] = DMS[3].lower()
DMS=tuple(DMS)
todd(DMS)
break
except:
try:
DD=var.split(',')
DD[0]=abs(float(DD[0]))
DD[1]=DD[1].lower()
toddums(DD)
break
except:
print 'Failure! Read the instructions, try again.'
def todd(A):
DD=A[0]+A[1]/60+A[2]/3600
if A [3]=='s' or A [3]=='e':
print "Your DMS measurement is shown, converted into decimal degrees: -"+str(DD)
elif A [3]=='n' or A [3]=='w':
print "Your DMS measurement is shown, converted into decimal degrees:", DD
else:
print "Failure to test, try again"
getvar()
def toddums(B):
D=int(B[0])
Md=((B[0]-D)*60)
M=int(Md)
S=round((Md-M)*60)
if B [1]=='s':
print "Your DD measurement is shown as",D,'degrees,',M,'minutes and\n',S,'seconds in the Southern hemisphere.'
elif B [1]=='e':
print "Your DD measurement is shown as",D,'degrees,',M,'minutes and\n',S,'seconds in the Eastern hemisphere.'
elif B [1]=='n':
print "Your DD measurement is shown as",D,'degrees,',M,'minutes and\n',S,'seconds in the Northern hemisphere.'
elif B [1]=='w':
print "Your DD measurement is shown as",D,'degrees,',M,'minutes and\n',S,'seconds in the Western hemisphere.'
else:
print "Does not compute, try again"
getvar()
cuantos()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment