/usr/share/pyshared/advancedcaching/astral.py is in agtl 0.8.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | # -*- coding: utf-8 -*-
#
# Copyright 2009-2010, Simon Kennedy, python@sffjunkie.co.uk
# Distributed under the terms of the MIT License.
#
# Shortened for AGTL by Daniel Fett
import datetime
from math import cos, sin, tan, acos, asin, atan2, floor, radians, degrees
SUN_POSITION_CACHE_DURATION = 3600 # seconds
class Astral(object):
sun_cache_result = None
sun_cache_time = None
instance = None
def __init__(self):
"""Initialise the list of cities.
"""
self._depression = 6 # Set default depression in degrees
def solar_depression():
doc = """The number of degrees the sun must be below the horizon for the dawn/dusk calc.
Can either be set as a number of degrees below the horizon or as
one of the following strings
============= =======
String Degrees
============= =======
civil 6.0
nautical 12.0
astronomical 18.0
============= =======
"""
def fget(self):
return self._depression
def fset(self, depression):
if isinstance(depression, basestring):
try:
self._depression = {'civil': 6, 'nautical': 12, 'astronomical': 18}[depression]
except:
raise KeyError("solar_depression must be either a number or one of 'civil', 'nautical' or 'astronomical'")
else:
self._depression = float(depression)
return locals()
solar_depression = property(**solar_depression())
def sun_utc(self, date, latitude, longitude):
"""Returns dawn, sunrise, noon, sunset and dusk times as a dictionary.
"""
dawn = self.dawn_utc(date, latitude, longitude)
sunrise = self.sunrise_utc(date, latitude, longitude)
noon = self.solar_noon_utc(date, longitude)
sunset = self.sunset_utc(date, latitude, longitude)
dusk = self.dusk_utc(date, latitude, longitude)
return {'dawn': dawn, 'sunrise': sunrise, 'noon': noon, 'sunset': sunset, 'dusk': dusk}
def dawn_utc(self, date, latitude, longitude):
"""Calculate dawn time for a specific date at a particular position.
Returns date/time in UTC
"""
julianday = self._julianday(date.day, date.month, date.year)
if latitude > 89.8:
latitude = 89.8
if latitude < -89.8:
latitude = -89.8
t = self._jday_to_jcentury(julianday)
eqtime = self._eq_of_time(t)
solarDec = self._sun_declination(t)
try:
hourangle = self._hour_angle_sunrise(latitude, solarDec)
except:
raise AstralError('Sun remains below horizon on this day, at this location.')
delta = longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720.0 + timeDiff - eqtime
newt = self._jday_to_jcentury(self._jcentury_to_jday(t) + timeUTC / 1440.0)
eqtime = self._eq_of_time(newt)
solarDec = self._sun_declination(newt)
hourangle = self._hour_angle_dawn(latitude, solarDec, self._depression)
delta = longitude - degrees(hourangle)
timeDiff = 4 * delta
timeUTC = 720 + timeDiff - eqtime
timeUTC = timeUTC/60.0
hour = int(timeUTC)
minute = int((timeUTC - hour) * 60)
second = int((((timeUTC - hour) * 60) - minute) * 60)
if hour > 23.0:
hour -= 24
date += datetime.timedelta(days=1)
dawn = datetime.datetime(date.year, date.month, date.day, hour, minute, second)
return dawn
def sunrise_utc(self, date, latitude, longitude):
"""Calculate sunrise time for a specific date at a particular position.
Returns date/time in UTC
"""
julianday = self._julianday(date.day, date.month, date.year)
t = self._jday_to_jcentury(julianday)
eqtime = self._eq_of_time(t)
solarDec = self._sun_declination(t)
try:
hourangle = self._hour_angle_sunrise(latitude, solarDec)
except:
raise AstralError('Sun remains below horizon on this day, at this location.')
delta = longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720.0 + timeDiff - eqtime
newt = self._jday_to_jcentury(self._jcentury_to_jday(t) + timeUTC / 1440.0)
eqtime = self._eq_of_time(newt)
solarDec = self._sun_declination(newt)
hourangle = self._hour_angle_sunrise(latitude, solarDec)
delta = longitude - degrees(hourangle)
timeDiff = 4 * delta
timeUTC = 720 + timeDiff - eqtime
timeUTC = timeUTC/60.0
hour = int(timeUTC)
minute = int((timeUTC - hour) * 60)
second = int((((timeUTC - hour) * 60) - minute) * 60)
if hour > 23:
hour -= 24
date += datetime.timedelta(days=1)
sunrise = datetime.datetime(date.year, date.month, date.day, hour, minute, second)
return sunrise
def solar_noon_utc(self, date, longitude):
"""Calculate solar noon time for a specific date at a particular position.
Returns date/time in UTC
"""
julianday = self._julianday(date.day, date.month, date.year)
newt = self._jday_to_jcentury(julianday + 0.5 + longitude / 360.0)
eqtime = self._eq_of_time(newt)
solarNoonDec = self._sun_declination(newt)
timeUTC = 720.0 + (longitude * 4.0) - eqtime
timeUTC = timeUTC/60.0
hour = int(timeUTC)
minute = int((timeUTC - hour) * 60)
second = int((((timeUTC - hour) * 60) - minute) * 60)
if hour > 23:
hour -= 24
date += datetime.timedelta(days=1)
noon = datetime.datetime(date.year, date.month, date.day, hour, minute, second)
return noon
def sunset_utc(self, date, latitude, longitude):
"""Calculate sunset time for a specific date at a particular position.
Returns date/time in UTC
"""
julianday = self._julianday(date.day, date.month, date.year)
t = self._jday_to_jcentury(julianday)
eqtime = self._eq_of_time(t)
solarDec = self._sun_declination(t)
try:
hourangle = self._hour_angle_sunset(latitude, solarDec)
except:
raise AstralError('Sun remains below horizon on this day, at this location.')
delta = longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720.0 + timeDiff - eqtime
newt = self._jday_to_jcentury(self._jcentury_to_jday(t) + timeUTC / 1440.0)
eqtime = self._eq_of_time(newt)
solarDec = self._sun_declination(newt)
hourangle = self._hour_angle_sunset(latitude, solarDec)
delta = longitude - degrees(hourangle)
timeDiff = 4 * delta
timeUTC = 720 + timeDiff - eqtime
timeUTC = timeUTC/60.0
hour = int(timeUTC)
minute = int((timeUTC - hour) * 60)
second = int((((timeUTC - hour) * 60) - minute) * 60)
if hour > 23:
hour -= 24
date += datetime.timedelta(days=1)
sunset = datetime.datetime(date.year, date.month, date.day, hour, minute, second)
return sunset
def dusk_utc(self, date, latitude, longitude):
"""Calculate dusk time for a specific date at a particular position.
Returns date/time in UTC
"""
julianday = self._julianday(date.day, date.month, date.year)
if latitude > 89.8:
latitude = 89.8
if latitude < -89.8:
latitude = -89.8
t = self._jday_to_jcentury(julianday)
eqtime = self._eq_of_time(t)
solarDec = self._sun_declination(t)
try:
hourangle = self._hour_angle_sunset(latitude, solarDec)
except:
raise AstralError('Sun remains below horizon on this day, at this location.')
delta = longitude - degrees(hourangle)
timeDiff = 4.0 * delta
timeUTC = 720.0 + timeDiff - eqtime
newt = self._jday_to_jcentury(self._jcentury_to_jday(t) + timeUTC / 1440.0)
eqtime = self._eq_of_time(newt)
solarDec = self._sun_declination(newt)
hourangle = self._hour_angle_dusk(latitude, solarDec, self._depression)
delta = longitude - degrees(hourangle)
timeDiff = 4 * delta
timeUTC = 720 + timeDiff - eqtime
timeUTC = timeUTC/60.0
hour = int(timeUTC)
minute = int((timeUTC - hour) * 60)
second = int((((timeUTC - hour) * 60) - minute) * 60)
if hour > 23:
hour -= 24
date += datetime.timedelta(days=1)
dusk = datetime.datetime(date.year, date.month, date.day, hour, minute, second)
return dusk
def solar_azimuth(self, dateandtime, latitude, longitude):
"""Calculate the azimuth of the sun as a specific date/time and location.
"""
if latitude > 89.8:
latitude = 89.8
if latitude < -89.8:
latitude = -89.8
zone = 0#-dateandtime.utcoffset().seconds / 3600.0
utc_datetime = dateandtime #dateandtime.astimezone(pytz.utc)
timenow = utc_datetime.hour + (utc_datetime.minute / 60.0) + (utc_datetime.second / 3600)
JD = self._julianday(dateandtime.day, dateandtime.month, dateandtime.year)
t = self._jday_to_jcentury(JD + timenow / 24.0)
R = self._sun_rad_vector(t)
alpha = self._sun_rt_ascension(t)
theta = self._sun_declination(t)
Etime = self._eq_of_time(t)
eqtime = Etime
solarDec = theta # in degrees
earthRadVec = R
solarTimeFix = eqtime - (4.0 * longitude) + (60 * zone)
trueSolarTime = dateandtime.hour * 60.0 + dateandtime.minute + dateandtime.second / 60.0 + solarTimeFix
# in minutes
while trueSolarTime > 1440:
trueSolarTime = trueSolarTime - 1440
hourangle = trueSolarTime / 4.0 - 180.0
# Thanks to Louis Schwarzmayr for the next line:
if hourangle < -180:
hourangle = hourangle + 360.0
harad = radians(hourangle)
csz = sin(radians(latitude)) * sin(radians(solarDec)) + \
cos(radians(latitude)) * cos(radians(solarDec)) * cos(harad)
if csz > 1.0:
csz = 1.0
elif csz < -1.0:
csz = -1.0
zenith = degrees(acos(csz))
azDenom = (cos(radians(latitude)) * sin(radians(zenith)))
if (abs(azDenom) > 0.001):
azRad = ((sin(radians(latitude)) * cos(radians(zenith))) - sin(radians(solarDec))) / azDenom
if abs(azRad) > 1.0:
if azRad < 0:
azRad = -1.0
else:
azRad = 1.0
azimuth = 180.0 - degrees(acos(azRad))
if hourangle > 0.0:
azimuth = -azimuth
else:
if latitude > 0.0:
azimuth = 180.0
else:
azimuth = 0#
if azimuth < 0.0:
azimuth = azimuth + 360.0
return azimuth
def solar_elevation(self, dateandtime, latitude, longitude):
"""Calculate the elevation of the sun as a specific date/time and location.
"""
if latitude > 89.8:
latitude = 89.8
if latitude < -89.8:
latitude = -89.8
zone = -dateandtime.utcoffset().seconds / 3600.0
utc_datetime = dateandtime.astimezone(pytz.utc)
timenow = utc_datetime.hour + (utc_datetime.minute / 60.0) + (utc_datetime.second / 3600)
JD = self._julianday(dateandtime.day, dateandtime.month, dateandtime.year)
t = self._jday_to_jcentury(JD + timenow / 24.0)
R = self._sun_rad_vector(t)
alpha = self._sun_rt_ascension(t)
theta = self._sun_declination(t)
Etime = self._eq_of_time(t)
eqtime = Etime
solarDec = theta # in degrees
earthRadVec = R
solarTimeFix = eqtime - (4.0 * longitude) + (60 * zone)
trueSolarTime = dateandtime.hour * 60.0 + dateandtime.minute + dateandtime.second / 60.0 + solarTimeFix
# in minutes
while trueSolarTime > 1440:
trueSolarTime = trueSolarTime - 1440
hourangle = trueSolarTime / 4.0 - 180.0
# Thanks to Louis Schwarzmayr for the next line:
if hourangle < -180:
hourangle = hourangle + 360.0
harad = radians(hourangle)
csz = sin(radians(latitude)) * sin(radians(solarDec)) + \
cos(radians(latitude)) * cos(radians(solarDec)) * cos(harad)
if csz > 1.0:
csz = 1.0
elif csz < -1.0:
csz = -1.0
zenith = degrees(acos(csz))
azDenom = (cos(radians(latitude)) * sin(radians(zenith)))
if (abs(azDenom) > 0.001):
azRad = ((sin(radians(latitude)) * cos(radians(zenith))) - sin(radians(solarDec))) / azDenom
if abs(azRad) > 1.0:
if azRad < 0:
azRad = -1.0
else:
azRad = 1.0
azimuth = 180.0 - degrees(acos(azRad))
if hourangle > 0.0:
azimuth = -azimuth
else:
if latitude > 0.0:
azimuth = 180.0
else:
azimuth = 0#
if azimuth < 0.0:
azimuth = azimuth + 360.0
exoatmElevation = 90.0 - zenith
if exoatmElevation > 85.0:
refractionCorrection = 0.0
else:
te = tan(radians(exoatmElevation))
if exoatmElevation > 5.0:
refractionCorrection = 58.1 / te - 0.07 / (te * te * te) + 0.000086 / (te * te * te * te * te)
elif exoatmElevation > -0.575:
step1 = (-12.79 + exoatmElevation * 0.711)
step2 = (103.4 + exoatmElevation * (step1))
step3 = (-518.2 + exoatmElevation * (step2))
refractionCorrection = 1735.0 + exoatmElevation * (step3)
else:
refractionCorrection = -20.774 / te
refractionCorrection = refractionCorrection / 3600.0
solarzen = zenith - refractionCorrection
solarelevation = 90.0 - solarzen
return solarelevation
def _julianday(self, day, month, year):
if month <= 2:
year = year - 1
month = month + 12
A = floor(year / 100.0)
B = 2 - A + floor(A / 4.0)
return floor(365.25 * (year + 4716)) + floor(30.6001 * (month + 1)) + day + B - 1524.5
def _jday_to_jcentury(self, julianday):
return (julianday - 2451545.0) / 36525.0
def _jcentury_to_jday(self, juliancentury):
return (juliancentury * 36525.0) + 2451545.0
def _mean_obliquity_of_ecliptic(self, juliancentury):
seconds = 21.448 - juliancentury * (46.815 + juliancentury * (0.00059 - juliancentury * (0.001813)))
return 23.0 + (26.0 + (seconds / 60.0)) / 60.0
def _obliquity_correction(self, juliancentury):
e0 = self._mean_obliquity_of_ecliptic(juliancentury)
omega = 125.04 - 1934.136 * juliancentury
return e0 + 0.00256 * cos(radians(omega))
def _geom_mean_long_sun(self, juliancentury):
l0 = 280.46646 + juliancentury * (36000.76983 + 0.0003032 * juliancentury)
return l0 % 360.0
def _eccentricity_earth_orbit(self, juliancentury):
return 0.016708634 - juliancentury * (0.000042037 + 0.0000001267 * juliancentury)
def _geom_mean_anomaly_sun(self, juliancentury):
return 357.52911 + juliancentury * (35999.05029 - 0.0001537 * juliancentury)
def _eq_of_time(self, juliancentury):
epsilon = self._obliquity_correction(juliancentury)
l0 = self._geom_mean_long_sun(juliancentury)
e = self._eccentricity_earth_orbit(juliancentury)
m = self._geom_mean_anomaly_sun(juliancentury)
y = tan(radians(epsilon) / 2.0)
y = y * y
sin2l0 = sin(2.0 * radians(l0))
sinm = sin(radians(m))
cos2l0 = cos(2.0 * radians(l0))
sin4l0 = sin(4.0 * radians(l0))
sin2m = sin(2.0 * radians(m))
Etime = y * sin2l0 - 2.0 * e * sinm + 4.0 * e * y * sinm * cos2l0 - \
0.5 * y * y * sin4l0 - 1.25 * e * e * sin2m
return degrees(Etime) * 4.0
def _sun_eq_of_center(self, juliancentury):
m = self._geom_mean_anomaly_sun(juliancentury)
mrad = radians(m)
sinm = sin(mrad)
sin2m = sin(mrad + mrad)
sin3m = sin(mrad + mrad + mrad)
c = sinm * (1.914602 - juliancentury * (0.004817 + 0.000014 * juliancentury)) + \
sin2m * (0.019993 - 0.000101 * juliancentury) + sin3m * 0.000289
return c
def _sun_true_long(self, juliancentury):
l0 = self._geom_mean_long_sun(juliancentury)
c = self._sun_eq_of_center(juliancentury)
return l0 + c
def _sun_apparent_long(self, juliancentury):
O = self._sun_true_long(juliancentury)
omega = 125.04 - 1934.136 * juliancentury
return O - 0.00569 - 0.00478 * sin(radians(omega))
def _sun_declination(self, juliancentury):
e = self._obliquity_correction(juliancentury)
lambd = self._sun_apparent_long(juliancentury)
sint = sin(radians(e)) * sin(radians(lambd))
return degrees(asin(sint))
def _hour_angle(self, latitude, solar_dec, solar_depression):
latRad = radians(latitude)
sdRad = radians(solar_dec)
HA = (acos(cos(radians(90 + solar_depression)) / (cos(latRad) * cos(sdRad)) - tan(latRad) * tan(sdRad)))
return HA
def _hour_angle_sunrise(self, latitude, solar_dec):
return self._hour_angle(latitude, solar_dec, 0.833)
def _hour_angle_sunset(self, latitude, solar_dec):
return -self._hour_angle(latitude, solar_dec, 0.833)
def _hour_angle_dawn(self, latitude, solar_dec, solar_depression):
return self._hour_angle(latitude, solar_dec, solar_depression)
def _hour_angle_dusk(self, latitude, solar_dec, solar_depression):
return -self._hour_angle(latitude, solar_dec, solar_depression)
def _sun_true_anomoly(self, juliancentury):
m = self._geom_mean_anomaly_sun(juliancentury)
c = self._sun_eq_of_center(juliancentury)
return m + c
def _sun_rad_vector(self, juliancentury):
v = self._sun_true_anomoly(juliancentury)
e = self._eccentricity_earth_orbit(juliancentury)
return (1.000001018 * (1 - e * e)) / (1 + e * cos(radians(v)))
def _sun_rt_ascension(self, juliancentury):
e = self._obliquity_correction(juliancentury)
lambd = self._sun_apparent_long(juliancentury)
tananum = (cos(radians(e)) * sin(radians(lambd)))
tanadenom = (cos(radians(lambd)))
return degrees(atan2(tananum, tanadenom))
def get_sun_azimuth_from_fix(self, fix):
if self.sun_cache_time == None or abs((fix.timestamp - self.sun_cache_time).seconds) > SUN_POSITION_CACHE_DURATION:
self.sun_cache_time = fix.timestamp
sunrise = self.sunrise_utc(fix.timestamp, fix.position.lat, -fix.position.lon)
sunset = self.sunset_utc(fix.timestamp, fix.position.lat, -fix.position.lon)
if fix.timestamp > sunrise and fix.timestamp < sunset:
self.sun_cache_result = self.solar_azimuth(fix.timestamp, fix.position.lat, -fix.position.lon)
else:
self.sun_cache_result = None
return self.sun_cache_result
|