Created
June 2, 2017 08:09
-
-
Save cpelley/1888fdceb5aa4cde8ddd47cab5acae90 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": true | |
}, | |
"outputs": [], | |
"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" | |
] | |
}, | |
{ | |
"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": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Now supply the source and target lat lons to ESMF as well as the source data for regridding..." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"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