To run this notebook, you need to install my class_code repository from github: https://github.com/a301-teaching/a301_code. I will be adding and updating code throughout the term. git is a "version control" program that allows you to synchronize your copy of my repository and keep track of my changes. See https://swcarpentry.github.io/git-novice/
To do this:
1) install git
if you are using msys2 bash on windows, open a bash shell and type
$ pacman -S git
and answer yes to the request to install the needed packages
on macs or if you aren't using bash on windows, install from https://git-scm.com/downloads
2) clone the repository
cd to your home directory
clone my a301_code folder to a folder called pythonlibs
$ git clone https://github.com/a301-teaching/a301_code pythonlibs
3) set the environmental variable PYTHONPATH to show python where to look for pythonlibs
in bash, use atom to edit your .bashrc file
$ atom ~/.bashrc
add the line:
export PYTHONPATH="${HOME}/pythonlibs"
4) Start a new shell and see if PYTHONPATH has been set
$echo $PYTHONPATH
should say, for example /c/Users/phil/pythonlibs
5) Test to see if python can import my modules
$ python -c 'import a301examples.week1'
should return without any messages
6) cd to pythonlibs/notebooks and make a copy of this notebook
$ cp day3_datetime.ipynb my_day3.ipynb
7) open this notebook with jupyter
$ jupyter notebook
the code below imports the week1.py module and uses its find_elevation function
In [3]:
import pytz, datetime
from a301examples.week1 import find_elevation, find_yearlength
import numpy as np
year = 2016
print('{} has {} days'.format(year,find_yearlength(year)))
print("note that on 3/23 and 6/22 the highest elevation occurs at 1 pm, because of DST")
print("{:5s} {:5s} {:5s} {:5s}".format('hour','12/22','3/23','6/22') )
print("="*30)
#
# Stull's three days in 2016
#
dates = [datetime.datetime(year,12,22,0,0,0),
datetime.datetime(year,3,23,0,0,0),
datetime.datetime(year,6,22,0,0,0)]
#
# save elevations in a list of (hour,elevation) tuples
# initalize a dictionary with three entries to hold the lists
# for each of the three days
#
save_elevations={'dec':[],'mar':[],'june':[]}
#
# vancouver coords
#
lat = 49.25
lon = 123.1
pacific = pytz.timezone('America/Vancouver')
for hour in np.arange(3,23,1.):
#
# the following two lines
#
# - define the number of hours to add to each start date
# - adds the hour to the date and sets it in the pacific timezone for each
# of the three days in the dates list using a list comprehension
# (see http://clouds.eos.ubc.ca/~phil/courses/atsc301/whirlwind/11-List-Comprehensions.html)
#
the_hour = datetime.timedelta(hours=hour)
local_dates = [pacific.localize(the_date + the_hour) for the_date in dates]
#
# print out table on stull ch2 p. 32 by calling the find_elevation function on
# each of the three datetimes in local_dates
# append the three elevations to the save list for each day as an (hour,elevation)
# pair
#
elevation=[find_elevation(the_date,lat,lon) for the_date in local_dates]
save_elevations['dec'].append((hour,elevation[0]))
save_elevations['mar'].append((hour,elevation[1]))
save_elevations['june'].append((hour,elevation[2]))
#right justify the columns and make them all 5 characters wide
# see http://clouds.eos.ubc.ca/~phil/djpine_python/chap4/chap4_io.html?highlight=format
print("{:>5.1f} {:>5.2f} {:>5.2f} {:>5.2f}".format(hour,elevation[0],elevation[1],elevation[2]))
For more on plotting see the Chapter 5 of the Pine book
In [4]:
from matplotlib import pyplot as plt
%matplotlib inline
months = save_elevations.keys()
fig,ax = plt.subplots(1,1,figsize=(10,8))
for month_name, month_line in save_elevations.items():
#
# use list comprehensions to unpack the (hour,elevation) pairs
# into a list of hours and a list of elevations for plotting
#
hours = [the_hour[0] for the_hour in month_line]
elevation = [the_hour[1] for the_hour in month_line]
ax.plot(hours,elevation,label=month_name,lw=5)
ax.set(xlabel='local time (hours)',ylabel='solar elevation (degrees)',
title='solar elevation for Vancouver, BC on 3 days in 2016')
#
# suppress final print by saving ax.legend output as temporary variable
#
_ = ax.legend()
In [ ]: