You are here

function _weather_parse_forecast in Weather 7.3

Same name and namespace in other branches
  1. 7.2 weather_parser.inc \_weather_parse_forecast()

Parses an XML forecast supplied by yr.no.

Parameters

string $xml: XML to be parsed.

string $geoid: The GeoID for which the forecasts should be parsed.

Return value

bool TRUE on success, FALSE on failure.

2 calls to _weather_parse_forecast()
weather_admin_places_form_submit in ./weather.forms.inc
Handle the submission of a new place.
_weather_download_forecast in ./weather_parser.inc
Downloads a new forecast from yr.no.

File

./weather_parser.inc, line 179
Retrieves and parses raw METAR data and stores result in database.

Code

function _weather_parse_forecast($xml, $geoid = '') {

  // In case the parsing fails, do not output all error messages.
  $use_errors = libxml_use_internal_errors(TRUE);
  $fc = simplexml_load_string($xml);

  // Restore previous setting of error handling.
  libxml_use_internal_errors($use_errors);
  if ($fc === FALSE) {
    return FALSE;
  }

  // Update weather_places table with downloaded information, if necessary.
  _weather_update_places($fc);

  // Extract meta information.
  // @TODO: Extract GeoID of returned XML data.
  // This might differ from the data we have in the database. An example
  // was Heraklion (ID 261745), which got the forecast for
  // Nomós Irakleíou (ID 261741).
  if ($geoid == '') {
    $geoid = $fc->location->location['geobase'] . "_" . $fc->location->location['geobaseid'];
  }
  $meta['geoid'] = $geoid;
  $meta['utc_offset'] = (int) $fc->location->timezone['utcoffsetMinutes'];

  // Calculate the UTC time.
  $utctime = strtotime((string) $fc->meta->lastupdate . ' UTC') - 60 * $meta['utc_offset'];
  $meta['last_update'] = gmdate('Y-m-d H:i:s', $utctime);

  // Calculate the UTC time.
  $utctime = strtotime((string) $fc->meta->nextupdate . ' UTC') - 60 * $meta['utc_offset'];
  $meta['next_update'] = gmdate('Y-m-d H:i:s', $utctime);
  $meta['next_download_attempt'] = $meta['next_update'];

  // Merge meta information for this location.
  // This prevents an integrity constraint violation, if multiple
  // calls to this function occur at the same time. See bug #1412352.
  db_merge('weather_forecast_information')
    ->key(array(
    'geoid' => $meta['geoid'],
  ))
    ->fields($meta)
    ->execute();

  // Remove all forecasts for this location.
  db_delete('weather_forecasts')
    ->condition('geoid', $meta['geoid'])
    ->execute();

  // Cycle through all forecasts and write them to the table.
  foreach ($fc->forecast->tabular->time as $time) {
    $forecast = array();
    $forecast['geoid'] = $meta['geoid'];
    $forecast['time_from'] = str_replace('T', ' ', (string) $time['from']);
    $forecast['time_to'] = str_replace('T', ' ', (string) $time['to']);
    $forecast['period'] = (string) $time['period'];
    $forecast['symbol'] = (string) $time->symbol['var'];

    // Remove moon phases, which are not supported.
    // This is in the format "mf/03n.56", where 56 would be the
    // percentage of the moon phase.
    if (strlen($forecast['symbol']) > 3) {
      $forecast['symbol'] = substr($forecast['symbol'], 3, 3);
    }
    $forecast['precipitation'] = (double) $time->precipitation['value'];
    $forecast['wind_direction'] = (int) $time->windDirection['deg'];
    $forecast['wind_speed'] = (double) $time->windSpeed['mps'];
    $forecast['temperature'] = (int) $time->temperature['value'];
    $forecast['pressure'] = (int) $time->pressure['value'];

    // Use db_merge to prevent integrity constraint violation, see above.
    db_merge('weather_forecasts')
      ->key(array(
      'geoid' => $meta['geoid'],
      'time_from' => $forecast['time_from'],
    ))
      ->fields($forecast)
      ->execute();
  }
  return TRUE;
}