Created
August 29, 2018 14:45
-
-
Save JiaweiZhuang/1398d6e5daaab846f3d477feb7fdb4c7 to your computer and use it in GitHub Desktop.
Test xgcm generate_grid_ds on cubed-sphere grid (2D curvilinear)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"```\n", | |
"pip install git+https://github.com/JiaweiZhuang/cubedsphere.git\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import cubedsphere as cs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_keys(['lon', 'lat', 'lon_b', 'lat_b'])" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"csgrid = cs.csgrid_GMAO(12)\n", | |
"csgrid.keys()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"lon (6, 12, 12)\n", | |
"lat (6, 12, 12)\n", | |
"lon_b (6, 13, 13)\n", | |
"lat_b (6, 13, 13)\n" | |
] | |
} | |
], | |
"source": [ | |
"for k in csgrid:\n", | |
" print(k, csgrid[k].shape) # 6 is the number of cubed-sphere faces" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def test_bound_2d(center, bnd, decimal=1):\n", | |
" center_inferred = 0.25*(bnd[1:,1:] + bnd[1:,:-1]+bnd[:-1,1:] + bnd[:-1,:-1])\n", | |
" \n", | |
" # don't be too strict. The center of a cuvilinear cell is not strictly equal to the average of the 4 corners\n", | |
" # just use a low-precision checking here\n", | |
" np.testing.assert_almost_equal(center, center_inferred, decimal=decimal)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# test a single panel\n", | |
"test_bound_2d(csgrid['lat'][0], csgrid['lat_b'][0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# test all panels\n", | |
"for i in range(6):\n", | |
" # decimal=1 will fail at a few cells\n", | |
" test_bound_2d(csgrid['lat'][i], csgrid['lat_b'][i], decimal=0)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"TODO: replace `csgrid['lat_b']` by auto-generated boundaries generated by xgcm.\n", | |
"\n", | |
"NOTE: `lon` is tricker to check because any `lon+360*n` should be considered as equal to `lon`. A more robust comparison should be done in cartesian `(x, y, z)` space." | |
] | |
} | |
], | |
"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.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment