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
def test_coverage(self): # Get data for some day at Frankfurt Airport data = Hourly(['10637'], start = datetime(2018, 1, 1), end = datetime(2018, 1, 1, 23, 59)) coverage = data.normalize().coverage() # Check if coverage is 100% self.assertEqual( coverage, 1, 'Normalized hourly data returns coverage of ' + str(coverage) + ', should be 1' )
def test_aggregate(self): # Get data for some days at Frankfurt Airport data = Hourly(['10637'], start = datetime(2018, 1, 1), end = datetime(2018, 1, 3, 23, 59)) count = data.normalize().aggregate(freq = '1D').count() # Check if count matches 3 self.assertEqual( count, 3, 'Aggregated hourly data returns count of ' + str(count) + ', should be 3' )
def test_normalize(self): # Get data for some day at Frankfurt Airport data = Hourly(['10637'], start = datetime(2018, 1, 1), end = datetime(2018, 1, 1, 23, 59)) count = data.normalize().count() # Check if count matches 24 self.assertEqual( count, 24, 'Normalized hourly data returns count of ' + str(count) + ', should be 24' )
""" Example: Interpolation 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 Hourly # Time period start = datetime(2018, 8, 1) end = datetime(2018, 8, 4, 23, 59) # Get hourly data data = Hourly('10730', start, end) data = data.normalize() data = data.interpolate(6) data = data.fetch() # Plot chart data.plot(y='temp') plt.show()
def point_hourly(): """ Return hourly point data in JSON format """ # Get query parameters args = utils.get_parameters(parameters) # Check if required parameters are set if args['lat'] and args['lon'] 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 < 3: cache_time = 60 * 60 elif now_diff < 30: cache_time = 60 * 60 * 24 else: cache_time = 60 * 60 * 24 * 3 Hourly.max_age = cache_time # Create a point location = Point(args['lat'], args['lon'], args['alt']) # Get data data = Hourly(location, start, end, timezone=args['tz'], 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') data['coco'] = data['coco'].astype('Int64') # DateTime Index to String data.index = data.index.strftime('%Y-%m-%d %H:%M:%S') 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') meta['stations'] = location.stations.to_list() # 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)