Ladybug Tools 上手教程
关联项目:Ladybug Tools 前置要求:Python 3.9+(以当前版本为准)与 pip;一份 EPW 气象文件;了解经纬度、时区与太阳角度概念;Grasshopper 使用需 Rhino 环境。 预计耗时:pip 安装约 5 分钟;跑通 EPW 与太阳位置示例约 30 分钟;完成一次日照表格输出约 1 小时。 官方仓库:github.com/ladybug-tools/ladybug 前置要求:Python 3.9+(以当前版本为准)与 pip;一份 EPW 气象文件;了解经纬度、时区与太阳角度概念;Grasshopper 使用需 Rhino 环境。
预计耗时:pip 安装约 5 分钟;跑通 EPW 与太阳位置示例约 30 分钟;完成一次日照表格输出约 1 小时。
官方仓库:github.com/ladybug-tools/ladybug
本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。
一、环境准备
核心 Python 包可直接安装:
python -m pip install ladybug
python -c "import ladybug; print(ladybug.__file__)"
需要舒适度指标时可装 ladybug-comfort;Grasshopper 插件请用官方安装器,版本要求以官方文档为准。
二、安装部署
验证安装与关键模块:
python -c "from ladybug.epw import EPW; from ladybug.sunpath import Sunpath; print('ladybug ok')"
预期输出:ladybug ok,无 ImportError 即成功。
三、快速上手:跑通第一个示例
读取 EPW 并计算夏至正午太阳位置:
from ladybug.epw import EPW
from ladybug.sunpath import Sunpath
epw = EPW("weather.epw")
print(epw.location.city, epw.location.latitude, epw.location.longitude)
print(epw.dry_bulb_temperature[:5])
sunpath = Sunpath(
latitude=epw.location.latitude,
longitude=epw.location.longitude,
timezone=epw.location.timezone,
)
sun = sunpath.calculate_sun(month=6, day=21, hour=12)
print(sun.altitude, sun.azimuth)
EPW 解析气象文件;location 含城市、经纬度与时区;dry_bulb_temperature 是逐时数据集合;calculate_sun() 返回太阳对象,altitude 为高度角、azimuth 为方位角。把 weather.epw 换成你自己的气象文件。
四、核心工作流
任务:输出某地点一年中每月 21 日正午的太阳高度角表
- 准备气象文件:把
weather.epw放到工作目录。 - 读取位置:
from ladybug.epw import EPW
epw = EPW("weather.epw")
loc = epw.location
- 逐月计算太阳位置:
from ladybug.sunpath import Sunpath
sp = Sunpath(latitude=loc.latitude, longitude=loc.longitude, timezone=loc.timezone)
rows = []
for month in range(1, 13):
sun = sp.calculate_sun(month=month, day=21, hour=12)
rows.append((month, round(sun.altitude, 2), round(sun.azimuth, 2)))
print(rows[:3])
预期:打印形如 (1, 角度, 方位角) 的元组列表。
- 导出 CSV:
import csv
with open("solar_noon.csv", "w", newline="", encoding="utf-8") as f:
w = csv.writer(f)
w.writerow(["month", "altitude", "azimuth"])
w.writerows(rows)
预期:生成 13 行(含表头)的 CSV。
- 用 EPW 温度数据做对照统计:
temps = epw.dry_bulb_temperature
print(temps.average(), temps.max(), temps.min())
预期:输出年平均、最高与最低干球温度。
五、进阶实践
- 舒适度指标:
pip install ladybug-comfort后按官方文档调用 UTCI/PMV 计算函数。 - 数据集合操作:
collection.filter_by_datetime()、collection.average()等可按时间筛选与统计,方法名以当前版本为准。 - 图表输出:ladybug 提供多种 chart 类,可导出 HTML/SVG 供报告使用,类名以官方文档为准。
- Grasshopper 联动:用官方安装器装 Ladybug 插件,在 Grasshopper 里用
EPW/Sunpath组件搭建可视化参数流程。 - 与 Honeybee 配合:把 Ladybug 得到的气象/太阳数据传给 Honeybee 模型,再调用 EnergyPlus/Radiance 做精细模拟。
六、常见问题
Q:找不到 EPW 文件? A:EPW 是典型气象年数据,可从官方或公开气象数据源下载;确认路径正确且文件编码可解析。
Q:calculate_sun() 的时区参数怎么填?
A:用 epw.location.timezone,它来自气象文件;自定义地点需按当地时区填写(东八区为 8)。
Q:pip 安装后 Grasshopper 插件没变化? A:Python 包与 Grasshopper 插件是两套安装路径,插件需通过官方安装器安装并重启 Rhino。
Q:AGPL-3.0 对商业使用意味着什么? A:可商用,但若以网络服务形式提供修改版,需履行相应开源义务;请核对仓库 LICENSE 与法务意见。
Q:能直接做室内能耗模拟吗? A:Ladybug 本身偏环境与日照分析,完整能耗模拟请配合 Honeybee + EnergyPlus。
七、学习资源
- 文档:https://docs.ladybug.tools/
- Ladybug 核心 API:https://docs.ladybug.tools/ladybug/
- Grasshopper 插件说明:https://docs.ladybug.tools/ladybug-grasshopper/
- 仓库:https://github.com/ladybug-tools/ladybug