-
Notifications
You must be signed in to change notification settings - Fork 2
/
getShipsAIS_CINMS_B_201204.m
207 lines (181 loc) · 7.84 KB
/
getShipsAIS_CINMS_B_201204.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
% stolen from getShipsAIS_CINMS_B_180820.m -> BJT
% to work with Shipplotter decoded AIS files
% 180912 smw
clear variables
% set AIS decoded file type
aisType = 3; % SBARC=1, Shipplotter=2, Marine Cadastre
% nominal site B location is CINMS_B_30_00
% ~7.6 km from edge of southbound side of the shipping lane
siteB = [ 34.2755, -120.0185 ];
rlat = siteB(1) * pi/180;
% Ref: American Practical Naviagator, Bowditch 1958, table 6 (explanation)
% page 1187
m = 111132.09 - 566.05 * cos(2*rlat) + 1.2 * cos(4*rlat) - 0.003 * cos(6*rlat);
p = 111415.10 * cos(rlat) - 94.55 * cos(3*rlat) - 0.12 * cos(5*rlat);
% for starters, let's filter for any ships inside some lat/lon box
boundd_m = 20e3; % in [m]
maxLat = [ siteB(1)-boundd_m/m, siteB(1)+boundd_m/m ];
maxLon = [ siteB(2)-boundd_m/p, siteB(2)+boundd_m/p ];
switch aisType
case 1
% aisFiles = {
% 'D:\Projects\ShippingCINMS\AIS\SBARC\AIS_SBARC_180713.txt';
% 'D:\Projects\ShippingCINMS\AIS\SBARC\AIS_SBARC_180714.txt';
% 'D:\Projects\ShippingCINMS\AIS\SBARC\AIS_SBARC_180715.txt';
% 'D:\Projects\ShippingCINMS\AIS\SBARC\AIS_SBARC_180716.txt';
% 'D:\Projects\ShippingCINMS\AIS\SBARC\AIS_SBARC_180717.txt';
% 'D:\Projects\ShippingCINMS\AIS\SBARC\AIS_SBARC_180718.txt';
% };
idir = 'D:\Projects\ShippingCINMS\AIS\SBARC\';
aisFiles = ls(fullfile(idir,'AIS_SBARC_*.txt'));
odir = 'G:\ShippingCINMS\matFiles';
case 2
% idir = 'P:\ShippingCINMS\AIS\testCOP\';
idir = 'P:\ShippingCINMS\AIS\COP\';
aisFiles = ls(fullfile(idir,'shipplotter*.log'));
% odir = 'P:\ShippingCINMS\matFilesTest';
odir = 'P:\ShippingCINMS\matFilesCOP';
case 3
% marine cadastre csv format (not geodatabases from 2014 and
% earlier)
idir = 'F:\MarineCadastre\CINMSonly';
aisFiles = ls(fullfile(idir,'AIS*10-11*.csv'));
odir = 'F:\MarineCadastre\matFilesMarCad';
otherwise
disp('unknown decoded AIS type')
end
if ~isfolder(odir) % no folder? make it
mkdir(odir)
end
NF = size(aisFiles,1);
for f = 1:NF
% aisFile = aisFiles{f};
aisFile = fullfile(idir,aisFiles(f,:));
% if ~isempty(strfind(aisFile,'NMEA'))
if contains(aisFile,'NMEA') || contains(aisFile,'nmea')...
&& aisType~=3
%don't want to use coded AIS
continue
end
% odir = 'G:\ShippingCINMS\matFiles';
% ofn = sprintf('siteB_AIS_10km_%s.mat',...
% datestr(floor(shipTracks(1).dnums(1)),'yymmdd'));
if aisType ~=3
dstamp = regexp(aisFile,'[0-9]{6}','match');
else
dstampOrig = char(regexp(aisFile,'[0-9]{4}_[0-9]{2}','match'));
dstamp = {datestr(datenum([str2num(dstampOrig(1:4)),str2num(dstampOrig(6:7)),01]),'yymmdd')};
end
ofn = sprintf('siteB_AIS_%dm_%s.mat',boundd_m,dstamp{1});
offn = fullfile(odir,ofn);
% if exist(offn,'file') == 2 % output exists, don't process input
% disp([offn,' Output file already exist'])
% continue;
% end
finfo = dir(aisFile);
% on BJT machine processes ~350 kbytes/sec
est_rt = (finfo.bytes/350e3)/60;
[ ~, fname, ext ] = fileparts(aisFile);
fprintf('%s: %s%s ~%.2f minutes to process\n', ...
datestr(now,'mm/dd/yy HH:MM:SS'),fname,ext, est_rt);
% tic
try
switch aisType
case 1
[ msg13_char, msg13_num, msg5_char, msg5_num ] = parseDecodedSBARC_180814(aisFile);
case 2
[ msg13_char, msg13_num, msg5_char, msg5_num ] = parseDecodedShipplotter_180912(aisFile);
case 3
[ msg13_char, msg13_num, msg5_char, msg5_num ] = parseDecodedMarCadastre(aisFile);
otherwise
disp('unknown decoded AIS type')
end
catch ME
fprintf('\tParse fail: %s\n',ME.message);
continue
end
% toc
% work on message IDs 1-3 first
lats = msg13_num(:,6);
lons = msg13_num(:,5);
% msg13_char0 = msg13_char;
% msg13_num0 = msg13_num;
xIdx = find(lats >= maxLat(1) & lats <= maxLat(2));
yIdx = find(lons >= maxLon(1) & lons <= maxLon(2));
clear lats lons
gIdx = intersect(xIdx, yIdx);
msg13_char = msg13_char(gIdx,:);
msg13_num = msg13_num(gIdx,:);
if ~isempty(msg13_num)
shipTracks = struct;
% get unique ships via MMSI number
uShips = unique(msg13_num(:,2));
fprintf('\t%d ships found:\n',length(uShips));
sInc = 1;
for s = 1:length(uShips)
s13idx = find(msg13_num(:,2)==uShips(s));
s5idx = find(msg5_num(:,2)==uShips(s));
switch aisType
case 1
shipTracksTemp.name = unique(msg13_char(s13idx,1));
case 2
shipTracksTemp.name = unique(msg5_char(s5idx,1));
case 3
shipTracksTemp.name = unique(msg13_char(s13idx,1));
s5idx = s13idx;
end
if isempty(shipTracksTemp.name)
shipTracksTemp.name{1} = 'unknown';
end
fprintf('\t%s\n',shipTracksTemp.name{1});
% sort times
[timeStamps,timeIdx] = sort(msg13_num(s13idx,1));
% decide if there are multiple passages of the same vessel.
% by looking for gaps of >=30mins
tDiff = diff(timeStamps);
transitGaps = [0,find(tDiff>=(1/(24*4)))',length(timeStamps)];
uTransits = length(transitGaps)-1;
fprintf('\t\t%0.0f Passage(s) Found\n',uTransits);
if uTransits>5
1;
end
tStart = 1;
thisShip_char_m5 = msg5_char(s5idx(timeIdx),:);
thisShip_num_m13 = msg13_num(s13idx(timeIdx),:);
thisShip_num_m5 = msg5_num(s5idx(timeIdx),:);
for iT = 1:uTransits
transitIdx = transitGaps(tStart)+1:transitGaps(tStart+1);
[~,uTimes] = unique(thisShip_num_m13(transitIdx,1));
shipTracks(sInc).dnums = thisShip_num_m13(transitIdx(uTimes),1);
shipTracks(sInc).name = shipTracksTemp.name;
shipTracks(sInc).shipType = unique(thisShip_char_m5(transitIdx(uTimes),2));
shipTracks(sInc).MMSI = uShips(s);
shipTracks(sInc).IMO = unique(thisShip_num_m5(transitIdx(uTimes),3));
shipTracks(sInc).lons = thisShip_num_m13(transitIdx(uTimes),5);
shipTracks(sInc).lats = thisShip_num_m13(transitIdx(uTimes),6);
shipTracks(sInc).SOG = thisShip_num_m13(transitIdx(uTimes),4);
shipTracks(sInc).COG = thisShip_num_m13(transitIdx(uTimes),7);
shipTracks(sInc).trueHeading = thisShip_num_m13(transitIdx(uTimes),8);
% voyage data - datenum, IMO, draught, dimensions ( toBow, toStern, toPort,
% toStarboard )
if aisType ~=3
shipTracks(sInc).vData = [ thisShip_num_m5(transitIdx(uTimes),1), thisShip_num_m5(transitIdx(uTimes),3:8) ];
else
shipTracks(sInc).vData = [thisShip_num_m13(transitIdx(uTimes),1),thisShip_num_m5(transitIdx(uTimes),1),...
thisShip_num_m5(transitIdx(uTimes),2:6) ];
end
%
% figure(101);
% plot(shipTracks(sInc).lats,shipTracks(sInc).lons,'o')
% hold on
% plot(siteB(1),siteB(2),'ok')
% hold off
tStart = tStart+1;
sInc = sInc+1;
end
end
save(offn,'shipTracks');
else
fprintf('No ships within bounds found\n');
end
end