Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

METAR: KM as unit in visibility #680 #682

Merged
merged 1 commit into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions app/Support/Metar.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ private function get_varies_wind($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,
* or just a 4-digit number nnnn (with leading zeros) for nnnn = meters.
* Unit can also be in KM
*
* @param mixed $part
*
Expand All @@ -858,7 +859,7 @@ private function get_visibility($part)
.'([\d]{0,2})?' // 3
.'(([1357])' // 4
.'/(2|4|8|16))?' // 5
.'SM|////)$@'; // 6
.'(SM|KM|M|MI)|////)$@'; // 6

if (!preg_match($r, $part, $found)) {
return false;
Expand All @@ -884,8 +885,7 @@ private function get_visibility($part)
// ICAO visibility (in meters)
if (isset($found[2]) && !empty($found[2])) {
$visibility = $this->createDistance((int) $found[2], 'm');
} // US visibility (in miles)
else {
} else {
if (isset($found[3]) && !empty($found[3])) {
$prefix = 'Less than ';
}
Expand All @@ -896,16 +896,34 @@ private function get_visibility($part)
$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'] <= 1) {
$unit = ' meter';
if ($visibility['m'] > 1000) {
$unit = ' km';
$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_report', $prefix.$visibility['m'].$unit);
$this->set_result_value('visibility_report', $report);
}

return true;
Expand Down
14 changes: 14 additions & 0 deletions tests/MetarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ public function testMetar4Clouds()
$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()
{
$this->mockXmlResponse('aviationweather/kjfk.xml');
Expand Down