+"""Summarize EnergyCAP BL30A reports by building and year.
+1. Run BL30A report with the following parameters:
+ Commodity Code One of NATURALGAS:ELECTRIC
+ First Month (01-12) Equals 1
+ Num of Months (1-12) Equals 12
+2. Export as BL30A_2015.csv to the same folder as this file
+3. Repeat steps 1-2 for additional years
+__author__ = "Dylan Barlett"
+__copyright__ = "Arlington County Government"
+__maintainer__ = "Dylan Barlett"
+__email__ = "dbarlett@arlingtonva.us"
+__status__ = "Production"
+def update_nested(d, u):
+ Update a nested dictionary.
+ From http://stackoverflow.com/a/3233356.
+ for k, v in u.iteritems():
+ if isinstance(v, collections.Mapping):
+ r = update_nested(d.get(k, {}), v)
+def site_energy_intensity(square_feet, electric_kwh, natural_gas_therm):
+ """Calculate site energy intensity (in BTU/sq. ft.).
+ if not natural_gas_therm:
+ return ((electric_kwh * 3412) + (natural_gas_therm * 100000)) / square_feet
+def source_energy_intensity(square_feet, electric_kwh, natural_gas_therm):
+ """Calculate source energy intensity (in BTU/sq. ft.).
+ if not natural_gas_therm:
+ return ((electric_kwh * 3412 * 3.34) + (natural_gas_therm * 100000 * 1.047)) / square_feet
+def carbon_footprint(electric_kwh, natural_gas_therm):
+ """Calculate carbon footprint (in metric tons CO2 equivalent).
+ if not natural_gas_therm:
+ return ((electric_kwh * 1.19) + (natural_gas_therm * 11.7)) / 2205
+def format_address(address):
+ "1001 N Quincy St,,Arlington,VA" -> "1001 N Quincy St, Arlington, VA"
+ parts = [i for i in address.split(",") if i]
+ return ", ".join(parts)
+if __name__ == '__main__':
+ locale.setlocale(locale.LC_ALL, "")
+ "Floor Area (square feet)",
+ "Site Energy Intensity (BTU/sq. ft.)",
+ "Source Energy Intensity (BTU/sq. ft.)",
+ "Carbon Footprint (Metric tons CO2 equivalent)",
+ for filename in glob.glob("BL30A_*.csv"):
+ with open(filename, "rb") as fp:
+ reader = csv.reader(fp)
+ place_code = row[6].split(" ", 1)[0]
+ place_name = row[6].split(" ", 1)[1]
+ address = format_address(row[7])
+ floor_area = int(locale.atof(row[8].split(" ", 1)[0]))
+ month = MONTHS[row[14]]
+ year = int(locale.atof(row[15]))
+ quantity = locale.atof(row[16])
+ "Place Name": place_name,
+ "Floor Area": floor_area,
+ for place_code, place_usage in usage.items():
+ for year, year_usage in place_usage["Years"].items():
+ for month, month_usage in year_usage.items():
+ if month == "Floor Area":
+ floor_area = month_usage
+ if "Electric" in month_usage:
+ electric += month_usage["Electric"]
+ electric = month_usage["Electric"]
+ if "Natural Gas" in month_usage:
+ natural_gas += month_usage["Natural Gas"]
+ natural_gas = month_usage["Natural Gas"]
+ site_intensity = site_energy_intensity(floor_area, electric, natural_gas)
+ source_intensity = source_energy_intensity(floor_area, electric, natural_gas)
+ footprint = carbon_footprint(electric, natural_gas)
+ place_usage["Place Name"],
+ place_usage["Address"],
+ # One decimal place for CSV output
+ for i in (site_intensity, source_intensity, footprint):
+ row.append("{:0.1f}".format(i))
+ with open("annual_usage.csv", "wb") as fp:
+ writer = csv.writer(fp)
+ writer.writerows(output)