Skip to content

Commit

Permalink
more merge errors
Browse files Browse the repository at this point in the history
  • Loading branch information
john-zither committed Jan 20, 2024
1 parent c9b94f8 commit fa7307c
Showing 1 changed file with 0 additions and 113 deletions.
113 changes: 0 additions & 113 deletions scripts/multi_thermostat_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,119 +85,6 @@
CLIMATE_ZONE_INSUFFICIENT_FILEPATH = OUTPUT_DIR / CLIMATE_ZONE_INSUFFICIENT_FILENAME
ZIP_FILEPATH = OUTPUT_DIR / ZIP_FILENAME

def write_errors(filepath, fieldnames, errors, extrasaction=None):
with open(filepath, 'w') as error_file:
if extrasaction:
writer = csv.DictWriter(
error_file,
fieldnames=fieldnames,
dialect='excel',
extrasaction=extrasaction)
else:
writer = csv.DictWriter(
error_file,
fieldnames=fieldnames,
dialect='excel')
writer.writeheader()
for error in errors:
writer.writerow(error)


def count_metadata(filepath):
with open(filepath, 'r') as metadata_file:
reader = csv.DictReader(metadata_file)
return len(list(reader))


def main():
'''
This script processes the thermostat metadata and data files to generate
the certification files for submission to EPA.
'''

logging.basicConfig()
with open(LOGGING_CONFIG, 'r') as logging_config:
logging.config.dictConfig(json.load(logging_config))

# Uses the 'epathermostat' logging
logger = logging.getLogger('epathermostat')
logger.debug('Starting...')
logging.captureWarnings(CAPTURE_WARNINGS)

thermostats, import_errors = from_csv(
METADATA_FILENAME,
verbose=VERBOSE,
save_cache=SAVE_CACHE,
cache_path=CACHE_PATH)

# This logs any import errors that might have occurred.
if import_errors:
# This writes a file with the thermostat ID as part of the file. This
# is for your own troubleshooting
fieldnames = ['thermostat_id', 'error']
write_errors(IMPORT_ERRORS_FILEPATH, fieldnames, import_errors)

# This writes a file without the thermostat ID as part of the file.
# This file is sent as part of the certification to help with
# diagnosing issues with missing thermostats
fieldnames = ['error']
write_errors(SANITIZED_IMPORT_ERRORS_FILEPATH, fieldnames, import_errors, extrasaction='ignore')

# Check to see how many thermostats we are importing and warn if less than 30%
metadata_count = count_metadata(METADATA_FILENAME)
thermostat_estimate_count = thermostats.__length_hint__() # Get a rough estimate of the number of thermostats
percent_thermostats_imported = (thermostat_estimate_count / metadata_count) * 100
if percent_thermostats_imported < 30:
logger.warning(f'Imported {percent_thermostats_imported}% of thermostats, which is less than 30%')
logger.warning(f'Please check {IMPORT_ERRORS_FILEPATH} for more details')
else:
logger.debug(f'Imported {percent_thermostats_imported}% of thermostats')

metrics = multiple_thermostat_calculate_epa_field_savings_metrics(thermostats)

metrics_out = metrics_to_csv(metrics, METRICS_FILEPATH)

stats, insufficient = compute_summary_statistics(metrics_out)

if insufficient:
fieldnames = ['climate_zone', 'count', 'error']
write_errors(CLIMATE_ZONE_INSUFFICIENT_FILEPATH, fieldnames, insufficient)

certification_to_csv(stats, CERTIFICATION_FILEPATH, PRODUCT_ID)

summary_statistics_to_csv(
stats,
STATS_FILEPATH,
PRODUCT_ID)

if ADVANCED_STATS:
stats_advanced = compute_summary_statistics(
metrics_out,
advanced_filtering=True)

summary_statistics_to_csv(
stats_advanced,
STATS_ADVANCED_FILEPATH,
PRODUCT_ID)

# Compile the files together in a neat package
files_to_zip = [
CERTIFICATION_FILEPATH,
STATS_FILEPATH,
]
if ADVANCED_STATS:
files_to_zip.append(STATS_ADVANCED_FILEPATH)

if import_errors:
files_to_zip.append(SANITIZED_IMPORT_ERRORS_FILEPATH)

if insufficient:
files_to_zip.append(CLIMATE_ZONE_INSUFFICIENT_FILEPATH)

with ZipFile(ZIP_FILEPATH, 'w') as certification_zip:
for filename in files_to_zip:
if filename.exists():
certification_zip.write(filename, arcname=filename.name)

if __name__ == '__main__':
mult_thermostat_driver(
Expand Down

0 comments on commit fa7307c

Please sign in to comment.