How to consult EPW file with Python
Weather file is considered as the basic input for energy modelling engineer, it presents a dataset of one year for the different Physical size. Energy model calibration often uses actual measured weather data, hence, in order to have energy model that mimics the actual energy and thermal behavior of the studied building, the weather file used in the simulation have to be modified according to the recorded and the real data.
An EPW file (EnergyPlus weather file) can be divided into two parts: header and weather data. The first eight lines of a standard EPW file present the header which contains data of location, design conditions, typical/extreme periods, ground temperatures, holidays/daylight savings, data periods and other comments. The second part presents the hourly data, the main columns of the data set are detailed here.
Pyepw is python package that provides the flexibility to read visualize or, replace the core weather data. The installation can be done by the simple command on the cmd pip install pyepw. the Python notebook used in the following can be downloaded from this Github link In order to read the weather file first of all, we must create an instance of the class EPW() and apply the read function with the name of the weather file (with the directory location) as input.
from pyepw.epw import EPW
epw = EPW()
epw.read(r"downloadFile.epw") # weather file of Bordeaux in France
The description related to the weather file location, can be accessed by these lines, note that location is another class presented as field of the EWP() class.
print(epw.location.country)
print(epw.location.latitude)
print(epw.location.longitude)
print(epw.location.elevation)
In order to read the hourly data of the dry temperature or the dew temperature, we must iterate over the class weatherdata which are the fields of the created EWP class and access to the field dry_bulb_temperature, dew_point_temperature.
for wd in epw.weatherdata:
print (wd.dry_bulb_temperature, ' ', wd.dew_point_temperature)
To change and save the weather file from recorded data, we must change each field of the epw.weatherdata class. In the following example, let’s assume that we have the recorded data of the dry temperature the relative humidity and the wind speed, these data are presented as lists of random value (just for demonstration)
import random
Temp_data=[round(random.uniform(-5,50), 2) for i in range(8760)] #generate new data for temperature
Humidity_data=[round(random.uniform(5,100), 2) for i in range(8760)] #generate new data for relative humidity
wind_data=[round(random.uniform(0,15), 2) for i in range(8760)] #generate new data for wind speed
i=0
for wd in epw.weatherdata:
wd.dry_bulb_temperature = Temp_data[i]
wd.relative_humidity =Humidity_data[i]
wd.wind_speed =wind_data[i]
i=i+1
epw.save(r"new_file.epw")
Let’s try to construct a data frame by using the weather file, we can use dummies lists to store the data of each iteration over the epw.weatherdata fields and next can we create a Pandas DataFrame from these lists.
def Create_df_weather():
#read_ewp_as_link(Link_weather)
epw = EPW()
epw.read(r"downloadFile.epw")
#print(epw.location.country)
#lat = epw.location.latitude
#print(epw.location.longitude)
#print(epw.location.elevation)
year = []
month = []
day = []
date=[]
hour = []
seconde = []
dry_bulb_temperature = []
dew_point_temperature = []
relative_humidity = []
atmospheric_station_pressure = []
global_horizontal_radiation = []
direct_normal_radiation = []
diffuse_horizontal_radiation = []
wind_direction = []
wind_speed = []
total_sky_cover = []
opaque_sky_cover = []
precipitable_water = []
for wd in epw.weatherdata:
year.append(wd.year)
month.append(wd.month)
day.append(wd.day)
hour.append(wd.hour)
dry_bulb_temperature.append(wd.dry_bulb_temperature)
dew_point_temperature.append(wd.dew_point_temperature)
relative_humidity.append(wd.relative_humidity)
atmospheric_station_pressure.append(wd.atmospheric_station_pressure)
global_horizontal_radiation.append(wd.global_horizontal_radiation)
direct_normal_radiation.append(wd.direct_normal_radiation)
diffuse_horizontal_radiation.append(wd.global_horizontal_radiation)
wind_direction.append(wd.wind_direction)
wind_speed.append(wd.wind_speed)
total_sky_cover.append(wd.total_sky_cover)
opaque_sky_cover.append(wd.opaque_sky_cover)
precipitable_water.append(wd.precipitable_water)
data_weather = pd.DataFrame(
[year, month, day, hour, dry_bulb_temperature, dew_point_temperature, relative_humidity,
atmospheric_station_pressure, global_horizontal_radiation, direct_normal_radiation, diffuse_horizontal_radiation,
wind_direction,
wind_speed, total_sky_cover, opaque_sky_cover, precipitable_water])
data_weather = data_weather.transpose()
data_weather.columns = ['year', 'month', 'day', 'hour', 'dry_bulb_temperature', 'dew_point_temperature',
'relative_humidity',
'atmospheric_station_pressure', 'global_horizontal_radiation', 'direct_normal_radiation',
'diffuse_horizontal_radiation',
'wind_direction', 'wind_speed', 'total_sky_cover', 'opaque_sky_cover', 'precipitable_water']
return data_weather
df=Create_df_weather()
df.head()
Once we have the dataframe, You can plot data directly from your DataFrame using the plot() method which is function as part of the Pandas DataFrame class
ploting the dry and the dew temperature
df[['dew_point_temperature','dry_bulb_temperature']].plot(figsize=(15,5))
Presenting the monthly data of the solar radiation
df_month=df.groupby(['month']).sum()[['global_horizontal_radiation','direct_normal_radiation','diffuse_horizontal_radiation']]/1000
df_month.plot(kind='bar',figsize=(15,5))
Presenting the main statistical value of the dry temperature (min, max, mean) (by month)
df1=df.groupby(['month']).max()[['dry_bulb_temperature']]
df2=df.groupby(['month']).mean()[['dry_bulb_temperature']]
df3=df.groupby(['month']).min()[['dry_bulb_temperature']]
ax = plt.gca()
df1.plot(figsize=(15,5),ax=ax,style='.-')
df2.plot(figsize=(15,5),ax=ax,style='.-')
df3.plot(figsize=(15,5),ax=ax,style='.-')