Thread: CVS: phpweather phpweather.php,1.35,1.36 test2.php,1.2,1.3
Brought to you by:
iridium
From: Etienne T. <eti...@us...> - 2003-09-20 22:19:07
|
Update of /cvsroot/phpweather/phpweather In directory sc8-pr-cvs1:/tmp/cvs-serv28865 Modified Files: phpweather.php test2.php Log Message: Mods for TAF. Use timestamps in the taf periods (YYYYMMDDhhmmss). Include PROB text in the hourly report. Descriptive data for periods is in a sub-array 'desc'. Added a function phpweather::get_taf_by_hour for a subset of the hourly report. Index: phpweather.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/phpweather.php,v retrieving revision 1.35 retrieving revision 1.36 diff -u -3 -r1.35 -r1.36 --- phpweather.php 13 Sep 2003 02:38:49 -0000 1.35 +++ phpweather.php 20 Sep 2003 22:18:47 -0000 1.36 @@ -697,16 +697,36 @@ * retrieve the TAF, so it is not necessary to connect to the database * before you call this function. * + * It should be noted that 3 arrays are filled. Only one should be kept, we'll see that later. + * periods1 contains the individual data for each period + * periods2 contains accumulated data for each period, based on pervious periods + * periods 3 contains hour-by-hour data, interpolated for BECMG type periods + * * @return array The decoded TAF. * @see $decoded_taf * @access public */ - //from http://www.nws.noaa.gov/mdl/icwf/avnfps/editor.html#TAFDecoder - //need to add NIL( AMD) (augmented data) - //and time consistency checks, Consistency Checks Within a Group - + /* + * taken from http://www.nws.noaa.gov/mdl/icwf/avnfps/editor.html#TAFDecoder + * need to add NIL( AMD) (augmented data) + * and time consistency checks, Consistency Checks Within a Group + */ + + /* + * taken from http://www.srh.noaa.gov/ftproot/MSD/html/note7.html + * need to add icing, turbulence and temp forecast + * + * FM change rapidly within less than one hour ->from begin to end + * BECMG change slowly, normally within 2 hours, never exceed 4 hours -> interpolate each 30 min + * TEMPO temporary, less than half of the period ->from 1/4 to 3/4 interpolating + * PROB chance of occurence for the whole period ->from begin to end + */ + /* todo: + * include year,month,day in the 'time_from','time_to','time_set' values for easier searching and processing + */ + function decode_taf() { /* initialization */ @@ -761,11 +781,15 @@ } - $decoded_periods = array(); - /* for each period, parse the data */ if(count($periods)>0) { + + $decoded_periods = array(); + $decoded_periods2 = array(); + $decoded_periods3 = array(); + $full_period = $full_period2 = false; $last_fm_becmg = false; + for($j=0;$j<count($periods);$j++) { $tmp_period = $periods[$j]; $data_period = $tmp_period; @@ -778,6 +802,7 @@ $first_i = 1; $time_from = $time_to = false; $type = $prob = false; + $set_time_to = false; if ( (substr($parts[0],0,2)=='FM') ) { $type = 'FM'; $time_from = substr($parts[0],2,4); @@ -786,7 +811,8 @@ $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; +// $decoded_periods[$last_fm_becmg]['time_to'] = $time_from; + $set_time_to = $last_fm_becmg; } $last_fm_becmg = $j; } @@ -798,7 +824,8 @@ $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; +// $decoded_periods[$last_fm_becmg]['time_to'] = $time_from; + $set_time_to = $last_fm_becmg; } $last_fm_becmg = $j; } @@ -829,6 +856,17 @@ $first_i = $num_parts; } + /* 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']); + + /* set the end time if the previous FM or BECMG to the start time of this one */ + if ($set_time_to!==false) { + $decoded_periods[$set_time_to]['time_to'] = $time_from; + } + + /* put the basic info in the decoded_period */ $decoded_period['data'] = $data_period; $decoded_period['type'] = $type; $decoded_period['time_from'] = $time_from; @@ -1024,54 +1062,36 @@ /* set the last FM or BECMG end time */ if($last_fm_becmg!==false && $decoded_periods[$last_fm_becmg]['time_to']===false) { - $decoded_periods[$last_fm_becmg]['time_to'] = $decoded_periods[0]['time_to']; + $decoded_periods[$last_fm_becmg]['time_to'] = $decoded_periods[0]['time_to']; } $decoded_taf['periods1'] = $decoded_periods; $decoded_taf['remarks'] = trim($decoded_taf['remarks']); } - //ET tmp pass each period and fill in the missing pieces - /* - * taken from http://www.srh.noaa.gov/ftproot/MSD/html/note7.html - * includes icing, turbulence and temp forecast - * - * FM change rapidly within less than one hour ->from begin to end - * BECMG change slowly, normally within 2 hours, never exceed 4 hours -> interpolate each 30 min - * TEMPO temporary, less than half of the period ->from 1/4 to 3/4 interpolating - * PROB chance of occurence for the whole period ->from begin to end - */ - - $decoded_periods2 = array(); - $full_period = $full_period2 = false; - - /* for each period, parse the data */ + /* We pass each 'periods1' and set the properties for each based on previous properties. + * We also construct the hour-by-hour report. */ if(count($decoded_taf['periods1'])>0) { $full_period = $decoded_taf['periods1'][0]; for($j=0;$j<count($decoded_taf['periods1']);$j++) { $tmp_period = $decoded_taf['periods1'][$j]; $tmp_period2 = false; - /* merge the permanent period with this one */ + /* merge the full period with this one */ if($tmp_period['type'] == "COMPLETE") $tmp_period2 = $full_period; else $tmp_period2 = $this->merge_period($tmp_period,$full_period); /* fill $decoded_periods3 with data from each hour */ - $time_from = $tmp_period2['time_from']; - $tms_from = gmmktime(substr($time_from,0,2),(substr($time_from,2,2))); - $time_set = $tmp_period2['time_set']; - $tms_set = gmmktime(substr($time_set,0,2),(substr($time_set,2,2))); - $time_to = $tmp_period2['time_to']; - $tms_to = gmmktime(substr($time_to,0,2),(substr($time_to,2,2))); - if($time_to <= $time_from ) $tms_to += 24*60*60; - if($time_set < $time_from ) $tms_set += 24*60*60; + $tms_from = tms_date2unix($tmp_period2['time_from']); + $tms_set = tms_date2unix($tmp_period2['time_set']); + $tms_to = tms_date2unix($tmp_period2['time_to']); $duration = ($tms_to - $tms_from) / 60.0 / 60.0; for($k=0;$k<ceil($duration);$k++) { $tmp_period3 = $tmp_period2; $tmp_tms_from = $tms_from + $k * 60*60; - $tmp_tms_to = $tmp_tms_from + 60*60;//see if fraction... - $tmp_time_from = gmdate("Hi",$tmp_tms_from); - $tmp_time_to = gmdate("Hi",$tmp_tms_to); + $tmp_tms_to = $tmp_tms_from + 60*60;//MUST see if fraction... + $tmp_time_from = tms_unix2date($tmp_tms_from); + $tmp_time_to = tms_unix2date($tmp_tms_to); $tmp_period3['time_from'] = $tmp_time_from; $tmp_period3['time_to'] = $tmp_time_to; unset($tmp_period3['time_set']); @@ -1079,9 +1099,15 @@ $tmp_period3['interpolate']='yes'; $this->interpolate_period($tmp_period3,$full_period2,$tms_from,$tms_set,$tmp_tms_from,$tmp_tms_to); } - /* for now do not include PROB in the hour-by-hour */ - if ($tmp_period['type'] != "PROB") { - $decoded_periods3[gmdate("H",$tmp_tms_from)] = $tmp_period3; + /* add the forecast fot this hour */ + if ($tmp_period['type'] != "PROB") { + $decoded_periods3[gmdate("H",$tmp_tms_from)] = $tmp_period3; + } + else { + /* add a 'PROB' element to the [normal] forecast for this hour */ + /* perhaps the PROB period should contain only the data that has changed + * and not the cumulative data, for simplicity */ + $decoded_periods3[gmdate("H",$tmp_tms_from)]['PROB'] = $tmp_period3; } } @@ -1103,7 +1129,25 @@ $this->decoded_taf = $decoded_taf; return $decoded_taf; - + } + + /** + * Gets the hour-by-hour decoded TAF. + * + * @return array an array of hour-by hour decoded TAF. + * @access public + */ + function get_taf_at_time($time_from=false,$time_to=false) { + if($this->decoded_taf===false) return false; + if($time_from==false&&$time_to===false) return $this->decoded_taf['periods3']; + $tmp_taf = array(); + while(list($i,$period) = each($this->decoded_taf['periods3'])) { + if( $period['time_from']<$time_to && $period['time_to']>$time_from) { + $tmp_taf[$i] = $period; + } + } + if(count($tmp_taf)==0) return false; + else return $tmp_taf; } function merge_period(&$period1,&$period2) { @@ -1121,13 +1165,6 @@ return $period; } - function add_time($str_time,$add_hour,$add_min=0) { - $tms = gmmktime(substr($str_time,0,2),(substr($str_time,2,2))); - $tms += $add_hour*60*60 + $add_min*60; - return gmdate("Hi",$tms); - } - - function interpolate_period(&$period,&$prev_period,$from,$set,$period_from,$period_to) { //only interpolate wind, not: -visibility -weather -clouds -ws $wind = $period['desc']['wind']; @@ -1160,6 +1197,42 @@ $period['desc']['wind']['miles_per_hour']); } + +} + +/* put these and other functions somewhere else? */ + +function hm2YMDhm($hm,$time_use,$next_day=false) { + if($hm===false) return false; + + /* prepare the data */ + $Y = gmdate("Y"); + $M = intval(gmdate("n")); + $d_time_use = intval(substr($time_use,0,2)); + $d = $d_time_use; + $h_time_use = intval(substr($time_use,2,2)); + $h = intval(substr($hm,0,2)); + $m = intval(substr($hm,2,2)); + + /* report is for next month, add a month */ + if($d > gmdate('j')) $M++; + $tms = gmmktime($h,$m,0,$M,$d,$Y); + /* report is for next day, add a day */ + if ($h<$h_time_use) $tms += 60*60*24; + else if ($next_day===true && $h==$h_time_use) $tms += 60*60*24; + + $YMDhm = gmdate("YmdHi"."00",$tms); + + return $YMDhm; +} + +function tms_date2unix($tmp_tms) { + return gmmktime(substr($tmp_tms,8,2),substr($tmp_tms,10,2),substr($tmp_tms,12,2), + substr($tmp_tms,4,2),substr($tmp_tms,6,2),substr($tmp_tms,0,4)); +} + +function tms_unix2date($tmp_tms) { + return gmdate("YmdHis",$tmp_tms); } ?> Index: test2.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/test2.php,v retrieving revision 1.2 retrieving revision 1.3 diff -u -3 -r1.2 -r1.3 --- test2.php 13 Sep 2003 02:38:49 -0000 1.2 +++ test2.php 20 Sep 2003 22:18:47 -0000 1.3 @@ -2,6 +2,9 @@ /* Call with a icao parameter i.e. test2.php?icao=KSFO + +You can add time_from and time_to to get only parts of the taf +i.e. test2.php?icao=CYUL&time_from=20030920220000&time_to=20030920230000 */ error_reporting(E_ALL); @@ -19,32 +22,6 @@ if (!empty($icao)) { - /* - require(PHPWEATHER_BASE_DIR . "/output/pw_text_$language.php"); - require(PHPWEATHER_BASE_DIR . "/output/pw_images.php"); - - $type = 'pw_text_' . $language; - $text = new $type($weather); - - echo "<blockquote>\n" . $text->print_pretty() . "</blockquote>\n"; - - $icons = new pw_images($weather); - echo '<p><img src="'.$icons->get_sky_image().'" /> '; - echo '<img src="'.$icons->get_winddir_image().'" /> '; - echo '<img src="'.$icons->get_temp_image().'" /></p>'; - */ - - /* - if (is_array($icao)) { - foreach ($icao as $i) { - $weather->set_icao($i); - echo "METAR for $i : ".$weather->get_metar()."<br>\n"; - echo "<br>"; - } - } - else { - */ - $weather->set_icao($icao); $language = 'en'; @@ -55,22 +32,21 @@ require(PHPWEATHER_BASE_DIR . "/output/pw_images.php"); $icons = new pw_images($weather); + $weather->decode_taf(); + + echo "TAF for $icao : ".$weather->get_taf()."<br>\n"; + echo "<br>Print TAF:<br><br>"; + if(empty($time_from) || empty($time_to) || $time_from=="" || $time_to=="") + $text->print_taf(); + else $text->print_taf($time_from,$time_to); + echo "<br>\n"; + /* echo "METAR for $icao : ".$weather->get_metar()."<br>\n"; echo "<br>Decoded METAR:<pre>"; print_r( $weather->decode_metar() ); echo "</pre><br>\n"; */ - - echo "TAF for $icao : ".$weather->get_taf()."<br>\n"; - echo "<br>Print TAF:<br><br>"; - print_r( $text->print_taf() ); - echo "<br>\n"; - echo "<br>Decoded TAF:<pre>"; - print_r( $weather->decode_taf() ); - echo "</pre><br>\n"; - - /* } */ } |