Skip to content

Sunrise/Sunset Calculation Returns None for Certain Date/Location Combinations #25

@laksh-krishna-sharma

Description

@laksh-krishna-sharma

Description

The jyotishganit library fails to calculate sunrise/sunset times for certain date/location combinations, returning None values. This causes a TypeError when the library attempts to compare None with float in the compute_nathonnatabala function during shadbala calculations.

Error Details

Error Type

TypeError: '<=' not supported between instances of 'NoneType' and 'float'

Error Location

File "/opt/venv/lib/python3.11/site-packages/jyotishganit/components/strengths.py", line 498, in compute_nathonnatabala
    is_day_birth = sunrise <= birth_hour < sunset
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<=' not supported between instances of 'NoneType' and 'float'

Full Traceback

Traceback (most recent call last):
  File "/app/src/services/shadbala_services.py", line 200, in calculate_shadbala
    chart = await to_thread_with_context(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/jyotishganit/main.py", line 86, in calculate_birth_chart
    strengths.calculate_all_strengths(d1_chart, person)
  File "/opt/venv/lib/python3.11/site-packages/jyotishganit/components/strengths.py", line 176, in calculate_all_strengths
    compute_shadbala(chart, person)
  File "/opt/venv/lib/python3.11/site-packages/jyotishganit/components/strengths.py", line 187, in compute_shadbala
    compute_kaalabala(chart, person)
  File "/opt/venv/lib/python3.11/site-packages/jyotishganit/components/strengths.py", line 479, in compute_kaalabala
    compute_nathonnatabala(chart, person)
  File "/opt/venv/lib/python3.11/site-packages/jyotishganit/components/strengths.py", line 498, in compute_nathonnatabala
    is_day_birth = sunrise <= birth_hour < sunset
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<=' not supported between instances of 'NoneType' and 'float'

Steps to Reproduce

  1. Install jyotishganit library (tested with version >=0.1.2)
  2. Call calculate_birth_chart with the following parameters:
from datetime import datetime
from jyotishganit import calculate_birth_chart

birth_date = datetime(2006, 7, 8, 22, 30, 0)
latitude = 28.643364
longitude = 77.115472
timezone_offset = 5.5
name = "Test Person"

chart = calculate_birth_chart(
    birth_date=birth_date,
    latitude=latitude,
    longitude=longitude,
    timezone_offset=timezone_offset,
    name=name,
)
  1. The error occurs during the shadbala calculation phase

Expected Behavior

The library should:

  1. Successfully calculate sunrise/sunset times for the given date/location
  2. If calculation fails, handle the None values gracefully instead of crashing
  3. Provide a meaningful error message if sunrise/sunset cannot be calculated

Actual Behavior

  1. Sunrise/sunset calculation returns None for this specific date/location combination
  2. The library crashes with TypeError when trying to compare None with float
  3. No graceful error handling for this edge case

Environment

  • Python Version: 3.11
  • jyotishganit Version: >=0.1.2
  • Operating System: Linux (tested in Docker container)
  • Location: Delhi, India (28.643364°N, 77.115472°E)
  • Date: July 8, 2006
  • Timezone: UTC+5.5 (IST)

Additional Context

Observations

  1. Date-Specific Issue: The same location works fine for July 9, 2006, suggesting this is a date-specific calculation issue
  2. Location: Delhi, India is not a polar region, so this shouldn't be a polar day/night issue
  3. Library Dependency: The library appears to use skyfield/ephemeris data (de421.bsp file is downloaded)
  4. Impact: This affects all calculations that require shadbala (kaalabala component specifically)

Workaround

Currently, we're handling this by:

  1. Catching the TypeError and providing a user-friendly error message
  2. Pre-validating sunrise/sunset calculation using the astral library before calling jyotishganit
  3. Suggesting users try a different date if the exact date is not critical

However, this is not ideal as:

  • Users cannot get calculations for their actual birth date
  • The library should handle this gracefully internally

Suggested Fix

  1. Immediate Fix: Add null checks in compute_nathonnatabala before comparing sunrise/sunset values:

    if sunrise is None or sunset is None:
        # Handle gracefully - either skip nathonnatabala calculation
        # or use a fallback method to determine day/night
        logger.warning("Sunrise/sunset calculation failed, skipping nathonnatabala")
        return
    is_day_birth = sunrise <= birth_hour < sunset
  2. Long-term Fix: Investigate why sunrise/sunset calculation returns None for certain dates and fix the underlying calculation

  3. Better Error Handling: Provide meaningful error messages when astronomical calculations fail

Related Information

  • The issue occurs specifically when calculating shadbala, which requires kaalabala
  • Kaalabala includes nathonnatabala, which needs sunrise/sunset times
  • Other calculations (like panchanga, ashtakavarga) may also be affected if they trigger shadbala calculation

Verification

We've verified that:

  • ✅ The same location works for July 9, 2006 (next day)
  • ✅ The coordinates are valid (Delhi, India)
  • ✅ The timezone offset is correct (5.5 for IST)
  • ✅ The date is valid (July 8, 2006 exists)
  • ✅ Using alternative libraries (astral) can calculate sunrise/sunset for this date

This suggests the issue is in jyotishganit's internal sunrise/sunset calculation logic, not in the input parameters.


Note: This issue prevents users from getting accurate astrological calculations for their actual birth dates when the library encounters this bug. A fix would greatly improve the library's reliability and user experience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions