如何获取ERA-Interim Reanalysis日降雨量数据

ERA-Interim数据的API下载请参考:https://blog.csdn.net/weixin_42924891/article/details/85225062

import linecache
from ecmwfapi import ECMWFDataServer
a = [31,28,31,30,31,30,31,31,30,31,30,31]
for i in range(2018,2019):#年份
    if i%4==0: # 判断闰年
        m=29
    else:
        m=28
    for j in range(1,13):#月份1到12月
        if j==2:
            b = m
        else:
            b = a[j-1]
        if j<10:
            y1 = "0"+str(j)
        else:
            y1 = str(j)
        date = str(i)+"-"+y1+"-01/to/"+str(i)+"-"+y1+"-"+str(b) # 构造日期字符串
        print(date)
        server = ECMWFDataServer()
        try:
            server.retrieve({
            "class": "ei",
            "dataset": "interim",
            "area" : "26/99/24/100", # 下载区域
            "date": date,
            "expver": "1",
            "grid": "0.25/0.25", # 原始数据0.75°×0.75°,插值到了2.5°-0.125°此处选择0.25°×0.25°
            "levtype": "sfc",
            "param": "228.128", # Total precipitation的参数编号228
            "step": "12", # 设置步长(step)
            "stream": "oper",
            "time": "00:00:00/12:00:00", #设置起始时间点 (time)
            "type": "fc", # 累积降雨是预报数据
            "format": "netcdf",        # 数据格式
            "target": "D:/down/"+str(i)+y1+".nc"  #输出位置(左斜线)及文件名
            })
        except Exception as e:
            print(e)
print("all have done!")

此处注意,下载的数据为time-time+step这个时间段的累计降雨量,0:00-12:00和12:00-24:00降雨量。刚好是一天的降雨,下面两个时段的降雨量进行求和。涉及到多维数组直接转化为Dataframe数据结构。

# 导入模块
from netCDF4 import Dataset

import itertools
import pandas as pd

from datetime import datetime 
from datetime import timedelta

df = None
for i in ['20180%d'%i if i<10 else '2018%d'%i for i in range(1,13)]:
    # 获取数据集中所有变量名
    filename = r'D:\down\%s.nc'%i
    data = Dataset(filename)
#     variables_name = list(data.variables.keys())
#     print(variables_name)
    # 注意此处的减去3没有具体含义,为了让数据重采样落入同一天便于统计
    time = [datetime(1900, 1, 1,0,0,0) + timedelta(hours=int(i)-3) for i in data['time'][:]] 
    latitude = data['latitude'][:]
    longitude = data['longitude'][:]
    # 涉及到dataframe的MultiIndex
    data = pd.DataFrame(data['tp'][:].flatten(),index=pd.MultiIndex.from_product([time,latitude,longitude]))
    data = data.mean(level=[0]).resample('D').sum() # 整个45个经纬度点直接求均值,然后日内重采样求和,此处经纬度可以根据需要采用更为精细化的插值方式
    data = data
    if df is None :
        df = data
    else:
        df = pd.concat([df,data],axis=0)

df2 = df*1e3 # 原单位是metre 化为mm

 

你可能感兴趣的:(python,ECMWF,气象数据)