A Python library for calculating Heating Degree Days (HDD) and Cooling Degree Days (CDD) from multiple weather data sources including the U.S. NWS API and Meteostat, with regression and visualization tools.
Project description
HDD/CDD Calculator
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
- Meteostat – global historical weather data
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 simple CLI to run the full workflow and save the plot
✨ Features
- Calculate HDD/CDD for any location by latitude/longitude
- Custom base temperature support
- Retrieve degree day data for specific date ranges
- Two data sources: NWS (U.S. forecast) and Meteostat (global historical)
- CSV utilities for loading and aligning energy consumption data
- Linear regression between degree days and energy usage
- Matplotlib visualization of results
- CLI example that runs end-to-end and saves a screenshot
- Clear error handling and type hints
- PyPI-ready packaging
📦 Installation
From PyPI:
pip install hdd-cdd-calculator
Optional extras:
pip install hdd-cdd-calculator[dev] # dev tools (pytest, linting, typing)
pip install hdd-cdd-calculator[viz] # includes matplotlib for plotting
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
from hdd_cdd_calculator import get_degree_days
results = get_degree_days(
lat=38.8977,
lon=-77.0365,
start_date="2023-06-01",
end_date="2023-06-07",
source="nws"
)
for r in results:
print(f"{r.date} | High: {r.high_temp}°F | Low: {r.low_temp}°F | HDD: {r.hdd} | CDD: {r.cdd}")
Meteostat Data Source
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="meteostat"
)
📂 Working with Energy CSVs
Expected CSV headers:
date,kwh,mmbtu,gal
Load Energy Data
from hdd_cdd_calculator import read_energy_data_from_csv
energy_values = read_energy_data_from_csv("energy_data.csv", column="kwh")
Align CSV & Degree Days → Regression → Plot
from hdd_cdd_calculator import (
get_degree_days_for_period,
align_energy_with_degree_days,
perform_regression,
plot_regression
)
import pandas as pd
# Step 1: Fetch HDD data
dd_results = get_degree_days_for_period(
lat=40.7128,
lon=-74.0060,
start_date="2023-06-01",
end_date="2023-06-10"
)
# Step 2: Align with CSV
energy_vals, hdd_vals = align_energy_with_degree_days(
dd_results,
"examples/sample_energy_data.csv",
energy_column="kwh",
degree_day_type="hdd"
)
# Step 3: Fit regression
model = perform_regression(hdd_vals, energy_vals)
print(f"Slope: {model.coef_:.2f}, Intercept: {model.intercept_:.2f}")
# Step 4: Plot and save
plot_regression(
pd.Series(hdd_vals),
pd.Series(energy_vals),
model,
save_path="examples/regression_plot.png" # Save file in examples/
)
⚡ Quick Try (With Included Dataset)
We include a complete dataset + CLI workflow.
From the repo root or after installing:
python -m hdd_cdd_calculator --example
This will:
- Fetch HDD data for NYC (June 1–10, 2023)
- Align with
examples/sample_energy_data.csv - Perform regression
- Display a plot and save it to
examples/regression_plot.png
Example Output
📖 API Overview
Data fetching
get_degree_days_for_location(...)get_degree_days_for_period(...)fetch_meteostat_data(...)get_degree_days(...)— unified source selector
CSV utilities
read_energy_data_from_csv(path, column="kwh")read_energy_data_with_dates(path, column="kwh")align_energy_with_degree_days(degree_days, csv, energy_column="kwh", degree_day_type="hdd")
Analysis
perform_regression(degree_days, energy_usage)plot_regression(degree_days, energy_usage, model, save_path=None, show=True)
Utilities
validate_coordinates(...)calculate_degree_days(...)- Temperature conversions:
fahrenheit_to_celsius(...),celsius_to_fahrenheit(...)
🧪 Development
Clone and install with developer tools:
git clone https://github.com/rmkenv/hdd_cdd_calculator.git
cd hdd_cdd_calculator
pip install -e .[dev]
Run tests:
pytest
📜 License
MIT License — see LICENSE for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hdd_cdd_calculator-0.1.4.tar.gz.
File metadata
- Download URL: hdd_cdd_calculator-0.1.4.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de581966266847ba4cac5343e74ee5746dcfa8419e720b4501aef1873594eeba
|
|
| MD5 |
99fc6eb59e58a6b6eba2f506389bc3dd
|
|
| BLAKE2b-256 |
9ee40cbdfe007ae3e0217da088f756df3579f9cf2ba4f64a9fdcf400f8487f21
|
File details
Details for the file hdd_cdd_calculator-0.1.4-py3-none-any.whl.
File metadata
- Download URL: hdd_cdd_calculator-0.1.4-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af87f1daeea1e69e2e8cfa3b08fe61864c60182eba91885393bfe0b6025503af
|
|
| MD5 |
dcd935c930a039afc3bc12b3bbb5079d
|
|
| BLAKE2b-256 |
0c7b66566c7252f856f43e3d6a019fb9fccd9cca6c4e1965a4df40adf2083aad
|