Skip to content

Commit

Permalink
Merge branch 'hotfix/wunderground'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwsmith committed Mar 30, 2019
2 parents af72e54 + 122125b commit 5aaabad
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 7 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
45 changes: 43 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions front-page.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
include('vendor/autoload.php');
use Rarst\WordPress\DateTime\WpDateTime;
use Rarst\WordPress\DateTime\WpDateTimeZone;

$data = json_decode(file_get_contents(WP_CONTENT_DIR . '/uploads/KCHS.json'), true);
//$data = json_decode(file_get_contents(WP_CONTENT_DIR . '/uploads/KCHS.json'), true);

$data = array();

$data['current_observation'] = chswx_normalize_observation_data(chswx_get_observation_data());

// Current conditions
if(isset($data['current_observation'])) {
Expand Down Expand Up @@ -75,18 +78,19 @@
}
?>
<?php get_header(); ?>
<script>
<?php /*<script>
jQuery(document).ready(function($) {
$('.alert').click(function(event) {
var toggleID = '#' + event.currentTarget.id.toString();
$(toggleID + ' ul').toggle();
});
});
</script>
*/?>
<div id="currentwx">
<h2>CURRENTLY</h2>
<?php
if(isset($data['current_observation'])) {
if(isset($data['current_observation'])) {
?>
<div id="temp" class="<?php echo $tempcolor?>"><?php echo $temperature?></div>
<?php if ($display_feels_like): ?>
Expand Down Expand Up @@ -146,6 +150,7 @@
?>
</div>
<?php } ?>
<?php if (isset($data['forecast'])) : ?>
<div id="forecast">
<h2>Forecast</h2>
<div class="updated-time">Forecast for Charleston updated at <?php echo $data['forecast']['txt_forecast']['date']?></div>
Expand All @@ -166,6 +171,7 @@
?>
</ul>
</div>
<?php endif; ?>
<?php
$blog_query = new WP_Query('post_type=post&limit=1');
if ($blog_query->have_posts()) {
Expand Down
93 changes: 93 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

include_once('vendor/autoload.php');

use Olifolkerd\Convertor\Convertor;

function register_stylesheet()
{
wp_enqueue_style('chswx-css', get_template_directory_uri() . '/css/responsive-style.css', false, 'all');
Expand Down Expand Up @@ -39,3 +43,92 @@ function is_blog_page()
// to determine if we're viewing a blog page.
return ( $post_type === 'post' ) && ( is_home() || is_archive() || is_single() );
}

function chswx_get_observation_data()
{
$ob = json_decode(file_get_contents(WP_CONTENT_DIR . '/uploads/KCHS_ob.json'), true);
return $ob['properties'];
}

/**
* Normalize observation data from the NWS API.
* For now, make it look like the wunderground output until we can do a better job.
* Prefixes: n_ = normalized, c_ = convertor
*/
function chswx_normalize_observation_data($ob)
{
// Initialize our array of normalized observations.
// Yeah, it's PHP, and you don't _have_ to, but no notices is nice
$n_ob = array(
'temp_f' => '',
'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)];
}

0 comments on commit 5aaabad

Please sign in to comment.