Wednesday, 14 January 2015

Download ERA-Interim data

ERA-Interim dataset (January 1979 to present).
http://www.ecmwf.int/en/research/climate-reanalysis/era-interim

Global atmospheric and surface parameters from 1 January 1979 to present, at T255 spectral resolution (~80 km) on 60 vertical levels. 6-hourly atmospheric fields.

Retrieve data using batch scripts:
https://software.ecmwf.int/wiki/display/WEBAPI/Accessing+ECMWF+data+servers+in+batch

To do this on gaia:

1) Create directory for ECMWF retrieval and change into that directory.
/gaia/home/sweeneyc/Code/ECMWF

2) Download ecmwf-api-client-python.tgz
wget https://software.ecmwf.int/wiki/download/attachments/23694554/ecmwf-api-client-python.tgz?version=4&modificationDate=1394123817025&api=v2

3) Unpack ecmwf-api-client-python.tgz
tar -xzf ecmwf-api-client-python.tgz

4) set environment variable to point to that folder.
PYTHONPATH=/gaia/home/sweeneyc/Code/ECMWF

5) Install your API key.
To access ECMWF you will need an  API key. For that you first need to login at https://apps.ecmwf.int/auth/login/
and then retrieve you key at
https://api.ecmwf.int/v1/key/
For this, you will need to have an account on ECMWF web site. If you don't have an account, please self register at
https://apps.ecmwf.int/registration/

Copy the information in this page and paste it in the file $HOME/.ecmwfapirc

6) Agree with conditions.
To access these dataset, you need to agree on the corresponding terms and conditions that can be found under the "Licence" link in the table on the using batch access web page. For ERA-Interim, it's this link:
http://apps.ecmwf.int/datasets/licences/general

7) Use web page to generate MARS request.
Test by getting MSLP. Surface level data are here:
http://apps.ecmwf.int/datasets/data/interim_full_daily/?levtype=sfc

retrieve,
class=ei,
dataset=interim,
date=1979-01-01/to/1979-01-31,
grid=0.75/0.75,
levtype=sfc,
param=151.128,
step=0,
stream=oper,
time=00/06/12/18,
type=an

8) Create python script.
Use the information you get from step 5 to create a file called GetERAI.py (or any name you like):

#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
server.retrieve({
'dataset' : "interim",
'step' : "0",
'levtype' : "sfc",
'date' : "19790101/to/19790131",
'time' : "00/06/12/18",
'type' : "an",
'param' : "151.128",
'area' : "80/-30/30/30",
'grid' : "0.75/0.75",
'target' : "data.grib"
})

9) Get the data!

Run the script:
python GetERAI.py

This will (hopefully) result in a file called data.grib in your directory. You can check the file:

module load WPS
g1print.exe data.grib | head

Copen: File = data.grib                                                                                                               
Fortran Unit = 0
UNIX File descriptor: 3

----------------------------------------------------
 rec GRIB GRIB  Lvl  Lvl  Lvl         Time      Fcst
 Num Code name  Code one  two                   hour
----------------------------------------------------
   1 151 MSL      1    0    0  1979-01-01_00:00 + 00
   2 151 MSL      1    0    0  1979-01-01_06:00 + 00

Check that the GRIB Code and Lvl numbers are the same as expected by Vtable.ERA-interim.ml

Here are the python files I used to download ERA-Interim data to drive WRF:

Invariant Fields:

GetERAI-sfc.py (only need to retrieve these once)
#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
server.retrieve({
'class' : "ei",
'dataset' : "interim",
'step' : "0",
'levtype' : "sfc",
'date' : "19890101",
'time' : "12",
'type' : "an",
'param' : "129.128/172.128",
'area' : "80/-30/30/20",
'grid' : "0.75/0.75",
'stream' : "oper",
'RESOL' : "AV",
'target' : "data-invariant.grb"
})

Surface Fields:

GetERAI-sfc.py
#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
server.retrieve({
'class' : "ei",
'dataset' : "interim",
'step' : "0",
'levtype' : "sfc",
'date' : "19790101/to/19790131",
'time' : "00/06/12/18",
'type' : "an",
'param' : "134.128/139.128/141.128/151.128/165.128/166.128/167.128/168.128/170.128/183.128/235.128/236.128/31.128/33.128/34.128/39.128/40.128/41.128/42.128",
'area' : "80/-30/30/20",
'grid' : "0.75/0.75",
'stream' : "oper",
'RESOL' : "AV",
'target' : "data.grib"
})

Model Level Fields:

GetERAI-ml.py
#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
server.retrieve({
'class' : "ei",
'dataset' : "interim",
'step' : "0",
'levtype' : "ml",
'levelist' : "1/to/60",
'date' : "19790101/to/19790131",
'time' : "00/06/12/18",
'type' : "an",
'param' : "130.128/131.128/132.128/133.128",
'area' : "80/-30/30/20",
'grid' : "0.75/0.75",
'stream' : "oper",
'RESOL' : "AV",
'target' : "data.grib"
})

Data retrieval can be automated for different dates by using a bash script like this one:

GetERAI.bash

#!/bin/bash -l

CODEDIR=/gaia/home/sweeneyc/Code/ECMWF
DATADIR=/gaia/home/sweeneyc/DATA/ERAI

PYTHONPATH=/gaia/home/sweeneyc/Code/ECMWF

cd $CODEDIR

DATE1=19790101
DATE2=19790103

YY1=`echo $DATE1 | cut -c1-4`
MM1=`echo $DATE1 | cut -c5-6`
DD1=`echo $DATE1 | cut -c7-8`

YY2=`echo $DATE2 | cut -c1-4`
MM2=`echo $DATE2 | cut -c5-6`
DD2=`echo $DATE2 | cut -c7-8`

sed -e s/NDATE1N/${DATE1}/g -e s/NDATE2N/${DATE2}/g GetERAI-sfc.py > GetERAI-${DATE1}-${DATE2}-sfc.py

python GetERAI-${DATE1}-${DATE2}-sfc.py

sed -e s/NDATE1N/${DATE1}/g -e s/NDATE2N/${DATE2}/g GetERAI-ml.py > GetERAI-${DATE1}-${DATE2}-ml.py

python GetERAI-${DATE1}-${DATE2}-ml.py

mkdir -p ${DATADIR}/$YY1

mv ERAI-${DATE1}-${DATE2}-sfc.grb ERAI-${DATE1}-${DATE2}-ml.grb ${DATADIR}/$YY1/

exit 0
----