Last active
November 4, 2016 01:17
-
-
Save mbertrand/e896cb8bec67b44c858f2740a91e18c0 to your computer and use it in GitHub Desktop.
IMED2016 RasterStats Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import geopandas\n", | |
"import json\n", | |
"from rasterstats import gen_zonal_stats\n", | |
"from datetime import date\n", | |
"from dateutil.relativedelta import relativedelta" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"countries = '/Users/mbertrand/Downloads/data/ne_10m_admin_0_countries/ne_10m_admin_0_countries.shp'\n", | |
"airtemp = '/Volumes/Seagate/imed2016/UDELAWARE/air.mon.mean.v401.ll.PM1994_2014.tif'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Band 1 is January 1994\n", | |
"band_date = date(1994,1,1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Processing band 192 for 201001\n", | |
"Processing band 193 for 201002\n", | |
"Processing band 194 for 201003\n" | |
] | |
} | |
], | |
"source": [ | |
"# Create a dict of geopandas dataframes containing country info + mean air temp for the first 3 months of 2010\n", | |
"\n", | |
"band2010_start = (2010-1994)*12\n", | |
"band2010_end = band2010_start + 3\n", | |
"\n", | |
"df_dict = {}\n", | |
"for i in range(band2010_start,band2010_end):\n", | |
" timestamp = (band_date + relativedelta(months=i)).strftime('%Y%m')\n", | |
" print ('Processing band {} for {}'.format(i, timestamp))\n", | |
" features = zonal_stats(countries, airtemp, band=i, stats=['mean'], geojson_out=True)\n", | |
" df_dict[timestamp] = geopandas.GeoDataFrame.from_features(features)\n", | |
" # Save to file for future reference\n", | |
" with open('countries_air_{}.geojson'.format(timestamp), 'w') as f:\n", | |
" f.write(df_dict[timestamp].to_json())\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[-2.5102562537560096, -5.01025625375601, -1.964102524977464]\n" | |
] | |
} | |
], | |
"source": [ | |
"# Get a list of mean air temperatures in Austria for each month\n", | |
"\n", | |
"austria_temps = [df[df['NAME'] == 'Austria']['mean'].values[0] for df in df_dict.values()]\n", | |
"print austria_temps " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment