offline_check.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. # Actual size is 1821 bytes
  17. # Last 227 bytes is filling
  18. def check_filled(block, lat_int, lon_int, grid_spacing):
  19. '''check a block for validity'''
  20. if len(block) != IO_BLOCK_SIZE - 227:
  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. # There is an extra 1821 bytes at the end on the file (2048-1821 = 227), as the
  69. # terrain blocks only take up 1821 bytes.
  70. if (len(tile)+227) != (total_blocks * IO_BLOCK_SIZE):
  71. print("Bad number of blocks: {0}, {1} vs {2}".format(file, len(tile), total_blocks * IO_BLOCK_SIZE))
  72. # 2b. Does each block have the correct CRC and fields?
  73. for blocknum in range(total_blocks):
  74. block = tile[(blocknum * IO_BLOCK_SIZE):((blocknum + 1)* IO_BLOCK_SIZE)-227]
  75. if not check_filled(block, lat_int, lon_int, 100):
  76. print("Bad data in block {0} of {1}".format(blocknum, total_blocks))
  77. else:
  78. print("Bad tile: " + file)
  79. print("Done!")