Skip to content

Instantly share code, notes, and snippets.

@cpelley
Created November 3, 2014 09:38
Show Gist options
  • Save cpelley/a9fb732b4c7aa2437391 to your computer and use it in GitHub Desktop.
Save cpelley/a9fb732b4c7aa2437391 to your computer and use it in GitHub Desktop.
Bug with save-load sequence. Inverse flattening representation changes between save-load, resulting in a failled equality condition. Also demonstrates the lack of support for a scalar cube.
{
"metadata": {
"name": "",
"signature": "sha256:563b350202cce7ea8c83bbb25c227c1bdcef905445af72e7b040121fc52884b8"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Bug with save-load sequence with inverse flattening"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Result of this is that equality condition will fail."
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import shutil\n",
"import tempfile\n",
"\n",
"\n",
"class TemporaryDirectory(object):\n",
" \"\"\"\n",
" Context manager for tempfile.mkdtemp().\n",
"\n",
" This class is available in python +v3.2.\n",
"\n",
" \"\"\"\n",
" def __enter__(self):\n",
" self.dir_name = tempfile.mkdtemp()\n",
" return self.dir_name\n",
" \n",
" def __exit__(self, exc_type, exc_value, traceback):\n",
" shutil.rmtree(self.dir_name)\n",
" \n",
" \n",
"TemporaryDirectory = getattr(tempfile, 'TemporaryDirectory',\n",
" TemporaryDirectory)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import iris\n",
"\n",
"\n",
"cube = iris.cube.Cube([1])\n",
"airy1830 = iris.coord_systems.GeogCS(semi_major_axis=6377563.396, inverse_flattening=299.3249646)\n",
"x_coord = iris.coords.DimCoord(1, standard_name='longitude',\n",
" units='degree_east',\n",
" coord_system=airy1830)\n",
"cube.add_dim_coord(x_coord, 0)\n",
"x1_coord_system = x_coord.coord_system"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import os\n",
"\n",
"with TemporaryDirectory() as fdir:\n",
" fnme = os.path.join(fdir, 'tmp.nc')\n",
" iris.save(cube, fnme)\n",
" cube2 = iris.load_cube(fnme)\n",
" x2_coord_system = cube2.coords()[0].coord_system"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"unknown / (1) (longitude: 1)\n",
" Dimension coordinates:\n",
" longitude x\n",
" Attributes:\n",
" Conventions: CF-1.5\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print x1_coord_system == x2_coord_system\n",
"print x1_coord_system.inverse_flattening, x2_coord_system.inverse_flattening\n",
"print x1_coord_system.inverse_flattening == x2_coord_system.inverse_flattening"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"False\n",
"299.3249646 299.3249646\n",
"False\n"
]
}
],
"prompt_number": 39
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Scalar cube failure save-load sequence failure"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cube = iris.cube.Cube(1, long_name='scalar_cube')\n",
"airy1830 = iris.coord_systems.GeogCS(semi_major_axis=6377563.396, inverse_flattening=299.3249646)\n",
"x_coord = iris.coords.DimCoord(1, standard_name='longitude',\n",
" units='degree_east',\n",
" coord_system=airy1830)\n",
"cube.add_aux_coord(x_coord, None)\n",
"x1_coord_system = x_coord.coord_system\n",
"\n",
"print cube"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"scalar_cube / (unknown) (scalar cube)\n",
" Scalar coordinates:\n",
" longitude: 1 degree_east\n"
]
}
],
"prompt_number": 44
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"with TemporaryDirectory() as fdir:\n",
" fnme = os.path.join(fdir, 'tmp.nc')\n",
" iris.save(cube, fnme)\n",
" cube2 = iris.load_cube(fnme)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ConstraintMismatchError",
"evalue": "no cubes found",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mConstraintMismatchError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-45-18b491f13e36>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mfnme\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfdir\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'tmp.nc'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0miris\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msave\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcube\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfnme\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mcube2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0miris\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mload_cube\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfnme\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m/home/h05/cpelley/git/iris/lib/iris/__init__.pyc\u001b[0m in \u001b[0;36mload_cube\u001b[1;34m(uris, constraint, callback)\u001b[0m\n\u001b[0;32m 332\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0miris\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexceptions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mConstraintMismatchError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 333\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 334\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0miris\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexceptions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mConstraintMismatchError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'no cubes found'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 335\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 336\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mcube\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mConstraintMismatchError\u001b[0m: no cubes found"
]
}
],
"prompt_number": 45
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment