From 4e59fb602376ed9fbaefc861fd91bc41dff9516b Mon Sep 17 00:00:00 2001 From: Patrick Kastner Date: Fri, 16 Feb 2024 14:41:54 -0500 Subject: [PATCH] Fixes --- .gitignore | 2 + nrel_psm3_2_epw/assets.py | 16 ++--- nrel_psm3_2_epw/epw.py | 147 +++++++++++++++++++------------------- tests/test_download.py | 20 +++--- 4 files changed, 94 insertions(+), 91 deletions(-) diff --git a/.gitignore b/.gitignore index 94ba891..c8f3504 100644 --- a/.gitignore +++ b/.gitignore @@ -132,3 +132,5 @@ __pycache__/ *.bak .vscode/settings.json run_act_test.bat +.DS_Store +*.py~ diff --git a/nrel_psm3_2_epw/assets.py b/nrel_psm3_2_epw/assets.py index 6383d8c..e8fa1a2 100644 --- a/nrel_psm3_2_epw/assets.py +++ b/nrel_psm3_2_epw/assets.py @@ -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) + ".") @@ -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() @@ -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), @@ -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") diff --git a/nrel_psm3_2_epw/epw.py b/nrel_psm3_2_epw/epw.py index bc5d9f7..9520493 100644 --- a/nrel_psm3_2_epw/epw.py +++ b/nrel_psm3_2_epw/epw.py @@ -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: @@ -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: @@ -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: @@ -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) \ No newline at end of file + 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) diff --git a/tests/test_download.py b/tests/test_download.py index d46516a..4eabe12 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -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' @@ -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)