Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info
for hindcastYear in hindcastYears
for hindcastMonth in hindcastMonths
for hindcastDay in hindcastDays
hindcastDate = HindcastYearhindcastYear-hindcastMonth-hindcastDay
S2S-request(hindcastDate)

...

Info
  • The objective of this example is to demonstrate how to iterate efficiently over all the available hindcastYears, hindcastMonths and hindcastDays for a CMA reforecast request
  • It can be used as a starting point, however you need to keep in mind that you have to adapt it to your needseg to set the keyword values according to your requirements ("param", "levtype", "step" etc).
  • In this way you can extend this request to download the whole S2S CMA reforecast. Don't forget to check CMA availability (warning)

(warning) Please note:

  • set the variable "target"  to write each hindcastDate on a separate file .
  • taking under consideration your request's size (eg Nr of fields and volume)  you can merge several hindcastDates hindcastDates on the same "target" (smile)
Code Block
languagepy
#!/usr/bin/env python
import calendar
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()

origin = "babj"
modelVersionDate = "2014-05-01"

def retrieve_cma_reforecast():
    """       
       A function to demonstrate how to iterate efficiently over all hindcastYears, hindcastMonths etc     
       for a particular cma_reforecast_request.      
       Change the variables below to adapt the iteration to your needs 
    """
    hindcastYearStart = 1994
    hindcastYearEnd = 2014
    hindcastMonthStart = 1
    hindcastMonthEnd = 12
    
    for hindcastYear in list(range(hindcastYearStart, hindcastYearEnd + 1)):
        for hindcastMonth in list(range(hindcastMonthStart, hindcastMonthEnd + 1)):
            numberOfDays = calendar.monthrange(hindcastYear, hindcastMonth)[1]
            for hindcastDay in list(range(numberOfDays)):
                hindcastDate = '%04d%02d%02d' % (
                    hindcastYear, hindcastMonth, hindcastDay + 1)
                target = "%s%s.grb" % (origin, hindcastDate)
                cma_reforecast_request(hindcastDate, target)

def cma_reforecast_request(hindcastDate, target):
    """       
        A CMA reforecast request. Change the keywords below to adapt it to your needs.   
    """
    
    server.retrieve({
        "class": "s2",
        "dataset": "s2s",
        "date": modelVersionDate,
        "expver": "prod",
        "hdate": hindcastDate,
        "levtype": "sfc",
        "origin": origin,
        "param": "165/166",
        "step": "24/to/1440/by/24",
        "stream": "enfh",
        "target": target,
        "time": "00",
        "number": "1/2/3",
        "type": "pf",
    })

if __name__ == '__main__':
    retrieve_cma_reforecast()


Useful links

Info