Accessing the batch service with Python

Installing the Python library

You can install the ecmwfapi python library by running:

sudo pip install https://software.ecmwf.int/wiki/download/attachments/23694554/ecmwf-api-python-client.tgz

If you cannot run the sudo command, just download the ecmwf-api-python-client.tgz, extract its content and copy the module ecmwfapi to a directory pointed by the environment variable PYTHONPATH.

Installing 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. Copy the information in this page and paste it in the file $HOME/.ecmwfapirc.

{
    "url"   : "https://api.ecmwf.int/v1",
    "key"   : "XXXXXXXXXXXXXXXXXXXXXX",
    "email" : "john.smith@example.com"
}

Sample Python script

Below is a simple Python script making use of the ecmwfapi library:

#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer

server = ECMWFDataServer()

server.retrieve({
    'dataset' : "tigge",
    'step'    : "24/to/120/by/24",
    'number'  : "all",
    'levtype' : "sl",
    'date'    : "20071001/to/20071003",
    'time'    : "00/12",
    'origin'  : "all",
    'type'    : "pf",
    'param'   : "tp",
    'area'    : "70/-130/30/-60",
    'grid'    : "2/2",
    'target'  : "data.grib"
    })

The "dataset" parameter is one of:

<script>
function build_dataset_table(table) 
{
	table.addClass("confluenceTable").addClass("tablesorter");
	 var tr = $("<tr>").addClass("sortableHeader").appendTo(table);
	var th = $('<th class="confluenceTh sortableHeader" data-column="0">').appendTo(tr);
	th.text("Loading table...");

	$.ajax({url:"https://api.ecmwf.int/v1/datasets?key=key1", dataType : 'jsonp', success : function(data) {

	var th = $('<th class="confluenceTh sortableHeader" data-column="2">').appendTo(tr);
	th.text("Description");

	var th = $('<th class="confluenceTh sortableHeader" data-column="1">').appendTo(tr);
	th.text("Licence");

	$.each(data.datasets,function (i, d) {
 var tr = $("<tr>").appendTo(table);
 var td = $('<td class="confluenceTd">').appendTo(tr);
td.text(d.name);

var td = $('<td class="confluenceTd">').appendTo(tr);
td.text(d.description ? d.description : "N/A");

var td = $('<td class="confluenceTd">').appendTo(tr);

if(d.licence) {
	var a = $("a")
	a.appendTo(td);
a.attr("href",d.licence.url);
a.text(d.licence.name);
 }
else
{
	td.text("N/A");
}

 });
th.text("Dataset");
 }, error: function() {

th.text("Failed to load table");

}});

   }
 </script>
<div class="table-wrap">
<table id="datasets_table"></table>
</div>
 <script>
build_dataset_table($("#datasets_table"));
</script>


To access these dataset, you need to agree on the  the corresponding terms and conditions that can be found at http://apps.ecmwf.int/datasets/

The other parameters are described at: http://www.ecmwf.int/publications/manuals/mars/guide/index.html