2019-08-11 15:30:14 -04:00
|
|
|
#!/usr/local/python3.7/bin/python3
|
|
|
|
from datetime import datetime
|
2019-08-03 12:55:52 -04:00
|
|
|
import time
|
2019-08-11 15:30:14 -04:00
|
|
|
import math
|
2019-08-03 12:55:52 -04:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import csv
|
2019-08-11 15:30:14 -04:00
|
|
|
from os import path
|
2019-08-03 12:55:52 -04:00
|
|
|
import sys
|
|
|
|
|
2019-08-11 15:30:14 -04:00
|
|
|
min_speed=10.0
|
|
|
|
min_width=15.0
|
2019-08-03 12:55:52 -04:00
|
|
|
|
|
|
|
def getships():
|
|
|
|
"""
|
|
|
|
Retrieves MarineTraffic.com Extended Data from the API, using the ACF.ShipTracker@gmail.com API key.
|
|
|
|
Collates the data, records the request time.
|
|
|
|
If a vessel has a speed over 10 kts, the estimated wave height at distance 1m, 1000m, 2000m, and 3000m, are recorded.
|
|
|
|
This is to be used in concert with the rest of ACF_ShipTracker python script to save as a CSV and email once a day.
|
|
|
|
"""
|
2019-08-11 15:30:14 -04:00
|
|
|
matrix = []
|
2019-08-11 21:29:27 -04:00
|
|
|
req_time=datetime.utcnow().strftime("%FT%H:%M:%S")
|
2019-08-11 15:30:14 -04:00
|
|
|
redata = json.loads(requests.get(
|
|
|
|
'https://services.marinetraffic.com/api/exportvessels/v:8/ea1f4a05afbd000ba30772a9979bf7aa743092cb/timespan:60/msgtype:extended/protocol:json').content)
|
|
|
|
for ship in range(len(redata)) :
|
|
|
|
ship_speed_kts=float(redata[ship][5])/10.0 #in kts
|
|
|
|
ship_width_m=float(redata[ship][17])
|
|
|
|
if ship_speed_kts >= min_speed and ship_width_m >= min_width:
|
|
|
|
shipdata = redata[ship]
|
2019-08-03 12:55:52 -04:00
|
|
|
lat = shipdata[3]
|
|
|
|
lon = shipdata[4]
|
|
|
|
parameters = {'latitude': lat, 'longitude': lon}
|
2019-08-11 15:30:14 -04:00
|
|
|
depth = float(str(requests.get('https://www.gmrt.org/services/PointServer?', params=parameters).content, 'utf-8'))*-1 # depth in m
|
|
|
|
shipdata.extend([req_time, depth])
|
2019-08-03 12:55:52 -04:00
|
|
|
ship_speed_ms=ship_speed_kts/1.944
|
|
|
|
froude=ship_speed_ms/math.sqrt(9.8*depth)
|
|
|
|
if shipdata[23]=='Tug':
|
2019-08-11 15:30:14 -04:00
|
|
|
alpha_1=1.0
|
2019-08-03 12:55:52 -04:00
|
|
|
else:
|
|
|
|
alpha_1=0.35
|
|
|
|
distance_interest=[1, 1000, 2000, 3000]
|
2019-08-11 15:30:14 -04:00
|
|
|
for dist in distance_interest:
|
|
|
|
wave_height=depth*alpha_1*((dist/depth)**-0.33)*froude**4
|
2019-08-03 12:55:52 -04:00
|
|
|
shipdata.extend([round(wave_height,2)])
|
|
|
|
matrix.append(shipdata)
|
2019-08-11 15:30:14 -04:00
|
|
|
time_ships=datetime.today().strftime("%m/%d/%Y, %H:%M:%S")
|
|
|
|
print('Ship data collected at {}'.format(time_ships), file=sys.stderr)
|
|
|
|
print('\n'.join(map(str, matrix)), file=sys.stderr)
|
|
|
|
return matrix
|
2019-08-03 12:55:52 -04:00
|
|
|
|
|
|
|
rowheadings=[
|
|
|
|
'MMSI', 'IMO', 'SHIP_ID', 'LAT', 'LON', 'SPEED',
|
|
|
|
'HEADING', 'COURSE', 'STATUS', 'TIMESTAMP', 'DSRC', 'UTC_SECONDS',
|
|
|
|
'SHIPNAME', 'SHIPTYPE', 'CALLSIGN', 'FLAG', 'LENGTH', 'WIDTH', 'GRT', 'DWT',
|
|
|
|
'DRAUGHT', 'YEAR_BUILT', 'ROT', 'TYPE_NAME', 'AIS_TYPE_SUMMARY',
|
|
|
|
'DESTINATION', 'ETA', 'Request Time', 'DEPTH','Est Wave Height 1m', 'Est Wave Height 1000m',
|
|
|
|
'Est Wave Height 2000m', 'Est Wave Height 3000m'
|
|
|
|
]
|
|
|
|
|
2019-08-11 21:29:27 -04:00
|
|
|
# The following is not UTC, since /etc/localtime is America/New_York
|
2019-08-11 15:30:14 -04:00
|
|
|
x2 = datetime.now()
|
|
|
|
matrix = getships()
|
|
|
|
csv_name = "/var/log/acfst/ACFshipdata_{}-{}-{}.csv".format(x2.year, '{:02d}'.format(x2.month), '{:02d}'.format(x2.day))
|
|
|
|
if path.exists(csv_name): # ship log exists
|
|
|
|
with open(csv_name, 'a') as outfile:
|
|
|
|
writer = csv.writer(outfile)
|
|
|
|
writer.writerows(matrix)
|
|
|
|
outfile.close()
|
|
|
|
else:
|
|
|
|
with open(csv_name, 'w+') as outfile:
|
|
|
|
writer = csv.writer(outfile)
|
|
|
|
writer.writerow(rowheadings)
|
|
|
|
writer.writerows(matrix)
|
2019-08-03 12:55:52 -04:00
|
|
|
outfile.close()
|
|
|
|
|