Last active
July 25, 2018 16:42
-
-
Save amithkk/3340dfc47e9143bf4f87d4a948a7540e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import itertools | |
import os | |
from openpyxl import Workbook, load_workbook | |
from openpyxl.utils.exceptions import IllegalCharacterError | |
OUTFILE = 'Weather_Data.xlsx' | |
if os.path.isfile(OUTFILE): | |
print("Exists. Append instead of create") | |
out_workbook = load_workbook(OUTFILE) | |
else: | |
out_workbook = Workbook() | |
out_workbook.remove(out_workbook.active) | |
fn_matrix = { | |
'NSRDB' : 'NSRDB', | |
'SGIS' : 'SolarGIS', | |
'Metnom' : 'Meteonorm', | |
'PVGIS' : 'PVGIS' | |
} | |
def floatify(data): | |
try: | |
return float(data) | |
except ValueError: | |
return data | |
#Meteonorm | |
MT_NAME = "Metonorm" | |
MT_PATH = os.path.join(os.getcwd(), fn_matrix['Metnom']) | |
if os.path.isdir(MT_PATH): | |
print(MT_NAME,"Data Found.") | |
mtfile = os.path.join(MT_PATH, os.listdir(MT_PATH)[0]) | |
with open(mtfile, 'r') as f: | |
if MT_NAME in out_workbook.sheetnames: | |
out_workbook.remove(out_workbook[MT_NAME]) | |
mtws = out_workbook.create_sheet(MT_NAME) | |
lines = iter(f.read().split('\n')[14:]) | |
for line in lines: | |
ls = line.split('\t') | |
if ls[0].startswith("--"): | |
next(lines) | |
break | |
if ls is not []: | |
try: | |
mtws.append([floatify(x) for x in ls]) | |
except IllegalCharacterError: | |
print("Illegal Character") | |
mtws.append([]) | |
for line in lines: | |
ls = line.split('\t') | |
if ls[0].startswith("=="): | |
break | |
if ls is not []: | |
try: | |
mtws.append([floatify(x) for x in ls]) | |
except IllegalCharacterError: | |
print("Illegal Character") | |
# NSRDB | |
NS_NAME = 'NSRDB' | |
NS_PATH = os.path.join(os.getcwd(), fn_matrix['NSRDB']) | |
if os.path.isdir(NS_PATH): | |
if NS_NAME in out_workbook.sheetnames: | |
out_workbook.remove(out_workbook[NS_NAME]) | |
print(NS_NAME,"Data Found.") | |
nsfile = os.path.join(NS_PATH, os.listdir(NS_PATH)[0]) | |
with open(nsfile, 'r') as f: | |
nsws = out_workbook.create_sheet(NS_NAME) | |
for row in csv.reader(f): | |
try: | |
nsws.append([floatify(x) for x in row]) | |
except IllegalCharacterError: | |
print("Illegal Character") | |
#SolarGIS | |
SG_NAME = 'SolarGIS' | |
SG_PATH = os.path.join(os.getcwd(), fn_matrix['SGIS']) | |
if os.path.isdir(SG_PATH): | |
print(SG_NAME,"Data Found.") | |
if SG_NAME+"_M" in out_workbook.sheetnames: | |
out_workbook.remove(out_workbook[SG_NAME+"_M"]) | |
if SG_NAME+"_D" in out_workbook.sheetnames: | |
out_workbook.remove(out_workbook[SG_NAME+"_D"]) | |
sgfile = os.path.join(SG_PATH, os.listdir(SG_PATH)[0]) | |
with open(sgfile, 'r') as f: | |
rows = itertools.islice(csv.reader(f), 48, None) | |
s = next(rows)[0] | |
if s == '#Data:': | |
sgws = out_workbook.create_sheet("SolarGIS_M") | |
else: | |
sgws = out_workbook.create_sheet("SolarGIS_D") | |
next(rows) | |
next(rows) | |
for row in rows: | |
try: | |
sgws.append([floatify(x) for x in row[0].split(';')]) | |
except IllegalCharacterError: | |
print("Illegal Character") | |
#PVGIS | |
PV_NAME = 'PVGIS' | |
PV_PATH = os.path.join(os.getcwd(), fn_matrix['PVGIS']) | |
if os.path.isdir(PV_PATH): | |
print("PVGIS Data Found.") | |
if PV_NAME in out_workbook.sheetnames: | |
print("Exists") | |
out_workbook.remove(out_workbook[PV_NAME]) | |
pvfile = os.path.join(PV_PATH, os.listdir(PV_PATH)[0]) | |
wb = load_workbook(pvfile) | |
pvwb = out_workbook.create_sheet("PVGIS") | |
ws = wb[next(filter(lambda x: x.startswith("Monthly"), wb.sheetnames))] | |
rows = itertools.islice(ws.rows, 5, None) | |
for row in rows: | |
row = [x.internal_value for x in row] | |
if type(row[0]) is str and row[0].startswith('Hh'): | |
break | |
pvwb.append(row) | |
#Save | |
try: | |
print('Saving') | |
out_workbook.save(OUTFILE) | |
except IndexError: | |
print("No data found. Quitting") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment