View on GitHub

Astro.js

A Javascript library for astronomy

Download this project as a .zip file Download this project as a tar.gz file

Unit tests | Bench tests

The plan is to make a Javascript framework for astronomy analagous to astropy. As a start I've made a core Javascript function that deals with package management: astro.js. The aim is to keep this core small and people who make use of the library only need to load the dependencies they need rather than a massive library. I will try to mirror the structure of astropy where possible.

Ethos

Packages

Example packages (work in progress):

Usage

astro.js creates a single global variable astrojs. The simplest way to include additional packages is with:

<script language="javascript" type="text/javascript" src="astro.js"></script>
<script language="javascript" type="text/javascript" src="astro.constants.js"></script>
<script language="javascript" type="text/javascript" src="astro.dates.js"></script>

// As packages may require dependencies that load
// asynchronously, we should wait for them
astrojs.ready(function(e){


});

An alternative is to use:

<script language="javascript" type="text/javascript" src="astro.js"></script>
<script type="text/javascript">
	
// This expects astro.constants.js and astro.dates.js to be in the same directory
astrojs.importPackages( ['constants', 'dates'], { me: 'tester', i: 8 }, function(e){


});
</script>

The formats of these two methods:

astrojs.ready( [eventData, ] callback );

astrojs.importPackages( packageArray [, eventData] [, callback] );

In the callback function we can then access the packages...

// This expects astro.constants.js, astro.dates.js, astro.coords.js
astrojs.importPackages( ['constants', 'dates', 'coords'], { me: 'tester', i: 8 }, function(e){

	// First let's check that we get the eventData
	console.log(e.data.me);           // <- print "tester" to the console	
	console.log(e.data.i);            // <- print 8 to the console


	// astro.constants.js - Each constant is an object containing value, error, name, origin, units

	// Get the SI value for the speed of light
	var c = astrojs.constants.c.value;

	// We can get a formatted string of the unit properties
	var c_string = astrojs.constants.c.toString();


	// astro.dates.js - Functions to manipulate astronomical dates and times

	// Get the Julian Date for now
	var jd = astrojs.dates.getJulianDate();
	console.log(jd);                  // <- print the Julian Date to the console
	
	// Return the Julian Date for March 21st 2012 (GMT)
	jd = astrojs.dates.getJulianDate(new Date('March 21, 2012 00:00:00 +00:00'));	
	console.log(jd);


	// astro.coords.js - Functions to manipulate astronomical coordinates

	// Coordinates for the LMC
	var l = 280.4652;
	var b = -32.8884;
	var ra = 80.8941708;
	var dec = -69.7561111;
	var ra_b1950 = 15.*(5.+(24./60));
	var dec_b1950 = -(69.+(48./60));

	// Convert from RA/Dec (J2000) to Galactic
	var coord = astrojs.coords.equatorial2galactic(ra,dec);
	// Returns an object { l: value, b: value }

	// Convert from RA/Dec (B1950) to Galactic
	var coord = astrojs.coords.equatorial2galactic(ra_b1950,dec_b1950,"B1950");
	// Returns an object { l: value, b: value }

	// Convert from Galactic to RA/Dec (J2000)
	var coord = astrojs.coords.galactic2equatorial(l,b);
	// Returns an object { ra: value, dec: value }

	// Convert from Galactic to RA/Dec (B1950)
	var coord = astrojs.coords.galactic2equatorial(l,b,"B1950");
	// Returns an object { ra: value, dec: value }

	// Convert from B1950 to J2000
	var coord = astrojs.coords.fk42fk5(ra_b1950,dec_b1950);
	// Returns an object { ra: value, dec: value }

	// Convert from J2000 to B1950
	var coord = astrojs.coords.fk52fk4(ra,dec);
	// Returns an object { ra: value, dec: value }

});

Example package

astrojs.importPackages() will load each package and their dependencies asynchronously. Once each package (with dependencies) has loaded, the package's init() function is called.

/*
	astro.example.js - a very simple example

*/
(function () {

	// Define any dependencies that are required for this package to run
	var dependencies = ['constants'];
	
	// Create the main function
	function init(astrojs) {

		// An internal variable
		var message = "Hello Universe!";

		// A variable that can be accessed from outside this package
		this.pi = 3.141;

		// An internal function
		function helper(){
			return message;
		}

		// A function that can be accessed from outside this package
		this.hello = function(){
			return helper();
		}
		return this;
	}

	// Register the package with the core
	astrojs.registerPackage({
		init: init,
		dependencies: dependencies,
		name: 'example',
		version: '0.0.1'
	});
	
})(astrojs);

// Once loaded you can call the package's public functions e.g.
console.log(astrojs.example.hello());

// ... and you can see their public variables;
console.log(astrojs.example.pi);

Unit Tests (using QUnit)

Bench Tests

These represent the performance on your computer. To reduce the chance of Javascript-engine caching, each loop uses an array of n random values (rather than the same value n times).