Converting between degrees in d,m,s to decimal degrees to radians

They are really meant for converting back and forth between navigational coordinates. Most navigational coordinate systems operate with some kind of 360 degrees system, and the functions should be usable, but maybe with some modifications in this context. The degree part of it all, when it comes to coordinates is where it is going to differ, latitude for instance is given in +/- 90 degrees. And maybe there are hour angles, that is 15 degrees. so there are no attempt to normalize the degree part in the handlers.

I post them, so you save some time to write your own.

# Version 2.10:
# Added makeDecimalDegrees, and handlers for map notation
# Version 2.9:
# Updated hexagesDegToDecDeg to treat negative degrees correctly.
# Version 2.8:
# Updated hexagesNormalize to deal with/respect negative zeroes.
# Version 2.7:
# Updated/Corrected hexagesNormalize, that normalizes an angle.

# Version 2.6:
# Removed wrong invariants in hexages add, was < 59, to > 59
# Version 2.5:
# Added lattitudeAsInTimePreferences that returns your Geodetic latitude.
# Version 2.4:
# Removed a bug in longitudeAsInTimePreferences - didn't respect seconds
# Removed a bug in NautMilesofDegOf360ArcInMetres - now return minutes of arc in metres.
# Version 2.3:
# Added handler for converting 0.23 hour angle into -12..-1 and 0 .. 11
# Added handler for getting your longitude converted to decimal angle
# from your Time Preferences.
# version2.2:
# Addded handler : offsetOfhourAngleOfLocalToGMTMeredian, that computes hour angle to the GMT
# Meredian given your longitude as a decimal angle
# version2.1:
# changed name of hexagesDiff to hexagesSubtract!
# reinforced hexagismalAdd, and hexagesDiff to act sanely on negative hexagesimals.
# increased accuracy in decDegToHexagesDeg and hexagesDegToDecDeg
# added dayHourMinSecTuple that converts a "decimal day" on the form day.fraction to d h m s.
# added decDay that converts a dayhourMinSecTuple into a decimal day on the form day.fraction.


# Usage: There are handlers here for adding and subtracting triplets that represents
# Angles in degrees.
# there are also handlers for converting between angles with decimal fraction to triplets
# and vice versa.

# A handler that "normalizes" a triplet, to become on the form d, 0<m<360,0<s360 is provided.

# you are supposed to normalize the degrees for your self, by dividing by 360 to get the
# magnitude, since these functions are also supposed to work for hour angles, which consists
# of 24 pieces of 15 degrees.

# there are also utility functions to convert directly from radians to decimal fractions,
# and degreeTriplets.
# http://macscripter.net/viewtopic.php?pid=166582#p166582
script coordDegLib
	
	on run
		set moonsd to decDegToHexagesDeg(27.321661)
		log moonsd
		
		# an arc second is 1852 metres = 1 nautical mile, = 6076 feet, 1 25/64 inch
		set decDeg to hexagesDegToDecDeg({58, 27, 37})
		set deghmin to decDegToHexagesDeg(58.4603)
		
		set thediff to hexagesDiff for {120, 15, 44} by {120, 15, 45}
		set theSum to hexagesAdd for {120, 15, 44} by {12, 14, 16}
		
		# The adjustment for 360 degrees.
		set item 1 of thediff to ((item 1 of thediff) + 360) mod 360
		# the adjustment would conversely be
		# set item 1 of thediff to ((item 1 of thediff) + 24) mod 24
		# for hours. Not that it would practical or make sense in all cases.
		
		# conversion between radians and degrees.
		set R to deg2rad(60)
		set D to rad2deg(0.5)
	end run
	
	to decimalTime from timeInSeconds
		tell (timeInSeconds)
			return (it div days + (it div hours mod 24 / 24) + (it div minutes mod 60 / 1440) + (it mod 3600 / 86400))
		end tell
	end decimalTime
	(*
	# This handler out to be in an ASDateLib
	to set_date_long(yr, mnth, dy, hr, min, sec)
		# NG http://macscripter.net/viewtopic.php?id=41374
		tell (current date) to set {day, year, its month, day, its hours, its minutes, its seconds, cur_date} to {1, yr, mnth, dy, hr, min, sec, it}
		return cur_date
		
	end set_date_long
	
	to timestampcompare(aTimeStamp, anotherTimeStamp)
		considering numeric strings
			if aTimeStamp as text < anotherTimeStamp then
				return -1
			else if aTimeStamp = anotherTimeStamp then
				return 0
			else
				return 1
			end if
		end considering
	end timestampcompare
	
	to makeTimeStamp from anASDate
		tell (anASDate)
			# yyyy.mm.dd hh:mm:ss
			return ((((year of it as text) & "." & ((month of it as integer) as text) & "." & ¬
				day of it as text) & " " & hours of it as text) & ":" & minutes of it as text) & ":" & ¬
				seconds of it as text
		end tell
	end makeTimeStamp
	
	to makeASDate from aTimeStamp
		local _yr, probe, _mnt, next_pos, _day
		set aTimeStamp to aTimeStamp as text
		set _yr to text 1 thru 4 of aTimeStamp
		set probe to text 6 thru 8 of aTimeStamp
		# the month can be of variable size 
		
		if (offset of "." in probe) is 2 then
			set _mnt to text 1 of (probe) as number
			set next_pos to 8
		else
			set _mnt to text 1 thru 2 of (probe) as number
			set next_pos to 9
		end if
		# the days can also be of variable size 
		set probe to text next_pos thru (next_pos + 2) of aTimeStamp
		if (offset of " " in probe) is 2 then
			set _day to text 1 of (probe) as number
			set next_pos to next_pos + 2
		else
			set _day to text 1 thru 2 of (probe) as number
			set next_pos to next_pos + 3
		end if
		tell (text next_pos thru -1 of aTimeStamp)
			set l to length of it
			if (length of it = 8) then
				return (my set_date_long(_yr, _mnt, _day, text 1 thru 2 of it as number, ¬
					text 4 thru 5 of it as number, text 7 thru -1 of it as number))
			else
				return (my set_date_long(_yr, _mnt, _day, text 1 thru 2 of it as number, ¬
					text 4 thru 5 of it as number, 0))
			end if
		end tell
	end makeASDate
	*)
	# det som må gå an, er at denne blir kallt opp *innefira ymd, og hms triples
	to splitInTwo for aTimeStamp
		local tids, theList
		tell (a reference to AppleScript's text item delimiters)
			set {tids, its contents} to {its contents, " "}
			set theList to text items of aTimeStamp
			set its contents to tids
			return theList
		end tell
	end splitInTwo
	
	to ymdTriple from aSplitTimeStamp
		local tids, theList
		tell (a reference to AppleScript's text item delimiters)
			set {tids, its contents} to {its contents, "."}
			set theList to text items of aSplitTimeStamp
			set its contents to tids
		end tell
		tell (theList)
			return {item 1 of it, item 2 of it, item 3 of it}
			# year, month, day
		end tell
	end ymdTriple
	
	to hmsTriple from aSplitTimeStamp
		local tids, theList
		tell (a reference to AppleScript's text item delimiters)
			set {tids, its contents} to {its contents, ":"}
			set theList to text items of aSplitTimeStamp
			set its contents to tids
		end tell
		if length of theList is 2 then
			tell (theList)
				return {item 1 of it, item 2 of it, 0}
				# hours, minutes, seconds
			end tell
		else if length of theList is 3 then
			tell (theList)
				return {item 1 of it, item 2 of it, item 3 of it}
				# hours, minutes, seconds
			end tell
		else
			error "hmsTriple: The string containing 3 or 2 numbers is bad." number 6001
		end if
	end hmsTriple
	
	# todo: unsure about adding 24, maybe better to subtract 24!
	to compareTriples for oneTriple against anotherTriple
		local m, N
		if class of oneTriple is not list or class of anotherTriple is not list or length of oneTriple < 3 or length of anotherTriple < 3 then
			error "At least one of the triples, doesn't contain a list with three values"
		end if
		tell (oneTriple)
			try
				if item 1 of it as number = 0 then
					set m to 24
				else
					set m to item 1 of it as number
				end if
				if item 1 of anotherTriple as number = 0 then
					set N to 24
				else
					set N to item 1 of anotherTriple as number
				end if
				
				if m < N then
					return -1
				else if m > N then
					return 1
				else
					if item 2 of it as number < item 2 of anotherTriple as number then
						return -1
					else if item 2 of it as number > item 2 of anotherTriple as number then
						return 1
					else
						if item 3 of it as number < item 3 of anotherTriple as number then
							return -1
						else if item 3 of it as number = item 3 of anotherTriple as number then
							return 0
						else
							return 1
						end if
					end if
				end if
			on error E number N
				error "compareTriples: One of the values in the triples probably not a number." number 6002
			end try
		end tell
	end compareTriples
	
	# This handler converts a quartet
	# with coordinates given in the form
	# N 66 67 00 to 66,67,00
	# It is a right handed system, so that
	# quartes with N and E becomes positive
	# whereas quartets with S and W becomes negative.
	
	to degreeTriplet from aMapNotationQuartet
		# length considerations
		if (length of aMapNotationQuartet < 4) then
			error "coordDegLib's degreeTriplet: incomplete quartet, Lacks one or more of: dir, deg ,min, sec (4 terms)." number 4000
		end if
		tell (aMapNotationQuartet)
			set dmsTriplet to rest of it
			if item 1 of it is "S" or item 1 of it is "W" then
				set item 1 of dmsTriplet to (item 1 of dmsTriplet) * -1 as text
			else
				
			end if
			return dmsTriplet
		end tell
	end degreeTriplet
	
	
	# by a hexagesemal number I mean a number given in degree/hours (=Number), 
	# minutes, and seconds, where minutes and seconds are given in base 60.
	# A normalized hexagesimal number is on the form above, with the further 
	# constraint, that minutes and seconds is in the range 0<it<60,
	# and both the seconds, and minutes should be positive, as long as there is 
	# a term in front. this means that if you have an angle of -4", and nothing more
	# then that is the normalized form of it.
	
	# The Normalization leads of course to some measure of caution to be taken
	# when you add or subtract. 
	# This because you can't add a negative hexagesimal number with a 
	# positive hexagesimal number. term by term.
	
	to hexagesNormalize for degreeTriplet
		if (length of degreeTriplet < 3) then
			error "coordDegLib's hexagesNormalize: incomplete triplet, Lacks one or more of: nr ,min, sec (3 terms)." number 4000
		end if
		local normalizedResult, hasNeg
		set {hasNeg, normalizedResult} to {false, {0, 0, 0}}
		
		tell normalizedResult
			set {item 1 of it, item 2 of it, item 3 of it} ¬
				to ¬
				{item 1 of degreeTriplet as number, ¬
					item 2 of degreeTriplet as number, ¬
					item 3 of degreeTriplet as number}
			# -0 = 0 , no way to detect "-0", so we take precautions.
			set b to item 1 of degreeTriplet as number
			if (item 1 of degreeTriplet as number) = 0 and item 1 of degreeTriplet begins with "-0" then
				set hasNeg to true
			else if item 1 of degreeTriplet as number = 0 and item 2 of degreeTriplet = 0 and item 2 of degreeTriplet begins with "-0" then
				set hasNeg to true
			end if
			# Removes any negative seconds
			if item 3 of it < 0 then
				if item 2 of it ≠ 0 then
					set item 3 of it to (item 3 of it) + 60
					set item 2 of it to (item 2 of it) - 1
				else if item 1 of it ≠ 0 then
					set item 1 of it to (item 1 of it) - 1
					set item 2 of it to 59
					set item 3 of it to (item 3 of it) + 60
				end if
			else if item 3 of it > 60 then
				# if the term is greater than 60, then we update the minutes
				# and subtract the minutes we transferred from the seconds.
				
				set item 2 of it to (item 2 of it) + ((item 3 of it) div 60)
				set item 3 to (item 3 of it) mod 60
			end if
			
			# removes any negative minutes if the term isn't the first
			if item 2 of it < 0 and item 1 of it ≠ 0 then
				set item 2 of it to (item 2 of it) + 60
				set item 1 of it to (item 1 of it) - 1
			else if item 2 of it > 60 then
				# if the term is greater than 60, then we update the degrees
				# and subtract the degrees we transferred from the minutes
				set item 1 of it to (item 1 of it) + ((item 2 of it) div 60)
				set item 2 of it to (item 2 of it) mod 60
			end if
			
			repeat with i from 1 to 3
				if item i of it < 0 then
					if i = 1 and length of (item 1 of it as text) < 3 then
						set item i of it to "-" & (text -2 thru -1 of ("0" & ((item i of it) * -1)))
					else if i = 1 then
						# We don't know the number of digits
						set item i of it to item i of it as text
					else
						set item i of it to "-" & (text -2 thru -1 of ("0" & ((item i of it) * -1)))
					end if
				else
					set item i of it to text -2 thru -1 of ("0" & (item i of it))
				end if
			end repeat
			# we must restore any negative signs, that has got lost in the process!
			if hasNeg and item 1 as number ≥ 0 and item 2 as number ≥ 0 and item 3 as number ≥ 0 then
				set item 1 to "-00"
			end if
			
		end tell
		return normalizedResult
	end hexagesNormalize
	
	to hexagesDiff for minTriplet by subTriplet
		# there isn't supposed to be more than 60 seconds or minutes 
		# hexagesimal difference aka base 60 number system.
		
		if (length of minTriplet < 3 or length of subTriplet < 3) then
			error "coordDegLib's hexagesDiff: incomplete triplet, Lacks one or more of: nr ,min, sec (3 terms)." number 4000
		end if
		local diffTriplet
		set diffTriplet to {0, 0, 0}
		set item 3 of diffTriplet to (item 3 of minTriplet) - (item 3 of subTriplet)
		set item 2 of diffTriplet to (item 2 of minTriplet) - (item 2 of subTriplet)
		set item 1 of diffTriplet to (item 1 of minTriplet) - (item 1 of subTriplet)
		
		if item 3 of diffTriplet < 0 then
			set item 3 of diffTriplet to (item 3 of diffTriplet) + 60
			set item 2 of diffTriplet to (item 2 of diffTriplet) - 1
		end if
		
		if item 2 of diffTriplet < 0 then
			set item 2 of diffTriplet to (item 2 of diffTriplet) + 60
			set item 1 of diffTriplet to (item 1 of diffTriplet) - 1
		end if
		# we don't adjust for any negative degrees here, as it can equally
		# well be hour angles, or we are operating within +/-90 degrees.
		# it is meant to be a general function
		return diffTriplet
	end hexagesDiff
	
	to hexagesAdd for add1Triplet by add2Triplet
		if (length of add1Triplet < 3 or length of add2Triplet < 3) then
			error "coordDegLib's hexagesAdd: incomplete triplet, Lacks one or more of: nr ,min, sec (3 terms)." number 4000
		end if
		local sumTriplet
		set sumTriplet to {0, 0, 0}
		set item 3 of sumTriplet to (item 3 of add1Triplet) + (item 3 of add2Triplet)
		set item 2 of sumTriplet to (item 2 of add1Triplet) + (item 2 of add2Triplet)
		set item 1 of sumTriplet to (item 1 of add1Triplet) + (item 1 of add2Triplet)
		
		if (item 3 of sumTriplet) > 59 then
		else
			set item 3 of sumTriplet to (item 3 of sumTriplet) - 60
			set item 2 of sumTriplet to (item 2 of sumTriplet) + 1
		end if
		
		if item 2 of sumTriplet > 59 then
		else
			set item 2 of sumTriplet to (item 2 of sumTriplet) - 60
			set item 1 of sumTriplet to (item 1 of sumTriplet) + 1
		end if
		# we don't adjust for any negative degrees here, as it can equally
		# well be hour angles, or we are operating within +/-90 degrees.
		# it is meant to be a general function
		return sumTriplet
		
	end hexagesAdd
	
	# converts a decimal degree number, that is a degree, with
	# minutes and seconds as a decimal fraction into a real
	# hexagesimal number with separate terms for degrees,
	# minutes and seconds
	-- Converts from the decimalDegree coordinat system (58.4603)
	-- to DMS ( 58 27 37 )
	on decDegToHexagesDeg(decimalDegrees)
		# new version, faster with increased precision
		local tempvar, hexagesDegs, hexagesMins, hexagesSecs
		set hexagesDegs to decimalDegrees div 1
		set tempvar to (decimalDegrees mod 1) * 3600
		# have to check for sign, and negate if have degrees,
		# and degrees are negative.
		if tempvar < 0 then
			if hexagesDegs ≠ 0 then
				set tempvar to tempvar * -1
			end if
		end if
		set hexagesMins to tempvar div 60
		# this sign always correct
		set hexagesSecs to tempvar mod 60
		# have to adjus if neg, and has degrees
		if hexagesSecs < 0 then
			if hexagesMins ≠ 0 then
				set hexagesSecs to hexagesSecs * -1
			end if
		end if
		return {hexagesDegs, hexagesMins, hexagesSecs}
	end decDegToHexagesDeg
	
	to makeDecimalDegree for tDegrees by tMinutes against tSeconds
		return (tDegrees + ((tMinutes + (tSeconds / 60)) / 60))
	end makeDecimalDegree
	
	# 16.09 2013 removed bug when dealing with negative degrees.
	on hexagesDegToDecDeg(degTriplet)
		if (length of degTriplet < 3) then
			error "coordDegLib's hexagesDegToDecDeg: incomplete triplet, Lacks one or more of: nr ,min, sec (3 terms)." number 4000
		end if
		local decimalDegrees
		tell (degreeTriplet)
			if item 1 of it as number ≥ 0 then
				set decimalDegrees to (((item 2 of it as number) / 60 ¬
					+ (item 3 of it as number) / 3600) ¬
					+ (item 1 of it as number))
			else
				set decimalDegrees to ((((item 2 of it as number) * -1) / 60 ¬
					+ ((item 3 of it as number) * -1) / 3600) ¬
					+ (item 1 of it as number))
			end if
		end tell
		return decimalDegrees
	end hexagesDegToDecDeg
	
	
	on deg2rad(decDegrees)
		return decDegrees * pi / 180
	end deg2rad
	
	on rad2deg(radians)
		return radians * 180 / pi
	end rad2deg
	
	# This handler converts from a 360 degree angle, into an hour angle.
	# an hour angle is 360/24 = 15 degrees, the rest is converted the same way,
	# this function relates the eart's movement that is 360 degrees through 24 hours
	# so that you can compute how long such and such rotation in degrees, takes in time.
	to hourAngle for a360DegMinSecAngle
		if (length of a360DegMinSecAngle < 3) then
			error "coordDegLib's hourAngle: incomplete triplet, Lacks one or more of: nr ,min, sec (3 terms)." number 4000
		end if
		
		local hourAngle, degreesLeft, minutesOfHour, minutesOfDegreeLeft, secondsofHour, secondsOfDegreeLeft, minutesByDegreesToMinutesOfHour
		
		#  Converts the whole number of hours from the degrees.
		set hourAngle to (item 1 of a360DegMinSecAngle) div 15
		# there are 24 hours per 360 degrees, 24 x 15 = 360
		set degreesLeft to ((item 1 of a360DegMinSecAngle) - (hourAngle * 15))
		# We'll now get all the pieces of 4 minutes, that is degrees of the angle since
		# using hour angles, there is 15 degrees in an hour, so that 1 degree takes 4 minutes.
		set minutesOfHour to degreesLeft * 4
		# since one degree takes 4 minutes, 15 minutes of a degree takes 1 minute of an hourAngle
		set minutesByDegreesToMinutesOfHour to ((item 2 of a360DegMinSecAngle) div 15)
		set minutesOfHour to minutesOfHour + minutesByDegreesToMinutesOfHour
		# we have now collected the minutes, if we overflow 60 minutes, we add an hour and subtract.
		set hourAngle to hourAngle + minutesOfHour div 60
		set minutesOfHour to minutesOfHour mod 60
		# We convert the less than 15 minutes we got to seconds, first we must get that rest.
		set minutesOfDegreeLeft to ((item 2 of a360DegMinSecAngle) - (minutesByDegreesToMinutesOfHour * 15))
		# the minutes of a degree left takes less than a minute of the hour to complete.
		# we convert the minutes to seconds by the identity that there are 60 seconds of time
		# per 15 minutes of angle. ( secs = minsLeft * 60 / 15
		set secondsofHour to minutesOfDegreeLeft * 60 div 15
		# We'll have to keep the rest of the minutesOfDegreeLeft and add it with secondsOfDegree.
		set minutesOfDegreeLeft to minutesOfDegreeLeft - (secondsofHour * 15 / 60)
		set secondsOfDegreeLeft to minutesOfDegreeLeft + (item 3 of a360DegMinSecAngle)
		# those seconds of degreeLeft, can be converted by taking the relationship further
		# there are 1 second of the hour per 900 seconds of angle 
		set secondsofHour to secondsofHour + secondsOfDegreeLeft div 900
		set minutesOfHour to minutesOfHour + secondsofHour div 60
		set secondsofHour to secondsofHour mod 60
		return {hourAngle, minutesOfHour, secondsofHour}
		
	end hourAngle
	
	to a360DegMinSecAngle for hourAngle
		# the inverse handler from above
		# it looses some precision, and that is naturl, since we are going
		# from quite coarse granularity to finer. 1296000 seconds versus 84600 seconds in an hour.
		if (length of hourAngle < 3) then
			error "coordDegLib's a360DegMinSecAngle:incomplete triplet, Lacks one or more of: nr ,min, sec (3 terms)." number 4000
		end if
		local deg360
		#it's the easy one.
		set deg360 to ((item 1 of hourAngle) * 15)
		# every 4 minutes is one degree, since 60 minutes is 15 degrees, and 60/15 is 4 
		set mins2Degrees to ((item 2 of hourAngle) div 4)
		set deg360 to deg360 + mins2Degrees
		# we have now any whole degrees, now it is the minutes.
		set minOfHourleft to (item 2 of hourAngle) - (mins2Degrees * 4)
		# there are 60 minutes in a degree. A minute of hour is 15 minutes of arc
		# since 4 minutes of hour is a degree of arc.
		set min360 to minOfHourleft * 15
		# a Minute of arc is 4 seconds of hour, since a minute of hour is 15 minutes of arc. 
		set secs2minsOfArc to ((item 3 of hourAngle) div 4)
		set min360 to min360 + secs2minsOfArc
		set secsOfHourLeft to ((item 3 of hourAngle) - (secs2minsOfArc * 4))
		# N seconds of arc, is 4 / M seconds of hour * 60.
		if secsOfHourLeft = 0 then # check for div by zero
			set sec360 to 0
		else
			set sec360 to ((4 / secsOfHourLeft) * 60) div 1
		end if
		return {deg360, min360, sec360}
		
	end a360DegMinSecAngle
	
	# Converts a decimal day, (a day with a fraction representing hours) into
	# a 4-tuple, consisting of days, hours, minutes and seconds
	# aDecimal day is a day with hours minutes and seconds given as a fraction
	# 27.321661 being an example.
	
	to dayHourMinSecTuple from decimalDay
		local decFrac, decMagn, tmpTriple, fourTuple
		set decMagn to (decimalDay div 1)
		set fourTuple to {decMagn, 0, 0, 0}
		set decFrac to (decimalDay - (decimalDay div 1)) * 24 as real
		if decFrac < 0 and decMagn ≠ 0 then set decFrac to decFrac * -1
		# adjusts for sign
		set tmpTriple to decDegToHexagesDeg(decFrac)
		
		set {item 2 of fourTuple, item 3 of fourTuple, item 4 of fourTuple} to {item 1 of tmpTriple, item 2 of tmpTriple, item 3 of tmpTriple}
		return fourTuple
	end dayHourMinSecTuple
	
	# returns a day with a decimal fraction representing hour mins and secs.
	to decDay from dhmsTuple
		local degTriplet
		tell dhmsTuple
			if (length of it < 4) then
				error "coordDegLib's decDay: Incomplete dhmsTuple: Lacks one or more of day,hour, min, sec  (4 terms)." number 4000
			end if
			set degTriplet to rest of it
		end tell
		tell item 1 of dhmsTuple
			if it < 0 then
				return ((it) - ((my hexagesDegToDecDeg(degTriplet)) / 24))
			else
				return ((it) + ((my hexagesDegToDecDeg(degTriplet)) / 24))
			end if
		end tell
	end decDay
	
	# minutes of arc of earth by equator (longitude), or by meredian (by altitude) turned into nautical miles 
	# (1852 metres/ 6076 feet ) which *approximates* a minute of arc of longitude at Equator.
	# this calculation isn't perfect, but it is the way the standards institutes has defined it.
	to NautMilesofDegOf360ArcInMetres for a360DegMinSecAngle
		if (length of a360DegMinSecAngle < 3) then
			error "coordDegLib's NautMilesofDegOf360Arc: incomplete triplet, Lacks one or more of: nr ,min, sec (3 terms)." number 4000
		end if
		local arcMinutes
		set arcMinutes to (item 1 of a360DegMinSecAngle) * 60 + (item 2 of a360DegMinSecAngle)
		tell (item 3 of a360DegMinSecAngle)
			if it is not 0 then
				set arcSecondsAsDecimal to it / 60 * 100
			else
				set arcSecondsAsDecimal to 0
			end if
		end tell
		return ((arcMinutes + arcSecondsAsDecimal) * 1852) # metres
	end NautMilesofDegOf360ArcInMetres
	
	# returns the number of seconds totally in the 4 tuple.
	to SecondsInInterval from dhmsTuple
		if length of dayHourMinSecTuple is not 4 then error "coordDegLib's SecondsInInterval: Incomplete dhmsTuple: Lacks one or more of: day hour min sec (4 terms)." number 4000
		return ((item 4 of dhmsTuple) + ((item 3 of dhmsTuple) * 60) + ((item 2 of dhmsTuple) * 3600) + ((item 1 of dhmsTuple) * 86400))
		# seconds + minutes as seconds, + hours as seconds, + days as seconds
	end SecondsInInterval
	
	to degreesPerSecond for orbitSeconds
		return (360 / orbitSeconds)
	end degreesPerSecond
	
	# Returns the phsyical timezone for a longitude
	to offsetOfhourAngleOfLocalToGMTMeredian by decimalAngle
		# see: http://macscripter.net/viewtopic.php?pid=166581#p166581
		# post 43
		# we remove any superfluos degrees first.
		set decimalAngle to decimalAngle mod 360
		
		# we make angles ≥ 180 into negatives
		if decimalAngle ≥ 180 then
			set decimalAngle to decimalAngle - 360
		end if
		local hourAngle
		if decimalAngle < 0 then
			set decimalAngle to (decimalAngle * -1) - 0.1
			# we have to skew the numbers by -0.1 to keep
			# boundary conditions
			set hourAngle to (24 - ((decimalAngle + 7.5) div 15)) mod 24
		else
			set hourAngle to (decimalAngle + 7.5) div 15
		end if
		return hourAngle
	end offsetOfhourAngleOfLocalToGMTMeredian
	
	# Converts an hour angle given in the interval 0 - 23 
	# to intervals -1..-12 and  0..11  offset to before or after
	# the Greenwhich Meredian.
	# set semiH to semiCircHourAngle from 1
	# see: http://macscripter.net/viewtopic.php?pid=166581#p166581
	# post 43
	to semiCircHourAngle from CircularHourAngle
		if CircularHourAngle > 11 then
			return ((12 - (CircularHourAngle mod 12)) * -1)
		else
			return CircularHourAngle
		end if
	end semiCircHourAngle
	
	# The idea is simple, the first character in the tuple
	# is for the positive direction, and the other for the
	#  negative, it is supposed to work with a normalized
	# Angle, (only first term to right negative, and 
	# {0≤d<360, 0≤m<60, 0≤s<60 }
	to aMapNotationQuartet from aDegreeTriplet by aDirectionTuple
		if (length of aDegreeTriplet < 3) then
			error "aMapNotationQuartet: incomplete triplet, Lacks one or more of: deg ,min, sec (3 terms)." number 4000
		end if
		
		if (length of aDirectionTuple < 2) then
			error "aMapNotationQuartet: incomplete tuplet, Lacks one or more of: {\"N\",\"S\"} or {\"E\",\"W\"}(2 terms)." number 4000
		end if
		local probe, newQuartet, mark
		set newQuartet to {" ", "0", "0", "0"}
		tell aDegreeTriplet
			set probe to item 1 of it as number
			if probe > 0 then
				set item 1 of newQuartet to item 1 of aDirectionTuple
				set item 2 of newQuartet to text -2 thru -1 of ("0" & (probe as text))
				set mark to 2
			else if probe < 0 then
				set item 1 of newQuartet to item 2 of aDirectionTuple
				set item 2 of newQuartet to text -2 thru -1 of ("0" & ((probe * -1) as text))
				set mark to 2
			else
				set item 2 of newQuartet to "00"
				set probe to item 2 of it as number
				if probe > 0 then
					set item 1 of newQuartet to item 1 of aDirectionTuple
					set item 3 of newQuartet to text -2 thru -1 of ("0" & (probe as text))
					set mark to 3
				else if probe < 0 then
					set item 1 of newQuartet to item 2 of aDirectionTuple
					set item 3 of newQuartet to text -2 thru -1 of ("0" & ((probe * -1) as text))
					set mark to 3
				else
					set item 3 of newQuartet to "00"
					set probe to item 3 of it as number
					if probe ≥ 0 then
						set item 1 of newQuartet to item 1 of aDirectionTuple
						set item 4 of newQuartet to text -2 thru -1 of ("0" & (probe as text))
						set mark to 4
					else if probe < 0 then
						set item 1 of newQuartet to item 2 of aDirectionTuple
						set item 4 of newQuartet to text -2 thru -1 of ("0" & ((probe * -1) as text))
						set mark to 4
					end if
				end if
			end if
			#  we transmit the rest of the arguments here.
			repeat with i from mark to 3
				set item (i + 1) of newQuartet to text -2 thru -1 of ("0" & (item i of it))
			end repeat
		end tell
		return newQuartet
	end aMapNotationQuartet
	
	
	# set decimalAngleForMyLongitude to longitudeAsInTimePreferences()
	# Returns the hourAngle offset from the Greenwhich Meredian, according to your Time Preferences.
	# see: http://macscripter.net/viewtopic.php?pid=166581#p166581
	# post 44
	
	to longitudeAsInTimePreferences()
		local LocalRegion, decimalDegree, theMinutes, theSeconds
		try
			tell (do shell script "ls -al /etc/localtime") to set LocalRegion to word -2 & "." & word -1
		on error
			error "longitudeAsInTimePreferences: something is wrong, do you have a position specified in Time Preferences?" number 5000
		end try
		tell (do shell script "sed -ne '/" & LocalRegion & "/ s/.*\\([-+][[:digit:]]*\\).*/\\1/p' </usr/share/zoneinfo/zone.tab")
			try
				if length of it is 6 then
					set {decimalDegree, theMinutes} to {(text 1 thru 4 of it as number), (text 5 thru -1 of it as number)}
					if theMinutes ≠ 0 then
						set decimalDegree to decimalDegree + theMinutes / 60
					end if
				else # length of it is 8 Thanks to kel1 fo showing me. 
					set {decimalDegree, theMinutes, theSeconds} to {(text 1 thru 4 of it as number), (text 5 thru 6 of it as number), (text 7 thru -1 of it as number)}
					if theMinutes ≠ 0 then
						set theMinutes to theMinutes / 60
					end if
					if theSeconds ≠ 0 then
						set theSeconds to theSeconds / 3600
					end if
					set decimalDegree to decimalDegree + theMinutes + theSeconds
				end if
			on error
				error "longitudeAsInTimePreferences: something is wrong, do you have a /usr/share/zoneinfo/zone.tab file on your system?" number 5000
			end try
		end tell
		return decimalDegree
	end longitudeAsInTimePreferences
	
	
	to latitudeAsInTimePreferences()
		# returns your Geodetic latitude.
		# see: http://macscripter.net/viewtopic.php?pid=166581#p166581
		# post 51
		
		local LocalRegion, decimalDegree, theMinutes, theSeconds
		try
			tell (do shell script "ls -al /etc/localtime") to set LocalRegion to word -2 & "." & word -1
		on error
			error "latitudeAsInTimePreferences: something is wrong, do you have a position specified in Time Preferences?" number 5000
		end try
		tell (do shell script "sed -ne '/" & LocalRegion & "/ s/^[^-+]*\\([-+][[:digit:]]*\\).*/\\1/p' </usr/share/zoneinfo/zone.tab")
			try
				if length of it is 5 then
					set {decimalDegree, theMinutes} to {(text 1 thru 3 of it as number), (text 4 thru -1 of it as number)}
					if theMinutes ≠ 0 then
						set decimalDegree to decimalDegree + theMinutes / 60
					end if
				else # length of it is 8 Thanks to kel1 fo showing me. 
					set {decimalDegree, theMinutes, theSeconds} to {(text 1 thru 3 of it as number), (text 4 thru 5 of it as number), (text 6 thru -1 of it as number)}
					if theMinutes ≠ 0 then
						set theMinutes to theMinutes / 60
					end if
					if theSeconds ≠ 0 then
						set theSeconds to theSeconds / 3600
					end if
					set decimalDegree to decimalDegree + theMinutes + theSeconds
				end if
			on error
				error "latitudeAsInTimePreferences: something is wrong, do you have a /usr/share/zoneinfo/zone.tab file on your system?" number 5000
			end try
		end tell
		return decimalDegree
	end latitudeAsInTimePreferences
	
	to longitudeAsInTimePreferencesWithMapNotation()
		# Thanks to Nigel Garvey for finding the bug, when -0 = 0 and not <0
		# returns your longitude E(+) or West(-).
		# This version returns a slightly transformed longitude, since the values are split with spaces,
		# A "+" is replaced with an "E" or a "-" is replaced by "W"
		local LocalRegion, decimalDegree, theMinutes, theSeconds
		try
			tell (do shell script "ls -al /etc/localtime") to set LocalRegion to word -2 & "." & word -1
		on error
			error "longitudeAsInTimePreferences: something is wrong, do you have a position specified in Time Preferences?" number 5000
		end try
		tell (do shell script "sed -ne '/" & LocalRegion & "/ s/.*\\([-+][[:digit:]]*\\).*/\\1/p' </usr/share/zoneinfo/zone.tab")
			try
				if length of it is 6 then
					set {TheDegree, theMinutes} to {(text 1 thru 4 of it), (text 5 thru -1 of it)}
					if TheDegree begins with "-" then
						set Direction to "W"
					else
						set Direction to "E"
					end if
					set degreeString to (Direction & space & text 2 thru -1 of TheDegree & space & theMinutes & space & "00")
					
				else # length of it is 8 Thanks to kel1 for showing me. 
					set {TheDegree, theMinutes, theSeconds} to {(text 1 thru 4 of it), (text 5 thru 6 of it), (text 7 thru -1 of it)}
					if TheDegree begins with "-" then
						set Direction to "W"
					else
						set Direction to "E"
					end if
					set degreeString to (Direction & space & text 2 thru -1 of TheDegree & space & theMinutes & space & theSeconds)
				end if
			on error
				error "longitudeAsInTimePreferences: something is wrong, do you have a /usr/share/zoneinfo/zone.tab file on your system?" number 5000
			end try
		end tell
		return degreeString
	end longitudeAsInTimePreferencesWithMapNotation
	
	to latitudeAsInTimePreferencesWithMapNotation()
		# returns your Geodetic latitude. ( North or South )
		# see: http://macscripter.net/viewtopic.php?pid=166581#p166581
		# post 51
		
		local LocalRegion, decimalDegree, theMinutes, theSeconds
		try
			tell (do shell script "ls -al /etc/localtime") to set LocalRegion to word -2 & "." & word -1
		on error
			error "latitudeAsInTimePreferences: something is wrong, do you have a position specified in Time Preferences?" number 5000
		end try
		tell (do shell script "sed -ne '/" & LocalRegion & "/ s/^[^-+]*\\([-+][[:digit:]]*\\).*/\\1/p' </usr/share/zoneinfo/zone.tab")
			try
				if length of it is 5 then
					set {TheDegree, theMinutes} to {(text 1 thru 3 of it), (text 4 thru -1 of it)}
					if TheDegree begins with "-" then
						set Direction to "S"
					else
						set Direction to "N"
					end if
					set degreeString to (Direction & space & text 2 thru -1 of TheDegree & space & theMinutes & space & "00")
					
				else # length of it is 8 Thanks to kel1 fo showing me. 
					set {TheDegree, theMinutes, theSeconds} to {(text 1 thru 3 of it), (text 4 thru 5 of it), (text 6 thru -1 of it)}
					if TheDegree begins with "-" then
						set Direction to "S"
					else
						set Direction to "N"
					end if
					# Thanks to Nigel Garvey for pointing out the bug here.
					set degreeString to (Direction & space & text 2 thru -1 of TheDegree & space & theMinutes & space & theSeconds)
				end if
			on error
				error "latitudeAsInTimePreferences: something is wrong, do you have a /usr/share/zoneinfo/zone.tab file on your system?" number 5000
			end try
		end tell
		return degreeString
	end latitudeAsInTimePreferencesWithMapNotation
	
end script
# tell coordDegLib to run


Added a handler above for converting a 360 degree angle in the form deg,min,sec into hour,min,sec.
Earth uses 24 hours on a 360 degree revolution.

Added a handler to convert from an hour angle to a 360 degree angle to the first post.

Added a handler to convert an arcminute of the earths arc, either by altitude over a meredian, or by longitude over equator into nautical miles. I am not to blame over the inaccuracy that the standards instutes has made, as there is a slight discrepancy of this calculation, with regards to the the circumference of the earth.

When muliplied out by 1852 metres, which there is in a Nautical mile, then the earths circumference is some 40003 kilometres, not 40075 kilometres, so there is 72 kilometres amiss by this.

Hello.

Improved accurachy with regards to number of significant digits, and signs! For the handlers that converts between angles with decimal fractions and triplets with deg, min, sec.

Added handlers for converting between day, hour, min, sec tuples, and decimal days, with hours minuts and seconds represented as a fraction.

Addded handler : offsetOfhourAngleOfLocalToGMTMeredian, that computes hour angle to the GMT
Meredian (Prime Meredian) given your longitude as a decimal angle. Thanks To Nigel Garvey, for showing me how to convert hour angles into base 24.

Added handler for converting 0.23 hour angle into -12…-1 and 0 … 11
Added handler for getting your longitude converted to decimal angle from your Time Preferences, thanks to Adam Bell.

Removed a bug in longitudeAsInTimePreferences - didn’t respect seconds
Removed a bug in NautMilesofDegOf360ArcInMetres - now return minutes of arc in metres.

Added lattitudeAsInTimePreferences that returns your Geodetic latitude.

Removed wrong invariants in hexages add, was < 59, to > 59.

Hello.

I have updated the hexagesNormalize handler to return a normalized result With regards to both negative values,
and values that extend the base.

Hello.

I have updated hexagesNormalize to deal with/respect negative zeroes. So that an angle that was initially −0Ë™ x’ y" doesn’t become 0Ë™ x’ y". Which is quite a different angle, differing with base −(2 *(x’ y")) from the opposite.

Hello.

I have added a handler that simply makes a decimal degree, that is a degree like 23.439291111111 from 23Ë™ 26 ’ 21.448 ". I have also added handlers that returns coordinates in map notation, that is, + is translated into E or N and - into W or S.

Just mentioning it: those coordinates are geodetic, and not geocentric.