diff --git a/year2024/tutorial/ISS_tracks_exercise.ipynb b/year2024/tutorial/ISS_tracks_exercise.ipynb index 813161f..5cc51b2 100644 --- a/year2024/tutorial/ISS_tracks_exercise.ipynb +++ b/year2024/tutorial/ISS_tracks_exercise.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "80bc30cc-cf8f-4b1c-b4d5-b6c0a316639c", + "id": "ff4eb71a-be1f-40c1-b169-e4d7366ac232", "metadata": {}, "source": [ "
\n", @@ -24,16 +24,16 @@ "\n", "
\n", "

Python Workflows to Extract and Plot Satellite Data Products along Tracks

\n", - "

International Space Station - Exercise 2

\n", + "

International Space Station - Option 2

\n", "
" ] }, { "cell_type": "markdown", - "id": "2d602f7c-1428-4cc8-9ec5-b35203b4f632", + "id": "f1c4d95f-fcf3-4c9f-b7ea-43902693a7e1", "metadata": {}, "source": [ - "## Objectives\n", + "## Objective\n", "\n", "- The International Space Station (ISS) makes 16 orbits of Earth, traveling through 16 sunrises and sunsets.\n", "- ISS travels at a speed of five miles per second, circling Earth about every 90 minutes.\n", @@ -54,16 +54,16 @@ }, { "cell_type": "markdown", - "id": "04341941-793b-4dae-abaf-4008650ff7fa", + "id": "5b1f1de0-6a93-4b6a-bfb8-5464090ad89d", "metadata": {}, "source": [ - "## Import necessary modules" + "## Import necessary modules" ] }, { "cell_type": "code", "execution_count": null, - "id": "21fc2647-f089-4f34-902b-9de9c55ba827", + "id": "919c6a68-a719-47c5-9427-e825739a8479", "metadata": {}, "outputs": [], "source": [ @@ -74,7 +74,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8810774d-0199-4844-8422-1f7e0cfec2cf", + "id": "8f34f491-837a-4756-aebd-a60c921e6f61", "metadata": {}, "outputs": [], "source": [ @@ -85,7 +85,7 @@ { "cell_type": "code", "execution_count": null, - "id": "97485812-d973-46e3-9550-ba277d859775", + "id": "3bbf5dcf-04b8-4b58-ad43-c69f94435120", "metadata": {}, "outputs": [], "source": [ @@ -98,7 +98,7 @@ { "cell_type": "code", "execution_count": null, - "id": "a9111525-5fe1-4dcc-a1d5-3d4d3602ab6e", + "id": "deb827c4-d360-4dc1-9fc4-9c5cae12a63f", "metadata": {}, "outputs": [], "source": [ @@ -110,7 +110,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4f3d419b-1e0b-4d3e-9915-240163100823", + "id": "674e48b5-2afe-405e-a729-3596efc1840b", "metadata": {}, "outputs": [], "source": [ @@ -121,7 +121,7 @@ { "cell_type": "code", "execution_count": null, - "id": "bda61917-08ef-4e8b-9e83-8342d8439dac", + "id": "cfd3fbd7-cf95-4c4d-9652-a187352f6a78", "metadata": {}, "outputs": [], "source": [ @@ -131,7 +131,7 @@ { "cell_type": "code", "execution_count": null, - "id": "e1d50feb-eb98-4198-b3da-a969f67b4ab9", + "id": "9f66efa9-07f1-43bc-9d90-c54ad4b03af9", "metadata": {}, "outputs": [], "source": [ @@ -139,18 +139,41 @@ "import hvplot.pandas " ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "34ddc136-6725-4676-853d-e980f058145c", + "metadata": {}, + "outputs": [], + "source": [ + "figsize = (17, 11)" + ] + }, { "cell_type": "markdown", - "id": "c1e64cfd-d7dd-456e-831b-1436f847c199", + "id": "75573709-477f-42d5-b039-86e8eec89e1c", "metadata": {}, "source": [ - "## Read the CSV file" + "## Read the CSV file with Pandas\n", + "\n", + "- The CSV file has eight columns:\n", + " - `t`: for the date/time as strings\n", + " - `latitude` as floats\n", + " - `longitude` as floats\n", + " - `land_flag`\n", + " - `temperature`\n", + " - `windspeed`\n", + " - `traj_id`: identify an orbit\n", + " - `country`: country of each location\n", + "- While reading the file, make sure that you transform the `t` values into datetime objects.\n", + "- Rename the columns.\n", + "- Create a new `geometry` columns" ] }, { "cell_type": "code", "execution_count": null, - "id": "123785d1-cef6-450c-8db7-35285fadeb92", + "id": "64d22f55-0147-4ff9-bcc0-8df895584398", "metadata": {}, "outputs": [], "source": [ @@ -161,77 +184,254 @@ { "cell_type": "code", "execution_count": null, - "id": "3f167aec-3a62-4763-b844-4058299ae8bb", + "id": "6ce63bb0-ee53-4199-acdd-835973f2af66", "metadata": {}, "outputs": [], "source": [ "file_name = Path(data_dir) / \"iss_timeseries_trajectories_20240303_104539.csv\"" ] }, + { + "cell_type": "markdown", + "id": "0a89ef0c-0f40-42a0-bc33-7c77950b833a", + "metadata": {}, + "source": [ + "#### Read the file to create a Pandas DataFrame\n", + "\n", + "Do no forget to handle the date/time." + ] + }, { "cell_type": "code", "execution_count": null, - "id": "dc50db02-a288-4586-8284-df166aaee910", + "id": "b53df351-19ee-4551-968d-51c89aa27b95", "metadata": {}, "outputs": [], "source": [ - "df = pd.read_csv(file_name)" + "dateparse = lambda x: ...\n", + "df_iss = ..." + ] + }, + { + "cell_type": "markdown", + "id": "8dff1c4d-55ca-45a0-9b77-c51b5bc47764", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "dateparse = lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')\n", + "df_iss = pd.read_csv(file_name,\n", + " parse_dates={'datetime': [0,]}, \n", + " date_parser=dateparse)\n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "608d3545-96fc-4d77-998f-4d530130284d", + "metadata": {}, + "source": [ + "#### Rename the `datetime` column to `t`" ] }, { "cell_type": "code", "execution_count": null, - "id": "b59a1e65-a76d-4aac-83e8-a2ac9f3f9913", + "id": "16d59fc3-6294-4fb9-8358-603246f1f04b", "metadata": {}, "outputs": [], "source": [ - "df" + "df_iss." + ] + }, + { + "cell_type": "markdown", + "id": "32bb6533-c1c3-4bf9-a62f-8c8f89b0736f", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "df_eclipse.rename(columns = {'datetime': 't'}, inplace=True)\n", + "```\n", + "

" ] }, { "cell_type": "markdown", - "id": "6b0f4348-d792-4b82-a75e-9ac2a67a1707", + "id": "d0902f9b-54ce-49ea-a8bb-7e41767b7190", "metadata": {}, "source": [ - "## Perform Analyses" + "#### Add a `geometry` column" ] }, { "cell_type": "code", "execution_count": null, - "id": "af7398fa-3667-478f-9598-5686319fc4f3", + "id": "5d445638-e769-447a-8250-750a009cb7a5", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "df_iss['geometry'] = ..." + ] + }, + { + "cell_type": "markdown", + "id": "949b8f18-ec92-4b8c-84fc-c897a99deb41", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "df_iss['geometry'] = [shpgeom.Point(xy) for xy in zip(df_iss['longitude'], df_iss['latitude'])] \n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "f74d73d9-46d1-4696-9e3e-fcdc285df8e2", + "metadata": {}, + "source": [ + "## Manipulation with GeoPandas\n", + "\n", + "#### Use GeoPandas to create the GeoDataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2d1fc0d2-b53d-4c28-ad3b-b8f6fab29b5a", + "metadata": {}, + "outputs": [], + "source": [ + "gdf_iss = ..." + ] + }, + { + "cell_type": "markdown", + "id": "371de972-a824-4718-9455-05eedf33dd89", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "gdf_iss = gpd.GeoDataFrame(df_iss, geometry=\"geometry\") \n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "4aa339ce-b3a5-4cdf-b6a5-f73ea1f795aa", + "metadata": {}, + "source": [ + "#### Interactive plot of the path of the eclipse" + ] }, { "cell_type": "code", "execution_count": null, - "id": "f56eb42c-6fa0-404d-b39a-a2115031ddb1", + "id": "69ad241e-5cfc-4cc1-ace7-be9a8696b271", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gdf_iss.hvplot(tiles='EsriTerrain', coastline=True, \n", + " hover_cols=[\"t\"])" + ] + }, + { + "cell_type": "markdown", + "id": "a7492552-e6d1-4c3a-8287-2315a8e35424", + "metadata": {}, + "source": [ + "## Manipulation with MovingPandas\n", + "\n", + "#### Create the MovingPandas trajectory\n", + "\n", + "__Note that we have here an identifier for the orbits.__" + ] }, { "cell_type": "code", "execution_count": null, - "id": "3bd3240d-d4a1-42db-a871-073d5318ca98", + "id": "10bb2a38-97c9-44ea-b0e6-ac84b5b9741d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "mdf_iss = ..." + ] + }, + { + "cell_type": "markdown", + "id": "e3115223-409c-40e6-b5b3-96119eea613b", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "mdf_iss = mpd.Trajectory(df_iss, traj_id_col=\"traj_id\",\n", + " x = \"longitude\", y=\"latitude\", t=\"t\")\n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "4a0767af-da4f-4713-9524-d0c9c758c48e", + "metadata": {}, + "source": [ + "#### Basic plot of the trajectory and the buffer" + ] }, { "cell_type": "code", "execution_count": null, - "id": "b57c8dd1-21d4-4f8c-8822-8155333e79b2", + "id": "54a323d7-de5e-4a7f-8931-af47d3a3843e", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "fig, ax = plt.subplots(1, figsize=(20,10))\n", + "mdf_iss.plot(ax=ax);" + ] + }, + { + "cell_type": "markdown", + "id": "cbf0bcd7-c64a-493d-8c59-338393517c93", + "metadata": {}, + "source": [ + "#### Interactive plot of the trajectory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "963e2f02-2d8b-4a3e-98c4-c05a75d12a75", + "metadata": {}, + "outputs": [], + "source": [ + "mdf_iss.hvplot(line_width=0.9)" + ] + }, + { + "cell_type": "markdown", + "id": "51266370-680b-4c1f-a188-8aa7bf733c97", + "metadata": {}, + "source": [ + "## Perform analyses" + ] }, { "cell_type": "code", "execution_count": null, - "id": "506f6121-537a-477a-9a6c-ecd1f070404a", + "id": "fa2f4839-e596-426d-b481-dbc6344c6314", "metadata": {}, "outputs": [], "source": [] diff --git a/year2024/tutorial/README.md b/year2024/tutorial/README.md index 4cf0262..115c166 100644 --- a/year2024/tutorial/README.md +++ b/year2024/tutorial/README.md @@ -45,9 +45,9 @@ time series data and plot I along the satellite path. - Perform analyses and visualizations 5. Break (10 minutes) 6. Exercises (40 minutes) - - Option 1: Movement of the Aura satellite - - Option 2: Movement of the International Space Station (ISS) - - Option 3: Tracking the April 8, 2024 solar eclipse over the US. + - __Option 1__: Tracking the April 8, 2024 solar eclipse over the US. + - __Option 2__: Movement of the International Space Station (ISS) + - __Option 3__: Movement of the Aura satellite 7. Wrapping Up (10 minutes) @@ -58,12 +58,12 @@ They will have pointers to write basic tools to read (using netCDF4, h5py, etc.) satellite data files, create GeoDataFrames containing the time series of the positions and field values (using GeoPandas and MovingPandas), perform static and interactive visualizations (using MovingPandas and hvplot) -of the tracks of the moving objects. +of the tracks of moving objects. In addition, participants will be able to use the knowledge gained to compare data from other sources (model outputs or other satellite products). ### Target Audience: -- Any practitioner curious of accessing NASA satellite data files, examine their content, plot fields over the satellite tracks. +- Any practitioner curious of accessing NASA satellite data files, examine their content, plot fields over satellite tracks. - Anyone familiar with the Python programming language and with a basic knowledge of NumPy. ### Training platform @@ -89,11 +89,11 @@ You are expected to write something like: LastName, FirstName (my_address@domain.ext) ``` -Note that you first and last names should match the ones you used for registering to this tutorial. +Note that your first and last names should match the ones you used for registering to this tutorial. ### Python packages -The following packages will be used: +The following Python packages will be used: - __h5py__: Reading HDF5 files - __Pandas__: Manipulation and exploratory data analysis of tabular data. diff --git a/year2024/tutorial/satellite_tracks_OMI_exercise.ipynb b/year2024/tutorial/satellite_tracks_OMI_exercise.ipynb index 74a3f07..72d7b9a 100644 --- a/year2024/tutorial/satellite_tracks_OMI_exercise.ipynb +++ b/year2024/tutorial/satellite_tracks_OMI_exercise.ipynb @@ -25,7 +25,7 @@ "\n", "
\n", "

Python Workflows to Extract and Plot Satellite Data Products along Tracks

\n", - "

Aura Satellite - Exercise 1

\n", + "

Aura Satellite - Option 3

\n", "
" ] }, @@ -325,7 +325,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "
Click here to access the solution\n", + "
Click here to access the solution\n", "

\n", "\n", "```python\n", @@ -520,7 +520,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "

Click here to access the solution\n", + "
Click here to access the solution\n", "

\n", "\n", "```python\n", @@ -611,7 +611,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "

Click here to access the solution\n", + "
Click here to access the solution\n", "

\n", "\n", "```python\n", diff --git a/year2024/tutorial/solar_eclipse_08Apr2024_exercise.ipynb b/year2024/tutorial/solar_eclipse_08Apr2024_exercise.ipynb new file mode 100644 index 0000000..a74d081 --- /dev/null +++ b/year2024/tutorial/solar_eclipse_08Apr2024_exercise.ipynb @@ -0,0 +1,658 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ff4eb71a-be1f-40c1-b169-e4d7366ac232", + "metadata": {}, + "source": [ + "

\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
\n", + "
\n", + "\n", + " \n", + "
\n", + "

PyCon 2024 Tutorial

\n", + "
\n", + "\n", + "---\n", + "\n", + "
\n", + "

Python Workflows to Extract and Plot Satellite Data Products along Tracks

\n", + "

2024 Solar Eclipse - Option 1

\n", + "
" + ] + }, + { + "cell_type": "markdown", + "id": "f1c4d95f-fcf3-4c9f-b7ea-43902693a7e1", + "metadata": {}, + "source": [ + "## Objective\n", + "\n", + "- We want to plot the path of the April 8, 2024 solar eclipse.\n", + "- We want to identify the cities that will have full view of the total eclipse.\n", + "\n", + "Part of this work was taken from: [Plan Your Next Eclipse Viewing](https://github.com/christyheaton/PlanYourNextEclipseViewing/blob/master/Notebook/Presentation.ipynb) by Christy Heaton" + ] + }, + { + "cell_type": "markdown", + "id": "d2b170cf-4d30-450d-b43b-44ebb4b43818", + "metadata": {}, + "source": [ + "## Import necessary modules" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "919c6a68-a719-47c5-9427-e825739a8479", + "metadata": {}, + "outputs": [], + "source": [ + "import warnings\n", + "warnings.filterwarnings(\"ignore\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f34f491-837a-4756-aebd-a60c921e6f61", + "metadata": {}, + "outputs": [], + "source": [ + "import datetime as dt\n", + "from pathlib import Path" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3bbf5dcf-04b8-4b58-ad43-c69f94435120", + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "import matplotlib\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.ticker as mticker" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "deb827c4-d360-4dc1-9fc4-9c5cae12a63f", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd\n", + "import geopandas as gpd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "674e48b5-2afe-405e-a729-3596efc1840b", + "metadata": {}, + "outputs": [], + "source": [ + "from shapely import geometry as shpgeom\n", + "from shapely import wkt as shpwkt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cfd3fbd7-cf95-4c4d-9652-a187352f6a78", + "metadata": {}, + "outputs": [], + "source": [ + "import movingpandas as mpd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f66efa9-07f1-43bc-9d90-c54ad4b03af9", + "metadata": {}, + "outputs": [], + "source": [ + "import holoviews as hv\n", + "import hvplot.pandas " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "34ddc136-6725-4676-853d-e980f058145c", + "metadata": {}, + "outputs": [], + "source": [ + "figsize = (17, 11)" + ] + }, + { + "cell_type": "markdown", + "id": "75573709-477f-42d5-b039-86e8eec89e1c", + "metadata": {}, + "source": [ + "## Read the CSV file with Pandas\n", + "\n", + "- The CSV file has three columns:\n", + " - `t`: for the date/time as strings\n", + " - `latitude` as floats\n", + " - `longitude` as floats\n", + "- While reading the file, make sure that you transform the `t` values into datetime objects.\n", + "- Rename the columns.\n", + "- Create a new `geometry` columns" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64d22f55-0147-4ff9-bcc0-8df895584398", + "metadata": {}, + "outputs": [], + "source": [ + "data_dir = \"/Users/jkouatch/myTasks/PythonTraining/ASTG606/Materials/sat_data/Eclipse_Data/\"\n", + "#data_dir = \"/tljh-data/sat_data/Eclipse_Data\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ce63bb0-ee53-4199-acdd-835973f2af66", + "metadata": {}, + "outputs": [], + "source": [ + "file_name = Path(data_dir) / \"eclipse_path_8April2024.csv\"" + ] + }, + { + "cell_type": "markdown", + "id": "0a89ef0c-0f40-42a0-bc33-7c77950b833a", + "metadata": {}, + "source": [ + "#### Read the file to create a Pandas DataFrame\n", + "\n", + "Do no forget to handle the date/time." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b53df351-19ee-4551-968d-51c89aa27b95", + "metadata": {}, + "outputs": [], + "source": [ + "dateparse = lambda x: ...\n", + "df_eclipse = ..." + ] + }, + { + "cell_type": "markdown", + "id": "8dff1c4d-55ca-45a0-9b77-c51b5bc47764", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "dateparse = lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')\n", + "df_eclipse = pd.read_csv(file_name,\n", + " parse_dates={'datetime': [0,]}, \n", + " date_parser=dateparse)\n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "608d3545-96fc-4d77-998f-4d530130284d", + "metadata": {}, + "source": [ + "#### Rename the `datetime` column to `t`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "16d59fc3-6294-4fb9-8358-603246f1f04b", + "metadata": {}, + "outputs": [], + "source": [ + "df_eclipse." + ] + }, + { + "cell_type": "markdown", + "id": "32bb6533-c1c3-4bf9-a62f-8c8f89b0736f", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "df_eclipse.rename(columns = {'datetime': 't'}, inplace=True)\n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "d0902f9b-54ce-49ea-a8bb-7e41767b7190", + "metadata": {}, + "source": [ + "#### Add a `geometry` column" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d445638-e769-447a-8250-750a009cb7a5", + "metadata": {}, + "outputs": [], + "source": [ + "df_eclipse['geometry'] = ..." + ] + }, + { + "cell_type": "markdown", + "id": "949b8f18-ec92-4b8c-84fc-c897a99deb41", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "df_eclipse['geometry'] = [shpgeom.Point(xy) for xy in zip(df_eclipse['longitude'], df_eclipse['latitude'])] \n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "f74d73d9-46d1-4696-9e3e-fcdc285df8e2", + "metadata": {}, + "source": [ + "## Manipulation with GeoPandas\n", + "\n", + "#### Use GeoPandas to create the GeoDataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2d1fc0d2-b53d-4c28-ad3b-b8f6fab29b5a", + "metadata": {}, + "outputs": [], + "source": [ + "gdf_eclipse = ..." + ] + }, + { + "cell_type": "markdown", + "id": "371de972-a824-4718-9455-05eedf33dd89", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "gdf_eclipse = gpd.GeoDataFrame(df_eclipse, geometry=\"geometry\") \n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "4aa339ce-b3a5-4cdf-b6a5-f73ea1f795aa", + "metadata": {}, + "source": [ + "#### Interactive plot of the path of the eclipse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69ad241e-5cfc-4cc1-ace7-be9a8696b271", + "metadata": {}, + "outputs": [], + "source": [ + "gdf_eclipse.hvplot(tiles='EsriTerrain', coastline=True, \n", + " hover_cols=[\"t\"])" + ] + }, + { + "cell_type": "markdown", + "id": "535c8fe7-5767-4623-89fc-d84606aef4b8", + "metadata": {}, + "source": [ + "#### Create a buffer along the path\n", + "- The buffer will correspond to the area along the path where we view the total elcipse." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3a99fbfd-5cfd-42e7-b8ae-ec29908c31ea", + "metadata": {}, + "outputs": [], + "source": [ + "size_buffer = 1.5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93fd7c3a-7f8d-41fe-820c-de04a6d8fd3c", + "metadata": {}, + "outputs": [], + "source": [ + "gdf_eclipse_buffer = gpd.GeoDataFrame(geometry=gdf_eclipse.buffer(size_buffer))\n", + "gdf_eclipse_buffer" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "972a11a3-46cc-4c05-b6c1-14e571a314e3", + "metadata": {}, + "outputs": [], + "source": [ + "fig, ax = plt.subplots(figsize=figsize)\n", + "gdf_eclipse.plot(ax=ax, color=\"blue\", alpha=1.0)\n", + "gdf_eclipse_buffer.plot(ax=ax, color=\"pink\", alpha=0.2);" + ] + }, + { + "cell_type": "markdown", + "id": "a7492552-e6d1-4c3a-8287-2315a8e35424", + "metadata": {}, + "source": [ + "## Manipulation with MovingPandas\n", + "\n", + "#### Create the MovingPandas trajectory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10bb2a38-97c9-44ea-b0e6-ac84b5b9741d", + "metadata": {}, + "outputs": [], + "source": [ + "mdf_eclipse = ..." + ] + }, + { + "cell_type": "markdown", + "id": "e3115223-409c-40e6-b5b3-96119eea613b", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "mdf_eclipse = mpd.Trajectory(df_eclipse, traj_id=1,\n", + " x = \"longitude\", y=\"latitude\", t=\"t\")\n", + "```\n", + "

" + ] + }, + { + "cell_type": "markdown", + "id": "4a0767af-da4f-4713-9524-d0c9c758c48e", + "metadata": {}, + "source": [ + "#### Basic plot of the trajectory and the buffer" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "54a323d7-de5e-4a7f-8931-af47d3a3843e", + "metadata": {}, + "outputs": [], + "source": [ + "fig, ax = plt.subplots(1, figsize=(20,10))\n", + "gdf_eclipse.plot(ax=ax, color=\"pink\", )\n", + "mdf_eclipse.plot(ax=ax);" + ] + }, + { + "cell_type": "markdown", + "id": "cbf0bcd7-c64a-493d-8c59-338393517c93", + "metadata": {}, + "source": [ + "#### Interactive plot of the trajectory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "963e2f02-2d8b-4a3e-98c4-c05a75d12a75", + "metadata": {}, + "outputs": [], + "source": [ + "mdf_eclipse.hvplot(line_width=0.9)" + ] + }, + { + "cell_type": "markdown", + "id": "51266370-680b-4c1f-a188-8aa7bf733c97", + "metadata": {}, + "source": [ + "## GeoDataFrame for all the countries of the world\n", + "\n", + "- We use the Naturel Earth database of the polygon geometry of each country of world" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cce70e17-e574-4471-896f-877fdcc899f5", + "metadata": {}, + "outputs": [], + "source": [ + "world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))\n", + "world" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "156b05f8-0f3e-4511-9076-b66d340788a8", + "metadata": {}, + "outputs": [], + "source": [ + "world.plot(color='white', linewidth=0.5, edgecolor='black', figsize=figsize);" + ] + }, + { + "cell_type": "markdown", + "id": "39fe9af6-faad-4451-8c9d-077d81e71253", + "metadata": {}, + "source": [ + "## GeoDataFrame for all the major cities of the world" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "09effde0-fd34-4431-9255-5af92d6b2f14", + "metadata": {}, + "outputs": [], + "source": [ + "url_cities = \"https://github.com/nvkelso/natural-earth-vector/raw/master/10m_cultural/ne_10m_populated_places.shp\"\n", + "world_cities = gpd.read_file(url_cities)\n", + "world_cities.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3769af0-abab-4cb0-811e-70c87e0943cb", + "metadata": {}, + "outputs": [], + "source": [ + "base = world.plot(color='white', linewidth=0.5, edgecolor='black', figsize=figsize)\n", + "world_cities.plot(ax=base, color='orange', markersize=3)\n", + "base.set_axis_off() " + ] + }, + { + "cell_type": "markdown", + "id": "3afd0fa6-c465-4302-9fe4-738ef89563cf", + "metadata": {}, + "source": [ + "## Identify the cities that will have full view of the solar eclipse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fced2983-3d91-4fab-b9c3-025081dfc479", + "metadata": {}, + "outputs": [], + "source": [ + "cities_eclipse = gpd.sjoin(world_cities, gdf_eclipse_buffer, \n", + " how='inner', op='intersects')\n", + "cities_eclipse" + ] + }, + { + "cell_type": "markdown", + "id": "b252eaa9-6e97-4035-bc9d-4502685ccf35", + "metadata": {}, + "source": [ + "#### Plot the cities that will have full view of the solar eclipse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83c0b45c-3e1e-4277-b438-fa43b7415d16", + "metadata": {}, + "outputs": [], + "source": [ + "base = world.plot(color='white', linewidth=0.5, edgecolor='black', figsize=figsize)\n", + "cities_eclipse.plot(ax=base, color='orange', markersize=3)\n", + "base.set_axis_off() " + ] + }, + { + "cell_type": "markdown", + "id": "bb82571d-388f-46ec-a6cd-b9b3675ff780", + "metadata": {}, + "source": [ + "#### Use the `world` GeoDataFrame to zoom in on North America" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c971ea42-87ec-4769-a384-a20ba25c237f", + "metadata": {}, + "outputs": [], + "source": [ + "north_america = world[world['continent']=='North America']" + ] + }, + { + "cell_type": "markdown", + "id": "aa736c8a-be89-4c54-8e4a-fe3c9de9eb04", + "metadata": {}, + "source": [ + "#### Plot the cities that will have full view of the solar eclipse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "42a79880-e78c-4887-872c-1d8aade59f11", + "metadata": {}, + "outputs": [], + "source": [ + "base = north_america.plot(color='white', linewidth=0.5, edgecolor='black', figsize=figsize)\n", + "cities_eclipse.plot(ax=base, color='orange', markersize=3)\n", + "base.set_axis_off() " + ] + }, + { + "cell_type": "markdown", + "id": "ff24e435-b523-4f8c-a45c-cadc9ebc064f", + "metadata": {}, + "source": [ + "#### Plot the solar eclipse path (buffer) and the cities\n", + "\n", + "Include the area along the path where the total eclipse will be seen." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "033eb077-7cc3-4b20-b9ed-45156e70b5d8", + "metadata": {}, + "outputs": [], + "source": [ + "base = north_america.plot(color='white', linewidth=0.5, edgecolor='black', figsize=figsize)\n", + "cities_eclipse.plot(ax=base, color='orange', markersize=3)\n", + "base.set_axis_off() " + ] + }, + { + "cell_type": "markdown", + "id": "c1bcfbff-c3e9-402a-8621-2cbd96fcae8f", + "metadata": {}, + "source": [ + "
Click here to access the solution\n", + "

\n", + "\n", + "```python\n", + "base = north_america.plot(color='white', linewidth=0.5, edgecolor='black', figsize=figsize)\n", + "cities_eclipse.plot(ax=base, color='orange', markersize=3)\n", + "gdf_eclipse_buffer.plot(ax=base, color=\"black\", alpha=0.03);\n", + "base.set_axis_off() \n", + "```\n", + "

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa2f4839-e596-426d-b481-dbc6344c6314", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}