Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Created September 14, 2020 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronpk/e6736c5e9b90781390f00fe24d2a98c6 to your computer and use it in GitHub Desktop.
Save aaronpk/e6736c5e9b90781390f00fe24d2a98c6 to your computer and use it in GitHub Desktop.
<?php
function aqiFromPM($pm) {
if($pm >= 350.5)
return scale($pm, 500, 401, 500, 350.5);
if($pm >= 250.5)
return scale($pm, 400, 301, 350.4, 250.5);
if($pm >= 150.5)
return scale($pm, 300, 201, 250.4, 150.5);
if($pm >= 55.5)
return scale($pm, 200, 151, 150.4, 55.5);
if($pm >= 35.5)
return scale($pm, 150, 101, 55.4, 35.5);
if($pm >= 12.1)
return scale($pm, 100, 51, 35.4, 12.1);
if($pm >= 0)
return scale($pm, 50, 0, 12, 0);
return '';
}
function scale($pm, $aqiHigh, $aqiLow, $pmHigh, $pmLow) {
$a = $aqiHigh - $aqiLow;
$b = $pmHigh - $pmLow;
$c = $pm - $pmLow;
return round(($a/$b) * $c + $aqiLow);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment