#!/usr/bin/env python
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This script downloads the zonal and meridional 10m height wind components
# from the ERA-interim reanalysis on the original reduced gaussian grid which
# was used to perform ERA-interim.
#
# If you use this script for the first time, you need to install you API key
# as described here : https://software.ecmwf.int/wiki/display/WEBAPI/Accessing+ECMWF+data+servers+in+batch
#
# History : Virginie Guemas - Initial version                     - May 2014 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#                                Arguments
#
# date1 - YYYYMMDD : First date to be downloaded
# date2 - YYYYMMDD : Last date to downloaded
#
# Example : ./download_u10v10.py 20140101 20140228
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import sys
date1=sys.argv[1]
date2=sys.argv[2]

from ecmwfapi import ECMWFDataServer
  
server = ECMWFDataServer()
  
server.retrieve({
  'dataset' : "interim",
  'stream'  : "oper",
  'step'    : "0",
  'levtype' : "sfc",
  'class'   : "ei",
  'date'    : date1+"/to/"+date2,
  'time'    : "00:00:00/06:00:00/12:00:00/18:00:00",
  'type'    : "an",
  'param'   : "165.128/166.128",
  'gaussian': "reduced",
  'grid'    : "128",
  'target'  : "output.grib"
  })
