コード例 #1
0
def test_run_catchmod_subcatchment_flows():
    """ test the subcatchment flows returned sum to the returned total flows """

    catchment = catchment_from_json(os.path.join(os.path.dirname(__file__),
                                                 "data", "thames.json"),
                                    n=2)
    dates = pandas.date_range("1920-01-01", periods=200, freq="D")

    rainfall = np.random.randint(0, 20, [200, 2])
    pet = np.random.randint(0, 5, [200, 2])

    flow_total = run_catchmod(catchment,
                              rainfall,
                              pet,
                              dates,
                              output_total=True)
    flow_subcatchment = run_catchmod(catchment,
                                     rainfall,
                                     pet,
                                     dates,
                                     output_total=False)

    np.testing.assert_allclose(flow_total, flow_subcatchment.sum(axis=2))

    # test correct flow reshape in __main__
    flow_reshape = flow_subcatchment.reshape(
        (len(dates), len(catchment.subcatchments) * 2), order='C')

    np.testing.assert_allclose(flow_reshape[:, 0], flow_subcatchment[:, 0, 0])
    np.testing.assert_allclose(flow_reshape[:, 3], flow_subcatchment[:, 1, 0])
コード例 #2
0
def test_run_catchmod(leap, total_flows):
    """ Test the shape of output is correct if when there are leap dates/no leap days
    and for returning total flows or individual subcatchment flows
    """
    shape = [200, 4]
    catchment = catchment_from_json(os.path.join(os.path.dirname(__file__),
                                                 "data", "thames.json"),
                                    n=shape[1])
    dates = pandas.date_range("1920-01-01", periods=200, freq="D")
    if leap:
        # remove input data for leap days
        num_leap = sum(
            [1 for date in dates if (date.month == 2 and date.day == 29)])
        shape[0] -= num_leap
    rainfall = np.ones(shape)
    pet = np.zeros(shape)
    flow = run_catchmod(catchment, rainfall, pet, dates, total_flows)

    assert (flow.shape[0] == len(dates))
    if total_flows:
        assert (len(flow.shape) == 2)
        assert (flow.shape[1] == rainfall.shape[1])
    else:
        assert (len(flow.shape) == 3)
        assert (flow.shape[2] == len(catchment.subcatchments))
        assert (flow.shape[1] == rainfall.shape[1])
コード例 #3
0
def test_run_catchmod(leap):
    shape = [200, 3]
    catchment = catchment_from_json(os.path.join(os.path.dirname(__file__), "data", "thames.json"), n=shape[1])
    dates = pandas.date_range("1920-01-01", periods=200, freq="D")
    if leap:
        # remove input data for leap days
        num_leap = sum([1 for date in dates if (date.month == 2 and date.day == 29)])
        shape[0] -= num_leap
    rainfall = np.ones(shape)
    pet = np.zeros(shape)
    flow = run_catchmod(catchment, rainfall, pet, dates)
    assert(flow.shape[0] == len(dates))
    assert(flow.shape[1] == rainfall.shape[1])
コード例 #4
0
ファイル: __main__.py プロジェクト: muguangyuze/pycatchmod
def run(ctx, parameters, rainfall, pet, output):
    import pandas
    import numpy as np
    from pycatchmod.io.json import catchment_from_json
    from pycatchmod.io.excel import excel_parameter_adjustment
    from pycatchmod import run_catchmod

    # load rainfall and pet data
    rainfall = pandas_read(rainfall)
    pet = pandas_read(pet)
    num_scenarios = rainfall.shape[1]

    # load catchmod model
    catchment = catchment_from_json(parameters, n=num_scenarios)
    excel_parameter_adjustment(catchment)

    idx0 = rainfall.index[0]
    idxN = rainfall.index[-1]
    dates = pandas.date_range(idx0, idxN, freq="D")

    # run catchmod
    flows = run_catchmod(catchment, rainfall.values, pet.values, dates=dates)
    print("Shape", flows.shape)

    df = pandas.DataFrame(flows, index=dates)
    df.index.name = "Date"

    # write output
    if output.endswith((".h5", ".hdf", ".hdf5")):
        df.to_hdf(output, key="flows", compress="zlib")
    elif output.endswith(".csv"):
        df.to_csv(output)
    else:
        raise ValueError("Unrecognised output format.")

    print("Results:", output)
コード例 #5
0
ファイル: __main__.py プロジェクト: snorfalorpagus/pycatchmod
def run(ctx, parameters, rainfall, pet, output):
    import pandas
    import numpy as np
    from pycatchmod.io.json import catchment_from_json
    from pycatchmod.io.excel import excel_parameter_adjustment
    from pycatchmod import run_catchmod

    # load rainfall and pet data
    rainfall = pandas_read(rainfall)
    pet = pandas_read(pet)
    num_scenarios = rainfall.shape[1]

    # load catchmod model
    catchment = catchment_from_json(parameters, n=num_scenarios)
    excel_parameter_adjustment(catchment)

    idx0 = rainfall.index[0]
    idxN = rainfall.index[-1]
    dates = pandas.date_range(idx0, idxN, freq="D")

    # run catchmod
    flows = run_catchmod(catchment, rainfall.values, pet.values, dates=dates)
    print("Shape", flows.shape)

    df = pandas.DataFrame(flows, index=rainfall.index)
    df.index.name = "Date"

    # write output
    if output.endswith((".h5", ".hdf", ".hdf5")):
        df.to_hdf(output, key="flows", compress="zlib")
    elif output.endswith(".csv"):
        df.to_csv(output)
    else:
        raise ValueError("Unrecognised output format.")

    print("Results:", output)
コード例 #6
0
ファイル: test_utils.py プロジェクト: muguangyuze/pycatchmod
def test_json(thames_json):
    catchment = catchment_from_json(thames_json)
    assert (catchment.name == 'Thames')
    assert (len(catchment.subcatchments) == 3)
    assert (catchment.subcatchments[0].area == 3900.0)
コード例 #7
0
ファイル: __main__.py プロジェクト: Sephra1/pycatchmod
def run(ctx, parameters, rainfall, pet, total, output, output_key,
        rainfall_key, pet_key, complib, complevel, output_mode):
    import pandas
    import numpy as np
    from pycatchmod.io.json import catchment_from_json
    from pycatchmod.io.excel import excel_parameter_adjustment
    from pycatchmod import run_catchmod

    print("Loading input data...")

    # load rainfall and pet data
    rainfall = pandas_read(rainfall, rainfall_key)
    pet = pandas_read(pet, pet_key)
    num_scenarios = rainfall.shape[1]

    # load catchmod model
    catchment = catchment_from_json(parameters, n=num_scenarios)
    excel_parameter_adjustment(catchment)

    idx0 = rainfall.index[0]
    idxN = rainfall.index[-1]
    dates = pandas.period_range(idx0, idxN, freq="D")

    # run catchmod
    print("Running catchmod...")
    flows = run_catchmod(catchment,
                         rainfall.values,
                         pet.values,
                         dates=dates,
                         output_total=total)
    print("Shape:", flows.shape)

    if total:
        df = pandas.DataFrame(flows, index=dates)
        df.index.name = "Date"
    else:
        flows = flows.reshape(
            (len(dates), len(catchment.subcatchments) * num_scenarios),
            order='C')
        df = pandas.DataFrame(
            flows,
            columns=pandas.MultiIndex.from_product([
                range(num_scenarios),
                [x.name for x in catchment.subcatchments]
            ],
                                                   names=('Scenarios',
                                                          'Subcatchments')),
            index=dates)

    # write output
    if output.endswith((".h5", ".hdf", ".hdf5")):
        df.to_hdf(output,
                  key=output_key,
                  mode=output_mode,
                  complib=complib,
                  complevel=complevel)
    elif output.endswith(".csv"):
        # workaround for bug in pandas
        # see https://github.com/pandas-dev/pandas/issues/15982
        df.reset_index().to_csv(output, index=False)
    else:
        raise ValueError("Unrecognised output format.")

    print("Results:", output)
コード例 #8
0
def test_json(thames_json):
    catchment = catchment_from_json(thames_json)
    assert(catchment.name == 'Thames')
    assert(len(catchment.subcatchments) == 3)
    assert(catchment.subcatchments[0].area == 3900.0)