Some fixes to trends parsing

This commit is contained in:
Nabeel Shahzad 2018-04-15 14:14:40 -05:00
parent e369695da5
commit 63c5d69e10
2 changed files with 39 additions and 9 deletions

View File

@ -37,6 +37,7 @@ class Metar implements \ArrayAccess
* Array of decoded result, by default all parameters is null.
*/
public $result = [
'category' => null,
'raw' => null,
'taf' => null,
'taf_flag' => null,
@ -45,7 +46,6 @@ class Metar implements \ArrayAccess
'observed_day' => null,
'observed_time' => null,
'observed_age' => null,
'category' => null,
'wind_speed' => null,
'wind_gust_speed' => null,
'wind_direction' => null,
@ -56,7 +56,6 @@ class Metar implements \ArrayAccess
'varies_wind_max' => null,
'varies_wind_max_label' => null,
'visibility' => null,
'visibility_nm' => null,
'visibility_report' => null,
'visibility_min' => null,
'visibility_min_direction' => null,
@ -117,6 +116,7 @@ class Metar implements \ArrayAccess
* Interpretation of weather conditions intensity codes.
*/
private static $weather_intensity_codes = [
'' => 'moderate',
'-' => 'light',
'+' => 'strong',
'VC' => 'in the vicinity',
@ -425,6 +425,7 @@ class Metar implements \ArrayAccess
$this->part++;
}
// Delete null values from the TAF report
if ($this->result['taf'] === true) {
foreach ($this->result as $parameter => $value) {
@ -433,14 +434,18 @@ class Metar implements \ArrayAccess
}
}
}
// Finally determine if it's VFR or IFR conditions
// https://www.aviationweather.gov/cva/help
if ($this->result['cavok']
|| ($this->result['cloud_height']['ft'] > 3000 && $this->result['visibility_nm'] > 5)) {
if(array_key_exists('cavok', $this->result) && $this->result['cavok']) {
$this->result['category'] = 'VFR';
} else {
$this->result['category'] = 'IFR';
if(array_key_exists('cloud_height', $this->result) && array_key_exists('visibility', $this->result)) {
if ($this->result['cloud_height']['ft'] > 3000 && $this->result['visibility']['nmi'] > 5) {
$this->result['category'] = 'VFR';
} else {
$this->result['category'] = 'IFR';
}
}
}
return $this->result;
@ -1325,7 +1330,7 @@ class Metar implements \ArrayAccess
// Get all parts after trend part
while ($this->part < \count($this->raw_parts)) {
if (preg_match($regexp, $this->raw_parts[$this->part], $found)) {
if (preg_match($r, $this->raw_parts[$this->part], $found)) {
// Get trend flag
if (isset($found[2]) && isset(static::$trends_flag_codes[$found[2]])) {
$trend['flag'] = $found[2];
@ -1366,7 +1371,7 @@ class Metar implements \ArrayAccess
$this->part++; // go to next part
// Detect ends of this trend, if the METAR raw data observed
if (!empty($raw_parts) && (!isset($this->raw_parts[$this->part]) || preg_match($regexp, $this->raw_parts[$this->part]))) {
if (!empty($raw_parts) && (!isset($this->raw_parts[$this->part]) || preg_match($r, $this->raw_parts[$this->part]))) {
$this->part--; // return pointer to finded part
break;
}

View File

@ -71,7 +71,7 @@ class MetarTest extends TestCase
}
public function testMetar2()
public function testMetarTrends()
{
$metar =
'KJFK 070151Z 20005KT 10SM BKN100 08/07 A2970 RMK AO2 SLP056 T00780067';
@ -85,4 +85,29 @@ class MetarTest extends TestCase
$parsed = Metar::parse($metar);
}
public function testMetarTrends2()
{
$metar = 'KAUS 092135Z 26018G25KT 8SM -TSRA BR SCT045CB BKN060 OVC080 30/21 A2992 RMK FQT LTGICCCCG OHD-W MOVG E RAB25 TSB32 CB ALQDS SLP132 P0035 T03020210 =';
$parsed = Metar::parse($metar);
$this->assertEquals('VFR', $parsed['category']);
$this->assertEquals(9.26, $parsed['wind_speed']);
$this->assertEquals(8, $parsed['visibility']['mi']);
$this->assertEquals(
'Scattered at 4500 feet, cumulonimbus; broken sky at 6000 feet; overcast sky at 8000 feet',
$parsed['clouds_report_ft']
);
$this->assertNotNull($parsed);
}
public function testMetarTrends3()
{
$metar = 'EHAM 041455Z 13012KT 9999 FEW034CB BKN040 05/01 Q1007 TEMPO 14017G28K 4000 SHRA =';
$metar = Metar::parse($metar);
$this->assertEquals('VFR', $metar['category']);
$this->assertNotNull($metar);
}
}