From 122125b4266e6f246cae1ed2e431fcf781599f6a Mon Sep 17 00:00:00 2001 From: Jared Smith Date: Sat, 30 Mar 2019 00:48:16 -0400 Subject: [PATCH] Get current conditions displaying from the NWS API --- composer.json | 9 ++++- composer.lock | 45 ++++++++++++++++++++++-- front-page.php | 14 +++++--- functions.php | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 65da8dc..a4a6b68 100644 --- a/composer.json +++ b/composer.json @@ -2,8 +2,15 @@ "name": "jaredwsmith/chswx.wp-theme", "description": "WordPress theme for chswx.com", "type": "project", + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/olifolkerd/convertor" + } + ], "require": { - "rarst/wpdatetime": "0.3" + "rarst/wpdatetime": "0.3", + "olifolkerd/convertor": "dev-master" }, "license": "GPLv2", "authors": [ diff --git a/composer.lock b/composer.lock index 1efa5a5..df9b22b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,47 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "759655579971a2c4a1e03d0f5a73f9cc", + "content-hash": "cc540301c4d9ab9a8b8d8ede4d1c9d13", "packages": [ + { + "name": "olifolkerd/convertor", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/olifolkerd/convertor.git", + "reference": "4487800116055a865c2e3245439d75fd4f6380a8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/olifolkerd/convertor/zipball/4487800116055a865c2e3245439d75fd4f6380a8", + "reference": "4487800116055a865c2e3245439d75fd4f6380a8", + "shasum": "" + }, + "require-dev": { + "phpunit/phpunit": "^6.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Olifolkerd\\Convertor\\": "src/" + } + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Oli Folkerd", + "email": "oliver.folkerd@gmail.com" + } + ], + "description": "An easy to use PHP unit conversion library.", + "support": { + "source": "https://github.com/olifolkerd/convertor/tree/master", + "issues": "https://github.com/olifolkerd/convertor/issues" + }, + "time": "2018-04-02T16:26:04+00:00" + }, { "name": "rarst/wpdatetime", "version": "0.3", @@ -56,7 +95,9 @@ "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "olifolkerd/convertor": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": [], diff --git a/front-page.php b/front-page.php index ee670fc..d4345d4 100644 --- a/front-page.php +++ b/front-page.php @@ -1,9 +1,12 @@ - +*/?>

CURRENTLY

@@ -146,6 +150,7 @@ ?>
+

Forecast

Forecast for Charleston updated at
@@ -166,6 +171,7 @@ ?>
+ have_posts()) { diff --git a/functions.php b/functions.php index 629e0ad..9b3d594 100644 --- a/functions.php +++ b/functions.php @@ -1,5 +1,9 @@ '', + 'dewpoint_f' => '', + 'pressure_in' => '', + 'relative_humidity' => '', + 'wind_mph' => '', + 'wind_dir' => '', + 'wind_gust_mph' => '', + 'feelslike_f' => '', + 'heat_index_f' => '', + 'weather' => '', + 'observation_epoch' => '', + ); + + // Set up individual elements to convert. + // We will need to do null checks on anything initializing a new Convertor. + // Otherwise, they will fail out as a fatal (sad! bad design! hiss!) + $t = $ob['temperature']['value']; + $tD = $ob['dewpoint']['value']; + $hi = $ob['heatIndex']['value']; + $wc = $ob['windChill']['value']; + $windSpd = $ob['windSpeed']['value']; + $windGust = $ob['windGust']['value']; + + // Start unit conversions... + if (!is_null($t)) { + $c_temp = new Convertor($t, 'c'); + $n_ob['temp_f'] = $c_temp->to('f'); + } + + if (!is_null($tD)) { + $c_dpt = new Convertor($tD, 'c'); + } + + if (!is_null($windSpd)) { + $c_wind = new Convertor($windSpd, 'm s**-1'); + } + + if (!is_null($windGust)) { + $c_gust = new Convertor($windGust, 'm s**-1'); + $n_ob['wind_gust_mph'] = round($c_gust->to('mi h**-1')) . " mph"; + } + + // Take the value of the heat index if it is not null, otherwise use wind chill + $feelslike = !is_null($hi) ? $hi : $wc; + + $c_pres = $ob['barometricPressure']['value'] / 3386.389; + + // End unit conversions. Start appending values to the array... + $n_ob['dewpoint_f'] = round($c_dpt->to('f')); + $n_ob['pressure_in'] = round($c_pres, 2); + $n_ob['relative_humidity'] = round($ob['relativeHumidity']['value']) . '%'; + $n_ob['wind_mph'] = round($c_wind->to('mi h**-1')) . " mph"; + $n_ob['wind_dir'] = chswx_get_wind_direction($ob['windDirection']['value']); + $n_ob['feelslike_f'] = !is_null($feelslike) ? round(Convertor($feelslike, 'c')->to('f')) : $n_ob['temp_f']; + $n_ob['observation_epoch'] = strtotime($ob['timestamp']); + + return $n_ob; +} + +/** + * PHP port of a solution found at https://stackoverflow.com/questions/7490660/converting-wind-direction-in-angles-to-text-words + * + * @param int $dir Angle of the compass + * + * @return string Textual wind direction + */ +function chswx_get_wind_direction($dir) +{ + $val = (int) ($dir / 22.5) + 0.5; + $directions = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']; + return $directions[($val % 16)]; +}