Skip to content

Instantly share code, notes, and snippets.

@alexstorer
Created December 6, 2012 21:35
Show Gist options
  • Save alexstorer/4228694 to your computer and use it in GitHub Desktop.
Save alexstorer/4228694 to your computer and use it in GitHub Desktop.
This script will extract the PS-B and PS-E tables from DOE .sim building files.
import glob
import re
import csv
'''
This script will extract the PS-B and PS-E tables from
DOE .sim building files.
This script was tested on DOE 2.1e .sim files.
To run, please ensure that you have the files in a folder called:
building_files
And this script is located in the same folder as the building_files folder
but not in that folder itself!
It will iterate over all .sim files in the building_files folder
Upon completion, it will add the files:
* psb.csv
* psb_total.csv
* pse.csv
* pse_elec.csv
* pse_fuel.csv
These are comma-separated variable files with the delimiter ','
Please run this using Python 2.7, which can be obtained here:
http://www.python.org/download/releases/2.7/
This script is released without any support! Good luck!
Alex Storer, Holly Samuelson
December 2012
Harvard University
'''
allfiles = glob.glob('building_files/*.sim')
# Write the PS-B Table
print "Extracting PS-B Data..."
fw = open('psb.csv','w')
dw = csv.DictWriter(fw,["filename","month","electricity","gas"])
dw.writeheader()
# Write the PS-B Table, with only totals
fwt = open('psb_total.csv','w')
dwt = csv.DictWriter(fwt,["filename","month","electricity","gas"])
for fname in allfiles:
f = open(fname)
print "PS-B>",fname
d = dict()
numMatches = 0
for line in f:
res = re.search('\(UNITS/MO\) \s* (\d+\.\d) (\s* (\d+\.\d))?',line)
if res is not None:
numMatches = numMatches + 1
d["month"] = numMatches
d["electricity"] = res.group(1)
d["gas"] = res.group(3)
d["filename"] = fname
dw.writerow(d)
res = re.search('\(UNITS/YR\) \s* (\d+\.\d) (\s* (\d+\.\d))?',line)
if res is not None:
numMatches = numMatches + 1
d["month"] = "total"
d["electricity"] = res.group(1)
d["gas"] = res.group(3)
d["filename"] = fname
dw.writerow(d)
dwt.writerow(d)
f.close()
fw.close()
fwt.close()
# A general function to write PS-E tables with Electricity or Fuel
def doPSE(writeElec):
if writeElec:
fw = open('pse_elec.csv','w')
print "Extracting PS-E Tables (Electricity only)>"
else:
fw = open('pse_fuel.csv','w')
print "Extracting PS-E Tables (Fuel only)>"
dw = csv.DictWriter(fw,["filename","month","misc-equipment","area-lights","space-cooling","pumps-misc","space-heating","domhot-water","vent-fans"])
dw.writeheader()
for fname in allfiles:
if writeElec:
print "PS-E (elec)>",fname
else:
print "PS-E (fuel)>",fname
all_me = None
all_al = None
all_sc = None
all_pm = None
all_sh = None
all_dw = None
all_vf = None
f = open(fname)
inPSE = False
useFuel = False
useElec = False
for line in f:
res = re.search('REPORT- PS-E',line)
if res is not None:
inPSE = True
res = re.search('REPORT- PS-F',line)
if res is not None:
inPSE = False
res = re.search('FUEL END-USES IN MBTU',line)
if res is not None:
useFuel = True
useElec = False
res = re.search('ELECTRICAL END-USES IN KWH',line)
if res is not None:
useFuel = False
useElec = True
# do we write out for electricity or fuel?
if writeElec:
doWrite = useElec
else:
doWrite = useFuel
res = re.search('MISC EQUIPMT',line)
if (res is not None) and (inPSE) and (doWrite):
all_me = re.findall('\d+\.',line)
if (res is not None) and (inPSE) and (doWrite):
all_vf = re.findall('\d+\.',line)
res = re.search('AREA LIGHTS',line)
if (res is not None) and (inPSE) and (doWrite):
all_al = re.findall('\d+\.',line)
res = re.search('SPACE COOL',line)
if (res is not None) and (inPSE) and (doWrite):
all_sc = re.findall('\d+\.',line)
res = re.search('PUMPS & MISC',line)
if (res is not None) and (inPSE) and (doWrite):
all_pm = re.findall('\d+\.',line)
res = re.search('SPACE HEAT',line)
if (res is not None) and (inPSE) and (doWrite):
all_sh = re.findall('\d+\.',line)
res = re.search('DOMHOT WATER',line)
if (res is not None) and (inPSE) and (doWrite):
all_dw = re.findall('\d+\.',line)
m = 0
for m in range(0,13):
d = dict()
if all_me is not None:
d["misc-equipment"] = all_me[m]
if all_al is not None:
d["area-lights"] = all_al[m]
if all_sc is not None:
d["space-cooling"] = all_sc[m]
if all_pm is not None:
d["pumps-misc"] = all_pm[m]
if all_sh is not None:
d["space-heating"] = all_sh[m]
if all_dw is not None:
d["domhot-water"] = all_dw[m]
if all_vf is not None:
d["vent-fans"] = all_vf[m]
# Python counts from 0, so write months incremented by 1
m = m+1
if m<=12:
d["month"] = m
else:
d["month"] = "total"
d["filename"] = fname
dw.writerow(d)
f.close()
fw.close()
# now generate PS-E tables for electricity and fuel separately
doPSE(writeElec = True)
doPSE(writeElec = False)
@hollywas
Copy link

hollywas commented Dec 6, 2012

P.S. The pse.csv file only contains monthly MISC EQUIP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment