-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape-noaa.php
More file actions
320 lines (263 loc) · 7.8 KB
/
Copy pathscrape-noaa.php
File metadata and controls
320 lines (263 loc) · 7.8 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
// if set to 0 will attempt to find all images currently stored
$GET_LATEST = 1;
// echo PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL.'SCRAPE NOAA GO > '.date('r').PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL;
$DATA_FOLDER = '/var/www/html/projects/noaa/scraped/';
$DATA_CSV = 'scraped-data.csv';
$URL = 'https://www.ndbc.noaa.gov/images/buoycam/';
function timer()
{
static $startTIME;
if (is_null($startTIME)) {
$startTIME = microtime(true);
} else {
$diff = round((microtime(true) - $startTIME), 4);
$startTIME = null;
return $diff;
}
}
function convertToKnots($wind_ms)
{
return (float) $wind_ms * 1.943844;
}
function getBeaufortForceFromWind($windFloat)
{
$knots = convertToknots($windFloat);
if ($knots < 1) {
return 0;
} elseif ($knots < 4) {
return 1;
} elseif ($knots < 7) {
return 2;
} elseif ($knots < 11) {
return 3;
} elseif ($knots < 17) {
return 4;
} elseif ($knots < 22) {
return 5;
} elseif ($knots < 28) {
return 6;
} elseif ($knots < 34) {
return 7;
} elseif ($knots < 41) {
return 8;
} elseif ($knots < 48) {
return 9;
} elseif ($knots < 56) {
return 10;
} elseif ($knots < 64) {
return 11;
} elseif ($knots >= 64) {
return 12;
}
}
function getBeaufortForceFromWave($waveFloat)
{
//#sea like mirror
if ($waveFloat < .1) {
return 0;
}
//# .1
elseif ($waveFloat < .2) {
return 1;
}
//# .2 - .3
elseif ($waveFloat < .6) {
return 2;
}
//# .6 - 1
elseif ($waveFloat < 1) {
return 3;
}
//# 1 - 1.5
elseif ($waveFloat < 2) {
return 4;
}
//# 2 - 2.5
elseif ($waveFloat < 3) {
return 5;
}
//# 3 - 4
elseif ($waveFloat < 4) {
return 6;
}
//# 4 - 5.5
elseif ($waveFloat < 5.5) {
return 7;
}
//# 5.5 - 7.5
elseif ($waveFloat < 7.5) {
return 8;
}
//# 7 - 10
elseif ($waveFloat < 10) {
return 9;
}
//# 9 - 12.5
elseif ($waveFloat < 12.5) {
return 10;
}
//# 11.5 - 16
elseif ($waveFloat < 16) {
return 11;
}
//# POSEIDON's WRATH
elseif ($waveFloat >= 16) {
return 12;
}
}
function fillzero($text, $zeros = 2)
{
return str_pad($text, $zeros, '0', STR_PAD_LEFT);
}
// constructs the image url from the individual components
// Z92A_2019_04_10_0110.jpg
function constructImageUrl($station_code, $year, $month, $day, $hour, $minute)
{
global $URL;
return $URL.$station_code.'_'.$year.'_'.fillzero($month).'_'.fillzero($day).'_'.fillzero($hour).fillzero($minute).'.jpg';
}
// store the image and the data in the dataset
function saveImage($image)
{
global $DATA_CSV, $DATA_FOLDER, $URL;
$image_name = str_replace($URL, '', $image['image_url']);
// PHP filesystem path normalization
$csv_path = realpath($DATA_FOLDER.$DATA_CSV);
$data = file_get_contents($csv_path);
// check to make sure the image isn't already stored
if (strpos($data, $image_name) !== false) {
return false;
}
// only use the beaufort force from wind stat since wave data incomplete
$new_data = $image_name.','.$image['data']['WSPD'].','.$image['data']['WVHT'].','.getBeaufortForceFromWind($image['data']['WSPD']);
// concatenate data on the end of the string
file_put_contents($csv_path, $data.PHP_EOL.$new_data);
$image_path = str_replace($DATA_CSV, $image_name, $csv_path);
file_put_contents($image_path, file_get_contents($image['image_url']));
return true;
}
function getLatestImage($station_id, $station_code)
{
// pull the last 5 days of data
$log = @file_get_contents('https://www.ndbc.noaa.gov/data/5day2/'.$station_id.'_5day.txt');
// fallback to the 45 day data set if 5 day data set doesn't exist.
if ($log == false) {
$log = @file_get_contents('https://www.ndbc.noaa.gov/data/realtime2/'.$station_id.'.txt');
if ($log == false) {
return 'ERROR: weather data not found';
}
}
$log = explode("\n", $log);
// get column headers; split by any whitespace
$headers = preg_split('/\s+/', $log[0]);
// remove the header information
unset($log[0]);
unset($log[1]);
$count = 0;
$since_last_image = 0;
foreach ($log as $row) {
$data = preg_split('/\s+/', $row);
++$count;
$image_url = constructImageUrl($station_code, $data[0], $data[1], $data[2], $data[3], $data[4]);
$file_headers = @get_headers($image_url);
// if it's been too long since the last image, break out. You won't find more here.
if ($since_last_image > 9) {
break;
}
// skip if file doesn't exist
if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
++$since_last_image;
continue;
}
$since_last_image = 0;
return [[
'image_url' => $image_url,
'data' => array_combine($headers, $data),
]];
}
return 'ERROR: image not found';
}
function getAllStoredImages($station_id, $station_code)
{
// pull the last 5 days of data
$log = @file_get_contents('https://www.ndbc.noaa.gov/data/5day2/'.$station_id.'_5day.txt');
// fallback to the 45 day data set if 5 day data set doesn't exist.
if ($log == false) {
$log = @file_get_contents('https://www.ndbc.noaa.gov/data/realtime2/'.$station_id.'.txt');
if ($log == false) {
return 'ERROR: weather data not found';
}
}
$log = explode("\n", $log);
// get column headers; split by any whitespace
$headers = preg_split('/\s+/', $log[0]);
// remove the header information
unset($log[0]);
unset($log[1]);
$count = 0;
$since_last_image = 0;
$images = [];
foreach ($log as $row) {
$data = preg_split('/\s+/', $row);
++$count;
$image_url = constructImageUrl($station_code, $data[0], $data[1], $data[2], $data[3], $data[4]);
$file_headers = @get_headers($image_url);
// if it's been too long since the last image, break out. You won't find more here.
if ($since_last_image > 9) {
break;
}
// skip if file doesn't exist
if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
++$since_last_image;
continue;
}
$since_last_image = 0;
$images[] = [
'image_url' => $image_url,
'data' => array_combine($headers, $data),
];
}
return $images;
}
/********************** MAIN **************************/
// echo '<pre>';
$buoycams = json_decode(file_get_contents('https://www.ndbc.noaa.gov/buoycams.php'));
foreach ($buoycams as $buoy) {
if ($buoy->img == null) {
continue;
}
// profiling for retriving image list
timer();
$station_code = explode('_', $buoy->img)[0];
echo PHP_EOL.' > buoy #'.$buoy->id.' '.$station_code;
if ($GET_LATEST == 1) {
$images = getLatestImage($buoy->id, $station_code);
} elseif ($GET_LATEST == 0) {
$images = getAllStoredImages($buoy->id, $station_code);
} else {
die('$GET_LATEST must be 1 or 0');
}
echo ' ('.timer().'s)';
echo PHP_EOL;
// if there was an error display the error
if (!is_array($images)) {
echo $images.PHP_EOL;
continue;
}
foreach ($images as $latest_image) {
echo str_replace($URL, '', $latest_image['image_url'])."\t";
if (isset($latest_image['image_url'])) {
timer();
if (saveImage($latest_image)) {
echo 'Image stored.';
} else {
echo 'Image already stored.';
}
echo ' ('.timer().'s)';
echo PHP_EOL;
} else {
echo $latest_image;
}
}
}