Ejemplo n.º 1
0
def test_model_gr4j_run(dataset_l0123001):
    air_gr_parameters = [265.072, 1.040]
    air_gr_rmse = 17.804161

    # One month aggregation using period as index :
    precipitation = dataset_l0123001['precipitation'].resample(
        'M', kind='period').sum()
    temperature = dataset_l0123001['temperature'].resample(
        'M', kind='period').mean()
    evapotranspiration = dataset_l0123001['evapotranspiration'].resample(
        'm', kind='period').sum()
    flow_mm = dataset_l0123001['flow_mm'].resample('M', kind='period').sum()
    df = pd.concat([precipitation, temperature, evapotranspiration, flow_mm],
                   axis=1)
    df['date'] = df.index

    input_handler = InputDataHandler(ModelGr2m, df)

    warm_up_start_date = datetime.datetime(1989, 1, 1, 0, 0)
    start_date = datetime.datetime(1990, 1, 1, 0, 0)
    end_date = datetime.datetime(1999, 12, 1, 0, 0)
    sub_input = input_handler.get_sub_period(warm_up_start_date, end_date)

    model = ModelGr2m(sub_input, air_gr_parameters)
    outputs = model.run()

    filtered_input = model.input_data[model.input_data.index >= start_date]
    filtered_output = outputs[outputs.index >= start_date]

    rmse = sqrt(
        mean(
            (filtered_output['Qsim'] - filtered_input['flow_mm'].values)**2.0))
    assert round(rmse, 6) == round(air_gr_rmse, 6)
Ejemplo n.º 2
0
def test_model_gr1a_run(dataset_l0123001):
    air_gr_parameters = [0.840]
    air_gr_rmse = 64.70577504958929  # Slightly different from airGR rmse as R method for resampling return NA for 1996 observed flow...

    # One year aggregation :
    precipitation = dataset_l0123001['precipitation'].resample('A').sum()
    temperature = dataset_l0123001['temperature'].resample('A').mean()
    evapotranspiration = dataset_l0123001['evapotranspiration'].resample('A').sum()
    flow_mm = dataset_l0123001['flow_mm'].resample('A').sum()
    df = pd.concat([precipitation, temperature, evapotranspiration, flow_mm], axis=1)
    df['date'] = df.index

    input_handler = InputDataHandler(ModelGr1a, df)

    warm_up_start_date = datetime.datetime(1989, 1, 1, 0, 0)
    start_date = datetime.datetime(1990, 1, 1, 0, 0)
    end_date = datetime.datetime(1999, 12, 31, 0, 0)
    sub_input = input_handler.get_sub_period(warm_up_start_date, end_date)

    model = ModelGr1a(sub_input, air_gr_parameters)
    outputs = model.run()

    filtered_input = model.input_data[model.input_data.index >= start_date]
    filtered_output = outputs[outputs.index >= start_date]

    rmse = sqrt(mean((filtered_output['Qsim'] - filtered_input['flow_mm'].values) ** 2.0))
    assert round(rmse, 6) == round(air_gr_rmse, 6)
Ejemplo n.º 3
0
def test_input_data_fail(dataset_l0123001):
    with pytest.raises(ValueError):
        _ = InputDataHandler(ModelGr1a, dataset_l0123001)
    with pytest.raises(ValueError):
        _ = InputDataHandler(ModelGr2m, dataset_l0123001)
    with pytest.raises(ValueError):
        _ = InputDataHandler(ModelGr4h, dataset_l0123001)
Ejemplo n.º 4
0
    def __init__(self, data, start_date, end_date):
        self.start = start_date
        self.end = end_date

        self.data = data
        self.model_inputs = InputDataHandler(ModelGr4j, self.data)
        self.bound = [[0.0, 200.0], [0.0, 1.0], [0.0, 100.0],
                      [0.0, 10.0]]  # Physical parameter boundaries
Ejemplo n.º 5
0
def test_input_yearly_data_period_index(dataset_l0123001):
    # One year aggregation with datetime index :
    precipitation = dataset_l0123001['precipitation'].resample('A', kind='period').sum()
    temperature = dataset_l0123001['temperature'].resample('A', kind='period').mean()
    evapotranspiration = dataset_l0123001['evapotranspiration'].resample('A', kind='period').sum()
    flow_mm = dataset_l0123001['flow_mm'].resample('A', kind='period').sum()
    df = pd.concat([precipitation, temperature, evapotranspiration, flow_mm], axis=1)
    df['date'] = df.index

    _ = InputDataHandler(ModelGr1a, df)
Ejemplo n.º 6
0
def test_model_gr6j_run(dataset_l0123001):
    air_gr_parameters = [242.257, 0.637, 53.517, 2.218, 0.424, 4.759]
    air_gr_rmse = 0.785089

    input_handler = InputDataHandler(ModelGr6j, dataset_l0123001)

    warm_up_start_date = datetime.datetime(1989, 1, 1, 0, 0)
    start_date = datetime.datetime(1990, 1, 1, 0, 0)
    end_date = datetime.datetime(1999, 12, 31, 0, 0)
    sub_input = input_handler.get_sub_period(warm_up_start_date, end_date)

    model = ModelGr6j(sub_input, air_gr_parameters)
    outputs = model.run()

    filtered_input = model.input_data[model.input_data.index >= start_date]
    filtered_output = outputs[outputs.index >= start_date]

    rmse = sqrt(
        mean(
            (filtered_output['Qsim'] - filtered_input['flow_mm'].values)**2.0))

    assert pytest.approx(rmse) == air_gr_rmse
Ejemplo n.º 7
0
def test_model_gr5j_run(dataset_l0123001):
    air_gr_parameters = [245.918, 1.027, 90.017, 2.198, 0.434]
    air_gr_rmse = 0.8072707

    input_handler = InputDataHandler(ModelGr5j, dataset_l0123001)

    warm_up_start_date = datetime.datetime(1989, 1, 1, 0, 0)
    start_date = datetime.datetime(1990, 1, 1, 0, 0)
    end_date = datetime.datetime(1999, 12, 31, 0, 0)
    sub_input = input_handler.get_sub_period(warm_up_start_date, end_date)

    model = ModelGr5j(sub_input, air_gr_parameters)
    outputs = model.run()

    filtered_input = model.input_data[model.input_data.index >= start_date]
    filtered_output = outputs[outputs.index >= start_date]

    rmse = sqrt(
        mean(
            (filtered_output['Qsim'] - filtered_input['flow_mm'].values)**2.0))

    assert pytest.approx(rmse) == air_gr_rmse
Ejemplo n.º 8
0
def test_input_data_get_sub_period(dataset_l0123001):
    input_data_handler = InputDataHandler(ModelGr4j, dataset_l0123001)
    main_start_date = datetime.datetime(1984, 1, 1, 0, 0, 0)
    main_end_date = datetime.datetime(2012, 12, 31, 0, 0, 0)
    assert input_data_handler.start_date == main_start_date
    assert input_data_handler.end_date == main_end_date

    start_date = datetime.datetime(1989, 1, 1, 0, 0)
    end_date = datetime.datetime(1999, 12, 31, 0, 0)
    sub_input = input_data_handler.get_sub_period(start_date, end_date)
    assert input_data_handler.start_date == main_start_date
    assert input_data_handler.end_date == main_end_date
    assert sub_input.start_date == start_date
    assert sub_input.end_date == end_date

    start_date = datetime.datetime(1970, 1, 1, 0, 0)
    end_date = datetime.datetime(2222, 12, 31, 0, 0)
    sub_input = input_data_handler.get_sub_period(start_date, end_date)
    assert input_data_handler.start_date == main_start_date
    assert input_data_handler.end_date == main_end_date
    assert sub_input.start_date == main_start_date
    assert sub_input.end_date == main_end_date
Ejemplo n.º 9
0
def test_model_gr4h_run(dataset_l0123003):
    air_gr_parameters = [521.113, -2.918, 218.009, 4.124]
    air_gr_rmse = 0.07847187

    input_handler = InputDataHandler(ModelGr4h, dataset_l0123003)

    warm_up_start_date = datetime.datetime(2004, 1, 1, 0, 0)
    start_date = datetime.datetime(2004, 3, 1, 0, 0)
    end_date = datetime.datetime(2008, 12, 31, 0, 0)
    sub_input = input_handler.get_sub_period(warm_up_start_date, end_date)

    model = ModelGr4h(sub_input, air_gr_parameters)
    outputs = model.run()

    filtered_input = model.input_data[model.input_data.index >= start_date]
    filtered_output = outputs[outputs.index >= start_date]

    rmse = sqrt(
        mean(
            (filtered_output['Qsim'] - filtered_input['flow_mm'].values)**2.0))
    assert round(rmse,
                 4) == round(air_gr_rmse,
                             4)  # slightly different after the 4th decimal...
Ejemplo n.º 10
0
from pathlib import Path
import pandas as pd
import datetime
from hydrogr import InputDataHandler, ModelGr6j
from hydrogr.plot_tools import plot_hydrograph

if __name__ == '__main__':

    data_path = Path.cwd().parent / 'data'
    df = pd.read_pickle(data_path / 'L0123001.pkl')
    df.columns = [
        'date', 'precipitation', 'temperature', 'evapotranspiration', 'flow',
        'flow_mm'
    ]
    df.index = df['date']

    air_gr_parameters = [242.257, 0.637, 53.517, 2.218, 0.424, 4.759]

    input_handler = InputDataHandler(ModelGr6j, df)

    start_date = datetime.datetime(1989, 1, 1, 0, 0)
    end_date = datetime.datetime(1999, 12, 31, 0, 0)
    sub_input = input_handler.get_sub_period(start_date, end_date)

    model = ModelGr6j(sub_input, air_gr_parameters)
    model.set_initial_conditions(production_store_filling=0.3,
                                 routing_store_filling=0.5)
    outputs = model.run()

    plot_hydrograph(outputs, sub_input.data['flow_mm'])
Ejemplo n.º 11
0
        'date', 'precipitation', 'temperature', 'evapotranspiration', 'flow',
        'flow_mm'
    ]
    df.index = df['date']

    # One month aggregation using period as index :
    precipitation = df['precipitation'].resample('M', kind='period').sum()
    temperature = df['temperature'].resample('M', kind='period').mean()
    evapotranspiration = df['evapotranspiration'].resample(
        'm', kind='period').sum()
    flow_mm = df['flow_mm'].resample('M', kind='period').sum()
    df = pd.concat([precipitation, temperature, evapotranspiration, flow_mm],
                   axis=1)
    df['date'] = df.index

    input_handler = InputDataHandler(ModelGr2m, df)

    warm_up_start_date = datetime.datetime(1989, 1, 1, 0, 0)
    start_date = datetime.datetime(1990, 1, 1, 0, 0)
    end_date = datetime.datetime(1999, 1, 1, 0, 0)
    sub_input = input_handler.get_sub_period(warm_up_start_date, end_date)

    air_gr_parameters = [265.072, 1.040]
    model = ModelGr2m(sub_input, air_gr_parameters)
    outputs = model.run()

    filtered_input = model.input_data[model.input_data.index >= start_date]
    filtered_output = outputs[outputs.index >= start_date]

    plot_hydrograph(filtered_output, sub_input.data['flow_mm'])
Ejemplo n.º 12
0
def test_input_hourly_data_datetime_index(dataset_l0123003):
    _ = InputDataHandler(ModelGr4h, dataset_l0123003)
Ejemplo n.º 13
0
def test_input_daily_data_datetime_index(dataset_l0123001):
    _ = InputDataHandler(ModelGr4j, dataset_l0123001)
Ejemplo n.º 14
0
import pandas as pd
import datetime
from hydrogr import InputDataHandler, ModelGr4h
from hydrogr.plot_tools import plot_hydrograph

if __name__ == '__main__':

    data_path = Path.cwd().parent / 'data'
    # df = pd.read_csv(data_path / 'L0123003.csv')
    # df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y %H:%M')
    # df.to_pickle(data_path / 'L0123003.pkl')
    df = pd.read_pickle(data_path / 'L0123003.pkl')
    df.columns = [
        'date', 'precipitation', 'temperature', 'evapotranspiration', 'flow',
        'flow_mm'
    ]
    df.index = df['date']

    my_inputs_handler = InputDataHandler(ModelGr4h, df)
    filtered_input = my_inputs_handler.get_sub_period(
        datetime.datetime(2004, 1, 1, 0, 0),
        datetime.datetime(2008, 1, 1, 0, 0))

    model = ModelGr4h(filtered_input, [521.113, -2.918, 218.009, 4.124])
    model.set_initial_conditions(production_store_filling=0.9,
                                 routing_store_filling=0.9)
    outputs = model.run()
    print(outputs.head())

    plot_hydrograph(outputs, my_inputs_handler.data['flow_mm'])