  Tracking bird migration with Python-3

File handling | NumPy | Python Methods and Functions

The data for this example is taken from the LifeWatch INBO project. Several datasets have been released as part of this project. We will use a small dataset that consists of migration data for three seagulls named Eric, Niko, and Sunn. Official_datasets ; The dataset used — cs> "& Gt; The csv file contains eight columns and contains variables such as latitude, longitude, altitude, and timestamps. In this example, we will first load data, present some simple flight paths, track flight speed, learn about daytime, and much, much more.

Purpose: to track the movement of three seagulls, and exactly — Erica, Niko and Sanne

Dataset: official_datasets ; used dataset —

Dependencies: Matplotlib, Pandas, Numpy, Cartopy, Shapely

Repository (Github): source code
(check repository for documentation source code.)

Review: explanation (.pdf)

We will divide our case study into five parts:
1. Visualization of longitude and latitude data of gulls.
2. Visualize the change in the speed of the seagulls.
3. Visualize the time it takes seagulls to cover equal distances along the way.
4. Visualize the average daily speed of the gulls.
5. A cartographic view of the travel of seagulls.

PART (1/5): latitude and longitude
In this part we are going to visualize the location of the birds. We are going to plot latitude and longitude along the Y and X axis respectively and render the location data present in the CSV file.

 import pandas as pd import matplotlib.pyplot as plt import numpy as np     birddata = pd.read_csv ( "bird_tracking.csv" ) bird_names = pd.unique (birddata.bird_name )    # storing bird indexes Eric ix = birddata .bird_name = = "Eric"   x, y = birddata.longitude [ix], birddata.latitude [ix] plt.figure (figsize = ( 7 , 7 )) plt.plot (x, y, "b." )   “To look at all the trajectories of the birds, we plot each bird into one plot plt.figure (fig size = ( 7 , 7 )) for bird_name in bird_names:   # storing bird indexes Eric ix = birddata.bird_name = = bird_name  x, y = birddata.longitude [ix], birddata.latitude [ix] plt.plot (x, y, "." , label = bird_name) plt.xlabel ( " Longitude " ) plt.ylabel ( "Latitude" ) plt.legend (loc = "lower right" ) plt.show ()

plt.figure (figsize = (7,7)) plt.plot ( x, y, "b.")

We use the matplotlib function, figure (), to initialize the size of the figure to 7 x 7 and plot it using the plot () function. The parameters inside the plot () function, ie X, y and "b." Specifies longitude data along the x-axis (for x), latitude along y (for y), and b = blue, to use. = circles in the render.

Output: You must have all the dependencies.Install them using "pip install dependency_name"  PART (2/5 ): 2D speed versus frequency
In the second part of the case, we are going to visualize 2D speed and frequency for a seagull named "Eric".

 import pandas as pd import matplotlib.pyplot as plt import numpy as np   birddata = pd.read_csv ( "bird_tracking.csv" ) bird_names = pd.unique (birddata.bird_name)     # storing Eric's bird index ix = birddata.bird_name = = "Eric"   speed = birddata.speed_2d [ix]    plt.figure (figsize = ( 8 , 4 )) ind = np.isnan (speed) plt.hist (speed [~ ind], bins = np.linspace ( 0 , 30 , 20 ), normed = True ) plt.xlabel ( "2D speed (m / s)" ) plt.ylabel ( "Frequency" ) plt.show ()

ind = np.isnan (speed) plt.hist (speed [~ ind], bins = np.linspace (0,30,20), normed = True) plt.xlabel ("2D speed (m / s) ") plt.ylabel (" Frequency ") plt.show ()

The speed [~ ind] parameters indicate that we will only include records for which ind! = True, bins = np.linspace (0,30,20) indicates that the cells on the x-axis will range from 0 to 30 with 20 cells within them being linearly spaced. Finally, we plot the 2D velocity in m / s along the x-axis and frequency along the y-axis using the xlabel () and ylabel () functions respectively, and plot the data using plt.show ().

Output: PART (3/5): time and date
The third part is related to the date and time. We're going to visualize the time (in days) it takes Eric to travel constant distances throughout his journey. If it covers equal distances in an equal amount of time, then the elapsed time and observation The curve will be linear.

 import pandas as pd import matplotlib.pyplot as plt import datetime import numpy as np   birddata = pd.read_csv ( "bird_tracking.csv" ) bird_names = pd.unique (birddata.bird_name)     timestamps = [] for k in range ( len (birddata)):   timestamps.append (datetime.datetime.strptime (birddata.date_time.iloc [k] [: - 3 ], "% Y-% m-% d% H:% M:% S " ))    birddata [ "timestamp" ] = pd.Series (timestamps, index = birddata.index)   times = birddata.timestamp [birddata.bird_name = = "Eric" ] elapsed_time = [time - times [ 0 ] for time in times]    plt.plot (np. array (elapsed_time) / datetime.timedelta (days = 1 )) plt.xlabel ( "Observation" ) plt.ylabel ( "Elapsed time (days)" ) plt.show ()

for k in range (len (birddata)): timestamps.append (datetime.datetime.strptime (birddata.date_time.iloc [k] [: - 3], "% Y-% m-% d% H:% M:% S"))

“& gt; & gt; & gt; datetime.datetime.today () ", returns the current date (yy-mm-dd) and time (h: m: s).
"& gt; & gt; & gt; date_str [: — 3] ”, cuts / removes coordinated UTC +00 timestamps.
"& gt; & gt; & gt; datetime.datetime.strptime (date_str [: — 3], "% Y-% m-% d% H:% M:% S") ", the timestamp strings from date_str are converted to a datetime object to work on"% Y-% m-% d% H:% M:% S "— this format is year-month-date and hour-minute-second.

Output: PART (4/5): Average Daily Speed ​​
We are going to visualize the average daily speed of Eric the seagull for the total number of days on recorded flight.

 import pandas as pd import matplotlib.pyplot as plt import datetime import numpy as np   birddata = pd.read_csv ( "bird_tracking.csv" ) bird_names = pd .unique (birddata.bird_name)    timestamps = [] for k in range ( len (birddata)): timestamps.append (datetime.datetime.strptime (birddata.date_time.iloc [k] [: - 3 ], "% Y-% m-% d% H:% M:% S " )) birddata [ "timestamp" ] = pd.Series (timestamps, index = birddata.index)   data =   birddata [birddata.bird_name = = "Eric" ] times = data.timestamp elapsed_time = [time - times [ 0 ] for time in times] elapsed_days = np.array (e lapsed_time) / datetime.timedelta (days = 1 )   next_day = 1 inds = [] daily_mean_speed = [] for (i, t) in enumerate (elapsed_days): if t & lt; next_day: inds.append (i) else : daily_mean_speed.append (np.mean (data.speed_2d [inds]))   next_day + = 1 inds = []   plt.figure (figsize = ( 8 , 6 )) plt.plot (daily_mean_speed, " rs- " ) plt.xlabel ( "Day" ) plt.ylabel ( "Mean Speed ​​(m / s)" ); plt.show ()

enumerate () - is one of the built-in Python functions. It returns an enumerated object. In our case, that object is a list of tuples (immutable lists), each containing a pair of count / index and value.

Output: PART (5/5) : Cartographic View
In this last part we are going to track birds above the map.

 import pandas as pd import cartopy.crs as ccrs import cartopy.feature as cfeature import matplotlib.pyplot as plt   birddata = pd.read_csv ( "bird_tracking.csv" ) bird_names = pd.unique (birddata.bird_name)   # To move forward, we you need to specify # specific projection that interests us # in use. proj = ccrs.Mercator ()    plt.figure (figs ize = ( 10 , 10 )) ax = plt.axes (projection = proj) ax.set_extent (( - 25.0 , 20.0 , 52.0 , 10.0 )) ax.add_feature (cfeature.LAND) ax.add_feature (cfeature.OCEAN) ax.add_feature (cfeature.COASTLINE) ax.add_feature (cfeature.BORDERS , linestyle = ': ' ) for name in bird_names: ix = birddata [ 'bird_name' ] = = name x, y = birddata.longitude [ix], birddata.latitude [ix]   ax.plot (x, y, '.' , transform = ccrs.Geodetic (), label = name) plt. legend (loc = "upper left" ) plt.show ()

import cartopy.crs as ccrs import cartopy.feature as cfeature

These modules are important for displaying data.

We will add significant physical features to the map.

Exit : This article is courtesy of Amartya Ranjan Saikia . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.