* Download data from LOBO server
* (if needed) transform data into a format suitable for plotting
* Plot time vs variable (e.g. temperature)
* Turn your code into function for future (easier) use
* Add docstrings and other relevant comments and documentation
time to work...
In [55]:
# URL quering the LOBO server for data (in this case, temperature data)
URL = 'http://lobo.satlantic.com/cgi-data/nph-data.cgi?min_date=20090610&max_date=20090706&y=temperature'
NOTE: You can see in the URL that the dates are included (i.e. min_date=20090610 and max_date=20090706) as well as the variable to query (i.e. y=temperature).
Now lets use "pandas" to actually dowload the data...
In [56]:
import pandas as pd
# Import data from LOBO server
data = pd.read_csv(URL,sep='\t')
In [57]:
data[:10]
Out[57]:
Now lets disect "data" a bit... lets find the title (or "keys") of its collumns...
In [58]:
data.keys()
Out[58]:
Lets see what is in the temperature column...
In [59]:
data['temperature [C]'][:10]
Out[59]:
...and in the "date" column...
In [60]:
data['date [AST]'][:10]
Out[60]:
...and the indices
In [61]:
data.index
Out[61]:
In [62]:
%matplotlib inline
Ok, now lets do the plot...
In [63]:
#import matplotlib.pyplot as plt
# ...just a quick plot
data.plot();
If we change the indices from "numbers" to "DatetimeIndex", then we can plot nicer!
In [64]:
# Change indices to DatetimeIndex objects
data.index = pd.DatetimeIndex(data['date [AST]'])
# Now that we made "date indices" we can drop the "date" column
data = data.drop('date [AST]',axis=1)
Lets see what happened....
In [65]:
data.index
Out[65]:
Lets plot again...
In [66]:
data.plot()
Out[66]:
Lets make it prettier!
Ok. NOw it is the time to put it all together... Note that I replaced the 'min_date' and 'max_date' arguments in the url for a couple of variables, so that we can easily change dates.
In [67]:
import pandas as pd
import matplotlib.pyplot as plt
# Start and End dates
mindate = '20090610'
maxdate = '20090706'
# URL quering the LOBO server for data (in this case, temperature data)
URL = 'http://lobo.satlantic.com/cgi-data/nph-data.cgi?min_date='+mindate+'&max_date='+maxdate+'&y=temperature'
# Import data from LOBO server
data = pd.read_csv(URL,sep='\t')
# Change indices to DatetimeIndex objects
data.index = pd.DatetimeIndex(data['date [AST]'])
# Now that we made "date indices" we can drop the "date" column
data = data.drop('date [AST]',axis=1)
# ...a bit fancier plot
data.plot(style='-r',legend=False)
plt.title('Temperature from LOBO (Halifax, Canada)')
plt.ylabel('Temperature (oC)')
plt.xlabel('Dates')
Out[67]:
Now it is time to make our code into a function, note that:
mindate and maxdate in the URL string!!!
In [68]:
def load_temp(mindate,maxdate):
import pandas as pd
import matplotlib.pyplot as plt
# URL quering the LOBO server for data (in this case, temperature data)
URL = 'http://lobo.satlantic.com/cgi-data/nph-data.cgi?min_date='+mindate+'&max_date='+maxdate+'&y=temperature'
# Import data from LOBO server
data = pd.read_csv(URL,sep='\t')
# Change indices to DatetimeIndex objects
data.index = pd.DatetimeIndex(data['date [AST]'])
# Now that we made "date indices" we can drop the "date" column
data = data.drop('date [AST]',axis=1)
# ...a bit fancier plot
data.plot(style='-r',legend=False)
plt.title('Temperature from LOBO (Halifax, Canada)')
plt.ylabel('Temperature (oC)')
plt.xlabel('Dates')
plt.show()
return data
Lets take our new function for a spin...
In [73]:
mydata = load_temp('20090616','20101017')
print mydata[:20]
Some final touch-ups, like Documentation and a karg to prevent plotting...
In [70]:
def load_temp(mindate,maxdate,plot=True):
'''
Downloads data from LOBO and makes a plot.
Arguments:
mindate (string): Start date
maxdate (string): End date
plot (boolean): If True, it produces a plot (default=True)
Returns:
DataFrame and a plot (if `plot` argument = True)
'''
import pandas as pd
import matplotlib.pyplot as plt
# URL quering the LOBO server for data (in this case, temperature data)
URL = 'http://lobo.satlantic.com/cgi-data/nph-data.cgi?min_date='+mindate+'&max_date='+maxdate+'&y=temperature'
# Import data from LOBO server
data = pd.read_csv(URL,sep='\t')
# Change indices to DatetimeIndex objects
data.index = pd.DatetimeIndex(data['date [AST]'])
# Now that we made "date indices" we can drop the "date" column
data = data.drop('date [AST]',axis=1)
if plot==True:
# ...a bit fancier plot
data.plot(style='-r',legend=False)
plt.title('Temperature from LOBO (Halifax, Canada)')
plt.ylabel('Temperature (oC)')
plt.xlabel('Dates')
plt.show()
return data
Lets see if we can see the documentation...
In [71]:
load_temp?
One last spin without making a plot:
In [72]:
mydata = load_temp('20090610','20090706',plot=False)
print mydata[:10]
Last thing... create a new file (in this directory) called lobo_YourName.py .... copy-paste your new function, so we can use it later.