From b87acf705f519309e40df8f84b96320f9a95011d Mon Sep 17 00:00:00 2001 From: reedts Date: Tue, 15 May 2018 16:04:14 +0200 Subject: [PATCH] Added first version of data-fetch/store backend --- pywatts/fetchdata.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ pywatts/models.py | 20 +++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 pywatts/fetchdata.py create mode 100644 pywatts/models.py diff --git a/pywatts/fetchdata.py b/pywatts/fetchdata.py new file mode 100644 index 0000000..a04e8b9 --- /dev/null +++ b/pywatts/fetchdata.py @@ -0,0 +1,53 @@ +from pypvwatts import PVWatts +from .models import * +from pathlib import Path + + +db = SqliteDatabase('pywatts.db') + + +def fetch_data(from_long, to_long, from_lat, to_lat, step_size=1): + my_api_key = '' + with open(str(Path.home()) + '/pvwatts_api_key.txt', 'r') as file: + api_key = file.readline() + + p = PVWatts(api_key=my_api_key) + + result_list = [] + + for longitude in range(from_long, to_long, step_size): + for latitude in range(from_lat, to_lat, step_size): + result_list.append(p.request( + system_capacity=4, module_type=1, array_type=1, format='json', + azimuth=190, tilt=30, dataset='intl', timeframe='hourly', + losses=13, lon=longitude, lat=latitude + )) + + return result_list + + +def store_data(db_name, result_list): + db.connect() + db.create_tables([WeatherStation, Result]) + + for result in result_list: + if WeatherStation.select().where(WeatherStation.id == result.location).exists(): + print("Data for station %s already in database. Skipping." % result.station_info.location) + continue + + station = WeatherStation.create( + longitude=result.station_info.lon, + latitude=result.station_info.lat, + location=result.station_info.location, + elevation=result.station_info.elevation, + city=result.station_info.city, + state=result.station_info.state, + id=result.station_info.location, + ) + + Result.create( + station=station, + dc_output=result.outputs.dc, + temperature=result.outputs.tamb, + wind_speed=result.outputs.wspd + ) diff --git a/pywatts/models.py b/pywatts/models.py new file mode 100644 index 0000000..141a2a8 --- /dev/null +++ b/pywatts/models.py @@ -0,0 +1,20 @@ +from peewee import * +from playhouse import sqlite_ext + + +class WeatherStation(Model): + longitude = IntegerField() + latitude = IntegerField() + location = CharField() + elevation = IntegerField() + city = CharField() + state = CharField() + id = CharField(unique=True) + + +class Result(Model): + station = ForeignKeyField(WeatherStation) + dc_output = sqlite_ext.JSONField() + temperature = sqlite_ext.JSONField() + wind_speed = sqlite_ext.JSONField() +