Justin Blancher's ACFST project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

74 lines
3.0 KiB

#!/usr/local/python3.7/bin/python3
from datetime import datetime
import time
import math
import requests
import json
import csv
from os import path
import sys
min_speed=10.0
min_width=15.0
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.
"""
matrix = []
req_time=datetime.today().strftime("%m/%d/%Y, %H:%M:%S")
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]
lat = shipdata[3]
lon = shipdata[4]
parameters = {'latitude': lat, 'longitude': lon}
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])
ship_speed_ms=ship_speed_kts/1.944
froude=ship_speed_ms/math.sqrt(9.8*depth)
if shipdata[23]=='Tug':
alpha_1=1.0
else:
alpha_1=0.35
distance_interest=[1, 1000, 2000, 3000]
for dist in distance_interest:
wave_height=depth*alpha_1*((dist/depth)**-0.33)*froude**4
shipdata.extend([round(wave_height,2)])
matrix.append(shipdata)
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
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'
]
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)
outfile.close()