Skip to content

Commit c59b0ef

Browse files
Filter out solar flare predictions with invalid coordinates (#19)
* Filter out flare predictions with invalid coordinates * Filter out flares with invalid coordinates
1 parent e77472a commit c59b0ef

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/Translator/FlarePrediction.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use HelioviewerEventInterface\Types\HelioviewerEvent;
99
use HelioviewerEventInterface\Util\Date;
1010
use HelioviewerEventInterface\Util\HapiRecord;
11+
use HelioviewerEventInterface\Util\LocationParser;
1112

1213
const FLARE_CLASSES = ["C", "CPlus", "M", "MPlus", "X"];
1314

@@ -68,6 +69,13 @@ public static function Transform(array $events, DateTimeInterface $obstime): arr
6869
foreach ($events['groups'] as &$group) {
6970
// $group:
7071
// array('name', 'contact', 'url', 'data')
72+
73+
// We have identified that some predictions in CCMC have invalid
74+
// coordinates. In this case, we are going to drop these values
75+
// from the list.
76+
$group['data'] = array_values(array_filter($group['data'], function ($event) {
77+
return LocationParser::IsValidLatitudeLongitude(GetLatitude($event['source']), GetLongitude($event['source']));
78+
}));
7179
foreach ($group['data'] as &$event) {
7280
// event:
7381
// array of HelioviewerEvent fields

src/Util/LocationParser.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ public static function ParseText(string $location): array {
2020
$longitude = $east_west == "E" ? -$east_west_value : $east_west_value;
2121
return [$latitude, $longitude];
2222
}
23-
}
23+
24+
/**
25+
* Verifies that the latitude and longitude are valid coordinates
26+
*/
27+
public static function IsValidLatitudeLongitude(float $lat, float $lon): bool {
28+
return (-90 <= $lat && $lat <= 90) && (-180 <= $lon && $lon <= 180);
29+
}
30+
}

tests/translators/FlarePredictionTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php declare(strict_types=1);
22

33
use HelioviewerEventInterface\Events;
4+
use HelioviewerEventInterface\Translator\FlarePrediction;
5+
use HelioviewerEventInterface\Util\Date;
46
use PHPUnit\Framework\TestCase;
57
use function HelioviewerEventInterface\Translator\CreateShortLabel;
68
use HelioviewerEventInterface\Util\HapiRecord;
7-
9+
use PHPUnit\TextUI\XmlConfiguration\Group;
810

911
final class FlarePredictionTest extends TestCase
1012
{
@@ -171,5 +173,45 @@ public function testShortLabel(): void {
171173

172174
$this->assertEquals("\nC+: 5%\nM+: 1%\nX: 1%", CreateShortLabel($hapi_record));
173175
}
176+
177+
static private function MakeEvent(float $lat, float $lon): array {
178+
return [
179+
'source' => [
180+
'NOAALatitude' => $lat,
181+
'NOAALongitude' => $lon,
182+
'NOAALocationTime' => Date::FormatDate(new DateTimeImmutable()),
183+
'issue_time' => new DateTimeImmutable(),
184+
]
185+
];
186+
}
187+
188+
/**
189+
* We've found that some CCMC Flare Scoreboard predictions have an invalid
190+
* coordinate. The FlarePrediction translator filters these out.
191+
* The FlarePrediction module now filters any records where the latitude
192+
* and longitude are out of bounds.
193+
*/
194+
#[Group('coordinator')]
195+
public function testFlarePredictionInvalidCoordinates(): void {
196+
$data = FlarePrediction::Transform([
197+
'groups' => [[
198+
'data' => [
199+
// Invalid coordinate
200+
self::MakeEvent(106, 130),
201+
// Edge cases out of bounds
202+
self::MakeEvent(90.01, 0),
203+
self::MakeEvent(-90.01, 0),
204+
self::MakeEvent(0, 180.01),
205+
self::MakeEvent(0, -180.01),
206+
// Edge cases in-bound
207+
self::MakeEvent(90, 180),
208+
self::MakeEvent(-90, -180),
209+
]
210+
]]
211+
], new DateTimeImmutable());
212+
$this->assertCount(2, $data['groups'][0]['data']);
213+
$this->assertArrayHasKey(0, $data['groups'][0]['data']);
214+
$this->assertArrayHasKey(1, $data['groups'][0]['data']);
215+
}
174216
}
175217

0 commit comments

Comments
 (0)