This notebook contains optional problems to test your skills with everything covered in workshop 1. Feel free to work on these problems in groups and don’t be afraid to reach out to anyone helping conduct the workshop for help if you get stuck.
### IMPORTS GO HERE ###
# STANDARD LIBRARIES #
import datetime as dt
# PUBLIC LIBRARIES #
import numpy as npWe’re first going to practice working with various data types.
Below is a string of reported weather station times followed by a temperature:
- extract the temperatures from the string and save them to a list. The string has the format ‘MM/DD/YYYY TEMP:’ repeating
There is also a string of just the dates from the weather station
- save strings of the dates to a list (we’ll use this later). The String has the format ‘MM/DD/YYYY:’ repeating
HINT: The .split() method we saw in the tutorial converts the strings it acts on into a list, where the string has been fractured into sub-segments based on a specific character
Once you’ve done both of those thing loop over the lists you made and print out the date and temperature
wx_stn_str = '08/01/2024 99:08/02/2024 101: 08/03/2024 97:08/04/2024 99: 08/05/2024 82:08/06/2024 86:08/07/2024 87:08/08/2024 68:08/09/2024 78'
wx_stn_dates_str = '08/01/2024:08/02/2024:08/03/2024:08/04/2024:08/05/2024:08/06/2024:08/07/2024:08/08/2024:08/09/2024'
# Now that we've split the weather station report into multiple lists let's convert the
# the contents of the lists to datetime objects and floats. A function to convert to
# datetime objects has been provided. Write a function to convert your list of
# temperatures from a list of strings to a list of floats.
def convert_datestr_to_datetime(date_str:str) -> dt.datetime:
'''
Uses built in datetime functions to convert a string to
a datetime object.
Parameters:
- date_str (str): The string we are converting to a datetime object
Outpuits:
- date (dt.datetime): The converted string
'''
date = dt.datetime.strptime(date_str,'%m/%d/%Y')
return date
# now that we have a list of the temperature take the mean, median, and std. dev. of the
# list of temperatures using np.mean(), np.median(), and np.std() and report those values
# using an f-string like we used in the tutorial, report values to the first decimal place
# using :.1f
# Now let's use an if else state combined with a loop
# Loop over the lists you made and only print the dates where
# the temperature was below 86
Now we’re going to practice with making our own functions and math operations
Dewpoint Temperature is an important concept in atmospheric science. It represents the temperature for the air to be saturated with water and is important for understanding how heat affects people.
Dewpoint can be approximated from a temperature (in oC) and relative humidity measurement ().
Dewpoint temperature () can be approximated using the Magnus Formula (Wikipedia Link)
Where = 17.625, and = 243.04 oC
# Before we make a function to calculate dewpoint we need temperature data
# Make a numpy array of containing temperature values spaced 2 degrees F apart
# from -20 to 110 (exclusive), and an array containing possible values of RH in % from
# 0 to 100 (exclusive) with a spacing of 5
# feel free to use any of the techniques mentioned in the tutorial
temp_array = None
rh_array = None# Here is the unit conversion function we saw back in the tutorial
def convert_f_to_c(temp:float) -> float:
'''
This functions converts temperatures in fahrenheit to celsius.
Parameters:
- temp (float): The temperature in fahrenheit
Outputs:
- conv_temp (float): The temperature in celsius
'''
conv_temp = 5*(temp - 32)/9
return conv_temp
# Now that we have our arrays let's make a function to calculate dewpoint
# I provide the empty skeleton of the functions you will need
# To calculate gamma you will need to use the function np.log() to
# calculate the natural log of RH/100
def calculate_gamma(temp,rh):
return None
def calculate_dewpoint(temp,rh):
return None
# Calculate the dewpoint of the 46th temperature value (index 45)
# with the 15th relative humidity value (index 14)
# As a check you should get a value of ~15.43 degrees Celsius
# Use print() to show the value of what you calculate
Now that we have our functions to calculate dewpoint let’s do some more practice with numpy arrays
# Now that we have our functions to calculate dewpoint let's try something more complicated
# Make an array called td_array that is empty and is the same
# length as temp_array. Fill this array with dewpoint temps assuming
# a relative humidity of 20%. Then take the difference between temp_array and td_array
# (Actual Temp - Dew Point Temperature)
# and print the maximum value inside that array.
# Functions you may want to use len(), np.max() and np.empty()
# As a check you should get a value of ~27.6 degrees Celsius, remember that units matter here
td_array = None27.614541903146744
Now that we’ve made our dewpoint functions and worked with them let’s go back to the class we made earlier weather_station
Below is the definition of our class I copied over for you.
- Add the calculate_gamma and calculate_dewpoint functions to the class.
- You’ll also notice that our class definition below doesn’t have ways to set the dewpoint as well as to report the current temp, RH, and dew point. Add those functions to the class definiton as well.
- You should modify the calculate_dewpoint function to take self as the only input.
- You should report the dew point only to the first decimal place, and the temperature in Celsius.
# Here is an example of a class for a weather station object
# This weather station class stores information such as its name, and geographic position
# it also has a (for now) empty function to convert an observed temperature and RH to dewpoint
class weather_station():
def __init__(self, name:str,lat:float,lon:float) -> None:
self.name = name
self.lat = lat
self.lon = lon
self.temp = None
self.RH = None
self.dew_pt_temp = None
def observe_temp_and_rh(self,temp:float,RH:float) -> None:
'''
Given a temperature provided by the user set the weather station's
current observed temp to that value.
Parameters:
- temp (float): The temperature in fahrenheit
Outputs:
- None
'''
# This sets the the temperature and relative humidity values
# that is stored by the class
# Calling self.{insert property name} either calls that value
# and is used to interact with data stored within the class object
self.temp = temp
self.RH = RH
return None
def convert_f_to_c(temp:float) -> float:
'''
This functions converts temperatures in fahrenheit to celsius.
Parameters:
- temp (float): The temperature in fahrenheit
Outputs:
- conv_temp (float): The temperature in celsius
'''
conv_temp = 5*(temp - 32)/9
return conv_temp
def calculate_dew_point(self) -> float:
'''
I am leaving this intentionally blank for now, we'll fill it in
later.
'''
return None
Double check your updated weather station class works correctly by creating a weather station called “Christman Field” located at 40.597N,105.144W.
Observe a temperature of 65 Fahrenheit with 88% relative humidity. Report the weather stations observables. You should get a dew point of 16.3 Celsius.
# Create the christman field weather station below, observe the environmental variables,
# calculate dewpoint then report the weather in this cell
# Below are several numpy arrays containing weather station names, lats, lons,
# observed temperature, and observed RH.
# Create a numpy array of weather stations, you may need to set dtype = Object, then loop over
# your array of weather stations, calculating the dewpoint for each one
# then find the weather station with the lowest dewpoint
# As a check your answer should be Denver
station_names = np.array(['New York','Miami','Anchorage','Denver','Boston','Detroit'])
station_lons = np.array([-74.00,-80.19,-149.90,-104.99,-71.05,-83.03])
station_lats = np.array([40.71,25.76,61.21,39.74,42.34,42.33])
station_temps = np.array([77,92,44,65,80,68])
station_rhs = np.array([68.9,88.2,75.1,34.9,80.0,65.6])