Swarm-NG  1.1
stats_tutorial.py
Go to the documentation of this file.
1 # -*- coding: utf-8 *-*
2 ## @file stats_tutorial.py Tutorial on extracting statistical information
3 # from a data file
4 
5 # @page TutorialPythonStats Extracting statistical information from data files
6 #
7 # In this tutorial, we revisit loading ensembles and extracting
8 # information from an ensemble; however, this time we are
9 # building a small utility to view statistical information about
10 # the contents of the ensemble.
11 #
12 # Source code for this tutorial can be found at @ref py/stats_tutorial.py
13 #
14 # @section ai Arguments
15 # First try to make sure that correct command-line arguments
16 # are provided.
17 import swarmng
18 from sys import argv,exit
19 from os import path
20 if len(argv) != 2 or not path.exists(argv[1]):
21  print("Usage: {0} <path_to_data_file>".format(argv[0]))
22  exit(1)
23 
24 # @section load Loading the ensemble
25 # Next load the data file based on the extension
26 fn = argv[1]
27 ext = path.splitext(fn)[1]
28 if ext == ".txt" : ens = swarmng.DefaultEnsemble.load_from_text(fn)
30 #
31 # @section cc Compute the orbital elements
32 # Now gather all data in a 3-dimensional NumPy array.
33 # NumPy has internal statstical information analysis
34 # functions that makes it easier to find different
35 # statistical information.
36 #
37 # Dimensions are:
38 # * planet number 0 to nbod-1
39 # * orbital element number : 0 to 5
40 # * system id
41 #
42 import numpy
43 
44 shape = (ens.nbod-1, 6, ens.nsys)
45 value = numpy.empty(shape)
46 for sysid, sys in enumerate(ens):
47  star = sys[0]
48  center = (star.pos,star.vel,star.mass)
49 
50  for bodid in range(1,ens.nbod):
51  bod = sys[bodid]
52  planet = (bod.pos,bod.vel,bod.mass)
53  orbital_elements = swarmng.keplerian_for_cartesian(planet,center)
54  value[bodid-1, :,sysid] = orbital_elements
55 
56 # @section pr Write the information to standard output
57 # Now iterate through the 3-dimensional array
58 # and calculate the average and standard deviation
59 # of each orbital element value for each body, then
60 # print it to the output.
61 #
62 print("Body number, Semi-major axis, Eccentricity, Inclination, Longtitude of the ascending node, Argument of periapsis, Mean anomaly")
63 for bodid in range(1,ens.nbod):
64  orbital_elements = [0,0,0,0,0]
65  o = "{0}".format(bodid)
66  for i in range(0,6):
67  o += ", {0}±{1}".format(numpy.average(value[bodid-1,i,:]),numpy.std(value[bodid-1,i,:]))
68  print(o)
69 #
70 # @section conc Conclusion
71 # This concludes this tutorial, to learn more about the
72 # orbital elements and the calculation method
73 # look at @ref swarmng.keplerian_for_cartesian
74