Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2017 22:17
Show Gist options
  • Save anonymous/3b7fb2f6b389f81d53c5e1e059acc79d to your computer and use it in GitHub Desktop.
Save anonymous/3b7fb2f6b389f81d53c5e1e059acc79d to your computer and use it in GitHub Desktop.
Desktop/indexing_exercise.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "import xarray as xr\nimport numpy as np",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.repeat(np.repeat(np.array([0 ,20 ,30, 40])[np.newaxis,np.newaxis,:],2, axis=1),3, axis=0)\nb = np.repeat(np.repeat(np.array([200 ,300 ,400 , 500])[np.newaxis,np.newaxis,:],2, axis=1),3, axis=0)\n\ncoords = {'x':range(3), 'y':range(2), 'z':range(4)}\ndims = ['x', 'y', 'z']\nda = xr.DataArray(a,coords=coords,dims=dims)\ndb = xr.DataArray(b,coords=coords,dims=dims)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# find the dimensional ('z') index at every position\nz_ind = abs(da-20).argmin('z')\nprint(z_ind)",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "<xarray.DataArray (x: 3, y: 2)>\narray([[1, 1],\n [1, 1],\n [1, 1]])\nCoordinates:\n * x (x) int64 0 1 2\n * y (y) int64 0 1\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Select the depth slice\ndb.isel_points(z_ind)",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "<xarray.DataArray (y: 2, z: 4, x: 3)>\narray([[[200, 200, 200],\n [300, 300, 300],\n [400, 400, 400],\n [500, 500, 500]],\n\n [[200, 200, 200],\n [300, 300, 300],\n [400, 400, 400],\n [500, 500, 500]]])\nCoordinates:\n * x (x) int64 0 1 2\n * y (y) int64 0 1\n * z (z) int64 0 1 2 3\n None (x, y) int64 1 1 1 1 1 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": true,
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.2",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"gist": {
"id": "",
"data": {
"description": "Desktop/indexing_exercise.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment