Calendars
Yesterday, I embarked on a coding adventure inspired by a book recommended by my friend Ts. Dr. Suresh Ramasamy CISSP,CISM,GCTI,GNFA,GCDA,CIPM. This book delved into the Balinese calendar, sparking my curiosity to delve deeper. Using Claude.AI and chatGPT, I developed a code showcasing the five key elements of the Hindu calendar, emphasizing the Balinese wara and Javanese macapat. The insights from the book on the initial Bali calendar were invaluable in refining my coding skills throughout this journey. Leveraging technology, I brought my ideas to life, continuously improving my work along the way.
Code:
import swisseph as swe
import pandas as pd
# Hindu Weekdays (Vaar)
vaar_names = ['Ravivaar', 'Somvaar', 'Mangalvaar', 'Budhvaar', 'Guruvar', 'Shukravar', 'Shanivaar']
# Balinese Wara Days
bali_pawukon_days = {
"wara_type": {
"triwara": ['pasah', 'beteng', 'kajeng'],
"sadwara": ['tungleh', 'aryang', 'urukung', 'pariron', 'was', 'maulu'],
"saptawara": ['redite', 'soma', 'anggara', 'buda', 'wraspati', 'sukra', 'saniscara'],
"pancawara": ['umanis', 'paing', 'pon', 'wage', 'keliwon'],
"asatawara": ['sri', 'indra', 'guru', 'yama', 'ludra', 'brahma', 'kala', 'uma'],
"sangawara": ['dangu', 'jangur', 'gigis', 'nohan', 'ogan', 'erangan', 'urungan', 'tulus', 'dadi'],
"dasawara": ['pandita', 'pati', 'suka', 'duka', 'sri', 'manuh', 'manusa', 'raja', 'dewa', 'raksasa'],
"caturwara": ['sri', 'laba', 'jaya', 'menala'],
"dwiwara": ['menga', 'pepet']
}
}
# **Macapat Calculation**
macapat_names = [
"Maskumambang", "Mijil", "Sinom", "Kinanthi", "Asmaradana",
"Gambuh", "Dhandhanggula", "Durma", "Pangkur", "Megatruh"
]
def calculate_macapat(wara_numbers):
"""Sum Wara values, reduce to single digit, and assign Macapat."""
total = sum(wara_numbers)
while total >= 10: # Reduce to single digit
total = sum(int(digit) for digit in str(total))
return macapat_names[total - 1] # Adjust index (0-9)
# Compute Balinese Pawukon Date from Fixed Date
bali_epoch = 146 # Simplified for Balinese calendar
def fixed_from_julian(year, month, day):
"""Convert Gregorian date to fixed day number (Julian day number)"""
a = (14 - month) // 12
y = year + 4800 - a
m = month + 12 * a - 3
jdn = day + (153 * m + 2) // 5 + 365 * y + y // 4 - 32083
return jdn
def balinese_pawukon_from_fixed(date):
"""Calculate Balinese Pawukon day from fixed date"""
return (date - bali_epoch) % 210
# Function to extract Wara components accurately
def bali_wara_component(wara_type, fixed_date):
"""Get the appropriate Wara component for a given date"""
pawukon_day = balinese_pawukon_from_fixed(fixed_date)
cycle_length = len(bali_pawukon_days["wara_type"][wara_type])
return bali_pawukon_days["wara_type"][wara_type][pawukon_day % cycle_length]
# **Compute Hindu Vaar (weekday)**
def calculate_vaar(year, month, day):
jd = swe.julday(year, month, day)
weekday_index = int(jd % 7)
return vaar_names[weekday_index]
# **Compute Hindu Lunisolar elements (FIXED)**
def calculate_lunisolar_elements(year, month, day):
jd = swe.julday(year, month, day)
try:
# Get the raw calculation results
sun_result = swe.calc_ut(jd, swe.SUN)
moon_result = swe.calc_ut(jd, swe.MOON)
# Extract the longitude from the nested tuple
# Based on debug output: ((longitude, ...), flag)
sun_long = sun_result[0][0] # First element of first tuple
moon_long = moon_result[0][0] # First element of first tuple
# Calculate tithi (lunar day)
elongation = (moon_long - sun_long) % 360
tithi_num = int(elongation // 12) % 30
tithi_names = ["Pratipada", "Dwitiya", "Tritiya", "Chaturthi", "Panchami",
"Shashthi", "Saptami", "Ashtami", "Navami", "Dashami",
"Ekadashi", "Dwadashi", "Trayodashi", "Chaturdashi", "Purnima",
"Pratipada", "Dwitiya", "Tritiya", "Chaturthi", "Panchami",
"Shashthi", "Saptami", "Ashtami", "Navami", "Dashami",
"Ekadashi", "Dwadashi", "Trayodashi", "Chaturdashi", "Amavasya"]
tithi_name = tithi_names[tithi_num]
# Calculate nakshatra (lunar mansion)
nakshatra_num = int(moon_long // (360 / 27)) % 27
nakshatra_names = ["Ashwini", "Bharani", "Krittika", "Rohini", "Mrigashirsha",
"Ardra", "Punarvasu", "Pushya", "Ashlesha", "Magha",
"Purva Phalguni", "Uttara Phalguni", "Hasta", "Chitra", "Swati",
"Vishakha", "Anuradha", "Jyeshtha", "Mula", "Purva Ashadha",
"Uttara Ashadha", "Shravana", "Dhanishta", "Shatabhisha",
"Purva Bhadrapada", "Uttara Bhadrapada", "Revati"]
nakshatra_name = nakshatra_names[nakshatra_num]
# Calculate yoga (auspicious timing)
yoga_num = int(((sun_long + moon_long) % 360) // (360 / 27)) % 27
yoga_names = ["Vishkumbha", "Priti", "Ayushman", "Saubhagya", "Shobhana",
"Atiganda", "Sukarma", "Dhriti", "Shula", "Ganda",
"Vriddhi", "Dhruva", "Vyaghata", "Harshana", "Vajra",
"Siddhi", "Vyatipata", "Variyan", "Parigha", "Shiva",
"Siddha", "Sadhya", "Shubha", "Shukla", "Brahma",
"Indra", "Vaidhriti"]
yoga_name = yoga_names[yoga_num]
# Calculate karana (half of a tithi)
karana_num = (2 * tithi_num) % 11
karana_names = ["Bava", "Balava", "Kaulava", "Taitila", "Garija",
"Vanija", "Vishti", "Bava", "Balava", "Kaulava", "Taitila"]
karana_name = karana_names[karana_num]
return tithi_name, nakshatra_name, yoga_name, karana_name
except Exception as e:
print(f"Error calculating lunisolar elements: {e}")
return "Unknown", "Unknown", "Unknown", "Unknown"
# **Process CSV Input**
def process_csv(file_path):
try:
# Add try/except for file reading
df = pd.read_csv(file_path)
results = []
for index, row in df.iterrows():
try:
julian_date = row["Julian Date"]
year, month, day = map(int, julian_date.split("-"))
# Calculate Hindu elements
vaar = calculate_vaar(year, month, day)
tithi_name, nakshatra_name, yoga_name, karana_name = calculate_lunisolar_elements(year, month, day)
# Calculate Balinese Wara elements
fixed_date = fixed_from_julian(year, month, day)
pawukon_day = balinese_pawukon_from_fixed(fixed_date)
# Get all Balinese Wara components
triwara = bali_wara_component("triwara", fixed_date)
sadwara = bali_wara_component("sadwara", fixed_date)
saptawara = bali_wara_component("saptawara", fixed_date)
pancawara = bali_wara_component("pancawara", fixed_date)
asatawara = bali_wara_component("asatawara", fixed_date)
sangawara = bali_wara_component("sangawara", fixed_date)
dasawara = bali_wara_component("dasawara", fixed_date)
caturwara = bali_wara_component("caturwara", fixed_date)
dwiwara = bali_wara_component("dwiwara", fixed_date)
# For Macapat calculation
wara_numbers = [
int(fixed_date % 3) + 1, # Triwara (1-3)
int(fixed_date % 6) + 1, # Sadwara (1-6)
int(fixed_date % 7) + 1, # Saptawara (1-7)
int(fixed_date % 5) + 1, # Pancawara (1-5)
int(fixed_date % 8) + 1, # Asatawara (1-8)
int(fixed_date % 9) + 1, # Sangawara (1-9)
int(fixed_date % 4) + 1, # Caturwara (1-4)
int(fixed_date % 2) + 1 # Dwiwara (1-2)
]
macapat = calculate_macapat(wara_numbers)
# Store results
result = {
"Date": julian_date,
"Pawukon Day": pawukon_day,
"Vaar": vaar,
"Triwara": triwara,
"Sadwara": sadwara,
"Saptawara": saptawara,
"Pancawara": pancawara,
"Asatawara": asatawara,
"Sangawara": sangawara,
"Dasawara": dasawara,
"Caturwara": caturwara,
"Dwiwara": dwiwara,
"Tithi": tithi_name,
"Nakshatra": nakshatra_name,
"Yoga": yoga_name,
"Karana": karana_name,
"Macapat": macapat
}
results.append(result)
# **Print Everything**
print(f"Date: {julian_date}")
print(f"Pawukon Day: {pawukon_day}")
print(f"Vaar: {vaar}")
print(f"Balinese Calendar:")
print("{} {} {} {} {} {} {} {} {}".format(triwara,sadwara,saptawara,pancawara,asatawara,sangawara,dasawara,caturwara,dwiwara))
print(f"Hindu Elements:")
print(f" Tithi: {tithi_name}")
print(f" Nakshatra: {nakshatra_name}")
print(f" Yoga: {yoga_name}")
print(f" Karana: {karana_name}")
print(f"Macapat: {macapat}\n")
except Exception as e:
print(f"Error processing row {index}: {e}")
# Save results to a new CSV
results_df = pd.DataFrame(results)
output_file = "processed_results.csv"
results_df.to_csv(output_file, index=False)
print(f"Results saved to {output_file}")
except Exception as e:
print(f"Error processing CSV file: {e}")
# Check if file exists before attempting to process
import os
csv_file = "input_julian_dates.csv"
if os.path.exists(csv_file):
process_csv(csv_file)
else:
print(f"Error: File '{csv_file}' not found. Please create this file with a 'Julian Date' column.")
# Create example CSV if it doesn't exist
example_df = pd.DataFrame({
"Julian Date": ["2023-01-01", "2023-02-01", "2023-03-01"]
})
example_df.to_csv(csv_file, index=False)
print(f"Created example file '{csv_file}' with sample dates. Please modify it and run the script again.")
(Note: The provided code snippet offers a glimpse into various calendar calculations, promising an engaging exploration of functionality.)
Book: Calendrical Calculations - The Ultimate Edition
Hardback ISBN: 9781197957623
Paperback ISBN: 9781107683167
#CodingFun #BalineseCulture #JavaneseHeritage
#HackThePlanet #HackersWillHack