Skip to content

Instantly share code, notes, and snippets.

@JiaweiZhuang
Created November 1, 2017 02:02
Show Gist options
  • Save JiaweiZhuang/ab8aa96253146a667c14059414e3f936 to your computer and use it in GitHub Desktop.
Save JiaweiZhuang/ab8aa96253146a667c14059414e3f936 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ESMPy grid area calculation test"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'7.1.0 beta snapshot'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import ESMF\n",
"ESMF.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Background\n",
"\n",
"From [ESMPy documentation](http://www.earthsystemmodeling.org/esmf_releases/last_built/esmpy_doc/html/grid.html#ESMF.api.grid.Grid), the `grid` class should have area information:\n",
"\n",
" add_user_area (bool) – Set to True to read in the cell area from the grid file;\n",
" otherwise, ESMF will calculate it. Defaults to False."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make fake grid"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([-114., -102., -90., -78., -66., -54., -42., -30., -18.,\n",
" -6., 6., 18., 30., 42., 54., 66., 78., 90.,\n",
" 102., 114.]),\n",
" array([-120., -108., -96., -84., -72., -60., -48., -36., -24.,\n",
" -12., 0., 12., 24., 36., 48., 60., 72., 84.,\n",
" 96., 108., 120.]),\n",
" array([-57., -51., -45., -39., -33., -27., -21., -15., -9., -3., 3.,\n",
" 9., 15., 21., 27., 33., 39., 45., 51., 57.]),\n",
" array([-60., -54., -48., -42., -36., -30., -24., -18., -12., -6., 0.,\n",
" 6., 12., 18., 24., 30., 36., 42., 48., 54., 60.]))"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lon = np.linspace(-114, 114, 20)\n",
"lon_b = np.linspace(-120, 120, 21)\n",
"lat = np.linspace(-57, 57, 20) # corners\n",
"lat_b = np.linspace(-60, 60, 21)\n",
"lon, lon_b, lat, lat_b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create Grid object"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"sourcegrid = ESMF.Grid(np.array([20,20]), \n",
" staggerloc = ESMF.StaggerLoc.CENTER,\n",
" coord_sys = ESMF.CoordSys.SPH_DEG)\n",
"\n",
"source_lon = sourcegrid.get_coords(0)\n",
"source_lat = sourcegrid.get_coords(1)\n",
"source_lon[...], source_lat[...] = np.meshgrid(lon, lat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add corners"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"sourcegrid.add_coords(staggerloc = ESMF.StaggerLoc.CORNER_VCENTER)\n",
"\n",
"source_lat_b = sourcegrid.get_coords(coord_dim = 1, \n",
" staggerloc = ESMF.StaggerLoc.CORNER)\n",
"source_lon_b = sourcegrid.get_coords(coord_dim = 0, \n",
" staggerloc = ESMF.StaggerLoc.CORNER)\n",
"\n",
"source_lon_b[...], source_lat_b[...] = np.meshgrid(lon_b, lat_b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check area"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Doesn't exist..."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[None, None, None, None]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sourcegrid.area"
]
}
],
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment