-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastro.py
313 lines (263 loc) · 11 KB
/
astro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# (c) 2019 Anirban Banerjee
#Licensed under:
#GNU GENERAL PUBLIC LICENSE
#Version 3, 29 June 2007
from mpapbf import *
class astro ():
pi = mpap('3.1415926535897932')
pix2 = mpap('6.2831853071795865')
D2R = pi/180
R2D = mpap(180)/pi
Longitude = mpap(81.889)
Latitude = mpap(25.426)
Elevation = mpap(0)
Ls = mpap(0)
Lm = mpap(0)
Ms = mpap(0)
Mm = mpap(0)
month = ["January","February","March","April","May","June",
"July","August","September","October","November","December"]
rashi = ["Mesha","Vrisha","Mithuna","Karka","Simha","Kanya","Tula",
"Vrischika","Dhanu","Makara","Kumbha","Meena"]
day = ["Ravi","Soma","Mangal","Budh","Brihaspati","Shukra","Shani"]
tithi = ["Prathamaa","Dvitiya","Tritiya","Chaturthi","Panchami",
"Shashthi","Saptami","Ashtami","Navami","Dashami","Ekadashi",
"Dvadashi","Trayodashi","Chaturdashi","Purnima","Pratipada",
"Dvitiya","Tritiya","Chaturthi","Panchami","Shashthi",
"Saptami","Ashtami","Navami","Dashami","Ekadashi","Dvadashi",
"Trayodashi","Chaturdashi","Amaavasya"]
karan = ["Bava","Baalava","Kaulava","Taitula","Garija","Vanija",
"Vishti","Shakuni","Chatushpada","Naga","Kimstughna"]
yoga = ["Vishakumbha","Priti","Ayushman","Saubhagya","Shobhana",
"Atiganda","Sukarman","Dhriti","Shula","Ganda","Vriddhi",
"Dhruva","Vyaghata","Harshana","Vajra","Siddhi","Vyatipata",
"Variyan","Parigha","Shiva","Siddha","Saadhya","Shubha","Shukla",
"Brahma","Indra","Vaidhriti"]
nakshatra = ["Ashvini","Bharani","Krittika","Rohini","Mrigashira","Ardra",
"Punarvasu","Pushya","Ashlesa","Magha","Purva Phalguni","Uttara Phalguni",
"Hasta","Chitra","Svaati","Vishakha","Anuradha","Jyeshtha","Mula",
"Purva Ashadha","Uttara Ashadha","Shravana","Dhanishtha","Shatabhisha",
"Purva Bhaadra","Uttara Bhaadra","Revati"]
def datenum(self, day, mon, year, hour, minute):
cumudays = [0, 0,31,59,90,120,151,181,212,243,273,304,334]
#Calculate the serial date number:
dNum = (365 * year + cumudays[mon] + day + \
year / 4 - year / 100 + year / 400 + \
(year % 4 != 0) - (year % 100 != 0) + (year % 400 != 0) + \
((hour * 60 + minute) / 1440.0))
if (mon > 2):
if (((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)):
dNum += 1.0
return dNum
def ayan (self, d):
t = (mpap(d) + 36523.5) / 36525
o = mpap('259.183275') - mpap('1934.142008333206') * t + mpap('0.0020777778') * t * t
L = mpap('279.696678') + mpap('36000.76892') *t + mpap('0.0003025') * t * t
ayanval = mpap(17.23) * (o * self.D2R).sin() + (L * self.D2R * 2).sin() * 1.27 - (mpap(5025.64) + mpap(1.11) * t) * t
# Based on Lahiri
ayanval = (ayanval-80861.27) / 3600
return ayanval
def REV (self, x):
return x - (x / 360).floor() * 360.0
def lsun (self, d):
#print ("d is ", d)
w = mpap('282.9404') + mpap ('4.70935e-5') * d
#print ("w is ", w)
a = mpap(1.0)
e = mpap(0.016709) - mpap('1.151e-9') * d
M = self.REV(mpap('356.0470') + mpap('0.9856002585') * d)
self.Ms = M
self.Ls = w + M
tmp = M * self.D2R
E = M + self.R2D * e * tmp.sin() * (e * tmp.cos() + 1)
#print ("E is ", E)
tmp = E * self.D2R
x = tmp.cos() - e
#print ("x is ", x)
y = tmp.sin() * (mpap(1) - e * e).sqrt()
#print ("y is ", y)
r = (x * x + y * y).sqrt()
#print ("r is ", r)
#print ("y.atan2(x) is ", y.atan2(x))
#print ("self.R2D * y.atan2(x) is ", self.R2D * y.atan2(x))
v = self.REV (self.R2D * y.atan2(x))
#print ("v is ", v)
return self.REV(v+w)
def lmoon (self, d):
N = mpap('125.1228') - mpap('0.0529538083') * d
i = mpap('5.1454')
w = self.REV(mpap('318.0634') + mpap('0.1643573223') * d)
a = mpap('60.2666')
e = mpap('0.054900')
M = self.REV(mpap('115.3654') + mpap('13.0649929509') * d)
self.Mm = M
self.Lm = N + w + M
#Calculate Eccentricity anomaly
tmp = M * self.D2R
E = M + self.R2D * e * tmp.sin() * (e * tmp.cos() + 1)
tmp = E * self.D2R
Et = E - (E - self.R2D * e * tmp.sin() - M) / (mpap(1) - e * tmp.cos())
while(E - Et > mpap('1e-3')):
E = Et
tmp = E * self.D2R
Et = E - (E - self.R2D * e * tmp.sin() - M) / (mpap(1) - e * tmp.cos())
tmp = E * self.D2R
x = a * (tmp.cos() - e)
y = a * (mpap(1) - e * e).sqrt() * tmp.sin()
r = (x * x + y * y).sqrt()
v = self.REV(self.R2D * y.atan2(x))
tmp = self.D2R * N
tmp1 = self.D2R * (v + w)
tmp2 = self.D2R * i
xec = r * (tmp.cos() * tmp1.cos() - tmp.sin() * tmp1.sin() * tmp2.cos())
yec = r * (tmp.sin() * tmp1.cos() + tmp.cos() * tmp1.sin() * tmp2.cos())
zec = r * tmp1.sin() * tmp2.sin()
# Do some corrections
D = mpap(self.Lm - self.Ls)
F = mpap(self.Lm - N)
lon = self.R2D * yec.atan2(xec) \
- mpap('1.274') * ((self.Mm - D * 2) * self.D2R).sin() \
+ mpap('0.658') * ((D * 2)*self.D2R).sin() \
- mpap('0.186') * (mpap(self.Ms) * self.D2R).sin() \
- mpap('0.059') * ((self.Mm * 2 - D * 2) * self.D2R).sin() \
- mpap('0.057') * ((self.Mm - D * 2 + self.Ms) * self.D2R).sin() \
+ mpap('0.053') * ((self.Mm + D * 2) * self.D2R).sin() \
+ mpap('0.046') * ((D * 2 - self.Ms) * self.D2R).sin() \
+ mpap('0.041') * ((self.Mm - self.Ms) * self.D2R).sin() \
- mpap('0.035') * (D * self.D2R).sin() \
- mpap('0.031') * ((self.Mm + self.Ms) * self.D2R).sin() \
- mpap('0.015') * ((F * 2 - D * 2)*self.D2R).sin() \
+ mpap('0.011') * ((self.Mm - D * 4) * self.D2R).sin()
return self.REV(lon)
def calc5 (self, dd, mm, yy, hr, zhr):
#Calculate day number since 2000 Jan 0.0 TDT
d = mpap(367) * yy - mpap(7) * (mpap(yy) + (mpap(mm) + 9) / 12) / 4 + mpap(275) * mm / 9 + dd - 730530
#d2_1 = mpap(367) * mpap(yy)
#d2_2 = ((mpap(7)) * (mpap(yy) + mpap(5001) + (mpap(mm) - 9) // 7)) // 4
#d2_3 = (mpap(275) * mm) // 9 + dd + 1729777
#d2 = mpap(367) * mpap(yy) - ((mpap(7)) * (mpap(yy) + mpap(5001) + \
# (mpap(mm) - 9) // 7)) // 4 + (mpap(275) * mm) // 9 + dd + 1729777
#Calculate Ayanamsa, moon and sun longitude
#print ("d is ", d)
#print ("d2 is ", d2)
#print ("d2_1 is ", d2_1)
#print ("d2_2 is ", d2_2)
#print ("d2_3 is ", d2_3)
#print ("d2 is ", d2_1 - d2_2 + d2_3)
date = self.datenum(dd, mm, yy, hr, zhr)
#print ("datenum is ", date)
#print ("datenum % 7 is ", int((date+6) % 7))
#vaara = self.day[int(d2) % 7]
vaara = self.day[int(date+5) % 7]
d = d + (hr - zhr) / 24
slon = self.lsun(d)
mlon = self.lmoon (d)
#print ("slon is ", slon)
#print ("mlon is ", mlon)
#Calculate Tithi and Paksha
tmlon = mlon + (360 if (mlon < slon) else 0)
tslon = slon
n = int((tmlon - tslon) / 12)
#print ("tithi and paksha index: ", n)
tithi = self.tithi[n]
paksha = "Shukla" if (n <= 14) else "Krishna"
ayanansha = self.ayan(d)
#print ("ayanansha", ayanansha)
#Calculate Nakshatra
tmlon = self.REV(mlon + ayanansha)
ni = int(tmlon * 6 / 80)
#print ("nakshatra index: ", ni)
nakshatra = self.nakshatra[int(tmlon * 6 / 80)]
#Calculate Yoga
tmlon = mlon + ayanansha
tslon = slon + ayanansha;
ni = int(self.REV(tmlon + tslon) * 6 / 80)
#print ("yoga index: ", ni)
yoga = self.yoga[int(self.REV(tmlon + tslon) * 6 / 80)]
#Calculate Karana
tmlon = mlon + 360 if (mlon < slon) else mpap(0)
tslon = slon
n = int((tmlon - tslon) / 6)
if n <= 0:
n = 10
if n >= 57:
n -= 50
if n > 0 and n < 57:
n = (n - 1) - int((n - 1) / 7) * 7
#print ("karana index: ", n)
karan = self.karan[n]
#Calculate the rashi in which the moon is present
tmlon = self.REV(mlon + ayanansha)
ni = int(tmlon / 30)
#print ("rashi index: ", n)
rashi = self.rashi[int(tmlon / 30)]
print(' Tithi: ' + tithi, '\n', paksha + ' paksha\n', vaara + 'vaara\n', 'Nakshatra: ' + nakshatra + '\n',\
'Yoga: ' + yoga + '\n',\
'Karana: ' + karan + '\n', 'Raashi: ' + rashi)
return
def sun (self, dd, mm, yy):
#the Julian date
d = mpap(367) * mpap(yy) - ((mpap(7)) * (mpap(yy) + mpap(5001) + \
(mpap(mm) - 9) // 7)) // 4 + (mpap(275) * mm) // 9 + dd + 1729777
#print ("Julian day to d is ", d)
#d = Jdate is the Julian date
#n is the number of days since Jan 1st, 2000 12:00.
#2451545.0 is the equivalent Julian year of Julian days for 2000, 1, 1.5.
#(68.184) / 86400 = 0.0008 is the fractional Julian Day for leap
#seconds and terrestrial time.
n = d - mpap('2451545.0') + mpap(68.184) / 86400
#---mean solar noon
#jSTAR is an approximation of mean solar time at d,
#expressed as a Julian day with the day fraction.
#self.Longitude is the longitude west (west is negative,
#east is positive) of the observer on the Earth in degrees
jSTAR = n - self.Longitude/360.0
#---solar mean anomaly
M = ((mpap('357.5291') + mpap('0.98560028') * jSTAR) % 360) * self.D2R
#---equation of the centre
#C is the Equation of the center value needed to calculate lambda (see next equation).
#1.9148 is the coefficient of the Equation of the Center for the planet the observer is on (in this case, Earth)
C = (mpap('1.9148') * M.sin() + mpap('0.02') * (M * 2).sin() + mpap('0.0003') * (M*3).sin()) * self.D2R
#---ecliptic longitude -- lambda
#lambdaLong is the ecliptic longitude.
#102.9372 is a value for the argument of perihelion.
lambdaLong = (M + C + self.pi + mpap('102.9372') * self.D2R) % self.pix2
#---solar transit
#jTRANSIT is the Julian date for the local true solar transit (or solar noon).
#2451545.0 is noon of the equivalent Julian year reference.
#mpap(0.0053) * M.sin() - mpap('0.0069') * (lambdaLong * 2).sin() is a
#simplified version of the equation of time. The coefficients are fractional day minutes.
jTRANSIT = mpap('2451545.0') + jSTAR + mpap(0.0053) * M.sin() - mpap('0.0069') * (lambdaLong * 2).sin()
#---declination of the sun
#delta is the declination of the sun
#23.44° is Earth's maximum axial tilt toward the sun
sineDelta = lambdaLong.sin() * (mpap('23.44') * self.D2R).sin()
delta = sineDelta.asin()
#---elevation correction (elevation is in metres)
#This corrects for both apparent dip and terrestrial refraction.
#For example, for an observer at 10,000 feet, add (−115°/60°) or about −1.92° to −0.83°.
elevationCorr = (mpap('-2.076') / 60) * self.Elevation.sqrt() * self.D2R
#---hour angle
#omega is the hour angle from the observer's zenith;
#phi is the north latitude of the observer (north is positive,
#south is negative) on the Earth.
phi = self.Latitude * self.D2R
cosOmega = ((mpap('-0.83') * self.D2R + elevationCorr).sin() - phi.sin() * sineDelta) / \
(phi.cos() * delta.cos())
omega = cosOmega.acos()
#calculate sunrise and sunset times
jRISE = jTRANSIT - omega / self.pix2
jSET = jTRANSIT + omega / self.pix2
#print ("Rise Time: ", jRISE)
#print ("Set Time: ", jSET)
jNOON = (jSET + jRISE) / 2
dayLightHours = (jSET - jRISE) * 24
#print ("jNOON is :", jNOON)
riseToNoonHrs = (jNOON - jRISE) * 24
noonToSetHrs = (jSET - jNOON) * 24
sriseHr = str( mpap(12) - riseToNoonHrs.ceil()) + ':' + ((mpap(1) - riseToNoonHrs.frac()) * 60).roundstr(2)
ssetHr = str(int(noonToSetHrs) + 12) + ':' + (noonToSetHrs.frac() * 60).roundstr(2)
print (" Sunrise at: ", sriseHr)
print (" Sunset at: ", ssetHr)
print (" Hours of daylight:", dayLightHours.roundstr(2))
return