Completed
Push — master ( 8892b3...024748 )
by Yannick
31:06
created
require/class.METAR.php 1 patch
Spacing   +51 added lines, -51 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 			$Connection = new Connection();
69 69
 			$sth = $Connection->db->prepare($query);
70 70
 			$sth->execute();
71
-		} catch(PDOException $e) {
71
+		} catch (PDOException $e) {
72 72
 			return "error : ".$e->getMessage();
73 73
 		}
74 74
 		$row = $sth->fetch(PDO::FETCH_ASSOC);
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
 			$Connection = new Connection();
88 88
 			$sth = $Connection->db->prepare($query);
89 89
 			$sth->execute();
90
-		} catch(PDOException $e) {
90
+		} catch (PDOException $e) {
91 91
 			return "error : ".$e->getMessage();
92 92
 		}
93 93
 	}
@@ -100,9 +100,9 @@  discard block
 block discarded – undo
100 100
 	public function parse($data) {
101 101
 		//$data = str_replace(array('\n','\r','\r','\n'),'',$data);
102 102
 		$codes = implode('|', array_keys($this->texts));
103
-		$regWeather = '#^(\+|\-|VC)?(' . $codes . ')(' . $codes . ')?$#';
103
+		$regWeather = '#^(\+|\-|VC)?('.$codes.')('.$codes.')?$#';
104 104
 		//$pieces = explode(' ',$data);
105
-		$pieces = preg_split('/\s/',$data);
105
+		$pieces = preg_split('/\s/', $data);
106 106
 		$pos = 0;
107 107
 		if ($pieces[0] == 'METAR') $pos++;
108 108
 		elseif ($pieces[0] == 'SPECI') $pos++;
@@ -112,25 +112,25 @@  discard block
 block discarded – undo
112 112
 		$result['location'] = $pieces[$pos];
113 113
 		$pos++;
114 114
 		if (!isset($pieces[$pos])) return $result;
115
-		$result['dayofmonth'] = substr($pieces[$pos],0,2);
116
-		$result['time'] = substr($pieces[$pos],2,4);
115
+		$result['dayofmonth'] = substr($pieces[$pos], 0, 2);
116
+		$result['time'] = substr($pieces[$pos], 2, 4);
117 117
 		$c = count($pieces);
118
-		for($pos++; $pos < $c; $pos++) {
118
+		for ($pos++; $pos < $c; $pos++) {
119 119
 			$piece = $pieces[$pos];
120 120
 			if ($piece == 'RMK') break;
121 121
 			if ($piece == 'AUTO') $result['auto'] = true;
122 122
 			if ($piece == 'COR') $result['correction'] = true;
123 123
 			// Wind Speed
124 124
 			if (preg_match('#(VRB|\d\d\d)(\d\d)(?:G(\d\d))?(KT|MPS|KPH)(?: (\d{1,3})V(\d{1,3}))?$#', $piece, $matches)) {
125
-				$result['wind']['direction'] = (float)$matches[1];
125
+				$result['wind']['direction'] = (float) $matches[1];
126 126
 				$result['wind']['unit'] = $matches[4];
127
-				if ($result['wind']['unit'] == 'KT') $result['wind']['speed'] = round(((float)$matches[2])*0.51444444444,2);
128
-				elseif ($result['wind']['unit'] == 'KPH') $result['wind']['speed'] = round(((float)$matches[2])*1000,2);
129
-				elseif ($result['wind']['unit'] == 'MPS') $result['wind']['speed'] = round(((float)$matches[2]),2);
130
-				$result['wind']['gust'] = (float)$matches[3];
127
+				if ($result['wind']['unit'] == 'KT') $result['wind']['speed'] = round(((float) $matches[2])*0.51444444444, 2);
128
+				elseif ($result['wind']['unit'] == 'KPH') $result['wind']['speed'] = round(((float) $matches[2])*1000, 2);
129
+				elseif ($result['wind']['unit'] == 'MPS') $result['wind']['speed'] = round(((float) $matches[2]), 2);
130
+				$result['wind']['gust'] = (float) $matches[3];
131 131
 				$result['wind']['unit'] = $matches[4];
132
-				$result['wind']['min_variation'] = array_key_exists(5,$matches) ? $matches[5] : 0;
133
-				$result['wind']['max_variation'] = array_key_exists(6,$matches) ? $matches[6] : 0;
132
+				$result['wind']['min_variation'] = array_key_exists(5, $matches) ? $matches[5] : 0;
133
+				$result['wind']['max_variation'] = array_key_exists(6, $matches) ? $matches[6] : 0;
134 134
 			}
135 135
 
136 136
 /*    			if (preg_match('#^([0-9]{3})([0-9]{2})(G([0-9]{2}))?(KT|MPS)$#', $piece, $matches)) {
@@ -151,17 +151,17 @@  discard block
 block discarded – undo
151 151
 
152 152
 			// Temperature
153 153
 			if (preg_match('#^(M?[0-9]{2,})/(M?[0-9]{2,})$#', $piece, $matches)) {
154
-				$temp = (float)$matches[1];
154
+				$temp = (float) $matches[1];
155 155
 				if ($matches[1]{0} == 'M') {
156
-					$temp = ((float)substr($matches[1], 1)) * -1;
156
+					$temp = ((float) substr($matches[1], 1))*-1;
157 157
 				}
158 158
 				$result['temperature'] = $temp;
159
-				$dew = (float)$matches[2];
159
+				$dew = (float) $matches[2];
160 160
 				if ($matches[2]{0} == 'M') {
161
-					$dew = ((float)substr($matches[2], 1)) * -1;
161
+					$dew = ((float) substr($matches[2], 1))*-1;
162 162
 				}
163 163
 				$result['dew'] = $dew;
164
-				$result['rh'] = 100*(exp((17.625*$dew)/(243.04+$dew))/exp((17.625*$temp)/(243.04+$temp)));
164
+				$result['rh'] = 100*(exp((17.625*$dew)/(243.04 + $dew))/exp((17.625*$temp)/(243.04 + $temp)));
165 165
 			}
166 166
 			// QNH
167 167
 			if (preg_match('#^(A|Q)([0-9]{4})$#', $piece, $matches)) {
@@ -171,7 +171,7 @@  discard block
 block discarded – undo
171 171
 					$result['QNH'] = $matches[2];
172 172
 				} else {
173 173
 					// inHg
174
-					$result['QNH'] = round(($matches[2] / 100)*33.86389,2);
174
+					$result['QNH'] = round(($matches[2]/100)*33.86389, 2);
175 175
 				}
176 176
 				/*
177 177
     				$result['QNH'] = $matches[1] == 'Q' ? $matches[2] : ($matches[2] / 100);
@@ -192,12 +192,12 @@  discard block
 block discarded – undo
192 192
     			// Visibility
193 193
 			if (preg_match('#^([0-9]{4})|(([0-9]{1,4})SM)$#', $piece, $matches)) {
194 194
 				if (isset($matches[3]) && strlen($matches[3]) > 0) {
195
-					$result['visibility'] = (float)$matches[3] * 1609.34;
195
+					$result['visibility'] = (float) $matches[3]*1609.34;
196 196
 				} else {
197 197
 					if ($matches[1] == '9999') {
198 198
 						$result['visibility'] = '> 10000';
199 199
 					} else {
200
-						$result['visibility'] = (float)$matches[1];
200
+						$result['visibility'] = (float) $matches[1];
201 201
 					}
202 202
 				}
203 203
 				if (preg_match('#^CAVOK$#', $piece, $matches)) {
@@ -219,7 +219,7 @@  discard block
 block discarded – undo
219 219
 				elseif ($type == 'OVC') $cloud['type'] = 'Overcast/Full cloud coverage';
220 220
 				elseif ($type == 'VV') $cloud['type'] = 'Vertical visibility';
221 221
 				$cloud['type_code'] = $type;
222
-				$cloud['level'] = round(((float)$matches[2]) * 100 * 0.3048);
222
+				$cloud['level'] = round(((float) $matches[2])*100*0.3048);
223 223
 				if (isset($matches[3])) $significant = $matches[3];
224 224
 				else $significant = '';
225 225
 				if ($significant == 'CB') $cloud['significant'] = 'Cumulonimbus';
@@ -240,8 +240,8 @@  discard block
 block discarded – undo
240 240
 				$rvr['runway'] = $matches[1];
241 241
 				$rvr['assessment'] = $matches[2];
242 242
 				$rvr['rvr'] = $matches[3];
243
-				$rvr['rvr_max'] = array_key_exists(4,$matches) ? $matches[4] : 0;
244
-				$rvr['unit'] = array_key_exists(5,$matches) ? $matches[5] : '';
243
+				$rvr['rvr_max'] = array_key_exists(4, $matches) ? $matches[4] : 0;
244
+				$rvr['unit'] = array_key_exists(5, $matches) ? $matches[5] : '';
245 245
 				$result['RVR'] = $rvr;
246 246
 			}
247 247
 			//if (preg_match('#^(R[A-Z0-9]{2,3})/([0-9]{4})(V([0-9]{4}))?(FT)?$#', $piece, $matches)) {
@@ -254,12 +254,12 @@  discard block
 block discarded – undo
254 254
 				$result['RVR']['friction'] = $matches[5];
255 255
 			}
256 256
 			if (preg_match('#^(R[A-Z0-9]{2,3})/([0-9]{4})(V([0-9]{4}))?(FT)?$#', $piece, $matches)) {
257
-				if (isset($matches[5])) $range = array('exact' => (float)$matches[2], 'unit' => $matches[5] ? 'FT' : 'M');
258
-				else $range = array('exact' => (float)$matches[2], 'unit' => 'M');
257
+				if (isset($matches[5])) $range = array('exact' => (float) $matches[2], 'unit' => $matches[5] ? 'FT' : 'M');
258
+				else $range = array('exact' => (float) $matches[2], 'unit' => 'M');
259 259
 				if (isset($matches[3])) {
260 260
 					$range = Array(
261
-					    'from' => (float)$matches[2],
262
-					    'to'   => (float)$matches[4],
261
+					    'from' => (float) $matches[2],
262
+					    'to'   => (float) $matches[4],
263 263
 					    'unit' => $matches[5] ? 'FT' : 'M'
264 264
 					);
265 265
 				}
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
 		try {
313 313
 			$sth = $this->db->prepare($query);
314 314
 			$sth->execute($query_values);
315
-		} catch(PDOException $e) {
315
+		} catch (PDOException $e) {
316 316
 			return "error : ".$e->getMessage();
317 317
 		}
318 318
 		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
@@ -328,19 +328,19 @@  discard block
 block discarded – undo
328 328
 	 * @param String $metar METAR text
329 329
 	 * @param String $date date of the METAR
330 330
 	*/
331
-	public function addMETAR($location,$metar,$date) {
331
+	public function addMETAR($location, $metar, $date) {
332 332
 		global $globalDBdriver;
333
-		$date = date('Y-m-d H:i:s',strtotime($date));
333
+		$date = date('Y-m-d H:i:s', strtotime($date));
334 334
 		if ($globalDBdriver == 'mysql') {
335 335
 			$query = "INSERT INTO metar (metar_location,metar_date,metar) VALUES (:location,:date,:metar) ON DUPLICATE KEY UPDATE metar_date = :date, metar = :metar";
336 336
 		} else {
337 337
 			$query = "UPDATE metar SET metar_date = :date, metar = metar WHERE metar_location = :location;INSERT INTO metar (metar_location,metar_date,metar) SELECT :location,:date,:metar WHERE NOT EXISTS (SELECT 1 FROM metar WHERE metar_location = :location);";
338 338
 		}
339
-		$query_values = array(':location' => $location,':date' => $date,':metar' => utf8_encode($metar));
339
+		$query_values = array(':location' => $location, ':date' => $date, ':metar' => utf8_encode($metar));
340 340
 		try {
341 341
 			$sth = $this->db->prepare($query);
342 342
 			$sth->execute($query_values);
343
-		} catch(PDOException $e) {
343
+		} catch (PDOException $e) {
344 344
 			return "error : ".$e->getMessage();
345 345
 		}
346 346
 	}
@@ -355,7 +355,7 @@  discard block
 block discarded – undo
355 355
 		try {
356 356
 			$sth = $this->db->prepare($query);
357 357
 			$sth->execute($query_values);
358
-		} catch(PDOException $e) {
358
+		} catch (PDOException $e) {
359 359
 			return "error : ".$e->getMessage();
360 360
 		}
361 361
 	}
@@ -368,7 +368,7 @@  discard block
 block discarded – undo
368 368
 		try {
369 369
 			$sth = $this->db->prepare($query);
370 370
 			$sth->execute();
371
-		} catch(PDOException $e) {
371
+		} catch (PDOException $e) {
372 372
 			return "error : ".$e->getMessage();
373 373
 		}
374 374
 	}
@@ -382,29 +382,29 @@  discard block
 block discarded – undo
382 382
 		date_default_timezone_set("UTC");
383 383
 		$Common = new Common();
384 384
 		if (isset($globalIVAO) && $globalIVAO) {
385
-			$Common->download('http://wx.ivao.aero/metar.php',dirname(__FILE__).'/../install/tmp/ivaometar.txt');
386
-			$handle = fopen(dirname(__FILE__).'/../install/tmp/ivaometar.txt',"r");
385
+			$Common->download('http://wx.ivao.aero/metar.php', dirname(__FILE__).'/../install/tmp/ivaometar.txt');
386
+			$handle = fopen(dirname(__FILE__).'/../install/tmp/ivaometar.txt', "r");
387 387
 		} else {
388
-			$Common->download('http://tgftp.nws.noaa.gov/data/observations/metar/cycles/'.date('H').'Z.TXT',dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT');
389
-			$handle = fopen(dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT',"r");
388
+			$Common->download('http://tgftp.nws.noaa.gov/data/observations/metar/cycles/'.date('H').'Z.TXT', dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT');
389
+			$handle = fopen(dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT', "r");
390 390
 		}
391 391
 		if ($handle) {
392 392
 			if (isset($globalDebug) && $globalDebug) echo "Done - Updating DB...";
393 393
 			$date = '';
394 394
 			if ($globalTransaction) $this->db->beginTransaction();
395
-			while(($line = fgets($handle,4096)) !== false) {
396
-				$line = str_replace(array("\r\n","\r", "\n"), '', $line);
397
-				if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#',$line)) {
395
+			while (($line = fgets($handle, 4096)) !== false) {
396
+				$line = str_replace(array("\r\n", "\r", "\n"), '', $line);
397
+				if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#', $line)) {
398 398
 					$date = $line;
399 399
 				} elseif (trim($line) != '') {
400 400
 					if ($date == '') $date = date('Y/m/d H:m');
401 401
 					$pos = 0;
402
-					$pieces = preg_split('/\s/',$line);
402
+					$pieces = preg_split('/\s/', $line);
403 403
 					if ($pieces[0] == 'METAR') $pos++;
404 404
 					if (strlen($pieces[$pos]) != 4) $pos++;
405 405
 					$location = $pieces[$pos];
406 406
 					//echo 'location: '.$location.' - date: '.$date.' - data: '.$line."\n";
407
-					echo $this->addMETAR($location,$line,$date);
407
+					echo $this->addMETAR($location, $line, $date);
408 408
 				}
409 409
 			}
410 410
 			fclose($handle);
@@ -423,23 +423,23 @@  discard block
 block discarded – undo
423 423
 		if ($globalMETARurl == '') return array();
424 424
 		date_default_timezone_set("UTC");
425 425
 		$Common = new Common();
426
-		$url = str_replace('{icao}',$icao,$globalMETARurl);
426
+		$url = str_replace('{icao}', $icao, $globalMETARurl);
427 427
 		$cycle = $Common->getData($url);
428 428
 		$date = '';
429
-		foreach(explode("\n",$cycle) as $line) {
430
-			$line = str_replace(array("\r\n","\r", "\n"), '', $line);
431
-			if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#',$line)) {
429
+		foreach (explode("\n", $cycle) as $line) {
430
+			$line = str_replace(array("\r\n", "\r", "\n"), '', $line);
431
+			if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#', $line)) {
432 432
 				$date = $line;
433 433
 			} 
434 434
 			if (trim($line) != '') {
435 435
 				if ($date == '') $date = date('Y/m/d H:m');
436 436
 				$pos = 0;
437
-				$pieces = preg_split('/\s/',$line);
437
+				$pieces = preg_split('/\s/', $line);
438 438
 				if ($pieces[0] == 'METAR') $pos++;
439 439
 				if (strlen($pieces[$pos]) != 4) $pos++;
440 440
 				$location = $pieces[$pos];
441 441
 				if (strlen($location == 4)) {
442
-					$this->addMETAR($location,$line,$date);
442
+					$this->addMETAR($location, $line, $date);
443 443
 					return array('0' => array('metar_date' => $date, 'metar_location' => $location, 'metar' => $line));
444 444
 				} else return array();
445 445
 			}
Please login to merge, or discard this patch.