Dataset: project=CMIP5, model=MIROC4h, Atmosphere and Ocean Research Institute (The University of Tokyo), National Institute for Environmental Studies, and Japan Agency for Marine-Earth Science and Technology, experiment=10- or 30-year run initialized in year 2000, time_frequency=mon, modeling realm=land, ensemble=r3i1p1, version=20120628
numpy: `pip install numpy` or `conda install numpy`
\n",
"
\n",
"
\n",
"\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from siphon.catalog import TDSCatalog\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import ipywidgets as widgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"catUrl = \"https://esgf2.dkrz.de/thredds/catalog/esgcet/89/cmip5.output1.MIROC.MIROC4h.decadal2000.mon.land.Lmon.r3i1p1.v20120628.xml\";\n",
"datasetName = \"project=CMIP5, model=MIROC4h, Atmosphere and Ocean Research Institute (The University of Tokyo), National Institute for Environmental Studies, and Japan Agency for Marine-Earth Science and Technology, experiment=10- or 30-year run initialized in year 2000, time_frequency=mon, modeling realm=land, ensemble=r3i1p1, version=20120628\";"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Access a dataset
\n",
"With the TDS catalog url, we can use Siphon to get the dataset named `datasetName`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"catalog = TDSCatalog(catUrl)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = catalog.datasets[datasetName]\n",
"ds.name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Datasets each have a set of access protocols:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list(ds.access_urls)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Siphon's `remote-access` returns a `Dataset` object, which opens the remote dataset and provides access to its metadata:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset = ds.remote_access()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list(dataset.ncattrs())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Display a variable:
\n",
"
\n",
"
Run the cells below to get an interactive list of variables in this dataset.
\n",
"
Select the variable you wish to view.
\n",
"
Execute the next cell to display info about the selected variable and plot it.
\n",
"
To plot a different variable, select it from the list and rerun the following cell.
\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var_name = widgets.RadioButtons(\n",
" options=list(dataset.variables),\n",
" description='Variable:')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(var_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var = dataset.variables[var_name.value]\n",
"# display information about the variable\n",
"print(var.name)\n",
"print(list(var.dimensions))\n",
"print(var.shape)\n",
"\n",
"%matplotlib inline\n",
"# attempt to plot the variable\n",
"canPlot = var.dtype == np.uint8 or np.can_cast(var.dtype, float, \"same_kind\") # Only plot numeric types\n",
"if (canPlot):\n",
" ndims = np.squeeze(var[:]).ndim\n",
" # for one-dimensional data, print value\n",
" if (ndims == 0):\n",
" print(var.name, \": \", var)\n",
" # for two-dimensional data, make a line plot\n",
" elif (ndims == 1):\n",
" plt.plot(np.squeeze(np.array([range(len(np.squeeze(var[:])))])), np.squeeze(var[:]), 'bo', markersize=5)\n",
" plt.title(var.name)\n",
" plt.show()\n",
" # for three-dimensional data, make an image\n",
" elif (ndims == 2):\n",
" plt.imshow(var[:])\n",
" plt.title(var.name)\n",
" plt.show()\n",
" # for four or more dimensional data, print values\n",
" else:\n",
" print(\"Too many dimensions - Cannot display variable: \", var.name)\n",
" print(var[:])\n",
"else:\n",
" print(\"Not a numeric type - Cannot display variable: \", var.name)\n",
" print(var[:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that data are only transferred over the network when the variable is sliced, and only data corresponding to the slice are downloaded. In this case, we are asking for all of the data with `var[:]`.\n",
"\n",
"\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"