sunrise-sunset.html
001| """Sunrise and Sunset throughout the year."""
002|
003| from sys import argv
004| from os import path
005| from datetime import date,time
006| from math import radians,degrees, sin,cos,tan,acos
007| import matplotlib.pyplot as plt
008| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
009| from matplotlib.dates import HourLocator,MinuteLocator, DayLocator,MonthLocator, DateFormatter
010|
011| BasePath = r'C:\CJS\prj\Python\blog\proj'
012| ImgsPath = path.join(BasePath,'img')
013| Filename = 'sunrise-sunset.png'
014|
015|
016| lat = 45.0
017|
018|
019| def make_chart (dts, ys1, ys2, ys3):
020| fig, ax = plt.subplots()
021| fig.suptitle('Sunrise-Sunset', fontweight='bold')
022|
023| ax.set_xlabel('Months of the Year')
024| ax.set_xlim(date(2020,3,20), date(2021,3,21))
025| ax.xaxis.set_major_locator(MonthLocator())
026| ax.xaxis.set_minor_locator(NullLocator())
027| ax.xaxis.set_major_formatter(DateFormatter('%b'))
028|
029| for tick in ax.xaxis.get_major_ticks():
030| tick.label1.set_rotation('vertical')
031| tick.label1.set_size(9)
032|
033| ax.set_ylabel('Hours of the Day')
034| ax.set_ylim(0, 24)
035| ax.yaxis.set_major_locator(MultipleLocator(6))
036| ax.yaxis.set_minor_locator(MultipleLocator(1))
037| ax.yaxis.set_major_formatter(FormatStrFormatter('%0d'))
038|
039| ax.grid(which='major', axis='both', color='#bfbfbf', lw=1.2,ls='-',zorder=1, alpha=1.0)
040| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
041| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
042| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
043|
044| ax.plot(dts,ys1, label='sunrise', color='#ff0000', lw=2.0, alpha=0.8, zorder=11)
045| ax.plot(dts,ys2, label='sunset' , color='#0000ff', lw=2.0, alpha=0.8, zorder=12)
046| ax.plot(dts,ys3, label='suntime', color='#8f5f00', lw=2.0, alpha=0.8, zorder=13)
047|
048| fig.set_figwidth(4.4)
049| fig.set_figheight(3.0)
050| fig.subplots_adjust(left=0.12, right=0.99, bottom=0.19, top=0.92)
051|
052| fn = path.join(ImgsPath, Filename)
053| fig.savefig(fn)
054| print('wrote: %s' % fn)
055| print()
056|
057|
058| def sunrise_and_sunset (latitude, declination):
059| t1 = tan(radians(latitude))
060| t2 = tan(radians(declination))
061| w0 = degrees(acos(-t1 * t2))
062| sunrise = 12.0 - (w0 / 15.0)
063| sunset = 12.0 + (w0 / 15.0)
064| rise_h = int(sunrise)
065| set_h = int(sunset)
066| rise_m = int((sunrise - rise_h) * 60.0)
067| set_m = int((sunset - set_h) * 60.0)
068| return (w0, sunrise, sunset, time(rise_h,rise_m), time(set_h,set_m))
069|
070|
071| def main ():
072| declination = lambda d:(23.0*sin(radians(d)))
073|
074| xs = list(range(365))
075| ds = [declination(360.0*(float(x)/365.0)) for x in xs]
076| sss = [sunrise_and_sunset(lat,d) for d in ds]
077|
078| ys1 = [s[1] for s in sss]
079| ys2 = [s[2] for s in sss]
080| ys3 = [s[2]-s[1] for s in sss]
081|
082| dt0 = date(2020,3,20).toordinal()
083| dts = [date.fromordinal(dt0+x) for x in xs]
084|
085| make_chart(dts, ys1, ys2, ys3)
086|
087|
088| if __name__ == '__main__':
089| print(argv[0])
090| print()
091| main()
092| print()
093|
094| '''eof'''
095|