Commit 35248126 authored by Emma Fritzberg's avatar Emma Fritzberg
Browse files

set up repo

parent 27329d3a
# Sample GitLab Project
# Why do some ocean drifters beach while others do not?
This sample project shows how a project in GitLab looks for demonstration purposes. It contains issues, merge requests and Markdown files in many branches,
named and filled with lorem ipsum.
## Description
This project aims to study the long-time transport of floating debris in the ocean and determine the likelihood that plastic pollution will either beach or remain in the open ocean perpetually. The project will use data from [Sofar]()'s drifting ocean wave buoys over time. The drifters will represent model debris that float at the surface. Their trajectories will be analyzed in order to determine the circulation patterns that result in debris beaching or accumulating in attracting regions (such as the subtropical gyres).
You can look around to get an idea how to structure your project and, when done, you can safely delete this project.
## People
[Learn more about creating GitLab projects.](https://docs.gitlab.com/ee/gitlab-basics/create-project.html)
### PI
[Michelle DiBennedetto](https://www.me.washington.edu/facultyfinder/michelle-dibenedetto)
mdiben@uw.edu
### Researcher
Emma Fritzberg
efritz19@cs.washington.edu
This diff is collapsed.
%% Cell type:code id: tags:
```
```
import csv
# import numpy as npl
from datetime import datetime, timedelta
from geopy import distance
from matplotlib import pyplot as plt
from cartopy import crs as ccrs
import math
TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.000Z'
def get_datetime(timestamp):
return datetime.strptime(timestamp, TIMESTAMP_FORMAT)
# Parse CSV data into global var spotters with a dict representing spotters and their paths.
#
# Each key is a spotter ID and its associated value is a dict representing the
# path of the spotter with that ID, in which keys are timestamp strings and
# values are tuples containing the location in the format (latitude, longitude).
#
# key: value:
# spotter ID --> [
# sub-key: sub-value:
# timestamp --> (latitude, longitude),
# timestamp --> (latitude, longitude),
# timestamp --> (latitude, longitude),
# ]
spotters = {}
with open('../data/all_time_spotter_tracks.csv') as raw_data:
reader = csv.DictReader(raw_data)
for row in reader:
spotter_id = row['spotterId']
timestamp = row['timestamp']
latitude = float(row['latitude'])
longitude = float(row['longitude'])
if spotter_id in spotters:
# Assumption: each spotter has only one recorded location for a given timestamp
spotters[spotter_id][timestamp] = (latitude, longitude)
else:
path = dict([(timestamp, (latitude, longitude))])
spotters[spotter_id] = path
# Returns time delta between two timestamps in hours
def time_elapsed(start_timestamp, end_timestamp):
return (get_datetime(end_timestamp) - get_datetime(start_timestamp)) / timedelta(hours=1)
# Returns distance between two coordinates in kilometers, as a floating point number
def distance_travelled(start_location, end_location):
return distance.distance(start_location, end_location).km
# Returns segment speed in kilometers per hour
def segment_speed(start, end):
start_timestamp, start_location = start
end_timestamp, end_location = end
return distance_travelled(start_location, end_location) / time_elapsed(start_timestamp, end_timestamp)
# Returns the average speed at which the spotter traveled its path, in kilometers per hour
def average_speed(spotter_id):
path = spotters[spotter_id]
total_distance = 0
total_time = 0
timestamps = list(path.keys())
timestamps.sort(key=(lambda e: get_datetime(e)))
for i in range(len(timestamps) - 1):
start_timestamp = timestamps[i]
end_timestamp = timestamps[i + 1]
total_time += time_elapsed(start_timestamp, end_timestamp)
total_distance += distance_travelled(path[start_timestamp], path[end_timestamp])
if total_time > 0:
return total_distance / total_time
# Returns the average velocity over a segment, in kilometers per hour
def average_segment_velocity(start_index, end_index, sorted_timestamps, path):
return distance_travelled(path[sorted_timestamps[start_index]], path[sorted_timestamps[end_index]]) / time_elapsed(sorted_timestamps[start_index], sorted_timestamps[end_index])
# Returns the velocity of the spotter at the given time
def time_resolved_velocity(spotter_id, time):
path = spotters[spotter_id]
timestamps = list(path.keys())
if len(timestamps) <= 1:
# Not enough data to calculate velocity
return math.nan
timestamps.sort(key=(lambda e: get_datetime(e)))
start_time = get_datetime(timestamps[0])
end_time = get_datetime(timestamps[-1])
if time < start_time or end_time < time:
# No data for given time
return math.nan
if time == start_time:
# TODO: At first timestamp, use forward Euler
return math.nan
if time == end_time:
# TODO: At last timestamp, use backward Euler
return math.nan
for i in range(len(timestamps)):
if time == get_datetime(timestamps[i]):
# At intermediary timestamp, use centered-in-time numerical differentiation
return average_segment_velocity(i - 1, i + 1, timestamps, path)
if get_datetime(timestamps[i]) < time and time < get_datetime(timestamps[i + 1]):
# Between timestamps, use segment average velocity
return average_segment_velocity(i, i + 1, timestamps, path)
return math.nan
def plot_single_trajectory(spotter_id):
path = spotters[spotter_id]
timestamps = list(path.keys())
timestamps.sort(key=(lambda e: get_datetime(e)))
(start_latitude, start_longitude) = path[timestamps[0]]
ax = plt.axes(projection=ccrs.Orthographic(central_longitude=start_longitude, central_latitude=start_latitude))
ax.set_global()
ax.coastlines()
for i in range(len(timestamps) - 1):
(start_latitude, start_longitude) = path[timestamps[i]]
print('start: lat: ', start_latitude, ' , long: ', start_longitude)
(end_latitude, end_longitude) = path[timestamps[i + 1]]
print('end: lat: ', end_latitude, ' , long: ', end_longitude)
velocity = average_segment_velocity(i, i + 1, timestamps, path)
print('velocity: ', velocity, 'km/h')
velocity_color = 'gray'
if not math.isnan(velocity):
if velocity < 0.5:
velocity_color = 'blue'
if velocity >= 0.5:
velocity_color = 'purple'
if velocity >= 1.0:
velocity_color = 'red'
plt.plot([start_longitude, end_longitude], [start_latitude, end_latitude], color=velocity_color, transform=ccrs.Geodetic())
plt.show()
def plot_all_trajectories():
ax = plt.axes(projection=ccrs.Mollweide())
ax.set_global()
ax.coastlines()
print(len(spotters.keys()))
#for spotter_id in spotters.keys():
for s in range(50):
spotter_id = list(spotters.keys())[s]
print(spotter_id)
path = spotters[spotter_id]
timestamps = list(path.keys())
timestamps.sort(key=(lambda e: get_datetime(e)))
for i in range(len(timestamps) - 1):
(start_latitude, start_longitude) = path[timestamps[i]]
#print('start: lat: ', start_latitude, ' , long: ', start_longitude)
(end_latitude, end_longitude) = path[timestamps[i + 1]]
#print('end: lat: ', end_latitude, ' , long: ', end_longitude)
velocity = average_segment_velocity(i, i + 1, timestamps, path)
#print('velocity: ', velocity, 'km/h')
velocity_color = 'gray'
if not math.isnan(velocity):
# TODO: make continuous map of speed
if velocity < 0.5:
velocity_color = 'blue'
if velocity >= 0.5:
velocity_color = 'purple'
if velocity >= 1.0:
velocity_color = 'red'
plt.plot([start_longitude, end_longitude], [start_latitude, end_latitude], color=velocity_color, transform=ccrs.Geodetic())
plt.show()
with open('test.csv', 'w', newline='') as enriched_data:
fieldnames = ['spotter_id', 'timestamp', 'latitude', 'longitude', 'velocity', 'coastline_distance']
writer = csv.DictWriter(enriched_data, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'spotter_id': 'SPOT-0284', 'coastline_distance': 1, 'velocity': 0})
# Example usage:
#spotter_id = 'SPOT-0284'
#plot_single_trajectory(spotter_id)
#plot_all_trajectories()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment