示例#1
0
def test_download_stored_query_notimplemented():
    """Test that proper error is raised for invalid request."""
    from fmiopendata.wfs import download_stored_query
    import pytest

    with pytest.raises(NotImplementedError):
        _ = download_stored_query("foo::bar::baz")
示例#2
0
def update_current_weather():
    global weather_station
    global weather_data
    global current_weather
    global current_date

    start_time = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
    end_time = datetime.datetime.utcnow() + datetime.timedelta(hours=3)
    start_time = start_time.isoformat(timespec="seconds") + "Z"
    end_time = end_time.isoformat(timespec="seconds") + "Z"
    obs = download_stored_query(
        "fmi::observations::weather::multipointcoverage",
        args=[
            "bbox=20,59,32,71", "timeseries=True", "starttime=" + start_time,
            "endtime=" + end_time
        ])

    data = obs.data[weather_station]
    rain = data["r_1h"]["values"][-1]
    current_temperature = data["t2m"]["values"][-1]
    gustSpeed = data["wg_10min"]["values"][-1]
    windSpeed = data["ws_10min"]["values"][-1]
    windDirection = data["wd_10min"]["values"][-1]
    humidity = data["rh"]["values"][-1]
    dew_temperature = data["td"]["values"][-1]
    rain_intensity = data["ri_10min"]["values"][-1]
    snow = data["snow_aws"]["values"][-1]
    pressure = data["p_sea"]["values"][-1]
    visibility = data["vis"]["values"][-1]
    clouds = data["n_man"]["values"][-1]
    latest_observation = data["times"][-1]
    latest_observation = latest_observation.isoformat(timespec="seconds") + "Z"

    current_date = {
        "day": latest_observation[8:10],
        "month": latest_observation[5:7],
        "year": latest_observation[0:4],
        "time": latest_observation[11:13],
        "min": latest_observation[14:16]
    }
    current_weather = {
        "d":
        [str(int(current_date["month"]) * 100 + int(current_date["day"]))],
        "time": [current_date["time"]],
        "min": [current_date["min"]],
        "rain": [rain, data["r_1h"]["unit"]],
        "temperature": [current_temperature, data["t2m"]["unit"]],
        "gust_speed": [gustSpeed, data["wg_10min"]["unit"]],
        "wind_speed": [windSpeed, data["ws_10min"]["unit"]],
        "weather_station": [weather_station],
        "wind_direction": [windDirection, data["wd_10min"]["unit"]],
        "humidity": [humidity, data["rh"]["unit"]],
        "dew_temperature": [dew_temperature, data["td"]["unit"]],
        "rain_intensity": [rain_intensity, data["ri_10min"]["unit"]],
        "snow": [snow, data["snow_aws"]["unit"]],
        "pressure": [pressure, data["p_sea"]["unit"]],
        "visibility": [visibility, data["vis"]["unit"]],
        "clouds": [clouds, data["n_man"]["unit"]]
    }
示例#3
0
def test_download_stored_query_multipointcoverage():
    """Test that download/parse function is imported from multipoint module."""
    with mock.patch("fmiopendata.multipoint.download_and_parse") as dap:
        from fmiopendata.wfs import download_stored_query

        res = download_stored_query("foo::multipointcoverage::bar")
        del res
    dap.assert_called_once()
示例#4
0
def test_download_stored_query_grid():
    """Test that download/parse function is imported from grid module."""
    with mock.patch("fmiopendata.grid.download_and_parse") as dap:
        from fmiopendata.wfs import download_stored_query

        res = download_stored_query("foo::grid::bar")
        del res
    dap.assert_called_once()
示例#5
0
def get_weather_observations(start, end, bbox):
    start_str = start.isoformat(timespec="seconds") + "Z"
    end_str = end.isoformat(timespec="seconds") + "Z"

    query = "fmi::observations::weather::multipointcoverage"

    res = download_stored_query(query,
                                args=[f"bbox={bbox}",
                                      "starttime=" + start_str,
                                      "endtime=" + end_str])
    return res.data
示例#6
0
def give_prediction(stationId, month, day, hour):
    place = "latlon=60.3267,24.95675"  # default place is Veromiehenkylä
    stationShortCode = ''
    weather_area = 0
    station_name = ''
    with open("utils/stations.json", 'r', encoding="utf-8") as f:
        stations = json.load(f)
        for station in stations:
            if station['stationShortCodeCategory'] == stationId:
                stationShortCode = station['stationShortCode']
                station_name = station['stationName']
                break
    if stationShortCode != '':
        with open("utils/weather_stations.json", 'r', encoding="utf-8") as f:
            weather_stations = json.load(f)
            weather_area = weather_stations.get(stationShortCode)
        with open("utils/weather-locations.json", 'r', encoding="utf-8") as f:
            places = json.load(f)
            place = places.get(str(weather_area))['latlon']

    now = datetime.datetime.utcnow()
    end_time = datetime.datetime(now.year, month, day, hour)
    start_time = end_time - datetime.timedelta(hours=1)
    # Convert times to properly formatted strings
    start_time = start_time.isoformat(timespec="seconds") + "Z"
    # -> 2020-07-07T12:00:00Z
    end_time = end_time.isoformat(timespec="seconds") + "Z"
    # -> 2020-07-07T13:00:00Z
    obs = download_stored_query(
        "fmi::forecast::hirlam::surface::point::multipointcoverage",
        args=["starttime=" + start_time, "endtime=" + end_time, place])

    print(obs.location_metadata)
    print(obs.data)
    time_of_day = max(obs.data.keys())
    print('timestamp', time_of_day)

    weather_station = list(obs.data[time_of_day].keys())[0]
    print(weather_station)

    data = obs.data[time_of_day][weather_station]
    rain = data['Precipitation amount 1 hour']['value']
    celcius = data['Air temperature']['value']
    windGustSpeed = data['Wind gust']['value']
    windSpeed = data['Wind speed']['value']

    weather = [
        rain, celcius, windGustSpeed, windSpeed, station_name, weather_station,
        time_of_day, weather_area
    ]
    return weather
示例#7
0
# COMMAND ----------

# Fetch weather observation data for the specified time range for the hard coded location: Kumpula
from fmiopendata.wfs import download_stored_query
import datetime as dt

startTime = dt.datetime.strptime(dbutils.widgets.get("start_time"),
                                 "%Y-%m-%dT%H:%M:%S.%f+0000")
endTime = dt.datetime.strptime(dbutils.widgets.get("end_time"),
                               "%Y-%m-%dT%H:%M:%S.%f+0000")
startTime = startTime.isoformat(timespec="seconds") + "Z"
endTime = endTime.isoformat(timespec="seconds") + "Z"
print("{} {}".format(startTime, endTime))
obs = download_stored_query("fmi::observations::weather::multipointcoverage",
                            args=[
                                "place=Kumpula", "starttime=" + startTime,
                                "endtime=" + endTime, "timeseries=True"
                            ])

# COMMAND ----------

# Create dataframe from multi-dimension dict objects
import pandas as pd
from pyspark.sql.functions import *

location = 'Helsinki Kumpula'
keys = obs.data[location]['times']
temperature = obs.data[location]['t2m']['values']
wind = obs.data[location]['ws_10min']['values']
windHigh = obs.data[location]['wg_10min']['values']
windDirection = obs.data[location]['wd_10min']['values']
示例#8
0
 async def fetch_weather(self):
     d = download_stored_query(w_query, ["place=" + self.location])
     print("weather fetched")
     _data = {"content": d, "pipe": 2}
     self.send_data_to_be_processed(_data)
place_index = [
    "Helsinki", "Espoo", "Kirkkonummi", "Lahti", "Hämeenlinna", "Tampere",
    "Hyvinkää", "Kouvola"
]
places = [
    "latlon=60.3267,24.95675", "latlon=60.17797,24.78743",
    "latlon=60.29128,24.56782", "latlon=60.97465,25.6202", "latlon=61,24.49",
    "latlon=61.50124,23.76478", "latlon=60.6,24.8", "latlon=60.7,26.81"
]

rows = []

for place in places:

    obs = download_stored_query(
        "fmi::forecast::hirlam::surface::point::multipointcoverage",
        args=["starttime=" + start_time, "endtime=" + end_time, place])

    print(obs.location_metadata)
    print(obs.data)
    time_of_day = max(obs.data.keys())
    print('timestamp', time_of_day)

    weather_station = list(obs.data[time_of_day].keys())[0]
    print(weather_station)

    data = obs.data[time_of_day][weather_station]
    rain = data['Precipitation amount 1 hour']['value']
    celcius = data['Air temperature']['value']
    windGustSpeed = data['Wind gust']['value']
    windSpeed = data['Wind speed']['value']
示例#10
0
def outdoor():
    location = request.args.get('location')

    # Handle date inputs
    if('start_date' in request.args):
        try:
            startTime = dt.datetime.strptime(request.args.get('start_date'), "%Y-%m-%dT%H:%M:%SZ")
        except ValueError:
            return 'Invalid start_date. Should be yyyy-MM-ddThh:mm:ssZ', 400
        if(startTime > dt.datetime.utcnow()):
            return 'start_date cannot be in future', 400
        if('end_date' in request.args):
            try:
                endTime = dt.datetime.strptime(request.args.get('end_date'), "%Y-%m-%dT%H:%M:%SZ")
            except ValueError:
                return "Invalid end_date. Should be yyyy-MM-ddThh:mm:ssZ", 400
            if(startTime > endTime):
                return 'start_date cannot be greater than end_date', 400
        else:
            endTime = startTime + dt.timedelta(minutes=(6*24*60)-10)
    else:
        if('end_date' in request.args):
            try:
                endTime = dt.datetime.strptime(request.args.get('end_date'), "%Y-%m-%dT%H:%M:%SZ")
            except ValueError:
                return 'Invalid end_date. Should be yyyy-MM-ddThh:mm:ssZ', 400
            if(endTime > dt.datetime.utcnow()):
                return 'end_date cannot be in future', 400
        else:
            endTime = dt.datetime.utcnow()
        startTime = endTime - dt.timedelta(minutes=(6*24*60)-10)

    startTime = startTime.isoformat(timespec="seconds") + "Z"
    endTime = endTime.isoformat(timespec="seconds") + "Z"
    print("Fetching weather data with values {} {} {}".format(location, startTime, endTime), flush=True)
    obs = download_stored_query("fmi::observations::weather::multipointcoverage",
                            args=["place=" + location,
                                  "starttime=" + startTime,
                                  "endtime=" + endTime,
                                  "timeseries=True"])

    #return jsonify(obs.data[list(obs.data.keys())[0]])

    location = list(obs.data.keys())[0]
    keys = obs.data[location]['times']
    temperature = obs.data[location]['t2m']['values']
    wind = obs.data[location]['ws_10min']['values']
    windHigh = obs.data[location]['wg_10min']['values']
    windDirection = obs.data[location]['wd_10min']['values']
    humidity = obs.data[location]['rh']['values']
    moistPoint = obs.data[location]['td']['values']
    precipitation  = obs.data[location]['r_1h']['values']
    precIntensity = obs.data[location]['ri_10min']['values']
    snowDept = obs.data[location]['snow_aws']['values']
    pressure = obs.data[location]['p_sea']['values']
    visibility = obs.data[location]['vis']['values']
    clouds = obs.data[location]['n_man']['values']
    weather = obs.data[location]['wawa']['values']

    df = pd.DataFrame({'dt': keys, 't2m': temperature, 'ws_10min': wind, 'wg_10min': windHigh, 'wd_10min': windDirection, 'rh': humidity, 'td': moistPoint, 'r_1h': precipitation, 'ri_10min': precIntensity, 'snow_aws': snowDept, 'p_sea': pressure, 'vis': visibility, 'n_man': clouds, 'wawa': weather })
    return Response(df.to_json(orient='records'), mimetype='application/json')
# Project is based largely on fmiopendata by Panu Lahtinnen https://github.com/pnuu/fmiopendata/
import datetime as dt
import requests
from fmiopendata.wfs import download_stored_query

# Määritellään tunnin mittainen aika
end_time = dt.datetime.utcnow()
start_time = end_time - dt.timedelta(hours=1)

# Muunnetaan ajat isoformat -muotoon
start_time = start_time.isoformat(timespec="seconds") + "Z"
end_time = end_time.isoformat(timespec="seconds") + "Z"

# luodaan fmiopednata-olio joka saa viimeisen tunnin tiedot Ilmatieteenlaitokselta dictionary-tyylisenä tietueena
ennuste = download_stored_query(
    "fmi::forecast::hirlam::surface::obsstations::multipointcoverage",
    args=["starttime=" + start_time, "endtime=" + end_time])

# latest_tstep = viimeisin "aika"
latest_tstep = max(ennuste.data.keys())

valinta1 = input("Haluatko luettelon havaintoasemista? (k/e)")
if valinta1 == "k":
    for key, value in sorted(ennuste.data[latest_tstep].items(
    )):  # tämä tulostaa kaikki havaintoasemat aakkosjärjestyksessä
        print(key)
    print()
    loc = input("Havaintoaseman nimi: ")
if valinta1 == "e":
    loc = input(
        "Havaintoaseman nimi: "