Reputation: 1
Trying to convert GPS times to a date and time of day in UTC. Inputs are space separated stream of x y seconds and output is a stream of x y z date time
This was designed to work with lidar data and the input may have come from a command like 'las2txt -parse xyt ...'.
import datetime
import fileinput
#number of seconds between the start of unix time (Jan 1, 1970) and gps time (Jan 6, 1980)
offset = 315964800
def countleaps(gpsTime):
"""Count number of leap seconds that have passed."""
# a tuple of the gps times where leap seconds were added
leaps = (
46828800, 78364801, 109900802, 173059203, 252028804,
315187205, 346723206, 393984007, 425520008, 457056009,
504489610, 551750411, 599184012, 820108813, 914803214,
1025136015
)
nleaps = 0
for leap in leaps:
if gpsTime >= leap:
nleaps += 1
return nleaps
if __name__ == '__main__':
for line in fileinput.input(r'E:\LiDAR\Staging\GW_Workspace\Beaverhead_Reports\BH_0001.txt'):
xvalue, yvalue, zvalue, timevalue=line.split(' ')[:4]
gpstime=float(timevalue)
gpstime += 1e9; # unadjusted GPS time
unixtime = gpstime + offset - countleaps(gpstime);
datetimestr = datetime.datetime.fromtimestamp(unixtime).strftime('%Y-%m-%d %H:%M:%S')
print xvalue, yvalue, zvalue, datetimestr
Error:
Traceback (most recent call last):
File "C:\Users\cwc051\PycharmProjects\sandbox\.venv\Sanbox_script.py", line 29, in <module>
xvalue, yvalue, zvalue, timevalue=line.split(' ') [:4]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 4, got 3)
Upvotes: 0
Views: 56
Reputation: 782166
If a line doesn't have at least 4 fields, you can't assign them to 4 variables. You need to check for that and take appropriate action:
fields = line.split(' ')
if len(fields) >= 4:
xvalue, yvalue, zvalue, timevalue=fields[:4]
else:
# assign variables some other way
or you could simply skip those lines:
if len(fields) < 4:
continue
Upvotes: 0
Reputation: 1717
Your problem is here: timevalue=line.split(' ')[:4]
You're telling python "Assign values to these four variables using the first four elements in the array resulting from the split operation", but based on the contents of the error you're receiving, at least one line
only has two spaces in it, so the array has only three elements, so there is no fourth element to assign to the timevalue
variable.
Upvotes: 1