Ejemplo n.º 1
0
# 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')

# Ausgabe des Diagramms
plt.show()
Ejemplo n.º 2
0
"""

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
plt.show()
Ejemplo n.º 3
0
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)