The GIS Professional Group

日の出・日の入時刻の計算

# 日の出・日の入時間の計算
# http://www.sci-museum.kita.osaka.jp/~egoshi/astronomy/python/python_moon.html

import ephem
import datetime
import pytz

# 定義
time_zone = 9
lon = '139.8090057'
lat = '35.682998'
date = '2023-01-01' # 現地日
horizon = '0:00'    # 水平線角度 (D:M:S)

# UTC 時刻へ変換
local = pytz.timezone("Asia/Tokyo")
naive = datetime.datetime.strptime(date, "%Y-%m-%d")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)

obs = ephem.Observer()
obs.lat = ephem.degrees(lat)
obs.lon = ephem.degrees(lon)
obs.date = ephem.Date(utc_dt)
obs.horizon = horizon

# 太陽
sun = ephem.Sun(obs)    
sun_rise = obs.next_rising(sun)
sun_set = obs.next_setting(sun)

# 月
moon = ephem.Moon(obs) 
moon_rise = obs.next_rising(moon)
moon_set = obs.next_setting(moon)

print("経度 (DMS) =\t",obs.lon)
print("緯度 (DMS) =\t", obs.lat)
print("水平線角度 =\t", obs.horizon)
print("日時 (現地) =\t", date)
print('日の出 (現地) :\t', ephem.localtime(sun_rise))
print('日の入 (現地) :\t', ephem.localtime(sun_set))
print('月の出 (現地) :\t', ephem.localtime(moon_rise))
print('月の入 (現地) :\t', ephem.localtime(moon_set))
  • B!