Added first version of data-fetch/store backend
This commit is contained in:
parent
8925455bc9
commit
b87acf705f
2 changed files with 73 additions and 0 deletions
53
pywatts/fetchdata.py
Normal file
53
pywatts/fetchdata.py
Normal file
|
@ -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
|
||||||
|
)
|
20
pywatts/models.py
Normal file
20
pywatts/models.py
Normal file
|
@ -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()
|
||||||
|
|
Loading…
Reference in a new issue