To create time series models with Pastas, you can use a Pastas Project. A Project is a Python class that contains observations, stresses and models of multiple locations. This class has convenient methods to store time series data, create models, add stressmodels and summarize the results. This notebook is an example of the current possibilities.
Pastas project is deprecated and will be removed from Pastas at the end of 2020. Check out our new package Pastastore to deal with multiple time series and Pastas models.
In [1]:
# First perform the necessary imports
import os
import pandas as pd
import matplotlib.pyplot as plt
import pastas as ps
%matplotlib inline
ps.show_versions()
In [2]:
## Starting a new Project
pr = ps.Project(name='project for notebook')
We can add oseries to the project. Set kind to 'oseries' to add the series as observations. Observation-series are added to pr.oseries, which is a Pandas DataFrame. The measurement-TimeSeries is in the 'series'-column. Metadata provided to add_series is shown in the other columns of pr.oseries.
In [3]:
# add the observations in all the files in the data-directory that end with _1.csv
datapath = '../data'
files = [x for x in os.listdir(datapath) if x.endswith('_1.csv')]
for file in files:
fname = os.path.join(datapath,file)
series = ps.read_dino(fname)
pr.add_series(series,kind='oseries')
# show the contents of pr.oseries
pr.oseries
Out[3]:
And we can add stresses. To make pr.add_recharge work later, we have to add the precipitation-series as kind='prec' and the evaporation-series as kind='evap'. Stress-series are added to pr.stresses, which is a Pandas DataFrame (just like pr.oseries). The stress-TimeSeries is in the 'series'-column. Metadata provided to add_series is shown in the other columns of pr.stresses.
In [4]:
# add evaporation
fname = os.path.join(datapath,'etmgeg_380.txt')
series = ps.read_knmi(fname, variables='EV24')
pr.add_series(series, kind='evap', settings='evap')
# add precipitation
fname = os.path.join(datapath,'KNMI_Akkrum.txt')
series = ps.read_knmi(fname, variables='RD')
pr.add_series(series, kind='prec', settings='prec')
# show the contents of pr.stresses
pr.stresses
Out[4]:
We can make models and add recharge. Models are added to pr.models, which is a dictionary with the model-names as the keys, and the models as the values. The add_recharge method finds the closest precipitation- and evaporation-series to the measurement location that the model describes.
The file that we used for precipitation did not contain any coordinates, which will therefore default to 0.0. The evaporation-file contains coordinates in epsg:4326, while our observation-files contain coordinates in epsg:28992. Right now we do not transform coordinates. So finding the closest precipitation- and evaporation-series will normally give wrong results. As we have only one precipitation and evaporation series however, this is not a problem.
In the code-section below, we make three models with recharge and solve them.
In [5]:
for name in pr.oseries.index:
ml = pr.add_model(name)
pr.add_recharge(ml)
ml.solve(report=False)
# show the contents of pr.models
pr.models
Out[5]:
In [6]:
name = 'B58C0698_1'
ml = pr.models[name]
ml.plots.decomposition(split=True)
Out[6]:
In [7]:
pr.get_statistics(['evp','aic'])
Out[7]:
Make a table with some parameters of the models
In [8]:
pr.get_parameters(['recharge_A','constant_d','noise_alpha'])
Out[8]:
In [9]:
f,ax= plt.subplots()
ax.axis('equal')
pr.maps.series(kind='oseries')
Out[9]:
In [10]:
pr.to_file('pastas_project.pas')
Later we can reload this project again
In [11]:
pr = ps.io.load_project('pastas_project.pas')
Test if everything went ok by plotting the decomposition of B58C0698_1 again. This figure is exactly the same as before.
In [12]:
name = 'B58C0698_1'
ml = pr.models[name]
ml.plots.decomposition()
Out[12]: