Skip to content

Commit

Permalink
clean up and map avg
Browse files Browse the repository at this point in the history
  • Loading branch information
henchc committed Oct 10, 2017
1 parent 0c8caf9 commit ec5b027
Showing 1 changed file with 37 additions and 204 deletions.
241 changes: 37 additions & 204 deletions 01-BNHM-Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -92,9 +92,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World!\n"
]
}
],
"source": [
"print(\"Hello, World!\")"
]
Expand All @@ -108,9 +116,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"👋, 🌏!\n"
]
}
],
"source": [
"print(\"\\N{WAVING HAND SIGN}, \\N{EARTH GLOBE ASIA-AUSTRALIA}!\")"
]
Expand All @@ -128,9 +144,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 + 2\n",
"\n",
Expand Down Expand Up @@ -1215,210 +1242,16 @@
"metadata": {},
"outputs": [],
"source": [
"ca_df[station_obs].plot()\n",
"plt.plot(tmax_means)\n",
"plt.plot(ca_df[station_obs])\n",
"\n",
"# Use matplotlib to title your plot.\n",
"plt.title('Argia agrioides and temperatures in Santa Cruz Island')\n",
"\n",
"# Use matplotlib to add labels to the x and y axes of your plot.\n",
"plt.xlabel('Year', fontsize=18)\n",
"plt.ylabel('Degrees (Fahrenheit)', fontsize=16)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can then take all the records for which we have climate data and grab the coordinates:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Filter GBIF records where we have climate data.\n",
"records_rvals = [rec for rec in records_g if rec['gbifID'] in tmax_means.index]\n",
"coords = [(r['decimalLongitude'], r['decimalLatitude']) for r in records_rvals]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can make a heat map of these temperatures:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xs2, ys2 = zip(*coords)\n",
"norm = mpl.colors.Normalize()\n",
"\n",
"# Create an array of the sizes of points to be plotted in a scatterplot of occurrence locations\n",
"size = np.pi * (15 * norm(tmax_means)) ** 2\n",
"# Create scatter plot below.\n",
"plt.scatter(xs2, ys2, c=tmax_means, s=size, cmap='viridis', alpha=0.5)\n",
"plt.colorbar()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also make a box plot for each record over all the years:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ca_df.iloc[:,:7].plot.box()\n",
"plt.title('Argia agrioides - %s' % req.slug)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Taking a look at minimum temperatures now, here are the temperature distributions by decade."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dfs = []\n",
"names = ('tasmin_year_CanESM2_rcp45', 'tasmin_year_CanESM2_rcp85')\n",
"for name in names:\n",
" req = CalAdaptRequest(name)\n",
" dfs.append(req.concat_features(records_g, 'gbifID'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for name, df in zip(names, dfs):\n",
" t = df.resample('10A').mean().transpose()\n",
" t.columns = t.columns.year\n",
" t.plot.box()\n",
" plt.title(name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make plots for historical and projected temperatures at three station locations. Once again, Pandas gives us a lot of functionality in displaying our data beautifully. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# station_urls = {\n",
"# 'Blodgett': 'https://raw.githubusercontent.com/BNHM/spatial-layers/master/wkt/BlodgettForestResearchStation.wkt',\n",
"# 'Hopland': 'https://raw.githubusercontent.com/BNHM/spatial-layers/master/wkt/HoplandREC.wkt',\n",
"# 'Sagehen': 'https://raw.githubusercontent.com/BNHM/spatial-layers/master/wkt/SagehenCreekFieldStation.wkt'\n",
"# }\n",
"# stn_features = [{'id': name, 'geometry': wkt.loads(requests.get(url).text)}\n",
"# for name, url in station_urls.items()]\n",
"# Reduce geometric complexity, we need smaller geometry serializations to circumvent \n",
"# URL length restrictions.\n",
"tol = .0001\n",
"for feat in reserves['features']:\n",
" feat['geometry'] = feat['geometry']['coordinates'].buffer(tol).simplify(tol)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stn_features"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dfstns_lst = []\n",
"names = ('tasmax_year_CanESM2_historical', 'tasmax_year_CanESM2_rcp85')\n",
"# Iterate through each name and make requests using the CalAdapt API. We can also use Pandas to display this data efficiently.\n",
"for name in names:\n",
" req = CalAdaptRequest(name)\n",
" d = req.concat_features(stn_features)\n",
" dfstns_lst.append(d)\n",
"\n",
"dfstns = pd.concat(dfstns_lst)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use .describe() to display summary statistics of dfstns.\n",
"dfstns.describe()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use dfstns to create a line graph.\n",
"dfstns.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we'll print the decadal average and difference from 20 years prior."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dfstns.resample('10A').mean().diff(periods=2).dropna()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dfstns.resample('10A').mean().diff().plot()"
]
}
],
"metadata": {
Expand Down

0 comments on commit ec5b027

Please sign in to comment.