meteostat,站点数据获取竟如此简单
个人信息
公众号:气python风雨
关注我获取更多学习资料,第一时间收到我的Python学习资料,也可获取我的联系方式沟通合作
温馨提示
由于可视化代码过长隐藏,可点击运行Fork查看
若没有成功加载可视化图,点击运行可以查看
ps:隐藏代码在【代码已被隐藏】所在行,点击所在行,可以看到该行的最右角,会出现个三角形,点击查看即可
前言
在当今数据驱动的世界中,气象数据对于众多领域如农业、能源、交通、环境科学等都至关重要。无论是进行科学研究还是商业决策,准确且易于获取的气象数据都是不可或缺的。然而,收集和处理这些数据往往是一个复杂且耗时的过程。幸运的是,Python 社区提供了许多强大的工具来简化这一过程,其中 meteostat 库就是一个非常实用的选择。
meteostat 是一个开源的 Python 库,旨在提供简单而高效的接口来访问历史和实时气象数据。它支持全球数以万计的气象站,包括月度、每日和每小时的数据记录。通过 meteostat,开发者和研究人员可以轻松地下载所需的数据,无需担心复杂的 API 调用或数据格式转换问题。
数据源包括国家气象局,如美国国家海洋和大气管理局 (NOAA) 和德国国家气象局 (DWD)。
本文将详细介绍如何使用 meteostat 库来下载气象站点的月度、每日和每小时数据,并展示一些实际应用案例。无论你是初学者还是有经验的开发者,都能从本文中获得有价值的信息,从而更高效地利用气象数据进行分析和研究。
安装库
!pip install meteostat -i https://pypi.mirrors.ustc.edu.cn/simple/
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple/ Collecting meteostat Downloading https://mirrors.ustc.edu.cn/pypi/packages/90/23/34e8913fa75d064be09303914ea0056ef2968b639ebc8e70decfecd8299b/meteostat-1.6.8-py3-none-any.whl (31 kB) Requirement already satisfied: pytz in /opt/conda/lib/python3.9/site-packages (from meteostat) (2022.1) Requirement already satisfied: numpy in /opt/conda/lib/python3.9/site-packages (from meteostat) (1.26.4) Requirement already satisfied: pandas>=1.1 in /opt/conda/lib/python3.9/site-packages (from meteostat) (2.0.3) Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.9/site-packages (from pandas>=1.1->meteostat) (2.8.2) Requirement already satisfied: tzdata>=2022.1 in /opt/conda/lib/python3.9/site-packages (from pandas>=1.1->meteostat) (2024.1) Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas>=1.1->meteostat) (1.16.0) Installing collected packages: meteostat Successfully installed meteostat-1.6.8
查询某地站点数量
下面我们查询一下该库能访问的天津的气象站数量
from meteostat import Stations
stations = Stations()
stations = stations.region('CN','TJ')
print('Stations in Ontario:', stations.count())
Stations in Ontario: 2
根据经纬度查询临近站点信息
# Import Meteostat library
from meteostat import Stations
# Get nearby weather stations
stations = Stations()
stations = stations.nearby(39, 118)
station = stations.fetch(1)
# Print DataFrame
print(station)
name country region wmo \ id ZBTJ0 Fanzhuangzi / Tianjin / Tian-jun / T'ien-chin ... CN TJ <NA> icao latitude longitude elevation timezone hourly_start \ id ZBTJ0 ZBTJ 39.1833 117.35 3.0 Asia/Shanghai 1981-11-25 hourly_end daily_start daily_end monthly_start monthly_end \ id ZBTJ0 2024-10-26 1996-03-07 2022-04-25 2005-01-01 2022-01-01 distance id ZBTJ0 59684.566333
以上可以看出各种时间分辨率的数据时间范围不尽相同
小时数据是最全的
下载月数据
# Import Meteostat library and dependencies
from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Stations, Monthly
# Set time period
start = datetime(2000, 1, 1)
end = datetime(2018, 12, 31)
# Get Monthly data
data = Monthly('ZBTJ0', start, end)
data = data.fetch()
# Plot line chart including average, minimum and maximum temperature
data.plot(y=['tavg', 'tmin', 'tmax'])
plt.show()
UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.
下载日数据
# Import Meteostat library and dependencies
from datetime import datetime
import matplotlib.pyplot as plt
from meteostat import Point, Daily
# Set time period
start = datetime(2022, 1, 1)
end = datetime(2022, 12, 31)
# Create Point for Vancouver, BC
location = Point(39.183 , 117.35 , 3.0)
# Get daily data for 2018
data = Daily(location, start, end)
data = data.fetch()
# Plot line chart including average, minimum and maximum temperature
data.plot(y=['tavg', 'tmin', 'tmax'])
plt.show()
下载小时数据
# Import Meteostat library and dependencies
from datetime import datetime
from meteostat import Hourly
# Set time period
start = datetime(2018, 1, 1)
end = datetime(2018, 12, 31, 23, 59)
# Get hourly data
data = Hourly('ZBTJ0', start, end)
data = data.fetch()
# Print DataFrame
print(data)
temp dwpt rhum prcp snow wdir wspd wpgt pres \ time 2018-01-01 00:00:00 -6.0 -10.1 73.0 NaN NaN 340.0 7.2 NaN NaN 2018-01-01 01:00:00 -4.0 -6.9 80.0 NaN NaN 10.0 7.2 NaN NaN 2018-01-01 02:00:00 -1.0 -7.0 64.0 NaN NaN 70.0 10.8 NaN NaN 2018-01-01 03:00:00 1.0 -7.0 55.0 NaN NaN 80.0 18.0 NaN NaN 2018-01-01 04:00:00 2.0 -6.1 55.0 NaN NaN 90.0 18.0 NaN NaN ... ... ... ... ... ... ... ... ... ... 2018-12-31 19:00:00 -9.0 -14.0 67.0 NaN NaN NaN 0.0 NaN NaN 2018-12-31 20:00:00 -9.0 -15.0 62.0 NaN NaN 330.0 3.6 NaN NaN 2018-12-31 21:00:00 -9.0 -15.0 62.0 NaN NaN 330.0 3.6 NaN NaN 2018-12-31 22:00:00 -10.0 -13.9 73.0 NaN NaN NaN 0.0 NaN NaN 2018-12-31 23:00:00 -11.0 -13.9 79.0 NaN NaN 260.0 3.6 NaN NaN tsun coco time 2018-01-01 00:00:00 NaN NaN 2018-01-01 01:00:00 NaN NaN 2018-01-01 02:00:00 NaN NaN 2018-01-01 03:00:00 NaN NaN 2018-01-01 04:00:00 NaN NaN ... ... ... 2018-12-31 19:00:00 NaN NaN 2018-12-31 20:00:00 NaN NaN 2018-12-31 21:00:00 NaN NaN 2018-12-31 22:00:00 NaN NaN 2018-12-31 23:00:00 NaN NaN [8701 rows x 11 columns]
小结
通过本文的介绍,我们已经了解了如何使用 meteostat 库来下载气象站点的月度、每日和每小时数据。meteostat 提供了一个简洁而直观的 API,使得即使是非专业人员也能快速上手并获取高质量的气象数据。我们探讨了如何安装库、查询可用的气象站、选择合适的时间范围以及下载不同类型的数据。
此外,我们还展示了几个实际的应用案例,通过这些示例,我们可以看到 meteostat 不仅简化了数据获取的过程,还极大地提高了数据分析的效率和准确性。