Reading Specific Data From A Text File?

Hello All,

Please excuse my lack of knowledge in advance, I’m a beginner but pick things up relatively quickly, and tend to do things by trial and error.

I would appreciate any help that anyone could offer with the following:

I would like to retrieve two pieces of data from a “text file” and use them to calculate a result from a function I have in javascript. An example of the javascript code is:

<script type="text/javascript">
function calculate() {
var drybulb = document.calc_form.drybulb.value;
var relhum = document.calc_form.relhum.value;
var answer = '';
if (drybulb !== '' && relhum !== '') {
answer = (0.567*drybulb*1) + (0.393 * (relhum*1/100 * 6.105 * Math.exp(17.27 * drybulb / (237.7 + drybulb*1)))) + 3.94;
}
document.calc_form.answer.value = answer;
return false;
}
</script>

Currently this is evaluated with entry from a simple html form, but I would like to have this performed “automatically”.

The “text file” that I would like to access is here, not on my server:

http://www.bom.gov.au/fwo/IDV60901/IDV60901.94870.axf

I would like to extract the “air_temp” and “rel_hum” values under the [data] section for use in my function. I just need the “most recent” values (i.e. first), not all those contained in the file. This is a single record from that section (sorry for the mess this no doubt makes):

[data]
sort_order,wmo,name[80],history_product[80],local_date_time[80],local_date_time_full[80],aifstime_utc[80],air_temp,apparent_t,cloud[80],cloud_base_m,cloud_oktas,cloud_type[80],cloud_type_id,delta_t,dewpt,gust_kmh,gust_kt,lat,lon,press,press_msl,press_qnh,press_tend[80],rain_trace[80],rel_hum,sea_state[80],swell_dir_worded[80],swell_height,swell_period,vis_km[80],weather[80],wind_dir[80],wind_spd_kmh,wind_spd_kt
0,94870,“Moorabbin Airport”,“IDV60901”,“27/10:00pm”,“20120227220000”,“20120227110000”,18.8,19.3,“Partly cloudy”,510,3,"-",-9999,0.8,17.5,13,7,-38.0,145.1,1013.5,-9999.0,1013.5,"-",“11.4”,92,"-","-",-9999.0,-9999,“10”,"-",“S”,11,6

Would it be possible to obtain these values using PHP?, and if so any advice on how this could be done (noting my minimal knowledge) would be greatly appreciated.

Cheers.

its possible, but all depends on if that file is hosted by a company that allows that kind of access. Given that its a military or government site, they may not.

Hi, thanks for your reply.

I appreciate your comment, yes that is a valid question.

The Bureau of Meteorology provides this information free of charge for download/access, provided copyright notices are posted, which is what I intend to do.

As an example you can see how this information is made available near the base of this page:

http://www.bom.gov.au/products/IDV60901/IDV60901.94870.shtml

If you are able to assist with as to how I can “pull” these values I would appreciate it.

Thanks

Prolly sumfin like this;

<?php header('Content-Type: text/plain'); $src=file_get_contents('http://www.bom.gov.au/products/IDV60901/IDV60901.94870.shtml'); preg_match_all('@headers="t1-temp">([^<]*)<.*?headers="t1-relhum">([^<]*)@s',$src,$matches); foreach($matches[0] as $key=>$val) { $data[]=array($matches[1][$key],$matches[2][$key]); } var_dump($data); ?>

Very easy… Here is code that pulls the page and data. Then, it decodes it by removing the headers and breaks down the data so you can have access to it. Finally, it displays the two fields that you wanted to view. I did leave in the full page display of all the DATA from the website in case you wanted to pull some other data from it.
The actual code is only 3 lines. The entire page is pulled into an array for whatever use you need.
[php]

<?PHP $DataPage = file_get_contents('http://www.bom.gov.au/fwo/IDV60901/IDV60901.94870.axf'); $DataParts = stristr($DataPage, "[data]"); $Parts = explode(",", $DataParts); // All data is now inside the "$Parts" array. use it as you need. First row is titles of the data. // Air Temp should be #7 (title), #41 (data) Rel-Hum should be #25 (title), #59 (data) echo("Air Temp Title =" . $Parts[7] . " , Rel Hum Title=" . $Parts[25]); echo("


"); echo("current value =" . $Parts[41] . " , current value =" . $Parts[59]); echo("


"); var_dump($Parts); ?>

[/php]
Just load this into a PHP/HTML page and run it and you will get the idea… Hope that works for you…

Thank you for your help, have this working, but I might be back for more…
Cheers.

Sponsor our Newsletter | Privacy Policy | Terms of Service