Skip to content

Instantly share code, notes, and snippets.

@jbusecke
Created October 25, 2017 16:03
Show Gist options
  • Save jbusecke/4f00e42c1506f6254ea5ec82caabbb45 to your computer and use it in GitHub Desktop.
Save jbusecke/4f00e42c1506f6254ea5ec82caabbb45 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\nfrom xarrayutils.utils import extract_surf",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.repeat(np.repeat(np.array([0 ,21 ,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)\nda[{'x':1,'y':0,'z':3}] = 20\nprint(da)\nprint(db)",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "<xarray.DataArray (x: 3, y: 2, z: 4)>\narray([[[ 0, 21, 30, 40],\n [ 0, 21, 30, 40]],\n\n [[ 0, 21, 30, 20],\n [ 0, 21, 30, 40]],\n\n [[ 0, 21, 30, 40],\n [ 0, 21, 30, 40]]])\nCoordinates:\n * x (x) int64 0 1 2\n * y (y) int64 0 1\n * z (z) int64 0 1 2 3\n<xarray.DataArray (x: 3, y: 2, z: 4)>\narray([[[200, 300, 400, 500],\n [200, 300, 400, 500]],\n\n [[200, 300, 400, 500],\n [200, 300, 400, 500]],\n\n [[200, 300, 400, 500],\n [200, 300, 400, 500]]])\nCoordinates:\n * x (x) int64 0 1 2\n * y (y) int64 0 1\n * z (z) int64 0 1 2 3\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# This illustrates a problem with noisy data or values that are found multiple times \n# in the watercolumn. Since one profile has the exact value of 20 at the bottom, 500 is \n# extracted from db. I will have to work on that at a later time. For 'well-behaved' data \n# (e.g. long averages this should work)\nsurf = extract_surf(da, db, 20, 'z')\nsurf",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "<xarray.DataArray (x: 3, y: 2)>\narray([[ 300., 300.],\n [ 500., 300.],\n [ 300., 300.]])\nCoordinates:\n * x (x) int64 0 1 2\n * y (y) int64 0 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": 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": "3b7fb2f6b389f81d53c5e1e059acc79d",
"data": {
"description": "Desktop/indexing_exercise.ipynb",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/3b7fb2f6b389f81d53c5e1e059acc79d"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment