Install
openclaw skills install @ruiduobao/drought-monitorCalculate SPI (Standardized Precipitation Index) and SPEI (Standardized Precipitation Evapotranspiration Index) from NASA POWER API data for drought monitoring. Supports multiple timescales (1-24 months), drought classification, and trend analysis.
openclaw skills install @ruiduobao/drought-monitorCalculate SPI (Standardized Precipitation Index) and SPEI (Standardized Precipitation Evapotranspiration Index) for drought monitoring using NASA POWER API precipitation data.
# Calculate 3-month SPI for a location
python scripts\drought-monitor.py spi --lat 39.9042 --lon 116.4074 --start 2020-01-01 --end 2023-12-31 --scale 3
# Calculate 12-month SPEI from local water balance CSV
python scripts\drought-monitor.py spei --input water_balance.csv --scale 12
# Generate drought report
python scripts\drought-monitor.py report --input spi_3m.csv --output report.json
| Parameter | Description | Default |
|---|---|---|
--lat | Latitude (-90 to 90) | Required |
--lon | Longitude (-180 to 180) | Required |
--start | Start date (YYYY-MM-DD) | Required |
--end | End date (YYYY-MM-DD) | Required |
--scale | SPI/SPEI timescale in months | 3 |
--input | Local CSV for offline mode | None |
--output | Output file path | Auto-generated |
pip install requests>=2.28.0 tqdm numpy scipy pandas
# Or: pip install -r scripts/requirements.txt
| Package | Purpose |
|---|---|
scipy | Gamma/log-logistic distribution fitting |
numpy | Numerical computation |
pandas | CSV I/O and time series handling |
requests | NASA POWER API calls |
tqdm | Progress bars |
PRECTOTCORR (corrected precipitation, mm/day)CSV output columns:
| Column | Description |
|---|---|
date | Monthly date (YYYY-MM) |
precipitation | Monthly precipitation total (mm) |
spi | SPI value (standardized) |
classification | Drought/wet classification label |
# Loop over multiple locations in shell
for lat_lon in "39.9 116.4" "31.2 121.5" "23.1 113.3"; do
set -- $lat_lon
python scripts\drought-monitor.py spi --lat $1 --lon $2 --start 2020-01-01 --end 2023-12-31 --scale 3 --output "spi_${1}_${2}.csv"
done
| Error | Cause | Solution |
|---|---|---|
ConnectionError | Network issue | Check internet, retry |
HTTP 429 | Rate limit | Wait 60s, retry |
ValueError | Invalid input (e.g., bad date) | Check parameter format |
| Empty output | No data for location/dates | Try different parameters |
ModuleNotFoundError | Missing dep | Run pip install |
For reliable SPI/SPEI calculation, at least 20–30 years of monthly data is recommended. Shorter records may produce unreliable distribution fitting, especially for 12- and 24-month scales.
# After computing SPI, run trend analysis
python scripts\drought-monitor.py report --input spi_3m.csv --output trend.json
The report subcommand performs linear regression on the SPI time series and outputs slope, p-value, and trend direction.
To export the raw precipitation time series without SPI calculation, use:
python scripts\drought-monitor.py spi --lat 39.9042 --lon 116.4074 --start 2020-01-01 --end 2023-12-31 --scale 3 --export-raw
This produces an additional raw_precipitation.csv with daily/monthly precipitation.
The default thresholds follow McKee et al. (1993). To customize, pass --thresholds:
python scripts\drought-monitor.py spi --lat 39.9042 --lon 116.4074 --start 2020-01-01 --end 2023-12-31 --scale 3 --thresholds "0.5,1.0,1.5,2.0"
The 4 values define boundaries between: normal / moderate / severe / extreme (symmetric for wet/dry).
# Offline mode with pre-downloaded precipitation CSV
# CSV format: date (YYYY-MM-DD), precipitation (mm)
python scripts\drought-monitor.py spi --input precip.csv --scale 6
If you use this tool, please cite the SPI methodology:
@article{mckee1993relationship,
title={The relationship of drought frequency and duration to time scales},
author={McKee, Thomas B and Doesken, Nolan J and Kleist, John and others},
journal={Proceedings of the 8th Conference on Applied Climatology},
volume={17},
number={22},
pages={179--183},
year={1993}
}
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("spi_3m.csv")
fig, ax = plt.subplots(figsize=(12, 4))
ax.fill_between(df["date"], df["spi"], 0, where=df["spi"]<0, color="red", alpha=0.3, label="Drought")
ax.fill_between(df["date"], df["spi"], 0, where=df["spi"]>=0, color="blue", alpha=0.3, label="Wet")
ax.plot(df["date"], df["spi"], color="black", linewidth=0.8)
ax.axhline(y=0, color="gray", linestyle="--")
ax.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig("spi_timeseries.png", dpi=150)
| SPI/SPEI Value | Classification |
|---|---|
| > 2.0 | Extremely wet |
| 1.5 to 2.0 | Very wet |
| 1.0 to 1.5 | Moderate wet |
| -1.0 to 1.0 | Normal |
| -1.5 to -1.0 | Moderate drought |
| -2.0 to -1.5 | Severe drought |
| < -2.0 | Extreme drought |
| Error | Cause | Solution |
|---|---|---|
ConnectionError | Network issue | Check internet, retry |
HTTP 429 | Rate limit | Wait 60s, retry |
ValueError | Invalid input | Check parameter format |
| Empty output | No data | Try different parameters |
ModuleNotFoundError | Missing dep | Run pip install |
while IFS=',' read -r id lat lon; do
python scripts\drought-monitor.py spi --lat $lat --lon $lon --scale 3 --start 2020-01-01 --end 2023-12-31 --output spi_${id}.csv
sleep 1
done < locations.csv
# .github/workflows/drought-monitor.yml
name: Drought Monitoring
on:
schedule:
- cron: '0 6 1 * *' # Monthly
jobs:
spi:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install requests scipy pandas
- run: |
python scripts\drought-monitor.py spi \
--lat 39.9 --lon 116.4 --scale 3 \
--start $(date -d '1 year ago' +%Y-%m-%d) \
--end $(date +%Y-%m-%d) \
--output data/beijing_spi3.csv
python scripts\drought-monitor.py spi --lat 39.9 --lon 116.4 --scale 3 --start 2020-01-01 --end 2023-12-31 --output spi.csv
psql -d gis_db -c "\COPY spi_index(station_id, date, spi) FROM 'spi.csv' CSV HEADER"
--scale 1 for monthly agricultural drought; --scale 12 for long-term hydrologicalsleep 1 between location queries to respect NASA POWER rate limits--export-raw to get precipitation data without SPI calculation基于 NASA POWER API 降水数据计算 SPI(标准化降水指数)和 SPEI(标准化降水蒸散指数),用于干旱监测。
pip install requests>=2.28.0 tqdm numpy scipy pandas
# 或: pip install -r scripts/requirements.txt
| 包 | 用途 |
|---|---|
scipy | Gamma/log-logistic 分布拟合 |
numpy | 数值计算 |
pandas | CSV 读写和时间序列处理 |
requests | NASA POWER API 调用 |
tqdm | 进度条 |
CSV 输出列:
| 列名 | 说明 |
|---|---|
date | 月度日期(YYYY-MM) |
precipitation | 月降水量(mm) |
spi | SPI 值(标准化) |
classification | 干旱/湿润等级标签 |
# Shell 循环处理多个位置
for lat_lon in "39.9 116.4" "31.2 121.5" "23.1 113.3"; do
set -- $lat_lon
python scripts\drought-monitor.py spi --lat $1 --lon $2 --start 2020-01-01 --end 2023-12-31 --scale 3 --output "spi_${1}_${2}.csv"
done
| 错误 | 原因 | 解决方案 |
|---|---|---|
ConnectionError | 网络问题 | 检查网络,重试 |
HTTP 429 | 速率限制 | 等待 60 秒后重试 |
ValueError | 无效输入 | 检查参数格式 |
| 空输出 | 无数据 | 尝试不同参数 |
ModuleNotFoundError | 缺少依赖 | 运行 pip install |
为获得可靠的 SPI/SPEI 计算结果,建议使用至少 20-30 年的月数据。较短的记录可能导致分布拟合不可靠,尤其是 12 和 24 个月尺度。
# 计算 SPI 后运行趋势分析
python scripts\drought-monitor.py report --input spi_3m.csv --output trend.json
report 子命令对 SPI 时间序列执行线性回归,输出斜率、p 值和趋势方向。
导出未经 SPI 计算的原始降水时间序列:
python scripts\drought-monitor.py spi --lat 39.9042 --lon 116.4074 --start 2020-01-01 --end 2023-12-31 --scale 3 --export-raw
将生成包含日/月降水量的 raw_precipitation.csv。
默认阈值遵循 McKee et al. (1993)。自定义阈值使用 --thresholds:
python scripts\drought-monitor.py spi --lat 39.9042 --lon 116.4074 --start 2020-01-01 --end 2023-12-31 --scale 3 --thresholds "0.5,1.0,1.5,2.0"
4 个值定义正常/中度/严重/极端之间的分界(湿润/干旱对称)。
# 离线模式,使用预下载的降水 CSV
# CSV 格式:date (YYYY-MM-DD), precipitation (mm)
python scripts\drought-monitor.py spi --input precip.csv --scale 6
如果使用本工具,请引用 SPI 方法论:
@article{mckee1993relationship,
title={The relationship of drought frequency and duration to time scales},
author={McKee, Thomas B and Doesken, Nolan J and Kleist, John and others},
journal={Proceedings of the 8th Conference on Applied Climatology},
volume={17},
number={22},
pages={179--183},
year={1993}
}
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("spi_3m.csv")
fig, ax = plt.subplots(figsize=(12, 4))
ax.fill_between(df["date"], df["spi"], 0, where=df["spi"]<0, color="red", alpha=0.3, label="干旱")
ax.fill_between(df["date"], df["spi"], 0, where=df["spi"]>=0, color="blue", alpha=0.3, label="湿润")
ax.plot(df["date"], df["spi"], color="black", linewidth=0.8)
ax.axhline(y=0, color="gray", linestyle="--")
ax.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig("spi_timeseries.png", dpi=150)
| 错误 | 原因 | 解决方案 |
|---|---|---|
ConnectionError | 网络问题 | 检查网络,重试 |
HTTP 429 | 速率限制 | 等待 60 秒后重试 |
ValueError | 无效输入 | 检查参数格式 |
| 空输出 | 无数据 | 尝试不同参数 |
ModuleNotFoundError | 缺少依赖 | 运行 pip install |
基于 NASA POWER API 降水数据计算 SPI(标准化降水指数)和 SPEI(标准化降水蒸散指数),用于干旱监测。
# 计算北京 3 个月 SPI
python scripts\drought-monitor.py spi --lat 39.9042 --lon 116.4074 --start 2020-01-01 --end 2023-12-31 --scale 3
# 从本地 CSV 计算 12 个月 SPEI
python scripts\drought-monitor.py spei --input water_balance.csv --scale 12
# 生成干旱报告
python scripts\drought-monitor.py report --input spi_3m.csv --output report.json
PRECTOTCORR(校正降水量,mm/天)| SPI/SPEI 值 | 等级 |
|---|---|
| > 2.0 | 极端湿润 |
| 1.5 至 2.0 | 非常湿润 |
| 1.0 至 1.5 | 中度湿润 |
| -1.0 至 1.0 | 正常 |
| -1.5 至 -1.0 | 中度干旱 |
| -2.0 至 -1.5 | 严重干旱 |
| < -2.0 | 极端干旱 |