├── .gitignore ├── LICENSE ├── README.md ├── epw ├── __init__.py └── epw.py ├── setup.py └── tests ├── new_epw_file.epw └── test_epw.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Building Energy Research Group (BERG) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # epw 2 | Lightweight Python package for editing EnergyPlus Weather (epw) files 3 | 4 | ## Overview 5 | 6 | EnergyPlus weather files or .epw files are comma separated variable (csv) files which contain weather and location information. They are used to provide climate data for the energy simulations of the EnergyPlus simulation software. 7 | 8 | This package provides a `epw` class for reading, modifying and saving .epw files. 9 | 10 | ## Installation 11 | 12 | `pip install git+https://github.com/building-energy/epw.git@master` 13 | 14 | Or save the entire package locally, and do a local pip install. 15 | 16 | ## Usage 17 | 18 | Reading a .epw file: 19 | 20 | ```python 21 | >>> from epw import epw 22 | >>> a=epw() 23 | >>> a.read(r'C:\EnergyPlusV8-9-0\WeatherData\USA_CA_San.Francisco.Intl.AP.724940_TMY3.epw') 24 | >>> print(a) 25 | 26 | ``` 27 | 28 | Viewing the header information: 29 | 30 | ```python 31 | >>> d=a.headers # this is a dictionary of the header information 32 | >>> print(d) 33 | {'LOCATION': ['San Francisco Intl Ap', 'CA', 'USA', 'TMY3', '724940', '37.62', '-122.40', '-8.0', '2.0'], 34 | 'DESIGN CONDITIONS': ['1', 'Climate Design Data 2009 ASHRAE Handbook', '', 'Heating', '1', '3.8', '4.9', '-3.7', '2.8', '10.7', '-1.2', '3.4', '11.2', '12.9', '12.1', '11.6', '12.2', '2.2', '150', 'Cooling', '8', '8.5', '28.3', '17.2', '25.7', '16.7', '23.6', '16.2', '18.6', '25.7', '17.8', '23.9', '17', '22.4', '5.9', '310', '16.1', '11.5', '19.9', '15.3', '10.9', '19.2', '14.7', '10.4', '18.7', '52.4', '25.8', '49.8', '23.8', '47.6', '22.4', '2038', 'Extremes', '12.8', '11.5', '10.6', '22.3', '1.8', '34.6', '1.5', '2.3', '0.8', '36.2', '-0.1', '37.5', '-0.9', '38.8', '-1.9', '40.5'], 35 | 'TYPICAL/EXTREME PERIODS': ['6', 'Summer - Week Nearest Max Temperature For Period', 'Extreme', '8/ 1', '8/ 7', 'Summer - Week Nearest Average Temperature For Period', 'Typical', '9/ 5', '9/11', 'Winter - Week Nearest Min Temperature For Period', 'Extreme', '2/ 1', '2/ 7', 'Winter - Week Nearest Average Temperature For Period', 'Typical', '2/15', '2/21', 'Autumn - Week Nearest Average Temperature For Period', 'Typical', '12/ 6', '12/12', 'Spring - Week Nearest Average Temperature For Period', 'Typical', '5/29', '6/ 4'], 36 | 'GROUND TEMPERATURES': ['3', '.5', '', '', '', '10.86', '10.57', '11.08', '11.88', '13.97', '15.58', '16.67', '17.00', '16.44', '15.19', '13.51', '11.96', '2', '', '', '', '11.92', '11.41', '11.51', '11.93', '13.33', '14.60', '15.61', '16.15', '16.03', '15.32', '14.17', '12.95', '4', '', '', '', '12.79', '12.27', '12.15', '12.31', '13.10', '13.96', '14.74', '15.28', '15.41', '15.10', '14.42', '13.60'], 37 | 'HOLIDAYS/DAYLIGHT SAVINGS': ['No', '0', '0', '0'], 38 | 'COMMENTS 1': ['Custom/User Format -- WMO#724940; NREL TMY Data Set (2008); Period of Record 1973-2005 (Generally)'], 39 | 'COMMENTS 2': [' -- Ground temps produced with a standard soil diffusivity of 2.3225760E-03 {m**2/day}'], 40 | 'DATA PERIODS': ['1', '1', 'Data', 'Sunday', ' 1/ 1', '12/31']} 41 | ``` 42 | 43 | Viewing the climate data 44 | ```python 45 | >>> df=a.dataframe # this is pandas dataframe 46 | >>> df1=df[['Year', 'Month', 'Day', 'Hour', 'Minute','Dry Bulb Temperature']] 47 | >>> print(df1.head()) 48 | Year Month Day Hour Minute Dry Bulb Temperature 49 | 0 1999 1 1 1 0 7.2 50 | 1 1999 1 1 2 0 7.2 51 | 2 1999 1 1 3 0 6.7 52 | 3 1999 1 1 4 0 6.1 53 | 4 1999 1 1 5 0 4.4 54 | ``` 55 | 56 | Modifying the header information: 57 | 58 | ```python 59 | >>> a.headers['LOCATION'][0]='New_location' # modifies the location city 60 | >>> print(a.headers['LOCATION']) 61 | ['New_location', 'CA', 'USA', 'TMY3', '724940', '37.62', '-122.40', '-8.0', '2.0'] 62 | 63 | ``` 64 | 65 | Modifying the climate data: 66 | 67 | ```python 68 | >>> a.dataframe['Dry Bulb Temperature']=a.dataframe['Dry Bulb Temperature']+1.0 # increases dry bulb temperature by 1 degree C 69 | >>> print(a.dataframe[['Year', 'Month', 'Day', 'Hour', 'Minute','Dry Bulb Temperature']].head()) 70 | Year Month Day Hour Minute Dry Bulb Temperature 71 | 0 1999 1 1 1 0 8.2 72 | 1 1999 1 1 2 0 8.2 73 | 2 1999 1 1 3 0 7.7 74 | 3 1999 1 1 4 0 7.1 75 | 4 1999 1 1 5 0 5.4 76 | 77 | ``` 78 | 79 | Saving the modified .epw file: 80 | 81 | ```python 82 | >>> a.write('new_epw_file.epw') 83 | ``` 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /epw/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from .epw import epw 4 | -------------------------------------------------------------------------------- /epw/epw.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import pandas as pd 4 | import csv 5 | 6 | class epw(): 7 | """A class which represents an EnergyPlus weather (epw) file 8 | """ 9 | 10 | def __init__(self): 11 | """ 12 | """ 13 | self.headers={} 14 | self.dataframe=pd.DataFrame() 15 | 16 | 17 | def read(self,fp): 18 | """Reads an epw file 19 | 20 | Arguments: 21 | - fp (str): the file path of the epw file 22 | 23 | """ 24 | 25 | self.headers=self._read_headers(fp) 26 | self.dataframe=self._read_data(fp) 27 | 28 | 29 | def _read_headers(self,fp): 30 | """Reads the headers of an epw file 31 | 32 | Arguments: 33 | - fp (str): the file path of the epw file 34 | 35 | Return value: 36 | - d (dict): a dictionary containing the header rows 37 | 38 | """ 39 | 40 | d={} 41 | with open(fp, newline='') as csvfile: 42 | csvreader = csv.reader(csvfile, delimiter=',', quotechar='"') 43 | for row in csvreader: 44 | if row[0].isdigit(): 45 | break 46 | else: 47 | d[row[0]]=row[1:] 48 | return d 49 | 50 | 51 | def _read_data(self,fp): 52 | """Reads the climate data of an epw file 53 | 54 | Arguments: 55 | - fp (str): the file path of the epw file 56 | 57 | Return value: 58 | - df (pd.DataFrame): a DataFrame comtaining the climate data 59 | 60 | """ 61 | 62 | names=['Year', 63 | 'Month', 64 | 'Day', 65 | 'Hour', 66 | 'Minute', 67 | 'Data Source and Uncertainty Flags', 68 | 'Dry Bulb Temperature', 69 | 'Dew Point Temperature', 70 | 'Relative Humidity', 71 | 'Atmospheric Station Pressure', 72 | 'Extraterrestrial Horizontal Radiation', 73 | 'Extraterrestrial Direct Normal Radiation', 74 | 'Horizontal Infrared Radiation Intensity', 75 | 'Global Horizontal Radiation', 76 | 'Direct Normal Radiation', 77 | 'Diffuse Horizontal Radiation', 78 | 'Global Horizontal Illuminance', 79 | 'Direct Normal Illuminance', 80 | 'Diffuse Horizontal Illuminance', 81 | 'Zenith Luminance', 82 | 'Wind Direction', 83 | 'Wind Speed', 84 | 'Total Sky Cover', 85 | 'Opaque Sky Cover (used if Horizontal IR Intensity missing)', 86 | 'Visibility', 87 | 'Ceiling Height', 88 | 'Present Weather Observation', 89 | 'Present Weather Codes', 90 | 'Precipitable Water', 91 | 'Aerosol Optical Depth', 92 | 'Snow Depth', 93 | 'Days Since Last Snowfall', 94 | 'Albedo', 95 | 'Liquid Precipitation Depth', 96 | 'Liquid Precipitation Quantity'] 97 | 98 | first_row=self._first_row_with_climate_data(fp) 99 | df=pd.read_csv(fp, 100 | skiprows=first_row, 101 | header=None, 102 | names=names) 103 | return df 104 | 105 | 106 | def _first_row_with_climate_data(self,fp): 107 | """Finds the first row with the climate data of an epw file 108 | 109 | Arguments: 110 | - fp (str): the file path of the epw file 111 | 112 | Return value: 113 | - i (int): the row number 114 | 115 | """ 116 | 117 | with open(fp, newline='') as csvfile: 118 | csvreader = csv.reader(csvfile, delimiter=',', quotechar='"') 119 | for i,row in enumerate(csvreader): 120 | if row[0].isdigit(): 121 | break 122 | return i 123 | 124 | 125 | def write(self,fp): 126 | """Writes an epw file 127 | 128 | Arguments: 129 | - fp (str): the file path of the new epw file 130 | 131 | """ 132 | 133 | with open(fp, 'w', newline='') as csvfile: 134 | csvwriter = csv.writer(csvfile, delimiter=',', 135 | quotechar='"', quoting=csv.QUOTE_MINIMAL) 136 | for k,v in self.headers.items(): 137 | csvwriter.writerow([k]+v) 138 | for row in self.dataframe.itertuples(index= False): 139 | csvwriter.writerow(i for i in row) -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | from setuptools import setup, find_packages 5 | from codecs import open 6 | from os import path 7 | 8 | setup( 9 | name='epw', 10 | version='0.0.0', 11 | description='Lightweight Python package for editing EnergyPlus Weather (epw) files', # Required 12 | url='https://github.com/building-energy/bredem2012', # Optional 13 | author='Steven Firth', # Optional 14 | author_email='s.k.firth@lboro.ac.uk', # Optional 15 | packages=find_packages() # Required 16 | ) 17 | -------------------------------------------------------------------------------- /tests/test_epw.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import unittest 4 | from epw import epw 5 | from pprint import pprint 6 | import pandas as pd 7 | 8 | class TestEpw(unittest.TestCase): 9 | 10 | # OBJECT CREATION 11 | 12 | def test_epw___init__(self): 13 | a=epw() 14 | self.assertIsInstance(a,epw) 15 | print(a) 16 | 17 | 18 | def test_epw_read(self): 19 | a=epw() 20 | a.read(r'C:\EnergyPlusV8-9-0\WeatherData\USA_CA_San.Francisco.Intl.AP.724940_TMY3.epw') 21 | print(a.headers) 22 | print(a.dataframe.columns) 23 | print(a.dataframe[['Year', 'Month', 'Day', 'Hour', 'Minute','Dry Bulb Temperature']].head()) 24 | 25 | 26 | def test_modify(self): 27 | a=epw() 28 | a.read(r'C:\EnergyPlusV8-9-0\WeatherData\USA_CA_San.Francisco.Intl.AP.724940_TMY3.epw') 29 | 30 | a.headers['LOCATION'][0]='New_location' 31 | print(a.headers['LOCATION']) 32 | 33 | a.dataframe['Dry Bulb Temperature']=a.dataframe['Dry Bulb Temperature']+1.0 34 | print(a.dataframe[['Year', 'Month', 'Day', 'Hour', 'Minute','Dry Bulb Temperature']].head()) 35 | 36 | a.write('new_epw_file.epw') 37 | 38 | if __name__=='__main__': 39 | 40 | o=unittest.main(TestEpw()) 41 | --------------------------------------------------------------------------------