#Loading the hydropy package
import hydropy as hp

"""
Explanation: Hydropy-package
End of explanation
"""


HTML('<iframe src=http://biomath.ugent.be/~stvhoey/maarkebeek_data/ width=700 height=350></iframe>')

"""
Explanation: We have a Dataframe with river discharge at different locations in the Maarkebeek basin (Belgium):
End of explanation
"""


flowdata.head()

flowdata.tail()

print len(flowdata), 'records', 'from', flowdata.index[0], 'till', flowdata.index[-1]

"""
Explanation: Data downloaded from http://www.waterinfo.be/, made available by the Flemish Environmental Agency (VMM).
End of explanation
"""


myflowserie = hp.HydroAnalysis(flowdata)

"""
Explanation: Converting the dataframe to a hydropy time series datatype, provides extra functionalities:
End of explanation
"""


myflowserie.get_year('2009').get_season('summer').plot(figsize=(12,6))

"""
Explanation: Select the summer of 2009:
End of explanation
"""


myflowserie.get_year('2011').get_month("Jun").get_recess().plot(figsize=(12,6))

"""
Explanation: Select only the recession periods of the discharges (catchment drying) in June 2011:
End of explanation
"""


fig, ax = plt.subplots(figsize=(13, 6))
myflowserie['LS06_347'].get_year('2010').get_month("Jul").get_highpeaks(150, above_percentile=0.9).plot(style='o', ax=ax)
myflowserie['LS06_347'].get_year('2010').get_month("Jul").plot(ax=ax)

"""
Explanation: Peak values above the 90th percentile for the station LS06_347 in July 2010:
End of explanation
"""


raindata.columns

storms = myflowserie.derive_storms(raindata['P05_019'], 'LS06_347',
                                   number_of_storms=3, drywindow=50,
                                   makeplot=True)

storms = myflowserie.derive_storms(raindata['P06_014'], 'LS06_347',
                                   number_of_storms=3, drywindow=96,
                                   makeplot=True)

"""
Explanation: Select storms and make plot
End of explanation
"""


myflowserie.data.groupby('season').mean()

"""
Explanation: Season averages (Pandas!)
End of explanation
"""


import hydropy as hp
flowdata = pd.read_pickle("./data/FlowData")
raindata = pd.read_pickle("./data/RainData")
myflowserie = hp.HydroAnalysis(flowdata)

"""
Explanation: Goals

Use the power of Python Pandas
Provide domain specific (hydrological) functionalities
Provide intuitive interface for hydrological time series (main focus on flow series)
Combine different earlier written loose functionalities in package
Independent, but useful in global Phd-developed framework: enables the user to quickly look at different properties of model behaviour

Where?

Code : https://github.com/stijnvanhoey/hydropy -->  Fork and contribute
Website : https://stijnvanhoey.github.io/hydropy/

How to start?

Fork the github repo

Get the code on your computer
git clone https://github.com/yourname/hydropy



Run the python setup script (install as development package):
python setup.py develop
4. Improve implementation, add functionalities,...


Make a new branch

Make improvements on this branch
push the branch towards the repo and perform a push request



Functionalities extended
End of explanation
"""


# Data inspection
myflowserie.summary() #head(), tail(), 

# Resampling frequencies
temp1 = myflowserie.frequency_resample('7D', 'mean')  # 7 day means
temp1.head()

temp2 = myflowserie.frequency_resample("M", "max")  # Monthly maxima
temp2.head()

temp3 = myflowserie.frequency_resample("A", 'sum')  # Yearly sums
temp3.head(6)

#slicing of the dataframes
myflowserie['L06_347']['2009'].plot()

"""
Explanation: Forwarding Pandas functionalities
End of explanation
"""


# get_month, get_year, get_season, get_date_range
myflowserie.get_date_range("01/01/2010","03/05/2010").plot(figsize=(13, 6))

# or combine different statements:
myflowserie.get_year('2010').get_month(6).plot(figsize=(13, 6))

"""
Explanation: Easy period selection
End of explanation
"""


myflowserie.current_season_dates()

myflowserie.info_season_dates('north', 'astro')

"""
Explanation: For the seasons some options are available: Meteorologic (first of the month) or astrologic (21st of the month)
End of explanation
"""


# Peaks (high or low)
myflowserie['LS06_348'].get_year('2012').get_highpeaks(60, above_percentile=0.8).data.dropna().head()

# Recessions and climbing periods  get_recess, get_climbing
myflowserie.get_year("2012").get_month("april").get_climbing().plot(figsize=(13, 6))

# above/below certain percentile values
myflowserie["LS06_348"].get_above_percentile(0.6).get_year('2011').get_season('summer').plot()

"""
Explanation: 'Hydrological' selections
End of explanation
"""


