Completed
Push — master ( 01a275...d13cef )
by Yannick
42:12
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
 		return '';
@@ -101,9 +101,9 @@  discard block
 block discarded – undo
101 101
 	public function parse($data) {
102 102
 		//$data = str_replace(array('\n','\r','\r','\n'),'',$data);
103 103
 		$codes = implode('|', array_keys($this->texts));
104
-		$regWeather = '#^(\+|\-|VC)?(' . $codes . ')(' . $codes . ')?$#';
104
+		$regWeather = '#^(\+|\-|VC)?('.$codes.')('.$codes.')?$#';
105 105
 		//$pieces = explode(' ',$data);
106
-		$pieces = preg_split('/\s/',$data);
106
+		$pieces = preg_split('/\s/', $data);
107 107
 		$pos = 0;
108 108
 		if ($pieces[0] == 'METAR') $pos++;
109 109
 		elseif ($pieces[0] == 'SPECI') $pos++;
@@ -113,25 +113,25 @@  discard block
 block discarded – undo
113 113
 		$result['location'] = $pieces[$pos];
114 114
 		$pos++;
115 115
 		if (!isset($pieces[$pos])) return $result;
116
-		$result['dayofmonth'] = substr($pieces[$pos],0,2);
117
-		$result['time'] = substr($pieces[$pos],2,4);
116
+		$result['dayofmonth'] = substr($pieces[$pos], 0, 2);
117
+		$result['time'] = substr($pieces[$pos], 2, 4);
118 118
 		$c = count($pieces);
119
-		for($pos++; $pos < $c; $pos++) {
119
+		for ($pos++; $pos < $c; $pos++) {
120 120
 			$piece = $pieces[$pos];
121 121
 			if ($piece == 'RMK') break;
122 122
 			if ($piece == 'AUTO') $result['auto'] = true;
123 123
 			if ($piece == 'COR') $result['correction'] = true;
124 124
 			// Wind Speed
125 125
 			if (preg_match('#(VRB|\d\d\d)(\d\d)(?:G(\d\d))?(KT|MPS|KPH)(?: (\d{1,3})V(\d{1,3}))?$#', $piece, $matches)) {
126
-				$result['wind']['direction'] = (float)$matches[1];
126
+				$result['wind']['direction'] = (float) $matches[1];
127 127
 				$result['wind']['unit'] = $matches[4];
128
-				if ($result['wind']['unit'] == 'KT') $result['wind']['speed'] = round(((float)$matches[2])*0.51444444444,2);
129
-				elseif ($result['wind']['unit'] == 'KPH') $result['wind']['speed'] = round(((float)$matches[2])*1000,2);
130
-				elseif ($result['wind']['unit'] == 'MPS') $result['wind']['speed'] = round(((float)$matches[2]),2);
131
-				$result['wind']['gust'] = (float)$matches[3];
128
+				if ($result['wind']['unit'] == 'KT') $result['wind']['speed'] = round(((float) $matches[2])*0.51444444444, 2);
129
+				elseif ($result['wind']['unit'] == 'KPH') $result['wind']['speed'] = round(((float) $matches[2])*1000, 2);
130
+				elseif ($result['wind']['unit'] == 'MPS') $result['wind']['speed'] = round(((float) $matches[2]), 2);
131
+				$result['wind']['gust'] = (float) $matches[3];
132 132
 				$result['wind']['unit'] = $matches[4];
133
-				$result['wind']['min_variation'] = array_key_exists(5,$matches) ? $matches[5] : 0;
134
-				$result['wind']['max_variation'] = array_key_exists(6,$matches) ? $matches[6] : 0;
133
+				$result['wind']['min_variation'] = array_key_exists(5, $matches) ? $matches[5] : 0;
134
+				$result['wind']['max_variation'] = array_key_exists(6, $matches) ? $matches[6] : 0;
135 135
 			}
136 136
 
137 137
 /*    			if (preg_match('#^([0-9]{3})([0-9]{2})(G([0-9]{2}))?(KT|MPS)$#', $piece, $matches)) {
@@ -152,17 +152,17 @@  discard block
 block discarded – undo
152 152
 
153 153
 			// Temperature
154 154
 			if (preg_match('#^(M?[0-9]{2,})/(M?[0-9]{2,})$#', $piece, $matches)) {
155
-				$temp = (float)$matches[1];
155
+				$temp = (float) $matches[1];
156 156
 				if ($matches[1]{0} == 'M') {
157
-					$temp = ((float)substr($matches[1], 1)) * -1;
157
+					$temp = ((float) substr($matches[1], 1))*-1;
158 158
 				}
159 159
 				$result['temperature'] = $temp;
160
-				$dew = (float)$matches[2];
160
+				$dew = (float) $matches[2];
161 161
 				if ($matches[2]{0} == 'M') {
162
-					$dew = ((float)substr($matches[2], 1)) * -1;
162
+					$dew = ((float) substr($matches[2], 1))*-1;
163 163
 				}
164 164
 				$result['dew'] = $dew;
165
-				$result['rh'] = round(100*(exp((17.625*$dew)/(243.04+$dew))/exp((17.625*$temp)/(243.04+$temp))));
165
+				$result['rh'] = round(100*(exp((17.625*$dew)/(243.04 + $dew))/exp((17.625*$temp)/(243.04 + $temp))));
166 166
 			}
167 167
 			// QNH
168 168
 			if (preg_match('#^(A|Q)([0-9]{4})$#', $piece, $matches)) {
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
 					$result['QNH'] = $matches[2];
173 173
 				} else {
174 174
 					// inHg
175
-					$result['QNH'] = round(($matches[2] / 100)*33.86389,2);
175
+					$result['QNH'] = round(($matches[2]/100)*33.86389, 2);
176 176
 				}
177 177
 				/*
178 178
     				$result['QNH'] = $matches[1] == 'Q' ? $matches[2] : ($matches[2] / 100);
@@ -193,12 +193,12 @@  discard block
 block discarded – undo
193 193
     			// Visibility
194 194
 			if (preg_match('#^([0-9]{4})|(([0-9]{1,4})SM)$#', $piece, $matches)) {
195 195
 				if (isset($matches[3]) && strlen($matches[3]) > 0) {
196
-					$result['visibility'] = (float)$matches[3] * 1609.34;
196
+					$result['visibility'] = (float) $matches[3]*1609.34;
197 197
 				} else {
198 198
 					if ($matches[1] == '9999') {
199 199
 						$result['visibility'] = '> 10000';
200 200
 					} else {
201
-						$result['visibility'] = (float)$matches[1];
201
+						$result['visibility'] = (float) $matches[1];
202 202
 					}
203 203
 				}
204 204
 			}
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
 				elseif ($type == 'OVC') $cloud['type'] = 'Overcast/Full cloud coverage';
222 222
 				elseif ($type == 'VV') $cloud['type'] = 'Vertical visibility';
223 223
 				$cloud['type_code'] = $type;
224
-				$cloud['level'] = round(((float)$matches[2]) * 100 * 0.3048);
224
+				$cloud['level'] = round(((float) $matches[2])*100*0.3048);
225 225
 				if (isset($matches[3])) $significant = $matches[3];
226 226
 				else $significant = '';
227 227
 				if ($significant == 'CB') $cloud['significant'] = 'Cumulonimbus';
@@ -242,8 +242,8 @@  discard block
 block discarded – undo
242 242
 				$rvr['runway'] = $matches[1];
243 243
 				$rvr['assessment'] = $matches[2];
244 244
 				$rvr['rvr'] = $matches[3];
245
-				$rvr['rvr_max'] = array_key_exists(4,$matches) ? $matches[4] : 0;
246
-				$rvr['unit'] = array_key_exists(5,$matches) ? $matches[5] : '';
245
+				$rvr['rvr_max'] = array_key_exists(4, $matches) ? $matches[4] : 0;
246
+				$rvr['unit'] = array_key_exists(5, $matches) ? $matches[5] : '';
247 247
 				$result['RVR'] = $rvr;
248 248
 			}
249 249
 			//if (preg_match('#^(R[A-Z0-9]{2,3})/([0-9]{4})(V([0-9]{4}))?(FT)?$#', $piece, $matches)) {
@@ -256,12 +256,12 @@  discard block
 block discarded – undo
256 256
 				$result['RVR']['friction'] = $matches[5];
257 257
 			}
258 258
 			if (preg_match('#^(R[A-Z0-9]{2,3})/([0-9]{4})(V([0-9]{4}))?(FT)?$#', $piece, $matches)) {
259
-				if (isset($matches[5])) $range = array('exact' => (float)$matches[2], 'unit' => $matches[5] ? 'FT' : 'M');
260
-				else $range = array('exact' => (float)$matches[2], 'unit' => 'M');
259
+				if (isset($matches[5])) $range = array('exact' => (float) $matches[2], 'unit' => $matches[5] ? 'FT' : 'M');
260
+				else $range = array('exact' => (float) $matches[2], 'unit' => 'M');
261 261
 				if (isset($matches[3])) {
262 262
 					$range = Array(
263
-					    'from' => (float)$matches[2],
264
-					    'to'   => (float)$matches[4],
263
+					    'from' => (float) $matches[2],
264
+					    'to'   => (float) $matches[4],
265 265
 					    'unit' => $matches[5] ? 'FT' : 'M'
266 266
 					);
267 267
 				}
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
 		try {
315 315
 			$sth = $this->db->prepare($query);
316 316
 			$sth->execute($query_values);
317
-		} catch(PDOException $e) {
317
+		} catch (PDOException $e) {
318 318
 			return "error : ".$e->getMessage();
319 319
 		}
320 320
 		$all = $sth->fetchAll(PDO::FETCH_ASSOC);
@@ -330,19 +330,19 @@  discard block
 block discarded – undo
330 330
 	 * @param String $metar METAR text
331 331
 	 * @param String $date date of the METAR
332 332
 	*/
333
-	public function addMETAR($location,$metar,$date) {
333
+	public function addMETAR($location, $metar, $date) {
334 334
 		global $globalDBdriver;
335
-		$date = date('Y-m-d H:i:s',strtotime($date));
335
+		$date = date('Y-m-d H:i:s', strtotime($date));
336 336
 		if ($globalDBdriver == 'mysql') {
337 337
 			$query = "INSERT INTO metar (metar_location,metar_date,metar) VALUES (:location,:date,:metar) ON DUPLICATE KEY UPDATE metar_date = :date, metar = :metar";
338 338
 		} else {
339 339
 			$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);";
340 340
 		}
341
-		$query_values = array(':location' => $location,':date' => $date,':metar' => utf8_encode($metar));
341
+		$query_values = array(':location' => $location, ':date' => $date, ':metar' => utf8_encode($metar));
342 342
 		try {
343 343
 			$sth = $this->db->prepare($query);
344 344
 			$sth->execute($query_values);
345
-		} catch(PDOException $e) {
345
+		} catch (PDOException $e) {
346 346
 			return "error : ".$e->getMessage();
347 347
 		}
348 348
 		return '';
@@ -358,7 +358,7 @@  discard block
 block discarded – undo
358 358
 		try {
359 359
 			$sth = $this->db->prepare($query);
360 360
 			$sth->execute($query_values);
361
-		} catch(PDOException $e) {
361
+		} catch (PDOException $e) {
362 362
 			return "error : ".$e->getMessage();
363 363
 		}
364 364
 		return '';
@@ -372,7 +372,7 @@  discard block
 block discarded – undo
372 372
 		try {
373 373
 			$sth = $this->db->prepare($query);
374 374
 			$sth->execute();
375
-		} catch(PDOException $e) {
375
+		} catch (PDOException $e) {
376 376
 			return "error : ".$e->getMessage();
377 377
 		}
378 378
 		return '';
@@ -387,30 +387,30 @@  discard block
 block discarded – undo
387 387
 		date_default_timezone_set("UTC");
388 388
 		$Common = new Common();
389 389
 		if (isset($globalIVAO) && $globalIVAO) {
390
-			$Common->download('http://wx.ivao.aero/metar.php',dirname(__FILE__).'/../install/tmp/ivaometar.txt');
391
-			$handle = fopen(dirname(__FILE__).'/../install/tmp/ivaometar.txt',"r");
390
+			$Common->download('http://wx.ivao.aero/metar.php', dirname(__FILE__).'/../install/tmp/ivaometar.txt');
391
+			$handle = fopen(dirname(__FILE__).'/../install/tmp/ivaometar.txt', "r");
392 392
 		} else {
393
-			$Common->download('http://tgftp.nws.noaa.gov/data/observations/metar/cycles/'.date('H').'Z.TXT',dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT');
394
-			$handle = fopen(dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT',"r");
393
+			$Common->download('http://tgftp.nws.noaa.gov/data/observations/metar/cycles/'.date('H').'Z.TXT', dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT');
394
+			$handle = fopen(dirname(__FILE__).'/../install/tmp/'.date('H').'Z.TXT', "r");
395 395
 		}
396 396
 		if ($handle) {
397 397
 			if (isset($globalDebug) && $globalDebug) echo "Done - Updating DB...";
398 398
 			$date = '';
399 399
 			if ($globalTransaction) $this->db->beginTransaction();
400
-			while(($line = fgets($handle,4096)) !== false) {
401
-				$line = str_replace(array("\r\n","\r", "\n"), '', $line);
402
-				if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#',$line)) {
400
+			while (($line = fgets($handle, 4096)) !== false) {
401
+				$line = str_replace(array("\r\n", "\r", "\n"), '', $line);
402
+				if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#', $line)) {
403 403
 					$date = $line;
404 404
 				} elseif (trim($line) != '') {
405 405
 					if ($date == '') $date = date('Y/m/d H:m');
406 406
 					$pos = 0;
407
-					$pieces = preg_split('/\s/',$line);
407
+					$pieces = preg_split('/\s/', $line);
408 408
 					if ($pieces[0] == 'METAR') $pos++;
409 409
 					if (strlen($pieces[$pos]) != 4) $pos++;
410 410
 					if (isset($pieces[$pos])) {
411 411
 						$location = $pieces[$pos];
412 412
 						//if ($location == 'LFLL') echo 'location: '.$location.' - date: '.$date.' - data: '.$line."\n";
413
-						echo $this->addMETAR($location,$line,$date);
413
+						echo $this->addMETAR($location, $line, $date);
414 414
 					}
415 415
 				}
416 416
 			}
@@ -430,23 +430,23 @@  discard block
 block discarded – undo
430 430
 		if ($globalMETARurl == '') return array();
431 431
 		date_default_timezone_set("UTC");
432 432
 		$Common = new Common();
433
-		$url = str_replace('{icao}',$icao,$globalMETARurl);
433
+		$url = str_replace('{icao}', $icao, $globalMETARurl);
434 434
 		$cycle = $Common->getData($url);
435 435
 		$date = '';
436
-		foreach(explode("\n",$cycle) as $line) {
437
-			$line = str_replace(array("\r\n","\r", "\n"), '', $line);
438
-			if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#',$line)) {
436
+		foreach (explode("\n", $cycle) as $line) {
437
+			$line = str_replace(array("\r\n", "\r", "\n"), '', $line);
438
+			if (preg_match('#^([0-9]{4})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})$#', $line)) {
439 439
 				$date = $line;
440 440
 			} 
441 441
 			if (trim($line) != '') {
442 442
 				if ($date == '') $date = date('Y/m/d H:m');
443 443
 				$pos = 0;
444
-				$pieces = preg_split('/\s/',$line);
444
+				$pieces = preg_split('/\s/', $line);
445 445
 				if ($pieces[0] == 'METAR') $pos++;
446 446
 				if (strlen($pieces[$pos]) != 4) $pos++;
447 447
 				$location = $pieces[$pos];
448 448
 				if (strlen($location == 4)) {
449
-					$this->addMETAR($location,$line,$date);
449
+					$this->addMETAR($location, $line, $date);
450 450
 					return array('0' => array('metar_date' => $date, 'metar_location' => $location, 'metar' => $line));
451 451
 				} else return array();
452 452
 			}
Please login to merge, or discard this patch.