CVS: phpweather data_retrieval.php,1.34,1.35 phpweather.php,1.36,1.37
Brought to you by:
iridium
From: Etienne T. <eti...@us...> - 2003-10-02 22:54:53
|
Update of /cvsroot/phpweather/phpweather In directory sc8-pr-cvs1:/tmp/cvs-serv19332 Modified Files: data_retrieval.php phpweather.php Log Message: Added retrieval of archived metars. The current METAR is still in metar and decoded_metar, time is in YYYYDDMMhhmmss format. There are additionnal arrays metar_arch decoded_metar_arch which contain all the METARs in the period set by data_retrieval::set_times(). Modified db/pw_adodb.php and db/pw_mysql.php. Index: data_retrieval.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/data_retrieval.php,v retrieving revision 1.34 retrieving revision 1.35 diff -u -3 -r1.34 -r1.35 --- data_retrieval.php 30 Sep 2003 19:47:59 -0000 1.34 +++ data_retrieval.php 2 Oct 2003 22:54:46 -0000 1.35 @@ -24,6 +24,8 @@ * @see set_metar() */ var $metar; + var $metar_time; + var $metar_arch; /** * The TAF is stored here. @@ -46,6 +48,9 @@ */ var $icao_data; + var $time_from; + var $time_to; + /** * Constructor * @@ -61,6 +66,13 @@ /* Then we set the station. */ $this->set_icao($this->properties['icao']); + $this->metar = false; + $this->metar_time = false; + $this->metar_arch = false; + $this->fd = false; + $this->time_from = false; + $this->time_to = false; + } /** @@ -93,12 +105,18 @@ $this->properties['icao'] = strtoupper($new_icao); $this->icao_data = false; $this->metar = false; + $this->metar_time = false; $this->decoded_metar = false; + $this->metar_arch = false; $this->taf = false; $this->decoded_taf = false; } } + function set_times($time_from=false, $time_to=false) { + $this->time_from = $time_from; + $this->time_to = $time_to; + } /** * Retrieves a raw METAR, either from the web or from a database. @@ -110,6 +128,7 @@ * @return string The raw METAR. */ function get_metar() { + if (empty($this->metar)) { /* The METAR is not set - we try to load it */ $this->debug('The METAR is not set, I\'ll try to find the METAR in the database.'); @@ -255,46 +274,69 @@ * @return string The raw METAR. */ function get_metar_from_db() { + if (!$this->db->connect()) { return false; } + + $tmp_metar = false; + $tmp_metar_time = false; if ($data = $this->db->get_metar($this->get_icao())) { /* found station */ $this->debug('get_metar_from_db(): Found the METAR in the database'); - list($metar, $timestamp) = $data; - + list($metar, $timestamp, $time) = end($data); + echo "metar: $metar timestamp:$timestamp time:$time<br>"; + reset($data); + /* We set the METAR right away, and then count on * get_metar_from_web() to set it to something else, if * necessary. */ + $tmp_metar = $metar; $this->metar = $metar; + $this->metar_time = $time; + if ($this->properties['always_use_db'] || $timestamp > time() - $this->properties['cache_timeout']) { - - /* We have asked explicit for a cached METAR, or the METAR is - * still fresh. Either way - we return the METAR we found in + /* We have asked explicit for a cached METAR, or the METAR is + * still fresh.Either way - we return the METAR we found in * the database. */ $this->debug('get_metar_from_db(): Using previously cached METAR for <code>' . $this->get_location() . '</code>. The METAR expires in ' . ($timestamp + $this->properties['cache_timeout'] - time()) . ' seconds.'); - return $metar; + $tmp_metar = $metar; } else { - /* The METAR is too old, so we fetch new */ + /* The METAR is too old, so we fetch new */ $this->debug('get_metar_from_db(): The METAR for <code>' . - $this->get_location() . '</code> was ' . - (time() - $this->properties['cache_timeout'] - $timestamp) . - ' seconds too old.'); - return $this->get_metar_from_web(false); + $this->get_location() . '</code> was ' . + (time() - $this->properties['cache_timeout'] - $timestamp) . + ' seconds too old.'); + $tmp_metar = $this->get_metar_from_web(false); } + } else { /* We need to get a new METAR from the web. */ $this->debug('get_metar_from_db(): New station <code>' . - $this->get_location() . '</code> - fetching a new METAR.'); - return $this->get_metar_from_web(true); + $this->get_location() . '</code> - fetching a new METAR.'); + $tmp_metar = $this->get_metar_from_web(true); + } + + /* Now we can get the archive METARs. */ + $this->metar_arch = false; + if ($this->time_from!==false) { + if ($data = $this->db->get_metar($this->get_icao(),$this->time_from,$this->time_to)) { + for($i=0;$i<count($data);$i++) { + $this->metar_arch[$i] = array('metar'=>$data[$i][0], + 'time'=>$data[$i][2]); + } + } } + + return $tmp_metar; } + /** * Fetches a METAR from the Internet. @@ -373,22 +415,23 @@ $timestamp = time() - $this->properties['cache_timeout'] + 600; } + /* side effect */ $metar_time = $date[0].$date[1].$date[2].$date[3].$date[4]."00"; - + echo "metar_time: $metar_time<br>"; + /* We then cache the METAR in our database */ if ($new_station) { $this->debug('get_metar_from_web(): Inserting new METAR for <code>' . $this->get_location() . '</code>'); -// $this->db->insert_metar($icao, $metar, $timestamp); $this->db->insert_metar($icao, $metar, $timestamp, $metar_time); } else { $this->debug('get_metar_from_web(): Updating METAR for <code>' . $this->get_location() . '</code>'); -// $this->db->update_metar($icao, $metar, $timestamp); $this->db->update_metar($icao, $metar, $timestamp, $metar_time); } /* We update and return the METAR */ $this->metar = $metar; + $this->metar_time = $metar_time; return $metar; } @@ -615,12 +658,10 @@ if ($new_station) { $this->debug('get_taf_from_web(): Inserting new TAF for <code>' . $this->get_location() . '</code>'); -// $this->db->insert_taf($icao, $taf, $timestamp); $this->db->insert_taf($icao, $taf, $timestamp, $taf_time); } else { $this->debug('get_taf_from_web(): Updating TAF for <code>' . $this->get_location() . '</code>'); -// $this->db->update_taf($icao, $taf, $timestamp); $this->db->update_taf($icao, $taf, $timestamp, $taf_time); } Index: phpweather.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/phpweather.php,v retrieving revision 1.36 retrieving revision 1.37 diff -u -3 -r1.36 -r1.37 --- phpweather.php 20 Sep 2003 22:18:47 -0000 1.36 +++ phpweather.php 2 Oct 2003 22:54:46 -0000 1.37 @@ -41,6 +41,7 @@ * @var array */ var $decoded_metar; + var $decoded_metar_arch; /** * The decoded TAF is stored here. @@ -60,6 +61,11 @@ * parent constructor. */ $this->data_retrieval($input); + + $this->decoded_metar = false; + $this->decoded_metar_arch = false; + $this->decoded_taf = false; + } @@ -160,26 +166,44 @@ * @see $decoded_metar * @access public */ - function decode_metar() { + function decode_metar($index=false) { /* initialization */ $temp_visibility_miles = ''; - $decoded_metar['remarks'] = ''; - - $decoded_metar['metar'] = $this->get_metar(); $decoded_metar['location'] = $this->get_location(); - - $parts = explode(' ', $this->metar); + + /* Make sure we got the metar */ + $tmp_metar = $this->get_metar(); + + /* Setup other variables */ + if ($index===false) { /* We are getting the current METAR */ + $decoded_metar['metar'] = $tmp_metar; + $decoded_metar['time'] = $this->metar_time; + } + else { + if ($this->metar_arch===false) { /* error */ + return false; + } + $tmp_metar = $this->metar_arch[$index]['metar']; + $decoded_metar['metar'] = $tmp_metar; + $decoded_metar['time'] = $this->metar_arch[$index]['time']; + } + + /* We parse the METAR */ + $parts = explode(' ', $tmp_metar); $num_parts = count($parts); + for ($i = 0; $i < $num_parts; $i++) { $part = $parts[$i]; if (ereg('RMK|TEMPO|BECMG|INTER', $part)) { /* The rest of the METAR is either a remark or temporary - * information. We skip the rest of the METAR. + * information. We keep the remark. */ - $decoded_metar['remarks'] .= ' ' . $part; - break; + for($j=$i;$j<$num_parts; $j++) + $decoded_metar['remarks'] .= ' ' . $parts[$j]; + $decoded_metar['remarks'] = trim($decoded_metar['remarks']); + break; } elseif ($part == 'METAR') { /* * Type of Report: METAR @@ -210,17 +234,24 @@ * $this->properties['offset'] to be +1 in your defaults.php * file. */ - if ($regs[1] > gmdate('j')) { - /* The day is greather that the current day of month => the - * report is from last month. - */ - $month = gmdate('n') - 1; - } else { - $month = gmdate('n'); - } - $decoded_metar['time'] = - gmmktime($regs[2] + $this->properties['offset'], - $regs[3], 0, $month, $regs[1], gmdate('Y')); + + /* We get the report time from elsewhere */ +// $decoded_metar['time'] = $this->metar_time; + +// if ($regs[1] > gmdate('j')) { +// /* The day is greather that the current day of month => the +// * report is from last month. +// */ +// $month = gmdate('n') - 1; +// } else { +// $month = gmdate('n'); +// } +// /* is this really useful? let's put this in a readable timestamp... */ +// $decoded_metar['time'] = +// gmmktime($regs[2] + $this->properties['offset'], +// $regs[3], 0, $month, $regs[1], gmdate('Y')); + + } elseif (ereg('(AUTO|COR|RTD|CC[A-Z]|RR[A-Z])', $part, $regs)) { /* @@ -685,11 +716,31 @@ /* Finally we store our decoded METAR in $this->decoded_metar so * that other methods can use it. */ - $this->decoded_metar = $decoded_metar; + if ($index===false) $this->decoded_metar = $decoded_metar; + else $this->decoded_metar_arch[$index] = $decoded_metar; + return $decoded_metar; } /** + * Decodes all METARs + * + * @return + * @access public + */ + + function decode_all_metars() { + + /* Decode the current METAR */ + $this->decode_metar(); + + /* Decode the archived METARs */ + for($i=0; $i<count($this->metar_arch); $i++) { + $this->decode_metar($i); + } + } + + /** * Decodes a raw TAF. * * This function loops over the various parts of the raw TAF, and @@ -748,11 +799,12 @@ $current_period = 0; $periods = array(); $periods[0] = 'COMPLETE'; - $remarks_index = $num_parts - 1; $decoded_taf['icao'] = $parts[0]; - $decoded_taf['time_emit'] = $parts[1]; - $decoded_taf['time_use'] = $parts[2]; + $tmp_time_use = $parts[2]; + $decoded_taf['time_emit'] = hm2YMDhm(substr($parts[1],2,4),$tmp_time_use); + $decoded_taf['time_use_from'] = hm2YMDhm(substr($parts[2],2,2)."00",$tmp_time_use); + $decoded_taf['time_use_to'] = hm2YMDhm(substr($parts[2],4,2)."00",$tmp_time_use); $periods[0] .= ' '.$parts[2]; /* first pass to get remarks and periods */ @@ -765,10 +817,10 @@ /* The rest of the TAF is either a remark or temporary * information. We keep the remark. */ - $remarks_index = $i; for($j=$i;$j<$num_parts; $j++) $decoded_taf['remarks'] .= ' ' . $parts[$j]; - break; + $decoded_taf['remarks'] = trim($decoded_taf['remarks']); + break; } else if ( (substr($part,0,2)=='FM') || $part=='BECMG' || ($part=='TEMPO') || (substr($part,0,4)=='PROB') ) { @@ -811,7 +863,6 @@ $first_i = 1; /* set the end time if the previous FM or BECMG to the start time of this one */ if($last_fm_becmg!==false && $decoded_periods[$last_fm_becmg]['time_to']===false) { -// $decoded_periods[$last_fm_becmg]['time_to'] = $time_from; $set_time_to = $last_fm_becmg; } $last_fm_becmg = $j; @@ -824,7 +875,6 @@ $first_i = 2; /* set the end time if the previous FM or BECMG to the start time of this one */ if($last_fm_becmg!==false && $decoded_periods[$last_fm_becmg]['time_to']===false) { -// $decoded_periods[$last_fm_becmg]['time_to'] = $time_from; $set_time_to = $last_fm_becmg; } $last_fm_becmg = $j; @@ -857,9 +907,9 @@ } /* make timestamps */ - $time_from = hm2YMDhm($time_from,$decoded_taf['time_use']); - $time_to = hm2YMDhm($time_to,$decoded_taf['time_use'],true); - $time_set = hm2YMDhm($time_set,$decoded_taf['time_use']); + $time_from = hm2YMDhm($time_from,$tmp_time_use); + $time_to = hm2YMDhm($time_to,$tmp_time_use,true); + $time_set = hm2YMDhm($time_set,$tmp_time_use); /* set the end time if the previous FM or BECMG to the start time of this one */ if ($set_time_to!==false) { @@ -1066,7 +1116,6 @@ } $decoded_taf['periods1'] = $decoded_periods; - $decoded_taf['remarks'] = trim($decoded_taf['remarks']); } /* We pass each 'periods1' and set the properties for each based on previous properties. |