Skip to main content

A Python library for calculating Heating Degree Days (HDD) and Cooling Degree Days (CDD) from the U.S. NWS API and Open-Meteo, with regression and visualization tools.

Project description

HDD/CDD Calculator

PyPI version Python versions

A Python library for calculating Heating Degree Days (HDD) and Cooling Degree Days (CDD) from multiple weather data sources, including:

  • U.S. National Weather Service (NWS) API — forecast data
  • Open-Meteo — free, global historical weather data (no API key required)

This package is designed primarily for energy managers and sustainability professionals who need to:

  • Understand how outdoor weather influences energy efficiency
  • Forecast energy usage based on HDD/CDD trends
  • Integrate weather-normalized analysis into energy reporting, benchmarking, and decision-making

It's suitable for both quick exploratory analyses and production workflows.

The package also supports:

  • Automated alignment of energy usage CSV data with degree days
  • Linear regression analysis between degree days and energy consumption
  • Visualization of regression results
  • An included example dataset for quick testing
  • A CLI to run the full workflow and save the plot

⚠️ Important Notes on Data Quality & Limitations

For energy efficiency analysis and forecasting, the quality of your weather data matters. Be aware of:

  • Missing or Low-Quality In Situ Data Some station datasets may have missing data or poor quality control (QC). Gaps or extreme values can bias results.

  • Limitations of Gridded Weather Data Reanalysis datasets (e.g., ERA5, which Open-Meteo draws from) are not always direct substitutes for local station observations and can be biased in some regions.

Understanding these factors helps you choose the right source and interpret results realistically.

For a deeper discussion of these issues in engineering contexts, see: "On the Use of Observed and Gridded Weather Data in Energy Analysis" — ESS Open Archive https://essopenarchive.org/doi/full/10.22541/essoar.175130623.32640121/v1


✨ Features

  • U.S.-focused but works globally via Open-Meteo
  • Calculate HDD/CDD for any lat/lon with a custom base temperature
  • Retrieve data for arbitrary date ranges (historical or recent)
  • Two data sources: NWS (U.S. forecast) & Open-Meteo (global historical)
  • No API key required for either source
  • CSV utilities for loading & aligning energy consumption data
  • Linear regression between HDD/CDD and energy usage
  • Matplotlib visualizations
  • Example dataset & CLI for end-to-end workflow
  • Clear error handling & type hints
  • PyPI-ready packaging

📦 Installation

From PyPI:

pip install hdd-cdd-calculator

Optional extras:

pip install hdd-cdd-calculator[dev]   # development tools
pip install hdd-cdd-calculator[viz]   # plotting support

From source:

git clone https://github.com/rmkenv/hdd_cdd_calculator.git
cd hdd_cdd_calculator
pip install -e .[dev]

🚀 Basic Usage

NWS Data Source (U.S. forecast, ~7-day window)

from hdd_cdd_calculator import get_degree_days

results = get_degree_days(
    lat=38.8977,
    lon=-77.0365,
    start_date="2024-06-01",
    end_date="2024-06-07",
    source="nws"
)

Open-Meteo Data Source (global historical, any date range)

from hdd_cdd_calculator import get_degree_days

results = get_degree_days(
    lat=40.7128,
    lon=-74.0060,
    start_date="2023-06-01",
    end_date="2023-06-30",
    source="open_meteo"
)

for r in results:
    print(r.date, r.high_temp, r.low_temp, r.hdd, r.cdd)

All temperatures are returned in °F. The default base temperature is 65°F.


📂 Working with Energy CSVs

Align internal usage data with HDD/CDD for regression and forecasting.

Expected CSV headers:

date,kwh,mmbtu,gal

Example — full regression workflow:

from hdd_cdd_calculator import (
    get_degree_days,
    align_energy_with_degree_days,
    perform_regression,
    plot_regression,
)

dd_results = get_degree_days(
    lat=40.7128,
    lon=-74.0060,
    start_date="2023-06-01",
    end_date="2023-06-30",
    source="open_meteo",
)

energy_vals, hdd_vals = align_energy_with_degree_days(
    dd_results,
    "my_energy_data.csv",
    energy_column="kwh",
    degree_day_type="hdd",
)

model = perform_regression(hdd_vals, energy_vals)
print(f"Slope: {model.coef_[0]:.2f} | Intercept: {model.intercept_:.2f}")

plot_regression(hdd_vals, energy_vals, model, save_path="regression_plot.png")

⚡ Quick Try (Example Dataset)

Run the built-in NYC June 2023 example end-to-end:

python -m hdd_cdd_calculator --example

Runs an HDD correlation against a sample building energy CSV and saves a regression plot to examples/regression_plot.png.


📖 API Reference

Function Description
get_degree_days(lat, lon, start_date, end_date, source, base_temp) Unified entry point — returns HDD/CDD for a date range from either source
get_degree_days_for_location(lat, lon, base_temp) NWS source: degree days for the next ~7 days
get_degree_days_for_period(lat, lon, start_date, end_date, base_temp) NWS source: filtered to a date range
fetch_meteostat_data(lat, lon, start_date, end_date, base_temp) Open-Meteo source: historical degree days
align_energy_with_degree_days(dd_results, csv_path, ...) Aligns a CSV of energy readings with degree day results
perform_regression(x, y) Linear regression between degree days and energy
plot_regression(x, y, model, ...) Matplotlib scatter + regression line
calculate_degree_days(high, low, base_temp, unit) Low-level HDD/CDD calculation from a single day's temps
validate_coordinates(lat, lon) Validates and rounds coordinates
celsius_to_fahrenheit(c) / fahrenheit_to_celsius(f) Unit conversion helpers

Data Source Options

source= Coverage Date Range API Key
"nws" U.S. only Forecast (~7 days) None
"open_meteo" Global Historical (1940–present) None

🧪 Development

git clone https://github.com/rmkenv/hdd_cdd_calculator.git
cd hdd_cdd_calculator
pip install -e .[dev]
pytest

🔍 Use Cases

  • Weather-normalized evaluation of building energy consumption
  • Forecasting heating and cooling energy demand from degree day trends
  • Energy efficiency reporting and benchmarking
  • Identifying changes in energy performance relative to outdoor temperature
  • Integrating degree day analysis into sustainability goals and compliance tracking
  • Modeling energy consumption sensitivity to temperature for operational planning

📜 License

MIT License — see LICENSE. Author: Ryan Kmetz

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eddc-0.1.5.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eddc-0.1.5-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file eddc-0.1.5.tar.gz.

File metadata

  • Download URL: eddc-0.1.5.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for eddc-0.1.5.tar.gz
Algorithm Hash digest
SHA256 4b927da0909ca8c229eb5d32eaacc617dbd81ebfad2f077718edc401921aec36
MD5 1f58eda9ef1883171fd79ceefe77586f
BLAKE2b-256 e5a3848de0548ad1e7cc1104c83df29945d10c3d7e92f9d2c3c868cfd9e29a8c

See more details on using hashes here.

File details

Details for the file eddc-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: eddc-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for eddc-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 da0283a15c30f41edf0eb88107b91b9fdede77115d1a13dd24f3282f43a2ef44
MD5 41fd41521027f6bf74a40914c64d5356
BLAKE2b-256 af9d2048215f946aa12eac66400a3c9b76ae128b6d237c0df53a05eb2158c8ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page