<commit_before><?php
chdir(__DIR__ . '/../');

ini_set('error_log', 'logs/site_error.log');
ini_set('date.timezone', 'UTC');
ini_set('log_errors', 'On');
ini_set('display_errors', 'Off');
ini_set('display_startup_errors', 'Off');
ini_set('error_reporting', E_ALL);
mb_internal_encoding('UTF-8');

require 'vendor/autoload.php';

use \PhpUnitsOfMeasure\PhysicalQuantity\Length;
use \PhpUnitsOfMeasure\PhysicalQuantity\Temperature;
use \PhpUnitsOfMeasure\PhysicalQuantity\Pressure;

$app = new Silex\Application();

$app->get(
    '/api/v1/getBreathability/{latitude}/{longitude}',
    function ($latitude, $longitude) use ($app) {

        $latitude = $app->escape($latitude);
        $longitude = $app->escape($longitude);

        $altitude = getAltitude($latitude, $longitude);

        // TODO: it's not obvious what the temperature should be. Wunderground.com api call?
        $temperature = new Temperature(0, 'C');

        $atmospheric_pressure = getAtmosphericPressure($altitude, $temperature);

        $breathability = isBreathable($atmospheric_pressure);

        $data = array(
            'latitude'             => $latitude,
            'longitude'            => $longitude,
            'altitude'             => (string) $altitude,
            'temperature'          => (string) $temperature,
            'atmospheric_pressure' => (string) $atmospheric_pressure,
            'breathability'        => $breathability
        );

        $data['html'] = (new Mustache_Engine())->render(
            file_get_contents('www/resources/templates/yes.mustache'),
            $data
        );

        return json_encode($data);
    }
);

$app->run();



/**
 * Given the altitude and temperature, find the atmospheric pressure
 *
 * @param \PhpUnitsOfMeasure\PhysicalQuantity\Length      $altitude    height above sea level, in meters
 * @param \PhpUnitsOfMeasure\PhysicalQuantity\Temperature $temperature temperature, in Kelvin
 *
 * @return \PhpUnitsOfMeasure\PhysicalQuantity\Pressure   the atmospheric pressure, in mbar
 */
function getAtmosphericPressure(Length $altitude, Temperature $temperature)
{
    // Thanks, wikipedia: http://en.wikipedia.org/wiki/Vertical_pressure_variation#In_the_context_of_Earth.27s_atmosphere

    // Sea level pressure (Pa)
    $P0 = 101325;

    // Mass of 1 mol of air (kg)
    $M = 0.0289644;

    // Gravitational accelleration (m/s^2)
    $g = 9.80665;

    // Altitude above sea level (m)
    $z = $altitude->toUnit('m');

    // Gas Constant (J/kg/m)
    $R = 8.31447;

    // Temperature (K)
    $T = $temperature->toUnit('K');

    // Calculated atmospheric pressure at altitude and temperature
    $P = $P0 * exp(-$M * $g * $z / $R / $T);
    return new Pressure($P, 'Pa');
}

/**
 * Answer, as best as possible, the question "Is the
 * air breathable here?".
 *
 * @param  \PhpUnitsOfMeasure\PhysicalQuantity\Pressure $atmospheric_pressure Atmospheric pressure in mbar
 *
 * @return string Yes, Maybe, or Nope, depending
 */
function isBreathable(Pressure $atmospheric_pressure)
{
    if ($atmospheric_pressure->toUnit('Pa') > 80609 ) {
        // Roughly equivalent to 6000' and below
        return 'Yes';

    } else if ($atmospheric_pressure->toUnit('Pa') > 69208 ) {
        // roughly between 6000' and 10,000'
        return 'Maybe';

    } else {
        // more than about 10,000'
        return 'Nope';

    }
}

/**
 * Given latitude and longitude, determine the altitude at that point on Earth.
 *
 * We're using earthtools.org's altitude service to fetch these values.
 *
 * @param  float $latitude  Latitude in decimal degrees
 * @param  float $longitude Longitude in decimal degrees<commit_msg>satisfying the nagging code standards<commit_after><?php
chdir(__DIR__ . '/../');

ini_set('error_log', 'logs/site_error.log');
ini_set('date.timezone', 'UTC');
ini_set('log_errors', 'On');
ini_set('display_errors', 'Off');
ini_set('display_startup_errors', 'Off');
ini_set('error_reporting', E_ALL);
mb_internal_encoding('UTF-8');

require 'vendor/autoload.php';

use \PhpUnitsOfMeasure\PhysicalQuantity\Length;
use \PhpUnitsOfMeasure\PhysicalQuantity\Temperature;
use \PhpUnitsOfMeasure\PhysicalQuantity\Pressure;

$app = new Silex\Application();

$app->get(
    '/api/v1/getBreathability/{latitude}/{longitude}',
    function ($latitude, $longitude) use ($app) {

        $latitude = $app->escape($latitude);
        $longitude = $app->escape($longitude);

        $altitude = getAltitude($latitude, $longitude);

        // TODO: it's not obvious what the temperature should be. Wunderground.com api call?
        $temperature = new Temperature(0, 'C');

        $atmospheric_pressure = getAtmosphericPressure($altitude, $temperature);

        $breathability = isBreathable($atmospheric_pressure);

        $data = array(
            'latitude'             => $latitude,
            'longitude'            => $longitude,
            'altitude'             => (string) $altitude,
            'temperature'          => (string) $temperature,
            'atmospheric_pressure' => (string) $atmospheric_pressure,
            'breathability'        => $breathability
        );

        $data['html'] = (new Mustache_Engine())->render(
            file_get_contents('www/resources/templates/yes.mustache'),
            $data
        );

        return json_encode($data);
    }
);

$app->run();



/**
 * Given the altitude and temperature, find the atmospheric pressure
 *
 * @param \PhpUnitsOfMeasure\PhysicalQuantity\Length      $altitude    height above sea level, in meters
 * @param \PhpUnitsOfMeasure\PhysicalQuantity\Temperature $temperature temperature, in Kelvin
 *
 * @return \PhpUnitsOfMeasure\PhysicalQuantity\Pressure   the atmospheric pressure, in mbar
 */
function getAtmosphericPressure(Length $altitude, Temperature $temperature)
{
    // Thanks, wikipedia: http://en.wikipedia.org/wiki/Vertical_pressure_variation#In_the_context_of_Earth.27s_atmosphere

    // Sea level pressure (Pa)
    $P0 = 101325;

    // Mass of 1 mol of air (kg)
    $M = 0.0289644;

    // Gravitational accelleration (m/s^2)
    $g = 9.80665;

    // Altitude above sea level (m)
    $z = $altitude->toUnit('m');

    // Gas Constant (J/kg/m)
    $R = 8.31447;

    // Temperature (K)
    $T = $temperature->toUnit('K');

    // Calculated atmospheric pressure at altitude and temperature
    $P = $P0 * exp(-$M * $g * $z / $R / $T);
    return new Pressure($P, 'Pa');
}

/**
 * Answer, as best as possible, the question "Is the
 * air breathable here?".
 *
 * @param  \PhpUnitsOfMeasure\PhysicalQuantity\Pressure $atmospheric_pressure Atmospheric pressure in mbar
 *
 * @return string Yes, Maybe, or Nope, depending
 */
function isBreathable(Pressure $atmospheric_pressure)
{
    if ($atmospheric_pressure->toUnit('Pa') > 80609) {
        // Roughly equivalent to 6000' and below
        return 'Yes';

    } else if ($atmospheric_pressure->toUnit('Pa') > 69208) {
        // roughly between 6000' and 10,000'
        return 'Maybe';

    } else {
        // more than about 10,000'
        return 'Nope';

    }
}

/**
 * Given latitude and longitude, determine the altitude at that point on Earth.
 *
 * We're using earthtools.org's altitude service to fetch these values.
 *
 * @param  float $latitude  Latitude in decimal degrees
 * @param  float $longitude Longitude in decimal degrees
{'fraction_non_alphanumeric': 0.1338411316648531, 'fraction_numerical': 0.017138193688792165, 'mean_word_length': 3.069175428887659, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5473377', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2261, 'n_words': 547}