Page 1 of 1

Simple way to add remote data to the Cumulus website

Posted: Mon 14 Feb 2011 10:46 am
by fractonimbus
In case anyone is interested...

For no essential reason I wanted to add the Southern Oscillation Index (NINO 3.4) and Indian Ocean Dipole index (updated weekly) to my main weather page (http://www.dcnicholls.com/wx). The data are available as time series on the Australian Bureau of Meteorology site. So the idea was simply to read the data pages, chop out the latest index values, and insert them into my web page. Sounded easy but it wasn't as it turns out to be quite difficult (impossible?) to use something like Javascript to access cross domain pages.

I finished up using PHP to read the data and "echo" it, and the jQuey Ajax routines to call the PHP and accept the output into a form that I could use Javascript to add to the page.

The PHP code is:

Code: Select all

<?php
header('Content-type: text/plain');   //probably unnecessary
$soi34 = file_get_contents('http://www.bom.gov.au/climate/enso/nino_3.4.txt');
if (soi34) { $soix = substr($soi34,-6); }  // the data I want is the final 5 characters
else { $soix = "n/a"; }
$iod= file_get_contents('http://www.bom.gov.au/climate/enso/iod_1.txt');
if ($iod) { $iodx = substr($iod,-6); }
else { $iodx = "n/a"; }
echo "$soix,$iodx";
?>
and the jQuery code, added to daj's js file (called wc2.js in my version of it), is:

Code: Select all

var bomproxy="/wx/p.php";
var bomdata = $.ajax({url: bomproxy, async: false, dataType: "text" }).responseText;
var rawbomdata=bomdata.split(',');
var soi = rawbomdata[0];
var iod = rawbomdata[1];
which is then "document.write" processed in the index.htm web page.

There are probably better ways to do this but I'm a neophyte js, Ajax and PHP programmer :-|

The BOM stuff is available via http://www.bom.gov.au/climate/IOD/about_IOD.shtml

DN

Re: Simple way to add remote data to the Cumulus website

Posted: Tue 15 Feb 2011 3:52 am
by fractonimbus
Whoops - the PHP only worked provided there were 5 characters - drop the minus sign and it breaks.

So this is now the revised code - using Regex to strip all but the last number and its sign after the last comma in the downloaded csv files:

Code: Select all

<?php
$pat = '#[\s\S]*,(.*)#';
$rep = '$1';
header('Content-type: text/plain');
$soi34 = file_get_contents('http://www.bom.gov.au/climate/enso/nino_3.4.txt');
if ($soi34) { $soix = preg_replace($pat, $rep, $soi34); }
else { $soix = "n/a"; }

$iod= file_get_contents('http://www.bom.gov.au/climate/enso/iod_1.txt');
if ($iod) { $iodx = preg_replace($pat, $rep, $iod); }
else { $iodx = "n/a"; }
echo "$soix,$iodx";
?>
DN