From 9e8e06f3d29725f1a882870fb3bc9ac35a7bef59 Mon Sep 17 00:00:00 2001
From: "Daniel.Frisinghelli" <daniel.frisinghelli@eurac.edu>
Date: Tue, 22 Jun 2021 11:43:03 +0200
Subject: [PATCH] Download ERA5 reanalyis data.

---
 climax/main/download_ERA5.py | 59 ++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 climax/main/download_ERA5.py

diff --git a/climax/main/download_ERA5.py b/climax/main/download_ERA5.py
new file mode 100644
index 0000000..e1d2e14
--- /dev/null
+++ b/climax/main/download_ERA5.py
@@ -0,0 +1,59 @@
+"""Download ERA-5 data from the Copernicus Climate Data Store."""
+
+# !/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# builtins
+import os
+import pathlib
+from joblib import Parallel, delayed
+
+# externals
+import cdsapi
+import numpy as np
+
+# ERA-5 product
+product = 'reanalysis-era5-pressure-levels'
+product_type = 'reanalysis'
+
+# pressure levels
+pressure_levels = ['850', '500']
+
+# variables
+variables = ['geopotential', 'temperature', 'u_component_of_wind',
+             'v_component_of_wind', 'specific_humidity']
+
+# time period
+years = [str(y) for y in np.arange(1981, 2011)]
+month = [str(m) for m in np.arange(1, 13)]
+days = [str(d) for d in np.arange(1, 31)]
+
+# area of interest (Europe): North, West, South, East
+area = [52, 2, 40, 20]
+
+# ERA5 download configuration dictionary
+CONFIG = {
+    'product_type': product_type,
+    'variable': variables,
+    'pressure_level': pressure_levels,
+    'year': years,
+    'month': month,
+    'day': days,
+    'format': 'netcdf',
+    'area': area
+}
+
+# output path
+target = pathlib.Path('/mnt/CEPH_PROJECTS/FACT_CLIMAX/REANALYSIS/ERA5/')
+
+
+if __name__ == '__main__':
+
+    # initialize client
+    c = cdsapi.Client()
+
+    # download data for the different variables
+    Parallel(n_jobs=min(len(variables), os.cpu_count()), verbose=51)(
+        delayed(c.retrieve)(product, CONFIG.update({'variable': var}), str(
+            target.joinpath('_'.join(['ERA5', var, years[0], years[-1]])
+                            + '.nc'))) for var in variables)
-- 
GitLab