Bike Riding Forecast
$weather = simplexml_load_file(“http://api.wunderground.com/api/c799df0081ddaacc/hourly10day/q/Australia/Geelong.xml”);
$monthDay = null;
$morningHtml = “”;
$nightHtml = “”;
echo(‘
‘); echo(getNextForecastConditions($weather)); echo(‘ |
‘); //echo(getCurrentConditions()); echo(‘ |
‘);
echo(‘
‘);
foreach ($weather->hourly_forecast->forecast as $forecast) {
if($forecast->FCTTIME->hour == ’10’ || $forecast->FCTTIME->hour == ’18’ || $forecast->FCTTIME->hour == ’19’){
$precipString = empty($forecast->pop)?’No rain‘:($forecast->pop.’% chance of rain’);
if($forecast->FCTTIME->hour == ’10’){
$morningHtml .= ‘RIDE IN’;
$morningHtml .= ‘
‘; $morningHtml .= ‘ |
‘;
$windSummaryStr = getWindDirSummary($forecast->wdir->degrees,true); $morningHtml .= $windSummaryStr; |
‘;
$windCallengeStr = getWindChallengeSummary($forecast->wspd->metric, $windSummaryStr); $morningHtml .= $windCallengeStr; |
|
Rain: | ‘.$precipString.’ |
UV: | ‘.$forecast->uvi.’ |
‘;
} else if($forecast->FCTTIME->hour == ’18’){
$nightHtml .= ‘RIDE HOME’;
$nightHtml .= ‘
‘; $nightHtml .= ‘ |
‘;
$windSummaryStr = getWindDirSummary($forecast->wdir->degrees,false); $nightHtml .= $windSummaryStr; |
‘;
$windCallengeStr = getWindChallengeSummary($forecast->wspd->metric, $windSummaryStr); $nightHtml .= $windCallengeStr; |
|
Rain: | ‘.$precipString.’ |
UV: | ‘.$forecast->uvi.’ |
‘;
} else if($forecast->FCTTIME->hour == ’19’ && (!empty($morningHtml) || !empty($nightHtml))){
echo(‘
‘.$forecast->FCTTIME->weekday_name_unlang.’, ‘.$forecast->FCTTIME->mday.’ ‘.$forecast->FCTTIME->month_name.’
‘);
echo(‘
‘);
echo(‘
‘); echo(empty($morningHtml) ? $nightHtml : $morningHtml); echo(‘ |
‘); echo(empty($morningHtml) ? ” : $nightHtml); echo(‘ |
‘);
echo(‘
‘);
echo(‘
‘);
echo(‘
‘);
$morningHtml = “”;
$nightHtml = “”;
}
}
}
function getWindDirSummary($degString, $morning) {
$degFloat = floatval($degString);
if($morning == true){
if($degFloat >= 348 || $degFloat <= 28){
return 'Full Tailwind!‘;
} else if($degFloat >= 323 || $degFloat <= 53){
return 'Tailwind';
} else if($degFloat >= 233){
$crossType = “Cutting”;
if($degFloat <= 257){
$crossType = "Resisting";
} else if($degFloat >= 299){
$crossType = “Boosting”;
}
return ‘Crosswind (Favourable – ‘.$crossType.’)’;
} else if($degFloat <= 143){
$crossType = "Cutting";
if($degFloat <= 78){
$crossType = "Boosting";
} else if($degFloat >= 119){
$crossType = “Resisting”;
}
return ‘Crosswind (Unfavourable – ‘.$crossType.’)’;
} else if($degFloat >= 208 || $degFloat <= 168){
return 'Headwind';
} else if($degFloat >= 169 || $degFloat <= 207){
return 'Full Headwind!‘;
} else {
return ‘Unknown ‘.$degFloat;
}
} else {
if($degFloat >= 348 || $degFloat <= 28){
return 'Full Headwind!‘;
} else if($degFloat >= 323 || $degFloat <= 53){
return 'Headwind';
} else if($degFloat >= 233){
$crossType = “Cutting”;
if($degFloat <= 257){
$crossType = "Boosting";
} else if($degFloat >= 299){
$crossType = “Resisting”;
}
return ‘Crosswind (Unfavourable – ‘.$crossType.’)’;
} else if($degFloat <= 143){
$crossType = "Cutting";
if($degFloat <= 78){
$crossType = "Resisting";
} else if($degFloat >= 119){
$crossType = “Boosting”;
}
return ‘Crosswind (Favourable – ‘.$crossType.’)’;
} else if($degFloat >= 208 || $degFloat <= 168){
return 'Tailwind';
} else if($degFloat >= 169 || $degFloat <= 207){
return 'Full Tailwind!‘;
} else {
return ‘Unknown ‘.$degFloat;
}
}
}
function getWindChallengeSummary($speedVal, $windSummaryStr) {
$speedFloat = floatval($speedVal);
$isTailwind = stripos($windSummaryStr, ‘tailwind’) !== false;
$windChallengeStr = “Unknown”;
if($speedFloat >= 40){
$windChallengeStr = ($isTailwind == true) ? ‘Ultra Super Sonic Boost’:’Extreme’;
} else if($speedFloat >= 35){
$windChallengeStr = ($isTailwind == true) ? ‘Ultra Boost’:’Very Difficult’;
} else if($speedFloat >= 30){
$windChallengeStr = ($isTailwind == true) ? ‘Super Boost’:’Difficult’;
} else if($speedFloat >= 25){
$windChallengeStr = ($isTailwind == true) ? ‘Great Boost’:’Challenging’;
} else if($speedFloat >= 20){
$windChallengeStr = ($isTailwind == true) ? ‘Good Boost’:’Moderate’;
} else if($speedFloat >= 15){
$windChallengeStr = ($isTailwind == true) ? ‘Helpful Boost’:’Normal’;
} else if($speedFloat >= 10){
$windChallengeStr = ($isTailwind == true) ? ‘Light Boost’:’Easy’;
} else if($speedFloat >= 5){
$windChallengeStr = ($isTailwind == true) ? ‘Tiny Boost’:’Breeze’;
} else if($speedFloat >= 1){
$windChallengeStr = ($isTailwind == true) ? ‘Micro Boost’:’Very Easy’;
} else {
return ‘No wind’;
}
return $windChallengeStr.’ @ ‘.$speedFloat.’ km/h’;
}
function getNextForecastConditions($weather){
$forecast = $weather->hourly_forecast->forecast[0];
$isMorning = (intval(date(‘G’,intval($forecast->FCTTIME->epoch)))) < 13;
$rideDirection = 'CURRENT RIDE '.($isMorning?'IN':'HOME');
$precipString = empty($forecast->pop)?’No rain‘:($forecast->pop.’% chance of rain’);
$morningHtml = $rideDirection .’ (‘.$forecast->FCTTIME->civil.’)’;
$morningHtml .= ‘
‘; $morningHtml .= ‘ |
‘;
$windSummaryStr = getWindDirSummary($forecast->wdir->degrees,$isMorning); $morningHtml .= $windSummaryStr; |
‘;
$windCallengeStr = getWindChallengeSummary($forecast->wspd->metric, $windSummaryStr); $morningHtml .= $windCallengeStr; |
|
Rain: | ‘.$precipString.’ |
UV: | ‘.$forecast->uvi.’ |
‘;
return $morningHtml;
}
//comes from different source (Geelong City)
function getCurrentConditions(){
$conditions = simplexml_load_file(“http://api.wunderground.com/api/c799df0081ddaacc/conditions/q/Australia/Geelong.xml”);
$precipString = floatval($conditions->current_observation->precip_1hr_metric) > 0 ? ($conditions->current_observation->precip_1hr_metric.” mm in the last hour”):’No rain’;
$isMorning = (intval(date(‘G’,intval($conditions->current_observation->local_epoch)))) < 13;
$rightNowHtml = "";
$rightNowHtml .= 'RIDE '.($isMorning?'IN':'HOME');
$rightNowHtml .= '
‘; $rightNowHtml .= ‘ |
‘;
$windSummaryStr = getWindDirSummary($conditions->current_observation->wind_degrees,$isMorning); $rightNowHtml .= $windSummaryStr; |
‘;
$windChallengeStr = getWindChallengeSummary($conditions->current_observation->wind_kph, $windSummaryStr); $gustingStr = (!empty($conditions->current_observation->wind_gust_kph) && (floatval($conditions->current_observation->wind_kph) !== floatval($conditions->current_observation->wind_gust_kph))) ? (‘ ( >>> ‘.$conditions->current_observation->wind_gust_kph.’ km/h)’) : ”; $rightNowHtml .= $windChallengeStr.$gustingStr; |
|
Rain: | ‘.$precipString.’ |
UV: | ‘.$conditions->current_observation->UV.’ |
‘;
return $rightNowHtml;
}