home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / buildMetarDB.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  15.5 KB  |  427 lines

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