You are here

public function weather_es_Aemet::__construct in Weather_es 6.3

Same name and namespace in other branches
  1. 7 weather_es.inc \weather_es_Aemet::__construct()

File

./weather_es.inc, line 14

Class

weather_es_Aemet
Class that gets the weather information of a city

Code

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->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)) {

                //$windSpeed[$dato->getAttributeNode('periodo')->value] = $dato->nodeValue;
                $gust[$dato
                  ->getAttributeNode('periodo')->value] = $dato->nodeValue;
              }
              else {

                //$windSpeed['00-24'] = $dato->nodeValue;
                $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) {

                //$sens[$d->getAttributeNode('hora')->value] = $d->nodeValue;
                $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' => array($date),
          'date' => $date,
          'rain' => $rain,
          'snow' => $snow,
          'sky' => $sky,
          'wind' => $wind,
          //'windSpeed' => $windSpeed,
          'gust' => $gust,
          'temp' => $temp,
          //'sens' => $sens,
          'chill' => $chill,
          'hum' => $hum,
          //'uv' => array($uv),
          'uv' => $uv,
        );
      }
    }
  }
}