Swarm-NG  1.1
Extracting statistical information from data files

In this tutorial, we revisit loading ensembles and extracting information from an ensemble; however, this time we are building a small utility to view statistical information about the contents of the ensemble.

Source code for this tutorial can be found at py/stats_tutorial.py


First try to make sure that correct command-line arguments are provided.

1 import swarmng
2 from sys import argv,exit
3 from os import path
4 if len(argv) != 2 or not path.exists(argv[1]):
5  print("Usage: {0} <path_to_data_file>".format(argv[0]))
6  exit(1)

Loading the ensemble

Next load the data file based on the extension

1 fn = argv[1]
2 ext = path.splitext(fn)[1]
3 if ext == ".txt" : ens = swarmng.DefaultEnsemble.load_from_text(fn)

Compute the orbital elements

Now gather all data in a 3-dimensional NumPy array. NumPy has internal statstical information analysis functions that makes it easier to find different statistical information.

Dimensions are:

  • planet number 0 to nbod-1
  • orbital element number : 0 to 5
  • system id
1 import numpy
3 shape = (ens.nbod-1, 6, ens.nsys)
4 value = numpy.empty(shape)
5 for sysid, sys in enumerate(ens):
6  star = sys[0]
7  center = (star.pos,star.vel,star.mass)
9  for bodid in range(1,ens.nbod):
10  bod = sys[bodid]
11  planet = (bod.pos,bod.vel,bod.mass)
12  orbital_elements = swarmng.keplerian_for_cartesian(planet,center)
13  value[bodid-1, :,sysid] = orbital_elements

Write the information to standard output

Now iterate through the 3-dimensional array and calculate the average and standard deviation of each orbital element value for each body, then print it to the output.

1 print("Body number, Semi-major axis, Eccentricity, Inclination, Longtitude of the ascending node, Argument of periapsis, Mean anomaly")
2 for bodid in range(1,ens.nbod):
3  orbital_elements = [0,0,0,0,0]
4  o = "{0}".format(bodid)
5  for i in range(0,6):
6  o += ", {0}±{1}".format(numpy.average(value[bodid-1,i,:]),numpy.std(value[bodid-1,i,:]))
7  print(o)


This concludes this tutorial, to learn more about the orbital elements and the calculation method look at swarmng.keplerian_for_cartesian