Skip to content

Instantly share code, notes, and snippets.

@tomkralidis
Last active January 26, 2022 20:52
Show Gist options
  • Save tomkralidis/a8dc9d8897238b183f5dec34a896ab82 to your computer and use it in GitHub Desktop.
Save tomkralidis/a8dc9d8897238b183f5dec34a896ab82 to your computer and use it in GitHub Desktop.
MSC GeoMet LTCE Jupyter demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LTCE Python demonstrations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using requests"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"req = 'https://api.weather.gc.ca/collections/ltce-temperature/items?LOCAL_DAY=30&LOCAL_MONTH=10&RECORD_HIGH_MAX_TEMP_YR=2021&f=json'\n",
"\n",
"data = requests.get(req).json()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of results: 13\n",
"Number of results returned: 13\n"
]
}
],
"source": [
"# sample results\n",
"\n",
"print(f\"Number of results: {data['numberMatched']}\")\n",
"print(f\"Number of results returned: {data['numberReturned']}\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 'VSNL40V-10-30',\n",
" 'type': 'Feature',\n",
" 'properties': {'WXO_CITY_CODE': 'NL-40',\n",
" 'VIRTUAL_STATION_NAME_E': 'NAIN AREA',\n",
" 'VIRTUAL_STATION_NAME_F': 'NAIN',\n",
" 'VIRTUAL_CLIMATE_ID': 'VSNL40V',\n",
" 'LOCAL_DAY': 30,\n",
" 'LOCAL_MONTH': 10,\n",
" 'RECORD_HIGH_MAX_TEMP_YR': 2021,\n",
" 'RECORD_HIGH_MAX_TEMP': 12.2,\n",
" 'PREV_RECORD_HIGH_MAX_TEMP_YR': 2004,\n",
" 'PREV_RECORD_HIGH_MAX_TEMP': 10.4,\n",
" 'RECORD_LOW_MAX_TEMP_YR': 1986,\n",
" 'RECORD_LOW_MAX_TEMP': -3.2,\n",
" 'PREV_RECORD_LOW_MAX_TEMP_YR': 1985,\n",
" 'PREV_RECORD_LOW_MAX_TEMP': -1.2,\n",
" 'RECORD_LOW_MIN_TEMP_YR': 2007,\n",
" 'RECORD_LOW_MIN_TEMP': -9.6,\n",
" 'PREV_RECORD_LOW_MIN_TEMP_YR': 1986,\n",
" 'PREV_RECORD_LOW_MIN_TEMP': -9.2,\n",
" 'RECORD_HIGH_MIN_TEMP_YR': 2012,\n",
" 'RECORD_HIGH_MIN_TEMP': 2.0,\n",
" 'PREV_RECORD_HIGH_MIN_TEMP_YR': 1994,\n",
" 'PREV_RECORD_HIGH_MIN_TEMP': 1.7,\n",
" 'FIRST_HIGH_MAX_TEMP': 12.2,\n",
" 'FIRST_HIGH_MAX_TEMP_YEAR': 2021,\n",
" 'SECOND_HIGH_MAX_TEMP': 10.4,\n",
" 'SECOND_HIGH_MAX_TEMP_YEAR': 2004,\n",
" 'THIRD_HIGH_MAX_TEMP': 9.6,\n",
" 'THIRD_HIGH_MAX_TEMP_YEAR': 2012,\n",
" 'FOURTH_HIGH_MAX_TEMP': 8.9,\n",
" 'FOURTH_HIGH_MAX_TEMP_YEAR': 2008,\n",
" 'FIFTH_HIGH_MAX_TEMP': 8.3,\n",
" 'FIFTH_HIGH_MAX_TEMP_YEAR': 1991,\n",
" 'LAST_UPDATED': '2022-01-26T12:49:06Z',\n",
" 'FIRST_LOW_MAX_TEMP': -3.2,\n",
" 'FIRST_LOW_MAX_TEMP_YEAR': 1986,\n",
" 'SECOND_LOW_MAX_TEMP': -2.9,\n",
" 'SECOND_LOW_MAX_TEMP_YEAR': 2007,\n",
" 'THIRD_LOW_MAX_TEMP': -1.2,\n",
" 'THIRD_LOW_MAX_TEMP_YEAR': 1985,\n",
" 'FOURTH_LOW_MAX_TEMP': -0.2,\n",
" 'FOURTH_LOW_MAX_TEMP_YEAR': 2009,\n",
" 'FIFTH_LOW_MAX_TEMP': 0,\n",
" 'FIFTH_LOW_MAX_TEMP_YEAR': 1999,\n",
" 'FIRST_HIGH_MIN_TEMP': 2,\n",
" 'FIRST_HIGH_MIN_TEMP_YEAR': 2012,\n",
" 'SECOND_HIGH_MIN_TEMP': 1.7,\n",
" 'SECOND_HIGH_MIN_TEMP_YEAR': 1994,\n",
" 'THIRD_HIGH_MIN_TEMP': 1.6,\n",
" 'THIRD_HIGH_MIN_TEMP_YEAR': 2017,\n",
" 'FOURTH_HIGH_MIN_TEMP': 1.2,\n",
" 'FOURTH_HIGH_MIN_TEMP_YEAR': 2006,\n",
" 'FIFTH_HIGH_MIN_TEMP': 0.8,\n",
" 'FIFTH_HIGH_MIN_TEMP_YEAR': 2019,\n",
" 'FIRST_LOW_MIN_TEMP': -9.6,\n",
" 'FIRST_LOW_MIN_TEMP_YEAR': 2007,\n",
" 'SECOND_LOW_MIN_TEMP': -9.2,\n",
" 'SECOND_LOW_MIN_TEMP_YEAR': 1986,\n",
" 'THIRD_LOW_MIN_TEMP': -7.5,\n",
" 'THIRD_LOW_MIN_TEMP_YEAR': 2020,\n",
" 'FOURTH_LOW_MIN_TEMP': -7.1,\n",
" 'FOURTH_LOW_MIN_TEMP_YEAR': 1989,\n",
" 'FIFTH_LOW_MIN_TEMP': -7.1,\n",
" 'FIFTH_LOW_MIN_TEMP_YEAR': 1999,\n",
" 'PROVINCE_CODE': 'NL',\n",
" 'MIN_TEMP_RECORD_BEGIN': '1984-09-01T00:00:00Z',\n",
" 'MIN_TEMP_RECORD_END': None,\n",
" 'MAX_TEMP_RECORD_BEGIN': '1984-09-01T00:00:00Z',\n",
" 'MAX_TEMP_RECORD_END': None,\n",
" 'IDENTIFIER': 'VSNL40V-10-30'},\n",
" 'geometry': {'type': 'Point', 'coordinates': [-61.69, 56.55]}}"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# show a sample result\n",
"data['features'][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using OWSLib"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"from owslib.ogcapi.features import Features "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hydrometric-stations: Hydrometric Monitoring Stations',\n",
" 'hydrometric-daily-mean: Daily Mean of Water Level or Flow',\n",
" 'hydrometric-monthly-mean: Monthly Mean of Water Level or Flow',\n",
" 'hydrometric-annual-statistics: Annual Maximum and Minimum Daily Water Level or Flow',\n",
" 'hydrometric-annual-peaks: Annual Maximum and Minimum Instantaneous Water Level or Flow',\n",
" 'hydrometric-realtime: Real-time hydrometric data',\n",
" 'climate-normals: 1981-2010 Climate Normals',\n",
" 'climate-stations: Climate Stations',\n",
" 'climate-monthly: Monthly Climate Observation Summaries',\n",
" 'climate-daily: Daily Climate Observations',\n",
" 'ahccd-stations: Adjusted and Homogenized Canadian Climate Data (AHCCD) Stations',\n",
" 'ahccd-annual: Adjusted and Homogenized Canadian Climate Data (AHCCD) Annual',\n",
" 'ahccd-seasonal: Adjusted and Homogenized Canadian Climate Data (AHCCD) Seasonal',\n",
" 'ahccd-monthly: Adjusted and Homogenized Canadian Climate Data (AHCCD) Monthly',\n",
" 'ahccd-trends: Adjusted and Homogenized Canadian Climate Data (AHCCD) Trends',\n",
" 'swob-realtime: Surface Weather Observations',\n",
" 'ltce-stations: Virtual Climate Stations (LTCE)',\n",
" 'ltce-temperature: Daily Extremes of Records (LTCE) – Temperature',\n",
" 'ltce-precipitation: Daily Extremes of Records (LTCE) – Precipitation',\n",
" 'ltce-snowfall: Daily Extremes of Records (LTCE) – Snowfall']"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"msc_geomet = Features('https://api.weather.gc.ca')\n",
"msc_geomet.collections()\n",
"[f\"{collection['id']}: {collection['title']}\" for collection in msc_geomet.collections()['collections']]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d2a47d2b52824281b1406b76a0b6efd1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(center=[55, -75], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_te…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# draw a station map\n",
"\n",
"from ipyleaflet import Map, GeoJSON\n",
"\n",
"m = Map(center=(55, -75), zoom=3)\n",
"\n",
"m\n",
"\n",
"geo_json = GeoJSON(\n",
" data=msc_geomet.collection_items('ltce-stations'),\n",
" style={\n",
" 'opacity': 1, 'dashArray': '9', 'fillOpacity': 0.1, 'weight': 1\n",
" }\n",
")\n",
"\n",
"m.add_layer(geo_json)\n",
"\n",
"m"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['FIFTH_HIGH_MAX_TEMP', 'FIFTH_HIGH_MAX_TEMP_YEAR', 'FIFTH_HIGH_MAX_TEMP_YR', 'FIFTH_HIGH_MIN_TEMP', 'FIFTH_HIGH_MIN_TEMP_YEAR', 'FIFTH_HIGH_MIN_TEMP_YR', 'FIFTH_LOW_MAX_TEMP', 'FIFTH_LOW_MAX_TEMP_YEAR', 'FIFTH_LOW_MAX_TEMP_YR', 'FIFTH_LOW_MIN_TEMP', 'FIFTH_LOW_MIN_TEMP_YEAR', 'FIFTH_LOW_MIN_TEMP_YR', 'FIRST_HIGH_MAX_TEMP', 'FIRST_HIGH_MAX_TEMP_YEAR', 'FIRST_HIGH_MAX_TEMP_YR', 'FIRST_HIGH_MIN_TEMP', 'FIRST_HIGH_MIN_TEMP_YEAR', 'FIRST_HIGH_MIN_TEMP_YR', 'FIRST_LOW_MAX_TEMP', 'FIRST_LOW_MAX_TEMP_YEAR', 'FIRST_LOW_MAX_TEMP_YR', 'FIRST_LOW_MIN_TEMP', 'FIRST_LOW_MIN_TEMP_YEAR', 'FIRST_LOW_MIN_TEMP_YR', 'FOURTH_HIGH_MAX_TEMP', 'FOURTH_HIGH_MAX_TEMP_YEAR', 'FOURTH_HIGH_MAX_TEMP_YR', 'FOURTH_HIGH_MIN_TEMP', 'FOURTH_HIGH_MIN_TEMP_YEAR', 'FOURTH_HIGH_MIN_TEMP_YR', 'FOURTH_LOW_MAX_TEMP', 'FOURTH_LOW_MAX_TEMP_YEAR', 'FOURTH_LOW_MAX_TEMP_YR', 'FOURTH_LOW_MIN_TEMP', 'FOURTH_LOW_MIN_TEMP_YEAR', 'FOURTH_LOW_MIN_TEMP_YR', 'IDENTIFIER', 'LAST_UPDATED', 'LOCAL_DAY', 'LOCAL_MONTH', 'MAX_TEMP_RECORD_BEGIN', 'MAX_TEMP_RECORD_END', 'MIN_TEMP_RECORD_BEGIN', 'MIN_TEMP_RECORD_END', 'PREV_RECORD_HIGH_MAX_TEMP', 'PREV_RECORD_HIGH_MAX_TEMP_YR', 'PREV_RECORD_HIGH_MIN_TEMP', 'PREV_RECORD_HIGH_MIN_TEMP_YR', 'PREV_RECORD_LOW_MAX_TEMP', 'PREV_RECORD_LOW_MAX_TEMP_YR', 'PREV_RECORD_LOW_MIN_TEMP', 'PREV_RECORD_LOW_MIN_TEMP_YR', 'PROVINCE_CODE', 'RECORD_HIGH_MAX_TEMP', 'RECORD_HIGH_MAX_TEMP_YR', 'RECORD_HIGH_MIN_TEMP', 'RECORD_HIGH_MIN_TEMP_YR', 'RECORD_LOW_MAX_TEMP', 'RECORD_LOW_MAX_TEMP_YR', 'RECORD_LOW_MIN_TEMP', 'RECORD_LOW_MIN_TEMP_YR', 'SECOND_HIGH_MAX_TEMP', 'SECOND_HIGH_MAX_TEMP_YEAR', 'SECOND_HIGH_MAX_TEMP_YR', 'SECOND_HIGH_MIN_TEMP', 'SECOND_HIGH_MIN_TEMP_YEAR', 'SECOND_HIGH_MIN_TEMP_YR', 'SECOND_LOW_MAX_TEMP', 'SECOND_LOW_MAX_TEMP_YEAR', 'SECOND_LOW_MAX_TEMP_YR', 'SECOND_LOW_MIN_TEMP', 'SECOND_LOW_MIN_TEMP_YEAR', 'SECOND_LOW_MIN_TEMP_YR', 'THIRD_HIGH_MAX_TEMP', 'THIRD_HIGH_MAX_TEMP_YEAR', 'THIRD_HIGH_MAX_TEMP_YR', 'THIRD_HIGH_MIN_TEMP', 'THIRD_HIGH_MIN_TEMP_YEAR', 'THIRD_HIGH_MIN_TEMP_YR', 'THIRD_LOW_MAX_TEMP', 'THIRD_LOW_MAX_TEMP_YEAR', 'THIRD_LOW_MAX_TEMP_YR', 'THIRD_LOW_MIN_TEMP', 'THIRD_LOW_MIN_TEMP_YEAR', 'THIRD_LOW_MIN_TEMP_YR', 'VIRTUAL_CLIMATE_ID', 'VIRTUAL_STATION_NAME_E', 'VIRTUAL_STATION_NAME_F', 'WXO_CITY_CODE'])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# fetch data from LTCE temperature\n",
"\n",
"dataset = 'ltce-temperature'\n",
"\n",
"dataset_queryables = msc_geomet.collection_queryables(dataset)\n",
"\n",
"dataset_queryables['properties'].keys()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# query\n",
"\n",
"query_params = {\n",
" 'LOCAL_DAY': 30,\n",
" 'LOCAL_MONTH': 10,\n",
" 'RECORD_HIGH_MAX_TEMP_YR': 2021\n",
"}\n",
"\n",
"data = msc_geomet.collection_items(dataset, **query_params)\n",
"\n",
"data['numberMatched']"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment