-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first function to download AIS data from Kystverket
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from wavy.credentials import credentials_from_netrc | ||
import requests | ||
import xarray as xr | ||
import pandas as pd | ||
import os | ||
import json | ||
|
||
|
||
def get_AIS_data(bbox, sd, ed, minspeed=0.5): | ||
''' | ||
Args: | ||
bbox: list of strings [lon min, lat min, | ||
lon max, lat max] | ||
sd, ed: start and end dates, as strings | ||
under "yyyymmddHHMM" format | ||
''' | ||
# get token | ||
auth = credentials_from_netrc('kystdatahuset.no') | ||
url = "https://kystdatahuset.no/ws/api/auth/login" | ||
data = {"username": auth[0], "password": auth[1]} | ||
headers = {'accept': '*/*', 'Content-Type': 'application/json'} | ||
response = requests.post(url, json=data, headers=headers, auth=auth) | ||
resp_json = response.json() | ||
token = resp_json['data']['JWT'] | ||
|
||
# prepare curl request | ||
curl_com = "curl -X 'POST' " | ||
url_req = "'https://kystdatahuset.no/ws/api/ais/positions/within-bbox-time' " | ||
head_1 = "-H 'accept: text/plain' " | ||
head_2 = "-H 'Authorization: Bearer " + token + "' " | ||
head_3 = "-H 'Content-Type: application/json' " | ||
|
||
dict_data = { | ||
"bbox": ",".join(bbox), | ||
"start": sd, | ||
"end": ed, | ||
"minSpeed": minspeed | ||
} | ||
|
||
data = "-d '" + str(dict_data).replace("'", '"') + "'" | ||
|
||
curl_request = curl_com + url_req + head_1 + head_2 + head_3 + data | ||
|
||
# get result content of curl request | ||
stream = os.popen(curl_request) | ||
output = stream.read() | ||
dict_output = json.loads(output) | ||
|
||
df_request = pd.DataFrame( | ||
data=dict_output["data"], | ||
columns=["mmsi", "datetime", "lon", | ||
"lat", "x1", "x2", "x3", "x4", | ||
"x5", "x6"]) | ||
|
||
# Get ship info | ||
unique_mmsi = [int(mm) for mm in df_request["mmsi"].unique()] | ||
url_ships_nsr = 'https://kystdatahuset.no/ws/api/ship/data/nsr/for-mmsis-imos' | ||
json_ships_nsr = {"mmsi": unique_mmsi, "imo": [], "callsign": []} | ||
headers_ships_nsr = {'accept': 'test/plain', | ||
'Content-Type': 'application/json'} | ||
response_ships_nsr = requests.post(url_ships_nsr, | ||
json=json_ships_nsr, | ||
headers=headers_ships_nsr) | ||
resp_ships_json_nsr = response_ships_nsr.json() | ||
ships_df = pd.DataFrame(resp_ships_json_nsr['data']) | ||
ships_df = ships_df[['mmsino', 'grosstonnage', 'dwt', 'length', | ||
'breadth', 'draught', 'shiptypeeng']] | ||
|
||
# Merge the two dataframes | ||
df_merge = pd.merge(df_request, ships_df, left_on="mmsi", | ||
right_on="mmsino", how="left") | ||
df_merge = df_merge[['mmsi', 'datetime', 'lon', 'lat', | ||
'grosstonnage', 'dwt', 'length', 'breadth', | ||
'draught', 'shiptypeeng']] | ||
df_merge = df_merge.rename(columns={'lon': 'lons', 'lat': 'lats', | ||
'datetime': 'time'}) | ||
|
||
# Turn the dataframe into a dataset | ||
df_merge['time'] = pd.to_datetime(df_merge['time']) | ||
ds = df_merge.set_index(['time']) | ||
ds = ds.to_xarray().sortby("time") | ||
|
||
return ds |