-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-usno-data.php
314 lines (267 loc) · 10.2 KB
/
get-usno-data.php
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
<?php
# ------------------------------------------------------------------------
#
# Program: get-usno-data.php
#
# Purpose: fetch one day's sun and moon x,y (azimuth,elevation) data from
# the USNO webpages and save data in usno-sunmoon-data.php
# That data with sunposa.php log of calc=sunmoon-data.php can
# be compared with check-sunmoon-data.php to validate proper
# working of sunposa.php display script.
#
# Author: Ken True - webmaster@saratoga-weather.org
#
# Version 1.00 - 26-Aug-2024 - initial release
#
# ------------------------------------------------------------------------
# SETTINGS -- overriden by calc-sunmoon-data.php if available
$lat = 37.2715;
$lon = -122.02274;
$tz = "America/Los_Angeles";
$outFile = 'usno-sunmoon-data.php';
$saveCacheFiles = false; # =true; to save usno-sun-html.txt and usno-moon-html.txt
# END of SETTINGS
# ------------------------------------------------------------------------
$Version ="get-usno-data.php V1.00 - 26-Aug-2024";
header('Content-type: text/plain,charset=ISO-8859-1');
print "$Version\n";
if(file_exists('calc-sunmoon-data.php')) {
include_once('calc-sunmoon-data.php');
/*
$calcMeta =array (
'date' => '2024-08-26',
'lat' => 37.2715,
'lon' => -122.02274,
'tz' => 'America/Los_Angeles',
'version' => 'sunposa.php Version 3.04 - 26-Aug-2024',
);
*/
$date = $calcMeta['date'];
$lat = $calcMeta['lat'];
$lon = $calcMeta['lon'];
$tz = $calcMeta['tz'];
date_default_timezone_set($tz);
$tzU = date("Z",strtotime($date))/3600;
if($tzU <0) {$tzSign = -1;} else {$tzSign = 1;}
#$tzSign = date("I")=='1'?-1:0;
$tzU = abs($tzU);
print "..using sunposa.php date=$date lat=$lat, lon=$lon tz=$tz tzU=$tzU tzSign=$tzSign\n";
} else {
date_default_timezone_set($tz);
$date = date("Y-m-d");
$tzU = date("Z")/3600;
if($tzU <0) {$tzSign = -1;} else {$tzSign = 1;}
#$tzSign = date("I")=='1'?-1:0;
$tzU = abs($tzU);
print "..using builtin date=$date lat=$lat, lon=$lon tz=$tz tzU=$tzU tzSign=$tzSign\n";
}
$sunType = 10;
$moonType = 11;
$dataMeta = array('date'=>$date,'lat'=>$lat,'lon'=>$lon,'tz'=>$tz,'tzSign'=>$tzSign,'version'=>$Version);
# sun data:
$SunURL = "https://aa.usno.navy.mil/calculated/altaz?body=10&date=$date&intv_mag=10&lat=$lat&lon=$lon&label=&tz=$tzU&tz_sign=$tzSign";
# https://aa.usno.navy.mil/calculated/altaz?body=10&date=2024-08-24&intv_mag=10&lat=37.27&lon=-122.02&label=Saratoga%2C+CA&tz=8&tz_sign=-1&submit=Get+Data
$MoonURL = "https://aa.usno.navy.mil/calculated/altaz?body=11&date=$date&intv_mag=10&lat=$lat&lon=$lon&label=&tz=$tzU&tz_sign=$tzSign";
#https://aa.usno.navy.mil/calculated/altaz?body=11&date=2024-08-24&intv_mag=10&lat=37.27&lon=-122.02&label=Saratoga%2C+CA&tz=8&tz_sign=-1&submit=Get+Data
#https://aa.usno.navy.mil/calculated/altaz?body=11&date=2024-08-24&intv_mag=10&lat=54.27&lon=4.02&label=London%2C+UK&tz=1&tz_sign=1&submit=Get+Data
$rawHTMLSun = fetchUrlWithoutHanging($SunURL);
if($saveCacheFiles) {
$didCache = file_put_contents("usno-sun-html.txt",$rawHTMLSun);
if($didCache) {
print " cache file for Sun data (usno-sun-html.txt) saved.\n";
} else {
print "-- Unable to save cache for Sun data (usno-sun-html.txt).\n";
}
}
$rawHTMLMoon = fetchUrlWithoutHanging($MoonURL);
if($saveCacheFiles) {
$didCache = file_put_contents("usno-moon-html.txt",$rawHTMLMoon);
if($didCache) {
print " cache file for Moon data (usno-moon-html.txt) saved.\n";
} else {
print "-- Unable to save cache for Moon data (usno-moon-html.txt).\n";
}
}
$rawHTMLSun = file_get_contents('usno-sun-html.txt');
$rawHTMLMoon = file_get_contents('usno-moon-html.txt');
$dataSun = parse_usno_html($rawHTMLSun);
$dataMoon = parse_usno_html($rawHTMLMoon);
$didIt = file_put_contents($outFile,
"<?php\n" .
"# generated by $Version on ".date('r')."\n" .
"# data for date=$date lat=$lat, lon=$lon tz=$tz tzU=$tzU tzSign=$tzSign\n" .
"#\n" .
"\$dataMeta = ".var_export($dataMeta,true).";\n" .
"#\n" .
"\$dataSun = ".var_export($dataSun,true).";\n" .
"#\n" .
"\$dataMoon = ".var_export($dataMoon,true).";\n"
);
if($didIt) {
print ".. data written to $outFile.\n";
} else {
print "-- Oops.. unable to write data to $outFile.\n";
}
print "..Done.\n";
function parse_usno_html($HTML) {
# <th colspan="4">Altitude and Azimuth of [Sun|Moon] is start
# </table> is end
$table = '';
$data = array();
$start = strpos($HTML,' Zone: ',0);
$end = strpos($HTML,'</table>',$start);
print " start=$start, end=$end length=".strlen($HTML)."\n";
#print $HTML;
#preg_match_all('!Zone: (.*)\<\/table\S>!Uis',$HTML,$M);
#var_export($M);
#return($data);
if($end >= $start) {
$table = substr($HTML,$start,$end-$start+1);
}
#print " ---------\n"
#print $table;
#print " ---------\n"
if(strlen($table) < 400) {
print "-- table not found\n";
return ($data);
} else {
print ".. table length=".strlen($table)."\n";
}
#print "\n ---------\n";
#var_export($table);
#print "\n ---------\n";
preg_match_all('!<tr>(.*)</tr>!Uis',$table,$rows);
#print "\n ---------\n";
#var_export($rows);
#print "\n ---------\n";
if(!isset($rows[0][0])) {
print "-- rows not found\n";
return ($data);
} else {
print ".. ".count($rows[1])." rows found.\n";
}
foreach ($rows[1] as $i => $tds) {
preg_match_all('!<td>(.*)</td>!Uis',$tds,$M);
#print " i=$i, M=".var_export($M,true).";\n";
if(isset($M[1][2])) {
$time = $M[1][0];
if(strpos($time,'Time') !== false) {continue;}
$altitude = preg_replace('![^\d\.\-]!Uis','',$M[1][1]);
$azimuth = preg_replace('![^\d\.\-]!Uis','',$M[1][2]);;
$data[$time] = "$azimuth,$altitude";
if(isset($M[1][3])) {
$data[$time] .= ",".$M[1][3];
}
}
}
return($data);
}
// get contents from one URL and return as string
// ---------------------------------------------------------------------------
function fetchUrlWithoutHanging($url)
{
global $needCookie;
// get contents from one URL and return as string
$Debug ='';
$overall_start = time();
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds = 30;
// Thanks to Curly from ricksturf.com for the cURL fetch functions
$data = '';
$domain = parse_url($url, PHP_URL_HOST);
$theURL = str_replace('nocache', '?' . $overall_start, $url); // add cache-buster to URL if needed
$Debug.= " curl fetching '$theURL' \n";
$ch = curl_init(); // initialize a cURL session
curl_setopt($ch, CURLOPT_URL, $theURL); // connect to provided URL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // don't verify peer certificate
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (usno-data.php - saratoga-weather.org)');
curl_setopt($ch, CURLOPT_HTTPHEADER, // request LD-JSON format
array(
"Accept: text/html,text/plain"
));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $numberOfSeconds); // connection timeout
curl_setopt($ch, CURLOPT_TIMEOUT, $numberOfSeconds); // data timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the data transfer
curl_setopt($ch, CURLOPT_NOBODY, false); // set nobody
curl_setopt($ch, CURLOPT_HEADER, true); // include header information
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow Location: redirect
// curl_setopt($ch, CURLOPT_MAXREDIRS, 1); // but only one time
if (isset($needCookie[$domain])) {
curl_setopt($ch, $needCookie[$domain]); // set the cookie for this request
curl_setopt($ch, CURLOPT_COOKIESESSION, true); // and ignore prior cookies
$Debug.= " cookie used '" . $needCookie[$domain] . "' for GET to $domain \n";
}
$data = curl_exec($ch); // execute session
if (curl_error($ch) <> '') { // IF there is an error
$Debug.= " curl Error: " . curl_error($ch) . " \n"; // display error notice
}
$cinfo = curl_getinfo($ch); // get info on curl exec.
/*
curl info sample
Array
(
[url] => http://saratoga-weather.net/clientraw.txt
[content_type] => text/plain
[http_code] => 200
[header_size] => 266
[request_size] => 141
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.125
[namelookup_time] => 0.016
[connect_time] => 0.063
[pretransfer_time] => 0.063
[size_upload] => 0
[size_download] => 758
[speed_download] => 6064
[speed_upload] => 0
[download_content_length] => 758
[upload_content_length] => -1
[starttransfer_time] => 0.125
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 74.208.149.102
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.1.104
[local_port] => 54156
)
*/
$Debug.= " HTTP stats: " . " RC=" . $cinfo['http_code'];
if (isset($cinfo['primary_ip'])) {
$Debug .= " dest=" . $cinfo['primary_ip'];
}
if (isset($cinfo['primary_port'])) {
$Debug.= " port=" . $cinfo['primary_port'];
}
if (isset($cinfo['local_ip'])) {
$Debug.= " (from sce=" . $cinfo['local_ip'] . ")";
}
$Debug.= "\n Times:" .
" dns=" . sprintf("%01.3f", round($cinfo['namelookup_time'], 3)) .
" conn=" . sprintf("%01.3f", round($cinfo['connect_time'], 3)) .
" pxfer=" . sprintf("%01.3f", round($cinfo['pretransfer_time'], 3));
if ($cinfo['total_time'] - $cinfo['pretransfer_time'] > 0.0000) {
$Debug.= " get=" . sprintf("%01.3f", round($cinfo['total_time'] - $cinfo['pretransfer_time'], 3));
}
$Debug.= " total=" . sprintf("%01.3f", round($cinfo['total_time'], 3)) . " secs \n";
// $Debug .= " curl info\n".print_r($cinfo,true)." \n";
curl_close($ch); // close the cURL session
// $Debug .= " raw data\n".$data."\n \n";
$stuff = explode("\r\n\r\n",$data); // maybe we have more than one header due to redirects.
$content = (string)array_pop($stuff); // last one is the content
$headers = (string)array_pop($stuff); // next-to-last-one is the headers
if ($cinfo['http_code'] <> '200') {
$Debug.= " headers returned:\n" . $headers . "\n \n";
}
print $Debug;
return $content; // return headers+contents
}
function ADV_fetch_microtime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}