Guest User

Untitled

a guest
Aug 17th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4.  
  5. class Body(object):
  6.     def __init__(self, name):
  7.         self.name = name
  8.  
  9. names  = ['Sun', 'Earth', 'Mars', 'EMM_Hope', 'Tianwen_1', 'Mars2020']
  10.  
  11. bodies = []
  12. for name in names:
  13.     fname = name + ' horizons_results.txt'
  14.     with open(fname, 'r') as infile:
  15.         lines = infile.read().splitlines()
  16.  
  17.     iSOE = [i for i, line in enumerate(lines) if "$$SOE" in line][0]
  18.     iEOE = [i for i, line in enumerate(lines) if "$$EOE" in line][0]
  19.  
  20.     print(iSOE, iEOE, lines[iSOE], lines[iEOE])
  21.     lines = [line.split(',') for line in lines[iSOE+1:iEOE]]
  22.     JD  = np.array([float(line[0]) for line in lines])
  23.     pos = np.array([[float(item) for item in line[2:5]] for line in lines])
  24.     vel = np.array([[float(item) for item in line[5:8]] for line in lines])
  25.     body = Body(name)
  26.     body.lines = lines
  27.     body.JD = JD
  28.     body.pos = pos.T.copy()
  29.     body.vel = vel.T.copy()
  30.     bodies.append(body)
  31.  
  32. Sun, Earth, Mars, EMM_Hope, Tianwen_1, Mars2020 = bodies
  33.  
  34. JDmin, JDmax = min([b.JD[0] for b in bodies]), max([b.JD[-1] for b in bodies])
  35.  
  36. for b in bodies:
  37.     # https://numpy.org/devdocs/reference/generated/numpy.pad.html
  38.     pad = [ [0, 0], [int(round(b.JD[0] - JDmin)), int(round(JDmax - b.JD[-1]))]]
  39.     b.pos = np.pad(b.pos, pad, constant_values=np.nan)
  40.     b.vel = np.pad(b.vel, pad, constant_values=np.nan)
  41.  
  42. JD = Earth.JD
  43. JD_2021 = [float(line[0]) for line in Earth.lines if '2021-Jan' in line[1]][0]
  44. days_rel_2021 = Earth.JD - JD_2021
  45.  
  46. if True:
  47.     million = 1E+06
  48.     days_min =days_rel_2021[0]
  49.     days_max = min(days_rel_2021[-1], 60)
  50.     days_limits = (days_min, days_max)
  51.  
  52.     fig = plt.figure()
  53.  
  54.     ax1 = fig.add_subplot(3, 1, 1)
  55.  
  56.     r_Mars_Earth = np.sqrt(((Mars.pos - Earth.pos)**2).sum(axis=0))
  57.     l, = ax1.plot(days_rel_2021, r_Mars_Earth/million, label='Mars/Earth')
  58.     ax1.set_ylabel('million km')
  59.     ax1.set_xlim(*days_limits)
  60.     ax1.set_ylim(0, 200)
  61.     ax1.legend()
  62.  
  63.     ax2 = fig.add_subplot(3, 1, 2)
  64.  
  65.     r_EMM_Hope_Tianwen_1 = np.sqrt(((EMM_Hope.pos - Tianwen_1.pos)**2).sum(axis=0))
  66.     l, = ax2.plot(days_rel_2021, r_EMM_Hope_Tianwen_1/million, label='EMM_Hope/Tianwen-1')
  67.  
  68.     r_Tianwen_1_Mars2020 = np.sqrt(((Tianwen_1.pos - Mars2020.pos)**2).sum(axis=0))
  69.     l, = ax2.plot(days_rel_2021, r_Tianwen_1_Mars2020/million, label='Tianwen-1/Mars2020')
  70.  
  71.     r_Mars2020_EMM_Hope = np.sqrt(((Mars2020.pos - EMM_Hope.pos)**2).sum(axis=0))
  72.     l, = ax2.plot(days_rel_2021, r_Mars2020_EMM_Hope/million, label='Mars2020/EMM_Hope')
  73.  
  74.     ax2.set_ylabel('million km')
  75.     ax2.set_xlim(*days_limits)
  76.     ax2.legend()
  77.  
  78.     ax3 = fig.add_subplot(3, 1, 3)
  79.  
  80.     r_EMM_Earth = np.sqrt(((EMM_Hope.pos - Earth.pos)**2).sum(axis=0))
  81.     l, = ax3.plot(days_rel_2021, r_EMM_Earth/million, label='EMM_Hope')
  82.     r_EMM_Mars = np.sqrt(((EMM_Hope.pos - Mars.pos)**2).sum(axis=0))
  83.     ax3.plot(days_rel_2021, r_EMM_Mars/million, color=l.get_color())
  84.  
  85.     r_Tianwen_1_Earth = np.sqrt(((Tianwen_1.pos - Earth.pos)**2).sum(axis=0))
  86.     l, = ax3.plot(days_rel_2021, r_Tianwen_1_Earth/million, label='Tianwen_1')
  87.     r_Tianwen_1_Mars = np.sqrt(((Tianwen_1.pos - Mars.pos)**2).sum(axis=0))
  88.     ax3.plot(days_rel_2021, r_Tianwen_1_Mars/million, color=l.get_color())
  89.  
  90.     r_Mars2020_Earth = np.sqrt(((Mars2020.pos - Earth.pos)**2).sum(axis=0))
  91.     l, = ax3.plot(days_rel_2021, r_Mars2020_Earth/million, label='Mars_2020')
  92.     r_Mars2020_Mars = np.sqrt(((Mars2020.pos - Mars.pos)**2).sum(axis=0))
  93.     ax3.plot(days_rel_2021, r_Mars2020_Mars/million, color=l.get_color())
  94.     ax3.legend()
  95.  
  96.     ax3.set_xlim(*days_limits)
  97.     ax3.set_ylim(0, 10)
  98.     ax3.set_ylabel('million km')
  99.     ax3.set_xlabel('days relative to 00:00 01-Jan-2021 UTC')
  100.  
  101.     plt.show()
Add Comment
Please, Sign In to add comment