Note
Go to the end to download the full example code.
Daytime/Nighttime Flagging¶
Demonstrates identifying daytime and nighttime periods using solar geometry calculations. Flags are determined by comparing potential shortwave radiation to a threshold, useful for quality control and analysis of ecosystem processes that vary between day and night.
Best for: Separating measurements into day/night periods for analysis.
Calculate Daytime/Nighttime Flags¶
This example shows how to identify daytime and nighttime periods based on solar elevation angle and potential radiation. Different thresholds can be used depending on the application.
import matplotlib.pyplot as plt
import diive as dv
# Load example data
df = dv.load_exampledata_parquet()
# Site coordinates: CH-DAV (Davos, Switzerland)
SITE_LAT = 46.81533
SITE_LON = 9.85591
UTC_OFFSET = 1
NIGHTTIME_THRESHOLD = 20 # W/m²
print("Daytime/Nighttime Flag Calculation")
print("=" * 50)
print(f"Site: CH-DAV (Davos)")
print(f"Coordinates: {SITE_LAT}°N, {SITE_LON}°E")
print(f"Nighttime threshold: {NIGHTTIME_THRESHOLD} W/m²")
# Calculate daytime/nighttime flags
dnf = dv.variables.DaytimeNighttimeFlag(
timestamp_index=df.index,
nighttime_threshold=NIGHTTIME_THRESHOLD,
lat=SITE_LAT,
lon=SITE_LON,
utc_offset=UTC_OFFSET
)
# Get results
results = dnf.get_results()
daytime_flag = dnf.get_daytime_flag()
nighttime_flag = dnf.get_nighttime_flag()
swinpot = dnf.get_swinpot()
print(f"\nResults:")
print(f"Total records: {len(results)}")
print(f"Daytime records: {(daytime_flag == 1).sum()}")
print(f"Nighttime records: {(nighttime_flag == 1).sum()}")
print(f"Potential radiation range: {swinpot.min():.1f} to {swinpot.max():.1f} W/m²")
> Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.049
seconds).
Daytime/Nighttime Flag Calculation
==================================================
Site: CH-DAV (Davos)
Coordinates: 46.81533°N, 9.85591°E
Nighttime threshold: 20 W/m²
Results:
Total records: 175296
Daytime records: 87503
Nighttime records: 87793
Potential radiation range: 0.0 to 1221.2 W/m²
Visualize Results with Heatmaps¶
Display spatial-temporal patterns of daytime/nighttime flags and potential radiation across the entire measurement period.
print("\nGenerating heatmap visualizations...")
fig, axes = plt.subplots(1, 3, figsize=(20, 9),
gridspec_kw={'wspace': 0.1},
constrained_layout=True)
dv.plotting.HeatmapDateTime(series=daytime_flag).plot(
ax=axes[0],
zlabel="flag (1=daytime)",
cb_digits_after_comma=0)
dv.plotting.HeatmapDateTime(series=nighttime_flag).plot(
ax=axes[1],
zlabel="flag (1=nighttime)",
cb_digits_after_comma=0)
dv.plotting.HeatmapDateTime(series=swinpot).plot(
ax=axes[2],
zlabel=r"$\mathrm{W\ m^{-2}}$",
cb_digits_after_comma=0)
axes[0].set_title("Daytime flag")
axes[1].set_title("Nighttime flag")
axes[2].set_title("Potential radiation")
plt.show()

Generating heatmap visualizations...
Total running time of the script: (0 minutes 1.437 seconds)