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

Source for file convert.php

Documentation is available at convert.php

  1. <?php
  2. /**
  3.  * One-time HRDI data conversion script.
  4.  * This conversion script converts the data imported from 4D to a format used
  5.  * by the HRDIODB software. It does the following conversions:
  6.  * - Changes the relationship between HRDI_Days and HRDI_Modes
  7.  * - Changes all instances of "Middle" flight direction to "Maneuver"
  8.  * - Corrects any incorrect azimuth (cause by human entry error)
  9.  * - Parses azimuths to store them in four numerical fields instead of one
  10.  *   string field
  11.  * @package HRDIODB
  12.  */
  13.  
  14. /**
  15.  * This file is part of HRDIODB.
  16.  *
  17.  * HRDIODB is free software; you can redistribute it and/or modify
  18.  * it under the terms of the GNU General Public License as published by
  19.  * the Free Software Foundation; either version 2 of the License, or
  20.  * (at your option) any later version.
  21.  *
  22.  * HRDIODB is distributed in the hope that it will be useful,
  23.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25.  * GNU General Public License for more details.
  26.  *
  27.  * You should have received a copy of the GNU General Public License
  28.  * along with HRDIODB; if not, write to the Free Software
  29.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30.  */
  31.  
  32. /** Configuration file */
  33. require_once("conf.php");
  34. /** HRDI database class */
  35. require_once("hrdi_db_class.php");
  36.  
  37. // Menu of conversions
  38. $conv array (=> "relationship between HRDI_Days and HRDI_Modes",
  39.                => "Flight Direction from \"Middle\" to \"Maneuver\"",
  40.                => "incorrect Azimuths",
  41.                => "String Azimuths to 4 separate numerical fields"
  42.                => "all of the above, in order");
  43.  
  44. // HRDI database instance
  45. $hdb new hrdi_db ($dsn);
  46.  
  47. // User entered value
  48. $type intval($argv[1]);
  49.  
  50. // Figure out which conversion to make
  51. switch ($type{
  52.   case 1:
  53.     one();
  54.     break;
  55.   case 2:
  56.     two();
  57.     break;
  58.   case 3:
  59.     three();
  60.     break;
  61.   case 4:
  62.     four();
  63.     break;
  64.   case 5:
  65.     one();
  66.     two();
  67.     three();
  68.     four();
  69.     break;
  70.   default:
  71.     print "Please specify the number of the conversion you want to run\n\n";
  72.  
  73.     for($i 1$i <= count($conv)$i++{
  74.       print "\t$iConvert {$conv[$i]}\n";
  75.     }
  76.  
  77.     print "\n";
  78. }
  79.  
  80. /**
  81.  * First conversion.
  82.  * Performs the first conversion, namely changes the relationship between
  83.  * HRDI_Days and HRDI_Modes.
  84.  */
  85. function one ({
  86.   global $conv$hdb;
  87.  
  88.   print "Converting {$conv[1]}\n";
  89.  
  90.   $sql "SELECT Record_Num,UARS_Day
  91.           FROM HRDI_Days";
  92.  
  93.   $days $hdb->get_list($sql);
  94.  
  95.   $total count($days);
  96.   print "$total days to convert\n";
  97.  
  98.   for ($i 0$i count($days)$i++{
  99.     print ($i 1)." of $total\n";
  100.  
  101.     $sql "UPDATE HRDI_Modes
  102.             SET UARS_Day='{$days[$i]['UARS_Day']}'
  103.             WHERE Days_Relate_Num='{$days[$i]['Record_Num']}'";
  104.  
  105.     $hdb->db_query($sql);
  106.   }
  107.  
  108.   print "Done\n";
  109. }
  110.  
  111. /**
  112.  * Second conversion.
  113.  * Performs the second conversion, namely changes all instances of "Middle"
  114.  * flight direction to "Maneuver".
  115.  */
  116. function two ({
  117.   global $conv$hdb;
  118.  
  119.   print "Converting {$conv[2]}\n";
  120.  
  121.   $sql "UPDATE HRDI_Days
  122.           SET Flight_Direction='Maneuver'
  123.           WHERE Flight_Direction='Middle'";
  124.  
  125.   $hdb->db_query($sql);
  126.  
  127.   print "Updated ".$hdb->db->affectedRows()." days\n";
  128. }
  129.  
  130. /**
  131.  * Third conversion.
  132.  * Performs the third conversion, namely corrects any incorrect azimuth.
  133.  */
  134. function three ({
  135.   global $conv$hdb;
  136.  
  137.   print "Converting {$conv[3]}\n";
  138.  
  139.   $sql "UPDATE HRDI_Modes
  140.           SET Azimuth='45,135'
  141.           WHERE Azimuth='45135'";
  142.  
  143.   $hdb->db_query($sql);
  144.  
  145.   print "Converted ".$hdb->db->affectedRows()." '45135' Azimuths\n";
  146.  
  147.   $sql "UPDATE HRDI_Modes
  148.           SET Azimuth='+-45,+-135'
  149.           WHERE Azimuth='±45,±135'";
  150.  
  151.   $hdb->db_query($sql);
  152.  
  153.   print "Converted ".$hdb->db->affectedRows()." '±45,±135' Azimuths\n";
  154.  
  155.   $sql "UPDATE HRDI_Modes
  156.           SET Azimuth='+45,+135'
  157.           WHERE Azimuth='+45+,135'";
  158.  
  159.   $hdb->db_query($sql);
  160.  
  161.   print "Converted ".$hdb->db->affectedRows()." '+45+,135' Azimuths\n";
  162. }
  163.  
  164. /**
  165.  * Fourth conversion.
  166.  * Performs the fourth conversion, namely parses azimuths to store them in four
  167.  * numerical fields instead of one string field.
  168.  */
  169. function four ({
  170.   global $conv$hdb;
  171.  
  172.   $sql "SELECT ID,Azimuth
  173.           FROM HRDI_Modes";
  174.  
  175.   $modes $hdb->get_list($sql);
  176.  
  177.   foreach ($modes as $mode{
  178.     if (preg_match("/(([+-]*)(\d+))(,\s*([+-]*)(\d+))*/"$mode['Azimuth']$match)) {
  179.       $azimuths array();
  180.  
  181.       switch ($match[2]{
  182.         case '+-':
  183.         case '=+':
  184.           $azimuths[$match[3];
  185.         case '-':
  186.           $azimuths['-'.$match[3];
  187.           break;
  188.         case '+':
  189.         case '':
  190.           $azimuths[$match[3];
  191.           break;
  192.         default;
  193.           print "ErrorUnhandled Azimuth (1) for ID {$mode['ID']}\n";
  194.       }
  195.  
  196.       if (strlen(@$match[4])) {
  197.         switch ($match[5]{
  198.           case '+-':
  199.           case '=+':
  200.             $azimuths[$match[6];
  201.           case '-':
  202.             $azimuths['-'.$match[6];
  203.             break;
  204.           case '+':
  205.           case '':
  206.             $azimuths[$match[6];
  207.             break;
  208.           default;
  209.             print "ErrorUnhandled Azimuth (2) for ID {$mode['ID']}\n";
  210.         }
  211.       }
  212.  
  213.       $sql "UPDATE HRDI_Modes
  214.               SET Azimuth1='{$azimuths[0]}'";
  215.  
  216.       if (@$azimuths[1]{
  217.         $sql .= ",Azimuth2='{$azimuths[1]}'";
  218.       }
  219.  
  220.       if (@$azimuths[2]{
  221.         $sql .= ",Azimuth3='{$azimuths[2]}'";
  222.       }
  223.  
  224.       if (@$azimuths[3]{
  225.         $sql .= ",Azimuth4='{$azimuths[3]}'";
  226.       }
  227.  
  228.       $sql .= " WHERE ID='{$mode['ID']}'";
  229.  
  230.       print "Updating Azimuths for mode ID {$mode['ID']}\n";
  231.  
  232.       $hdb->db_query($sql);
  233.     else if (strlen($mode['Azimuth'])) {
  234.       print "ErrorCould not parse azimuth {$mode['Azimuth']} for mode ID {$mode['ID']}\n";
  235.     }
  236.   }
  237. }
  238.  
  239. ?>

Documentation generated on Tue, 23 Jan 2007 22:57:44 -0500 by phpDocumentor 1.3.0RC6