def test_coverage(self): # Get 2018 daily data for Frankfurt Airport data = Daily(['10637'], start = datetime(2018, 1, 1), end = datetime(2018, 12, 31)) coverage = data.normalize().coverage() # Check if coverage is 100% self.assertEqual( coverage, 1, 'Normalized daily data returns coverage of ' + str(coverage) + ', should be 1' )
def test_aggregate(self): # Get 2018 daily data for Frankfurt Airport data = Daily(['10637'], start = datetime(2018, 1, 1), end = datetime(2018, 12, 31)) count = data.normalize().aggregate(freq = '1W').count() # Check if count matches 53 self.assertEqual( count, 53, 'Aggregated daily data returns count of ' + str(count) + ', should be 53' )
def test_normalize(self): # Get 2018 daily data for Frankfurt Airport data = Daily(['10637'], start = datetime(2018, 1, 1), end = datetime(2018, 12, 31)) count = data.normalize().count() # Check if count matches 365 self.assertEqual( count, 365, 'Normalized daily data returns count of ' + str(count) + ', should be 365' )
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
def get_historical_data(self): """ returns: Dataframe of historical average temperature for a given city within the specified time period """ lat, lon = self.get_lat_and_lon() city = Point(lat, lon) data = Daily(city, self.start_date, self.end_date) data = data.fetch() data = data.filter(['tavg']) # select average temperature column data = data.fillna(method='ffill') # fill missing values # get a specific day #day = data.iloc[lambda x: x.index == '2014-06-23'] return data
# General configuration Daily.max_age = 0 STATIONS_PER_CYCLE = 10 # Create Jasper instance jsp = Jasper("task.inventory.daily") # Get stations stations = get_stations(jsp, "SELECT `id` FROM `stations`", STATIONS_PER_CYCLE) if len(stations) > 0: for station in stations: try: # Get daily data from Meteostat data = Daily(station[0], model=False).fetch() # Get start & end dates of time series start = data.index.min().strftime("%Y-%m-%d") end = data.index.max().strftime("%Y-%m-%d") if len(start) == 10 and len(end) == 10: jsp.query(f""" INSERT INTO `inventory`( `station`, `mode`, `start`, `end` ) VALUES ( "{station[0]}",
""" Meteostat JSON API Server The code is licensed under the MIT license. """ from meteostat import Hourly, Daily, Monthly, Normals # Change cache directory to Apache user cache_dir = '/var/www/.meteostat/cache' Hourly.cache_dir = cache_dir Daily.cache_dir = cache_dir Monthly.cache_dir = cache_dir Normals.cache_dir = cache_dir # Max. cache time max_cache_time = 60 * 60 * 24 * 30 # Clear hourly cache Hourly.clear_cache(max_cache_time) # Clear daily cache Daily.clear_cache(max_cache_time) # Clear monthly cache Monthly.clear_cache(max_cache_time) # Clear normals cache Normals.clear_cache(max_cache_time)
from datetime import datetime from meteostat import Point, Daily import seaborn as sns import matplotlib.pyplot as plt import matplotlib as mpl import pandas as pd start = datetime(2021, 1, 1) end = datetime(2021, 12, 31) oslo = Point(59.9139, 10.7522) od = Daily(oslo, start, end) od = od.fetch() print(od.isnull().sum()) df = od.drop(['snow', 'wdir', 'wspd', 'wpgt', 'pres', 'tsun'], axis=1) df = df.dropna() print(df) print(df.isna().sum()) corr = df.corr() print(corr) sns.heatmap(corr, cmap="Blues", annot=True) if not od.empty: exit()
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) plt.show()
""" 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
""" 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 import matplotlib.pyplot as plt from meteostat import Daily # Time period start = datetime(2018, 1, 1) end = datetime(2018, 12, 31) # Get daily data data = Daily('10637', start, end) # Group & aggregate weekly data = data.normalize().aggregate(freq='1W').fetch() # Plot chart data.plot(y=['tavg', 'tmin', 'tmax']) plt.show()
def get_raw_data(station_id, start=start, end=end): data_daily = Daily([station_id], start=start, end=end) return data_daily.fetch()
start = datetime(2021, 1, 1) end = datetime(2021, 12, 31) container = [] retrived = [] retrivedIndex = [] notRetrived = [] notRetrivedIndex = [] for k, val in enumerate(uniqueLocations): stations = Stations() stations = stations.nearby(val[0], val[1]) station = stations.fetch(1) data = Daily(station, start, end) data = data.fetch() if len(data) > 0: data['week'] = data.index.isocalendar().week ndst = data.groupby('week').mean() localdf = ndst[['tavg', 'prcp', 'pres']] disc = localdf.isna().sum().sum() if disc > 40: notRetrived.append(val) notRetrivedIndex.append(k) print('Wait') time.sleep(4) print('Go')
""" How the Atlantic causes mild winters in Europe """ from meteostat import Stations, Daily from datetime import datetime import pandas as pd import matplotlib.pyplot as plt # Get weather stations by Meteostat ID stations = Stations(id=['D1424', '10729', '10803', '10513']).fetch() # Get daily data since 1980 data = Daily(stations, max_threads=5, start=datetime(1980, 1, 1), end=datetime(2019, 12, 31)) # Normalize data data = data.normalize().fetch() # Function for converting degrees to Boolean (North-East or Not-North-East) def direction(value): if (value >= 337 and value <= 360) or value <= 114: return 1 else: return 0 # Convert wind direction
def stations_daily(): """ Return daily station data in JSON format """ # Get query parameters args = utils.get_parameters(parameters) # Check if required parameters are set if args['station'] and len(args['start']) == 10 and len(args['end']) == 10: try: # Convert start & end date strings to datetime start = datetime.strptime(args['start'], '%Y-%m-%d') end = datetime.strptime(f'{args["end"]} 23:59:59', '%Y-%m-%d %H:%M:%S') # Get number of days between start and end date date_diff = (end - start).days # Check date range if date_diff < 0 or date_diff > max_days: # Bad request abort(400) # Caching now_diff = (datetime.now() - end).days if now_diff < 30: cache_time = 60 * 60 * 24 else: cache_time = 60 * 60 * 24 * 3 Daily.max_age = cache_time # Get data data = Daily(args['station'], start, end, model=args['model']) # Check if any data if data.count() > 0: # Normalize data data = data.normalize() # Aggregate if args['freq']: data = data.aggregate(args['freq']) # Unit conversion if args['units'] == 'imperial': data = data.convert(units.imperial) elif args['units'] == 'scientific': data = data.convert(units.scientific) # Fetch DataFrame data = data.fetch() # Convert to integer data['tsun'] = data['tsun'].astype('Int64') # DateTime Index to String data.index = data.index.strftime('%Y-%m-%d') data.index.rename('date', inplace=True) data = data.reset_index().to_json(orient="records") else: # No data data = '[]' # Inject meta data meta = {} meta['generated'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # Generate output string output = f'''{{"meta":{json.dumps(meta)},"data":{data}}}''' # Return return utils.send_response(output, cache_time) except BaseException: # Bad request abort(400) else: # Bad request abort(400)
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 fig, ax = plt.subplots(figsize=(8, 6)) data.unstack('station')['tmax'].plot(legend=True, ax=ax, style='.-', ylabel='Max. Annual Temperature (°C)', title='Max. Temperature Report') plt.legend(names) plt.show()
from datetime import datetime import pandas as pd from meteostat import Point, Daily start = datetime(2018, 1, 1) end = datetime(2021, 3, 8) YT = Point(37.251494, 127.071288) data = Daily(YT, start, end) data = data.fetch() df = pd.DataFrame(data) df.to_csv('weather.csv', index=True) test = 1
# Mehrere Threads für schnellere Downloads Daily.max_threads = 6 # Zeitliche Periode start = datetime(2019, 12, 1) end = datetime(2020, 11, 30) # 50 Zufällige Wetterstationen in Deutschland stations = Stations() stations = stations.region('DE') stations = stations.inventory('daily', (start, end)) stations = stations.fetch(limit=50, sample=True) # Tageswerte laden weather = Daily(stations, start, end) # Daten monatlich aggregieren weather = weather.aggregate('1MS', spatial=True).fetch() # Titel des Diagramms TITLE = 'Interesse an Klimaanlagen & Max. Temperaturen in Deutschland' # Darstellung der Seitenaufrufe ax = pageviews['Klimaanlage'].plot(color='tab:blue', title=TITLE) ax.set_ylabel('Seitenaufrufe', color='tab:blue') # Darstellung der Temperaturspitzen ax2 = ax.twinx() ax2.set_ylabel('Max. Temperatur (°C)', color='tab:red') weather['tmax'].plot(ax=ax2, color='tab:red')
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 data.unstack('station')['tavg'].plot( legend=True, ylabel='Avg. Daily Temperature °C', title='Average Temperature Report for 2019') plt.legend(names) plt.show()
""" 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()
""" Example: Daily point data access 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 Point, Daily # Set time period start = datetime(2018, 1, 1) end = datetime(2018, 12, 31) # Create Point for Vancouver, BC vancouver = Point(49.2497, -123.1193, 70) # Get daily data for 2018 data = Daily(vancouver, start, end) data = data.fetch() # Plot line chart including average, minimum and maximum temperature data.plot(y=['tavg', 'tmin', 'tmax']) plt.show()
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()
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()
""" 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 Daily # Time period start = datetime(2019, 1, 1) end = datetime(2019, 12, 31) # Get daily data data = Daily(['71624', '72295', '68816', '94767'], start, end) data = data.fetch() # Plot chart data.unstack('station')['tavg'].plot( legend=True, ylabel='Avg. Daily Temperature °C', title='Average Temperature Report for 2019') plt.show()
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()
from meteostat import Stations, Daily from datetime import datetime import matplotlib.pyplot as plt data = Daily(['10637'], start = datetime(2018, 1, 1), end = datetime(2018, 12, 31)) data = data.normalize().aggregate(freq = '1W').fetch() data.plot(x = 'time', y = ['tavg', 'tmin', 'tmax'], kind = 'line') plt.show()
from datetime import datetime import json import matplotlib.pyplot as plt from meteostat import Stations, Daily stations = Stations(lat=53.9, lon=27.5667, daily=datetime(2019, 1, 1)) #data = Daily(station, start = datetime(2018, 1, 1), end = datetime(2018, 12, 31)) station = stations.fetch(1) data = Daily(station, start=datetime(2019, 11, 1), end=datetime(2020, 11, 30)) data = data.fetch() print(data) data.plot(kind='scatter', x='snow', y='tmin') plt.show() #data.to_csv('wlastyear.csv', encoding='utf-8', date_format='%Y/%m/%d',na_rep='NULL')