Python: reading a text file with time in hours, minutes and seconds; and angle in degrees, minutes of arc and seconds of arc

I am trying to build data from a file in python using matplotlib .

The file contains columns 2 . The first column has hour:minute:seconds . The second column has degree:arc minutes:arc seconds .

For hour:minute:seconds I use datetime.strptime('%H:%M:%S.%f') . Is there a similar function for degree:arc minutes:arc seconds in Python or Matplotlib?

Here is an example data file:

  00:06:04.8 -70:00:00.0 00:07:01.7 -66:00:00.0 00:14:17.7 -59:00:00.0 00:23:00.0 -52:00:00.0 00:23:50.3 -49:00:00.0 00:23:54.4 -29:00:00.0 00:23:59.4 -28:00:00.0 00:24:03.7 -26:00:00.0 00:24:03.8 -14:00:00.0 00:24:03.9 +25:00:00.0 00:30:30.10 +30:00:00.0 
+4
source share
1 answer

Using matplotlib.dates.datestr2num , you can easily convert your first column to paid numbers, but I did not find a function for your second column. You can build a function to handle, though:

 import numpy as np def calc_hour( str ): hour, min, sec = [float(i) for i in str.split(':')] min += sec/60. hour += min/60. return hour calc_hour = np.vectorize( calc_hour ) def calc_deg( str ): deg, min, sec = [float(i) for i in str.split(':')] min += sec/60. deg += min/60. return deg calc_deg = np.vectorize( calc_deg ) 

Then read the data from the intended tmp.txt file:

 values = np.loadtxt('tmp.txt', dtype=str) hours= calc_hour( values[:,0] ) degs = calc_deg( values[:,1] ) 

Getting something like:

 hours = array([ 0.10133333, 0.11713889, 0.23825 , 0.38333333, 0.39730556, 0.39844444, 0.39983333, 0.40102778, 0.40105556, 0.40108333, 0.50836111]) degs = array([-70., -66., -59., -52., -49., -29., -28., -26., -14., 25., 30.]) 

This can be built:

 import matplotlib.pyplot as plt plt.plot(hours,degs) 

For you, giving:

enter image description here

+3
source

Source: https://habr.com/ru/post/1483200/


All Articles