0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

指定期間の日の出、日の入データをPythonのastralライブラリを使用して取得する

Last updated at Posted at 2024-08-04

以下にコードを添付します。
実行前にastralライブラリをインストールしてください。

下記のページと比較することでデータに差異がないか確認できます。

国立天文台 日の出入り

日の入り時刻.py
from astral import LocationInfo
from astral.sun import sun
from datetime import datetime, timedelta, timezone

def get_sunrise_sunset(lat, lon, start_date, end_date):
    location = LocationInfo(latitude=lat, longitude=lon)
    current_date = start_date
    sun_times = []
    
    while current_date <= end_date:
        s = sun(location.observer, date=current_date)
        # 時間の形式を変更と日本標準時に変更
        jst = timezone(timedelta(hours=9))
        sunrise = s['sunrise'].astimezone(jst).strftime('%Y-%m-%d %H:%M:%S')
        sunset = s['sunset'].astimezone(jst).strftime('%Y-%m-%d %H:%M:%S')
        sun_times.append((current_date, sunrise, sunset))
        current_date += timedelta(days=1)
    
    return sun_times

# 名古屋市の緯度経度を指定
latitude = 35.1667
longitude = 136.9167

# 開始日と終了日を指定
start_date = datetime(2017, 1, 1)
end_date = datetime(2017, 1, 31)

sun_times = get_sunrise_sunset(latitude, longitude, start_date, end_date)

for date, sunrise, sunset in sun_times:
    print(f"日付: {date.date()}, 日の出: {sunrise}, 日の入り: {sunset}")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?