气ython风雨 发表于 2024-5-4 22:41:32

数据获取 | 基于openmeteo 下载最新EC aifs预报数据


01 项目简介
本项目旨在利用 OpenMeteo 平台提供的最新 EC AIFS 预报数据,进行气象数据分析和可视化。我们将使用 Python 编程语言和相关的气象数据处理工具来实现这一目标。

02 项目背景
气象预报数据对于天气预测、气候研究以及环境监测等方面具有重要意义。EC AIFS 预报数据是欧洲中期天气预报中心(ECMWF)的大气初始条件场预报系统(AIFS)提供的气象预报数据,包含了全球范围内的多种气象要素预报,如温度、湿度、风速等。

03 项目目标
1、下载并获取最新的 EC AIFS 预报数据。

2、实现循环多格点下载

3、对下载的预报表格数据进行处理,转为更加适合处理的xarray格式

04 结束语
通过本项目,我们希望能够深入探索气象数据的价值,并为气象爱好者、科研人员以及气象行业工作者提供有益的工具和资源。

更多数据下载可从 OPEN-METEO API (https://open-meteo.com/en) 下载气象数据

05 温馨提示
由于部分代码过长隐藏,可前往

基于openmeteo 下载最新EC aifs预报数据 - Heywhale.com

点击运行Fork查看基于openmeteo 下载最新EC aifs预报数据🔜🔜若没有成功加载可视化图,点击运行可以查看
ps:隐藏代码在【代码已被隐藏】所在行,点击所在行,可以看到该行的最右角,会出现个三角形,点击查看即可

06 安装库
In :
!pip install openmeteo-requests
!pip install requests-cache retry-requests numpy pandas
07 单格点温度数据下载
In :
import openmeteo_requests

import requests_cache
import pandas as pd
from retry_requests import retry

# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
openmeteo = openmeteo_requests.Client(session = retry_session)

# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to assign them correctly below
url = "https://api.open-meteo.com/v1/ecmwf"
params = {
"latitude": 52.52,
"longitude": 13.41,
"hourly": "temperature_2m",
"models": "ecmwf_aifs025"
}
responses = openmeteo.weather_api(url, params=params)

# Process first location. Add a for-loop for multiple locations or weather models
response = responses
print(f"Coordinates {response.Latitude()}°N {response.Longitude()}°E")
print(f"Elevation {response.Elevation()} m asl")
print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")
print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")

# Process hourly data. The order of variables needs to be the same as requested.
hourly = response.Hourly()
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()

hourly_data = {"date": pd.date_range(
start = pd.to_datetime(hourly.Time(), unit = "s", utc = True),
end = pd.to_datetime(hourly.TimeEnd(), unit = "s", utc = True),
freq = pd.Timedelta(seconds = hourly.Interval()),
inclusive = "left"
)}
hourly_data["temperature_2m"] = hourly_temperature_2m

hourly_dataframe = pd.DataFrame(data = hourly_data)
print(hourly_dataframe)Coordinates 52.41217803955078°N 13.333333015441895°E
Elevation 38.0 m asl
Timezone None None
Timezone difference to GMT+0 0 s
                         datetemperature_2m
0   2024-03-01 00:00:00+00:00            4.20
1   2024-03-01 01:00:00+00:00            3.80
2   2024-03-01 02:00:00+00:00            3.40
3   2024-03-01 03:00:00+00:00            3.05
4   2024-03-01 04:00:00+00:00            2.80
..                        ...             ...
235 2024-03-10 19:00:00+00:00            7.45
236 2024-03-10 20:00:00+00:00            6.90
237 2024-03-10 21:00:00+00:00            6.20
238 2024-03-10 22:00:00+00:00            5.55
239 2024-03-10 23:00:00+00:00            5.00




08 循环下载多格点
In :
import openmeteo_requests
import requests_cache
import pandas as pd
from retry_requests import retry
import numpy as np

# 设置Open-Meteo API客户端并进行缓存和错误重试
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
openmeteo = openmeteo_requests.Client(session=retry_session)

# 生成要下载数据的格点经纬度范围
latitude_points = np.arange(52.0, 53.1, 0.25)
longitude_points = np.arange(13.0, 14.1, 0.25)

# 定义要查询的变量和模型
variables = ["temperature_2m"]
model = "ecmwf_aifs025"

# 创建一个空列表,用于存储每个格点的数据
dataframes = []

# 循环遍历每个格点并下载数据
for lat in latitude_points:
    for lon in longitude_points:
      url = "https://api.open-meteo.com/v1/ecmwf"
      params = {
            "latitude": lat,
            "longitude": lon,
            "hourly": ",".join(variables),
            "models": model
      }
      responses = openmeteo.weather_api(url, params=params)

      # 处理响应数据
      for response in responses:
            hourly = response.Hourly()
            hourly_data = {
                "date": pd.date_range(
                  start=pd.to_datetime(hourly.Time(), unit="s", utc=True),
                  end=pd.to_datetime(hourly.TimeEnd(), unit="s", utc=True),
                  freq=pd.Timedelta(seconds=hourly.Interval()),
                  inclusive="left"
                )
            }

            for i, variable in enumerate(variables):
                variable_data = hourly.Variables(i).ValuesAsNumpy()
                hourly_data = variable_data

            hourly_dataframe = pd.DataFrame(data=hourly_data)
            dataframes.append(hourly_dataframe)

# 将所有数据拼接成一个表格
final_dataframe = pd.concat(dataframes, ignore_index=True)

# 打印最终的数据表格
print(final_dataframe)
                        datetemperature_2m
0    2024-03-01 00:00:00+00:00            3.05
1    2024-03-01 01:00:00+00:00            2.60
2    2024-03-01 02:00:00+00:00            2.15
3    2024-03-01 03:00:00+00:00            1.75
4    2024-03-01 04:00:00+00:00            1.50
...                        ...             ...
5995 2024-03-10 19:00:00+00:00            7.05
5996 2024-03-10 20:00:00+00:00            6.45
5997 2024-03-10 21:00:00+00:00            5.80
5998 2024-03-10 22:00:00+00:00            5.20
5999 2024-03-10 23:00:00+00:00            4.65




09 增加经纬度
final_dataframOut:


010 转为xarray格式并简单可视化
<xarray.Dataset>
Dimensions:         (date: 240, latitude: 5, longitude: 5)
Coordinates:
* date            (date) datetime64 2024-03-01 ... 2024-03-10T23:00:00
* latitude      (latitude) float64 52.0 52.25 52.5 52.75 53.0
* longitude       (longitude) float64 13.0 13.25 13.5 13.75 14.0
Data variables:
    temperature_2m(date, latitude, longitude) float32 3.05 3.45 ... 4.35 4.65
In :
ds.temperature_2m.plot()

Out:

<matplotlib.collections.QuadMesh at 0x7f0ae4061b80>




文章来源于公众号:气python风雨
页: [1]
查看完整版本: 数据获取 | 基于openmeteo 下载最新EC aifs预报数据