Update of /cvsroot/phpweather/phpweather/db
In directory usw-pr-cvs1:/tmp/cvs-serv4090
Modified Files:
pw_db_common.php
Log Message:
Hehe - this is kind of cool :-) We can now update the databases with
fresh data from cycle files. These files contain all the METARs
received in the last 'cycle' at the NWS.
Index: pw_db_common.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/db/pw_db_common.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- pw_db_common.php 20 Mar 2002 19:26:50 -0000 1.2
+++ pw_db_common.php 25 Mar 2002 19:35:55 -0000 1.3
@@ -57,6 +57,58 @@
$this->result_id = false;
}
+
+ /**
+ * Updates the database with new data from a cycle file.
+ *
+ * This method is used when a new cycle-file has received. The idea
+ * is, that it is run periodically to update the database with fresh
+ * data. This should be done from a different script than the main
+ * script (the script the uses see), as this can take some time.
+ *
+ * @param string The filename of the new cycle file. The filename
+ * should be an absolute filename.
+ *
+ * Or would it be better, if it was relative to PHPWEATHER_BASE_DIR?
+ *
+ * @see insert_metar(), update_metar()
+ */
+ function update_all_metars($file) {
+
+ $fp = fopen($file, 'r');
+
+ while (!feof($fp)) {
+
+ $line = trim(fgets($fp, 256));
+ if ($line == '') {
+ continue;
+ }
+
+ /* We have now moved past one or more blank lines, next is the
+ * date: */
+ $date = explode(':', strtr($line, '/ ', '::'));
+ $timestamp = gmmktime($date[3], $date[4], 0,
+ $date[1], $date[2], $date[0]);
+
+ /* The next lines are the METAR: */
+ $metar = trim(fgets($fp, 256));
+ while (!feof($fp) && ($line = trim(fgets($fp, 256))) != '') {
+ $metar .= ' ' . $line;
+ }
+
+ /* The ICAO is always the first four characters in the METAR: */
+ $icao = substr($metar, 0, 4);
+
+ if ($this->get_metar($icao)) {
+ $this->update_metar($icao, $metar, $timestamp);
+ } else {
+ $this->insert_metar($icao, $metar, $timestamp);
+ }
+
+ } /* while (!feof($fp)) */
+
+ fclose($fp);
+ }
}
?>
|