Skip to content

Instantly share code, notes, and snippets.

@bekozi
Last active September 7, 2018 16:17
Show Gist options
  • Save bekozi/79d60cecebfdc1c37c1ed480550fffc2 to your computer and use it in GitHub Desktop.
Save bekozi/79d60cecebfdc1c37c1ed480550fffc2 to your computer and use it in GitHub Desktop.
ESMF corners demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import ocgis\n",
"from ocgis.test.base import create_gridxy_global"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a coarse resolution grid and assert it is in factorized form before printing the latitude coordinates and their bounds."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-165. -135. -105. -75. -45. -15. 15. 45. 75. 105. 135. 165.]\n",
"\n",
"[[-180. -150.]\n",
" [-150. -120.]\n",
" [-120. -90.]\n",
" [ -90. -60.]\n",
" [ -60. -30.]\n",
" [ -30. 0.]\n",
" [ 0. 30.]\n",
" [ 30. 60.]\n",
" [ 60. 90.]\n",
" [ 90. 120.]\n",
" [ 120. 150.]\n",
" [ 150. 180.]]\n"
]
}
],
"source": [
"grid = create_gridxy_global(resolution=30.0)\n",
"assert(grid.x.ndim == 1)\n",
"print(grid.x.v())\n",
"print('')\n",
"print(grid.x.bounds.v())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Expand the grid to use 2D coordinates and print the new corners."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Corners shape: (6, 12, 4)\n",
"\n",
"[[[-180. -150. -150. -180.]\n",
" [-150. -120. -120. -150.]]\n",
"\n",
" [[-180. -150. -150. -180.]\n",
" [-150. -120. -120. -150.]]]\n"
]
}
],
"source": [
"grid.expand()\n",
"assert(grid.x.ndim == 2)\n",
"print('Corners shape:', grid.x.bounds.shape)\n",
"print('')\n",
"print(grid.x.bounds.v()[0:2, 0:2, :])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert to ESMF corners for longitude."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Some ESMF y corners:\n",
"\n",
" [[-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]\n",
" [-90. -60. -30. 0. 30. 60. 90.]]\n",
"\n",
"Some ESMF x corners:\n",
"\n",
" [[-180. -180. -180. -180. -180. -180. -180.]\n",
" [-150. -150. -150. -150. -150. -150. -150.]\n",
" [-120. -120. -120. -120. -120. -120. -120.]\n",
" [ -90. -90. -90. -90. -90. -90. -90.]\n",
" [ -60. -60. -60. -60. -60. -60. -60.]\n",
" [ -30. -30. -30. -30. -30. -30. -30.]\n",
" [ 0. 0. 0. 0. 0. 0. 0.]\n",
" [ 30. 30. 30. 30. 30. 30. 30.]\n",
" [ 60. 60. 60. 60. 60. 60. 60.]\n",
" [ 90. 90. 90. 90. 90. 90. 90.]\n",
" [ 120. 120. 120. 120. 120. 120. 120.]\n",
" [ 150. 150. 150. 150. 150. 150. 150.]\n",
" [ 180. 180. 180. 180. 180. 180. 180.]]\n"
]
}
],
"source": [
"from ocgis.util.helpers import get_esmf_corners_from_ocgis_corners as gec\n",
"\n",
"ye = gec(grid.y.bounds.v())\n",
"xe = gec(grid.x.bounds.v())\n",
"print('Some ESMF y corners:\\n\\n', ye)\n",
"print('')\n",
"print('Some ESMF x corners:\\n\\n', xe)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert to Python/C major indexing. Arguably the y ordering on these should be changed but it doesn't matter for this exercise. I inserted a flip up & down to make it easier to interpret."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python ordered y:\n",
"\n",
" [[ 90. 90. 90. 90. 90. 90. 90. 90. 90. 90. 90. 90. 90.]\n",
" [ 60. 60. 60. 60. 60. 60. 60. 60. 60. 60. 60. 60. 60.]\n",
" [ 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30. 30.]\n",
" [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
" [-30. -30. -30. -30. -30. -30. -30. -30. -30. -30. -30. -30. -30.]\n",
" [-60. -60. -60. -60. -60. -60. -60. -60. -60. -60. -60. -60. -60.]\n",
" [-90. -90. -90. -90. -90. -90. -90. -90. -90. -90. -90. -90. -90.]]\n",
"\n",
"Python ordered x:\n",
"\n",
" [[-180. -150. -120. -90. -60. -30. 0. 30. 60. 90. 120. 150.\n",
" 180.]\n",
" [-180. -150. -120. -90. -60. -30. 0. 30. 60. 90. 120. 150.\n",
" 180.]\n",
" [-180. -150. -120. -90. -60. -30. 0. 30. 60. 90. 120. 150.\n",
" 180.]\n",
" [-180. -150. -120. -90. -60. -30. 0. 30. 60. 90. 120. 150.\n",
" 180.]\n",
" [-180. -150. -120. -90. -60. -30. 0. 30. 60. 90. 120. 150.\n",
" 180.]\n",
" [-180. -150. -120. -90. -60. -30. 0. 30. 60. 90. 120. 150.\n",
" 180.]\n",
" [-180. -150. -120. -90. -60. -30. 0. 30. 60. 90. 120. 150.\n",
" 180.]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"yep = np.flipud(np.swapaxes(ye, 0, 1))\n",
"xep = np.swapaxes(xe, 0, 1)\n",
"print('Python ordered y:\\n\\n', yep)\n",
"print('')\n",
"print('Python ordered x:\\n\\n', xep)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert back to ocgis corners."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[-180. -180. -150. -150.]\n",
" [-180. -180. -150. -150.]]\n",
"\n",
" [[-150. -150. -120. -120.]\n",
" [-150. -150. -120. -120.]]]\n"
]
}
],
"source": [
"from ocgis.util.helpers import create_ocgis_corners_from_esmf_corners as coc\n",
"\n",
"ocx = coc(xe)\n",
"print(ocx[0:2, 0:2, :])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment