Dates when the Earth is closest and farthest away from the sun

Hello.

This script gives the dates of Aphelion and Perihelion. It works best from 2001 onwards, where it is as far as I have seen accurate to within 5 minutes, in comparision to This table. The earth is closest at the Perihelion, and farthest away at Aphelion. You can find the distance in Astronomical Units in there. Printing it out on the report, is left as an excersise. And you can enter only one year in the dialog, and get the result in a dialog. :slight_smile:

You’ll need Satimage.osax in order to run this.

script periapsis
	# Meeus chp 37
	# 2 first colums are a linear comb for a sine
	# argument, column nr 3 is the maginitude
	# for perihelion, the 4th is the same for aphelion.
	# The constant k is increased with 0.5 for finding the aphelion
	
	# Will also deliver length ( in kilometres ).
	property scriptTitle : "Dates of Perihelion and Aphelion"
	property Debug : true
	property corr_table : {¬
		{328.41, 132.788585, 1.278, -1.352}, ¬
		{316.13, 584.903153, -0.055, 0.061}, ¬
		{346.2, 450.380738, -0.091, 0.062}, ¬
		{136.95, 659.306737, -0.056, 0.029}, ¬
		{249.52, 329.653368, -0.045, 0.031}}
	
	property instance : {"Perihelion", "Aphelion"}
	
	script Earthlib
		property parent : AppleScript

		property g_L0EarthCoefficients : {¬
			{175347046, 0, 0}, ¬
			{3341656, 4.6692568, 6283.07585}, ¬
			{34894, 4.6261, 1.25661517E+4}, ¬
			{3497, 2.7441, 5753.3849}, ¬
			{3418, 2.8289, 3.5231}, ¬
			{3136, 3.6277, 7.77137715E+4}, ¬
			{2676, 4.4181, 7860.4194}, ¬
			{2343, 6.1352, 3930.2097}, ¬
			{1324, 0.7425, 1.15067698E+4}, ¬
			{1273, 2.0371, 529.691}, ¬
			{1199, 1.1096, 1577.3435}, ¬
			{990, 5.233, 5884.927}, ¬
			{902, 2.045, 26.298}, ¬
			{857, 3.508, 398.149}, ¬
			{780, 1.179, 5223.694}, ¬
			{753, 2.533, 5507.553}, ¬
			{505, 4.583, 1.8849228E+4}, ¬
			{492, 4.205, 775.523}, ¬
			{357, 2.92, 0.067}, ¬
			{317, 5.849, 1.1790629E+4}, ¬
			{284, 1.899, 796.298}, ¬
			{271, 0.315, 1.0977079E+4}, ¬
			{243, 0.345, 5486.778}, ¬
			{206, 4.806, 2544.314}, ¬
			{205, 1.869, 5573.143}, ¬
			{202, 2.458, 6069.777}, ¬
			{156, 0.833, 213.299}, ¬
			{132, 3.411, 2942.463}, ¬
			{126, 1.083, 20.775}, ¬
			{115, 0.645, 0.98}, ¬
			{103, 0.636, 4694.003}, ¬
			{102, 0.976, 1.5720839E+4}, ¬
			{102, 4.267, 7.114}, ¬
			{99, 6.21, 2146.17}, ¬
			{98, 0.68, 155.42}, ¬
			{86, 5.98, 1.6100069E+5}, ¬
			{85, 1.3, 6275.96}, ¬
			{85, 3.67, 7.14307E+4}, ¬
			{80, 1.81, 1.726015E+4}, ¬
			{79, 3.04, 1.203646E+4}, ¬
			{75, 1.76, 5088.63}, ¬
			{74, 3.5, 3154.69}, ¬
			{74, 4.68, 801.82}, ¬
			{70, 0.83, 9437.76}, ¬
			{62, 3.98, 8827.39}, ¬
			{61, 1.82, 7084.9}, ¬
			{57, 2.78, 6286.6}, ¬
			{56, 4.39, 1.41435E+4}, ¬
			{56, 3.47, 6279.55}, ¬
			{52, 0.19, 1.213955E+4}, ¬
			{52, 1.33, 1748.02}, ¬
			{51, 0.28, 5856.48}, ¬
			{49, 0.49, 1194.45}, ¬
			{41, 5.37, 8429.24}, ¬
			{41, 2.4, 1.965105E+4}, ¬
			{39, 6.17, 1.044739E+4}, ¬
			{37, 6.04, 1.021329E+4}, ¬
			{37, 2.57, 1059.38}, ¬
			{36, 1.71, 2352.87}, ¬
			{36, 1.78, 6812.77}, ¬
			{33, 0.59, 1.778985E+4}, ¬
			{30, 0.44, 8.399685E+4}, ¬
			{30, 2.74, 1349.87}, ¬
			{25, 3.16, 4690.48}}
		
		property g_L1EarthCoefficients : {¬
			{6.28331966747E+11, 0, 0}, ¬
			{206059, 2.678235, 6283.07585}, ¬
			{4303, 2.6351, 1.25661517E+4}, ¬
			{425, 1.59, 3.523}, ¬
			{119, 5.796, 26.298}, ¬
			{109, 2.966, 1577.344}, ¬
			{93, 2.59, 1.884923E+4}, ¬
			{72, 1.14, 529.69}, ¬
			{68, 1.87, 398.15}, ¬
			{67, 4.41, 5507.55}, ¬
			{59, 2.89, 5223.69}, ¬
			{56, 2.17, 155.42}, ¬
			{45, 0.4, 796.3}, ¬
			{36, 0.47, 775.52}, ¬
			{29, 2.65, 7.11}, ¬
			{21, 5.34, 0.98}, ¬
			{19, 1.85, 5486.78}, ¬
			{19, 4.97, 213.3}, ¬
			{17, 2.99, 6275.96}, ¬
			{16, 0.03, 2544.31}, ¬
			{16, 1.43, 2146.17}, ¬
			{15, 1.21, 1.097708E+4}, ¬
			{12, 2.83, 1748.02}, ¬
			{12, 3.26, 5088.63}, ¬
			{12, 5.27, 1194.45}, ¬
			{12, 2.08, 4694.0}, ¬
			{11, 0.77, 553.57}, ¬
			{10, 1.3, 6286.6}, ¬
			{10, 4.24, 1349.87}, ¬
			{9, 2.7, 242.73}, ¬
			{9, 5.64, 951.72}, ¬
			{8, 5.3, 2352.87}, ¬
			{6, 2.65, 9437.76}, ¬
			{6, 4.67, 4690.48}}
		
		property g_L2EarthCoefficients : {¬
			{52919, 0, 0}, ¬
			{8720, 1.0721, 6283.0758}, ¬
			{309, 0.867, 1.2566152E+4}, ¬
			{27, 0.05, 3.52}, ¬
			{16, 5.19, 26.3}, ¬
			{16, 3.68, 155.42}, ¬
			{10, 0.76, 1.884923E+4}, ¬
			{9, 2.06, 7.771377E+4}, ¬
			{7, 0.83, 775.52}, ¬
			{5, 4.66, 1577.34}, ¬
			{4, 1.03, 7.11}, ¬
			{4, 3.44, 5573.14}, ¬
			{3, 5.14, 796.3}, ¬
			{3, 6.05, 5507.55}, ¬
			{3, 1.19, 242.73}, ¬
			{3, 6.12, 529.69}, ¬
			{3, 0.31, 398.15}, ¬
			{3, 2.28, 553.57}, ¬
			{2, 4.38, 5223.69}, ¬
			{2, 3.75, 0.98}}
		
		property g_L3EarthCoefficients : {¬
			{289, 5.844, 6283.076}, ¬
			{35, 0, 0}, ¬
			{17, 5.49, 1.256615E+4}, ¬
			{3, 5.2, 155.42}, ¬
			{1, 4.72, 3.52}, ¬
			{1, 5.3, 1.884923E+4}, ¬
			{1, 5.97, 242.73}}
		
		property g_L4EarthCoefficients : {¬
			{114, 3.142, 0}, ¬
			{8, 4.13, 6283.08}, ¬
			{1, 3.84, 1.256615E+4}}
		
		property g_L5EarthCoefficients : {1, 3.14, 0}
		
		property g_B0EarthCoefficients : {¬
			{280, 3.199, 8.4334662E+4}, ¬
			{102, 5.422, 5507.553}, ¬
			{80, 3.88, 5223.69}, ¬
			{44, 3.7, 2352.87}, ¬
			{32, 4.0, 1577.34}}
		
		property g_B1EarthCoefficients : {¬
			{9, 3.9, 5507.55}, ¬
			{6, 1.73, 5223.69}}
		
		property g_R0EarthCoefficients : {¬
			{100013989, 0, 0}, ¬
			{1670700, 3.0984635, 6283.07585}, ¬
			{13956, 3.05525, 1.25661517E+4}, ¬
			{3084, 5.1985, 7.77137715E+4}, ¬
			{1628, 1.1739, 5753.3849}, ¬
			{1576, 2.8469, 7860.4194}, ¬
			{925, 5.453, 1.150677E+4}, ¬
			{542, 4.564, 3930.21}, ¬
			{472, 3.661, 5884.927}, ¬
			{346, 0.964, 5507.553}, ¬
			{329, 5.9, 5223.694}, ¬
			{307, 0.299, 5573.143}, ¬
			{243, 4.273, 1.1790629E+4}, ¬
			{212, 5.847, 1577.344}, ¬
			{186, 5.022, 1.0977079E+4}, ¬
			{175, 3.012, 1.8849228E+4}, ¬
			{110, 5.055, 5486.778}, ¬
			{98, 0.89, 6069.78}, ¬
			{86, 5.69, 1.572084E+4}, ¬
			{86, 1.27, 1.6100069E+5}, ¬
			{65, 0.27, 1.726015E+4}, ¬
			{63, 0.92, 529.69}, ¬
			{57, 2.01, 8.399685E+4}, ¬
			{56, 5.24, 7.14307E+4}, ¬
			{49, 3.25, 2544.31}, ¬
			{47, 2.58, 775.52}, ¬
			{45, 5.54, 9437.76}, ¬
			{43, 6.01, 6275.96}, ¬
			{39, 5.36, 4694.0}, ¬
			{38, 2.39, 8827.39}, ¬
			{37, 0.83, 1.965105E+4}, ¬
			{37, 4.9, 1.213955E+4}, ¬
			{36, 1.67, 1.203646E+4}, ¬
			{35, 1.84, 2942.46}, ¬
			{33, 0.24, 7084.9}, ¬
			{32, 0.18, 5088.63}, ¬
			{32, 1.78, 398.15}, ¬
			{28, 1.21, 6286.6}, ¬
			{28, 1.9, 6279.55}, ¬
			{26, 4.59, 1.044739E+4}}
		
		property g_R1EarthCoefficients : {¬
			{103019, 1.10749, 6283.07585}, ¬
			{1721, 1.0644, 1.25661517E+4}, ¬
			{702, 3.142, 0}, ¬
			{32, 1.02, 1.884923E+4}, ¬
			{31, 2.84, 5507.55}, ¬
			{25, 1.32, 5223.69}, ¬
			{18, 1.42, 1577.34}, ¬
			{10, 5.91, 1.097708E+4}, ¬
			{9, 1.42, 6275.96}, ¬
			{9, 0.27, 5486.78}}
		
		property g_R2EarthCoefficients : {¬
			{4359, 5.7846, 6283.0758}, ¬
			{124, 5.579, 1.2566152E+4}, ¬
			{12, 3.14, 0}, ¬
			{9, 3.63, 7.771377E+4}, ¬
			{6, 1.87, 5573.14}, ¬
			{3, 5.47, 1.884923E+4}}
		
		property g_R3EarthCoefficients : {¬
			{145, 4.273, 6283.076}, ¬
			{7, 3.92, 1.256615E+4}}
		
		property g_R4EarthCoefficients : {4, 2.56, 6283.08}
		
		property g_L1EarthCoefficientsJ2000 : {¬
			{6.28307584999E+11, 0, 0}, ¬
			{206059, 2.678235, 6283.07585}, ¬
			{4303, 2.6351, 1.25661517E+4}, ¬
			{425, 1.59, 3.523}, ¬
			{119, 5.796, 26.298}, ¬
			{109, 2.966, 1577.344}, ¬
			{93, 2.59, 1.884923E+4}, ¬
			{72, 1.14, 529.69}, ¬
			{68, 1.87, 398.15}, ¬
			{67, 4.41, 5507.55}, ¬
			{59, 2.89, 5223.69}, ¬
			{56, 2.17, 155.42}, ¬
			{45, 0.4, 796.3}, ¬
			{36, 0.47, 775.52}, ¬
			{29, 2.65, 7.11}, ¬
			{21, 5.43, 0.98}, ¬
			{19, 1.85, 5486.78}, ¬
			{19, 4.97, 213.3}, ¬
			{17, 2.99, 6275.96}, ¬
			{16, 0.03, 2544.31}, ¬
			{16, 1.43, 2146.17}, ¬
			{15, 1.21, 1.097708E+4}, ¬
			{12, 2.83, 1748.02}, ¬
			{12, 3.26, 5088.63}, ¬
			{12, 5.27, 1194.45}, ¬
			{12, 2.08, 4694.0}, ¬
			{11, 0.77, 553.57}, ¬
			{10, 1.3, 6286.6}, ¬
			{10, 4.24, 1349.87}, ¬
			{9, 2.7, 242.73}, ¬
			{9, 5.64, 951.72}, ¬
			{8, 5.3, 2352.87}, ¬
			{6, 2.65, 9437.76}, ¬
			{6, 4.67, 4690.48}}
		
		property g_L2EarthCoefficientsJ2000 : {¬
			{8722, 1.0725, 6283.0758}, ¬
			{991, 3.1416, 0}, ¬
			{295, 0.437, 1.2566152E+4}, ¬
			{27, 0.05, 3.52}, ¬
			{16, 5.19, 26.3}, ¬
			{16, 3.69, 155.42}, ¬
			{9, 0.3, 1.884923E+4}, ¬
			{9, 2.06, 7.771377E+4}, ¬
			{7, 0.83, 775.52}, ¬
			{5, 4.66, 1577.34}, ¬
			{4, 1.03, 7.11}, ¬
			{4, 3.44, 5573.14}, ¬
			{3, 5.14, 796.3}, ¬
			{3, 6.05, 5507.55}, ¬
			{3, 1.19, 242.73}, ¬
			{3, 6.12, 529.69}, ¬
			{3, 0.3, 398.15}, ¬
			{3, 2.28, 553.57}, ¬
			{2, 4.38, 5223.69}, ¬
			{2, 3.75, 0.98}}
		
		property g_L3EarthCoefficientsJ2000 : {¬
			{289, 5.842, 6283.076}, ¬
			{21, 6.05, 1.256615E+4}, ¬
			{3, 5.2, 155.42}, ¬
			{3, 3.14, 0}, ¬
			{1, 4.72, 3.52}, ¬
			{1, 5.97, 242.73}, ¬
			{1, 5.54, 1.884923E+4}}
		
		property g_L4EarthCoefficientsJ2000 : {¬
			{8, 4.14, 6283.08}, ¬
			{1, 3.28, 1.256615E+4}}
		
		property g_B1EarthCoefficientsJ2000 : {¬
			{227778, 3.413766, 6283.07585}, ¬
			{3806, 3.3706, 1.25661517E+4}, ¬
			{3620, 0, 0}, ¬
			{72, 3.33, 1.884923E+4}, ¬
			{8, 3.89, 5507.55}, ¬
			{8, 1.79, 5223.69}, ¬
			{6, 5.2, 2352.87}}
		
		property g_B2EarthCoefficientsJ2000 : {¬
			{9721, 5.1519, 6283.07585}, ¬
			{233, 3.1416, 0}, ¬
			{134, 0.644, 1.2566152E+4}, ¬
			{7, 1.07, 1.884923E+4}}
		
		property g_B3EarthCoefficientsJ2000 : {¬
			{276, 0.595, 6283.076}, ¬
			{17, 3.14, 0}, ¬
			{4, 0.12, 1.256615E+4}}
		
		property g_B4EarthCoefficientsJ2000 : {¬
			{6, 2.27, 6283.08}, ¬
			{1, 0, 0}}
		# Not done with all the tests and excersises in chapter 31!
		#on run
		#	set myjde to JDE(2013, 10, 2, 17, 30, 0) of JulianDateLib
		#	set l to EclipticLongitude(myjde)
		#	set m to EclipticalLatitude(myjde)
		#	set R to RadiusVector(myjde)
		#end run
		
		on EclipticLongitude(JDE)
			local rho, rho2, rho3, rho4, rho5, L0, L1, L2, L3, L4, L5
			set rho to (JDE - 2451545) / 365250
			set rho2 to rho * rho
			set rho3 to rho2 * rho
			set rho4 to rho3 * rho
			set rho5 to rho4 * rho
			# calculate L0
			set {L0, L1, L2, L3, L4} to {0, 0, 0, 0, 0}
			repeat with i from 1 to (length of g_L0EarthCoefficients)
				set L0 to L0 + (item 1 of item i of my g_L0EarthCoefficients) * (cos ((item 2 of item i of my g_L0EarthCoefficients) + (item 3 of item i of my g_L0EarthCoefficients) * rho))
			end repeat
			# calculate L1
			repeat with i from 1 to (length of g_L1EarthCoefficients)
				set L1 to L1 + (item 1 of item i of my g_L1EarthCoefficients) * (cos ((item 2 of item i of my g_L1EarthCoefficients) + (item 3 of item i of my g_L1EarthCoefficients) * rho))
			end repeat
			# calculate L2
			repeat with i from 1 to (length of g_L2EarthCoefficients)
				set L2 to L2 + (item 1 of item i of my g_L2EarthCoefficients) * (cos ((item 2 of item i of my g_L2EarthCoefficients) + (item 3 of item i of my g_L2EarthCoefficients) * rho))
			end repeat
			# calculate L3
			repeat with i from 1 to (length of g_L3EarthCoefficients)
				set L3 to L3 + (item 1 of item i of my g_L3EarthCoefficients) * (cos ((item 2 of item i of my g_L3EarthCoefficients) + (item 3 of item i of my g_L3EarthCoefficients) * rho))
			end repeat
			# calculate L4
			repeat with i from 1 to (length of g_L4EarthCoefficients)
				set L4 to L4 + (item 1 of item i of my g_L4EarthCoefficients) * (cos ((item 2 of item i of my g_L4EarthCoefficients) + (item 3 of item i of my g_L4EarthCoefficients) * rho))
			end repeat
			# Calculate L5
			set L5 to (item 1 of my g_L5EarthCoefficients) * (cos ((item 2 of my g_L5EarthCoefficients) + (item 3 of my g_L5EarthCoefficients) * rho))
			# convert results back to degrees, and map them within 360 
			return _mapWithinModulus(((L0 + L1 * rho + L2 * rho2 + L3 * rho3 + L4 * rho4 + L5 * rho5) / 100000000) * 180 / pi, 360)
		end EclipticLongitude
		
		on EclipticalLatitude(JDE)
			local rho, B0, B1
			set {rho, B0, B1} to {((JDE - 2451545) / 365250), 0, 0}
			
			repeat with i from 1 to (length of g_B0EarthCoefficients)
				set B0 to B0 + (item 1 of item i of my g_B0EarthCoefficients) * (cos ((item 2 of item i of my g_B0EarthCoefficients) + (item 3 of item i of my g_B0EarthCoefficients) * rho))
			end repeat
			
			repeat with i from 1 to (length of g_B1EarthCoefficients)
				set B1 to B1 + (item 1 of item i of my g_B1EarthCoefficients) * (cos ((item 2 of item i of my g_B1EarthCoefficients) + (item 3 of item i of my g_B1EarthCoefficients) * rho))
			end repeat
			
			return (((B0 + B1 * rho) / 100000000) * 180 / pi)
			
		end EclipticalLatitude
		
		on RadiusVector(JDE)
			local rho, rho2, rho3, rho4, rho5, R0, R1, R2, R3, R4
			set {rho, R0, R1, R2, R3} to {((JDE - 2451545) / 365250), 0, 0, 0, 0}
			set rho2 to rho * rho
			set rho3 to rho2 * rho
			set rho4 to rho3 * rho
			
			repeat with i from 1 to (length of g_R0EarthCoefficients)
				set R0 to R0 + (item 1 of item i of my g_R0EarthCoefficients) * (cos ((item 2 of item i of my g_R0EarthCoefficients) + (item 3 of item i of my g_R0EarthCoefficients) * rho))
			end repeat
			
			repeat with i from 1 to (length of g_R1EarthCoefficients)
				set R1 to R1 + (item 1 of item i of my g_R1EarthCoefficients) * (cos ((item 2 of item i of my g_R1EarthCoefficients) + (item 3 of item i of my g_R1EarthCoefficients) * rho))
			end repeat
			
			repeat with i from 1 to (length of g_R2EarthCoefficients)
				set R2 to R2 + (item 1 of item i of my g_R2EarthCoefficients) * (cos ((item 2 of item i of my g_R2EarthCoefficients) + (item 3 of item i of my g_R2EarthCoefficients) * rho))
			end repeat
			
			repeat with i from 1 to (length of g_R3EarthCoefficients)
				set R3 to R3 + (item 1 of item i of my g_R3EarthCoefficients) * (cos ((item 2 of item i of my g_R3EarthCoefficients) + (item 3 of item i of my g_R3EarthCoefficients) * rho))
			end repeat
			
			set R4 to (item 1 of my g_R4EarthCoefficients) * (cos ((item 2 of my g_R4EarthCoefficients) + (item 3 of my g_R4EarthCoefficients) * rho))
			return ((R0 + R1 * rho + R2 * rho2 + R3 * rho3 + R4 * rho4) / 100000000)
		end RadiusVector
		
		on _mapWithinModulus(aDecNumber, aModulus)
			set aDecNumber to aDecNumber mod aModulus
			if aDecNumber < 0 then set aDecNumber to aDecNumber + aModulus
			return aDecNumber
		end _mapWithinModulus
		
	end script
	
	set {startY, endY} to getRangeOfYears(2001, "SemiCentury", scriptTitle)
	if (startY - endY) = 0 then
		set singular to true
	else
		set singular to false
		_MAKEDOC(scriptTitle & " from " & startY & " to " & endY & linefeed & linefeed & linefeed, 500, 800)
	end if
	# set dyr to decimalYear(2010, 1)
	repeat with dyr from startY to endY
		
		repeat with i from 1 to 2
			if i mod 2 = 1 then
				set kap to k_perihelion(dyr)
				set factor to 1
			else
				set kap to k_aphelion(dyr)
				set factor to -1
			end if
			
			set jdeaph to jdeofvertice(kap) 
			
			set {delta, N} to {1.5, 1}
			set R0 to RadiusVector(jdeaph) of Earthlib

			set jdeaph to jdeaph + N
			
			repeat while true
				set R1 to RadiusVector(jdeaph) of Earthlib
				
				if R1 - R0 > 1.0E-8 then
					set jdeaph to jdeaph - (delta / (2 ^ N)) * factor
				else if R0 - R1 > 1.0E-8 then
					set jdeaph to jdeaph + (delta / (2 ^ N)) * factor
				else
					exit repeat
				end if
				set R0 to R1
				set N to N + 1
			end repeat
			set jdetime to jdeaph mod 1 + 0.5
			if jdetime > 1 then set jdetime to jdetime mod 1
			# set {yr_, mnt_, day_, h_, m_, s_} to adjustJulianDateAccordingToGMT(jdeaph)
			set {yr, mnt, dy} to gregorianCalendarTriplet from jdeaph
			set dateStr to formatDate(yr, mnt, dy)
			set {hr, min, sec} to timeTriplet from jdetime
			set timeStr to formatTime(hr, min, sec)
			if singular then
				if i = 1 then
					set yield to item i of instance & tab & dateStr & " " & timeStr & return
				else
					set yield to yield & item i of instance & tab & dateStr & " " & timeStr
					
					tell application (path to frontmost application as text)
						activate
						display dialog yield with title scriptTitle buttons {"Ok"} default button 1 cancel button 1 with icon note
					end tell
				end if
			else
				_PRINT(item i of instance & tab & dateStr & " " & timeStr & return)
			end if
		end repeat
		_PRINT(linefeed)
	end repeat
	
	on k_aphelion(dyr)
		return (approxK((dyr + 7 / 12)) div 1 + 0.5)
	end k_aphelion
	
	on k_perihelion(dyr)
		return (approxK((dyr + 1 / 12)) div 1)
	end k_perihelion
	
	# timetriplet should manage.
	
	# script extrema
	on jdeofvertice(K)
		# k is an integer for perihelion, increased by exactly 
		# 0.5 for aphelion
		# Returns JDE for the time and day of the event.
		local interrim, corr
		set {interrim, corr} to {(2.451547507E+6 + (K * (365.2596358 + (K * 1.58E-8)))), 0}
		
		if (abs ((K mod 1) - 0.5)) < 1.0E-5 then
			# we correct for 	aphelion
			set magn to 4
		else
			set magn to 3
		end if
		repeat with i from 1 to (length of corr_table)
			set corr to corr + ((item magn of item i of corr_table ¬
				) * (sin (((item 1 of item i of corr_table) ¬
				+ ((item 2 of item i of corr_table) * K)) * pi / 180)))
		end repeat

		return (interrim + corr)
		
	end jdeofvertice
	
	on approxK(decimalYear)

		return (0.99997 * (decimalYear - 2000.01))
	end approxK
	# end script
	
	-- Ask the user for the range of dates to be covered.
	-- from a start date, up to, and including enddate.
	on getRangeOfYears(initialYear, initialRange, scriptTitle)
		-- Added 2013.10.7, used in Equation Of Time, high accuracy..
		-- Nigel Garvey http://macscripter.net/viewtopic.php?id=41273
		-- customized a little by McUsr (All faults are mine!), 
		-- so it is able to deliver just one date denoting a range of 1 day.
		-- There are preset ranges of Month, Week, and Year, based on the initalDate.
		local anicon, theMessage, StartYear, EndYear
		set anicon to note
		set theMessage to "Enter the required date range:"
		set StartYear to initialYear
		repeat
			if initialRange = "Century" then
				set EndYear to StartYear + 100
			else if initialRange = "SemiCentury" then
				set EndYear to StartYear + 50
			else if initialRange = "Decade" then
				set EndYear to StartYear + 10
			else if initialRange = "Year" then
				set EndYear to StartYear + 5
				# We'll add one day for a leap year
			end if
			local d1, d2, yearRange
			set d1 to StartYear as text
			set d2 to EndYear as text
			
			tell application (path to frontmost application as text)
				set yearRange to text returned of (display dialog theMessage default answer d1 & " - " & d2 with title scriptTitle buttons {"Quit", "Ok"} cancel button 1 default button 2 with icon anicon)
				
			end tell
			local tids, gotOne, yearRangeStart, yearRangeEnd
			try
				set {tids, AppleScript's text item delimiters, gotOne} to {AppleScript's text item delimiters, "-", false}
				set {yearRangeStart, gotOne} to {text item 1 of yearRange as number, true}
				
				set yearRangeEnd to text item 2 of yearRange as number
				set AppleScript's text item delimiters to tids
				exit repeat
			on error
				if gotOne and length of text items of yearRange = 1 then
					set yearRangeEnd to text item 1 of yearRange as number
					set AppleScript's text item delimiters to tids
					exit repeat
				else if gotOne then
					set today to yearRangeStart
				else
					set today to initialYear
				end if
				set AppleScript's text item delimiters to tids
				if theMessage does not contain "wasn't" then
					set theMessage to "The input  wasn't  valid!" & return & return & theMessage
					set anicon to caution
				end if
			end try
		end repeat
		return {yearRangeStart, yearRangeEnd}
	end getRangeOfYears
	
	on _PRINT(txt)
		# Nigel Garvey made the original version.
		local a
		tell application "TextEdit"
			if not my Debug then
				activate
			else
				do shell script "open -b \"com.apple.textedit\""
			end if
			if ((count its documents) is 0) then make new document
			set a to (get path of its front document)
			try
				if a is "" then set a to missing value
			on error
				set a to missing value
			end try
			if a is not missing value then make new document
			make new paragraph at end of text of front document with data (txt & linefeed) with properties {font:"Andale Mono", size:12}
		end tell
		return true
	end _PRINT
	
	on _MAKEDOC(heading, _width, _height)
		tell application "TextEdit"
			tell (make new document at front)
				set text of it to heading
				set font of every character to "Andale Mono"
				set size of every character to 13
			end tell
			tell its front window
				#	set {item 3 of its bounds, item 4 of its bounds} to {800, 300}
				set _bounds to bounds
				set item 3 of _bounds to (item 1 of _bounds) + _width
				set item 4 of _bounds to (item 2 of _bounds) + _height
				set bounds to _bounds
				
			end tell
		end tell
	end _MAKEDOC
	
	on formatDate(yr, mnt, _day)
		global us_citizen
		local a
		try
			set a to us_citizen
		on error
			set us_citizen to getUsCitizenShip()
		end try
		
		if us_citizen then
			return (pad(mnt) & "/" & pad(_day) & "/" & yr)
		else
			return (pad(_day) & "/" & pad(mnt) & "/" & yr)
		end if
	end formatDate
	
	on formatTime(_hr, _min, _sec)
		return (pad((_hr div 1)) & ":" & pad(_min) & ":" & pad((_sec div 1)))
	end formatTime
	
	to getUsCitizenShip()
		try
			set localeString to (do shell script "defaults read .GlobalPreferences AppleLocale 2>/dev/null || echo en_US |tr '
' ' '")
		on error E number N
			return true
		end try
		if localeString is "en_US" then return true
		return false
	end getUsCitizenShip
	
	on pad(N)
		-- 22. September 2013, improved pad, for the cases where n < 0 and n > 100.
		-- Thanks to Yvan Koenig.
		-- Fixed a bug
		if N < -100 then
			return N as text
		else if N < 0 then
			return ("-" & text -2 thru -1 of ("0" & ((N as number) * -1) as text))
			--	error "The pad handler assumes positive values" number 5000
		else if N < 100 then
			return text 2 thru -1 of ((100 + N) as text)
		else
			return N as text
		end if
	end pad
	
	on gregorianCalendarTriplet from JDN
		# This is an algorithm by Richards to convert a Julian Day Number, J, to a date in the Gregorian calendar
		# (proleptic, when applicable). Richards does not state which dates the algorithm is valid for.
		# it seems to work for dates after, BUT NOT INCLUDING 15th of oct 1582.
		# I have extended it to return a hour, min, sec triplet for the fractional part of the JDN.
		local y, m, N, R, p, V, s, w, b, C, f, g, h, D, MT, yr, JD
		set y to 4716
		set j to 1401
		set m to 2
		set N to 12
		set R to 4
		set p to 1461
		set V to 3
		set u to 5
		set s to 153
		set w to 2
		set b to 274277
		set C to -38
		
		set JD to JDN div 1
		set f to JD + j + (((4 * JD + b) div 146097) * 3) div 4 + C
		set E to R * f + V
		set g to (E mod p) div R
		set h to u * g + w
		set D to (h mod s) div u + 1
		set MT to (h div s + m) mod N + 1
		set yr to E div p - y + (N + m - MT) div N
		return {yr, MT, D}
	end gregorianCalendarTriplet
	
	on timeTriplet from JDN
		# This returns as standard time triplet, where the first point of time of a day is 00:00
		# and the last point of a time is 23:59:59.9999999 and so on...
		local timeFraction, tempvar, _hours, _minutes, _seconds
		set timeFraction to (JDN mod 1) * 24
		set tempvar to (timeFraction div 1 as integer)
		set _hours to (tempvar + 12) mod 24
		set timeFraction to (timeFraction - tempvar)
		set _minutes to timeFraction * 60 div 1
		set _seconds to (timeFraction - (_minutes / 60)) * 3600
		return {_hours, _minutes, _seconds}
	end timeTriplet
end script
tell periapsis to run