1) using python modules: putting the module repository in your PYTHONPATH

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

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

2) Reproducing the solar elevation table in Stull ch 2, p. 32

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]))


2016 has 366 days
note that on 3/23 and 6/22 the highest elevation occurs at 1 pm, because of DST
hour   12/22  3/23  6/22 
==============================
  3.0  0.00   0.00   0.00
  4.0  0.00   0.00   0.00
  5.0  0.00   0.00   0.00
  6.0  0.00   0.00   6.62
  7.0  0.00   0.00  15.60
  8.0  0.00   8.20  25.14
  9.0  5.66  17.66  34.91
 10.0 11.56  26.35  44.53
 11.0 15.54  33.70  53.42
 12.0 17.26  38.93  60.53
 13.0 16.54  41.28  64.07
 14.0 13.46  40.29  62.54
 15.0  8.31  36.16  56.66
 16.0  1.48  29.59  48.34
 17.0  0.00  21.37  38.93
 18.0  0.00  12.17  29.17
 19.0  0.00   2.48  19.49
 20.0  0.00   0.00  10.24
 21.0  0.00   0.00   1.74
 22.0  0.00   0.00   0.00

3) now make a plot

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 [ ]: