A Python package for quality control (QC) checks on BSRN station-to-archive files.
Project description
bsrn
bsrn is a Python package for the Baseline Surface Radiation Network (BSRN). It provides automated quality control (QC), solar geometry, clear-sky modeling, clear-sky detection (CSD), cloud enhancement event (CEE) detection, irradiance separation, data retrieval, and visualization tools for BSRN station-to-archive files.
๐ Getting Started
Installation
From PyPI (stable release):
pip install bsrn
From GitHub (latest development version):
pip install git+https://github.com/dazhiyang/bsrn.git
Quick Example (Single-File Workflow)
from bsrn.io.retrieval import download_bsrn_stn, get_bsrn_file_inventory
from bsrn.io.readers import read_bsrn_station_to_archive
from bsrn.modeling.clear_sky import add_clearsky_columns
from bsrn.constants import BSRN_STATIONS
# 1. See what data is available
inventory = get_bsrn_file_inventory(["QIQ"], username="your_user", password="your_pass")
# 2. Download data for a station
download_bsrn_stn("QIQ", "data/QIQ", username="your_user", password="your_pass")
# 3. Read a single monthly file (one file at a time)
df = read_bsrn_station_to_archive("data/QIQ/qiq0124.dat.gz")
# 4. Add clear-sky reference columns for this file only
df = add_clearsky_columns(df, "QIQ")
๐ Features
The QC implementation is primarily based on the BSRN Operations Manual (2018) and Forstinger et al. (2021). See code for other references.
- Level 1 (Physically Possible): Absolute physical bounds for $G_h, B_n, D_h$, and $L_d$.
- Level 2 (Extremely Rare): Climatological limits for specific regimes.
- Level 3 (Comparison): Consistency checks ($G_h$ vs $B_n \cos Z + D_h$) with zenith-dependent thresholds.
- Level 4 (Diffuse Ratio): Diffuse-fraction and $k$โ$k_t$ checks combining $G_h$, $D_h$, and extraterrestrial irradiance.
- Level 5 (K-Indices): Advanced clearness-index and $k_b$/$k_t$ index tests using clear-sky benchmarks and site elevation.
- Level 6 (Tracker-Off Detection): Identify tracking errors by comparing measured values with clear-sky and extraterrestrial irradiance.
- Solar Geometry: Native NREL SPA implementation for high-precision solar position calculations.
- Clear-Sky Models: Ineichen (monthly Linke turbidity), McClear (CAMS SoDa API, from 2004 onward), and REST2 (MERRA-2 from Hugging Face).
- Clear-Sky Detection (CSD): Reno, Ineichen, Lefevre, and BrightSun methods to identify clear-sky periods from irradiance time series.
- Cloud Enhancement Event (CEE) Detection: Killinger, Gueymard-style, and Wang methods to detect periods when measured GHI significantly exceeds clear-sky or extraterrestrial references and to filter events by temporal duration.
- Irradiance Separation: Erbs, BRL, Engerer2, and Yang4 models to estimate diffuse fraction and DHI/BNI from GHI.
- Robust Retrieval: High-level API for FTP downloads from BSRN-AWI with exponential backoff retries (analysis functions assume one station-to-archive file at a time).
- Visualization: Data availability heatmaps and k vs kt separation plots via
plotnine.
๐ File Structure
[!NOTE] Not all files are uploaded with Git. Data files and intermediate outputs are excluded via
.gitignore.
bsrn-qc/
โโโ pyproject.toml
โโโ LICENSE
โโโ README.md
โโโ .gitignore
โโโ src/
โ โโโ bsrn/
โ โโโ __init__.py
โ โโโ constants.py # Station database, Linke turbidity & physical constants
โ โโโ io/
โ โ โโโ readers.py # Read .001, .002 station-to-archive files
โ โ โโโ retrieval.py # FTP downloads with retries
โ โ โโโ writers.py # Export results
โ โโโ physics/
โ โ โโโ spa.py # Native NREL SPA (solar position algorithm)
โ โ โโโ geometry.py # Solar position and extraterrestrial irradiance
โ โโโ qc/
โ โ โโโ ppl.py # Physically possible limits (Level 1)
โ โ โโโ erl.py # Extremely rare limits (Level 2)
โ โ โโโ closure.py # Internal consistency checks (Level 3)
โ โ โโโ diff_ratio.py # Diffuse ratio checks (Level 4)
โ โ โโโ k_index.py # Radiometric index tests (Level 5)
โ โ โโโ tracker.py # Solar tracker off detection (Level 6)
โ โ โโโ wrapper.py # High-level QC pipeline
โ โโโ visualization/
โ โ โโโ availability.py # File coverage heatmaps (plotnine)
โ โ โโโ qc_table.py # QC result tables
โ โ โโโ separation.py # Decomposition visualization
โ โ โโโ timeseries.py # Time series plots
โ โโโ utils/
โ โ โโโ calculations.py # Supporting math
โ โ โโโ quality.py # Quality utilities
โ โ โโโ clear_sky_detection.py # Clear-sky detection (Reno, Ineichen, Lefevre, BrightSun)
โ โ โโโ cee_detection.py # Cloud enhancement detection (Killinger, Gueymard, Wang)
โ โโโ modeling/
โ โโโ clear_sky.py # Ineichen clear-sky model
โ โโโ separation.py # Irradiance separation (Erbs, BRL, Engerer2, Yang4)
โโโ tests/
โ โโโ test_io.py
โ โโโ test_physics.py
โ โโโ test_visualization.py
โ โโโ test_modeling.py
โ โโโ test_cs_detection.py
โโโ notebooks/
โ โโโ qiq_cee_pipeline.ipynb # QIQ CEE pipeline demo
โ โโโ mcclear_qiq_september.ipynb # McClear model demo for QIQ (September)
โโโ data/
โโโ download_qiq.py # Script to download QIQ station data
โโโ retrive_Linke.py # Script to retrieve Linke turbidity data
โโโ QIQ/ # Sample data for station QIQ
๐ Examples
Solar Position
import pandas as pd
from bsrn.physics.geometry import get_solar_position, get_bni_extra
times = pd.date_range("2024-07-01", periods=1440, freq="1min", tz="UTC")
solpos = get_solar_position(times, lat=47.80, lon=124.49, elev=170)
print(solpos[["zenith", "apparent_zenith", "azimuth"]].head())
Extraterrestrial Irradiance
from bsrn.physics.geometry import get_bni_extra
bni_extra = get_bni_extra(times) # Spencer (1971) method
Clear-Sky GHI (Ineichen)
from bsrn.modeling.clear_sky import add_clearsky_columns
df = add_clearsky_columns(df, "QIQ")
# Adds columns: ghi_clear, bni_clear, dhi_clear
Clear-Sky GHI from McClear (CAMS)
from bsrn.modeling.clear_sky import add_clearsky_columns
# McClear data are available from 2004-01-01 onward.
# McClear ๆฐๆฎ่ช 2004-01-01 ่ตทๅฏ็จใ
df = add_clearsky_columns(
df,
station_code="QIQ",
model="mcclear",
mcclear_email="your_email@example.com", # SoDa / CAMS account email
)
# Adds columns: ghi_clear, bni_clear, dhi_clear based on CAMS McClear
Clear-Sky GHI from REST2 (MERRA-2 via Hugging Face)
REST2 uses MERRA-2 atmospheric inputs from dazhiyang/bsrn-merra2. The bsrn package fetches parquet files into RAM (no disk cache) when model="rest2" is used.
from bsrn.modeling.clear_sky import add_clearsky_columns
# MERRA-2 is fetched from Hugging Face into RAM automatically.
df = add_clearsky_columns(df, station_code="QIQ", model="rest2")
# Adds columns: ghi_clear, bni_clear, dhi_clear based on REST2 + MERRA-2
Clear-Sky Detection
from bsrn.utils import detect_clearsky
# Requires GHI and clear-sky GHI (e.g. from add_clearsky_columns)
out = detect_clearsky("reno", ghi=df["ghi"], ghi_clear=df["ghi_clear"], times=df.index)
# out["is_clearsky"] is True/False/NA; out["cloud_flag"] is 0/1/NaN
# Other methods: "ineichen", "lefevre", "brightsun" (different inputs)
Cloud Enhancement Event (CEE) Detection
from bsrn.utils.cee_detection import detect_cee
# Killinger CEE detection: requires 1โmin GHI, clear-sky GHI, zenith, and a 1โmin index
out_cee_k = detect_cee(
"killinger",
ghi=df["ghi"],
ghi_clear=df["ghi_clear"],
zenith=df["zenith"],
times=df.index,
)
# Gueymard-style CEE detection: flags kt = G_h / E_0 > 1
out_cee_g = detect_cee(
"gueymard",
ghi=df["ghi"],
ghi_extra=df["ghi_extra"],
times=df.index,
)
# Wang CEE detection: combines GHI, BNI, DHI masks and removes overly long events
out_cee_w = detect_cee(
"wang",
ghi=df["ghi"],
ghi_clear=df["ghi_clear"],
bni=df["bni"],
bni_clear=df["bni_clear"],
dhi=df["dhi"],
dhi_clear=df["dhi_clear"],
times=df.index,
mag_threshold=1.10,
bni_fraction=0.8,
dhi_multiplier=1.5,
max_duration_minutes=15.0,
)
# out_cee_*["is_enhancement"] is True/False/NA; out_cee_*["cee_flag"] is 0/1/NaN
Data Availability Heatmap
from bsrn.visualization.availability import plot_bsrn_availability
fig = plot_bsrn_availability(inventory_df, station_code="QIQ")
fig.save("availability.png", dpi=300)
๐ License
MIT License. See LICENSE for details.
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 bsrn-0.1.2.tar.gz.
File metadata
- Download URL: bsrn-0.1.2.tar.gz
- Upload date:
- Size: 105.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce90e2108443260ae0685f482686ec173815616f24ff8e914d0833d43a4a5a2a
|
|
| MD5 |
3db79eeb577973d3d6719e7b501d2abb
|
|
| BLAKE2b-256 |
a3da9425973dad5c4d928bbca5b017e69f0c6a42fa651304b02663d54a2d45ae
|
File details
Details for the file bsrn-0.1.2-py3-none-any.whl.
File metadata
- Download URL: bsrn-0.1.2-py3-none-any.whl
- Upload date:
- Size: 115.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27cc8f04cea7d58ce6b60b9753b15362800b6f971dfd9af4d9cdcfa5600e3034
|
|
| MD5 |
22e26cc0c3513436f7c4ca1b6b399307
|
|
| BLAKE2b-256 |
a11b43d7c1e02c99d7d33d265ed1e926815e95e5c73b4e236f75d6f3e3de15aa
|