You are here

class weather_es_Aemet in Weather_es 7

Same name and namespace in other branches
  1. 6.3 weather_es.inc \weather_es_Aemet

@file Classes to get the weather information from AEMET

Hierarchy

Expanded class hierarchy of weather_es_Aemet

File

./weather_es.inc, line 6
Classes to get the weather information from AEMET

View source
class weather_es_Aemet {
  private $city_code;
  private $name;
  private static $url = 'http://www.aemet.es/xml/municipios/';

  //private static $url = 'http://drupal.local/';

  //private static $url = 'file:///var/www/jose/drupal-6.22/';
  private $dom;
  private $days;
  private $load_ok;
  public function __construct($cit_cod) {
    $this->city_code = $cit_cod;
    $this->dom = new DOMDocument('1.0', 'ISO-8859-15');
    $this->name = '';
    $success = $this->dom
      ->load(self::$url . 'localidad_' . $this->city_code . '.xml');
    if ($success) {
      $this->load_ok = TRUE;
      $this->name = $this->dom->documentElement
        ->getElementsByTagName('nombre')
        ->item(0)->nodeValue;
      $dias = $this->dom->documentElement
        ->getElementsByTagName('prediccion')
        ->item(0)->childNodes;
      $days = array();
      foreach ($dias as $dia) {
        if ($dia->nodeType == XML_ELEMENT_NODE) {
          $date = $dia
            ->getAttributeNode("fecha")->value;
          $datos = $dia->childNodes;
          $rain = array();
          $snow = array();
          $sky = array();
          $wind = array();
          $windSpeed = array();
          $temp = array();
          $sens = array();
          $hum = array();
          $uv = '';
          foreach ($datos as $dato) {
            if ($dato->nodeType == XML_ELEMENT_NODE) {
              if ($dato->nodeName == 'prob_precipitacion') {
                if (isset($dato
                  ->getAttributeNode('periodo')->value)) {
                  $rain[$dato
                    ->getAttributeNode('periodo')->value] = $dato->nodeValue;
                }
                else {
                  $rain['00-24'] = $dato->nodeValue;
                }
              }
              elseif ($dato->nodeName == 'cota_nieve_prov') {
                if (isset($dato
                  ->getAttributeNode('periodo')->value)) {
                  $snow[$dato
                    ->getAttributeNode('periodo')->value] = $dato->nodeValue;
                }
                else {
                  $snow['00-24'] = $dato->nodeValue;
                }
              }
              elseif ($dato->nodeName == 'estado_cielo') {
                if (isset($dato
                  ->getAttributeNode('periodo')->value)) {
                  $sky[$dato
                    ->getAttributeNode('periodo')->value] = array(
                    $dato
                      ->getAttributeNode('descripcion')->value => $dato->nodeValue,
                  );
                }
                else {
                  $sky['00-24'] = array(
                    $dato
                      ->getAttributeNode('descripcion')->value => $dato->nodeValue,
                  );
                }
              }
              elseif ($dato->nodeName == 'viento') {
                if (isset($dato
                  ->getAttributeNode('periodo')->value)) {
                  $wind[$dato
                    ->getAttributeNode('periodo')->value] = array(
                    $dato
                      ->getElementsByTagName('direccion')
                      ->item(0)->nodeValue => $dato
                      ->getElementsByTagName('velocidad')
                      ->item(0)->nodeValue,
                  );
                }
                else {
                  $wind['00-24'] = array(
                    $dato
                      ->getElementsByTagName('direccion')
                      ->item(0)->nodeValue => $dato
                      ->getElementsByTagName('velocidad')
                      ->item(0)->nodeValue,
                  );
                }
              }
              elseif ($dato->nodeName == 'racha_max') {
                if (isset($dato
                  ->getAttributeNode('periodo')->value)) {
                  $gust[$dato
                    ->getAttributeNode('periodo')->value] = $dato->nodeValue;
                }
                else {
                  $gust['00-24'] = $dato->nodeValue;
                }
              }
              elseif ($dato->nodeName == 'temperatura') {
                $temp = array(
                  'maxima' => $dato
                    ->getElementsByTagName('maxima')
                    ->item(0)->nodeValue,
                  'minima' => $dato
                    ->getElementsByTagName('minima')
                    ->item(0)->nodeValue,
                );
                foreach ($dato
                  ->getElementsByTagName('dato') as $d) {
                  $temp[$d
                    ->getAttributeNode('hora')->value] = $d->nodeValue;
                }
              }
              elseif ($dato->nodeName == 'sens_termica') {
                $chill = array(
                  'maxima' => $dato
                    ->getElementsByTagName('maxima')
                    ->item(0)->nodeValue,
                  'minima' => $dato
                    ->getElementsByTagName('minima')
                    ->item(0)->nodeValue,
                );
                foreach ($dato
                  ->getElementsByTagName('dato') as $d) {
                  $chill[$d
                    ->getAttributeNode('hora')->value] = $d->nodeValue;
                }
              }
              elseif ($dato->nodeName == 'humedad_relativa') {
                $hum = array(
                  'maxima' => $dato
                    ->getElementsByTagName('maxima')
                    ->item(0)->nodeValue,
                  'minima' => $dato
                    ->getElementsByTagName('minima')
                    ->item(0)->nodeValue,
                );
                foreach ($dato
                  ->getElementsByTagName('dato') as $d) {
                  $hum[$d
                    ->getAttributeNode('hora')->value] = $d->nodeValue;
                }
              }
              elseif ($dato->nodeName == 'uv_max') {
                $uv = $dato->nodeValue;
              }
            }
          }
          $this->days[] = array(
            'date' => $date,
            'rain' => $rain,
            'snow' => $snow,
            'sky' => $sky,
            'wind' => $wind,
            'gust' => $gust,
            'temp' => $temp,
            'chill' => $chill,
            'hum' => $hum,
            'uv' => $uv,
          );
        }
      }
    }
    else {
      $this->load_ok = FALSE;
    }
  }
  public function getName() {
    return $this->name;
  }
  public function getInfo() {
    return $this->days;
  }
  public function isLoadOk() {
    return $this->load_ok;
  }

}

Members