Skip to content

Instantly share code, notes, and snippets.

@niallrobinson
Created December 6, 2013 16:12
Show Gist options
  • Save niallrobinson/7827371 to your computer and use it in GitHub Desktop.
Save niallrobinson/7827371 to your computer and use it in GitHub Desktop.
cube common coord broadcaster
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"d = iris.load_cube('/data/local/nrobin/nacvl/rg/t_nacvl_rg.nc')\n",
"iris.coord_categorisation.add_month_number(d, 'time', 'month_number')\n",
"cl = d.aggregated_by('month_number', iris.analysis.MEAN)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print d\n",
"print d.coord('month_number')\n",
"print cl\n",
"print cl.coord('month_number')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"sea_water_potential_temperature / (degC) (time: 24; grid_latitude: 200; grid_longitude: 400; depth: 46)\n",
" Dimension coordinates:\n",
" time x - - -\n",
" grid_latitude - x - -\n",
" grid_longitude - - x -\n",
" depth - - - x\n",
" Auxiliary coordinates:\n",
" month_number x - - -\n",
" Attributes:\n",
" Conventions: CF-1.5\n",
" history: 04/12/13 15:49:05 Iris: Converted from Geodetic to PlateCarree\n",
"AuxCoord(array([12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4,\n",
" 5, 6, 7, 8, 9, 10, 11]), standard_name=None, units=Unit('1'), long_name=u'month_number', attributes={'time_origin': ' 2000-DEC-01 00:00:00', 'title': 'Time'})\n",
"sea_water_potential_temperature / (degC) (time: 12; grid_latitude: 200; grid_longitude: 400; depth: 46)\n",
" Dimension coordinates:\n",
" time x - - -\n",
" grid_latitude - x - -\n",
" grid_longitude - - x -\n",
" depth - - - x\n",
" Auxiliary coordinates:\n",
" month_number x - - -\n",
" Attributes:\n",
" Conventions: CF-1.5\n",
" history: 04/12/13 15:49:05 Iris: Converted from Geodetic to PlateCarree\n",
" Cell methods:\n",
" mean: month_number\n",
"AuxCoord(array([12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]), standard_name=None, units=Unit('1'), long_name=u'month_number', attributes={'time_origin': ' 2000-DEC-01 00:00:00', 'title': 'Time'})\n"
]
}
],
"prompt_number": 77
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import iris\n",
"import numpy as np\n",
"import iris.coord_categorisation\n",
"\n",
"def bc(aux_cube, dim_cube, comparable_coord):\n",
" if type(aux_cube.coord(comparable_coord)) is not iris.coords.AuxCoord:\n",
" raise TypeError\n",
" if type(dim_cube.coord(comparable_coord)) is not iris.coords.AuxCoord:\n",
" raise TypeError\n",
" \n",
" aux_cube_coord = aux_cube.coord(comparable_coord)\n",
" dim_cube_coord = dim_cube.coord(comparable_coord)\n",
" aux_cube_dim, = aux_cube.coord_dims(aux_cube_coord)\n",
" dim_cube_dim, = aux_cube.coord_dims(dim_cube_coord)\n",
" \n",
" s_aux_cube = [slice(None)]*len(aux_cube.shape)\n",
" s_aux_cube[aux_cube_dim] = 0\n",
" s_dim_cube = [slice(None)]*len(dim_cube.shape)\n",
" s_dim_cube[dim_cube_dim] = 0\n",
" a = aux_cube[tuple(s_aux_cube)]\n",
" a.attributes = None\n",
" a.cell_methods = None\n",
" b = dim_cube[tuple(s_dim_cube)]\n",
" b.attributes = None\n",
" b.cell_methods = None\n",
" if not a.is_compatible(b):\n",
" iris.util.describe_diff(a, b)\n",
" raise RuntimeError(\"Cubes are not compatible\")\n",
" \n",
" ind = []\n",
" for p in aux_cube.coord(comparable_coord).points:\n",
" i = np.where(dim_cube.coord(comparable_coord).points == p)\n",
" ind.append(i[0][0])\n",
"\n",
" s = [slice(None)]*len(dim_cube.shape)\n",
" s[dim_cube_dim] = ind\n",
" new_data = dim_cube.data[tuple(s)]\n",
" new_cube = aux_cube.copy()\n",
" new_cube.data = new_data\n",
" new_cube.history = \"%s comparable to %s in terms of %s\" % (dim_cube.name(),\n",
" aux_cube.name(),\n",
" comparable_coord)\n",
" \n",
" return new_cube"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 74
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"new_cube = bc(d, cl, 'month_number')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 78
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print new_cube"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"sea_water_potential_temperature / (degC) (time: 24; grid_latitude: 200; grid_longitude: 400; depth: 46)\n",
" Dimension coordinates:\n",
" time x - - -\n",
" grid_latitude - x - -\n",
" grid_longitude - - x -\n",
" depth - - - x\n",
" Auxiliary coordinates:\n",
" month_number x - - -\n",
" Attributes:\n",
" Conventions: CF-1.5\n",
" history: 04/12/13 15:49:05 Iris: Converted from Geodetic to PlateCarree\n"
]
}
],
"prompt_number": 79
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print new_cube[:, 100, 100, 0].data\n",
"print cl[:, 100, 100, 0].data"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 26.38871956 27.34636974 28.21290207 28.87269211 28.62853909\n",
" 27.45426464 26.37894917 25.82944107 25.69618034 25.23513889\n",
" 25.72982407 25.60594463 26.38871956 27.34636974 28.21290207\n",
" 28.87269211 28.62853909 27.45426464 26.37894917 25.82944107\n",
" 25.69618034 25.23513889 25.72982407 25.60594463]\n",
"[ 26.38871956 27.34636974 28.21290207 28.87269211 28.62853909\n",
" 27.45426464 26.37894917 25.82944107 25.69618034 25.23513889\n",
" 25.72982407 25.60594463]\n"
]
}
],
"prompt_number": 80
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"anom_cube = d = new_cube"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 81
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print anom_cube"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"sea_water_potential_temperature / (degC) (time: 24; grid_latitude: 200; grid_longitude: 400; depth: 46)\n",
" Dimension coordinates:\n",
" time x - - -\n",
" grid_latitude - x - -\n",
" grid_longitude - - x -\n",
" depth - - - x\n",
" Auxiliary coordinates:\n",
" month_number x - - -\n",
" Attributes:\n",
" Conventions: CF-1.5\n",
" history: 04/12/13 15:49:05 Iris: Converted from Geodetic to PlateCarree\n"
]
}
],
"prompt_number": 83
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment