<jupyter_start><jupyter_text>Example of using xarray with NASA DCP-30 datasetsThis example is based on the following code: https://cloud-gc.readthedocs.io/en/stable/chapter06_appendix/plot_NASANEX.html, but uses data fetched from the Kita Lab server rather than downloaded directly from S3.Note that the only differences from the referenced example is open_dataset call - we provide a url to the data rather than a file name.  Also it is necessary to specify engine='h5netcdf' for xarray to connect with the server.<jupyter_code># get the h5netcdf package
import sys
!{sys.executable} -m pip install h5netcdf
%matplotlib inline
import matplotlib.pyplot as plt
import xarray as xr
import cartopy.crs as ccrs
import h5pyd
import h5netcdf
# should show 0.4.2 or greater
h5pyd.version.version
xr.show_versions()  # xarray should be 0.10.8
# open the dataset using xarray
# with engine='h5netcdf', xarray will use Kita Lab server to access the data
lab_url = "http://hsdshdflab.hdfgroup.org"
filename = "tasmax_day_BCSD_rcp85_r1i1p1_inmcm4_2100.nc"
filepath = "/shared/NASA/" + filename
print(lab_url + filepath)
ds = xr.open_dataset(lab_url + filepath, engine="h5netcdf")
ds
# temp fix for issue noted in: https://github.com/SciTools/cartopy/issues/1120
from matplotlib.axes import Axes
from cartopy.mpl.geoaxes import GeoAxes
GeoAxes._pcolormesh_patched = Axes.pcolormesh
# adjust the longitude axis to run from -180 to 180  (takes a while)
ds.coords['lon'] = (ds.coords['lon'] + 180) % 360 - 180
ds = ds.sortby(ds.lon)
%time arr = ds['tasmax'][0]
ax = plt.axes(projection=ccrs.PlateCarree())
arr.plot(cmap='jet')
ax.coastlines()
plt.title('Near-Surface Air Temperature');
# Zoom into to an area around the continental US
plt.figure(figsize=[8, 6])
ax = plt.axes(projection=ccrs.PlateCarree())
(ds['tasmax'][0].
 sel(lon=slice(-140, -50), lat=slice(20, 60)).
 plot(cmap='jet', cbar_kwargs={'shrink': 0.6, 'label': 'K'})
)
ax.coastlines()
ax.gridlines()
plt.title('Near-Surface Air Temperature over US');<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup