Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import argparse
- import datetime
- from functools import partial
- import pytz
- import sys
- parser = argparse.ArgumentParser(description='Convert MTR file to new file.')
- parser.add_argument(
- 'data_file', metavar='input.MTR', type=file, nargs=1,
- help='input file')
- parser.add_argument(
- 'output_file', metavar='output.TXT', type=argparse.FileType('w'),
- default=sys.stdout, nargs='?', help='output file')
- def two_complement_resolve(value, bit_length=10):
- limit = 1 << (bit_length - 1)
- sub = 1 << bit_length
- return (value - sub) if (value > limit) else value
- def convert_sentence(sentence):
- # Reminder: the measure is coded on 30 bits.
- # 10 MSB represent X value.
- # 10 following MSB represent Y value.
- # 10 following MSB represent Z value.
- # 2 LSB are not used.
- # The time value is coded on 26 bits.
- # The two MSB come to complete the buffer 4th byte.
- # Then the three LSB are stored in bytes 5, 6, 7.
- x_value = ((ord(sentence[0]) << 2) | (ord(sentence[1]) >> 6)) & 0x3FF
- y_value = ((ord(sentence[1]) << 4) | (ord(sentence[2]) >> 4)) & 0x3FF
- z_value = ((ord(sentence[2]) << 6) | (ord(sentence[3]) >> 2)) & 0x3FF
- time_value = (
- (ord(sentence[3]) << 24)
- | (ord(sentence[4]) << 16)
- | (ord(sentence[5]) << 8)
- | ord(sentence[6])
- ) & 0x3FFFFFF
- return (
- time_value, two_complement_resolve(x_value),
- two_complement_resolve(y_value), two_complement_resolve(z_value))
- def string_to_meteor_id(buffer_string):
- return format(
- ord(buffer_string[0])
- | (ord(buffer_string[1]) << 8)
- | (ord(buffer_string[2]) << 16)
- | (ord(buffer_string[3]) << 24), 'x')
- def string_to_timestamp(buffer_string):
- """
- Timestamp inherited from fatfs get_fattime()
- bit31:25
- Year origin from the 1980 (0..127)
- bit24:21
- Month (1..12)
- bit20:16
- Day of the month(1..31)
- bit15:11
- Hour (0..23)
- bit10:5
- Minute (0..59)
- bit4:0
- Second / 2 (0..29)
- """
- second = 2 * (ord(buffer_string[0]) & 0x1F)
- minute = (
- ((ord(buffer_string[0]) & 0xE0) >> 5)
- | ((ord(buffer_string[1]) & 0x7) << 3))
- hour = ((ord(buffer_string[1]) & 0xF8) >> 3)
- day = ord(buffer_string[2]) & 0x1F
- month = (
- ((ord(buffer_string[2]) & 0xE0) >> 5)
- | ((ord(buffer_string[3]) & 0x1) << 3))
- year = ((ord(buffer_string[3]) & 0xFE) >> 1) + 1980
- return datetime.datetime(
- year, month, day, hour, minute, second, 0, pytz.UTC)
- def extract_header(file_pointer):
- """
- The header of the .MTR file is composed of the 4 ascii char "MTR"
- followed by one byte containing the length of the header in bytes.
- This length includes the 4 first bytes.
- It's then followed by :
- 4 bytes containing the id of the tracker.
- 4 containing the timestamp (format cf get_fattime()) of the start.
- """
- head_length = 4
- head_buffer = file_pointer.read(head_length)
- if head_buffer[0:3] != "MTR":
- raise CorruptedReportException
- header_size = ord(head_buffer[3])
- buffer_body = file_pointer.read(header_size - head_length)
- meteor_id = string_to_meteor_id(buffer_body[0:4])
- try:
- time_stamp = string_to_timestamp(buffer_body[4:8])
- except ValueError:
- raise CorruptedReportException
- return {'meteor_id': meteor_id, 'time_stamp': time_stamp}
- def bin_file_print(file_pointer, output_pointer):
- header_info = extract_header(file_pointer);
- print "File created on %s with tracker %s." % (
- header_info['time_stamp'],
- header_info['meteor_id'])
- for index, data in enumerate(iter(partial(file_pointer.read, 7), '')):
- time_value, x_value, y_value, z_value = convert_sentence(data)
- output_pointer.write(
- '%s %s %s %s\n' % (time_value, x_value, y_value, z_value))
- print "Successfully converted to %s." % output_pointer.name
- args = parser.parse_args()
- bin_file_print(args.data_file[0], args.output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement