Access and Process ESGF CORDEX 22km and CMIP6 datasets in python notebook USING PYESGF API¶
A bit of story !!! :)
Regional Climate Models (RCM) and Empirical Statistical Downscaling (ESD), applied over a limited area and driven by Global Climate Models (GCMs) can provide information on much smaller scales supporting more detailed impact and adaptation assessment and planning, which is vital in many vulnerable regions of the world. Hence CORDEX (Coordinated Regional Climate Downscaling Experiment) which downsclaed GCM Model from CMIP to provide high resolution climate data for climate impact assessment.Get more information on CORDEX project with this link.
CORDEX datasets are stored on Earth System Grid Federation system (ESGF). ESGF maintains a global system of federated data centers that allow access to the largest archive of model climate data world-wide
This post aims to show how to quickly access a particular file out of the hundred of thousand files available on each ESGF repository portals. Which might be troublesome if it is your first time.
About PYESGFI API¶
The main idea of this post is to use an API designed for searching files on ESGF named ESGF PyClient. ESGF PyClient is a Python package designed for interacting with the Earth System Grid Federation system (ESGF). Currently this package contains API code for calling the ESGF Search API within client code. A set of constraints facets (search keywords) are made available and can make this exerice completely more friendly and less complicated.
Import packages¶
from pyesgf.search import SearchConnection
import os
import pandas as pd
import requests
from tqdm import tqdm
import xarray as xr
import netCDF4
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import seaborn as sns
import sys
LOGIN CREDENTIALS¶
There are many node available where you can download RCM or GCM data. A huge number of climatic projection datasets are available from this node : https://esgf-data.dkrz.de/projects/esgf-dkrz/. Sign up and login with your openid and passowrd.
from pyesgf.logon import LogonManager
lm = LogonManager()
lm.logoff()
lm.is_logged_on()
False
ESGF Logon¶
myproxy_host = 'esgf-data.dkrz.de'
lm.logon(username='xxxxxxxxxx', password='xxxxxxxxx', hostname=myproxy_host)
lm.is_logged_on()
True
CORDEX - ESGF data availability overview¶
Link to summary table of available datasets in ESGF
import webbrowser
cordex_esgf = 'https://' + 'is-enes-data.github.io/CORDEX_status.html'
webbrowser.open(cordex_esgf)
False
FRAMEWORK¶
We will retrieve future temperature data under RCP 4.5 from AFR-44& AFR-22 of CORDEX Experiment datasets.
SearchConnection¶
This instances represent a connection to an ESGF Search web service. This stores the service URL and also service-level parameters.¶
from pyesgf.search import SearchConnection
conn = SearchConnection('http://esgf-data.dkrz.de/esg-search/', distrib=True)
ctx_cordex= conn.new_context(
project='CORDEX',
product='output',
ensemble='r1i1p1',
experiment='rcp45',
domain='AFR-44,AFR-22',
institute = 'CCCMA',
driving_model='CCCma-CanESM2',
rcm_name = 'CanRCM4',
time_frequency='day',
variable='tas',
data_node ='data.meteo.unican.es',
rcm_version ='r2',
latest=True
)
Print the total number of hits available with current constraints¶
ctx_cordex.hit_count
2
Quick cross-check of search constraints results by displaying search instance id and date time length¶
results=ctx_cordex.search()
for i in range(ctx_cordex.hit_count):
print(results[i].json['instance_id'],' | Start :',results[i].json['datetime_start'],' | End :',results[i].json['datetime_stop'])
cordex.output.AFR-44.CCCma.CCCma-CanESM2.rcp45.r1i1p1.CanRCM4.r2.day.tas.v20210518 | Start : 2006-01-01T12:00:00Z | End : 2100-12-31T12:00:00Z cordex.output.AFR-22.CCCma.CCCma-CanESM2.rcp45.r1i1p1.CanRCM4.r2.day.tas.v20210518 | Start : 2006-01-01T12:00:00Z | End : 2100-12-31T12:00:00Z
Get first subset of future dataset of the instances¶
cordex44_link= results[0].file_context().search()[0].opendap_url
cordex22_link= results[1].file_context().search()[0].opendap_url
print(cordex44_link,'\n\n',cordex22_link)
http://data.meteo.unican.es/thredds/dodsC/esgcet/c3s34d/from_PROVIDERS/CORDEX/output/AFR-44/CCCma/CCCma-CanESM2/rcp45/r1i1p1/CCCma-CanRCM4/r2/day/tas/v20210518/tas_AFR-44_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231.nc http://data.meteo.unican.es/thredds/dodsC/esgcet/c3s34d/from_PROVIDERS/CORDEX/output/AFR-22/CCCma/CCCma-CanESM2/rcp45/r1i1p1/CCCma-CanRCM4/r2/day/tas/v20210518/tas_AFR-22_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231.nc
# import land mask
mask=xr.open_dataset('/mys3bucket/ERA_Temp/mask_new.nc')['region']
# you can get the region shapefile from this link :https://drive.google.com/file/d/1PqJv7UrdrziCumLmEA-gXOC4mgEHDEhm/view?usp=sharing
mask.plot()
<matplotlib.collections.QuadMesh at 0x7f498428c580>
mask
<xarray.DataArray 'region' (lat: 121, lon: 181)> array([[0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], ..., [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258]]) Coordinates: * lat (lat) float32 30.0 29.75 29.5 29.25 29.0 ... 1.0 0.75 0.5 0.25 0.0 * lon (lon) float32 -20.0 -19.75 -19.5 -19.25 ... 24.25 24.5 24.75 25.0
- lat: 121
- lon: 181
- 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.03226 0.03226 0.03226 0.03226 0.03226
array([[0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], ..., [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258], [0. , 0. , 0. , ..., 0.032258, 0.032258, 0.032258]])
- lat(lat)float3230.0 29.75 29.5 ... 0.5 0.25 0.0
array([30. , 29.75, 29.5 , 29.25, 29. , 28.75, 28.5 , 28.25, 28. , 27.75, 27.5 , 27.25, 27. , 26.75, 26.5 , 26.25, 26. , 25.75, 25.5 , 25.25, 25. , 24.75, 24.5 , 24.25, 24. , 23.75, 23.5 , 23.25, 23. , 22.75, 22.5 , 22.25, 22. , 21.75, 21.5 , 21.25, 21. , 20.75, 20.5 , 20.25, 20. , 19.75, 19.5 , 19.25, 19. , 18.75, 18.5 , 18.25, 18. , 17.75, 17.5 , 17.25, 17. , 16.75, 16.5 , 16.25, 16. , 15.75, 15.5 , 15.25, 15. , 14.75, 14.5 , 14.25, 14. , 13.75, 13.5 , 13.25, 13. , 12.75, 12.5 , 12.25, 12. , 11.75, 11.5 , 11.25, 11. , 10.75, 10.5 , 10.25, 10. , 9.75, 9.5 , 9.25, 9. , 8.75, 8.5 , 8.25, 8. , 7.75, 7.5 , 7.25, 7. , 6.75, 6.5 , 6.25, 6. , 5.75, 5.5 , 5.25, 5. , 4.75, 4.5 , 4.25, 4. , 3.75, 3.5 , 3.25, 3. , 2.75, 2.5 , 2.25, 2. , 1.75, 1.5 , 1.25, 1. , 0.75, 0.5 , 0.25, 0. ], dtype=float32)
- lon(lon)float32-20.0 -19.75 -19.5 ... 24.75 25.0
array([-20. , -19.75, -19.5 , -19.25, -19. , -18.75, -18.5 , -18.25, -18. , -17.75, -17.5 , -17.25, -17. , -16.75, -16.5 , -16.25, -16. , -15.75, -15.5 , -15.25, -15. , -14.75, -14.5 , -14.25, -14. , -13.75, -13.5 , -13.25, -13. , -12.75, -12.5 , -12.25, -12. , -11.75, -11.5 , -11.25, -11. , -10.75, -10.5 , -10.25, -10. , -9.75, -9.5 , -9.25, -9. , -8.75, -8.5 , -8.25, -8. , -7.75, -7.5 , -7.25, -7. , -6.75, -6.5 , -6.25, -6. , -5.75, -5.5 , -5.25, -5. , -4.75, -4.5 , -4.25, -4. , -3.75, -3.5 , -3.25, -3. , -2.75, -2.5 , -2.25, -2. , -1.75, -1.5 , -1.25, -1. , -0.75, -0.5 , -0.25, 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. , 2.25, 2.5 , 2.75, 3. , 3.25, 3.5 , 3.75, 4. , 4.25, 4.5 , 4.75, 5. , 5.25, 5.5 , 5.75, 6. , 6.25, 6.5 , 6.75, 7. , 7.25, 7.5 , 7.75, 8. , 8.25, 8.5 , 8.75, 9. , 9.25, 9.5 , 9.75, 10. , 10.25, 10.5 , 10.75, 11. , 11.25, 11.5 , 11.75, 12. , 12.25, 12.5 , 12.75, 13. , 13.25, 13.5 , 13.75, 14. , 14.25, 14.5 , 14.75, 15. , 15.25, 15.5 , 15.75, 16. , 16.25, 16.5 , 16.75, 17. , 17.25, 17.5 , 17.75, 18. , 18.25, 18.5 , 18.75, 19. , 19.25, 19.5 , 19.75, 20. , 20.25, 20.5 , 20.75, 21. , 21.25, 21.5 , 21.75, 22. , 22.25, 22.5 , 22.75, 23. , 23.25, 23.5 , 23.75, 24. , 24.25, 24.5 , 24.75, 25. ], dtype=float32)
Interpolating, Resampling, Convertion and Cropping over West Africa region¶
# GET DATA ACCESS , it may takes time depending of your internet band width
cordex_44_df= xr.open_dataset(cordex44_link)
cordex_22_df= xr.open_dataset(cordex22_link)
cordex_22_df,cordex_44_df
(<xarray.Dataset> Dimensions: (bnds: 2, rlat: 402, rlon: 388, time: 1825) Coordinates: * time (time) object 2006-01-01 12:00:00 ... 2010-12-31 12:00:00 * rlon (rlon) float64 -24.75 -24.53 -24.31 ... 59.95 60.17 60.39 * rlat (rlat) float64 -45.87 -45.65 -45.43 ... 41.91 42.13 42.35 height float64 ... lon (rlat, rlon) float64 ... lat (rlat, rlon) float64 ... Dimensions without coordinates: bnds Data variables: rotated_pole |S64 ... time_bnds (time, bnds) object ... tas (time, rlat, rlon) float32 ... Attributes: (12/27) title: CanRCM4 model output prepared for CORDEX... institution: CCCma (Canadian Centre for Climate Model... institute_id: CCCma contact: cccma_info@ec.gc.ca Conventions: CF-1.4 experiment: RCP4.5 run driven by CCCma-CanESM2 ... ... data_licence: 1) GRANT OF LICENCE - The Government of ... creation_date: 2012-12-22-T23:16:50Z c3s_comment: This data has been published at ESGF wit... tracking_id: hdl:21.14103/c6ad64ad-3e4f-4540-8987-33b... DODS.strlen: 0 DODS_EXTRA.Unlimited_Dimension: time, <xarray.Dataset> Dimensions: (bnds: 2, rlat: 201, rlon: 194, time: 1825) Coordinates: * time (time) object 2006-01-01 12:00:00 ... 2010-12-31 12:00:00 * rlon (rlon) float64 -24.64 -24.2 -23.76 -23.32 ... 59.4 59.84 60.28 * rlat (rlat) float64 -45.76 -45.32 -44.88 ... 41.36 41.8 42.24 height float64 ... lon (rlat, rlon) float64 ... lat (rlat, rlon) float64 ... Dimensions without coordinates: bnds Data variables: rotated_pole |S64 ... time_bnds (time, bnds) object ... tas (time, rlat, rlon) float32 ... Attributes: (12/27) title: CanRCM4 model output prepared for CORDEX... institution: CCCma (Canadian Centre for Climate Model... institute_id: CCCma contact: cccma_info@ec.gc.ca Conventions: CF-1.4 experiment: RCP4.5 run driven by CCCma-CanESM2 ... ... data_licence: 1) GRANT OF LICENCE - The Government of ... creation_date: 2012-06-29-T23:48:22Z c3s_comment: This data has been published at ESGF wit... tracking_id: hdl:21.14103/c2886168-8f74-4acf-aa15-2b9... DODS.strlen: 0 DODS_EXTRA.Unlimited_Dimension: time)
Crop dataset over West-Africa¶
ds_44=cordex_44_df.sel(rlat=slice(-5,35), rlon=slice(-25,35))
ds_22=cordex_22_df.sel(rlat=slice(-5,35), rlon=slice(-25,35))
## save copy to netcdf files
ds_44.to_netcdf('tas_AFR-44_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231_rg.nc')
ds_22.to_netcdf('tas_AFR-22_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231_rg.nc')
print(ds_44,'\n-----------------------------------------\n',ds_22)
<xarray.Dataset> Dimensions: (bnds: 2, rlat: 91, rlon: 136, time: 1825) Coordinates: * time (time) object 2006-01-01 12:00:00 ... 2010-12-31 12:00:00 * rlon (rlon) float64 -24.64 -24.2 -23.76 ... 33.88 34.32 34.76 * rlat (rlat) float64 -4.84 -4.4 -3.96 -3.52 ... 33.88 34.32 34.76 height float64 2.0 lon (rlat, rlon) float64 335.4 335.8 336.2 ... 33.88 34.32 34.76 lat (rlat, rlon) float64 -4.84 -4.84 -4.84 ... 34.76 34.76 34.76 Dimensions without coordinates: bnds Data variables: rotated_pole |S64 b'' time_bnds (time, bnds) object 2006-01-01 00:00:00 ... 2011-01-01 00:0... tas (time, rlat, rlon) float32 ... Attributes: (12/27) title: CanRCM4 model output prepared for CORDEX... institution: CCCma (Canadian Centre for Climate Model... institute_id: CCCma contact: cccma_info@ec.gc.ca Conventions: CF-1.4 experiment: RCP4.5 run driven by CCCma-CanESM2 ... ... data_licence: 1) GRANT OF LICENCE - The Government of ... creation_date: 2012-06-29-T23:48:22Z c3s_comment: This data has been published at ESGF wit... tracking_id: hdl:21.14103/c2886168-8f74-4acf-aa15-2b9... DODS.strlen: 0 DODS_EXTRA.Unlimited_Dimension: time ----------------------------------------- <xarray.Dataset> Dimensions: (bnds: 2, rlat: 182, rlon: 272, time: 1825) Coordinates: * time (time) object 2006-01-01 12:00:00 ... 2010-12-31 12:00:00 * rlon (rlon) float64 -24.75 -24.53 -24.31 ... 34.43 34.65 34.87 * rlat (rlat) float64 -4.95 -4.73 -4.51 -4.29 ... 34.43 34.65 34.87 height float64 2.0 lon (rlat, rlon) float64 ... lat (rlat, rlon) float64 ... Dimensions without coordinates: bnds Data variables: rotated_pole |S64 b'' time_bnds (time, bnds) object 2006-01-01 00:00:00 ... 2011-01-01 00:0... tas (time, rlat, rlon) float32 ... Attributes: (12/27) title: CanRCM4 model output prepared for CORDEX... institution: CCCma (Canadian Centre for Climate Model... institute_id: CCCma contact: cccma_info@ec.gc.ca Conventions: CF-1.4 experiment: RCP4.5 run driven by CCCma-CanESM2 ... ... data_licence: 1) GRANT OF LICENCE - The Government of ... creation_date: 2012-12-22-T23:16:50Z c3s_comment: This data has been published at ESGF wit... tracking_id: hdl:21.14103/c6ad64ad-3e4f-4540-8987-33b... DODS.strlen: 0 DODS_EXTRA.Unlimited_Dimension: time
Interpolating to a regular latlon grid using nctoolkit package¶
nctoolkit features built in methods for horizontal and vertical interpolation.¶
print(cordex_22_df.rlon.attrs)
print(cordex_44_df.rlon.attrs)
{'long_name': 'longitude in rotated pole grid', 'units': 'degrees', 'axis': 'X', 'standard_name': 'grid_longitude', '_ChunkSizes': 388} {'long_name': 'longitude in rotated pole grid', 'units': 'degrees', 'axis': 'X', 'standard_name': 'grid_longitude', '_ChunkSizes': 194}
import nctoolkit as nc
# Regrid cordex 44km data to regular lonlat
ds = nc.open_thredds('tas_AFR-44_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231_rg.nc')
ds.to_latlon(lon = [-24.64,34.76],
lat = [-4.84,34.76],
res = [0.44, 0.44])
cordex_44=ds.to_xarray()
cordex_44
<xarray.Dataset> Dimensions: (bnds: 2, lat: 90, lon: 136, time: 1825) Coordinates: * time (time) object 2006-01-01 12:00:00 ... 2010-12-31 12:00:00 * lon (lon) float32 -24.64 -24.2 -23.76 -23.32 ... 33.88 34.32 34.76 * lat (lat) float32 -4.84 -4.4 -3.96 -3.52 ... 33.0 33.44 33.88 34.32 height float64 2.0 Dimensions without coordinates: bnds Data variables: time_bnds (time, bnds) object 2006-01-01 00:00:00 ... 2011-01-01 00:00:00 tas (time, lat, lon) float32 ... Attributes: (12/29) CDI: Climate Data Interface version ?? (http:... history: Fri Jul 02 21:56:46 2021: cdo -L -remap,... institution: CCCma (Canadian Centre for Climate Model... Conventions: CF-1.4 title: CanRCM4 model output prepared for CORDEX... institute_id: CCCma ... ... creation_date: 2012-06-29-T23:48:22Z c3s_comment: This data has been published at ESGF wit... tracking_id: hdl:21.14103/c2886168-8f74-4acf-aa15-2b9... DODS.strlen: 0 DODS_EXTRA.Unlimited_Dimension: time CDO: Climate Data Operators version 1.9.3 (ht...
- bnds: 2
- lat: 90
- lon: 136
- time: 1825
- time(time)object2006-01-01 12:00:00 ... 2010-12-...
- standard_name :
- time
- long_name :
- time
- bounds :
- time_bnds
- axis :
- T
array([cftime.DatetimeNoLeap(2006, 1, 1, 12, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 2, 12, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 3, 12, 0, 0, 0), ..., cftime.DatetimeNoLeap(2010, 12, 29, 12, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 30, 12, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 31, 12, 0, 0, 0)], dtype=object)
- lon(lon)float32-24.64 -24.2 -23.76 ... 34.32 34.76
- standard_name :
- longitude
- long_name :
- Longitude
- units :
- degrees_east
- axis :
- X
array([-24.64, -24.2 , -23.76, -23.32, -22.88, -22.44, -22. , -21.56, -21.12, -20.68, -20.24, -19.8 , -19.36, -18.92, -18.48, -18.04, -17.6 , -17.16, -16.72, -16.28, -15.84, -15.4 , -14.96, -14.52, -14.08, -13.64, -13.2 , -12.76, -12.32, -11.88, -11.44, -11. , -10.56, -10.12, -9.68, -9.24, -8.8 , -8.36, -7.92, -7.48, -7.04, -6.6 , -6.16, -5.72, -5.28, -4.84, -4.4 , -3.96, -3.52, -3.08, -2.64, -2.2 , -1.76, -1.32, -0.88, -0.44, 0. , 0.44, 0.88, 1.32, 1.76, 2.2 , 2.64, 3.08, 3.52, 3.96, 4.4 , 4.84, 5.28, 5.72, 6.16, 6.6 , 7.04, 7.48, 7.92, 8.36, 8.8 , 9.24, 9.68, 10.12, 10.56, 11. , 11.44, 11.88, 12.32, 12.76, 13.2 , 13.64, 14.08, 14.52, 14.96, 15.4 , 15.84, 16.28, 16.72, 17.16, 17.6 , 18.04, 18.48, 18.92, 19.36, 19.8 , 20.24, 20.68, 21.12, 21.56, 22. , 22.44, 22.88, 23.32, 23.76, 24.2 , 24.64, 25.08, 25.52, 25.96, 26.4 , 26.84, 27.28, 27.72, 28.16, 28.6 , 29.04, 29.48, 29.92, 30.36, 30.8 , 31.24, 31.68, 32.12, 32.56, 33. , 33.44, 33.88, 34.32, 34.76], dtype=float32)
- lat(lat)float32-4.84 -4.4 -3.96 ... 33.88 34.32
- standard_name :
- latitude
- long_name :
- Latitude
- units :
- degrees_north
- axis :
- Y
array([-4.84, -4.4 , -3.96, -3.52, -3.08, -2.64, -2.2 , -1.76, -1.32, -0.88, -0.44, 0. , 0.44, 0.88, 1.32, 1.76, 2.2 , 2.64, 3.08, 3.52, 3.96, 4.4 , 4.84, 5.28, 5.72, 6.16, 6.6 , 7.04, 7.48, 7.92, 8.36, 8.8 , 9.24, 9.68, 10.12, 10.56, 11. , 11.44, 11.88, 12.32, 12.76, 13.2 , 13.64, 14.08, 14.52, 14.96, 15.4 , 15.84, 16.28, 16.72, 17.16, 17.6 , 18.04, 18.48, 18.92, 19.36, 19.8 , 20.24, 20.68, 21.12, 21.56, 22. , 22.44, 22.88, 23.32, 23.76, 24.2 , 24.64, 25.08, 25.52, 25.96, 26.4 , 26.84, 27.28, 27.72, 28.16, 28.6 , 29.04, 29.48, 29.92, 30.36, 30.8 , 31.24, 31.68, 32.12, 32.56, 33. , 33.44, 33.88, 34.32], dtype=float32)
- height()float642.0
- standard_name :
- height
- long_name :
- height
- units :
- m
- positive :
- up
- axis :
- Z
array(2.)
- time_bnds(time, bnds)object2006-01-01 00:00:00 ... 2011-01-...
array([[cftime.DatetimeNoLeap(2006, 1, 1, 0, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 2, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2006, 1, 2, 0, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 3, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2006, 1, 3, 0, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 4, 0, 0, 0, 0)], ..., [cftime.DatetimeNoLeap(2010, 12, 29, 0, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 30, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2010, 12, 30, 0, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 31, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2010, 12, 31, 0, 0, 0, 0), cftime.DatetimeNoLeap(2011, 1, 1, 0, 0, 0, 0)]], dtype=object)
- tas(time, lat, lon)float32...
- standard_name :
- air_temperature
- long_name :
- Near-Surface Air Temperature
- units :
- K
- cell_methods :
- time: mean
- _ChunkSizes :
- [ 1 201 194]
[22338000 values with dtype=float32]
- CDI :
- Climate Data Interface version ?? (http://mpimet.mpg.de/cdi)
- history :
- Fri Jul 02 21:56:46 2021: cdo -L -remap,/tmp/nctoolkitjyypyjshnctoolkittmpq43fsen,/tmp/nctoolkitjyypyjshnctoolkittmpo8cguxsx.nc tas_AFR-44_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231_rg.nc /tmp/nctoolkitjyypyjshnctoolkittmpa7tcobs_.nc created: 2012-06-29 23:55:27 by rcm2nc
- institution :
- CCCma (Canadian Centre for Climate Modelling and Analysis, Victoria, BC, Canada)
- Conventions :
- CF-1.4
- title :
- CanRCM4 model output prepared for CORDEX Project
- institute_id :
- CCCma
- contact :
- cccma_info@ec.gc.ca
- experiment :
- RCP4.5 run driven by CCCma-CanESM2
- experiment_id :
- rcp45
- driving_experiment :
- CCCma-CanESM2, rcp45, r1i1p1
- driving_model_id :
- CCCma-CanESM2
- driving_model_ensemble_member :
- r1i1p1
- driving_experiment_name :
- rcp45
- forcing :
- GHG,Oz,SA,BC,OC,LU,Vl (GHG includes CO2,CH4,N2O,CFC11,effective CFC12)
- model_id :
- CCCma-CanRCM4
- rcm_version_id :
- r2
- project_id :
- CORDEX
- CORDEX_domain :
- AFR-44
- frequency :
- day
- product :
- output
- CCCma_runid :
- afr017_ica44
- references :
- http://www.cccma.ec.gc.ca/models
- data_licence :
- 1) GRANT OF LICENCE - The Government of Canada (Environment Canada) is the owner of all intellectual property rights (including copyright) that may exist in this Data product. You (as "The Licensee") are hereby granted a non-exclusive, non-assignable, non-transferable unrestricted licence to use this data product for any purpose including the right to share these data with others and to make value-added and derivative products from it. This licence is not a sale of any or all of the owner's rights. 2) NO WARRANTY - This Data product is provided "as-is"; it has not been designed or prepared to meet the Licensee's particular requirements. Environment Canada makes no warranty, either express or implied, including but not limited to, warranties of merchantability and fitness for a particular purpose. In no event will Environment Canada be liable for any indirect, special, consequential or other damages attributed to the Licensee's use of the Data product.
- creation_date :
- 2012-06-29-T23:48:22Z
- c3s_comment :
- This data has been published at ESGF with the support of the Copernicus Climate Change Service (C3S - https://climate.copernicus.eu/) in the context of the C3S_34d contract.
- tracking_id :
- hdl:21.14103/c2886168-8f74-4acf-aa15-2b90a5658fb5
- DODS.strlen :
- 0
- DODS_EXTRA.Unlimited_Dimension :
- time
- CDO :
- Climate Data Operators version 1.9.3 (http://mpimet.mpg.de/cdo)
# Regrid cordex 22 km data to regular lonlat
dk = nc.open_thredds('tas_AFR-22_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231_rg.nc')
dk.to_latlon(lon = [-24.75,34.87],
lat = [-4.95,34.87],
res = [0.22,0.22])
cordex_22=dk.to_xarray()
cordex_22
<xarray.Dataset> Dimensions: (bnds: 2, lat: 182, lon: 272, time: 1825) Coordinates: * time (time) object 2006-01-01 12:00:00 ... 2010-12-31 12:00:00 * lon (lon) float32 -24.75 -24.53 -24.31 -24.09 ... 34.43 34.65 34.87 * lat (lat) float32 -4.95 -4.73 -4.51 -4.29 ... 34.21 34.43 34.65 34.87 height float64 2.0 Dimensions without coordinates: bnds Data variables: time_bnds (time, bnds) object 2006-01-01 00:00:00 ... 2011-01-01 00:00:00 tas (time, lat, lon) float32 ... Attributes: (12/29) CDI: Climate Data Interface version ?? (http:... history: Fri Jul 02 21:58:52 2021: cdo -L -remap,... institution: CCCma (Canadian Centre for Climate Model... Conventions: CF-1.4 title: CanRCM4 model output prepared for CORDEX... institute_id: CCCma ... ... creation_date: 2012-12-22-T23:16:50Z c3s_comment: This data has been published at ESGF wit... tracking_id: hdl:21.14103/c6ad64ad-3e4f-4540-8987-33b... DODS.strlen: 0 DODS_EXTRA.Unlimited_Dimension: time CDO: Climate Data Operators version 1.9.3 (ht...
- bnds: 2
- lat: 182
- lon: 272
- time: 1825
- time(time)object2006-01-01 12:00:00 ... 2010-12-...
- standard_name :
- time
- long_name :
- time
- bounds :
- time_bnds
- axis :
- T
array([cftime.DatetimeNoLeap(2006, 1, 1, 12, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 2, 12, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 3, 12, 0, 0, 0), ..., cftime.DatetimeNoLeap(2010, 12, 29, 12, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 30, 12, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 31, 12, 0, 0, 0)], dtype=object)
- lon(lon)float32-24.75 -24.53 ... 34.65 34.87
- standard_name :
- longitude
- long_name :
- Longitude
- units :
- degrees_east
- axis :
- X
array([-24.75, -24.53, -24.31, ..., 34.43, 34.65, 34.87], dtype=float32)
- lat(lat)float32-4.95 -4.73 -4.51 ... 34.65 34.87
- standard_name :
- latitude
- long_name :
- Latitude
- units :
- degrees_north
- axis :
- Y
array([-4.95, -4.73, -4.51, -4.29, -4.07, -3.85, -3.63, -3.41, -3.19, -2.97, -2.75, -2.53, -2.31, -2.09, -1.87, -1.65, -1.43, -1.21, -0.99, -0.77, -0.55, -0.33, -0.11, 0.11, 0.33, 0.55, 0.77, 0.99, 1.21, 1.43, 1.65, 1.87, 2.09, 2.31, 2.53, 2.75, 2.97, 3.19, 3.41, 3.63, 3.85, 4.07, 4.29, 4.51, 4.73, 4.95, 5.17, 5.39, 5.61, 5.83, 6.05, 6.27, 6.49, 6.71, 6.93, 7.15, 7.37, 7.59, 7.81, 8.03, 8.25, 8.47, 8.69, 8.91, 9.13, 9.35, 9.57, 9.79, 10.01, 10.23, 10.45, 10.67, 10.89, 11.11, 11.33, 11.55, 11.77, 11.99, 12.21, 12.43, 12.65, 12.87, 13.09, 13.31, 13.53, 13.75, 13.97, 14.19, 14.41, 14.63, 14.85, 15.07, 15.29, 15.51, 15.73, 15.95, 16.17, 16.39, 16.61, 16.83, 17.05, 17.27, 17.49, 17.71, 17.93, 18.15, 18.37, 18.59, 18.81, 19.03, 19.25, 19.47, 19.69, 19.91, 20.13, 20.35, 20.57, 20.79, 21.01, 21.23, 21.45, 21.67, 21.89, 22.11, 22.33, 22.55, 22.77, 22.99, 23.21, 23.43, 23.65, 23.87, 24.09, 24.31, 24.53, 24.75, 24.97, 25.19, 25.41, 25.63, 25.85, 26.07, 26.29, 26.51, 26.73, 26.95, 27.17, 27.39, 27.61, 27.83, 28.05, 28.27, 28.49, 28.71, 28.93, 29.15, 29.37, 29.59, 29.81, 30.03, 30.25, 30.47, 30.69, 30.91, 31.13, 31.35, 31.57, 31.79, 32.01, 32.23, 32.45, 32.67, 32.89, 33.11, 33.33, 33.55, 33.77, 33.99, 34.21, 34.43, 34.65, 34.87], dtype=float32)
- height()float64...
- standard_name :
- height
- long_name :
- height
- units :
- m
- positive :
- up
- axis :
- Z
array(2.)
- time_bnds(time, bnds)object...
array([[cftime.DatetimeNoLeap(2006, 1, 1, 0, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 2, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2006, 1, 2, 0, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 3, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2006, 1, 3, 0, 0, 0, 0), cftime.DatetimeNoLeap(2006, 1, 4, 0, 0, 0, 0)], ..., [cftime.DatetimeNoLeap(2010, 12, 29, 0, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 30, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2010, 12, 30, 0, 0, 0, 0), cftime.DatetimeNoLeap(2010, 12, 31, 0, 0, 0, 0)], [cftime.DatetimeNoLeap(2010, 12, 31, 0, 0, 0, 0), cftime.DatetimeNoLeap(2011, 1, 1, 0, 0, 0, 0)]], dtype=object)
- tas(time, lat, lon)float32...
- standard_name :
- air_temperature
- long_name :
- Near-Surface Air Temperature
- units :
- K
- cell_methods :
- time: mean
- _ChunkSizes :
- [ 1 402 388]
[90344800 values with dtype=float32]
- CDI :
- Climate Data Interface version ?? (http://mpimet.mpg.de/cdi)
- history :
- Fri Jul 02 21:58:52 2021: cdo -L -remap,/tmp/nctoolkitjyypyjshnctoolkittmpuf4eaph,/tmp/nctoolkitjyypyjshnctoolkittmppmbv59_e.nc tas_AFR-22_CCCma-CanESM2_rcp45_r1i1p1_CCCma-CanRCM4_r2_day_20060101-20101231_rg.nc /tmp/nctoolkitjyypyjshnctoolkittmp_5l2ii_g.nc created: 2012-12-22 23:41:09 by rcm2nc
- institution :
- CCCma (Canadian Centre for Climate Modelling and Analysis, Victoria, BC, Canada)
- Conventions :
- CF-1.4
- title :
- CanRCM4 model output prepared for CORDEX Project
- institute_id :
- CCCma
- contact :
- cccma_info@ec.gc.ca
- experiment :
- RCP4.5 run driven by CCCma-CanESM2
- experiment_id :
- rcp45
- driving_experiment :
- CCCma-CanESM2, rcp45, r1i1p1
- driving_model_id :
- CCCma-CanESM2
- driving_model_ensemble_member :
- r1i1p1
- driving_experiment_name :
- rcp45
- forcing :
- GHG,Oz,SA,BC,OC,LU,Vl (GHG includes CO2,CH4,N2O,CFC11,effective CFC12)
- model_id :
- CCCma-CanRCM4
- rcm_version_id :
- r2
- project_id :
- CORDEX
- CORDEX_domain :
- AFR-22
- frequency :
- day
- product :
- output
- CCCma_runid :
- afr026_ica22
- references :
- http://www.cccma.ec.gc.ca/models
- data_licence :
- 1) GRANT OF LICENCE - The Government of Canada (Environment Canada) is the owner of all intellectual property rights (including copyright) that may exist in this Data product. You (as "The Licensee") are hereby granted a non-exclusive, non-assignable, non-transferable unrestricted licence to use this data product for any purpose including the right to share these data with others and to make value-added and derivative products from it. This licence is not a sale of any or all of the owner's rights. 2) NO WARRANTY - This Data product is provided "as-is"; it has not been designed or prepared to meet the Licensee's particular requirements. Environment Canada makes no warranty, either express or implied, including but not limited to, warranties of merchantability and fitness for a particular purpose. In no event will Environment Canada be liable for any indirect, special, consequential or other damages attributed to the Licensee's use of the Data product.
- creation_date :
- 2012-12-22-T23:16:50Z
- c3s_comment :
- This data has been published at ESGF with the support of the Copernicus Climate Change Service (C3S - https://climate.copernicus.eu/) in the context of the C3S_34d contract.
- tracking_id :
- hdl:21.14103/c6ad64ad-3e4f-4540-8987-33b243461ec8
- DODS.strlen :
- 0
- DODS_EXTRA.Unlimited_Dimension :
- time
- CDO :
- Climate Data Operators version 1.9.3 (http://mpimet.mpg.de/cdo)
Compute Seasonal mean¶
seas_22=cordex_22.groupby('time.season').mean()
seas_44=cordex_44.groupby('time.season').mean()
# drop unecessary dimensions
seas_22=seas_22.drop(['height'])
seas_44=seas_44.drop(['height'])
# convert in degree celsius
seas_22['tas']=seas_22['tas']-273.15
seas_44['tas']=seas_44['tas']-273.15
print (seas_44,'\n-------------------------\n',seas_22)
<xarray.Dataset> Dimensions: (lat: 90, lon: 136, season: 4) Coordinates: * lon (lon) float32 -24.64 -24.2 -23.76 -23.32 ... 33.88 34.32 34.76 * lat (lat) float32 -4.84 -4.4 -3.96 -3.52 ... 33.0 33.44 33.88 34.32 * season (season) object 'DJF' 'JJA' 'MAM' 'SON' Data variables: tas (season, lat, lon) float32 nan nan nan nan ... 23.38 23.38 nan ------------------------- <xarray.Dataset> Dimensions: (lat: 182, lon: 272, season: 4) Coordinates: * lon (lon) float32 -24.75 -24.53 -24.31 -24.09 ... 34.43 34.65 34.87 * lat (lat) float32 -4.95 -4.73 -4.51 -4.29 ... 34.21 34.43 34.65 34.87 * season (season) object 'DJF' 'JJA' 'MAM' 'SON' Data variables: tas (season, lat, lon) float32 nan nan nan nan nan ... nan nan nan nan
Plots¶
## Get coordinate extent
long_22=seas_22['tas'].lon.values
lat_22=seas_22['tas'].lat.values
long_44=seas_44['tas'].lon.values
lat_44=seas_44['tas'].lat.values
import matplotlib
# Plotting all the subplots
matplotlib.rcParams['font.size'] =12
plt.rcParams["axes.linewidth"] = 1.5
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.serif'] = 'New Century Schoolbook'
plt.rcParams["xtick.major.size"]=9
plt.rcParams["ytick.major.size"]=9
plt.rcParams['ytick.right'] =True
plt.rcParams['xtick.top'] =True
# Define x, y axis labels
x_tick_labels = [u'20\N{DEGREE SIGN}W',u'10 \N{DEGREE SIGN}W',
u'0\N{DEGREE SIGN}',u'10\N{DEGREE SIGN}E',
u'20\N{DEGREE SIGN}E',u'30\N{DEGREE SIGN}E']
y_tick_labels = [u'0\N{DEGREE SIGN}N', u'5\N{DEGREE SIGN}N', u'10 \N{DEGREE SIGN}N',
u'15\N{DEGREE SIGN}N',u'20\N{DEGREE SIGN}N',u'25\N{DEGREE SIGN}N',u'30\N{DEGREE SIGN}N']
import matplotlib
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
# set projection
projection = ccrs.PlateCarree()
## Set subplots layout and size
fig, axes = plt.subplots(2,4,sharex=True, sharey=True,subplot_kw=dict(projection=ccrs.PlateCarree()))
fig.set_size_inches(18,10)
# plot each dataset per season
for i, season in enumerate(('DJF','MAM','JJA', 'SON')):
o= axes[0,i].contourf(long_22, lat_22,seas_22['tas'].sel(season=season),
transform=projection,cmap='Spectral_r')
p= axes[1,i].contourf(long_44,lat_44,seas_44['tas'].sel(season=season),
transform=projection,cmap='Spectral_r')
axes[0, i].set_title(season,fontsize=16,fontweight='bold')
# add coastlines and country borders to the contour plot
for ax in axes.flat:
ax.set_xlim(-20,30)
ax.set_ylim(0,30)
ax.set_xticks(np.linspace(-20,30,5), crs=projection)
ax.set_yticks(np.linspace(0,30,7), crs=projection)
ax.coastlines()
ax.add_feature(cfeature.BORDERS, linewidths=1)
ax.axes.axis('tight')
# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axes.flat:
ax.label_outer()
ax.set_xticks([-20,-10,0,10,20,30])
ax.set_xticklabels(x_tick_labels)
ax.set_yticks([0,5,10,15,20,25,30])
ax.set_yticklabels(y_tick_labels)
# Assign label to y axis
for i,rowlabel in enumerate(('CORDEX 22 km' ,'CORDEX 44 km')):
axes[i, 0].set_ylabel(rowlabel,fontsize=16,fontweight='bold')
for ax in axes[-1,:].flatten():
ax.set_xlabel('Longitude',fontsize=16,fontweight='bold')
# flatten axes and get subplots positions
DP = axes.T.flatten()
# Adjust colorbar to the right side of each row
# where arg is [left, bottom, width, height]
cax1=fig.add_axes([DP[6].get_position().x1+0.01,DP[6].get_position().y0,
0.01,DP[6].get_position().height])
cax2=fig.add_axes([DP[7].get_position().x1+0.01,DP[7].get_position().y0,
0.01,DP[7].get_position().height])
# plot colobar on figures
fig.colorbar(o, cax=cax1,label='Deg C')
fig.colorbar(p, cax=cax2,label='Deg C')
plt.savefig('seasonal_trend_CORDEX22_44.png',transparent=True,dpi=300)
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
Enjoy and share it, I am back now with some regular posts