from pyesgf.search import SearchConnection
conn = SearchConnection('https://esgf-data.dkrz.de/esg-search', distrib=True)

ctx = conn.new_context(
    project='CMIP5', 
    experiment='rcp45',
    model='HadCM3',
    ensemble='r1i1p1',
    time_frequency='mon',
    realm='atmos',
    data_node='esgf-data1.ceda.ac.uk',
    )
ctx.hit_count

result = ctx.search()[0]
result.dataset_id

files = result.file_context().search()
for file in files:
    if 'tasmax' in file.opendap_url:
        tasmax_url = file.opendap_url
        print(tasmax_url)

"""
Explanation: Subset CMIP5 Datasets with xarray
xarray: http://xarray.pydata.org/en/stable/index.html
Search CMIP5 Dataset with ESGF pyclient
using: https://esgf-pyclient.readthedocs.io/en/latest/index.html
End of explanation
"""


from pyesgf.logon import LogonManager
lm = LogonManager()
lm.logoff()
lm.is_logged_on()

lm.logon(hostname='esgf-data.dkrz.de', interactive=True, bootstrap=True)
lm.is_logged_on()

"""
Explanation: ESGF Logon
End of explanation
"""


import xarray as xr
ds = xr.open_dataset(tasmax_url, chunks={'time': 120})
print(ds)

da = ds['tasmax']
da = da.isel(time=slice(0, 1))
da = da.sel(lat=slice(-50, 50), lon=slice(0, 50))


%matplotlib inline
da.plot()

"""
Explanation: Subset single dataset with xarray
Using OpenDAP: http://xarray.pydata.org/en/stable/io.html?highlight=opendap#opendap
End of explanation
"""


da.to_netcdf('tasmax.nc')

"""
Explanation: Download to NetCDF
End of explanation
"""


