Skip to content

Commit f2f738b

Browse files
committed
adhik mas calculation improved. testing lunar month.
# added lunar month in debug. # lunar month is considered to starts from new moon. # lunar adhik and kshaya month calculation improved # resized day day detail modal hight to full if content is more like while opening debug information.
1 parent e6f6f9e commit f2f738b

File tree

2 files changed

+165
-47
lines changed

2 files changed

+165
-47
lines changed

qml/PanchangaDetailDialog.qml

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "qrc:/qml/"
88
Dialog {
99
id: panchangaDetailDialogRoot
1010
width: Math.min(parent.width * 0.9, 600)
11-
height: Math.min(contentItem.implicitHeight, parent.height * 0.8)
11+
height: Math.min(contentItem.implicitHeight, parent.height)
1212
modal: true
1313
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
1414
x: (parent.width - width) / 2
@@ -88,7 +88,7 @@ Dialog {
8888
}
8989
}
9090

91-
// Layout for the Panchanga details
91+
// Layout for the Panchanga details.
9292
ColumnLayout {
9393
id: panchangaDetails
9494
visible: !panchangaDetailDialogRoot.debugVisible
@@ -113,20 +113,26 @@ Dialog {
113113

114114
Flickable {
115115
anchors.fill: parent
116-
contentWidth: debugInfoText.paintedWidth
116+
contentWidth: Math.min(parent.width * 0.96, 600)
117117
contentHeight: debugInfoText.paintedHeight
118118
clip: true
119119

120-
TextEdit {
121-
id: debugInfoText
122-
text: currentDebugInfo
123-
color: "white"
124-
font.family: "monospace"
125-
font.pointSize: 9
126-
readOnly: true
127-
wrapMode: TextEdit.NoWrap
128-
selectByMouse: true
129-
padding: 10
120+
ScrollView {
121+
anchors.fill: parent
122+
clip: true
123+
124+
TextEdit {
125+
id: debugInfoText
126+
text: currentDebugInfo
127+
color: "white"
128+
font.family: "monospace"
129+
font.pointSize: 9
130+
readOnly: true
131+
wrapMode: TextEdit.NoWrap
132+
textFormat: TextEdit.RichText
133+
selectByMouse: true
134+
padding: 10
135+
}
130136
}
131137
}
132138
}
@@ -207,17 +213,17 @@ Dialog {
207213
}
208214

209215
var details = [
210-
createDetailRow("बिक्रम संवत", toDevanagari(panchangaData.bsYear) + " " + panchangaData.monthName + " " + toDevanagari(panchangaData.bsDay)),
211-
createDetailRow("वार", panchangaData.weekday),
212-
createDetailRow("तिथि", panchangaData.tithi + " (" + panchangaData.paksha + ")"),
213-
createDetailRow("नक्षत्र", panchangaData.nakshatra),
214-
createDetailRow("योग", panchangaData.yoga),
215-
createDetailRow("करण", panchangaData.karana),
216-
createDetailRow("सूर्य राशि", panchangaData.sunRashi),
217-
createDetailRow("चन्द्र राशि", panchangaData.moonRashi),
218-
createDetailRow("उदयास्त", "सूर्योदय " + panchangaData.sunrise + " | सूर्यास्त " + panchangaData.sunset),
219-
createDetailRow("अधिक/क्षय मास", panchangaData.adhikaMasa)
220-
];
216+
createDetailRow("बिक्रम संवत", toDevanagari(panchangaData.bsYear) + " " + panchangaData.monthName + " " + toDevanagari(panchangaData.bsDay)),
217+
createDetailRow("वार", panchangaData.weekday),
218+
createDetailRow("तिथि", panchangaData.tithi + " (" + panchangaData.paksha + ")"),
219+
createDetailRow("नक्षत्र", panchangaData.nakshatra),
220+
createDetailRow("योग", panchangaData.yoga),
221+
createDetailRow("करण", panchangaData.karana),
222+
createDetailRow("सूर्य राशि", panchangaData.sunRashi),
223+
createDetailRow("चन्द्र राशि", panchangaData.moonRashi),
224+
createDetailRow("उदयास्त", "सूर्योदय " + panchangaData.sunrise + " | सूर्यास्त " + panchangaData.sunset),
225+
createDetailRow("अधिक/क्षय मास", panchangaData.adhikaMasa)
226+
];
221227
for (var i = 0; i < details.length; ++i) {
222228
details[i].parent = panchangaDetails;
223229
}

resources/PanchangaCalculator.js

Lines changed: 135 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -226,39 +226,134 @@ function fromGregorianAstronomical(gYear, gMonth, gDay) {
226226
};
227227
}
228228

229+
230+
229231
function findNewMoon(ahar) {
230-
var getElongation = function (a) { return zero360(trueLongitudeMoon(a) - trueLongitudeSun(a)); };
231-
var lo = ahar - 2, hi = ahar + 2;
232-
var el = getElongation(lo), eh = getElongation(hi);
233-
if (el < eh) el += 360;
234-
for (var i = 0; i < 30; i++) {
232+
var getElongation = function(a) {
233+
return zero360(trueLongitudeMoon(a) - trueLongitudeSun(a));
234+
};
235+
236+
var guess = ahar;
237+
var step = 0.5;
238+
239+
for (var i = 0; i < 10; i++) {
240+
var elong = getElongation(guess);
241+
242+
if (elong < 5 || elong > 355) {
243+
break;
244+
}
245+
246+
if (elong < 180) {
247+
guess -= (elong / 360) * 29.53;
248+
} else {
249+
guess += ((360 - elong) / 360) * 29.53;
250+
}
251+
}
252+
253+
var lo = guess - 1;
254+
var hi = guess + 1;
255+
256+
for (let i = 0; i < 50; i++) {
235257
var mid = (lo + hi) / 2;
236258
var em = getElongation(mid);
237-
if (el < em) em -= 360;
238-
if (em > 0) { lo = mid; el = em; }
239-
else { hi = mid; eh = em; }
259+
260+
if (em < 180) {
261+
hi = mid;
262+
} else {
263+
lo = mid;
264+
}
240265
}
266+
241267
return (lo + hi) / 2;
242268
}
243269

244270
function calculateAdhikaMasa(ahar) {
245-
const tithi = getTithi(trueLongitudeSun(ahar), trueLongitudeMoon(ahar));
246-
const daysPerTithi = 29.530588 / 30; // ~0.9843 days
247-
const nextNewMoon = findNewMoon(ahar + (30 - tithi) * daysPerTithi);
248-
const prevNewMoon = findNewMoon(nextNewMoon - 29.6);
271+
var lunarMonthStart = findNewMoon(ahar);
272+
if (lunarMonthStart > ahar) {
273+
lunarMonthStart = findNewMoon(lunarMonthStart - 29.530588853);
274+
}
275+
276+
var lunarMonthEnd = findNewMoon(lunarMonthStart + 29.530588853);
277+
var sunLongStart = trueLongitudeSun(lunarMonthStart);
278+
var sunLongEnd = trueLongitudeSun(lunarMonthEnd);
279+
280+
var startSign = Math.floor(sunLongStart / 30);
281+
var endSign = Math.floor(sunLongEnd / 30);
282+
283+
var signCrossings = 0;
284+
var currentSign = startSign;
285+
286+
for (var i = 1; i <= 29; i++) {
287+
var checkAhar = lunarMonthStart + i;
288+
var checkSunLong = trueLongitudeSun(checkAhar);
289+
var checkSign = Math.floor(checkSunLong / 30);
290+
291+
if (checkSign < currentSign) {
292+
checkSign += 12;
293+
}
294+
295+
if (checkSign > currentSign) {
296+
signCrossings += (checkSign - currentSign);
297+
currentSign = checkSign % 12;
298+
}
299+
}
300+
301+
if (endSign < currentSign) {
302+
endSign += 12;
303+
}
304+
if (endSign > currentSign) {
305+
signCrossings += (endSign - currentSign);
306+
}
307+
308+
var result = "छैन";
309+
310+
if (signCrossings === 0) {
311+
result = "अधिक " + solarMonths[startSign]
312+
}
313+
314+
if (signCrossings >= 2) {
315+
var skippedSign = (startSign + 1) % 12;
316+
result = "क्षय " + solarMonths[skippedSign]
317+
}
249318

250-
const signAtPrev = Math.floor(trueLongitudeSun(prevNewMoon) / 30);
251-
const signAtNext = Math.floor(trueLongitudeSun(nextNewMoon) / 30);
319+
return result;
320+
}
252321

253-
if (signAtPrev === signAtNext) {
254-
return "अधिक " + solarMonths[signAtNext];
322+
function getLunarMonthNameWithAdhik(ahar) {
323+
var lunarMonthStart = findNewMoon(ahar);
324+
if (lunarMonthStart > ahar) {
325+
lunarMonthStart = findNewMoon(lunarMonthStart - 29.53);
255326
}
256327

257-
if ((signAtNext - signAtPrev + 12) % 12 === 2) {
258-
return "क्षय मास";
328+
var purnima = findNewMoon(lunarMonthStart + 14.77); // ~14.77 days after new moon
329+
330+
var sunLongPurnima = trueLongitudeSun(purnima);
331+
var purnimaSign = Math.floor(sunLongPurnima / 30);
332+
333+
var signCrossings = 0;
334+
var currentSign = Math.floor(trueLongitudeSun(lunarMonthStart) / 30);
335+
336+
for (var i = 1; i <= 29; i++) {
337+
var checkAhar = lunarMonthStart + i;
338+
var checkSunLong = trueLongitudeSun(checkAhar);
339+
var checkSign = Math.floor(checkSunLong / 30);
340+
341+
if (checkSign < currentSign) {
342+
checkSign += 12;
343+
}
344+
345+
if (checkSign > currentSign) {
346+
signCrossings += (checkSign - currentSign);
347+
currentSign = checkSign % 12;
348+
}
259349
}
260350

261-
return "छैन";
351+
var isAdhik = (signCrossings === 0);
352+
var result = {
353+
monthName: solarMonths[purnimaSign],
354+
isAdhik: isAdhik
355+
};
356+
return result;
262357
}
263358

264359
function getSunriseSunset(date, lat, lon, tz) {
@@ -608,6 +703,12 @@ function generateDebugInfo(date, lat = 27.7172, lon = 85.3240, tz = 5.75) {
608703
// Calculate difference in milliseconds
609704
var timeDiff = date.getTime() - gregFromAstronomical.getTime();
610705
dayDifference = Math.round(timeDiff / (1000 * 60 * 60 * 24));
706+
var dayDifferenceDisplay = dayDifference;
707+
if (Math.abs(dayDifference) > 2) { // Show in red if difference is more than 2 days
708+
dayDifferenceDisplay = `<font color="#ff0000">${dayDifference}</font>`;
709+
} else if (Math.abs(dayDifference) > 0) {
710+
dayDifferenceDisplay = `<font color="yellow">${dayDifference}</font>`;
711+
}
611712
var sunriseSunset = getSunriseSunset(date, lat, lon, tz);
612713
var isComputed = (bsInfoData.year < Bsdata.BS_START_YEAR || bsInfoData.year > Bsdata.BS_END_YEAR);
613714

@@ -623,13 +724,23 @@ Data-Driven BS Date: Not available (using astronomical calculation)
623724
`;
624725
}
625726

727+
var lunarMonthInfo = getLunarMonthNameWithAdhik(ahar);
728+
var lunarMonthDisplay;
729+
if (lunarMonthInfo.isAdhik) {
730+
lunarMonthDisplay = "अधिक " + lunarMonthInfo.monthName + " " + paksha;
731+
} else {
732+
lunarMonthDisplay = lunarMonthInfo.monthName + " " + paksha;
733+
}
734+
626735
var debugOutput = `
736+
<pre style="font-family: monospace; font-size: 9pt; color: white; white-space: pre; line-height: 1.2;">
627737
${dataDrivenInfo}Consistency Check:
628738
Data-Driven BS Date: ${bsInfoData ? `${toDevanagari(bsInfoData.year)} ${bsInfoData.monthName} ${toDevanagari(bsInfoData.day)}` : 'N/A'}
629739
Astronomical BS Date (Corrected): ${toDevanagari(bsInfoCalc.year)} ${bsInfoCalc.monthName} ${toDevanagari(bsInfoCalc.day)}
630-
Day Difference: ${dayDifference} ${Math.abs(dayDifference) === 1 ? 'day' : 'days'}
631-
(Note: Positive = Astronomical date is behind; Negative = Astronomical date is ahead)
632-
740+
Day Difference: ${dayDifferenceDisplay} ${Math.abs(dayDifference) === 1 ? 'day' : 'days'}
741+
<span style="margin: 0; color: yellow; padding: 0;">lunar Month(testing): ${lunarMonthDisplay}</span>
742+
<span style="margin: 0; color: red; padding: 0;">Note: Positive = Astronomical date is behind;
743+
Negative = Astronomical date is ahead)</span>
633744
Detailed Astronomical Calculation:
634745
gregorianDate: ${Qt.formatDateTime(date, "dddd, MMMM d, yyyy")}
635746
bsMonthIndex(0 based index): ${bsInfoCalc.monthIndex}
@@ -646,8 +757,9 @@ karana: ${karanaName} | index: ${karanaIdx}
646757
karanaAngle: ${(2 * tithiVal).toFixed(4)}
647758
sunRashi: ${rashis[Math.floor(sunLong / 30) % 12]} | index: ${Math.floor(sunLong / 30) % 12 + 1}
648759
moonRashi: ${rashis[Math.floor(moonLong / 30) % 12]} | index: ${Math.floor(moonLong / 30) % 12 + 1}
649-
adhikaMasa: ${calculateAdhikaMasa(ahar)}
760+
adhikaMasa: ${calculateAdhikaMasa(ahar)}(approximation)
650761
isComputed: ${isComputed}
762+
</pre>
651763
`.trim();
652764

653765
var result = { debug: debugOutput.replace(/^\s*[\r\n]/gm, "") };

0 commit comments

Comments
 (0)