-
-
Save pletzer/43fc5be751f0be3e5a2b72db89486b75 to your computer and use it in GitHub Desktop.
transforming cell corners for ESMF usage
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": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/home/pletzera/anaconda2/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7.egg/iris/fileformats/grib/__init__.py:59: IrisDeprecation: The module iris.fileformats.grib is deprecated since v1.10. Please install the package 'iris_grib' package instead.\n", | |
" \"The module iris.fileformats.grib is deprecated since v1.10. \"\n" | |
] | |
} | |
], | |
"source": [ | |
"import copy\n", | |
"import iris\n", | |
"import ants.tests.stock as stock\n", | |
"import ants\n", | |
"import cartopy.crs as ccrs\n", | |
"import numpy as np\n", | |
"from ants.regrid import _ESMFRegridder" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Lets create a fictitious source.\n", | |
"source = stock.geodetic((4, 4), with_bounds=True)\n", | |
"sx_coord, sy_coord = ants.utils.cube.horizontal_grid(source)\n", | |
"\n", | |
"# Lets create a fictitious target by copying the source and replacing the coordinate system with a rotated one.\n", | |
"target = source.copy()\n", | |
"tx_coord, ty_coord = ants.utils.cube.horizontal_grid(target)\n", | |
"tx_coord.coord_system = iris.coord_systems.GeogCS(semi_major_axis=6378137.0, semi_minor_axis=6356752.314245179)\n", | |
"ty_coord.coord_system = copy.copy(tx_coord.coord_system)\n", | |
"ty_coord.standard_name = 'grid_latitude'\n", | |
"tx_coord.standard_name = 'grid_longitude'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# True lat-lon crs\n", | |
"latlon_crs = ccrs.Geodetic()\n", | |
"\n", | |
"# Transform the coordinates of both source and target (which could be any crs) to a true lat-lon crs for usage with ESMF.\n", | |
"src_crs = sx_coord.coord_system.as_ants_crs().as_cartopy_crs()\n", | |
"tgt_crs = tx_coord.coord_system.as_ants_crs().as_cartopy_crs()\n", | |
"\n", | |
"# Get cell corners of the source as true lat lon points\n", | |
"x_bounds, y_bounds = np.meshgrid(sx_coord.bounds, sy_coord.bounds)\n", | |
"xyz = latlon_crs.transform_points(src_crs, x_bounds, y_bounds)\n", | |
"sx, sy = xyz[..., 0], xyz[..., 1]\n", | |
"\n", | |
"# Get cell corners of the target as true lat lon points\n", | |
"x_bounds, y_bounds = np.meshgrid(tx_coord.bounds, ty_coord.bounds)\n", | |
"xyz = latlon_crs.transform_points(tgt_crs, x_bounds, y_bounds)\n", | |
"tx, ty = xyz[..., 0], xyz[..., 1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Now supply the source and target lat lons to ESMF as well as the source data for regridding\n", | |
"regridder = _ESMFRegridder(source, target, method='conserve')\n", | |
"# Initialise the target data\n", | |
"target.data[...] = -1\n", | |
"# Apply the regridding weights\n", | |
"target = regridder(source)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0 1 2 3]\n", | |
" [ 4 5 6 7]\n", | |
" [ 8 9 10 11]\n", | |
" [12 13 14 15]]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Check\n", | |
"print(target.data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [Root]", | |
"language": "python", | |
"name": "Python [Root]" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment