Skip to content

Commit f0bc1cb

Browse files
committed
improve tithi start and end time
1 parent 10c67ec commit f0bc1cb

File tree

4 files changed

+58
-83
lines changed

4 files changed

+58
-83
lines changed

bikram-calendar/src/panchanga.ts

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -143,66 +143,42 @@ function calculateTithiTimes(dateForPanchanga: Date) {
143143
const elongation = siderealMoon_calc - siderealSun_calc;
144144
return REV(elongation);
145145
}
146-
147-
148-
function findTithiBoundary(
149-
searchStartJD: number,
150-
searchEndJD: number,
151-
targetBoundaryAngle: number,
152-
tithiAngleFunc: (jd: number) => number,
153-
contextLog: string = ""
154-
): number {
155-
let lowJD = searchStartJD;
156-
let highJD = searchEndJD;
157-
let midJD;
158-
let iterations = 0;
159-
const maxIterations = 100; // Max iterations to prevent infinite loops
160-
const normalizedTargetAngle = REV(targetBoundaryAngle);
161-
const angleAtLow = REV(tithiAngleFunc(lowJD));
162-
const angleAtHigh = REV(tithiAngleFunc(highJD));
163-
164-
if (angleAtLow <= angleAtHigh) {
165-
if (!(angleAtLow <= normalizedTargetAngle && normalizedTargetAngle <= angleAtHigh)) {
166-
return -1; // Optional: Handle unbracketed case if needed
167-
}
168-
} else {
169-
if (!(angleAtLow <= normalizedTargetAngle || normalizedTargetAngle <= angleAtHigh)) {
170-
return -1; // Optional: Handle unbracketed case if needed
146+
function findTithiBoundary(
147+
searchStartJD: number,
148+
searchEndJD: number,
149+
targetBoundaryAngle: number,
150+
tithiAngleFunc: (jd: number) => number
151+
): number {
152+
let lowJD = searchStartJD;
153+
let highJD = searchEndJD;
154+
let midJD;
155+
let iterations = 0;
156+
const maxIterations = 100;
157+
const normalizedTargetAngle = REV(targetBoundaryAngle);
158+
while ((highJD - lowJD) * 86400.0 > 1 && iterations < maxIterations) {
159+
midJD = (lowJD + highJD) / 2;
160+
if (midJD === lowJD || midJD === highJD) {
161+
break;
162+
}
163+
const angleAtMid = REV(tithiAngleFunc(midJD));
164+
const diff = REV(angleAtMid - normalizedTargetAngle + 180) - 180;
165+
if (diff < 0) {
166+
lowJD = midJD;
167+
} else {
168+
highJD = midJD;
169+
}
170+
iterations++;
171171
}
172+
return (lowJD + highJD) / 2;
172173
}
173-
174-
// Precision target: interval smaller than 1 second
175-
while ((highJD - lowJD) * 86400.0 > 1 && iterations < maxIterations) {
176-
midJD = (lowJD + highJD) / 2;
177-
if (midJD === lowJD || midJD === highJD) { // Prevent infinite loop if JDs become identical due to precision
178-
break;
179-
}
180-
const angleAtMid = REV(tithiAngleFunc(midJD));
181-
const diff = REV(angleAtMid - normalizedTargetAngle + 180) - 180;
182-
183-
if (diff < 0) {
184-
lowJD = midJD;
185-
} else {
186-
highJD = midJD;
187-
}
188-
iterations++;
189-
}
190-
// --- Test ---
191-
if (iterations >= maxIterations) {
192-
console.warn(
193-
`IMPROVED SEARCH (${contextLog}): Max iterations (${maxIterations}) reached for target ${normalizedTargetAngle.toFixed(4)}.\n` +
194-
` Final Window JD: [${lowJD.toFixed(6)}, ${highJD.toFixed(6)}]. Result might be imprecise.`
195-
);
196-
}
197-
return (lowJD + highJD) / 2;
198-
}
199-
174+
// --- Find tithi angle and index at sunrise ---
200175
const tithiAngleAtSunrise = tithiAngleAt(jdSunrise);
201176
const currentTithiIndex = Math.floor(tithiAngleAtSunrise / 12);
202177
const currentTithiStartBoundaryAngle = currentTithiIndex * 12;
203178
const nextTithiStartBoundaryAngle = (currentTithiIndex + 1) * 12;
204-
const tithiStartJD = findTithiBoundary(jdSunrise - 1.5, jdSunrise + 0.5, currentTithiStartBoundaryAngle, tithiAngleAt);
205-
const tithiEndJD = findTithiBoundary(jdSunrise - 0.5, jdSunrise + 1.5, nextTithiStartBoundaryAngle, tithiAngleAt);
179+
// Find start and end for the tithi at sunrise
180+
const tithiStartJD = findTithiBoundary(jdSunrise - 2, jdSunrise, currentTithiStartBoundaryAngle, tithiAngleAt);
181+
const tithiEndJD = findTithiBoundary(jdSunrise, jdSunrise + 2, nextTithiStartBoundaryAngle, tithiAngleAt);
206182
const tithiStartParts = jdToDateParts(tithiStartJD, zhr_nepal);
207183
const tithiEndParts = jdToDateParts(tithiEndJD, zhr_nepal);
208184
const tithiStartTimeStr = `${pad(tithiStartParts.day)}/${pad(tithiStartParts.month)}/${tithiStartParts.year} ${formatAMPM(tithiStartParts.hours, tithiStartParts.minutes)}`;

docs/404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<link href="https://fonts.googleapis.com/css2?family=Lohit+Devanagari&display=swap" rel="stylesheet">
1818
<!-- Print stylesheet -->
1919
<link rel="stylesheet" href="./print.css" media="print">
20-
<script type="module" crossorigin src="./assets/index-BdVvpjz5.js"></script>
20+
<script type="module" crossorigin src="./assets/index--z-fS1h9.js"></script>
2121
<link rel="stylesheet" crossorigin href="./assets/index-CBfEqjCd.css">
2222
</head>
2323

0 commit comments

Comments
 (0)