11from pathlib import Path
22
33import pandas as pd
4- from loguru import logger
54from lxml import objectify
6- from lxml .builder import ElementMaker
5+ from lxml .objectify import ElementMaker
76
87from openstudio_hpxml_calibration .hpxml import HpxmlDoc
98
@@ -17,7 +16,8 @@ def set_consumption_on_hpxml(hpxml_object: HpxmlDoc, csv_bills_filepath: Path) -
1716 E = ElementMaker (namespace = NS , nsmap = NSMAP )
1817
1918 bills = pd .read_csv (csv_bills_filepath )
20- # Convert to datetimes, and include the final day of the bill period
19+
20+ # Datetime handling
2121 bills ["StartDateTime" ] = pd .to_datetime (bills ["StartDateTime" ], format = "mixed" )
2222 bills ["EndDateTime" ] = pd .to_datetime (bills ["EndDateTime" ], format = "mixed" ) + pd .Timedelta (
2323 days = 1
@@ -33,35 +33,37 @@ def set_consumption_on_hpxml(hpxml_object: HpxmlDoc, csv_bills_filepath: Path) -
3333 consumption_details ,
3434 )
3535
36- # separate bill data by fuel type, then remove fuel type info
37- dfs_by_fuel = {}
38- for f_type in bills ["FuelType" ].unique ():
39- fuel_data = bills .loc [bills ["FuelType" ] == f_type ]
40- dfs_by_fuel [f_type ] = fuel_data .drop (["FuelType" ], axis = 1 )
36+ # Separate bill data by fuel type, then remove fuel type info
37+ for fuel in bills ["FuelType" ].unique ():
38+ consumption_df = bills .loc [bills ["FuelType" ] == fuel ].drop (["FuelType" ], axis = 1 )
4139
42- # Turn the dfs of bills into xml objects that match hpxml schema
43- for fuel , consumption_df in dfs_by_fuel .items ():
44- # Grab the unit of measure from the first row, then drop it from the dataframe because it
45- # doesn't get added to every consumption section in the xml object.
4640 unit = consumption_df ["UnitofMeasure" ].iloc [0 ]
47- narrower_consumption_df = consumption_df .drop (["UnitofMeasure" ], axis = 1 )
48- # logger.debug(f"{fuel=}")
49- xml_str = narrower_consumption_df .to_xml (
41+ narrower_df = consumption_df .drop (["UnitofMeasure" ], axis = 1 )
42+
43+ if unit is None :
44+ raise ValueError (f"Unsupported fuel type: { fuel } " )
45+
46+ xml_str = narrower_df .to_xml (
5047 root_name = "ConsumptionInfo" ,
5148 row_name = "ConsumptionDetail" ,
5249 index = False ,
5350 xml_declaration = False ,
5451 namespaces = NSMAP ,
5552 )
56- new_obj = objectify .fromstring (xml_str )
5753
58- if unit is None :
59- logger .error (f"Unsupported fuel type: { fuel } " )
54+ new_obj = objectify .fromstring (xml_str .encode ())
55+
56+ consumption_type = E .ConsumptionType (
57+ E .Energy (
58+ E .FuelType (fuel ),
59+ E .UnitofMeasure (unit ),
60+ )
61+ )
6062
61- consumption_type = E .ConsumptionType (E .Energy (E .FuelType (fuel ), E .UnitofMeasure (unit )))
6263 new_obj .insert (0 , consumption_type )
6364 new_obj .insert (0 , E .UtilityID ())
6465 consumption_details .append (new_obj )
6566
6667 hpxml_object .root .append (consumption_section )
68+
6769 return hpxml_object
0 commit comments