{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"2020-11-24 - class #15 - activities.ipynb","provenance":[],"collapsed_sections":["OTxnF4FcnyCh","Nzy8oBSAszAS","nPKNFP1qwrH9","ydkA4VMmwrgb","EfBCqB9Ewrq4","1Nb0s-s6S2jt","9IE6wGbNNhI0","2Pv0K7MYMtKx"]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"BRAp37uklN9X"},"source":["# Class \\#15 activities"]},{"cell_type":"markdown","metadata":{"id":"OTxnF4FcnyCh"},"source":["# **Detecting sea level rise from Florida tide gauge records**\n","### Practice with Pandas time series analysis"]},{"cell_type":"markdown","metadata":{"id":"3YihuiaSoCYK"},"source":["\n","\n","**Data description:** Two netCDF files with hourly tide gauge records from Key West, FL (from 1913 to present) and Pensacola, FL (from 1923 to present). The tide gauge measurements are of relative sea level (RSL), which includes both sea level rise (from ice melt and thermal expansion) and local vertical land motion (from subsidence and isostatic rebound).\n","\n","**Data source:** [University of Hawaii Sea Level Center](https://uhslc.soest.hawaii.edu/datainfo/)"]},{"cell_type":"markdown","metadata":{"id":"P4f4df_NoXYA"},"source":["1. Uncomment the blocks of code with the installation lines (`!pip install`, etc.). Remember the shortcut to uncomment/comment multiple lines is `Command-/` on Macs or `Control-/` on Windows.\n","\n","2. Then run this cell to install the packages, import libraries, and give Colab access to Google Drive.\n","\n","3. When prompted, click the link to give Colab access to Google Drive, copy the code, and paste back into here."]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Cz1ypHghoXYA","executionInfo":{"status":"ok","timestamp":1606274859188,"user_tz":480,"elapsed":467,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"f3ae4e4b-adb4-4429-fe34-7cfa5803912e"},"source":["# # This code installs the netCDF4 module\n","# # Run this code once per session, then comment it out\n","# !pip install netcdf4\n","\n","# # This code allows Cartopy to work with Google Colab\n","# # Run this code once per session, then comment it out\n","# !grep '^deb ' /etc/apt/sources.list | \\\n","# sed 's/^deb /deb-src /g' | \\\n","# tee /etc/apt/sources.list.d/deb-src.list\n","# !apt-get -qq update\n","# !apt-get -qq build-dep python3-cartopy\n","# !pip uninstall -y shapely\n","# !pip install shapely --no-binary shapely\n","# !pip install cartopy\n","\n","# Import NumPy, xarray, Matplotlib, Cartopy (and related imports)\n","import numpy as np\n","import pandas as pd\n","from scipy import interpolate, stats\n","import xarray as xr\n","import matplotlib.pyplot as plt\n","from datetime import datetime, timedelta\n","\n","import cartopy.crs as ccrs\n","import cartopy.feature as cfeature\n","from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER\n","\n","# Give Colab access to Google Drive\n","from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":3,"outputs":[{"output_type":"stream","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"Rree4xFzoXYA"},"source":["4. Next, update the filepath below. To find the correct filepath, you can click on the left sidebar (folder icon), navigate to the Class #15 data folder, click the \"...\" on the file, and select \"Copy path.\"\n","\n","5. Add back slashes (`\\`) in front of quotation marks in the filepath, as necessary."]},{"cell_type":"code","metadata":{"id":"pV6oEXF2oXYA","executionInfo":{"status":"ok","timestamp":1606274861390,"user_tz":480,"elapsed":542,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}}},"source":["# Folder for tide gauge records\n","folder = '/content/drive/MyDrive/OCEAN 215 - Autumn \\'20/OCEAN 215 - Autumn \\'20 - Course documents/Zoom class slides and notebooks/2020-11-24 - class #15 - data/'"],"execution_count":4,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"uUZDLPdzoXYA"},"source":["6. Run the code cell below to load and process the netCDF files using `xarray`, then convert them to Pandas Series format."]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":611},"id":"VZR6BZlXoXYA","executionInfo":{"status":"ok","timestamp":1606274865986,"user_tz":480,"elapsed":4351,"user":{"displayName":"Ethan C Campbell","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GjCBYTiuomqOsCakND1k_5wj0kYvFY53Jt7kunt=s64","userId":"11255944928409084259"}},"outputId":"92ae89c9-cc33-44d8-fb56-830caf47dc0a"},"source":["# Load netCDF files using xarray\n","key_west_xr = xr.open_dataset(folder + 'tide_gauge_key_west_fl.nc')\n","key_west_xr = key_west_xr.drop('record_id').squeeze()\n","pensacola_xr = xr.open_dataset(folder + 'tide_gauge_pensacola_fl.nc')\n","pensacola_xr = pensacola_xr.drop('record_id').squeeze()\n","\n","# Display file\n","display(key_west_xr)\n","\n","# Convert each sea level record to Pandas Series\n","key_west = key_west_xr['sea_level'].to_pandas()\n","key_west.name = 'sea_level'\n","pensacola = pensacola_xr['sea_level'].to_pandas()\n","pensacola.name = 'sea_level'\n","\n","# Display new Pandas series\n","display(pensacola)"],"execution_count":5,"outputs":[{"output_type":"display_data","data":{"text/html":["
array(['1913-01-19T06:00:00.000000000', '1913-01-19T07:00:00.028800000',\n"," '1913-01-19T07:59:59.971200000', ..., '2020-09-30T21:00:00.000000000',\n"," '2020-09-30T22:00:00.028800000', '2020-09-30T22:59:59.971200000'],\n"," dtype='datetime64[ns]')
[944082 values with dtype=float32]
array(24.553, dtype=float32)
array(278.192, dtype=float32)
array(b'Key West, FL', dtype='|S12')
array(b'United States of America (the)', dtype='|S30')
array(840.)
array(242, dtype=int16)
array(216.)
array(b'kwfl', dtype='|S4')
array('2019-12-31T22:59:59.971200000', dtype='datetime64[ns]')
\n"," | Station ID | \n","Station Name | \n","First Year | \n","Last Year | \n","Year Range | \n","% Complete | \n","MSL Trends (mm/yr) | \n","+/- 95% CI (mm/yr) | \n","MSL Trend (ft/century) | \n","+/- 95% CI (ft/century) | \n","Latitude | \n","Longitude | \n","
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n","010-001 | \n","Reykjavik, Iceland | \n","1956 | \n","2018 | \n","63 | \n","0.9814 | \n","2.35 | \n","0.40 | \n","0.77 | \n","0.13 | \n","64.1500 | \n","-21.9333 | \n","
1 | \n","015-011 | \n","Torshavn, Denmark | \n","1957 | \n","2006 | \n","50 | \n","0.8378 | \n","1.80 | \n","0.40 | \n","0.59 | \n","0.13 | \n","62.0167 | \n","-6.7667 | \n","
2 | \n","025-001 | \n","Barentsburg, Norway | \n","1948 | \n","2018 | \n","71 | \n","0.9397 | \n","-2.65 | \n","0.45 | \n","-0.87 | \n","0.15 | \n","78.0667 | \n","14.2500 | \n","
3 | \n","025-021 | \n","Ny-Alesund, Norway | \n","1976 | \n","2018 | \n","43 | \n","0.8895 | \n","-4.69 | \n","0.68 | \n","-1.54 | \n","0.22 | \n","78.9285 | \n","11.9380 | \n","
4 | \n","030-003 | \n","Russkaya Gavan II, Russia | \n","1953 | \n","1993 | \n","41 | \n","0.9753 | \n","-0.54 | \n","1.05 | \n","-0.18 | \n","0.34 | \n","76.2000 | \n","62.5833 | \n","
... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","... | \n","
360 | \n","970-141 | \n","Churchill, Canada | \n","1940 | \n","2018 | \n","79 | \n","0.8997 | \n","-9.21 | \n","0.49 | \n","-3.02 | \n","0.16 | \n","58.7667 | \n","-94.1833 | \n","
361 | \n","970-162 | \n","Alert, Canada | \n","1969 | \n","2018 | \n","50 | \n","0.2906 | \n","-2.34 | \n","1.07 | \n","-0.77 | \n","0.35 | \n","82.4900 | \n","-62.3200 | \n","
362 | \n","970-211 | \n","Tuktoyaktuk, Canada | \n","1961 | \n","2018 | \n","58 | \n","0.4336 | \n","2.75 | \n","1.07 | \n","0.90 | \n","0.35 | \n","69.4167 | \n","-132.9667 | \n","
363 | \n","999-001 | \n","Bahia Esperanza, Antarctica | \n","1961 | \n","1993 | \n","33 | \n","0.3528 | \n","-4.82 | \n","2.45 | \n","-1.58 | \n","0.80 | \n","-63.3000 | \n","-56.9167 | \n","
364 | \n","999-003 | \n","Argentine Islands, Antarctica | \n","1958 | \n","2018 | \n","61 | \n","0.9795 | \n","1.29 | \n","0.35 | \n","0.42 | \n","0.11 | \n","-65.2500 | \n","-64.2667 | \n","
365 rows × 12 columns
\n","\n"," | First Year | \n","Last Year | \n","Year Range | \n","% Complete | \n","MSL Trends (mm/yr) | \n","+/- 95% CI (mm/yr) | \n","MSL Trend (ft/century) | \n","+/- 95% CI (ft/century) | \n","Latitude | \n","Longitude | \n","
---|---|---|---|---|---|---|---|---|---|---|
count | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","365.000000 | \n","
mean | \n","1942.945205 | \n","2013.268493 | \n","69.802740 | \n","0.874792 | \n","1.401123 | \n","0.692356 | \n","0.459836 | \n","0.226849 | \n","25.952370 | \n","23.535294 | \n","
std | \n","34.157719 | \n","9.238847 | \n","35.107676 | \n","0.144909 | \n","2.445547 | \n","0.603309 | \n","0.802217 | \n","0.198167 | \n","32.793653 | \n","82.813661 | \n","
min | \n","1807.000000 | \n","1969.000000 | \n","10.000000 | \n","0.290600 | \n","-9.210000 | \n","0.080000 | \n","-3.020000 | \n","0.030000 | \n","-65.250000 | \n","-173.233300 | \n","
25% | \n","1927.000000 | \n","2014.000000 | \n","46.000000 | \n","0.816800 | \n","0.670000 | \n","0.340000 | \n","0.220000 | \n","0.110000 | \n","6.980500 | \n","-8.666700 | \n","
50% | \n","1954.000000 | \n","2018.000000 | \n","58.000000 | \n","0.934500 | \n","1.540000 | \n","0.510000 | \n","0.510000 | \n","0.170000 | \n","37.083300 | \n","15.550000 | \n","
75% | \n","1968.000000 | \n","2018.000000 | \n","85.000000 | \n","0.981100 | \n","2.430000 | \n","0.870000 | \n","0.800000 | \n","0.290000 | \n","51.500000 | \n","100.613300 | \n","
max | \n","1993.000000 | \n","2018.000000 | \n","212.000000 | \n","1.002700 | \n","16.870000 | \n","3.980000 | \n","5.530000 | \n","1.310000 | \n","82.490000 | \n","179.195200 | \n","