Services_Weather
[ class tree: Services_Weather ] [ index: Services_Weather ] [ all elements ]

Source for file buildMetarDB.php

Documentation is available at buildMetarDB.php

  1. #!/usr/local/bin/php
  2. <?php
  3. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2004 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available through the world-wide-web at                              |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: buildMetarDB.php,v 1.21 2004/05/05 08:33:43 eru Exp $
  21.  
  22. /**
  23. * This script downloads, saves and processes the textfiles needed for
  24. * the building the databases to enable searching for METAR stations.
  25. *
  26. * You can download the locations, which is a database of about 12000 world-
  27. * wide locations, which can be used to determine the coordinates of your
  28. * city or you can download a file with 6500 airports providing the metar
  29. * data. This database is used for the next-METAR-station search. Please see
  30. * the apropriate documentation in the Services_Weather_Metar class.
  31. *
  32. * For usage of this script, invoke with '-h'.
  33. *
  34. @author       Alexander Wirtz <alex@pc4p.net>
  35. @link         http://weather.noaa.gov/tg/site.shtml
  36. @package      Services_Weather
  37. @subpackage   buildMetarDB
  38. @filesource
  39. @version      1.3
  40. */
  41.  
  42. /**
  43. */
  44. require_once "DB.php";
  45.  
  46. /**
  47. @ignore
  48. */
  49. // {{{ constants
  50. // {{{ natural constants and measures
  51. define("SERVICES_WEATHER_RADIUS_EARTH"6378.15);
  52. // }}}
  53. // }}}
  54.  
  55. // {{{ Services_Weather_checkData()
  56. /**
  57. * Services_Weather_checkData
  58. *
  59. * Checks the data for a certain string-length and if it either consists of
  60. * a certain char-type or a string of "-" as replacement.
  61. *
  62. @param    array                           $data           The data to be checked
  63. @param    array                           $dataOrder      Because the data is in different locations, we provide this
  64. @return   bool 
  65. */
  66. function Services_Weather_checkData($data$dataOrder)
  67. {
  68.     $return = true;
  69.     foreach ($dataOrder as $type => $idx{
  70.         switch (strtolower($type)) {
  71.             case "b":
  72.                 $len  = 2;
  73.                 $func "ctype_digit";
  74.                 break;
  75.             case "s":
  76.                 $len  = 3;
  77.                 $func "ctype_digit";
  78.                 break;
  79.             case "i":
  80.                 $len  = 4;
  81.                 $func "ctype_alnum";
  82.                 break;
  83.             default:
  84.                 break;
  85.         }
  86.         if ((strlen($data[$idx]!= $len|| (!$func($data[$idx]&& ($data[$idx!= str_repeat("-"$len)))) {
  87.             $return = false;
  88.             break;
  89.         }
  90.     }
  91.     return $return;
  92. }
  93. // }}}
  94.  
  95. // {{{ Services_Weather_getNextArg()
  96. /**
  97. * Services_Weather_getNextArg
  98. *
  99. * Checks, if the next argument is a parameter to a predecessing option.
  100. * Returns either that parameter or false, if the next argument is an option
  101. *
  102. @param    int                             $c              Internal argument counter
  103. @return   string|bool
  104. */
  105. {
  106.     if ((($c + 1$_SERVER["argc"]&& ($_SERVER["argv"][$c + 1]{0!= "-")) {
  107.         $c++;
  108.         return $_SERVER["argv"][$c];
  109.     else {
  110.         return false;
  111.     }    
  112. }
  113. // }}}
  114.  
  115. // First set a few variables for processing the options
  116. $modeSet   = false;
  117. $saveFile  = false;
  118. $printHelp = false;
  119. $invOpt    = false;
  120. $verbose   = 0;
  121. $dbType    "mysql";
  122. $dbProt    "unix";
  123. $dbName    "servicesWeatherDB";
  124. $dbUser    "root";
  125. $dbPass    "";
  126. $dbHost    "localhost";
  127. $dbOptions = array();
  128. $userFile  "";
  129.  
  130. // Iterate through the arguments and check their validity
  131. for ($c = 1; $c $_SERVER["argc"]$c++{
  132.     switch ($_SERVER["argv"][$c]{1}{
  133.         case "l":
  134.             // location-mode, if another mode is set, bail out
  135.             if ($modeSet{
  136.                 $printHelp = true;
  137.             else {
  138.                 $modeSet   = true;
  139.                 $filePart  "bbsss";
  140.                 $tableName "metarLocations";
  141.                 $dataOrder = array("b" => 0"s" => 1"i" => 2);
  142.             }
  143.             break;
  144.         case "a":
  145.             // dito for airport-mode
  146.             if ($modeSet{
  147.                 $printHelp = true;
  148.             else {
  149.                 $modeSet   = true;
  150.                 $filePart  "cccc";
  151.                 $tableName "metarAirports";
  152.                 $dataOrder = array("b" => 1"s" => 2"i" => 0);
  153.             }
  154.             break;
  155.         case "f":
  156.             // file-flag was provided, check if next argument is a string
  157.             if (($userFile Services_Weather_getNextArg($c)) === false{
  158.                 $printHelp = true;
  159.             }
  160.             break;
  161.         case "s":
  162.             // If you download the file, it will be saved to disk
  163.             $saveFile      = true;
  164.             break;
  165.         case "t":
  166.             // The type of the DB to be used
  167.             if (($dbType Services_Weather_getNextArg($c)) === false{
  168.                 $printHelp = true;
  169.             }
  170.             break;
  171.         case "r":
  172.             // The protocol of the DB to be used
  173.             if (($dbProt Services_Weather_getNextArg($c)) === false{
  174.                 $printHelp = true;
  175.             }
  176.             break;
  177.         case "d":
  178.             // The name of the DB to be used
  179.             if (($dbName Services_Weather_getNextArg($c)) === false{
  180.                 $printHelp = true;
  181.             }
  182.             break;
  183.         case "u":
  184.             // The user of the DB to be used
  185.             if (($dbUser Services_Weather_getNextArg($c)) === false{
  186.                 $printHelp = true;
  187.             }
  188.             break;
  189.         case "p":
  190.             // The password of the DB to be used
  191.             if (($dbPass Services_Weather_getNextArg($c)) === false{
  192.                 $printHelp = true;
  193.             }
  194.             break;
  195.         case "h":
  196.             // The host of the DB to be used
  197.             if (($dbHost Services_Weather_getNextArg($c)) === false{
  198.                 $printHelp = true;
  199.             }
  200.             break;
  201.         case "o":
  202.             // Options for the DB
  203.             if (($options Services_Weather_getNextArg($c)) === false{
  204.                 $printHelp = true;
  205.             else {
  206.                 $options   explode(","$options);
  207.                 foreach ($options as $option{
  208.                     $optPair explode("="$option);
  209.                     $dbOptions[$optPair[0]] $optPair[1];
  210.                 }
  211.             }
  212.             break;
  213.         case "v":
  214.             // increase verbosity
  215.             for ($i = 1; $i strlen($_SERVER["argv"][$c])$i++{
  216.                 if ($_SERVER["argv"][$c]{$i== "v"{
  217.                     $verbose++;
  218.                 else {
  219.                     $invOpt    = true;
  220.                     break;
  221.                 }
  222.             }
  223.             break;
  224.         default:
  225.             // argument not valid, bail out
  226.             $invOpt    = true;
  227.             break;
  228.     }
  229.     if ($invOpt{
  230.         // see above
  231.         $printHelp = true;
  232.         echo "Invalid option: '".$_SERVER["argv"][$c]."'\n";
  233.         break;
  234.     }
  235. }
  236.  
  237. // help-message
  238. if (!$modeSet || $printHelp{
  239.     echo "Usage: ".basename($_SERVER["argv"][0]".php")." -l|-a [options]\n";
  240.     echo "Options:\n";
  241.     echo "  -l              build locationsDB\n";
  242.     echo "  -a              build airportsDB\n";
  243.     echo "  -f <file>       use <file> as input\n";
  244.     echo "  -s              save downloaded file to disk\n";
  245.     echo "  -t <dbtype>     type of the DB to be used\n";
  246.     echo "  -r <dbprotocol> protocol -----\"----------\n";
  247.     echo "  -d <dbname>     name ---------\"----------\n";
  248.     echo "  -u <dbuser>     user ---------\"----------\n";
  249.     echo "  -p <dbpass>     pass ---------\"----------\n";
  250.     echo "  -h <dbhost>     host ---------\"----------\n";
  251.     echo "  -o <dboptions>  options ------\"----------\n";
  252.     echo "                  in the notation option=value,...\n";
  253.     echo "  -v              display verbose debugging messages\n";
  254.     echo "                  multiple -v increases verbosity\n";
  255.     exit(255);
  256. }
  257.  
  258. // check, if zlib is available
  259. if (extension_loaded("zlib")) {
  260.     $open  "gzopen";
  261.     $close "gzclose";
  262.     $files = array(
  263.         $userFile"nsd_".$filePart"nsd_".$filePart.".txt",
  264.         "nsd_".$filePart.".gz""http://weather.noaa.gov/data/nsd_".$filePart.".gz"
  265.     );
  266. else {
  267.     $open  "fopen";
  268.     $close "fclose";
  269.     $files = array(
  270.         $userFile"nsd_".$filePart"nsd_".$filePart.".txt",
  271.         "http://weather.noaa.gov/data/nsd_".$filePart.".txt"
  272.     );
  273. }
  274. // then try to open a source in the given order
  275. foreach ($files as $file{
  276.     $fp @$open($file"rb");
  277.     if ($fp{
  278.         // found a valid source
  279.         if ($verbose > 0{
  280.             echo "Services_Weather: Using '".$file."' as source.\n";
  281.         }
  282.         if ($saveFile && !file_exists($file)) {
  283.             // apparently we want to save the file, and it's a remote file
  284.             $file basename($file);
  285.             $fps @$open($file"wb");
  286.             if (!$fps{
  287.                 echo "Services_Weather: Couldn't save to '".$file."'!\n";
  288.             else {
  289.                 if ($verbose > 0{
  290.                     echo "Services_Weather: Saving source to '".$file."'.\n";
  291.                 }
  292.                 // read from filepointer and save to disk
  293.                 while ($line fread($fp1024)) {
  294.                     fwrite($fps$linestrlen($line));
  295.                 }
  296.                 // unfortunately zlib does not support r/w on a resource,
  297.                 // so no rewind -> move $fp to new file on disk
  298.                 $close($fp);
  299.                 $close($fps);
  300.                 $fp @$open($file"rb");
  301.             }
  302.         }
  303.         break;
  304.     }
  305. }
  306. if (!$fp{
  307.     // no files found, or connection not available... bail out
  308.     die("Services_Weather: Sourcefile nsd_".$filePart." not found!\n");
  309. }
  310.  
  311. $dsn     $dbType."://".$dbUser.":".$dbPass."@".$dbProt."+".$dbHost."/".$dbName;
  312. $dsninfo = array(
  313.     "phptype"  => $dbType,
  314.     "protocol" => $dbProt,
  315.     "username" => $dbUser,
  316.     "password" => $dbPass,
  317.     "hostspec" => $dbHost,
  318.     "database" => $dbName,
  319.     "mode"     => "0644"
  320. );
  321.  
  322. $db  = DB::connect($dsninfo$dbOptions);
  323. if (DB::isError($db)) {
  324.     echo "Services_Weather: Connection to DB with '".$dbType."://".$dbUser.":PASS@".$dbHost."/".$dbName."' failed!\n";
  325.     die($db->getMessage()."\n");
  326. else {
  327.     // Test, if we have to swipe or create the table first
  328.     $select "SELECT * FROM ".$tableName;
  329.     $result $db->query($select);
  330.  
  331.     if (DB::isError($result)) {
  332.         // Create new table
  333.         $create "CREATE TABLE ".$tableName."(id int,block int,station int,icao varchar(4),name varchar(80),state varchar(2),country varchar(50),wmo int,latitude float,longitude float,elevation float,x float,y float,z float)";
  334.         if ($verbose > 0{
  335.             echo "Services_Weather: Creating table '".$tableName."'.\n";
  336.         }
  337.         $result  $db->query($create);
  338.         if (DB::isError($result)) {
  339.             die($result->getMessage()."\n");
  340.         }
  341.     else {
  342.         // Delete the old stuff
  343.         $delete "DELETE FROM ".$tableName;
  344.         if ($verbose > 0{
  345.             echo "Services_Weather: Deleting from table '".$tableName."'.\n";
  346.         }
  347.         $result $db->query($delete);
  348.         if (DB::isError($result)) {
  349.             die($result->getMessage()."\n");
  350.         }
  351.     }
  352.  
  353.     // Ok, DB should be up and running now, let's shove in the data
  354.     $line   = 0;
  355.     $error  = 0;
  356.     // read data from file
  357.     while ($data fgetcsv($fp1000";")) {
  358.         // Check for valid data
  359.         if ((sizeof($data< 9|| !Services_Weather_checkData($data$dataOrder)) {
  360.                 echo "Services_Weather: Invalid data in file!\n";
  361.                 echo "\tLine ".($line + 1).": ".implode(";"$data)."\n";
  362.                 $error++;
  363.         else {
  364.             // calculate latitude and longitude
  365.             // it comes in a ddd-mm[-ss]N|S|E|W format
  366.             $coord = array"latitude" => 7"longitude" => 8);
  367.             foreach ($coord as $latlon => $aId{
  368.                 preg_match("/^(\d{1,3})-(\d{1,2})(-(\d{1,2}))?([NSEW])$/"$data[$aId]$result);
  369.                 ${$latlon= 0; $factor = 1;
  370.                 foreach ($result as $var{
  371.                     if ((strlen($var> 0&& ctype_digit($var)) {
  372.                         ${$latlon+= $var $factor;
  373.                         $factor *= 60;
  374.                     elseif (ctype_alpha($var&& in_array($vararray("S""W"))) {
  375.                         ${$latlon*= (-1);
  376.                     }
  377.                 }
  378.             }
  379.  
  380.             // Calculate the cartesian coordinates for latitude and longitude
  381.             $theta deg2rad($latitude);
  382.             $phi   deg2rad($longitude);
  383.  
  384.             $x SERVICES_WEATHER_RADIUS_EARTH * cos($phicos($theta);
  385.             $y SERVICES_WEATHER_RADIUS_EARTH * sin($phicos($theta);
  386.             $z SERVICES_WEATHER_RADIUS_EARTH             * sin($theta);
  387.  
  388.             // Check for elevation in data
  389.             $elevation is_numeric($data[11]$data[11: 0;
  390.  
  391.             // integers: convert "--" fields to null, empty fields to 0
  392.             foreach (array($dataOrder["b"]$dataOrder["s"]6as $i{
  393.                 if (strpos($data[$i]"--"!== false
  394.                     $data[$i"null"
  395.                 elseif ($data[$i== ""
  396.                     $data[$i= 0; 
  397.                 }
  398.             }
  399.  
  400.             // strings: quote
  401.             foreach (array($dataOrder["i"]345as $i{
  402.                 $data[$i$db->quote($data[$i]);
  403.             }
  404.  
  405.             // insert data
  406.             $insert  "INSERT INTO ".$tableName." VALUES(".($line $error).",";
  407.             $insert .= $data[$dataOrder["b"]].",".$data[$dataOrder["s"]].",";
  408.             $insert .= $data[$dataOrder["i"]].",".$data[3].",".$data[4].",";
  409.             $insert .= $data[5].",".$data[6].",".round($latitude4).",";
  410.             $insert .= round($longitude4).",".$elevation.",".round($x4).",";
  411.             $insert .= round($y4).",".round($z4).")";
  412.  
  413.             $result $db->query($insert);
  414.             if (DB::isError($result)) {
  415.                 echo "\tLine ".($line + 1).": ".$insert."\n";
  416.                 echo $result->getMessage()."\n";
  417.                 $error++;
  418.             elseif($verbose > 2{
  419.                 echo $insert."\n";
  420.             }
  421.         }
  422.         $line++;
  423.     }
  424.     // commit and close
  425.     $db->disconnect();
  426.     if ($verbose > 0 || $error > 0{
  427.         echo "Services_Weather: ".($line $error)." ".$tableName." added ";
  428.         echo "to database '".$dbName."' (".$error." error(s)).\n";
  429.     }
  430. }
  431. $close($fp);
  432. ?>

Documentation generated on Mon, 11 Mar 2019 14:17:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.