Beispiel #1
0
def fetch(
        start=datetime(2020, 1, 1),
        end=datetime.now(),
        lat=33.5020,  # Closest to UAB
        lon=-86.8064):
    start += timedelta(hours=6)  # UTC offset
    end += timedelta(hours=6)
    end.replace(microsecond=0, second=0)
    stations = Stations(lat=lat, lon=lon)
    station = stations.fetch(1)
    data = Hourly(station, start, end)
    data = data.normalize()
    data = data.interpolate()
    df = data.fetch()
    out = {}
    last_row = None
    for row in df.itertuples():
        if last_row:
            out.update(interpolate_minutes(last_row, row))
        last_row = row
    current_time = last_row.time - timedelta(hours=6)
    while current_time <= end - timedelta(hours=6):
        out[create_datetime_ID(current_time)] = {
            "time": current_time,
            "temp": last_row.temp
        }
        current_time += timedelta(minutes=1)
    return out
Beispiel #2
0
def _weather_data_extraction(lat, lon):
    start = datetime(2015, 1, 1)
    end = datetime(2021, 1, 16)

    stations = Stations()
    stations = stations.nearby(lat, lon)
    stations = stations.inventory('daily', (start, end))
    station = stations.fetch(1)

    data = Daily(station, start, end)
    data = data.fetch()

    return data
Beispiel #3
0
        def meteostat(lat_degr,long_degr,alt_avrg,start_time,end_time):
            #Retrieve nearest weather station
            stations = Stations()
            stations = stations.nearby(lat_degr, long_degr)
            station = stations.fetch(1)

            #Use Point to agregate nearby weather stations data for better accuracy
            weather_point = Point(lat_degr, long_degr,alt_avrg)
            weather_data = Hourly(weather_point, start_time - timedelta(0,7200), end_time + timedelta(0,7200))
            weather_data = weather_data.fetch()
            #Use weather data from nearest station if Point returns an empty dataset
            if weather_data.empty:
                weather_data = Hourly(station, start_time - timedelta(0,7200), end_time + timedelta(0,7200))
                weather_data = weather_data.fetch()
            return weather_data
    def test_area(self):

        # Select weather stations in southern hemisphere
        station = Stations(bounds = [0, -180, -90, 180]).fetch(1).to_dict('records')[0]

        # Check if -90 <= latitude <= 0
        self.assertTrue(
            -90 <= station['latitude'] <= 0,
            'Weather station is not in latitude range'
        )
    def test_nearby(self):

        # Selecting closest weather station to Frankfurt Airport
        station = Stations(lat = 50.05, lon = 8.6).fetch(1).to_dict('records')[0]

        # Check if country code matches Germany
        self.assertEqual(
            station['country'],
            'DE',
            'Closest weather stations returns country code ' + station['country'] + ', should be DE'
        )
    def test_identifier(self):

        # Select weather station 'Toronto Pearson Airport'
        station = Stations(id = '71624').fetch(1).to_dict('records')[0]

        # Check if ICAO ID matches CYYZ
        self.assertEqual(
            station['icao'],
            'CYYZ',
            'Weather station returns ICAO ID ' + station['icao'] + ', should be CYYZ'
        )
    def test_area(self):
        """
        Test: Stations by geographical area
        """

        # Select weather stations in southern hemisphere
        station = Stations().bounds((0, -180),
                                    (-90, 180)).fetch(1).to_dict('records')[0]

        # Check if -90 <= latitude <= 0
        self.assertTrue(-90 <= station['latitude'] <= 0,
                        'Weather station is not in latitude range')
    def test_regional(self):
        """
        Test: Stations by country/region code
        """

        # Select a weather station in Ontario, Canada
        station = Stations().region('CA', 'ON').fetch(1).to_dict('records')[0]

        # Check if country code matches Canada
        self.assertEqual(
            station['country'], 'CA', 'Weather station returns country code ' +
            station['country'] + ', should be CA')

        # Check if region code matches Ontario
        self.assertEqual(
            station['region'], 'ON', 'Weather station returns province code ' +
            station['region'] + ', should be ON')
Beispiel #9
0
"""
The hottest places in Germany
"""

from meteostat import Stations, Daily
from datetime import datetime
import matplotlib.pyplot as plt

# Get weather stations by Meteostat ID
stations = Stations(id=['D1424', '10729', '10803', '10513']).fetch()

# Get names of weather stations
names = stations['name'].to_list()

# Get daily data since 2000
data = Daily(stations, start=datetime(2000, 1, 1), end=datetime(2019, 12, 31))

# Aggregate annually
data = data.aggregate(freq='1Y').fetch()

# Plot data
fig, ax = plt.subplots(figsize=(8, 6))
data.unstack('station')['tmax'].plot(kind='line',
                                     legend=True,
                                     ax=ax,
                                     style='.-',
                                     ylabel='Max. Annual Temperature (°C)',
                                     title='Max. Temperature Report')
plt.legend(names)

# Show plot
Beispiel #10
0
"""
Spatial sampling with Meteostat
"""

from meteostat import Stations, Daily
from datetime import datetime
import matplotlib.pyplot as plt

# Get 20 random weather stations in Germany
stations = Stations(country='DE', daily=datetime(2005, 1,
                                                 1)).fetch(limit=20,
                                                           sample=True)

# Get daily data
data = Daily(stations,
             max_threads=5,
             start=datetime(1980, 1, 1),
             end=datetime(2019, 12, 31))

# Normalize data and aggregate
data = data.normalize().aggregate(freq='5Y', spatial=True).fetch()

# Plot chart
data.plot(y=['tavg'],
          kind='line',
          title='Sampled DE Annual Mean Temperature from 1980 to 2019')
plt.show()
Beispiel #11
0
from meteostat import Stations

# Get number of stations in northern hemisphere
stations = Stations(bounds=[90, -180, 0, 180])

print('Stations in northern hemisphere:')
print(stations.count())

# Get number of stations in southern hemisphere
stations = Stations(bounds=[0, -180, -90, 180])

print('Stations in southern hemisphere:')
print(stations.count())
Beispiel #12
0
"""
Example: Aggregation

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from datetime import datetime
from meteostat import Stations, Hourly

# Hourly
stations = Stations()
stations = stations.id('wmo', '10637')
station = stations.fetch()

# Time period
start = datetime(2018, 1, 1)
end = datetime(2018, 1, 1, 23, 59)

# Get hourly data & aggregate
data = Hourly(station, start, end)
data = data.aggregate('1D')
data = data.fetch()

# Print
print(data)
Beispiel #13
0
"""
Example: Comparing multiple weather stations

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Daily

# Get weather stations by WMO ID
stations = Stations()
stations = stations.id('wmo', ('71624', '72295', '68816', '94767'))
stations = stations.fetch()

# Get names of weather stations
names = stations['name'].to_list()

# Time period
start = datetime(2019, 1, 1)
end = datetime(2019, 12, 31)

# Get daily data
data = Daily(stations, start, end)
data = data.fetch()

# Plot chart
Beispiel #14
0
The code is licensed under the MIT license.
"""

from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Daily

# Configuration
Daily.max_threads = 5

# Time period
start = datetime(1980, 1, 1)
end = datetime(2019, 12, 31)

# Get random weather stations in the US
stations = Stations()
stations = stations.region('US')
stations = stations.inventory('daily', (start, end))
stations = stations.fetch(limit=20, sample=True)

# Get daily data
data = Daily(stations, start, end)

# Normalize & aggregate
data = data.normalize().aggregate('1Y', spatial=True).fetch()

# Chart title
TITLE = 'Average US Annual Temperature from 1980 to 2019'

# Plot chart
data.plot(y=['tavg'], title=TITLE)
Beispiel #15
0
Example: Simple chart

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Daily

# Time period
start = datetime(2018, 1, 1)
end = datetime(2018, 12, 31)

# Get a weather station
stations = Stations()
stations = stations.nearby(49.2497, -123.1193)
stations = stations.inventory('daily', (start, end))
station = stations.fetch(1)

# Get daily data
data = Daily(station, start, end)
data = data.fetch()

# Plot chart
data.plot(y=['tavg', 'tmin', 'tmax'])
plt.show()
Beispiel #16
0
def get_station(lat, lon, cnt=10):
    return Stations(lat=lat, lon=lon).fetch(
        cnt)  # get closest station to given coordinates
Beispiel #17
0
from meteostat import Stations, Hourly
from datetime import datetime
import matplotlib.pyplot as plt

# Hourly
stations = Stations(lat = 50, lon = 8)
station = stations.fetch(1)

data = Hourly(station, start = datetime(2010, 1, 1), end = datetime(2020, 1, 1, 23, 59))
data = data.fetch()

data.plot(x = 'time', y = ['temp'], kind = 'line')
plt.show()
Beispiel #18
0
"""
Example: Comparing aggregated data

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Daily

# Get weather stations by Meteostat ID
stations = Stations()
stations = stations.fetch()
stations = stations[stations.index.isin(('D1424', '10729', '10803', '10513'))]

# Get names of weather stations
names = stations['name'].to_list()

# Time period
start = datetime(2000, 1, 1)
end = datetime(2019, 12, 31)

# Get daily data
data = Daily(stations, start, end)

# Aggregate annually
data = data.aggregate(freq='1Y').fetch()
Beispiel #19
0
"""
Example: Get weather stations by identifier

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from meteostat import Stations

# Get weather station with ICAO ID EDDF
stations = Stations()
station = stations.id('icao', 'EDDF').fetch()

# Print station
print(station)
Beispiel #20
0
from meteostat import Stations
from datetime import datetime

# Closest weather station for position
stations = Stations(lat = 50, lon = 8, hourly = datetime(1940, 1, 1))
station = stations.fetch(1).to_dict('records')[0]

print('Closest weather station at coordinates 50, 8:')
print(station["name"])
from meteostat import Stations

# Get weather station with Meteostat ID 10637
stations = Stations(id='10637', icao='EDDF')
station = stations.fetch(1).to_dict('records')[0]

# Print name
print(station["name"])
Beispiel #22
0
def get_nearest_station(latitude: float, longitude: float) -> str:
    # Get closest weather station to the match location
    stations = Stations().nearby(latitude, longitude)
    station = stations.fetch(1)
    LOGGER.info("Using nearest station: %s" % station)
    return station.index.array[0]
Beispiel #23
0
from meteostat import Stations, Daily
from datetime import datetime
import matplotlib.pyplot as plt

# Hourly
stations = Stations(lat=49.2497, lon=-123.1193)
station = stations.fetch(1)

data = Daily(station, start=datetime(2018, 1, 1), end=datetime(2018, 12, 31))
data = data.fetch()

data.plot(x='time', y=['tavg', 'tmin', 'tmax'], kind='line')
plt.show()
Beispiel #24
0
"""
Example: Get weather stations by geographical area

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from meteostat import Stations

# Get all stations
stations = Stations()

# Get number of stations in northern hemisphere
northern = stations.bounds((90, -180), (0, 180))
print('Stations in northern hemisphere:', northern.count())

# Get number of stations in southern hemisphere
southern = stations.bounds((0, -180), (-90, 180))
print('Stations in southern hemisphere:', southern.count())
from meteostat import Stations, Hourly
from datetime import datetime

# Hourly
stations = Stations(wmo='10637')
station = stations.fetch(1)

data = Hourly(station,
              start=datetime(2020, 1, 1),
              end=datetime(2020, 1, 1, 23, 59)).aggregate(freq='1D')
print(data.fetch())
from meteostat import Stations, Daily
from datetime import datetime
import matplotlib.pyplot as plt

stations = Stations(country='US', daily=datetime(2005, 1, 1)).sample(5).fetch()

data = Daily(stations,
             max_threads=5,
             start=datetime(1980, 1, 1),
             end=datetime(2019, 12, 31))
data = data.normalize().aggregate(freq='1Y', spatial=True).fetch()

data.plot(y=['tavg'],
          kind='line',
          title='Average US Annual Temperature from 1980 to 2019')
plt.show()
"""
Example: Comparing aggregated data

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Daily

# Get weather stations by WMO ID
stations = Stations()
stations = stations.id('meteostat', ('D1424', '10729', '10803', '10513'))
stations = stations.fetch()

# Get names of weather stations
names = stations['name'].to_list()

# Time period
start = datetime(2000, 1, 1)
end = datetime(2019, 12, 31)

# Get daily data
data = Daily(stations, start, end)
data = data.aggregate(freq='1Y').fetch()

# Plot chart
Beispiel #28
0
from meteostat import Stations, Daily
from datetime import datetime
import matplotlib.pyplot as plt

# Get weather stations by WMO ID
stations = Stations(wmo = ['71624', '72295', '68816', '94767']).fetch()

# Get names of weather stations
names = stations['name'].to_list()

# Get daily data for 2019
data = Daily(stations, start = datetime(2019, 1, 1), end = datetime(2019, 12, 31))
data = data.fetch()

# Plot data
ax = data.set_index('time').groupby(['station'])['tavg'].plot(kind = 'line', legend = True, ylabel = 'Avg. Daily Temperature °C', title = 'Average Temperature Report for 2019')
plt.legend(names)

# Show plot
plt.show()
Beispiel #29
0
"""
Example: Closest weather station by coordinates

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from meteostat import Stations

# Get weather station
stations = Stations()
stations = stations.nearby(50, 8)
stations = stations.inventory('hourly', True)
station = stations.fetch(1).to_dict('records')[0]

# Print name
print('Closest weather station at coordinates 50, 8:', station["name"])
Beispiel #30
0
"""
Example: Simple chart

Meteorological data provided by Meteostat (https://dev.meteostat.net)
under the terms of the Creative Commons Attribution-NonCommercial
4.0 International Public License.

The code is licensed under the MIT license.
"""

from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Hourly

# Get a weather station
stations = Stations()
stations = stations.nearby(50, 8)
station = stations.fetch(1)

# Time period
start = datetime(2017, 1, 1)
end = datetime(2017, 1, 1, 23, 59)

# Get hourly data
data = Hourly(station, start, end)
data = data.fetch()

# Plot chart
data.plot(y='temp')
plt.show()