<filename>lighting_replay_app/analysis/lux_time_utils.py
"""
Time utilities for lux analysis and replay
"""

# Sunrise sunset data for Sunnyvale, CA, 2016.
# From http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl?ID=AA&year=2016&task=0&state=CA&place=Sunnyvale
# Eventually use ephem (https://pypi.python.org/pypi/pyephem/) to
# compute the sunrise/sunset.
sunrise_sunset_data =\
"""01  0722 1701  0712 1732  0638 1803  0553 1831  0512 1858  0449 1924  0451 1933  0513 1915  0539 1836  0604 1750  0633 1709  0704 1650
02  0723 1702  0711 1733  0637 1804  0551 1832  0511 1859  0449 1924  0452 1933  0514 1914  0540 1835  0605 1749  0635 1708  0705 1650
03  0723 1703  0710 1734  0636 1805  0550 1833  0510 1900  0448 1925  0452 1932  0515 1913  0541 1833  0606 1747  0636 1707  0706 1650
04  0723 1703  0709 1735  0634 1806  0548 1834  0509 1901  0448 1925  0453 1932  0516 1912  0542 1832  0607 1746  0637 1706  0707 1650
05  0723 1704  0708 1737  0633 1807  0547 1835  0508 1902  0448 1926  0453 1932  0516 1911  0542 1830  0608 1744  0638 1705  0708 1650
06  0723 1705  0707 1738  0631 1808  0545 1836  0507 1903  0448 1927  0454 1932  0517 1910  0543 1829  0608 1743  0639 1704  0709 1650
07  0723 1706  0706 1739  0630 1808  0544 1837  0506 1904  0447 1927  0455 1931  0518 1909  0544 1827  0609 1742  0640 1704  0710 1650
08  0723 1707  0705 1740  0629 1809  0543 1838  0505 1905  0447 1928  0455 1931  0519 1908  0545 1826  0610 1740  0641 1703  0710 1650
09  0723 1708  0704 1741  0627 1810  0541 1839  0504 1906  0447 1928  0456 1931  0520 1907  0546 1824  0611 1739  0642 1702  0711 1650
10  0723 1709  0703 1742  0626 1811  0540 1839  0503 1906  0447 1929  0456 1930  0521 1905  0547 1822  0612 1737  0643 1701  0712 1650
11  0722 1710  0702 1743  0624 1812  0538 1840  0502 1907  0447 1929  0457 1930  0521 1904  0547 1821  0613 1736  0644 1700  0713 1651
12  0722 1711  0701 1744  0623 1813  0537 1841  0501 1908  0447 1930  0458 1930  0522 1903  0548 1819  0614 1734  0645 1659  0713 1651
13  0722 1712  0700 1745  0621 1814  0535 1842  0500 1909  0447 1930  0458 1929  0523 1902  0549 1818  0615 1733  0646 1659  0714 1651
14  0722 1713  0659 1746  0620 1815  0534 1843  0500 1910  0447 1930  0459 1929  0524 1901  0550 1816  0616 1732  0647 1658  0715 1651
15  0721 1714  0658 1747  0618 1816  0533 1844  0459 1911  0447 1931  0500 1928  0525 1859  0551 1815  0617 1730  0648 1657  0716 1652
16  0721 1715  0656 1748  0617 1817  0531 1845  0458 1912  0447 1931  0501 1928  0526 1858  0551 1813  0618 1729  0649 1657  0716 1652
17  0721 1716  0655 1749  0615 1818  0530 1846  0457 1912  0447 1931  0501 1927  0527 1857  0552 1812  0619 1728  0650 1656  0717 1652
18  0720 1717  0654 1751  0614 1819  0529 1847  0456 1913  0447 1932  0502 1926  0527 1856  0553 1810  0620 1726  0651 1655  0717 1653
19  0720 1718  0653 1752  0612 1820  0527 1848  0456 1914  0447 1932  0503 1926  0528 1854  0554 1809  0620 1725  0652 1655  0718 1653
20  0719 1719  0652 1753  0611 1821  0526 1848  0455 1915  0448 1932  0503 1925  0529 1853  0555 1807  0621 1724  0653 1654  0718 1654
21  0719 1720  0650 1754  0609 1821  0525 1849  0454 1916  0448 1932  0504 1924  0530 1852  0556 1806  0622 1722  0654 1654  0719 1654
22  0718 1721  0649 1755  0608 1822  0523 1850  0454 1916  0448 1932  0505 1924  0531 1850  0556 1804  0623 1721  0655 1653  0719 1655
23  0718 1722  0648 1756  0606 1823  0522 1851  0453 1917  0448 1933  0506 1923  0532 1849  0557 1803  0624 1720  0656 1653  0720 1655
24  0717 1723  0647 1757  0605 1824  0521 1852  0453 1918  0449 1933  0507 1922  0532 1847  0558 1801  0625 1719  0657 1652  0720 1656
25  0717 1724  0645 1758  0603 1825  0520 1853  0452 1919  0449 1933  0507 1921  0533 1846  0559 1759  0626 1717  0658 1652  0721 1657
26  0716 1726  0644 1759  0602 1826  0518 1854  0451 1919  0449 1933  0508 1921  0534 1845  0600 1758  0627 1716  0659 1652  0721 1657
27  0715 1727  0643 1800  0600 1827  0517 1855  0451 1920  0450 1933  0509 1920  0535 1843  0601 1756  0628 1715  0700 1651  0721 1658
28  0715 1728  0641 1801  0559 1828  0516 1856  0450 1921  0450 1933  0510 1919  0536 1842  0602 1755  0629 1714  0701 1651  0722 1659
29  0714 1729  0640 1802  0557 1829  0515 1857  0450 1922  0451 1933  0511 1918  0537 1840  0602 1753  0630 1713  0702 1651  0722 1659
30  0713 1730             0556 1830  0514 1857  0450 1922  0451 1933  0511 1917  0537 1839  0603 1752  0631 1712  0703 1651  0722 1700
31  0712 1731             0554 1830             0449 1923             0512 1916  0538 1837             0632 1711             0722 1701"""

DST_START=(3,13)
DST_END=(11,6)
SUNRISE_SUNSET_TABLE=[[None for d in range(31)] for m in range(12)]
def parse_sunrise_sunset_table():
    day = 1
    for line in sunrise_sunset_data.split('\n'):
        assert day==int(line[0:2])
        for m in range(12):
            start = (m*11)+4
            sunrise = line[start:start+4]
            sunset = line[start+5:start+10]
            month = m+1
            if (month>DST_START[0] and month<DST_END[0]) or \
               (month==DST_START[0] and day>=DST_START[1]) or \
               (month==DST_END[0] and day<DST_END[1]):
                dst = 60
            else:
                dst = 0
            if sunrise!="    ":
                sunrise_minutes = int(sunrise[0:2])*60+int(sunrise[2:4]) + dst
                sunset_minutes = int(sunset[0:2])*60+int(sunset[2:4]) + dst
                SUNRISE_SUNSET_TABLE[m][day-1] = (sunrise_minutes, sunset_minutes)
                #print("%d day %d, sunrise=%s [%d], sunset=%s [%d]" %
                #  (m+1, day, sunrise, sunrise_minutes, sunset, sunset_minutes))
        day += 1

parse_sunrise_sunset_table()

def get_sunrise_sunset(month, day):
    return SUNRISE_SUNSET_TABLE[month-1][day-1]
        

# # We divide the day into "zones" based on a rough idea of the amount of sunlight.
def time_of_day_to_zone(minutes, sunrise, sunset):
    if minutes < sunrise:
        return 0 # early morning
    elif minutes <= (sunset-30):
        return 1 # daytime
    elif minutes <= max(sunset+60,21.5*60):
        return 2 # evening
    else:
        return 3 # later evening
    
NUM_ZONES=4

def dt_to_minutes(dt):
    return dt.time().hour*60 + dt.time().minute

def minutes_to_time(minutes):
    hrs = int(minutes/60)
    mins = minutes-(hrs*60)
    assert mins<60
    return (hrs, mins)


<filename>lighting_replay_app/analysis/lux_time_utils.py
"""
Time utilities for lux analysis and replay
"""

# Sunrise sunset data for Sunnyvale, CA, 2016.
# From http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl?ID=AA&year=2016&task=0&state=CA&place=Sunnyvale
# Eventually use ephem (https://pypi.python.org/pypi/pyephem/) to
# compute the sunrise/sunset.
sunrise_sunset_data =\
"""01  0722 1701  0712 1732  0638 1803  0553 1831  0512 1858  0449 1924  0451 1933  0513 1915  0539 1836  0604 1750  0633 1709  0704 1650
02  0723 1702  0711 1733  0637 1804  0551 1832  0511 1859  0449 1924  0452 1933  0514 1914  0540 1835  0605 1749  0635 1708  0705 1650
03  0723 1703  0710 1734  0636 1805  0550 1833  0510 1900  0448 1925  0452 1932  0515 1913  0541 1833  0606 1747  0636 1707  0706 1650
04  0723 1703  0709 1735  0634 1806  0548 1834  0509 1901  0448 1925  0453 1932  0516 1912  0542 1832  0607 1746  0637 1706  0707 1650
05  0723 1704  0708 1737  0633 1807  0547 1835  0508 1902  0448 1926  0453 1932  0516 1911  0542 1830  0608 1744  0638 1705  0708 1650
06  0723 1705  0707 1738  0631 1808  0545 1836  0507 1903  0448 1927  0454 1932  0517 1910  0543 1829  0608 1743  0639 1704  0709 1650
07  0723 1706  0706 1739  0630 1808  0544 1837  0506 1904  0447 1927  0455 1931  0518 1909  0544 1827  0609 1742  0640 1704  0710 1650
08  0723 1707  0705 1740  0629 1809  0543 1838  0505 1905  0447 1928  0455 1931  0519 1908  0545 1826  0610 1740  0641 1703  0710 1650
09  0723 1708  0704 1741  0627 1810  0541 1839  0504 1906  0447 1928  0456 1931  0520 1907  0546 1824  0611 1739  0642 1702  0711 1650
10  0723 1709  0703 1742  0626 1811  0540 1839  0503 1906  0447 1929  0456 1930  0521 1905  0547 1822  0612 1737  0643 1701  0712 1650
11  0722 1710  0702 1743  0624 1812  0538 1840  0502 1907  0447 1929  0457 1930  0521 1904  0547 1821  0613 1736  0644 1700  0713 1651
12  0722 1711  0701 1744  0623 1813  0537 1841  0501 1908  0447 1930  0458 1930  0522 1903  0548 1819  0614 1734  0645 1659  0713 1651
13  0722 1712  0700 1745  0621 1814  0535 1842  0500 1909  0447 1930  0458 1929  0523 1902  0549 1818  0615 1733  0646 1659  0714 1651
14  0722 1713  0659 1746  0620 1815  0534 1843  0500 1910  0447 1930  0459 1929  0524 1901  0550 1816  0616 1732  0647 1658  0715 1651
15  0721 1714  0658 1747  0618 1816  0533 1844  0459 1911  0447 1931  0500 1928  0525 1859  0551 1815  0617 1730  0648 1657  0716 1652
16  0721 1715  0656 1748  0617 1817  0531 1845  0458 1912  0447 1931  0501 1928  0526 1858  0551 1813  0618 1729  0649 1657  0716 1652
17  0721 1716  0655 1749  0615 1818  0530 1846  0457 1912  0447 1931  0501 1927  0527 1857  0552 1812  0619 1728  0650 1656  0717 1652
18  0720 1717  0654 1751  0614 1819  0529 1847  0456 1913  0447 1932  0502 1926  0527 1856  0553 1810  0620 1726  0651 1655  0717 1653
19  0720 1718  0653 1752  0612 1820  0527 1848  0456 1914  0447 1932  0503 1926  0528 1854  0554 1809  0620 1725  0652 1655  0718 1653
20  0719 1719  0652 1753  0611 1821  0526 1848  0455 1915  0448 1932  0503 1925  0529 1853  0555 1807  0621 1724  0653 1654  0718 1654
21  0719 1720  0650 1754  0609 1821  0525 1849  0454 1916  0448 1932  0504 1924  0530 1852  0556 1806  0622 1722  0654 1654  0719 1654
22  0718 1721  0649 1755  0608 1822  0523 1850  0454 1916  0448 1932  0505 1924  0531 1850  0556 1804  0623 1721  0655 1653  0719 1655
23  0718 1722  0648 1756  0606 1823  0522 1851  0453 1917  0448 1933  0506 1923  0532 1849  0557 1803  0624 1720  0656 1653  0720 1655
24  0717 1723  0647 1757  0605 1824  0521 1852  0453 1918  0449 1933  0507 1922  0532 1847  0558 1801  0625 1719  0657 1652  0720 1656
25  0717 1724  0645 1758  0603 1825  0520 1853  0452 1919  0449 1933  0507 1921  0533 1846  0559 1759  0626 1717  0658 1652  0721 1657
26  0716 1726  0644 1759  0602 1826  0518 1854  0451 1919  0449 1933  0508 1921  0534 1845  0600 1758  0627 1716  0659 1652  0721 1657
27  0715 1727  0643 1800  0600 1827  0517 1855  0451 1920  0450 1933  0509 1920  0535 1843  0601 1756  0628 1715  0700 1651  0721 1658
28  0715 1728  0641 1801  0559 1828  0516 1856  0450 1921  0450 1933  0510 1919  0536 1842  0602 1755  0629 1714  0701 1651  0722 1659
29  0714 1729  0640 1802  0557 1829  0515 1857  0450 1922  0451 1933  0511 1918  0537 1840  0602 1753  0630 1713  0702 1651  0722 1659
30  0713 1730             0556 1830  0514 1857  0450 1922  0451 1933  0511 1917  0537 1839  0603 1752  0631 1712  0703 1651  0722 1700
31  0712 1731             0554 1830             0449 1923             0512 1916  0538 1837             0632 1711             0722 1701"""

DST_START=(3,13)
DST_END=(11,6)
SUNRISE_SUNSET_TABLE=[[None for d in range(31)] for m in range(12)]
def parse_sunrise_sunset_table():
    day = 1
    for line in sunrise_sunset_data.split('\n'):
        assert day==int(line[0:2])
        for m in range(12):
            start = (m*11)+4
            sunrise = line[start:start+4]
            sunset = line[start+5:start+10]
            month = m+1
            if (month>DST_START[0] and month<DST_END[0]) or \
               (month==DST_START[0] and day>=DST_START[1]) or \
               (month==DST_END[0] and day<DST_END[1]):
                dst = 60
            else:
                dst = 0
            if sunrise!="    ":
                sunrise_minutes = int(sunrise[0:2])*60+int(sunrise[2:4]) + dst
                sunset_minutes = int(sunset[0:2])*60+int(sunset[2:4]) + dst
                SUNRISE_SUNSET_TABLE[m][day-1] = (sunrise_minutes, sunset_minutes)
                #print("%d day %d, sunrise=%s [%d], sunset=%s [%d]" %
                #  (m+1, day, sunrise, sunrise_minutes, sunset, sunset_minutes))
        day += 1

parse_sunrise_sunset_table()

def get_sunrise_sunset(month, day):
    return SUNRISE_SUNSET_TABLE[month-1][day-1]
        

# # We divide the day into "zones" based on a rough idea of the amount of sunlight.
def time_of_day_to_zone(minutes, sunrise, sunset):
    if minutes < sunrise:
        return 0 # early morning
    elif minutes <= (sunset-30):
        return 1 # daytime
    elif minutes <= max(sunset+60,21.5*60):
        return 2 # evening
    else:
        return 3 # later evening
    
NUM_ZONES=4

def dt_to_minutes(dt):
    return dt.time().hour*60 + dt.time().minute

def minutes_to_time(minutes):
    hrs = int(minutes/60)
    mins = minutes-(hrs*60)
    assert mins<60
    return (hrs, mins)


Time utilities for lux analysis and replay # Sunrise sunset data for Sunnyvale, CA, 2016. # From http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl?ID=AA&year=2016&task=0&state=CA&place=Sunnyvale # Eventually use ephem (https://pypi.python.org/pypi/pyephem/) to # compute the sunrise/sunset. 01  0722 1701  0712 1732  0638 1803  0553 1831  0512 1858  0449 1924  0451 1933  0513 1915  0539 1836  0604 1750  0633 1709  0704 1650 02  0723 1702  0711 1733  0637 1804  0551 1832  0511 1859  0449 1924  0452 1933  0514 1914  0540 1835  0605 1749  0635 1708  0705 1650 03  0723 1703  0710 1734  0636 1805  0550 1833  0510 1900  0448 1925  0452 1932  0515 1913  0541 1833  0606 1747  0636 1707  0706 1650 04  0723 1703  0709 1735  0634 1806  0548 1834  0509 1901  0448 1925  0453 1932  0516 1912  0542 1832  0607 1746  0637 1706  0707 1650 05  0723 1704  0708 1737  0633 1807  0547 1835  0508 1902  0448 1926  0453 1932  0516 1911  0542 1830  0608 1744  0638 1705  0708 1650 06  0723 1705  0707 1738  0631 1808  0545 1836  0507 1903  0448 1927  0454 1932  0517 1910  0543 1829  0608 1743  0639 1704  0709 1650 07  0723 1706  0706 1739  0630 1808  0544 1837  0506 1904  0447 1927  0455 1931  0518 1909  0544 1827  0609 1742  0640 1704  0710 1650 08  0723 1707  0705 1740  0629 1809  0543 1838  0505 1905  0447 1928  0455 1931  0519 1908  0545 1826  0610 1740  0641 1703  0710 1650 09  0723 1708  0704 1741  0627 1810  0541 1839  0504 1906  0447 1928  0456 1931  0520 1907  0546 1824  0611 1739  0642 1702  0711 1650 10  0723 1709  0703 1742  0626 1811  0540 1839  0503 1906  0447 1929  0456 1930  0521 1905  0547 1822  0612 1737  0643 1701  0712 1650 11  0722 1710  0702 1743  0624 1812  0538 1840  0502 1907  0447 1929  0457 1930  0521 1904  0547 1821  0613 1736  0644 1700  0713 1651 12  0722 1711  0701 1744  0623 1813  0537 1841  0501 1908  0447 1930  0458 1930  0522 1903  0548 1819  0614 1734  0645 1659  0713 1651 13  0722 1712  0700 1745  0621 1814  0535 1842  0500 1909  0447 1930  0458 1929  0523 1902  0549 1818  0615 1733  0646 1659  0714 1651 14  0722 1713  0659 1746  0620 1815  0534 1843  0500 1910  0447 1930  0459 1929  0524 1901  0550 1816  0616 1732  0647 1658  0715 1651 15  0721 1714  0658 1747  0618 1816  0533 1844  0459 1911  0447 1931  0500 1928  0525 1859  0551 1815  0617 1730  0648 1657  0716 1652 16  0721 1715  0656 1748  0617 1817  0531 1845  0458 1912  0447 1931  0501 1928  0526 1858  0551 1813  0618 1729  0649 1657  0716 1652 17  0721 1716  0655 1749  0615 1818  0530 1846  0457 1912  0447 1931  0501 1927  0527 1857  0552 1812  0619 1728  0650 1656  0717 1652 18  0720 1717  0654 1751  0614 1819  0529 1847  0456 1913  0447 1932  0502 1926  0527 1856  0553 1810  0620 1726  0651 1655  0717 1653 19  0720 1718  0653 1752  0612 1820  0527 1848  0456 1914  0447 1932  0503 1926  0528 1854  0554 1809  0620 1725  0652 1655  0718 1653 20  0719 1719  0652 1753  0611 1821  0526 1848  0455 1915  0448 1932  0503 1925  0529 1853  0555 1807  0621 1724  0653 1654  0718 1654 21  0719 1720  0650 1754  0609 1821  0525 1849  0454 1916  0448 1932  0504 1924  0530 1852  0556 1806  0622 1722  0654 1654  0719 1654 22  0718 1721  0649 1755  0608 1822  0523 1850  0454 1916  0448 1932  0505 1924  0531 1850  0556 1804  0623 1721  0655 1653  0719 1655 23  0718 1722  0648 1756  0606 1823  0522 1851  0453 1917  0448 1933  0506 1923  0532 1849  0557 1803  0624 1720  0656 1653  0720 1655 24  0717 1723  0647 1757  0605 1824  0521 1852  0453 1918  0449 1933  0507 1922  0532 1847  0558 1801  0625 1719  0657 1652  0720 1656 25  0717 1724  0645 1758  0603 1825  0520 1853  0452 1919  0449 1933  0507 1921  0533 1846  0559 1759  0626 1717  0658 1652  0721 1657 26  0716 1726  0644 1759  0602 1826  0518 1854  0451 1919  0449 1933  0508 1921  0534 1845  0600 1758  0627 1716  0659 1652  0721 1657 27  0715 1727  0643 1800  0600 1827  0517 1855  0451 1920  0450 1933  0509 1920  0535 1843  0601 1756  0628 1715  0700 1651  0721 1658 28  0715 1728  0641 1801  0559 1828  0516 1856  0450 1921  0450 1933  0510 1919  0536 1842  0602 1755  0629 1714  0701 1651  0722 1659 29  0714 1729  0640 1802  0557 1829  0515 1857  0450 1922  0451 1933  0511 1918  0537 1840  0602 1753  0630 1713  0702 1651  0722 1659 30  0713 1730             0556 1830  0514 1857  0450 1922  0451 1933  0511 1917  0537 1839  0603 1752  0631 1712  0703 1651  0722 1700 31  0712 1731             0554 1830             0449 1923             0512 1916  0538 1837             0632 1711             0722 1701 #print("%d day %d, sunrise=%s [%d], sunset=%s [%d]" % #  (m+1, day, sunrise, sunrise_minutes, sunset, sunset_minutes)) # # We divide the day into "zones" based on a rough idea of the amount of sunlight. # early morning # daytime # evening # later evening