'2010') { // got a last rain valid date // $dateoflastrainalways = MHdateOnly($WX['year1_rain0_ratemax_time'],$SITE['WDdateMDY']); // $timeoflastrainalways = MHtimeOnly($WX['year1_rain0_ratemax_time']); // // $dayswithnorain = intval((strtotime(date('Y-m-d'))-strtotime(MHISOdateOnly($WX['year1_rain0_ratemax_time']))-1)/86400); //} // Note: above removed because it is not a good way to determine the values from existing Meteohub variables. V1.03 $raincurrentweek = 0; for ($i=0;$i<7;$i++) { $raccumweek = MH_seqval('seqday1_rain0_total_mm',8,$uomrain); if(is_numeric($raccumweek)) {$raincurrentweek+= $raccumweek;} } $raincurrentweek = MHsetRainDP($raincurrentweek,$uomrain); #------------------------------------------------------------------------------------- # MH support function - MH_RainRateIcon #------------------------------------------------------------------------------------- function MH_RainRateIcon($inText,$inIcon,$inRate,$inUOM,$time,$sunrise,$sunset,$lastRain='0000-00-00T00:00:00') { global $Debug; /* Rainfall intensity is classified according to the rate of precipitation: Light rain — rate is < 2.5 mm (0.098 in) per hour Moderate rain — rate is between 2.5 mm (0.098 in) - 7.6 mm (0.30 in) or 10 mm (0.39 in) per hour Heavy rain — rate is > 7.6 mm (0.30 in) per hour, or between 10 mm (0.39 in) and 50 mm (2.0 in) per hour Violent rain — rate is > 50 mm (2.0 in) per hour */ $Debug .= "\n"; $newText = ''; // assume no changes $newIcon = $inIcon; $rate = $inRate; if(preg_match('|in|i',$inUOM)) { // convert to mm/hr rate $rate = $inRate * 25.4; } if(substr($lastRain,0,4) <> '0000') { if($rate < 0.001 and time()-strtotime($lastRain) < 30*60) { $newText = 'Moderate Drizzle'; } } if ($rate > 0.0 and $rate < 2.5) { $newText = 'Light Rain'; } if ($rate >=2.5 and $rate < 7.6) { $newText = 'Moderate Rain'; } if ($rate >=7.6 and $rate < 50.0) { $newText = 'Heavy Rain'; } if ($rate >= 50.0) { $newText = 'Violent Rain'; } if($newText <> '' or $rate == 0.0) { if ($newText <> '' and $inText <> '') {$newText .= ', ';} $newText .= preg_replace('/(Light|Moderate|Heavy|Violent|Extreme){0,1}\s*(Rain|Mist|Drizzle), /i','',$inText); $newIcon = mtr_get_iconnumber ($time,$newText,$sunrise,$sunset); } else { $newText = $inText; } $Debug .= "\n"; return(array($newText,$newIcon)); } #------------------------------------------------------------------------------------- # MH support function - MHtimeOnly #------------------------------------------------------------------------------------- function MHsetRainDP ($val,$units) { if(preg_match('|in|',$units)) { return(sprintf("%01.2f",$val)); } else { return(sprintf("%01.1f",$val)); } } #------------------------------------------------------------------------------------- # MH support function - MHtimeOnly #------------------------------------------------------------------------------------- function MHtimeOnly ($indatetime) { // Return HH:MM (24hr time format) // expecting // 0....+....1....+ // 20110622061003 // yyyymmddhhMMss return(substr($indatetime,8,2).':'.substr($indatetime,10,2)); } #------------------------------------------------------------------------------------- # MH support function - MHdateOnly #------------------------------------------------------------------------------------- function MHdateOnly ($indatetime,$MDY=true) { // Return dd/mm/yyyy or mm/dd/yyyy // expecting // 0....+....1....+ // 20110622061003 // yyyymmddhhMMss if($MDY) { return(substr($indatetime,4,2).'/'.substr($indatetime,6,2).'/'.substr($indatetime,0,4)); } else { return(substr($indatetime,6,2).'/'.substr($indatetime,4,2).'/'.substr($indatetime,0,4)); } } #------------------------------------------------------------------------------------- # MH support function - MHdateOnly #------------------------------------------------------------------------------------- function MHISOdateOnly ($indatetime) { // Return dd/mm/yyyy or mm/dd/yyyy // expecting // 0....+....1....+ // 20110622061003 // yyyymmddhhMMss return(substr($indatetime,0,4).'-'. substr($indatetime,4,2).'-'.substr($indatetime,6,2)); } #------------------------------------------------------------------------------------- # MH support function - MHWDrecordDate #------------------------------------------------------------------------------------- function MHWDrecordDate ($indatetime) { // extract Y, M, D and return as array // expecting // 0....+....1....+ // 20110622061003 // yyyymmddhhMMss return(array(substr($indatetime,0,4),substr($indatetime,4,2),substr($indatetime,6,2))); } #------------------------------------------------------------------------------------- # MH support function - MHfixupTime #------------------------------------------------------------------------------------- function MHfixupTime ($intime) { global $Debug; $tfixed = preg_replace('/^(\S+)\s+(\S+)$/is',"$2",$intime); $t = explode(':',$tfixed); if (preg_match('/p/i',$tfixed)) { $t[0] = $t[0] + 12; } if ($t[0] > 23) {$t[0] = 12; } if (preg_match('/^12.*a/i',$tfixed)) { $t[0] = 0; } if ($t[0] < '10') {$t[0] = sprintf("%02d",$t[0]); } // leading zero on hour. $t2 = join(':',$t); // put time back to gether; $t2 = preg_replace('/[^\d\:]/is','',$t2); // strip out the am/pm if any $Debug .= "\n"; return($t2); } // end MHfixupTime #------------------------------------------------------------------------------------- # MH support function - MH_setDateTimes #------------------------------------------------------------------------------------- function MH_setDateTimes ($indate,$intime,$MDYformat=true) { // returns: $date_year,$date_month,$date_day,$time_hour,$time_minute,$date_month,$monthname,$dayname global $Debug; $Debug .= "\n"; $d = explode('/',$indate); if(!isset($d[2])) {$d = explode("-",$indate);} if($d[2]<2000) {$d[2]+=2000;} if($MDYformat) { // mm/dd/yyyy $YMD = "$d[2]-$d[0]-$d[1]"; } else { // dd/mm/yyyy $YMD = "$d[2]-$d[1]-$d[0]"; } $t = MHfixupTime($intime); $MHtime = strtotime("$YMD $t:00"); $Debug .= "\n"; $MHtime = date('Y m d H i F l',$MHtime); $Debug .= "\n"; if(isset($_REQUEST['debug'])) {echo $Debug; } return(explode(' ',$MHtime)); // results returned in array for list() assignment } // end MH_setDateTimes #------------------------------------------------------------------------------------- # MH support function - MH_beaufortNumber #------------------------------------------------------------------------------------- function MH_beaufortNumber ($inWind,$usedunit) { global $Debug; $rawwind = $inWind; // first convert all winds to knots if(strpos($inWind,',') !== false) { $rawwind = preg_replace('|,|','.',$inWind); } $wind0kts = 0.0; if (preg_match('/kts|knot/i',$usedunit)) { $wind0kts = $rawwind * 1.0; } elseif (preg_match('/mph/i',$usedunit)) { $wind0kts = $rawwind * 0.8689762; } elseif (preg_match('/mps|m\/s/i',$usedunit)) { $wind0kts = $rawwind * 1.94384449; } elseif (preg_match('/kmh|km\/h/i',$usedunit)) { $wind0kts = $rawwind * 0.539956803; } else { $Debug .= "\n"; $wind0kts = $rawwind * 1.0; } // return a number for the beaufort scale based on wind in knots if ($wind0kts < 1 ) {return(0); } if ($wind0kts < 4 ) {return(1); } if ($wind0kts < 7 ) {return(2); } if ($wind0kts < 11 ) {return(3); } if ($wind0kts < 17 ) {return(4); } if ($wind0kts < 22 ) {return(5); } if ($wind0kts < 28 ) {return(6); } if ($wind0kts < 34 ) {return(7); } if ($wind0kts < 41 ) {return(8); } if ($wind0kts < 48 ) {return(9); } if ($wind0kts < 56 ) {return(10); } if ($wind0kts < 64 ) {return(11); } if ($wind0kts >= 64 ) {return(12); } return("0"); } // end MH_beaufortNumber #------------------------------------------------------------------------------------- # MH support function - MH_beaufortText #------------------------------------------------------------------------------------- function MH_beaufortText ($beaufortnumber) { $B = array( /* Beaufort 0 to 12 in English */ "Calm", "Light air", "Light breeze", "Gentle breeze", "Moderate breeze", "Fresh breeze", "Strong breeze", "Near gale", "Gale", "Strong gale", "Storm", "Violent storm", "Hurricane" ); if(isset($B[$beaufortnumber])) { return $B[$beaufortnumber]; } else { return "Unknown $beaufortnumber Bft"; } } // end MH_beaufortText #------------------------------------------------------------------------------------- # MH support function - MH_setFeelslike #------------------------------------------------------------------------------------- function MH_setFeelslike ($temp,$windchill,$heatindex,$tempUOM) { global $Debug; // establish the feelslike temperature and return a word describing how it feels $HeatWords = array( 'Unknown', 'Extreme Heat Danger', 'Heat Danger', 'Extreme Heat Caution', 'Extremely Hot', 'Uncomfortably Hot', 'Hot', 'Warm', 'Comfortable', 'Cool', 'Cold', 'Uncomfortably Cold', 'Very Cold', 'Extreme Cold' ); // first convert all temperatures to Centigrade if need be $TC = $temp; $WC = $windchill; $HC = $heatindex; if(strpos($TC,',') !== false) { $TC = preg_replace('|,|','.',$temp); $WC = preg_replace('|,|','.',$windchill); $HC = preg_replace('|,|','.',$heatindex); } if (preg_match('|F|i',$tempUOM)) { // convert F to C if need be $TC = sprintf("%01.1f",round(($TC-32.0) / 1.8,1)); $WC = sprintf("%01.1f",round(($WC-32.0) / 1.8,1)); $HC = sprintf("%01.1f",round(($HC-32.0) / 1.8,1)); } // Feelslike if ($TC <= 16.0 ) { $feelslike = $WC; //use WindChill } elseif ($TC >=27.0) { $feelslike = $HC; //use HeatIndex } else { $feelslike = $TC; // use temperature } if (preg_match('|F|i',$tempUOM)) { // convert C back to F if need be $feelslike = (1.8 * $feelslike) + 32.0; } $feelslike = round($feelslike,0); // determine the 'heat color word' to use $hcWord = $HeatWords[0]; $hcFound = false; if ($TC > 32 and $HC > 29) { if ($HC > 54 and ! $hcFound) { $hcWord = $HeatWords[1]; $hcFound = true;} if ($HC > 45 and ! $hcFound) { $hcWord = $HeatWords[2]; $hcFound = true; } if ($HC > 39 and ! $hcFound) { $hcWord = $HeatWords[4]; $hcFound = true; } if ($HC > 29 and ! $hcFound) { $hcWord = $HeatWords[6]; $hcFound = true; } } elseif ($WC < 16 ) { if ($WC < -18 and ! $hcFound) { $hcWord = $HeatWords[13]; $hcFound = true; } if ($WC < -9 and ! $hcFound) { $hcWord = $HeatWords[12]; $hcFound = true; } if ($WC < -1 and ! $hcFound) { $hcWord = $HeatWords[11]; $hcFound = true; } if ($WC < 8 and ! $hcFound) { $hcWord = $HeatWords[10]; $hcFound = true; } if ($WC < 16 and ! $hcFound) { $hcWord = $HeatWords[9]; $hcFound = true; } } elseif ($WC >= 16 and $TC <= 32) { if ($TC <= 26 and ! $hcFound) { $hcWord = $HeatWords[8]; $hcFound = true; } if ($TC <= 32 and ! $hcFound) { $hcWord = $HeatWords[7]; $hcFound = true; } } if(isset($_REQUEST['debug'])) { echo "\n"; } return(array($feelslike,$hcWord)); } // end of MH_setFeelslike #------------------------------------------------------------------------------------- # MH support function - MH_CBI - Chandler Burning Index #------------------------------------------------------------------------------------- function MH_CBI($inTemp,$inTempUOM,$inHumidity) { // thanks to Chris from sloweather.com for the CBI calculation script // modified by Ken True for template usage preg_match('/([\d\.\,\+\-]+)/',$inTemp,$t); // strip non-numeric from inTemp if any $ctemp = $t[1]; if(strpos($ctemp,',') !== false) { $ctemp = preg_replace('|,|','.',$ctemp); } if(!preg_match('|C|i',$inTempUOM)) { $ctemp = ($ctemp-32.0) / 1.8; // convert from Fahrenheit } preg_match('/([\d\.\,\+\-]+)/',$inHumidity,$t); // strip non-numeric from inHumidity if any $rh = $t[1]; if(strpos($rh,',') !== false) { $rh = preg_replace('|,|','.',$rh); } // Start Index Calcs // Chandler Index $cbi = (((110 - 1.373 * $rh) - 0.54 * (10.20 - $ctemp)) * (124 * pow(10,-0.0142 * $rh) ))/60; // CBI = (((110 - 1.373*RH) - 0.54 * (10.20 - T)) * (124 * 10**(-0.0142*RH)))/60 //Sort out the Chandler Index $cbi = round($cbi,1); if ($cbi > "97.5") { $cbitxt = "EXTREME"; $cbiimg= "fdl_extreme.gif"; } elseif ($cbi >="90") { $cbitxt = "VERY HIGH"; $cbiimg= "fdl_vhigh.gif"; } elseif ($cbi >= "75") { $cbitxt = "HIGH"; $cbiimg= "fdl_high.gif"; } elseif ($cbi >= "50") { $cbitxt = "MODERATE"; $cbiimg= "fdl_moderate.gif"; } else { $cbitxt="LOW"; $cbiimg= "fdl_low.gif"; } $data = array($cbi,$cbitxt,$cbiimg); return $data; } // end MH_CBI #------------------------------------------------------------------------------------- # MH support function - MH_getRecordDate - Convert 'dd Month '.'YYYY' to list(d,m,y) #------------------------------------------------------------------------------------- function MH_getRecordDate($inDayMonth,$inYear) { global $SITE,$Debug; $EnglishMonths = array( 'January','February','March','April','May','June', 'July','August','September','October','November','December'); $tstr = $inDayMonth . ' ' . $inYear; $Debug .= "\n"; // input dates are assumed to be in English only if (isset($SITE['monthNames'])) { // convert TO English for strtotime() foreach ($EnglishMonths as $i => $monthEN) { $tstr = preg_replace('|'.$SITE['monthNames'][$i].'|i',$monthEN,$tstr); } $Debug .= "\n"; } $ttime = strtotime($tstr); $ltime = date('j n Y',$ttime); $Debug .= "\n"; return(explode(' ',$ltime)); } #------------------------------------------------------------------------------------- # MH support function - MH_getAlltimeDate - Convert 'at 2:45 PM on 16 October 2009' to list(d,m,y) #------------------------------------------------------------------------------------- function MH_getAlltimeDate($inDate) { global $SITE,$Debug; $EnglishMonths = array( 'January','February','March','April','May','June', 'July','August','September','October','November','December'); $Debug .= "\n"; if(preg_match('|^at (.*) on (.*)$|i',$inDate)) { // at 2:45 PM on 16 October 2009 $tstr = "$2 $1"; } elseif (preg_match('|^on (.*)$|i',$inDate)) { // on 16 October 2009 $tstr = "$1"; } // input dates are assumed to be in English only if (isset($SITE['monthNames'])) { // convert TO English for strtotime() foreach ($EnglishMonths as $i => $monthEN) { $tstr = preg_replace('|'.$SITE['monthNames'][$i].'|i',$monthEN,$tstr); } $Debug .= "\n"; } $ttime = strtotime($tstr); $ltime = date('j n Y H:i',$ttime); $Debug .= "\n"; return(explode(' ',$ltime)); } #------------------------------------------------------------------------------------- # MH support function - MH_get_barotrend_text #------------------------------------------------------------------------------------- function MH_get_barotrend_text($rawpress,$usedunit='hPa') { global $myUOM,$dpBaro,$Debug; // routine from Anole's wxsticker PHP (adapted) // Barometric Trend(3 hour) // Change Rates // Rapidly: =.06" inHg; 1.5 mm Hg; 2 hPa; 2 mb // Slowly: =.02" inHg; 0.5 mm Hg; 0.7 hPa; 0.7 mb // 5 Arrow Positions: // Rising Rapidly // Rising Slowly // Steady // Falling Slowly // Falling Rapidly // Page 52 of the PDF Manual // http://www.davisnet.com/product_documents/weather/manuals/07395.234-VP2_Manual.pdf // first convert to hPa for comparisons if (preg_match('/hPa|mb/i',$usedunit)) { $btrend = sprintf("%02.1f",round($rawpress * 1.0,1)); // leave in hPa } elseif (preg_match('/mm/i',$usedunit)) { $btrend = sprintf("%02.1f",round($rawpress * 1.333224,1)); } else { $btrend = sprintf("%02.1f",round($rawpress / 33.86388158,1)); } // figure out a text value for barometric pressure trend (float)$baromtrend = $btrend; // settype($baromtrend, "float"); switch (TRUE) { case (($baromtrend >= -0.6) and ($baromtrend <= 0.6)): $baromtrendwords = "Steady"; break; case (($baromtrend > 0.6) and ($baromtrend < 2.0)): $baromtrendwords = "Rising Slowly"; break; case ($baromtrend >= 2.0): $baromtrendwords = "Rising Rapidly"; break; case (($baromtrend < -0.6) and ($baromtrend > -2.0)): $baromtrendwords = "Falling Slowly"; break; case ($baromtrend <= -2.0): $baromtrendwords = "Falling Rapidly"; break; } // end switch $Debug .= "\n"; return($baromtrendwords); } #------------------------------------------------------------------------------------- # MH support function - MH_seqval #------------------------------------------------------------------------------------- function MH_seqval ($tag,$index,$outUOM) { // purpose: explode seq... array from tag, extract one value by index and convert // from default UOM to needed UOM in site global $WX,$Debug; if(!isset($WX[$tag])) { $Debug .= "\n"; return (""); } $vals = explode(" ",$WX[$tag]); if(!isset($vals[$index])) { $Debug .= "\n"; return (""); } $val = $vals[$index]; // got the thing $uoms = explode("_",$tag); $inUOM = array_pop($uoms); // get UOM from tag name $outVal = $val; // default to return with NO CONVERSION // perform required conversions based on UOMs switch ($inUOM) { case 'c': // temperature conversion if(preg_match('|F|i',$outUOM) ) { $outVal = round(($val * 1.8) + 32.0,1); } $outVal = sprintf("%01.1f",$outVal); // ensure 1 decimal point break; case 'kn': // wind conversion if(preg_match('|mph|i',$outUOM) ) { // kts->mph $outVal = sprintf("%01.1f",round($val*1.15077945,1)); } if(preg_match('/(ms|mps|m\/s)/i',$outUOM) ) { // kts->meters per second $outVal = sprintf("%01.1f",round($val*0.514444444,1)); } if(preg_match('/(kmh|kph|km\/h)/i',$outUOM) ) { // kts->kilometers per hour $outVal = sprintf("%01.1f",round($val*1.85200,1)); } break; case 'hpa': // barometer conversion if(preg_match('|inHg|i',$outUOM) ) { $outVal = (float)($val * 0.0295333727 ); // hPa(or mb) to inHg $outVal = sprintf("%01.2f",round($outVal,2)); } else { $outVal = sprintf("%01.1f",round($val,1)); } break; case 'mm': // rain conversion if(preg_match('|in|i',$outUOM) ) { // convert mm to inches $outVal = (float)($val * 0.0393700787); $outVal = sprintf("%01.2f",round($outVal,2)); } break; case 'rel': // humidity $outVal = round($val,0); break; } // end switch($inUOM) $Debug .= "\n"; return($outVal); } #------------------------------------------------------------------------------------- # MH support function - MH_seqvals #------------------------------------------------------------------------------------- function MH_seqvals ($tag,$istart,$iend,$outUOM,$oper) { // purpose: explode seq... array from tag, extract one value by index and convert // from default UOM to needed UOM in site global $WX,$Debug; if(!isset($WX[$tag])) { $Debug .= "\n"; return (""); } $vals = explode(" ",$WX[$tag]); $val = 0; for ($index=$istart;$index<=$iend;$index++) { if(!isset($vals[$index])) { echo "\n"; return (""); } else { $t = $vals[$index]; // got the thing if ($oper == 'plus') {$val += $t;} if ($oper == 'minus') {$val -= $t;} } } $uoms = explode("_",$tag); $inUOM = array_pop($uoms); // get UOM from tag name $outVal = $val; // default to return with NO CONVERSION // perform required conversions based on UOMs switch ($inUOM) { case 'c': // temperature conversion if(preg_match('|F|i',$outUOM) ) { $outVal = round(($val * 1.8) + 32.0,1); } $outVal = sprintf("%01.1f",$outVal); // ensure 1 decimal point break; case 'kn': // wind conversion if(preg_match('|mph|i',$outUOM) ) { // kts->mph $outVal = sprintf("%01.1f",round($val*1.15077945,1)); } if(preg_match('/(ms|mps|m\/s)/i',$outUOM) ) { // kts->meters per second $outVal = sprintf("%01.1f",round($val*0.514444444,1)); } if(preg_match('/(kmh|kph|km\/h)/i',$outUOM) ) { // kts->kilometers per hour $outVal = sprintf("%01.1f",round($val*1.85200,1)); } break; case 'hpa': // barometer conversion if(preg_match('|inHg|i',$outUOM) ) { $outVal = (float)($val * 0.0295333727 ); // hPa(or mb) to inHg $outVal = sprintf("%01.2f",round($outVal,2)); } else { $outVal = sprintf("%01.1f",round($val,1)); } break; case 'mm': // rain conversion if(preg_match('|in|i',$outUOM) ) { // convert mm to inches $outVal = (float)($val * 0.0393700787); $outVal = sprintf("%01.2f",round($outVal,2)); } break; case 'rel': // humidity $outVal = round($val,0); break; } // end switch($inUOM) echo "\n"; return($outVal); } #------------------------------------------------------------------------------------- # MH support function - MH_seqval #------------------------------------------------------------------------------------- function MH_getWindDir ($degrees) { // figure out a text value for compass direction // Given the wind direction, return the text label // for that value. 16 point compass $winddir = $degrees; if ($winddir == "n/a") { return($winddir); } if (!isset($winddir)) { return "---"; } if (!is_numeric($winddir)) { return($winddir); } $windlabel = array ("N","NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW","SW", "WSW", "W", "WNW", "NW", "NNW"); $dir = $windlabel[ (integer)fmod((($winddir + 11) / 22.5),16) ]; return($dir); } // end function getWindDir #------------------------------------------------------------------------------------- # end of MH support functions #------------------------------------------------------------------------------------- ?>