Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions catchment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,38 @@ def read_variable_from_csv(filename):

return newdataset


def read_variable_from_xml(filename):
"""Reads a named variable from a XML file, and returns a
pandas dataframe containing that variable. The XML file must contain
a column of dates, a column of site ID's, and (one or more) columns
of data - only one of which will be read.

:param filename: Filename of XML to load
:return: 2D array of given variable. Index will be dates,
Columns will be the individual sites
"""
dataset = pd.read_xml(filename)
dataset = dataset.rename({'Date':'OldDate', "Site_Name": "Site Name", "Rainfall_mm": "Rainfall (mm)"}, axis='columns')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubbish.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry boss

dataset['Date'] = [pd.to_datetime(x, dayfirst=True) for x in dataset['OldDate']]
dataset = dataset.drop('OldDate', axis='columns')

newdataset = pd.DataFrame(index=dataset['Date'].unique())

for site in dataset['Site Name'].unique():
newdataset[site] = dataset[dataset['Site'] == site].set_index('Date')["Rainfall (mm)"]

newdataset = newdataset.sort_index()

return newdataset


def daily_total(data):
"""Calculate the daily total of a 2D data array.
Index must be np.datetime64 compatible format."""
return data.groupby(data.index.date).sum()


def daily_mean(data):
"""Calculate the daily mean of a 2D data array.
Index must be np.datetime64 compatible format."""
Expand Down
Loading