%matplotlib inline

import os, sys
import numpy as np
import matplotlib
import pandas as pd
import requests
import StringIO

# set matplotlib  style
matplotlib.style.use('ggplot')

sitename = 'alligatorriver'
roiname = 'DB_0001'
infile = "{}_{}_1day.csv".format(sitename, roiname)
print infile

%%bash
head -30 alligatorriver_DB_0001_1day.csv

"""
Explanation: PhenoCam ROI Summary Files
Here's a python notebook demonstrating how to read in and plot an ROI (Region of Interest) summary using python.  In this case I'm using the 1-day summary file from the alligatorriver site.  The summary files are in CSV format and can be read directly from the site using a URL.  Before reading from a URL let's make sure we can read directly from a file.
End of explanation
"""


with open(infile,'r') as fd:
    df = pd.read_csv(fd, comment='#', parse_dates=[0])

df.head()

df.plot('date', ['gcc_90'], figsize=(16,4),
        grid=True, style=['g'] )


"""
Explanation: While the data can be read directly from a URL we'll start by doing the simple thing of reading the CSV file directly from our local disk.
End of explanation
"""


url = "https://phenocam.sr.unh.edu/data/archive/{}/ROI/{}_{}_1day.csv"
url = url.format(sitename, sitename, roiname)
print url

"""
Explanation: That was pretty simple.  Now try to read directly from a URL to see if we get the same result.  This has the advantage that you always get the latest version of the file which is updated nightly.
End of explanation
"""


response = requests.get(url)
fd = StringIO.StringIO(response.text)
df = pd.read_csv(fd, comment='#', parse_dates=[0])
fd.close
df[0:5]

"""
Explanation: Use the requests package to read the CSV file from the URL.
End of explanation
"""


df[df['gcc_90'] == -9999.].gcc_90 = np.nan

df.plot('date', ['gcc_90'], figsize=(16,4),
        grid=True, style=['g'] )



"""
Explanation: If necessary we'll need to convert nodata values.
End of explanation
"""


df.plot('date','image_count', figsize=(16,4), style='b')

"""
Explanation: We can look at other columns and also filter the data in a variety of ways.  Recently we had a site where the number of images varied a lot over time.  Let's look at how consistent the number of images for the alligator river site.  The image_count reflects our brightness threshold which will eliminate images in the winter time when the days are shorter.  But there are a number of other ways the image count can be reduced.  The ability reliably extract a 90^th precentile value is dependent on the number of images available for a particular summary period.
End of explanation
"""


df10 = df[df['image_count'] >= 10]
df10.plot('date', ['gcc_90'], figsize=(16,4),
        grid=True, style=['g'] )

"""
Explanation: One possibility would be to filter the data for summary periods which had at least 10 images.
End of explanation
"""


