-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjain_digambaras.py
90 lines (74 loc) · 3.71 KB
/
jain_digambaras.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
#!/usr/bin/python3
# A programme to convert between Julian Days and dates in the Digimbaras Jain calendar
from math import floor, ceil
from fractions import Fraction
from solun import sankranti, conj, indian_sunrise as sunrise, conj as newmoon, phase, sid_year, lunar_month as syn_month, rasi, phasetime, dayof_hindi as dayof, vs
from months import INDIAN_LUNAR_NUM as NUMON, NUM_INDIAN_LUNAR as MONTHNO
epoch = sankranti((vs - (605 * sid_year) + (6 * rasi)), 180) # Tulā Saṁkrānti of the year 0
tz = Fraction(11,48) # Indian timezone; UTC+5:30
def fromjd(jday):
'''Given a Julian Day, compute the date in the Digimbaras Jain calendar'''
jday = Fraction(jday)
# start with the new moon
crescent = newmoon(jday, tz)
while (dayof(newmoon(crescent, tz)) > dayof(jday)):
crescent -= syn_month
while (dayof(newmoon((crescent + syn_month), tz)) <= dayof(jday)):
crescent += syn_month
# now we can sort out the year
year = (crescent - epoch) // sid_year
tula = epoch + (year * sid_year) # Tulā Saṁkrānti
while (dayof(sankranti(tula, 180)) > dayof(newmoon(crescent, tz))):
year -= 1
tula -= sid_year
while (dayof(sankranti((tula + sid_year), 180)) <= dayof(newmoon(crescent, tz))):
year += 1
tula += sid_year
# now to figure out the month
cigra = (crescent - tula) // rasi # number of the star sign the new moon falls in
angle = ((cigra + 6) * 30) % 360 # zodiacal angle of the cusp of the star sign we're in; note that in the case of the Jain calendar, we start at 180°
s = tula + (rasi * cigra) # approximate time of the saṁkrānti immediately preceding the current lunar cycle
while (dayof(sankranti(s, angle)) > dayof(newmoon(crescent, tz))):
s -= rasi
cigra = (cigra - 1) % 12
angle = (angle - 30) % 360
while (dayof(sankranti((s + rasi), ((angle + 30) % 360))) <= dayof(newmoon(crescent, tz))):
s += rasi
cigra = (cigra + 1) % 12
angle = (angle + 30) % 360
month = NUMON[(cigra + 7) % 12]
if (dayof(newmoon((crescent + syn_month), tz)) <= dayof(sankranti((s + rasi), ((angle + 30) % 360)))):
# we're in the leap month
month = "Adhik " + month
# and now the tithi
tithi = int(1 + (phase(jday, tz) // 12))
return (tithi, month, year)
def tojd(tithi, month, year):
'''Given a tithi, a month, and a day, compute the corresponding Julian Day'''
tithi = int(tithi) - 1
month = str(month)
year = int(year)
# leap month check
if (month[:5] == "Adhik"):
# leap month specificed
adhik = True
month = month[6:]
else:
adhik = False
m = (MONTHNO[month] - 7) % 12 # number of the star sign that the month begins in; subtract 6 because we start at 180°
jday = epoch + (year * sid_year) + (m * rasi) # approximate saṁkrānti of the month specified
angle = ((m + 6) * 30) % 360 # zodiacal angle of the beginning of the star sign we're interested in
crescent = newmoon(jday, tz)
while (dayof(newmoon(crescent, tz)) < dayof(sankranti(jday, angle))):
crescent += syn_month
while (dayof(newmoon((crescent - syn_month), tz)) >= dayof(sankranti(jday, angle))):
crescent -= syn_month
if ((adhik == False) and (dayof(newmoon((crescent + syn_month), tz)) <= dayof(sankranti((jday + rasi), ((angle + 30) % 360))))):
# we're in the leap month, but shouldn't be
crescent += syn_month
if (tithi == 0):
# for some reason, it gets stuck in an infinite loop if it's the first tithi of the month
jday = dayof(newmoon(crescent, tz))
else:
jday = dayof(phasetime((crescent + (tithi * Fraction(syn_month, 30))), (12 * tithi), tz))
return jday