Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explanation for unamed-variables error and a masked-data example #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 145 additions & 22 deletions doc/tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -38,8 +39,7 @@
"%matplotlib inline\n",
"\n",
"nt, nx = 100, 30\n",
"da = xr.DataArray(np.random.randn(nt, nx), dims=['time', 'x'],\n",
" name='foo') # all inputs need a name\n",
"da = xr.DataArray(np.random.randn(nt, nx), dims=['time', 'x'])\n",
"display(da)\n",
"da.plot()"
]
Expand All @@ -55,16 +55,60 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
"tags": []
},
"outputs": [],
"source": [
"from xhistogram.xarray import histogram\n",
"\n",
"bins = np.linspace(-4, 4, 20)\n",
"h = histogram(da, bins=[bins])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This error occurred because no name was given to `da` (e.g. `da.name == None`) and `histogram` needs that to determine the name of the output dimension.\n",
"We can solve this by either renaming `da` at the `histogram` input as\n",
"\n",
"```python\n",
"h = histogram(da.rename(\"foo\"), bins=[bins])\n",
"```\n",
"\n",
"or redefining `da` by giving the name at the input"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"da = xr.DataArray(np.random.randn(nt, nx), dims=['time', 'x'], name = \"foo\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can calculate the histogram and visualize it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"tags": []
},
"outputs": [],
"source": [
"h = histogram(da, bins=[bins])\n",
"display(h)\n",
"h.plot()"
Expand All @@ -90,7 +134,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -113,7 +158,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -136,7 +182,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -158,7 +205,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -182,7 +230,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -219,7 +268,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -232,7 +282,7 @@
" xr.open_dataset(Temp_url).tmn.load(),\n",
" xr.open_dataset(Salt_url).smn.load(),\n",
" xr.open_dataset(Oxy_url).omn.load()])\n",
"ds"
"display(ds)"
]
},
{
Expand All @@ -249,7 +299,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -264,7 +315,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -292,7 +344,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand All @@ -301,7 +354,7 @@
"\n",
"# Create a dz variable\n",
"dz = np.diff(ds.lev)\n",
"dz =np.insert(dz, 0, dz[0])\n",
"dz = np.insert(dz, 0, dz[0])\n",
"dz = xr.DataArray(dz, coords= {'lev':ds.lev}, dims='lev')\n",
"\n",
"# weight by volume of grid cell (resolution = 5degree, 1degree=110km)\n",
Expand All @@ -319,6 +372,75 @@
"The ridges of this above plot are indicative of T/S classes with a lot of volume, and some of them are indicative of Mode Waters (example Eighteen Degree water with T$\\sim18^oC$, and S$\\sim36.5psu$. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's suppose that we have a mask for different regions in the planet. For the sake of simplicity, we will create a mask to separate the globe into three regions.\n",
"\n",
"- Tropical: `np.abs(latitude) <= 30`\n",
"- Temperate: `30 < np.abs(latitude) <= 60`\n",
"- Polar: `60 < np.abs(latitude)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"zones = np.abs(ds.lat / 30).round().rename(\"zones\")\n",
"display(zones)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now there are two ways of doing that, we can create a new dimension on the histogram"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"zonebins = np.arange(-0.5, 4, 1)\n",
"hTSw = histogram(ds.smn, ds.tmn, zones, bins=[sbins, tbins, zonebins], weights=dVol)\n",
"np.log10(hTSw.T).plot(levels=31, vmin=11.5, vmax=16, cmap='brg', col = \"zones_bin\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or use groupby function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"hTSw = ds.assign(dVol = dVol).groupby(zones).apply(lambda ds: histogram(ds.smn, ds.tmn, bins=[sbins, tbins], weights=ds.dVol))\n",
"np.log10(hTSw.T).plot(levels=31, vmin=11.5, vmax=16, cmap='brg', col = \"zones\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The second option is more verbose, but more efficient and also works with text or mask values that does not vary monotonically."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -339,7 +461,8 @@
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"tags": []
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -381,9 +504,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "coringa0.3.0",
"language": "python",
"name": "python3"
"name": "coringa0.3.0"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -395,7 +518,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down