# - - - p a r s e L o n
def parseLon ( s ):
"""Validate and convert an external longitude.
[ s is a nonempty string ->
if s is a valid external longitude ->
return that longitude in radians
else -> raise SyntaxError ]
"""
First we'll use Section 8.12, “EW_PAT: Longitude suffix” to validate the
suffix letter specifying east or west longitude.
#-- 1 --
# [ last := last character of s
# rawAngle := s up to the last character ]
last = s[-1]
rawAngle = s[:-1]
#-- 2 --
# [ if EW_PAT matches last ->
# ewFlag := last, lowercased
# else -> raise SyntaxError ]
m = EW_PAT.match ( last )
if m is None:
raise SyntaxError, ( "Longitude '%s' does not end with "
"'e' or 'w'." % s )
else:
ewFlag = last.lower()
Next, check the angle with Section 9.14, “parseAngle(): Convert an external
angle string”.
#-- 3 --
# [ if rawAngle is a valid angle ->
# absAngle := that angle in radians
# else -> raise SyntaxError ]
absAngle = parseAngle ( rawAngle )
All that remains is to attach the sign. West Longitude is converted to East Longitude by subtracting from 2π.
#-- 4 --
if ewFlag == 'w': angle = TWO_PI - absAngle
else: angle = absAngle
#-- 5 --
return angle