0

I am measuring the mass of Jupiter based on Kepler's Third Law. I have taken 147 images of Jupiter and its moon, specifically Io. I have written code to take the position vectors from the image file so I can calculate the magnitude of the distance between Io and Jupiter (distance). This will allow me to plot distance over time. ie: plot(t,d)

The code below while not elegant maybe, achieves this wonderfully. Yet I am trying to convert my array of datetimes into a plot-able value. I have several nights worth of data. The plot will be sinusoidal and the period of Io's orbit will be derived by interpreting the plot. I have included the code, followed by the CSV file in .txt format: python3

import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
import math

# Declare arrays (lists) for (x,y) pixel position vectors from .fits
# image files. 
jup_x = []
jup_y = []
io_x = []
io_y = []
cal_x = []
cal_y = []
gan_x = []
gan_y = []
eur_x = []
eur_y = []
date_and_time = []

readFile = open('raw_data1.txt','r')

sepFile = readFile.read().split('\n')

readFile.close()

for j_pos_pair in sepFile:
    j_xandy = j_pos_pair.split(',')
    jup_x.append(int(j_xandy[1]))
    jup_y.append(int(j_xandy[2]))

print(jup_x)
print(jup_y)

for io_pos_pair in sepFile:
    io_xandy = io_pos_pair.split(',')
    io_x.append(int(io_xandy[3]))
    io_y.append(int(io_xandy[4]))

print(io_x)
print(io_y)

for cal_pos_pair in sepFile:
    cal_xandy = cal_pos_pair.split(',')
    cal_x.append(int(cal_xandy[5]))
    cal_y.append(int(cal_xandy[6]))

print(cal_x)
print(cal_y)

for gan_pos_pair in sepFile:
    gan_xandy = gan_pos_pair.split(',')
    gan_x.append(int(gan_xandy[7]))
    gan_y.append(int(gan_xandy[8]))

print(gan_x)
print(gan_y)

for eur_pos_pair in sepFile:
    eur_xandy = eur_pos_pair.split(',')
    eur_x.append(int(eur_xandy[9]))
    eur_y.append(int(eur_xandy[10]))

print(eur_x)
print(eur_y)

for date_time in sepFile:
    d_and_t = date_time.split(',')
    date_and_time.append(str(d_and_t[0]))

print(date_and_time)

# make into arrays for vector operations
# date_and_time = np.array(date_and_time)
datetime = np.array(date_and_time)
Jup_x = np.array(jup_x)
Jup_y = np.array(jup_y)

Io_x = np.array(io_x)
Io_y = np.array(io_y)

print(Jup_x)
print(Jup_y)
print(Io_x)
print(Io_y)

# vector subtraction (Position vector between Jupiter and Io)
J_Io_s_x = Jup_x - Io_x  # reads: separation between Io and Jupiter x values
J_Io_s_y = Jup_y - Io_x  # reads: separation between Io and Jupiter y values

print(J_Io_s_x)
print(J_Io_s_y)

M_s_J_Io = ((J_Io_s_x)**2 + (J_Io_s_y)**2)**0.5 # The magnitude of the separation 
                                                # between Jupiter and Io in pixels
print(M_s_J_Io)

from datetime import datetime
datetime.strptime("2015-feb-05 19:00", "%Y-%b-%d %H:%M")

x = np.array([datetime.datetime(2015, 2, 5, i, 0) for i in range(24)])
plot_M_s_J_Io = np.array(M_s_J_Io)

plt.plot(x,plot_M_s_J_Io)
plt.show()

The CSV file: (This is the test run copy, the actual list is waaaay larger)

2015-feb-05 20:11,1150,735,1335,647,307,1123,0000,000,965,818

This CSV file above includes info on all the moons as well. For my testing i restricted it to just info on Jupiter and Io. That is only the first five fields in the CSV file. Its indexed from 0-10.

Any suggestions, I know I am close, I just don't understand the time conversion aspect of it. Full disclosure: this IS for a LAB at NMT, however I am already going above and beyond in this report as I am using python instead of doing this by hand. All help is of course greatly appreciated.

Jesse

11thZone
  • 71
  • 3
  • 10
  • There's easily a tousand question on plotting mplt timescales. date2num is the command you're most likely looking for in combination with plot_date command. I propose this question gets closed as duplicate. [example1](http://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib) [example2](http://stackoverflow.com/questions/19079143/how-to-plot-time-series-in-python) – ljetibo Feb 19 '15 at 00:13
  • That would be really screwed up ljetibo, because I HAVE LOOKED AT ALL OF THEM!!!!! And they are all script specific, and I cant implement the fix. I am a beginner, so how about helping me instead of being a jerk. Not all of us are instantly born with coding skills. – 11thZone Feb 19 '15 at 01:39
  • Don't get your panties in a twist. If you did how come you didn't notice you use `plt.plot` and not `plt.plot_dates` and why don't you convert your dates to numbers so mpl understands them with `date2num` function as suggested in my answer as well as all the ones I linked. `plt.plot_dates` will interpret those numbers for you and display dates. Posting all your code and having a edgy response is a bad way of asking for help. I wasn't born with coding skills either, but I didn't skimp on code reading. After checking it turns out you don't even **have** to convert the dates with date2num. – ljetibo Feb 19 '15 at 01:53
  • Because I dont know how to. maybe if I had more skills I would know how to fix it. But for now all I can do is stare at my screen and wonder why I cant convert some time values into a timestamp. – 11thZone Feb 19 '15 at 02:01
  • 1
    **literally** replace your second last line which is `plt.plot` with `plt.plot_date`. Your ˙x` is a numpy array with 24 elements representing 24h in a day. If `plot_M_s_J_Io` is also 24 long numpy array, you should have no problems by just **literally** replacing `plt.plot` with `plt.plot_date` and you would have your plot. – ljetibo Feb 19 '15 at 02:06
  • thanks man ill give it a shot, sorry too. – 11thZone Feb 19 '15 at 02:14
  • Don't worry, I'm not mad. Programing takes patience and nerves of steel. Sometimes it's best to get away if you're stuck and the errors reappear miraculously. – ljetibo Feb 19 '15 at 02:17
  • Got it workin' with a variation of your theme. The plot looks good too, just have to tweak some values into negative to denote when Io is on the left side of Jupiter vs the right so the plot will look sinusoidal. Thanks for the help again. Between you and my buddy Im back on track. – 11thZone Feb 19 '15 at 03:38
  • @ljetibo how about adding your suggestion as an answer? That way, 11thZone can reward you with your deserved juicy rep points. – Marijn van Vliet Feb 19 '15 at 09:07

2 Answers2

0

Since I don't have your files, I can only presume about your data processing that's going on above the line:

from datetime import datetime

However from that point onwards, you have a few wird quirks in your code.

First your statement datetime.strptime("2015-feb-05 19:00", "%Y-%b-%d %H:%M") does nothing. It runs, but doesn't assign the returned value to a variable. So it's safe to get rid of it.

Secondly is in the line:

x = np.array([datetime.datetime(2015, 2, 5, i, 0) for i in range(24)])

This line will work because you imported the datetime module at the start of your script. But if you didn't do that you would've caused an error message to appear. Since you explicitly imported datetime function of datetime module I'd recommend just using it, for brevity/clarity reasons, instead of datetime.datetime

And the final (and probably the only actual mistake made) is that to plot a time sequence in pyplot you have to use pyplot.plot_dates function and not pyplot.plot function.

ljetibo
  • 3,048
  • 19
  • 25
  • This is what is running and working, thanks for the input folks: from datetime import datetime t = datetime.strptime("2015-feb-05 19:00", "%Y-%b-%d %H:%M") for line in date_and_time: t = datetime.strptime(line, "%Y-%b-%d %H:%M") plot_time.append(t.timestamp()) plt.plot(plot_time,M_s_J_Io) plt.show() – 11thZone Feb 20 '15 at 18:11
0

This is what is running and working, thanks for the input folks:

from datetime import datetime                   
t = datetime.strptime("2015-feb-05 19:00", "%Y-%b-%d %H:%M")
for line in date_and_time:
    t = datetime.strptime(line, "%Y-%b-%d %H:%M")
    plot_time.append(t.timestamp())
plt.plot(plot_time,M_s_J_Io)
plt.show()
11thZone
  • 71
  • 3
  • 10