offline_check.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. '''
  3. Check a set of terrain files for corruption
  4. '''
  5. import os
  6. #from multiprocessing import Pool
  7. from multiprocessing.pool import ThreadPool
  8. import argparse
  9. import time
  10. import gzip
  11. import shutil
  12. import struct
  13. import crc16
  14. from terrain_gen import TERRAIN_GRID_BLOCK_SIZE_Y, east_blocks, IO_BLOCK_SIZE, TERRAIN_GRID_FORMAT_VERSION, GridBlock
  15. # IO block size is 2048
  16. # CRC the last 1821 bytes of this
  17. # Last 22 bytes is the header
  18. def check_filled(block, lat_int, lon_int, grid_spacing):
  19. '''check a block for validity'''
  20. if len(block) != IO_BLOCK_SIZE:
  21. print("Bad size {0} of {1}".format(len(block), IO_BLOCK_SIZE))
  22. return False
  23. (bitmap, lat, lon, crc, version, spacing) = struct.unpack("<QiiHHH", block[:22])
  24. if (version != TERRAIN_GRID_FORMAT_VERSION or
  25. abs(lat_int - (lat/1E7)) > 2 or
  26. abs(lon_int - (lon/1E7)) > 2 or
  27. spacing != 100 or
  28. bitmap != (1<<56)-1):
  29. print("Bad fields")
  30. return False
  31. block = block[:16] + struct.pack("<H", 0) + block[18:]
  32. crc2 = crc16.crc16xmodem(block[:1821])
  33. if crc2 != crc:
  34. print("Bad CRC")
  35. return False
  36. return True
  37. if __name__ == '__main__':
  38. # Create the parser
  39. parser = argparse.ArgumentParser(description='ArduPilot Terrain DAT file generator')
  40. # Add the arguments
  41. # Folder to store processed DAT files
  42. parser.add_argument('-folder', action="store", dest="folder", default="processedTerrain")
  43. args = parser.parse_args()
  44. targetFolder = os.path.join(os.getcwd(), args.folder)
  45. grid_spacing = 100
  46. #for each file in folder
  47. for file in os.listdir(targetFolder):
  48. if file.endswith("DAT.gz"):
  49. # It's a compressed tile
  50. # 1. Check it's a valid gzip
  51. tile = None
  52. try:
  53. lat_int = int(os.path.basename(file)[1:3])
  54. if os.path.basename(file)[0:1] == "S":
  55. lat_int = -lat_int
  56. lon_int = int(os.path.basename(file)[4:7])
  57. if os.path.basename(file)[3:4] == "W":
  58. lon_int = -lon_int
  59. with gzip.open(os.path.join(targetFolder, file), 'rb') as f:
  60. tile = f.read()
  61. print("Checking {0}, {1}".format(lat_int, lon_int))
  62. except:
  63. print("Bad gzip: " + file)
  64. # 2. Is it a valid dat file?
  65. if (tile):
  66. total_blocks = east_blocks(lat_int*1e7, lon_int*1e7, grid_spacing) * TERRAIN_GRID_BLOCK_SIZE_Y
  67. # 2a. Are the correct number of blocks present?
  68. # TODO: Why is there an extra 1821 bytes at the end on the file??? (2048-1821 = 227)
  69. if (len(tile)+227) != (total_blocks * IO_BLOCK_SIZE):
  70. print("Bad number of blocks: {0}, {1} vs {2}".format(file, len(tile), total_blocks * IO_BLOCK_SIZE))
  71. # 2b. Does each block have the correct CRC and fields?
  72. for blocknum in range(total_blocks):
  73. block = tile[(blocknum * IO_BLOCK_SIZE):((blocknum + 1)* IO_BLOCK_SIZE)]
  74. if not check_filled(block, lat_int, lon_int, 100):
  75. print("Bad data in block {0} of {1}".format(blocknum, total_blocks))
  76. else:
  77. print("Bad tile: " + file)