Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kastnerp committed Feb 16, 2024
1 parent 083e126 commit 4e59fb6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 91 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,5 @@ __pycache__/
*.bak
.vscode/settings.json
run_act_test.bat
.DS_Store
*.py~
16 changes: 8 additions & 8 deletions nrel_psm3_2_epw/assets.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import json
import calendar
import sys
from datetime import datetime

import numpy as np
import pandas as pd
import requests

from . import epw
from datetime import datetime
import calendar


def download_epw(lon, lat, year, location, attributes, interval, utc, your_name, api_key, reason_for_use,
your_affiliation, your_email, mailing_list, leap_year):
currentYear = datetime.now().year
if int(year) == currentYear or int(year) == currentYear-1:
if int(year) == currentYear or int(year) == currentYear - 1:
raise Exception("NREL does not provide data for the current year " + str(
year) + ". It is also unlikely that there is data availability for " + str(int(year) - 1) + ".")

Expand Down Expand Up @@ -42,8 +42,8 @@ def download_epw(lon, lat, year, location, attributes, interval, utc, your_name,

try:
r = requests.request("GET", url, params=
payload, headers=headers, timeout=20)
payload, headers=headers, timeout=20)

print(r.text)
r.raise_for_status()

Expand All @@ -66,7 +66,7 @@ def download_epw(lon, lat, year, location, attributes, interval, utc, your_name,

hours_per_year = 8760

if calendar.isleap(int(year)) and bool(leap_year) is True and all_data.shape[0] == 8784+2:
if calendar.isleap(int(year)) and bool(leap_year) is True and all_data.shape[0] == 8784 + 2:
hours_per_year = 8784

datetimes = pd.date_range('01/01/' + str(year),
Expand Down Expand Up @@ -310,7 +310,7 @@ def download_epw(lon, lat, year, location, attributes, interval, utc, your_name,

d = "_"
file_name = str(location) + d + str(lat) + d + \
str(lon) + d + str(year) + '.epw'
str(lon) + d + str(year) + '.epw'

out.write(file_name)
print("Success: File", file_name, "written")
Expand Down
147 changes: 72 additions & 75 deletions nrel_psm3_2_epw/epw.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# -*- coding: utf-8 -*-
import pandas as pd
import csv

import pandas as pd


class EPW():
"""A class which represents an EnergyPlus weather (epw) file
"""

def __init__(self):
"""
"""
self.headers={}
self.dataframe=pd.DataFrame()


def read(self,fp):
self.headers = {}
self.dataframe = pd.DataFrame()

def read(self, fp):
"""Reads an epw file
Arguments:
- fp (str): the file path of the epw file
"""

self.headers=self._read_headers(fp)
self.dataframe=self._read_data(fp)


def _read_headers(self,fp):

self.headers = self._read_headers(fp)
self.dataframe = self._read_data(fp)

def _read_headers(self, fp):
"""Reads the headers of an epw file
Arguments:
Expand All @@ -35,19 +35,18 @@ def _read_headers(self,fp):
- d (dict): a dictionary containing the header rows
"""
d={}

d = {}
with open(fp, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
if row[0].isdigit():
break
else:
d[row[0]]=row[1:]
d[row[0]] = row[1:]
return d


def _read_data(self,fp):

def _read_data(self, fp):
"""Reads the climate data of an epw file
Arguments:
Expand All @@ -57,52 +56,51 @@ def _read_data(self,fp):
- df (pd.DataFrame): a DataFrame comtaining the climate data
"""
names=['Year',
'Month',
'Day',
'Hour',
'Minute',
'Data Source and Uncertainty Flags',
'Dry Bulb Temperature',
'Dew Point Temperature',
'Relative Humidity',
'Atmospheric Station Pressure',
'Extraterrestrial Horizontal Radiation',
'Extraterrestrial Direct Normal Radiation',
'Horizontal Infrared Radiation Intensity',
'Global Horizontal Radiation',
'Direct Normal Radiation',
'Diffuse Horizontal Radiation',
'Global Horizontal Illuminance',
'Direct Normal Illuminance',
'Diffuse Horizontal Illuminance',
'Zenith Luminance',
'Wind Direction',
'Wind Speed',
'Total Sky Cover',
'Opaque Sky Cover (used if Horizontal IR Intensity missing)',
'Visibility',
'Ceiling Height',
'Present Weather Observation',
'Present Weather Codes',
'Precipitable Water',
'Aerosol Optical Depth',
'Snow Depth',
'Days Since Last Snowfall',
'Albedo',
'Liquid Precipitation Depth',
'Liquid Precipitation Quantity']
first_row=self._first_row_with_climate_data(fp)
df=pd.read_csv(fp,
skiprows=first_row,
header=None,
names=names)

names = ['Year',
'Month',
'Day',
'Hour',
'Minute',
'Data Source and Uncertainty Flags',
'Dry Bulb Temperature',
'Dew Point Temperature',
'Relative Humidity',
'Atmospheric Station Pressure',
'Extraterrestrial Horizontal Radiation',
'Extraterrestrial Direct Normal Radiation',
'Horizontal Infrared Radiation Intensity',
'Global Horizontal Radiation',
'Direct Normal Radiation',
'Diffuse Horizontal Radiation',
'Global Horizontal Illuminance',
'Direct Normal Illuminance',
'Diffuse Horizontal Illuminance',
'Zenith Luminance',
'Wind Direction',
'Wind Speed',
'Total Sky Cover',
'Opaque Sky Cover (used if Horizontal IR Intensity missing)',
'Visibility',
'Ceiling Height',
'Present Weather Observation',
'Present Weather Codes',
'Precipitable Water',
'Aerosol Optical Depth',
'Snow Depth',
'Days Since Last Snowfall',
'Albedo',
'Liquid Precipitation Depth',
'Liquid Precipitation Quantity']

first_row = self._first_row_with_climate_data(fp)
df = pd.read_csv(fp,
skiprows=first_row,
header=None,
names=names)
return df


def _first_row_with_climate_data(self,fp):

def _first_row_with_climate_data(self, fp):
"""Finds the first row with the climate data of an epw file
Arguments:
Expand All @@ -112,27 +110,26 @@ def _first_row_with_climate_data(self,fp):
- i (int): the row number
"""

with open(fp, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for i,row in enumerate(csvreader):
for i, row in enumerate(csvreader):
if row[0].isdigit():
break
return i


def write(self,fp):

def write(self, fp):
"""Writes an epw file
Arguments:
- fp (str): the file path of the new epw file
"""

with open(fp, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
for k,v in self.headers.items():
csvwriter.writerow([k]+v)
for row in self.dataframe.itertuples(index= False):
csvwriter.writerow(i for i in row)
quotechar='"', quoting=csv.QUOTE_MINIMAL)
for k, v in self.headers.items():
csvwriter.writerow([k] + v)
for row in self.dataframe.itertuples(index=False):
csvwriter.writerow(i for i in row)
20 changes: 12 additions & 8 deletions tests/test_download.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from nrel_psm3_2_epw.assets import *
from pathlib import Path
import os
from pathlib import Path

from nrel_psm3_2_epw.assets import *


def test_download_epw():

lat, lon = 40.755840, -73.982684
location = "NYC"
year = '2012'
attributes = 'air_temperature,clearsky_dhi,clearsky_dni,clearsky_ghi,cloud_type,dew_point,dhi,dni,fill_flag,ghi,' \
'relative_humidity,solar_zenith_angle,surface_albedo,surface_pressure,total_precipitable_water,' \
'wind_direction,wind_speed,ghuv-280-400,ghuv-295-385'
'relative_humidity,solar_zenith_angle,surface_albedo,surface_pressure,total_precipitable_water,' \
'wind_direction,wind_speed,ghuv-280-400,ghuv-295-385'

interval = '60'
utc = 'false'
Expand All @@ -19,22 +19,26 @@ def test_download_epw():
cwd = Path.cwd()
api_key_file_path = cwd / Path("api_key")

api_key = None

if api_key_file_path.is_file():
api_key_file = open(api_key_file_path, 'r')
api_key = api_key_file.readline().rstrip()
else:
api_key = os.environ['APIKEY']

print(api_key)

reason_for_use = 'beta+testing'
your_affiliation = 'aaa'
your_email = "Joe@Doe.edu"
mailing_list = 'false'
leap_year = 'true'

download_epw(lat, lon, year, location, attributes, interval, utc, your_name, api_key, reason_for_use, your_affiliation,
your_email, mailing_list, leap_year)
download_epw(lon, lat, int(year), location, attributes, interval, utc, your_name,
api_key, reason_for_use, your_affiliation, your_email, mailing_list, leap_year)

data = epw.EPW()
data.read("NYC_40.75584_-73.982684_2012.epw")

assert(data.dataframe['Year'][0] == 2012)
assert (data.dataframe['Year'][0] == 2012)

0 comments on commit 4e59fb6

Please sign in to comment.