2018-05-15 16:04:14 +02:00
|
|
|
from pypvwatts import PVWatts
|
2018-05-17 14:19:05 +02:00
|
|
|
from pywatts.db import *
|
2018-05-15 16:04:14 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-16 18:57:47 +02:00
|
|
|
def fetch_data(from_lon, to_lon, from_lat, to_lat, step_size=1):
|
2018-05-15 16:04:14 +02:00
|
|
|
my_api_key = ''
|
|
|
|
with open(str(Path.home()) + '/pvwatts_api_key.txt', 'r') as file:
|
2018-05-16 18:57:47 +02:00
|
|
|
my_api_key = file.readline()
|
|
|
|
# Strip newline
|
|
|
|
my_api_key = my_api_key[:-1]
|
2018-05-15 16:04:14 +02:00
|
|
|
|
|
|
|
p = PVWatts(api_key=my_api_key)
|
|
|
|
|
2018-05-16 18:57:47 +02:00
|
|
|
results = []
|
2018-05-15 16:04:14 +02:00
|
|
|
|
2018-05-16 18:57:47 +02:00
|
|
|
for longitude in range(from_lon, to_lon, step_size):
|
2018-05-15 16:04:14 +02:00
|
|
|
for latitude in range(from_lat, to_lat, step_size):
|
2018-05-29 15:28:50 +02:00
|
|
|
try:
|
|
|
|
results.append(p.request(
|
|
|
|
format='JSON', system_capacity=4, module_type=1, array_type=1,
|
|
|
|
azimuth=190, tilt=40, dataset='intl', timeframe='hourly',
|
|
|
|
losses=10, lon=longitude, lat=latitude
|
|
|
|
))
|
|
|
|
except:
|
|
|
|
print("Could not fetch all data")
|
|
|
|
return results
|
2018-05-15 16:04:14 +02:00
|
|
|
|
2018-05-16 18:57:47 +02:00
|
|
|
return results
|
2018-05-15 16:04:14 +02:00
|
|
|
|
|
|
|
|
2018-05-17 14:19:05 +02:00
|
|
|
def store_data(result_list):
|
2018-05-15 16:04:14 +02:00
|
|
|
db.connect()
|
2018-05-17 14:19:05 +02:00
|
|
|
|
|
|
|
if not db.table_exists(WeatherStation):
|
|
|
|
db.create_tables([WeatherStation, Result])
|
2018-05-15 16:04:14 +02:00
|
|
|
|
|
|
|
for result in result_list:
|
2018-05-17 15:28:36 +02:00
|
|
|
if result.errors:
|
|
|
|
continue
|
|
|
|
|
2018-05-17 14:19:05 +02:00
|
|
|
if WeatherStation.select().where(WeatherStation.id == result.station_info['location']).exists():
|
2018-05-17 15:28:36 +02:00
|
|
|
print("Data for station %s already in database. Skipping." % result.station_info['location'])
|
2018-05-15 16:04:14 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
station = WeatherStation.create(
|
2018-05-17 14:19:05 +02:00
|
|
|
longitude=result.station_info['lon'],
|
|
|
|
latitude=result.station_info['lat'],
|
|
|
|
location=result.station_info['location'],
|
|
|
|
elevation=result.station_info['elev'],
|
|
|
|
city=result.station_info['city'],
|
|
|
|
state=result.station_info['state'],
|
|
|
|
id=result.station_info['location'],
|
2018-05-15 16:04:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
Result.create(
|
|
|
|
station=station,
|
2018-05-17 14:19:05 +02:00
|
|
|
dc_output=result.outputs['dc'],
|
|
|
|
temperature=result.outputs['tamb'],
|
|
|
|
wind_speed=result.outputs['wspd']
|
2018-05-15 16:04:14 +02:00
|
|
|
)
|
2018-05-17 15:28:36 +02:00
|
|
|
|
|
|
|
db.close()
|