-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_orbits.py
executable file
·83 lines (61 loc) · 1.58 KB
/
plot_orbits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /usr/bin/env python
## 2014.11.08 - [email protected]
## Plot the orbits
import sys
args = sys.argv
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
###############################################
t, x, y, vx, vy = [], [], [], [], []
def add_row( row_array ):
t.append( row_array[0] )
x.append( row_array[1] )
y.append( row_array[2] )
vx.append( row_array[3] )
vy.append( row_array[4] )
return
def read_orbit_file( filename ):
ff = open(filename, 'r')
for line in ff:
ltemp = line.strip().split('\t')
rowvals = []
for i in range(5):
rowvals.append( float(ltemp[i]) )
add_row( rowvals )
ff.close()
return (t, x, y, vx, vy)
def make_3d_coords( x, y, theta ):
xnew = x * np.cos(theta)
ynew = y * np.cos(theta)
znew = x * np.sin(theta)
return (xnew, ynew, znew)
def plot_orbit( x, y, z, **kwargs ):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot( x, y, z, **kwargs )
fig.show()
return
def save_orbit( x, y, z, filename ):
ff = open( filename + '.3d', 'w' )
for i in range(len(x)):
ff.write( np.str(x[i]) + "," +
np.str(y[i]) + "," +
np.str(z[i]) + "\n" )
ff.close()
return
################################################
## Run the code
def run_plot():
try:
filename = args[1]
read_orbit_file( filename )
except:
print "ERROR: Check input arguments"
return
theta = np.random.uniform() * 2.0*np.pi
(xp, yp, zp) = make_3d_coords( np.array(x), np.array(y), theta )
plot_orbit( xp, yp, zp, ls='-', lw=2 )
save_orbit( xp, yp, zp, filename )
return
run_plot()