Skip to content

Instantly share code, notes, and snippets.

@JiaweiZhuang
Last active October 19, 2017 18:34
Show Gist options
  • Save JiaweiZhuang/9bbb3f5eba5ff0a88a167cc619f2a5e7 to your computer and use it in GitHub Desktop.
Save JiaweiZhuang/9bbb3f5eba5ff0a88a167cc619f2a5e7 to your computer and use it in GitHub Desktop.
Bugging esmpy 7.1.0.dev35
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Switch to Python 3 syntax because we are using Python2.7\n",
"# from https://hub.docker.com/r/continuumio/miniconda/\n",
"from __future__ import (absolute_import, division,\n",
" print_function, unicode_literals)\n",
"\n",
"import numpy as np\n",
"import xarray as xr\n",
"import ESMF"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ESMPyManager:\n",
" local_pet = 0\n",
" pet_count = 1\n",
")"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ESMF.Manager(debug=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create source and destination grids"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The source grid has 20x20 = 400 grid cells and the destination grid has 10x10 = 100 grid cells. They cover the same spatial domain."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"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(np.linspace(-120, 120, 20), \n",
" np.linspace(-60, 60, 20))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"destgrid = ESMF.Grid(np.array([10,10]), \n",
" staggerloc = ESMF.StaggerLoc.CENTER,\n",
" coord_sys = ESMF.CoordSys.SPH_DEG)\n",
"\n",
"dest_lon = destgrid.get_coords(0)\n",
"dest_lat = destgrid.get_coords(1)\n",
"dest_lon[...], dest_lat[...] = np.meshgrid(np.linspace(-120, 120, 10),\n",
" np.linspace(-60, 60, 10))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sourcefield = ESMF.Field(sourcegrid)\n",
"destfield = ESMF.Field(destgrid)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Write offline regridding weights"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"regrid_offline = ESMF.Regrid(sourcefield, destfield, filename='weights.nc',\n",
" regrid_method = ESMF.RegridMethod.BILINEAR,\n",
" unmapped_action = ESMF.UnmappedAction.IGNORE)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Open the weight file"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (n_s: 400)\n",
"Dimensions without coordinates: n_s\n",
"Data variables:\n",
" S (n_s) float64 1.0 0.0 0.0 0.0 0.8887 0.1113 -3.947e-16 ...\n",
" col (n_s) int32 1 1 2 1 21 1 22 1 3 2 4 2 23 2 24 2 5 3 6 3 25 3 26 ...\n",
" row (n_s) int32 1 2 1 21 1 22 1 3 2 4 2 23 2 24 2 5 3 6 3 25 3 26 3 ..."
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds = xr.open_dataset('weights.nc')\n",
"ds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"According to ESMF documentation [12.7.3 Regrid Calculation Output](http://www.earthsystemmodeling.org/esmf_releases/last_built/ESMF_refdoc/node3.html#SECTION03027300000000000000), the variable meanings are\n",
"\n",
" n_s: The number of entries in the regridding matrix.\n",
" col: The position in the source grid for each entry in the regridding matrix.\n",
" row: The position in the destination grid for each entry in the weight matrix.\n",
" S: The weight for each entry in the regridding matrix."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Fortran code for applying weights is\n",
"\n",
" ! Apply weights\n",
" do i=1, n_s\n",
" dst_field(row(i))=dst_field(row(i))+S(i)*src_field(col(i))\n",
" enddo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This means, the variable `col` should take integers from 1 to the number of grid boxes in the source grid (400 here), and the variable `row` should take integers from 1 to the destination grid number (100 here). \n",
"\n",
"**However, the ranges of `col` and `row` are wrong:**"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"col = ds['col'].values\n",
"row = ds['row'].values"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 200)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"col.min(), col.max() # should have been 1~400"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 201)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"row.min(), row.max() # should have been 1~100"
]
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment