如何保存为每日文件而不是每月(Python、ERA5、ECMWF)

2024-10-01 15:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我目前使用API检索数据的脚本,目标是“每月”文件。我需要缩小一个文件的大小,以腾出时间准备和传输数据。主要通过尝试在日常文件中定位/写入,例如…..20151001.grib。在

由于我是Python的初学者,我在这里发帖希望有人愿意帮助我改进这个脚本,主要考虑目标/编写文件。在

谢谢你! /约瑟芬

#!/usr/bin/env python
import calendar
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()

def retrieve_era5():
"""
    A function to demonstrate how to iterate efficiently over several years and months etc
    for a particular era5 request.
    Change the variables below to adapt the iteration to your needs.
    You can use the variable 'target' to organise the requested data in files as you wish.
    In the example below the data are organised in files per month. (eg "era5_daily_201510.grb")
"""
yearStart = 2010
yearEnd = 2017
monthStart = 1
monthEnd = 12
for year in list(range(yearStart, yearEnd + 1)):
    for month in list(range(monthStart, monthEnd + 1)):
        startDate = '%04d%02d%02d' % (year, month, 1)
        numberOfDays = calendar.monthrange(year, month)[1]
        lastDate = '%04d%02d%02d' % (year, month, numberOfDays)
        target = "/Volumes/JOSEFINE/ERA5/era5_sfc_6hr_%04d%02d%.grb" % (year, month)
        requestDates = (startDate + "/TO/" + lastDate)
        era5_request(requestDates, target)

def era5_request(requestDates, target):
"""
    An ERA5 request for analysis pressure level data.
    Change the keywords below to adapt it to your needs.
    (eg to add or to remove  levels, parameters, times etc)
"""
server.retrieve({
                "class": "ea",                                  # do not change
                "dataset": "era5",                              # do not change
                "expver": "1",                                  # do not change
                "stream": "oper",                               # do not change
                "type": "an",                                   # analysis (versus forecast, fc)
                "date": requestDates,                           # dates, set automatically from above
                "levtype": "sfc",                               # pressure level data (versus surface, sfc, and model level, ml)
                "param": "134.128/136.128/137.128/165.128/166.128/232.128/246.228/247.228",     # see http://apps.ecmwf.int/codes/grib/param-db
                "target": target,                               # output file name, set automatically from above
                "time": "00:00/06:00/12:00/18:00",                          # times of analysis (with type:an), or initialization time of forecast (with type:fc)
                "grid": "0.25/0.25",                            # Optional for GRIB, required for NetCDF. The horizontal resolution in decimal degrees. If not set, the archived grid as specified in the data documentation is used.
                "area": "90/-40/-30/120",                       # Optional. Subset (clip) to an area. Specify as N/W/S/E in Geographic lat/long degrees. Southern latitudes and western longitudes must be
                "RESOL" : "AV",
               })
if __name__ == '__main__':
    retrieve_era5()

Tags: 文件thetointargetfordatarequest

热门问题