如何保存为每日文件而不是每月(Python,ERA-5,ECMWF)

时间:2018-03-07 16:07:04

标签: python python-requests target data-retrieval

这是我目前用于使用API​​检索数据的脚本,定位"每月"文件。我需要减少单个文件的大小,以便为数据准备和传输腾出时间。主要是通过尝试定位/写入日常文件,例如。 .... 20151001.grib。

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

谢谢! / Josefine

#!/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()

0 个答案:

没有答案