Skip to content

Commit

Permalink
Working version of (possibly) aligned precip periods
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-gade committed Apr 26, 2024
1 parent c04379b commit 3a7b681
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 35 deletions.
53 changes: 43 additions & 10 deletions web/modules/weather_data/src/Service/DailyForecastTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,6 @@ public function getDailyForecastFromGrid(
$now = new \DateTimeImmutable("now", new \DateTimeZone($timezone));
}

// Fetch the 6-hour precipitation amount
// data for each day
$precipPeriods = $this->getHourlyPrecipitation(
$wfo,
$x,
$y,
$now
);
$precipPeriods = array_chunk($precipPeriods, 4);

// Fetch the actual daily forecast periods
$periods = DateTimeUtility::filterToAfter(
$forecast->periods,
Expand Down Expand Up @@ -207,6 +197,49 @@ public function getDailyForecastFromGrid(
$alerts,
);

// Get a mapping of the starTime for each day,
// starting with the today period. These will
// be used for grouping precipitation totals
// for each day.
$periodStartTimes = [
DateTimeUtility::stringToDate(
$todayPeriods[0]->startTime,
$timezone
)];
$detailedPeriodStartTimes = array_map(
function($periodPair) use (&$timezone){
return DateTimeUtility::stringToDate(
$periodPair[0]->startTime,
$timezone
);
},
array_chunk($detailedPeriods, 2)
);
$periodStartTimes = array_merge(
$periodStartTimes,
$detailedPeriodStartTimes
);

// Get raw precipitation periods data, then map and
// chunk into groups of periods based on each day's
// startTime
$precipPeriods = $this->getHourlyPrecipitation(
$wfo,
$x,
$y,
$now
);
$precipPeriods = array_map(
function($startTime) use(&$precipPeriods, &$timezone){
return $this->filterHourlyPrecipitationToDay(
$startTime,
$precipPeriods,
$timezone
);
},
$periodStartTimes
);

return [
"today" => array_values($todayPeriodsFormatted),
"todayHourly" => array_values($todayHourlyDetails),
Expand Down
72 changes: 47 additions & 25 deletions web/modules/weather_data/src/Service/HourlyForecastTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function getHourlyForecastFromGrid(
$timezone = $place->timezone;

$forecast = $this->dataLayer->getGridpoint($wfo, $gridX, $gridY)
->properties;
->properties;

$extraForecast = $this->dataLayer->getHourlyForecast(
$wfo,
Expand Down Expand Up @@ -255,8 +255,8 @@ public function getHourlyForecastFromGrid(
return [
"apparentTemperature" =>
abs($apparentTemperature - $temperature) >= 5
? $apparentTemperature
: null,
? $apparentTemperature
: null,
"conditions" => $this->t->translate(
ucfirst(strtolower($period["shortForecast"])),
),
Expand Down Expand Up @@ -285,31 +285,19 @@ public function getHourlyForecastFromGrid(
return $forecast;
}

public function getHourlyPrecipitation(
$wfo,
$x,
$y,
$now = false,
$self = false,
public function filterHourlyPrecipitationToDay(
\DateTimeImmutable $day,
$precipitationPeriods,
$timezone
) {
if (!$self) {
$self = $this;
}

if (!($now instanceof \DateTimeImmutable)) {
$now = new \DateTimeImmutable();
}

date_default_timezone_set("America/New_York");

$forecast = $this->dataLayer->getGridpoint($wfo, $x, $y)->properties;

$place = $this->getPlaceFromGrid($wfo, $x, $y);
$timezone = $place->timezone;
// Our NWS day starts after 6am of the "now" day
$dayStart = $day->setTime(6, 0);
// Our day ends at 6am of the day after the "now" day
$dayEnd = $day->modify("+ 1 day")->setTime(6, 0);

$periods = [];

foreach ($forecast->quantitativePrecipitation->values as $quantPrecip) {
foreach ($precipitationPeriods as $quantPrecip) {
$valid = $quantPrecip->validTime;
$value = $quantPrecip->value;
$value = UnitConversion::millimetersToInches($value);
Expand All @@ -320,7 +308,14 @@ public function getHourlyPrecipitation(
$duration = new \DateInterval($valid[1]);
$end = $start->add($duration);

if ($end >= $now) {
// Because we only have accumulated precip totals
// for the duration period, we can't cut off the end of that period.
// Therefore we only care about when the period starts.
// If it starts within the $dayStart and $dayEnd window, it
// counts for the current day.
$precipPeriodIsToday = $start >= $dayStart && $start < $dayEnd;

if ($precipPeriodIsToday) {
$periods[] = (object) [
"start" => $start->format("g A"),
"startRaw" => $start->format("c"),
Expand All @@ -333,4 +328,31 @@ public function getHourlyPrecipitation(

return $periods;
}

public function getHourlyPrecipitation(
$wfo,
$x,
$y,
$now = false,
$self = false,
) {
if (!$self) {
$self = $this;
}

if (!($now instanceof \DateTimeImmutable)) {
$now = new \DateTimeImmutable();
}

date_default_timezone_set("America/New_York");

$forecast = $this->dataLayer->getGridpoint($wfo, $x, $y)->properties;

$place = $this->getPlaceFromGrid($wfo, $x, $y);
$timezone = $place->timezone;

$periods = [];

return $forecast->quantitativePrecipitation->values;
}
}

0 comments on commit 3a7b681

Please sign in to comment.