Skip to content

Instantly share code, notes, and snippets.

@AlexHilson
Created May 4, 2017 12:25
Show Gist options
  • Save AlexHilson/1dd15cf17cb05b52f73e217ee9f11e18 to your computer and use it in GitHub Desktop.
Save AlexHilson/1dd15cf17cb05b52f73e217ee9f11e18 to your computer and use it in GitHub Desktop.
Simple mean example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert each cube in the dataset into its mean value. Not particuarly useful but it requires a bunch of numpy calculations + all of the data to be loaded so it's a decent test."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<Client: scheduler=\"172.31.18.5:8786\" processes=40 cores=40>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import iris\n",
"from distributed import Client\n",
"\n",
"c = Client('172.31.18.5:8786')\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cubes = c.get_dataset('remote_mogreps')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(<iris 'Cube' of wet_bulb_freezing_level_altitude / (m) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of wet_bulb_potential_temperature / (K) (time: 2; pressure: 3; latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of air_pressure_at_sea_level / (Pa) (time: 2; latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of air_temperature / (K) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of air_temperature / (K) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of air_temperature / (K) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of air_temperature / (K) (time: 2; pressure: 16; latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of dew_point_temperature / (K) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of fog_area_fraction / (1) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of geopotential_height / (m) (time: 2; pressure: 9; latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of high_type_cloud_area_fraction / (1) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of low_type_cloud_area_fraction / (1) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of medium_type_cloud_area_fraction / (1) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of relative_humidity / (%) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of relative_humidity / (%) (time: 2; pressure: 16; latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of stratiform_rainfall_amount / (kg m-2) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of stratiform_snowfall_amount / (kg m-2) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of surface_downward_eastward_stress / (Pa) (latitude: 600; longitude: 800)>,\n",
" <iris 'Cube' of surface_downward_northward_stress / (Pa) (latitude: 601; longitude: 800)>,\n",
" <iris 'Cube' of visibility_in_air / (m) (latitude: 600; longitude: 800)>)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cubes.take(20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This sample analysis requires all of the data to be loaded. If working with s3fs mounted data this process is either borked or it takes so long that I've never had the patience to see it through. If the data is already loaded onto the nodes it's much more reasonable, but it still seems temperamental when shuffling cubes around."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def meaner(cube):\n",
" cube.collapsed(['latitude', 'longitude'], iris.analysis.MEAN)\n",
" for coord in cube.dim_coords:\n",
" cube = cube.collapsed(coord.name(), iris.analysis.MEAN)\n",
" return cube.data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"meaned_cubes = cubes.map(meaner)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(array(1734.5778125),\n",
" array(286.42127591145834),\n",
" array(100690.60130000001),\n",
" array(277.743828125),\n",
" array(278.213203125),\n",
" array(278.60755859375),\n",
" array(240.6882495686849),\n",
" array(274.6958984375),\n",
" array(0.01849791646003723),\n",
" array(5433.2906414351855))"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"meaned_cubes.take(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment