#!/usr/bin/env python # coding: utf-8 # #
Assignment 3: Evapotranspiration and Precipitation
#
4 HW pts + 3 final project points
# Due date: 2/21/17 #
# ## Instructions # As always, feel free to work on this assignment in groups. In fact, I strongly suggest it, particularly for Part II. Turn in your answers in the form of a Jupyter Notebook, converted to .HTML format. For convenience, you may produce separate files for the downloaded code that you modify in Part II, but please include all of your written answers for part II in your main document. # # ## Part I: Clausius-Clapeyron relationship and extreme rainfall # **1.** Use the Tetens equation to show that, for every 1-degree increase in temperature, we expect a 7.5% increase in the magnitude of extreme rain events for temperatures common on Earth. Deliverable: Produce a plot of the percent increase in the magnitude of extreme rain events (in terms of the maximum mass of water air can hold) as a function of temperature. Is the plot roughly flat, or does it have a steep slope? For what temperature or range of temperatures is the 7.5% increase valid? [1 pt] # # *Suggestions: First, use the commenting syntax (#) in python to write out the different steps you will need to take to solve this problem. (This is called “pseudocode”.) Then write in the accompanying code. Remember that you will need to convert vapor pressure to a mass of water. (You might want to do this within a module!) Note that if you write your pseudocode clearly but there are problems with your code itself, you will still get generous partial credit, for this and other assignments.* # # *Remember the formula for percent increase:* # \begin{equation*} # \frac{(x_{new}-x_{old})}{x_{old}} \times 100\% # \end{equation*} # **2.** What happens if you redo the calculation using saturation vapor pressure rather than mass of water? (Hint, if you have written a module to solve the Tetens equation AND convert vapor pressure to mass, you just need to copy and paste it into a new cell and have it return vapor pressure instead of mass.) [½ pt] # # ## Part II: Getting started with the malaria model # Go to https://bcourses.berkeley.edu/courses/1458496/files/folder/Materials/Assignment3 and download the data file (in the `data` folder, `Larval habitat.ipynb`, and `habitat.py.` Take some time to look in depth at all of these files. It is expected that you won’t understand all of the programming strategies or syntax yet, but they should be starting to make sense. Note that the `habitat.py` file uses **classes**, which are similar to the modules that we have already discussed, although they can contain several different modules. (Here, you might think of them as a collection of pre-loaded modules.) A class refers to itself using the variable `self`, and each class must be initiated with the `__init__` module, in which variables are defined that may be used in all other modules within the class. # # Note that the "+=" syntax updates the value of a variable by adding what is on the right-hand-side of the equation to its old value. As an example: # In[5]: x = 0 for i in range(5): x += 2 print(x) # Try this again with the “-=” syntax to understand how it works. # # Note that you may find it helpful to turn to Google to illuminate certain parts of the model or to answer certain questions below. # **3.** Describe in a few narrative sentences/paragraphs how the model is currently functioning to simulate larval habitat as functions of evapotranspiration and precipitation. At this point you can ignore the stream habitat and focus only on the temporary habitat. How are evapotranspiration and precipitation being represented in the model? Discuss the pros and cons of representing evapotranspiration and precipitation in this manner. [1 pt] # ### *Short answer*: # **4.** Are there data columns in the input data file that are currently being ignored but that could be useful in improving the representation of evapotranspiration or precipitation? If so, which column(s)? [½ pt] # **5.** How does the model handle blanks that are present in the input data columns? Which package (of the ones imported at the beginning of the code) contains the function(s) used for dealing with the blanks? Hint: Google :) Can you think of a way to improve the way blanks are handled? [½ pt] # **6.** What happens to temporary habitat if you increase the evaporation scale constant? Take a look at how the scale constant appears in the model (in the `.py` file), and then change the scale constant in the `.ipynb` file to verify that you are correct in your thinking about how it works. Why might the original coders have used an evaporation scale constant? [½ pt] # ### *Problem solving (there will be time in class for this next Thursday)*: # **7.** Develop an alternative approach to solve for ET with this input dataset. Note that you may need to assume some reasonable values for some of the variables needed for the equation that you choose to use. [3 pts total]

# **a.** Produce a plot that shows your predicted evapotranspiration rate as a function of day of the year. You can do this by initializing a new variable (let’s call it `ETrate`) before the `for` loop, as well as a counter, `i`:

# `ETrate = np.zeros(365)`
# `i = 0`

# Within the `for` loop that solves for the habitat, you can then tack on these two additional lines:

# `ETrate[i] = model1.evaporation_rate_new(weather)`
# `i += 1`

# Now you can plot `ETrate` just as you would plot any array, using the `plt.plot()` function. [1 pt] # # **b.** Produce a plot that shows the old estimate of the temporary habitat together with the updated estimate of temporary habitat. The x-axis should be date, as in the plot contained in the original program. [1 pt] # **c.** Can you get the two estimates to line up, more or less, by changing the evaporation scale factor? In the version of your Jupyter Notebook that you turn in, you only need to display a single plot for **a** and **b**, but use the evaporation scale factor that gets them to line up fairly well. [¼ pt] # **d.** What are your assumptions invoked for the new solution of ET? [¾ pt] # **e.** Extra credit bonus (worth one point on final grade): Plot the old and updated temporary habitat against each other with a scatter plot (the x-axis should be the value of the old habitat estimate, and the y-axis should be the updated estimate). Use the scaling factor you identified in **b**. Plot a linear regression through the scatter plot. # # ### *Pointers*: # Start by changing the `.ipynb` code to read in the extra data column(s) that you need. Don’t forget that you can use the head() function to verify that they loaded properly. Example:
# # `print(weather_df.head())`
# # Next, define new modules to perform any needed unit conversions and to interpret the “missing data” identifiers as NaNs. The `relative humidity` module provides a useful example.
# # Finally, write a new line near the bottom of the first cell to convert missing values of the new data column(s) into a more useful number, looking at the other lines as an example.
# # When you define an updated evaporation rate within the `.py` code, you should do so with a new module (I called mine `evaporation_rate_new`). In the `.ipynb` code, you’ll want to copy and paste the larval habitat model cell into a whole new cell that you can manipulate without worrying about messing up the original one. I also defined new channels in my data frame (`temporary_habitat2`) so as not to overwrite the originals. This is necessary, as you’ll need to plot the new estimate alongside the original.
# # Note that the `.py` file is raw Python code. This code can still be edited within Jupyter Notebooks, but you’ll notice that there is no button to run it within Jupyter Notebook. This is because the classes contained within the `.py` file can only be run when another program imports them and calls the specific functions within the classes. One thing that is counterintuitive when you are working with an `.ipynb` file that calls a `.py` file is that the `.ipynb` file will not automatically reload the `.py` file and its modules/classes when you make a change to the `.py` file, even if you saved that change. Fortunately, there is a way to get around this problem! In the beginning of your `.ipynb file`, add the following lines:
# # `%load_ext autoreload`
# `%autoreload 2`
# `from __future__ import absolute_import`
# # Also, in the same directory where you have stored the `.ipynb` and `.py` file, add a new text file from the main jupyter interface (the window where you can see your files and directories) by going to New>Text File. Leave the file blank, but rename it `__init__.py`. Now, whenever you call your `.py` file from the `.ipynb` file, it will reload. Make sure that you still save your `.py` file whenever you edit it, because the reloaded version will be the last version that you saved.
# # Notice that rates of rainfall and ET in the model are in mm/day. Make sure that you keep track of all of your units in the comments of your code. If your ET estimate seems way off (i.e., different from 1-20 mm/day), it is probably a units problem. # # In[ ]: