Merge pull request #682 from nabeelio/680-METAR-Parse-Error

METAR: KM as unit in visibility #680
This commit is contained in:
Nabeel S
2020-05-03 11:48:42 -04:00
committed by GitHub
2 changed files with 40 additions and 8 deletions

View File

@@ -843,6 +843,7 @@ class Metar implements \ArrayAccess
* if visibility is limited to an integer mile plus a fraction part. * if visibility is limited to an integer mile plus a fraction part.
* Format is mmSM for mm = statute miles, or m n/dSM for m = mile and n/d = fraction of a mile, * Format is mmSM for mm = statute miles, or m n/dSM for m = mile and n/d = fraction of a mile,
* or just a 4-digit number nnnn (with leading zeros) for nnnn = meters. * or just a 4-digit number nnnn (with leading zeros) for nnnn = meters.
* Unit can also be in KM
* *
* @param mixed $part * @param mixed $part
* *
@@ -858,7 +859,7 @@ class Metar implements \ArrayAccess
.'([\d]{0,2})?' // 3 .'([\d]{0,2})?' // 3
.'(([1357])' // 4 .'(([1357])' // 4
.'/(2|4|8|16))?' // 5 .'/(2|4|8|16))?' // 5
.'SM|////)$@'; // 6 .'(SM|KM|M|MI)|////)$@'; // 6
if (!preg_match($r, $part, $found)) { if (!preg_match($r, $part, $found)) {
return false; return false;
@@ -884,8 +885,7 @@ class Metar implements \ArrayAccess
// ICAO visibility (in meters) // ICAO visibility (in meters)
if (isset($found[2]) && !empty($found[2])) { if (isset($found[2]) && !empty($found[2])) {
$visibility = $this->createDistance((int) $found[2], 'm'); $visibility = $this->createDistance((int) $found[2], 'm');
} // US visibility (in miles) } else {
else {
if (isset($found[3]) && !empty($found[3])) { if (isset($found[3]) && !empty($found[3])) {
$prefix = 'Less than '; $prefix = 'Less than ';
} }
@@ -896,16 +896,34 @@ class Metar implements \ArrayAccess
$visibility = (int) $found[4]; $visibility = (int) $found[4];
} }
$visibility = $this->createDistance($visibility, 'mi'); $units = strtoupper($found[8]);
if ($units == 'MI' || $units == 'SM') {
$unit = 'mi';
} elseif ($units == 'M') {
$unit = 'm';
} elseif ($units == 'KM') {
$unit = 'km';
} else {
$unit = $units;
}
$visibility = $this->createDistance($visibility, $unit);
} }
$unit = ' meters'; if ($visibility['m'] > 1000) {
if ($visibility['m'] <= 1) { $unit = ' km';
$unit = ' meter'; $report = $prefix.$visibility['km'].$unit;
} else {
$unit = ' meters';
if ($visibility['m'] <= 1) {
$unit = ' meter';
}
$report = $prefix.$visibility['m'].$unit;
} }
$this->set_result_value('visibility', $visibility); $this->set_result_value('visibility', $visibility);
$this->set_result_value('visibility_report', $prefix.$visibility['m'].$unit); $this->set_result_value('visibility_report', $report);
} }
return true; return true;

View File

@@ -148,6 +148,20 @@ class MetarTest extends TestCase
$this->assertEquals('A few at 1500 feet; a few at 25000 feet', $metar['clouds_report_ft']); $this->assertEquals('A few at 1500 feet; a few at 25000 feet', $metar['clouds_report_ft']);
} }
/**
* Visibility in KM not parsed
*
* https://github.com/nabeelio/phpvms/issues/680
*/
public function testMetar5()
{
$metar = 'NZOH 031300Z 04004KT 38KM SCT075 BKN090 15/14 Q1002 RMK AUTO NZPM VATSIM USE ONL';
$metar = Metar::parse($metar);
$this->assertEquals(38, $metar['visibility']['km']);
$this->assertEquals('38 km', $metar['visibility_report']);
}
public function testHttpCallSuccess() public function testHttpCallSuccess()
{ {
$this->mockXmlResponse('aviationweather/kjfk.xml'); $this->mockXmlResponse('aviationweather/kjfk.xml');