Download Weather file with Python

Weather file is considered as the basic input for energy modelling engineer, it represents 1 year of hourly (8760) weather data values, this data set are commonly used by energy engineers for modelling renewable energy systems and assess the building heating and cooling needs, as it provides a typical description of the main physical properties as the temperature, the humidity, solar radiation, wind speed… for the concerned region.

The Energy Plus weather file is commonly used by engineer as it is provided by Energy plus, easy to download and suitable for almost energy simulation softwares.

In this article we will try to build a python application that allow as to download the energy plus weather file without accessing to energy Plus website. the python code can be downloaded from this link

We can notice that in EnergyPlus website there is cartography presenting the locations where the EnergyPlus weather files are available. The list of these locations can be downloaded as json format from this link NREL Github. If we open this file with Notepad, we notice that each point/location is presented by these lines which is repeated every 15 lines.

image 11

Moreover, in the file name, we notice that it is divided in 2 parts which are the country code with three letters and the city name, for example “FRA_Paris.Orly.071490_IWEC” the FRA is the country code for France and Paris.Orly is the name of the city (location). First of all, we can easily read this file line by line with python

import os
import pandas as pd
import re
filename = "master.geojson.txt"
with open(filename) as f:
    line = f.readlines()

We began with the first city, we can access to the country code and the city name by some split functions => the output is IND_Kota

line[9].split(":")[1].split("\n")[0][2:-3].split(".")[0]
>> IND_Kota

image 22

With the same methodology we can extract the EWP URL file and the (X,Y) coordinates.

Ewp_URL='https://'+line[9].split(":")[2][2:-29]
X_coor=float(line[15].split(":")[1].split("\n")[0][1:][1:-1].split(",")[0])
Y_coor float(line[15].split(":")[1].split("\n")[0][1:][1:-1].split(",")[1])

With an iterative loop through all the line of the file we can append for each location the information concerning: the country ID, the city name, the EPW download URl, X_coordinate and the Y_coordinate, and store this information as data frame

import os
import pandas as pd
import re

filename = "master.geojson.txt"
with open(filename) as f:
    line = f.readlines()

# Show the file contents line by line.
Country=[]
city=[]
ewp=[]
X=[]
Y=[]
i=8 # first line for the city and country name
j=9 # first line for the EPW file
k=15 # first line for the X_coord and Y_coord

while i<len(line):
    a=line[i].split(":")[1].split("\n")[0][2:-3].split(".")[0].split("_")

    if len(a)>2:
        a[1]=a[1]+" "+a[2]
        a.remove(a[2])

    if len(a)>1:
        Country.append(a[0])
        city.append(a[1])
        ewp.append('https://'+line[j].split(":")[2][2:-29])
        X.append(float(line[k].split(":")[1].split("\n")[0][1:][1:-1].split(",")[0]))
        Y.append(float(line[k].split(":")[1].split("\n")[0][1:][1:-1].split(",")[1]))
    i=i+15 #next location
    j=j+15 #next location
    k=k+15 #next location

data=pd.DataFrame({'Country':Country,'city':city, 'EWP':ewp,'coorX':X,'coordY':Y})
data.to_csv('EWP_data_base.csv', sep=';')

the exported data set is presented as bellow.

image 33

In order to add the full country name and the continent name, as first step we can download as csv file from this link a database that consists of list of countries by continent, and then, we merge this dataset with the data set extracted form the EnergyPlus data based on the country_id.

image 44

country_data=pd.read_csv('country-and-continent-codes-list-csv_csv.csv',sep=',')[['Continent_Name','Country_Name','Three_Letter_Country_Code']]

country_data.columns=['Continent_Name','Country_Name','Country_ID']

result = pd.merge(data_E_PLUS, country_data, on=['Country_ID'])
result.to_csv('EWP_data_base.csv', sep=';')

image 5

Once we have this dataset, we can use dash which is python framework that allows to built web app.

In our case we use the widget map in order to present the different location referring to the X and Y coordinate, in addition, a conditioned selectors to choose the location based on the continent, the country and the chosen city. The full code can be downloaded from this link on Github and the output is similar to bellow. image 6

Avatar
Habib MEZGHANI
Energy efficiency engineer

Energy efficiency engineer, with advanced Python programming skills