2016年7月14日 星期四

擷取天氣資料


前天編寫的抓取天氣數據程式中,沒有當月的數據,只有上月及之前的資料;而我希望的是伺服器每天儲存昨天的溫度記錄,所以要找過一條新的數據源。打開天文台的網頁,找到了昨天天氣資料的版面。若用 Chrome 觀察當中的連線,會發現數據來自 http://www.hko.gov.hk/cis/dailyExtract/dailyExtract_201607.xml 這個檔。打開惠會看到當月的天氣數據,正是我想要的東西。那麼,只要簡單改寫一下程式,便能把數據載入、擷取、儲存。
<?php
//----------------------------------------------------------------------------------------
//   Weather Data Parser
//----------------------------------------------------------------------------------------
//   Platform: PHP
//   Written by Pacess HO
//   Copyright 2016 Pacess Studio.  All rights reserved.
//----------------------------------------------------------------------------------------

header("Access-Control-Allow-Origin: https://home.pacess.com");
header("Access-Control-Allow-Methods: POST");

header("Content-type: application/json");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Tue, 07 Nov 2000 00:00:00 GMT");

date_default_timezone_set("Asia/Hong_Kong");
// mb_internal_encoding("UTF-8");
ini_set("memory_limit", "-1");
set_time_limit(0);

//----------------------------------------------------------------------------------------
require("./library/constants.php");
require("./library/DBConnection.php");
require("./library/weatherDataHongKong.php");

//----------------------------------------------------------------------------------------
//  Weather data come from Hong Kong Observatory
$urlFormat = "http://www.hko.gov.hk/cis/dailyExtract/dailyExtract_YYYYMM.xml";

$timeStamp = time()-(60*60*24);
$year = date("Y", $timeStamp);
$month = date("m", $timeStamp);
$day = intval(date("d", $timeStamp));

echo("\n");
echo("-----------------------------------------------------------\n");
echo("--  Weather Data Parser Version 1.00                     --\n");
echo("--  Written by Pacess HO                                 --\n");
echo("--  Copyright 2016 Pacess Studio.  All rights reserved.  --\n");
echo("-----------------------------------------------------------\n\n");

//  Prepare save file
$savePath = "./files/dailyExtract_$year$month.xml";

//  Construct download URL
$url = str_replace("YYYY", $year, $urlFormat);
$url = str_replace("MM", $month, $url);

//  Get content from URL
echo("Data source: $url\n");
$content = file_get_contents($url);

//  Save it to local folder
$result = file_put_contents($savePath, $content);

//  Parse content
echo("Parsing date: $year-$month-$day\n");
$jsonObject = json_decode($content);
$dataArray = $jsonObject->stn->data;
foreach ($dataArray as $data)  {

   $dataMonth = $data->month;
   if ($month != $dataMonth)  {continue;}

   //  Looping here because avoid non-linear ordering
   $monthData = $data->dayData;
   foreach ($monthData as $dayData)  {

      if ($dayData[0] != $day)  {continue;}
      echo("Data: ".json_encode($dayData)."\n");

      //  Parse data to variables
      $i = 1;
      $meanPressure = $dayData[$i++];
      $absoluteDailyMaxDegree = $dayData[$i++];
      $meanDegree = $dayData[$i++];
      $absoluteDailyMinDegree = $dayData[$i++];
      $meanDewPointDegree = $dayData[$i++];
      $meanRelativeHumidityPercentage = $dayData[$i++];
      $meanAmountOfCloudPercentage = $dayData[$i++];
      $totalRainfallMM = $dayData[$i++];

      //  Save to database
      $dictionary = array(
         "year"=>$year,
         "month"=>$month,
         "day"=>$day,
         "meanPressure"=>$meanPressure,
         "absoluteDailyMaxDegree"=>$absoluteDailyMaxDegree,
         "meanDegree"=>$meanDegree,
         "absoluteDailyMinDegree"=>$absoluteDailyMinDegree,
         "meanDewPointDegree"=>$meanDewPointDegree,
         "meanRelativeHumidityPercentage"=>$meanRelativeHumidityPercentage,
         "meanAmountOfCloudPercentage"=>$meanAmountOfCloudPercentage,
         "totalRainfallMM"=>$totalRainfallMM);

      $weatherDataHongKong = new weatherDataHongKong();
      if ($weatherDataHongKong != null)  {

         $dictionary = $weatherDataHongKong->insertOrUpdateRecord($dictionary);
         if ($dictionary != null)  {

            $affectedRows = $dictionary["affectedRows"];
            echo("Data saved with result #$affectedRows\n\n");
         }  else  {

            //  Unable to save data, maybe query problem?
            echo("### Unable to save data...\n\n");
         }
      }  else  {

         //  Unable to create database instance
         echo("### Unable to create database instance...\n\n");
      }

      //  Release database instance
      unset($weatherDataHongKong);
      break;
   }
   break;
}
?>

沒有留言: