Book Review: Practical Astronomy with your Calculator by Peter Duffett-Smith

I have a love of astronomy, and combined with my love for programming, that creates in me that burning ambition to try and use both these passions together. This is where this book is invaluable.

Practical Astronomy with your Calculator is a very useful books to figure out how to calculate just about every astronomical phenomena which can be calculated from solar eclipses to the phase and location of the moon, to finding the positions of the planets at any given time.

Now, this book is aimed at calculating using a calculator, so the algorithms are slightly simplified to make that task easier, and therefore the precision of the calculations is not as great as if the algorithms had ben designed purely for a computer, but for most calculations this level of accuracy is more than sufficient.

The advantage of this method though, is that each step is very clearly laid out in the calculation, so that it is very easy to understand.

I have used this book extensively in writing an astronomical calculation application, which I will soon make available for download, and has proven indespensible to me.

O3D: Fast, powerful 3D applications in a web browser

A friend of mine called me up the other day and asked me if I knew of a way to create a simple maze application on a web page. I told him to google O3D (and view the samples in particular) and then phone me back to tell me how fabulously awesome it was. A little while later, I got that call. He could not believe what he was seeing.

O3D is a 3D open source cross-platform, cross-browser Javascript API (which you can find here). There have been several other attempts at introducing 3D to a web browser, but all have failed. XRML is just plain old, X3D is rather high level, which impacts performance and Flash 3D is dependant on the speed of the Flash engine.

O3D is different. It is fast. Very fast. The reason for this is that the API hooks directly into the low level OpenGL and Direct3D libraries. This strips away several layers which normally exist between a web application and the system hardware.

There is a little competition though. A project by the name of C3DL seems to be competing on similar grounds, although at this stage it needs the latest version of firefox 3.5 to run and is still considered experimental.

The one worrying thing about all this is that there lacks a clearly defined standard for 3D graphics on the web, which will likely mean a coming technology war amongst the various plugin providers.

And until that dust settles, I’ll be backing O3D.

Writing an exchange rate graph in PHP – Part one: Getting the data

Over the next few blog posts I am going to explain how to create an exchange rate graphing script in PHP, which takes the two currencies as input as well as the period to calculate for, and then outputs an image showing the graph of how the exchange rate has varied over time.

One of the biggest problems when trying to display exchange rate data is where to get the data to display, but that is not a major worry, as there are several sources of this information freely available on the web.

The source where I got the historical data is from the European Central Bank, which publishes a list of exchange rates for many currencies against the Euro, with historical data dating back to 1999. The data is available for download here.

Of particular note here is the historical file, which can be downloaded as a CSV, which we are going to use to populate our database.

The first thing we need to do though is to create a MySQL database to store the exchange rate data in. The query to create the table we need is as follows:

CREATE TABLE `exchangerates` (
`ratedate` date DEFAULT NULL,
`GBP` decimal(10,4) DEFAULT NULL,
`USD` decimal(10,4) DEFAULT NULL,
`ZAR` decimal(10,4) DEFAULT NULL,
`AUD` decimal(10,4) DEFAULT NULL,
`NZD` decimal(10,4) DEFAULT NULL,
`CHF` decimal(10,4) DEFAULT NULL,

Now that we have a database structure and have the historical exchange rate data, we need a script to populate our database. This is done with this little script.

what this script does is parse the CSV file, and then selects the columns in the CSV corresponding to the currencies we are interested in – we don’t want most of the currencies. The script builds an insert statement for each line and then executes the query against the database, and at the end of it, we have the full historical exchange rate data since 1999 in our database.

$row[ipaddress] = "localhost";
$row[dbusername] = "root";
$row[dbpassword] = "";
$row[dbname] = "exchange";

$data = csvParser::readCSV('eurofxref-hist.csv');
$dbconn = mysql_connect($row[ipaddress], $row[dbusername], $row[dbpassword]);
mysql_select_db($row[dbname], $dbconn);

echo "Rows: " . count($data)."
for($i = 1; $i < count($data); $i++){
$sql = "INSERT INTO `exchangerates` (`ratedate`, `GBP`, `USD`, `ZAR`, `AUD`, `NZD`, `CHF`) VALUES ('".$data[$i][0]."', '".$data[$i][8]."', '".$data[$i][1]."', '".$data[$i][40]."', '".$data[$i][26]."', '".$data[$i][36]."', '".$data[$i][19]."')";
echo $sql."
mysql_query($sql, $dbconn);
The csvparser.php file is as follows:

class csvParser{

public static function readCSV($filename, $delimiter = ",", $lineLength = 2000){
if($handle = fopen($filename, "r")){
$csvData = array();
while (($row = fgetcsv($handle, $lineLength, $delimiter)) !== false) {
$csvData[] = $row;
return $csvData;
return false;

public static function insertQuotes($str, $delimiter = ",")
$str = str_replace('"', '""', $str);
if (strpos($str, $delimiter) == false){
return $str;
return '"'.$str.'"';

public static function formatCSV($contentArray, $headerArray = null, $delimiter = ",")
if (!is_array($contentArray)){
throw new customException("Cannot convert to CSV - data is not an array");
if ((!is_array($headerArray)) && (!is_null($headerArray))){
throw new customException("Cannot convert to CSV - header data is not an array");
$csvStr = "";
if (!is_null($headerArray)){
for($i = 0; $i < sizeof($headerArray); $i++){
if ($i != 0){
$csvStr .= $delimiter;
$csvStr .= csvParser::insertQuotes($headerArray[$i], $delimiter);
$csvStr = substr($csvStr, 1);
$csvStr .= "\r\n";
for($j = 0; $j < sizeof($contentArray); $j++){

for($i = 0; $i < sizeof($contentArray[$j]); $i++){
if ($i != 0){
$csvStr .= $delimiter;
$csvStr .= csvParser::insertQuotes($contentArray[$j][$i], $delimiter);

$csvStr .= "\r\n";
return $csvStr;


In the next part, we are going to look at how to draw the graph using this data.

Getting ExtJS 3.0 to work in Adobe Air

Adobe Air has quite a lot of security restrictions which it imposes on the Javascript trying to run in the sandbox. One of the side effects of this is that ExtJS throws up one of these security violations.

ExtJS 2.3 came with an extension so that it was able to be used within Adobe Air, which managed to bypass this problem, and using this extension, ExtJS works very well in Adobe Air.

However, the latest ExtJS 3.0 does not have this extension built in. According to the forums, they are still working on this. There is, fortunately, a workaround.

I have successfully managed to get ExtJS 3.0 to run in Adobe Air by copying the air extension from the ExtJS 2.3 codebase into the ExtJS 3.0 codebase, and then referencing it appropriately in the html page.

I do have to admit that doing this hack might potentially cause problems if you are trying to use any of the newest features in ExtJS 3.0, but so far have not had any issues.

When the air extension for ExtJS 3.0 does get release, that, of course, would be preferable, but until then, this works for me perfectly.

Object oriented Javascript

Javascript is a rather curious language. It began it’s life as a language used mainly for form validation and a few special effects, and has matured into a language that can now do just about anything other languages can do. Using Google’s O3D, Javascript can do 3D graphics, using Titanium it can be used to write desktop applications, andin it’s regular position t functions well in a web application environment.

With all these modern demands being made on Javascript, there is one feature that is severely lacking – classes.

We are fortunate, however, as due to the way in which functions are handled in Javascript, functions are able to be substituted in place of class definitions.

As an example

ExampleClass = function() {
var text = 'Hello ';

this.testFunction = function(name) {
alert(text + name);

instance = new ExampleClass();

It seems like quite a strange idea at first, but makes Javascript able to compete with real object orientated languages, and also allows the code to be neater and able to be written in a more modular way. Traditional Javascript is notorious for having tons of variables and functions floating around in the global scope.

It must be said, however, that this is a rather limited way of doing object orientated programming since it does not easily allow inheritance, and while the scope of variables can be either private or public (denoted by the use of the this keyword), concepts such as protected and virtual functions are not supported.

Overall though, I just think it is awesome that this can be done at all with a langauge that’s main use was making sure your email address was entered correctly.