# -*- python -*-
# This example can be used in xmobar to display the
# Sun's elevation and to what degree it is day time.
# Copyright © 2014, 2015, 2016, 2017 Mattias Andrée (m@maandree.se)
#
# Permission is granted to copy, distribute and/or modify this document
# under the terms of the GNU Free Documentation License, Version 1.3
# or any later version published by the Free Software Foundation;
# with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
# You should have received a copy of the GNU General Public License
# along with this software package. If not, see <http://www.gnu.org/licenses/>.
# Geographical coodinates.
# ("Kristall, vertikal accent i glas och stål" (Crystal, vertical accent
# in glass and steal) in this example. A glass obelisk, lit from the inside
# with adjustable colours and a default colour of 5600 K, in the middle
# of a hyperelliptic roundabout.)
latitude, longitude = 59.3326, 18.0652
# The colour temperature at day and at night.
temperature_day, temperature_night = 6500, 3700
# Get current time.
now = epoch()
now_m = now - 1
now_p = now + 1
now_d = now_p - now_m
now = epoch_to_julian_centuries(now)
now_m = epoch_to_julian_centuries(now_m)
now_p = epoch_to_julian_centuries(now_p)
# The visibility of the Sun.
dayness = sun(latitude, longitude, now)
dayness_m = sun(latitude, longitude, now_m)
dayness_p = sun(latitude, longitude, now_p)
dayness_d = (dayness_p - dayness_m) / now_d
# The Sun's elevation.
elevation = solar_elevation(latitude, longitude, now)
elevation_m = solar_elevation(latitude, longitude, now_m)
elevation_p = solar_elevation(latitude, longitude, now_p)
elevation_d = (elevation_p - elevation_m) / now_d
# Calculation of the colour temperature.
temperature = temperature_day * dayness + temperature_night * (1 - dayness)
temperature_m = temperature_day * dayness_m + temperature_night * (1 - dayness_m)
temperature_p = temperature_day * dayness_p + temperature_night * (1 - dayness_p)
temperature_d = (temperature_p - temperature_m) / now_d
# Calculate the whitepoint with adjusted colour temperature.
whitepoint = divide_by_maximum(cmf_10deg(temperature))
whitepoint = [('0' + hex(int(c * 255 + 0.5))[2:].upper())[-2:] for c in whitepoint]
whitepoint = whitepoint[0] + whitepoint[1] + whitepoint[2]
# Convert derivative to per minutes from per seconds.
temperature_d *= 60
elevation_d *= 60
dayness_d *= 60
# Print information.
dayness *= 100
dayness_d *= 100
output = '<fc=#%s>%0.f</fc>K %.1fK/min %.1f° %.2f°/min %.0f%% %.1f%%/min'
output %= (whitepoint, temperature, temperature_d, elevation, elevation_d, dayness, dayness_d)
print(output)