Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import argparse
  4. import datetime
  5. from functools import partial
  6. import pytz
  7. import sys
  8.  
  9. parser = argparse.ArgumentParser(description='Convert MTR file to new file.')
  10. parser.add_argument(
  11. 'data_file', metavar='input.MTR', type=file, nargs=1,
  12. help='input file')
  13. parser.add_argument(
  14. 'output_file', metavar='output.TXT', type=argparse.FileType('w'),
  15. default=sys.stdout, nargs='?', help='output file')
  16.  
  17. def two_complement_resolve(value, bit_length=10):
  18. limit = 1 << (bit_length - 1)
  19. sub = 1 << bit_length
  20. return (value - sub) if (value > limit) else value
  21.  
  22.  
  23. def convert_sentence(sentence):
  24. # Reminder: the measure is coded on 30 bits.
  25. # 10 MSB represent X value.
  26. # 10 following MSB represent Y value.
  27. # 10 following MSB represent Z value.
  28. # 2 LSB are not used.
  29. # The time value is coded on 26 bits.
  30. # The two MSB come to complete the buffer 4th byte.
  31. # Then the three LSB are stored in bytes 5, 6, 7.
  32. x_value = ((ord(sentence[0]) << 2) | (ord(sentence[1]) >> 6)) & 0x3FF
  33. y_value = ((ord(sentence[1]) << 4) | (ord(sentence[2]) >> 4)) & 0x3FF
  34. z_value = ((ord(sentence[2]) << 6) | (ord(sentence[3]) >> 2)) & 0x3FF
  35. time_value = (
  36. (ord(sentence[3]) << 24)
  37. | (ord(sentence[4]) << 16)
  38. | (ord(sentence[5]) << 8)
  39. | ord(sentence[6])
  40. ) & 0x3FFFFFF
  41. return (
  42. time_value, two_complement_resolve(x_value),
  43. two_complement_resolve(y_value), two_complement_resolve(z_value))
  44.  
  45.  
  46. def string_to_meteor_id(buffer_string):
  47. return format(
  48. ord(buffer_string[0])
  49. | (ord(buffer_string[1]) << 8)
  50. | (ord(buffer_string[2]) << 16)
  51. | (ord(buffer_string[3]) << 24), 'x')
  52.  
  53.  
  54. def string_to_timestamp(buffer_string):
  55. """
  56. Timestamp inherited from fatfs get_fattime()
  57. bit31:25
  58. Year origin from the 1980 (0..127)
  59. bit24:21
  60. Month (1..12)
  61. bit20:16
  62. Day of the month(1..31)
  63. bit15:11
  64. Hour (0..23)
  65. bit10:5
  66. Minute (0..59)
  67. bit4:0
  68. Second / 2 (0..29)
  69. """
  70. second = 2 * (ord(buffer_string[0]) & 0x1F)
  71. minute = (
  72. ((ord(buffer_string[0]) & 0xE0) >> 5)
  73. | ((ord(buffer_string[1]) & 0x7) << 3))
  74. hour = ((ord(buffer_string[1]) & 0xF8) >> 3)
  75. day = ord(buffer_string[2]) & 0x1F
  76. month = (
  77. ((ord(buffer_string[2]) & 0xE0) >> 5)
  78. | ((ord(buffer_string[3]) & 0x1) << 3))
  79. year = ((ord(buffer_string[3]) & 0xFE) >> 1) + 1980
  80. return datetime.datetime(
  81. year, month, day, hour, minute, second, 0, pytz.UTC)
  82.  
  83.  
  84. def extract_header(file_pointer):
  85. """
  86. The header of the .MTR file is composed of the 4 ascii char "MTR"
  87. followed by one byte containing the length of the header in bytes.
  88. This length includes the 4 first bytes.
  89. It's then followed by :
  90. 4 bytes containing the id of the tracker.
  91. 4 containing the timestamp (format cf get_fattime()) of the start.
  92. """
  93. head_length = 4
  94. head_buffer = file_pointer.read(head_length)
  95. if head_buffer[0:3] != "MTR":
  96. raise CorruptedReportException
  97. header_size = ord(head_buffer[3])
  98. buffer_body = file_pointer.read(header_size - head_length)
  99. meteor_id = string_to_meteor_id(buffer_body[0:4])
  100. try:
  101. time_stamp = string_to_timestamp(buffer_body[4:8])
  102. except ValueError:
  103. raise CorruptedReportException
  104.  
  105. return {'meteor_id': meteor_id, 'time_stamp': time_stamp}
  106.  
  107.  
  108. def bin_file_print(file_pointer, output_pointer):
  109. header_info = extract_header(file_pointer);
  110. print "File created on %s with tracker %s." % (
  111. header_info['time_stamp'],
  112. header_info['meteor_id'])
  113. for index, data in enumerate(iter(partial(file_pointer.read, 7), '')):
  114. time_value, x_value, y_value, z_value = convert_sentence(data)
  115. output_pointer.write(
  116. '%s %s %s %s\n' % (time_value, x_value, y_value, z_value))
  117. print "Successfully converted to %s." % output_pointer.name
  118.  
  119. args = parser.parse_args()
  120.  
  121. bin_file_print(args.data_file[0], args.output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement