function noaabeaufort($wind, $usedunit = '') { global $wsDebug; $beaufort = array(); // return array with nr - color - text $colors = array("transparent", "transparent", "transparent", "transparent", "transparent", "transparent", "#FFFF53", "#F46E07", "#F00008", "#F36A6A", "#6D6F04", "#640071", "#650003"); $texts = array("Calm", "Light air", "Light breeze", "Gentle breeze", "Moderate breeze", "Fresh breeze", "Strong breeze", "Near gale", "Gale", "Strong gale", "Storm", "Violent storm", "Hurricane"); if ($usedunit != '' && $usedunit != 'kts') { // convert windspeed $wind = noaaconvertwind($wind, $usedunit, 'kts'); } $windkts = $wind * 1.0; # switch (TRUE) { // return a number for the beaufort scale based on wind in knots case $windkts < 1: $beuafortnr = 0; break; case $windkts < 4: $beuafortnr = 1; break; case $windkts < 7: $beuafortnr = 2; break; case $windkts < 11: $beuafortnr = 3; break; case $windkts < 17: $beuafortnr = 4; break; case $windkts < 22: $beuafortnr = 5; break; case $windkts < 28: $beuafortnr = 6; break; case $windkts < 34: $beuafortnr = 7; break; case $windkts < 41: $beuafortnr = 8; break; case $windkts < 48: $beuafortnr = 9; break; case $windkts < 56: $beuafortnr = 10; break; case $windkts < 64: $beuafortnr = 11; break; default: $beuafortnr = 12; break; } // eo switch $beaufort[] = $beuafortnr; $beaufort[] = $colors[$beuafortnr]; $beaufort[] = $texts[$beuafortnr]; ws_message('<!-- module noaaSettings.php (' . __LINE__ . '): function noaabeaufortnr in = winspeed: ' . $wind . ' , unitFrom: ' . $usedunit . ', nr = ' . $beaufort[0] . ' color =' . $beaufort[1] . ', text = ' . $beaufort[2] . ' -->'); return $beaufort; }
public function getWeatherDataDigital($lat = '', $lon = '') { global $myPageNoaa1, $myCacheDir, $dateLongFormat, $timeFormat, $myDefaultIconsDir, $myDefaultIconsExt, $myDefaultIconsSml, $myNoaaIconsDir, $myNoaaIconsExt, $myNoaaIconsSml, $uomTemp, $uomWind, $uomRain, $uomSnow, $uomDistance; #---------------------------------------------------------------------------------------------- # clean user input #---------------------------------------------------------------------------------------------- $this->apiUrlpart[1] = round(trim($lat), 3); $this->apiUrlpart[3] = round(trim($lon), 3); #---------------------------------------------------------------------------------------------- # try loading data from cache #---------------------------------------------------------------------------------------------- if ($this->enableCache && !empty($this->cache)) { $this->cache = $myCacheDir; $uoms = $uomTemp . '-' . $uomWind . '-' . $uomRain . '-' . $uomSnow . '-' . $uomDistance; $string = $myPageNoaa1 . $this->apiUrlpart[1] . $this->apiUrlpart[3] . $uoms; $from = array('°', '∞', '/', ' ', '.'); $string = str_replace($from, '', $string); $this->cacheFile = $this->cache . $string . '.txt'; $returnArray = $this->loadFromCache(); // load from cache returns data only when its data is valid if (!empty($returnArray)) { return $returnArray; // if data is in cache and valid return data to calling program } // eo valid data, return to calling program } // eo check cache #---------------------------------------------------------------------------------------------- # combine everything into required url # http://forecast.weather.gov/MapClick.php?lat=41.3&lon=-72.78&FcstType=dwml #---------------------------------------------------------------------------------------------- $this->weatherApiUrl = ''; $end = count($this->apiUrlpart); for ($i = 0; $i < $end; $i++) { $this->weatherApiUrl .= $this->apiUrlpart[$i]; } #---------------------------------------------------------------------------------------------- ws_message('<!-- module noaaDigitalGenerateHtml.php (' . __LINE__ . '): loading from ' . $this->weatherApiUrl . ' -->'); if ($this->makeRequest()) { // load xml from url and process $this->rawData = str_replace('-name', '_name', $this->rawData); $xml = new SimpleXMLElement($this->rawData); if (!isset($xml->head->product->{'creation-date'})) { $string = substr($this->rawData, 0, 100); ws_message('Module ' . $myPageNoaa1 . ' (' . __LINE__ . '): Invalid data loaded for ' . $this->weatherApiUrl . '<br />First 100 chars of data loaded = ' . $string, true); return; } $returnArray = array(); $utcDiff = date('Z'); // to help to correct utc differences $string = (string) $xml->head->product->{'creation-date'}; $time = strtotime($string); $returnArray['information']['fileTimeStamp'] = strtotime($string); $returnArray['information']['fileTime'] = date('c', strtotime($string)); $returnArray['information']['updated'] = date($dateLongFormat, $time) . ' ' . date($timeFormat, $time); $returnArray['information']['location'] = (string) $xml->data->location->description . '-' . (string) $xml->data->location->point['latitude'] . '-' . (string) $xml->data->location->point['longitude']; $returnArray['information']['issued'] = (string) $xml->head->source->{'production-center'}; #-------------------------------------------------------------------------------------------------- # get forecast info #-------------------------------------------------------------------------------------------------- $arrTimes = array(); $endData = count($xml->data); $nData = 0; $endLayouts = count($xml->data[$nData]->{'time-layout'}); if ($endLayouts == 0) { echo '<h3> ' . $myPageNoaa1 . ' - invalid xml file - program halted </h3>'; exit; } for ($nLayouts = 0; $nLayouts < $endLayouts; $nLayouts++) { $forecast = $xml->data[$nData]->{'time-layout'}[$nLayouts]; $endTimes = count($forecast->{'start-valid-time'}); $layoutKey = (string) $forecast->{'layout-key'}; for ($nTimes = 0; $nTimes < $endTimes; $nTimes++) { $key = $layoutKey . '|' . $nTimes; $startTime = (string) $forecast->{'start-valid-time'}[$nTimes]; $daypart = (string) $forecast->{'start-valid-time'}[$nTimes]['period_name']; $arrTimes[$key]['startTime'] = $startTime; if ($daypart != '') { $arrTimes[$key]['daypart'] = $daypart; } $endTime = (string) $forecast->{'end-valid-time'}[$nTimes]; $arrTimes[$key]['endTime'] = $endTime; } // eof times } // eof layouts # echo '<pre>'; print_r ($arrTimes); exit; $result = array(); $endParameters = count($xml->data[$nData]->{'parameters'}); for ($nParameters = 0; $nParameters < $endParameters; $nParameters++) { $arr = $xml->data[$nData]->{'parameters'}[$nParameters]; $count = -1; foreach ($arr as $key => $value) { $count++; $name = (string) $key; $layout = (string) $value['time-layout']; if (isset($value['type'])) { $type = (string) $value['type']; } else { $type = ''; } if (isset($value['units'])) { $units = (string) $value['units']; } else { $units = ''; } $endValues = count($value->value); for ($nValues = 0; $nValues < $endValues; $nValues++) { $start = $arrTimes[$layout . '|' . $nValues]['startTime'] . '|' . $arrTimes[$layout . '|' . $nValues]['endTime']; #echo '<pre>'.$start.PHP_EOL; $result[$start][$name][$type] = array('units' => $units, 'value' => (string) $value->value[$nValues]); } // eo for evert parameter value if (isset($value->{'weather-conditions'})) { $endConds = count($value->{'weather-conditions'}); for ($nConds = 0; $nConds < $endConds; $nConds++) { $start = $arrTimes[$layout . '|' . $nConds]['startTime'] . '|' . $arrTimes[$layout . '|' . $nConds]['endTime']; // $condition = $value->{'weather-conditions'}[$nConds]; # echo '<pre>'; print_r($condition); exit; $endValues = count($condition->value); for ($nValues = 0; $nValues < $endValues; $nValues++) { $type = 'weather-conditions'; $weather = (string) $condition->value[$nValues]['weather-type']; // rain showers $units = (string) $condition->value[$nValues]['coverage']; // chance $extra = (string) $condition->value[$nValues]['additive']; // and $intensity = (string) $condition->value[$nValues]['intensity']; // light or non $qualifier = (string) $condition->value[$nValues]['qualifier']; // ?? none? $result[$start][$name][$type][$nValues] = array('units' => $units, 'intensity' => $intensity, 'value' => $weather, 'qualifier' => $qualifier, 'extra' => $extra); } // for every conditionvalue } // eo for every weather-conditions } // eo if weather-conditions if (isset($value->{'icon-link'})) { # echo '<pre>'; print_r($value->{'icon-link'}); print_r($value); exit; $endIcons = count($value->{'icon-link'}); for ($nIcons = 0; $nIcons < $endIcons; $nIcons++) { $start = $arrTimes[$layout . '|' . $nIcons]['startTime'] . '|' . $arrTimes[$layout . '|' . $nIcons]['endTime']; $icon = $value->{'conditions-icon'}[$nIcons]; $result[$start][$name][$type] = array('units' => 'url', 'value' => (string) $value->{'icon-link'}[$nIcons]); } // eo for every icon } // eo if conditions-icon } // eo foreach parameter values } // eo for every parameter ksort($result); # echo '<pre>'.PHP_EOL; print_r ($result); echo '</pre>'.PHP_EOL;# exit; $arrFcst = array(); $tempFound = $dewpFound = $appFound = $windFound = $gustFound = $winddirFound = $cloudsFound = $humFound = $txtFound = $rainFound = $snowFound = $popFound = false; foreach ($result as $key => $arr) { list($from, $to) = explode('|', $key); if ($to == '') { // new point forecast if (isset($arrFcst['to'])) { $returnArray['forecast'][] = $arrFcst; $arrFcst['liquid'] = $arrFcst['liquidNU'] = $arrFcst['snow'] = $arrFcst['snowNU'] = '-'; } else { $arrFcst['to'] = $arrFcst['from']; $arrFcst['toStamp'] = strtotime($arrFcst['from']); } $arrFcst['from'] = $arrFcst['to']; $arrFcst['fromStamp'] = strtotime($arrFcst['to']); $arrFcst['to'] = $from; $arrFcst['toStamp'] = strtotime($from); if (isset($arr['temperature']['hourly']['value'])) { $string = $arr['temperature']['hourly']['value']; $amount = round(noaaconvertemp($string, $this->uomTemp), 1); $arrFcst['temp'] = $amount . $uomTemp; $arrFcst['tempNU'] = $amount; $tempFound = true; } else { $arrFcst['temp'] = $arrFcst['tempNU'] = ''; } if (isset($arr['temperature']['dew point']['value'])) { $string = $arr['temperature']['dew point']['value']; $amount = round(noaaconvertemp($string, $this->uomTemp), 1); $arrFcst['dewp'] = $amount . $uomTemp; $arrFcst['dewpNU'] = $amount; $dewpFound = true; } else { $arrFcst['dew'] = $arrFcst['dewpNU'] = ''; } if (isset($arr['temperature']['apparent']['value'])) { $string = $arr['temperature']['apparent']['value']; $amount = round(noaaconvertemp($string, $this->uomTemp), 1); $arrFcst['tempApp'] = $amount . $uomTemp; $arrFcst['tempAppNU'] = $amount; $appFound = true; } else { $arrFcst['tempApp'] = $arrFcst['tempAppNU'] = ''; } if (isset($arr['wind-speed']['sustained']['value'])) { $string = $arr['wind-speed']['sustained']['value']; $arrbft = noaabeaufort($string, $this->uomWindSpeed); $arrFcst['beaufortnr'] = $arrbft[0]; $arrFcst['beaufortclr'] = $arrbft[1]; $arrFcst['beauforttxt'] = $arrbft[2]; $amount = round(noaaconvertwind($string, $this->uomWindSpeed)); $arrFcst['windSpeed'] = $amount . $uomWind; $arrFcst['windSpeedNU'] = $amount; $windFound = true; $arrFcst['windSpeedNU'] = $amount; } else { $arrFcst['windSpeed'] = $arrFcst['windSpeedNU'] = ''; } if (isset($arr['wind-speed']['gust']['value'])) { $string = $arr['wind-speed']['gust']['value']; $amount = round(noaaconvertwind($string, $this->uomWindSpeed)); $arrFcst['windGust'] = $amount . $uomWind; $arrFcst['windGustNU'] = $amount; $gustFound = true; } else { $arrFcst['windGust'] = $arrFcst['windGustNU'] = ''; } if (isset($arr['direction']['wind']['value'])) { $arrFcst['windDirDeg'] = $arr['direction']['wind']['value']; $string = noaaconvertwinddir($arrFcst['windDirDeg']); $arrFcst['windDirRose'] = $string; $from = array('North', 'East', 'South', 'West'); $to = array('N', 'E', 'S', 'W'); $arrFcst['windLbl'] = str_replace($from, $to, $string); $winddirFound = true; } else { $arrFcst['windDirDeg'] = $arrFcst['windLbl'] = $arrFcst['windDirRose'] = ''; } if (isset($arr['cloud-amount']['total']['value'])) { $arrFcst['clouds'] = $arr['cloud-amount']['total']['value']; $cloudsFound = true; } else { $arrFcst['clouds'] = ''; } if (isset($arr['humidity']['relative']['value'])) { $arrFcst['hum'] = $arr['humidity']['relative']['value'] . ' %'; $humFound = true; } else { $arrFcst['hum'] = ''; } if (isset($arr['conditions-icon']['forecast-NWS']['value'])) { $iconFound = true; $string = $arr['conditions-icon']['forecast-NWS']['value']; $arrFcst['iconUrl'] = $string; $arrUrl = explode('/', $string); $nr = count($arrUrl) - 1; list($icon, $ext) = explode('.', $arrUrl[$nr]); $arrFcst['icon'] = $icon; $from = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); $inconIn = str_replace($from, '', $icon); $arrFcst['iconCleaned'] = $inconIn; $iconDefault = wsconvertnoaaicon($inconIn); $arrFcst['defailtIcon'] = $iconDefault; $arrFcst['defailtIconUrl'] = $myDefaultIconsDir . $iconDefault . '.' . $myDefaultIconsExt; $arrFcst['defailtIconSmlUrl'] = $myDefaultIconsSml . $iconDefault . '.' . $myDefaultIconsExt; $arrFcst['noaaIconUrl'] = $myNoaaIconsDir . $icon . '.' . $myNoaaIconsExt; $arrFcst['noaaIconSmlUrl'] = $myNoaaIconsSml . $icon . '.' . $myNoaaIconsExt; } else { $arrFcst['iconUrl'] = $arrFcst['iconDefault'] = $arrFcst['icon'] = ''; } $nr = 1.0 * $arrFcst['defailtIcon']; if ($nr < 101) { $string = 'no clouds'; } elseif ($nr < 201) { $string = 'some clouds'; } elseif ($nr < 301) { $string = 'partly cloudy'; } else { $string = 'overcast'; } $string = wsnoaafcttransstr($string); if (isset($arr['weather']['weather-conditions'])) { $endCond = count($arr['weather']['weather-conditions']); for ($i = 0; $i < $endCond; $i++) { $arrCond = $arr['weather']['weather-conditions'][$i]; # if ($arrCond['extra'] <> '') {$string .= '<br />'.wsnoaafcttransstr($arrCond['extra']);} if ($arrCond['units'] != '') { $string .= '<br />' . wsnoaafcttransstr($arrCond['units']) . ' '; } if ($arrCond['intensity'] != '' && $arrCond['intensity'] != 'none') { $string .= wsnoaafcttransstr($arrCond['intensity']) . ' '; } if ($arrCond['value'] != '') { $string .= wsnoaafcttransstr($arrCond['value']) . ' '; } if ($arrCond['qualifier'] != '' && $arrCond['qualifier'] != 'none') { $string .= wsnoaafcttransstr($arrCond['qualifier']) . ' '; } } $txtFound = true; } $arrFcst['weatherTxt'] = $string; continue; } else { if (!isset($arrFcst['from'])) { $arrFcst['from'] = $from; } } if (isset($arr['precipitation'])) { $arrFcst['liquid'] = $arrFcst['liquidNU'] = $arrFcst['snow'] = $arrFcst['snowNU'] = '-'; if (isset($arr['precipitation']['liquid'])) { $string = $arr['precipitation']['liquid']['value']; $amount = noaaconvertrain($string, $this->uomRain); $arrFcst['liquid'] = $amount . $uomRain; $arrFcst['liquidNU'] = $amount; $rainFound = true; } if (isset($arr['precipitation']['snow'])) { $string = $arr['precipitation']['snow']['value']; $amount = noaaconvertrain($string, $this->uomRain); $arrFcst['snow'] = $amount . $uomSnow; $arrFcst['snowNU'] = $amount; if ($amount != 0) { $snowFound = true; } } continue; } if (isset($arr['temperature'])) { $arrFcst['tempMax'] = $arrFcst['tempMin'] = ''; if (isset($arr['temperature']['maximum'])) { $string = $arr['temperature']['maximum']['value']; $amount = round(noaaconvertemp($string, $this->uomTemp)); $arrFcst['tempMax'] = $amount . $uomTemp; $arrFcst['tempMaxNU'] = $amount; } if (isset($arr['temperature']['minimum'])) { $string = $arr['temperature']['minimum']['value']; $amount = round(noaaconvertemp($string, $this->uomTemp)); $arrFcst['tempMin'] = $amount . $uomTemp; $arrFcst['tempMinNU'] = $amount; } } if (isset($arr['probability-of-precipitation'])) { $arrFcst['pop'] = $arr['probability-of-precipitation']['12 hour']['value']; $popFound = true; } } // eo for each $return $returnArray['forecast'][] = $arrFcst; $returnArray['information']['tempFound'] = $tempFound; $returnArray['information']['dewpFound'] = $dewpFound; $returnArray['information']['appFound'] = $appFound; $returnArray['information']['windFound'] = $windFound; $returnArray['information']['gustFound'] = $gustFound; $returnArray['information']['winddirFound'] = $winddirFound; $returnArray['information']['cloudsFound'] = $cloudsFound; $returnArray['information']['humFound'] = $humFound; $returnArray['information']['txtFound'] = $txtFound; $returnArray['information']['rainFound'] = $rainFound; $returnArray['information']['snowFound'] = $snowFound; $returnArray['information']['popFound'] = $popFound; $ret = $this->writeToCache($returnArray); return $returnArray; } else { echo '<h3>Loading data ' . $this->weatherApiUrl . ' failed </h3>' . PHP_EOL; } }