#!/usr/bin/env python # coding: utf-8 # # Iowa State University - Mteor 227 - Computational Meteorology # #### Here is my template for plotting station data from a Campbell Scientific weather station. Work through the code and comments below for hints and tips on completing this task. # Import python packages. NumPy for numerical calculuation, Pandas for reading the csv file, matplotlib for plotting functionality, and PyLab for inline visual tools. Notice the aliasing for ease of use later in the program. # In[1]: get_ipython().run_line_magic('pylab', 'inline') import numpy as np import pylab as pl import pandas as pd import matplotlib.pyplot as plt # Read station data from comma delimited file that is in data directory. data is an object that is created from the method read_csv in the pandas package. # In[2]: data = pd.read_csv('./data/CR1000_Franz_open_20131011.dat',skiprows=(0,2,3),header=0) # I have left these here so you can mess around with attributes and methods that can be used on the object. These do not need to be run. You could just just down to the plotting section if all you are trying to do is create a plot. # In[3]: data # In[4]: data.dtypes # In[5]: data.columns # In[6]: data.values # Ok, Let's finally get down to the fun part, plotting. This is the simplest way to make a plot. # In[7]: plt.plot(data.AirTC) plt.show() # You can also specify the X and Y axes by providing two arguments to the plot method. # In[8]: plt.plot(data.RECORD,data.AirTC) plt.show() # No plot is complete without a title and axis labels, so let's add those. Notice how the degree symbol is generated in Python. # In[9]: plt.title('2-m Air Temperature') plt.xlabel('Record Number') plt.ylabel('Temperature ($^\circ$C)') plt.plot(data.RECORD,data.AirTC) plt.show() # A more advanced technique, but one that is very important in this case, would be making the x-axis something more meaningful. This would require converting the timestamp (data.TIMESTAMP) provided in the data to something that matplotlib understands which is a datetime string. This requires the dateutil package, so we need to import it first. # In[10]: import dateutil as du # To see how this conversion works, let's look at the data timestamp before and after we run the dateutil parse method. # In[11]: data.TIMESTAMP.head(5) # The command below used to convert the data timestamp contains an implied Python loop. It basically states that the parse function should be applied for all values in the data['TIMESTAMP'] list. As the list is traversed, each value gets assigned to x and x is then passed to the parse method. The result of this command is a list. # In[12]: new_timestamp = [du.parser.parse(x) for x in data['TIMESTAMP']] # Let's now take a look at the list we created. It is now an instance of the datetime class. matplotlib understands how to handle datetime instances. Converting strings to datetime instances and back is one of the primary functionalities of the dateutil package. # In[13]: new_timestamp[0:5] # MATLAB and pyplot have the concept of the current figure and the current axes. Because of this, matplotlib does as well. The first line below creates an instance of figure and axis objects for the current plot. We can then use attributes and methods associated with these objects. The autofmt_xdate method rotates the the tick labels and right aligns them. # In[14]: fig, ax = plt.subplots() ax.plot(new_timestamp,data.AirTC) fig.autofmt_xdate() plt.show() # The last thing I want to explore here is how to generate more than one plot. For example, let's say that we want to compare the average solar radiation to any precipitation that fell. It might be useful to put these plots on the same page. Here is one way it can be done. I should add here that the subplots_adjust method creates space between the subplots to prevent labels from overlapping or the plots from being too close together. # In[15]: print data.columns # In[16]: fig,(ax0,ax1) = plt.subplots(nrows=2) ax0.plot(new_timestamp,data.SlrW_Avg) ax0.set_title('Hourly Average Solar Radiation') ax0.set_ylabel('W/m^2') ax1.plot(new_timestamp,data.Rain_Tot) ax1.set_title('Hourly Precipitation') ax1.set_ylabel('mm') fig.autofmt_xdate() plt.subplots_adjust(hspace=0.5) plt.show() # In[17]: fig,(ax0,ax1, ax2) = plt.subplots(nrows=3) ax0.plot(new_timestamp,data.SlrW_Avg) ax0.set_title('Hourly Average Solar Radiation') ax0.set_ylabel('W/m^2') ax1.plot(new_timestamp,data.Rain_Tot) ax1.set_title('Hourly Precipitation') ax1.set_ylabel('mm') ax2.plot(new_timestamp,data.Press) ax2.set_title('Station Pressure') ax2.set_ylabel('mb') fig.autofmt_xdate() plt.subplots_adjust(hspace=0.5) plt.show() # In[17]: