Swarm-NG  1.1
query.py
Go to the documentation of this file.
1 ## @file query.py Support routines for @ref swarm-query "swarm-query.py" command-line utility.
2 #
3 # The routines here are not documented because it solely consists of generating
4 # a table output consistent with "swarm query" command.
5 #
6 
7 from logdb import IndexedLogDB, PKey
8 from bsddb3.db import *
9 from logrecord import LogRecord
10 from struct import pack, unpack
11 from functools import partial
12 import math
13 from exceptions import *
14 
15 
16 RAD2DEG = 180.0/math.pi
17 class Keplerian(object):
18  ASTROCENTRIC, BARYCENTRIC, JACOBI, ORIGIN = range(4)
19  choices = [ "astrocentric", "barycentric", "jacobi", "origin" ]
20  def __init__(self,type):
21  if isinstance(type,str):
22  self.type = Keplerian.choices.index(type)
23  else:
24  self.type = type
25  def __repr__(self):
26  return "Keplerian(%s)" % Keplerian.choices[self.type]
27 
28 
29 
30 TABLE, KEYS = range(2)
31 def print_record(print_mode, r, body_range):
32  k, l = r
33  if print_mode == TABLE :
34  i = 0
35  for b in l.bodies:
36  if(body_range.contains(i)):
37  print "%10d %lg %6d %6d %9.2g %10.4g %10.4g %10.4g %10.5lg %10.5lg %10.5lg %d" % (l.msgid, l.time, l.sys, i, b.mass, b.position[0], b.position[1], b.position[2], b.velocity[0], b.velocity[1], b.velocity[2], l.state)
38  i = i + 1
39 
40  elif isinstance( print_mode , Keplerian ):
41  tp = print_mode.type
42  if tp == Keplerian.JACOBI:
43  body_orbits = l.bodies_in_keplerian_jacobi()
44  else:
45  if tp == Keplerian.ASTROCENTRIC:
46  center = l.star()
47  elif tp == Keplerian.BARYCENTRIC:
48  center = l.barycenter()
49  elif tp == Keplerian.ORIGIN:
50  center = l.origin()
51  else:
52  raise ValueError("Wrong keplerian index")
53  body_orbits = l.bodies_in_keplerian(center)
54 
55  for i, b, orbit in body_orbits:
56  if(body_range.contains(i)):
57  print "%10d %lg %6d %6d %9.2g %9.5lg %9.5lg %9.5lg %9.5lg %9.5lg %9.5lg %d" % (l.msgid, l.time, l.sys, i, b.mass, orbit.a, orbit.e , orbit.i*RAD2DEG, orbit.O*RAD2DEG, orbit.w *RAD2DEG, orbit.M*RAD2DEG, l.state)
58 
59  elif print_mode == KEYS :
60  print k
61  else:
62  print print_mode, type(print_mode)
63 
64 
65 def truncate(number,iterator):
66  for i in range(0,number):
67  yield iterator.next()
68 
69 def run_with_args(args):
70  d = IndexedLogDB(args.database)
71 
72  if args.keplerian != None:
73  print_mode = Keplerian(args.keplerian)
74  else:
75  print_mode = TABLE
76 
77 
78  if args.initial_conditions :
79  q = d.initial_conditions(args.system_range)
80  elif args.final_conditions :
81  q = d.final_conditions(args.system_range)
82  else:
83  q = d.query(args.time_range,args.system_range,args.evt_id)
84 
85  for r in truncate(args.max_records,q):
86  print_record(print_mode,r, args.body_range)
87 
88 
89