Thread: Accessing elements directly
Brought to you by:
iridium
From: Tom G. <Tom...@LA...> - 2002-06-20 20:11:29
Attachments:
winmail.dat
|
I currently have v 1.6x setup at: http://codeworks.org/phpweather/dtn.php displaying a table with 12 airports from our local area (within a couple of hundred miles). It currently gives an error if the cloud array is empty. I've decided to make it work with the new version... Here's my test code for accessing array elements directly. I used as a base a reply to a question on this list from back in March. Apparently the get_metar_value function doesn't exist anymore. Has it been replaced? Or, do I need to include some other files. I've searched through most of the files, but haven't been able to find it. All help is appreciated! Thanks! <?php error_reporting(E_ALL); require('./phpweather.php'); require('./pw_utilities.php'); $weather = new phpweather(array()); $weather->set_icao('KDTN'); $language = "en"; include(PHPWEATHER_BASE_DIR . "/output/pw_text_$language.php"); $type = 'pw_text_' . $language; $text = new $type($weather, array()); $text->print_pretty(); $temp_f = get_metar_value('temperature','temp_f'); echo "$temp_f °F"; echo '<pre>'; print_r($weather->decode_metar()); echo '</pre>'; ?> Tom Grant Louisiana CodeWORKS, Inc. 104 Marshall St. Minden, LA 71055 318-377-1149 / 800-489-1149 Voice/Fax 318-469-2934 / 877-489-1149 Cell |
From: Martin G. <gim...@gi...> - 2002-06-26 12:31:21
|
"Tom Grant" <Tom...@LA...> writes: > I currently have v 1.6x setup at: > http://codeworks.org/phpweather/dtn.php displaying a table with 12 > airports from our local area (within a couple of hundred miles). It > currently gives an error if the cloud array is empty. >=20 > I've decided to make it work with the new version... Cool! =20 > Here's my test code for accessing array elements directly. I used as > a base a reply to a question on this list from back in March. >=20 > Apparently the get_metar_value function doesn't exist anymore. Has > it been replaced? No, it's hiding in the output/pw_text.php file... But there's no real reason to use that function instead of just accessing the array yourself - the following pieces of code to exactly the same: <?php $weather =3D new phpweather(); $data =3D $weather->decode_metar(); $temp =3D $data['temperature']['temp_c']; $hum =3D $data['rel_humidity']; ?> <?php $weather =3D new phpweather(); $text =3D new pw_text($weather); $temp =3D $text->get_metar_value('temperature', 'temp_c'); $hum =3D $text->get_metar_value('rel_humidity'); ?> The get_metar_value method is just a wrapper around the associative array returned by a call to decode_metar. > Or, do I need to include some other files. I've searched through > most of the files, but haven't been able to find it. Hehe - I couldn't find it either until I used 'grep' recursively on all the files :-) =20 > All help is appreciated! >=20 > Thanks! >=20 > <?php > error_reporting(E_ALL); >=20 > require('./phpweather.php'); > require('./pw_utilities.php'); >=20 > $weather =3D new phpweather(array()); $weather->set_icao('KDTN'); The above line should be shortened into this: $weather =3D new phpweather(array('icao' =3D> 'KDTN')); The array-argument is meant to be used when you know some values at the time you construct the object. =20 > $language =3D "en"; >=20 > include(PHPWEATHER_BASE_DIR . "/output/pw_text_$language.php"); > $type =3D 'pw_text_' . $language; > $text =3D new $type($weather, array()); It's no longer necessary to use array() as a second argument it's the default value. Since you're fiddling with the raw data returned by decode_metar, you'll probably be interested in this: The Raw Data =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D It's easy for you to get hold of the raw data produced by the code in `phpweather.php': $weather =3D new phpweather(); $data =3D $weather->decode_metar(); The variable `$data' is now an multidimensional associative array filled with all the information that the `decode_metar' function was able to extract from the METAR. The different parts in the METAR is stored under specific keys in the array -- if the METAR lacks a given part, then the corresponding entry in the array will be left out. This means, that you'll have to check each entry with `empty' before you use it, otherwise you see warnings from PHP, if you've set the error reporting level high enough. The following entries is always part of a METAR report, and is therefore always part of the decoded METAR array: `type' This can either be `METAR' or `SPECI' and denotes the type of the report. This isn't used for anything. `metar' The raw METAR report. `icao' The ICAO of the station which made the report. `location' The English name of the station. `time' A UNIX timestamp corresponding to the time of the report. The following entries are optional: `report_mod' A report modifier which gives information about how the report was made: `AUTO' for a fully automated report with no human intervention or `COR' for a corrected report. It's supposed to be part of all METARs, but experience shows that it's not. `wind' This is an array with information about the wind. The `deg' entry tells you the direction of the wind as compass-direction. If the wind came from variable directions, then will be the string `VRB'. The `meters_per_second', `miles_per_hour', and `knots' entries tells you the wind speed in various units. If gust was observer, there will be three extra entries: `gust_meters_per_second', `gust_miles_per_hour', and `gust_knots'. If the wind was varying, then two extra entries are present: `var_beg' and `var_end'. Both of these entries contain a compass-direction `visibility' This is an array with visibility information. Each entry in the array is a separate visibility group. Each visibility group contains a `prefix' which can be `-1', `0', or `1' which tells you if the visibility reported is "less than", "normal", or "greater than" respectively. The visibility itself is stored in the `meter', `km', `ft', and `miles' entries. There is an optional entry `dir' for a direction -- if there's no direction, then the visibility refers to the overall visibility. `runway' This is an array of runway groups. Each entry is a separate group. The groups contain an `nr' entry and an optional `approach' entry which tells you the runway-number and the approach respectively. The approach is one of `L', `C', or `R' for "left", "center", or "right" respectively. The visibility information can either be a single value or a pair of values in case of varying conditions. If there's only a single value, then `meter' and `ft' holds the visibility, an optional `prefix' can be `-1' if range is less than the lowest reportable value or `1' if greater than the greatest reportable value. A `tendency' entry holds either `D', `N' or `U' for "downward tendency", "no distinct tendency", or "upward tendency" respectively. `weather' This holds the current weather phenomena and is one of the most interesting groups. It's an array of weather groups. Each group contains a `proximity' entry which can be either `VC' for phenomena in the vicinity or the empty string otherwise. Next comes the `intensity'. This can be `-' for light phenomena, the empty string for moderate phenomena, or `+' for heavy phenomena. This is followed by a `descriptor', a `precipitation', a `obscuration', and finally a `other' entry. These entries contain the actual data found in the METAR. This means that it's up to the application to decode these symbols further, so that `RA' becomes "rain" and so on. `clouds' This is an array of cloud groups. Each group has a `condition' entry which can be either `CAVOK' for no clouds and no significant weather, `VV' for "vertical visibility", `FEW' for "few clouds", `SCT' for "scattered clouds", `BKN' for "broken clouds", `OVC' for "overcast", and `SKC' and `CLR' for "clear sky". The condition `CAVOK' is a bit special, because if it's present, then the `condition' entry will be the only entry in the cloud group. The other conditions will be part of a larger array with an optional `cumulus' entry denoting the presence of cumulonimbus (`CB') or towering cumulus (`TCU') clouds. There will also be `meter' and `ft' entries with the height of the clouds. If the clouds are below the station, then the height will be stored as `nil'. Theres an optional `prefix' with the usual meaning. `temperature' This is the temperature group. It's an array with two or four entries: `temp_c' and `temp_f' is always present and is the temperature in degrees Celsius and Fahrenheit respectively. If information about the dew-point is present, then `dew_c' and `dew_f' will be added to the array. `temp_min_max' Some stations report the minimum and maximum temperature over various timescales. The temperatures are stored in `min6h_c', `min6h_f', `max6h_c' and `max6h_f' for the temperatures in the last 6 hours, and in `min24h_c', `min24h_f', `max24h_c' and `max24h_f' for the temperatures in the last 24 hours. `altimeter' This is the air pressure measured in various units. The array has these entries: `inhg', `mmhg', `hpa', and `atm'. `precipitation' This is an array which stores the precipitation measured over various timescales. The entries `mm' and `in' contains the precipitation during the last hour, entries `mm_h6' and `in_6h' store the amount of precipitation in the last 3-6 hours and finally there's `mm_24h' and `in_24h' with the precipitation measured over the last 24 hours. If there has been snowfall, then that will be reported in the `snow_mm' and `snow_in' entries. All the entries can contain the special value `-1' which means that there was only a trace of precipitation. `remarks' Anything which isn't recognized is added to this entry. =2D-=20 Martin Geisler My GnuPG Key: 0xF7F6B57B See my homepage at http://www.gimpster.com/ for: PHP Weather =3D> Shows the current weather on your webpage. PHP Shell =3D> A telnet-connection (almost :-) in a PHP page. |