예제 #1
0
def test_request():

    observations = DWDObservationData(
        station_ids=[1048],
        parameters=DWDObservationParameterSet.CLIMATE_SUMMARY,
        resolution=DWDObservationResolution.DAILY,
        periods=DWDObservationPeriod.RECENT,
    )

    df = observations.collect_safe()
    assert not df.empty
예제 #2
0
def test_export_crate():

    observations = DWDObservationData(
        station_ids=[1048],
        parameters=DWDObservationParameterSet.CLIMATE_SUMMARY,
        resolution=DWDObservationResolution.DAILY,
        periods=DWDObservationPeriod.RECENT,
    )

    with mock.patch("pandas.DataFrame.to_sql", ) as mock_to_sql:

        df = observations.collect_safe()
        df.io.export("crate://localhost/?database=test&table=testdrive")

        mock_to_sql.assert_called_once_with(
            name="testdrive",
            con="crate://localhost/?database=test&table=testdrive",
            if_exists="replace",
            index=False,
            method="multi",
            chunksize=5000,
        )
예제 #3
0
def test_export_influxdb():

    observations = DWDObservationData(
        station_ids=[1048],
        parameters=DWDObservationParameterSet.CLIMATE_SUMMARY,
        resolution=DWDObservationResolution.DAILY,
        periods=DWDObservationPeriod.RECENT,
    )

    mock_client = mock.MagicMock()
    with mock.patch(
            "influxdb.dataframe_client.DataFrameClient",
            side_effect=[mock_client],
            create=True,
    ) as mock_connect:

        df = observations.collect_safe()
        df.dwd.lower().io.export(
            "influxdb://localhost/?database=dwd&table=weather")

        mock_connect.assert_called_once_with(database="dwd")
        mock_client.create_database.assert_called_once_with("dwd")
        mock_client.write_points.assert_called_once()
예제 #4
0
def test_export_duckdb():

    observations = DWDObservationData(
        station_ids=[1048],
        parameters=DWDObservationParameterSet.CLIMATE_SUMMARY,
        resolution=DWDObservationResolution.DAILY,
        periods=DWDObservationPeriod.RECENT,
    )

    mock_connection = mock.MagicMock()
    with mock.patch("duckdb.connect",
                    side_effect=[mock_connection],
                    create=True) as mock_connect:

        df = observations.collect_safe()
        df.io.export("duckdb:///test.duckdb?table=testdrive")

        mock_connect.assert_called_once_with(database="test.duckdb",
                                             read_only=False)
        mock_connection.register.assert_called_once()
        mock_connection.execute.assert_called()
        mock_connection.table.assert_called_once_with("testdrive")
        # a.table.to_df.assert_called()
        mock_connection.close.assert_called_once()
예제 #5
0
def dwd_readings(
    station: str = Query(default=None),
    parameter: str = Query(default=None),
    resolution: str = Query(default=None),
    period: str = Query(default=None),
    date: str = Query(default=None),
    sql: str = Query(default=None),
):
    """
    Acquire data from DWD.

    # TODO: Obtain lat/lon distance/number information.

    :param station:     Comma-separated list of station identifiers.
    :param parameter:   Observation measure
    :param resolution:  Frequency/granularity of measurement interval
    :param period:      Recent or historical files
    :param date:        Date or date range
    :param sql:         SQL expression
    :return:
    """

    if station is None:
        raise HTTPException(
            status_code=400, detail="Query argument 'station' is required"
        )

    if parameter is None or resolution is None or period is None:
        raise HTTPException(
            status_code=400,
            detail="Query arguments 'parameter', 'resolution' "
            "and 'period' are required",
        )

    station_ids = map(int, read_list(station))
    parameter = parse_enumeration_from_template(parameter, DWDObservationParameterSet)
    resolution = parse_enumeration_from_template(resolution, DWDObservationResolution)
    period = parse_enumeration_from_template(period, DWDObservationPeriod)

    # Data acquisition.
    observations = DWDObservationData(
        station_ids=station_ids,
        parameters=parameter,
        resolution=resolution,
        periods=period,
        tidy_data=True,
        humanize_column_names=True,
    )

    # Postprocessing.
    df = observations.collect_safe()

    if date is not None:
        df = df.dwd.filter_by_date(date, resolution)

    df = df.dwd.lower()

    if sql is not None:
        df = df.io.sql(sql)

    data = json.loads(df.to_json(orient="records", date_format="iso"))
    return make_json_response(data)