Skip to content

Instantly share code, notes, and snippets.

@leifdenby
Created July 13, 2018 09:49
Show Gist options
  • Save leifdenby/a91b616ce5fb077e44d28556208626b4 to your computer and use it in GitHub Desktop.
Save leifdenby/a91b616ce5fb077e44d28556208626b4 to your computer and use it in GitHub Desktop.
xESMF - Input array is not F_CONTIGUOUS issue
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import xesmf\n",
"import xarray as xr\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# (i,j) indexing in input grid"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (x: 200, y: 100)\n",
"Coordinates:\n",
" * x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...\n",
" * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...\n",
"Data variables:\n",
" lat (x, y) float64 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 ...\n",
" lon (x, y) float64 20.0 20.1 20.2 20.3 20.4 20.5 20.6 20.7 20.8 ..."
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lat_ = np.arange(10, 30, 0.1)\n",
"lon_ = np.arange(20, 30, 0.1)\n",
"\n",
"lat, lon = np.meshgrid(lat_, lon_, indexing='ij')\n",
"\n",
"x = np.arange(len(lat_))\n",
"y = np.arange(len(lon_))\n",
"\n",
"ds_in = xr.Dataset(coords=dict(x=x, y=y))\n",
"ds_in['lat'] = (('x', 'y'), lat)\n",
"ds_in['lon'] = (('x', 'y'), lon)\n",
"\n",
"ds_in"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"lat_new = np.arange(10, 30, 1.)\n",
"lon_new = np.arange(20, 30, 1.)\n",
"\n",
"ds_out = xr.Dataset(coords=dict(lat=lat_new, lon=lon_new))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Create weight file: bilinear_200x100_20x10.nc\n"
]
}
],
"source": [
"regridder = xesmf.Regridder(ds_in=ds_in, ds_out=ds_out, method='bilinear')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# (x,y) indexing in input grid"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (x: 200, y: 100)\n",
"Coordinates:\n",
" * x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...\n",
" * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...\n",
"Data variables:\n",
" lat (x, y) float64 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 ...\n",
" lon (x, y) float64 20.0 20.1 20.2 20.3 20.4 20.5 20.6 20.7 20.8 ..."
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lat_ = np.arange(10, 30, 0.1)\n",
"lon_ = np.arange(20, 30, 0.1)\n",
"\n",
"lat, lon = np.meshgrid(lat_, lon_, indexing='xy')\n",
"\n",
"x = np.arange(len(lat_))\n",
"y = np.arange(len(lon_))\n",
"\n",
"ds_in = xr.Dataset(coords=dict(x=x, y=y))\n",
"ds_in['lat'] = (('x', 'y'), lat.T)\n",
"ds_in['lon'] = (('x', 'y'), lon.T)\n",
"\n",
"ds_in"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"lat_new = np.arange(10, 30, 1.)\n",
"lon_new = np.arange(20, 30, 1.)\n",
"\n",
"ds_out = xr.Dataset(coords=dict(lat=lat_new, lon=lon_new))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwrite existing file: bilinear_200x100_20x10.nc \n",
" You can set reuse_weights=True to save computing time.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/earlcd/anaconda2/envs/ghana-training/lib/python3.6/site-packages/xesmf/backend.py:36: UserWarning: Input array is not F_CONTIGUOUS. Will affect performance.\n",
" warnings.warn(\"Input array is not F_CONTIGUOUS. \"\n"
]
}
],
"source": [
"regridder = xesmf.Regridder(ds_in=ds_in, ds_out=ds_out, method='bilinear')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:ghana-training]",
"language": "python",
"name": "conda-env-ghana-training-py"
},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment