read_data.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ###### Read in data from KALYPSO
  2. #from docopt import docopt
  3. import numpy as np
  4. #import matplotlib.pyplot as plt
  5. #from mpl_toolkits.axes_grid1 import make_axes_locatable
  6. #import matplotlib as mpl
  7. #import strip_data
  8. import os
  9. NUMBER_PIXELS = 256
  10. DDR_FILLING = [0xba98,0xfedc]
  11. HEADER_0 = 0xABBA
  12. short_pattern = [0,1,2,3,3,2,1,0]
  13. long_pattern = np.repeat(short_pattern, 32)
  14. #####################################################################################
  15. ## strip_data (filename, number of pixels, orbits[opt.])
  16. ## return: data_array_stripped, data_consistency_is_ok, orbits
  17. #####################################################################################
  18. #####################################################################################
  19. def strip_data(filename, number_pixels, orbits, offset = None):
  20. # np.set_printoptions(threshold='nan')
  21. # data = np.fromfile(str(filename), dtype=np.uint16)
  22. if offset is None:
  23. data = np.memmap( str(filename), dtype=np.dtype('<u2'), mode='r')
  24. else:
  25. data = np.memmap( str(filename), dtype=np.dtype('<u2'), offset = (2*NUMBER_PIXELS*offset),mode='r')
  26. print(data.shape)
  27. if (orbits):
  28. data = data[0:orbits*number_pixels]
  29. print(data.shape)
  30. #####################################################################################
  31. ### Remove Header
  32. #####################################################################################
  33. if (data[0] == 43962):
  34. print("Removing header...")
  35. data = data[16:]
  36. data = np.append(data,[DDR_FILLING,DDR_FILLING,DDR_FILLING,DDR_FILLING,DDR_FILLING,DDR_FILLING,DDR_FILLING,DDR_FILLING])
  37. #####################################################################################
  38. ## Reshape list as an 256*whatever array
  39. #####################################################################################
  40. data = np.reshape(data, (-1,NUMBER_PIXELS))
  41. print(data.shape)
  42. # data = data[0:-8,:]
  43. #####################################################################################
  44. ### Remove Filling at the end
  45. #####################################################################################
  46. lines_with_filling = 0
  47. print("\n***********************")
  48. print("Removing DDR filling...")
  49. while (np.all(data[-1:]==DDR_FILLING) or np.all(data[-1:]==0x00000000)):
  50. data = data[0:-1,:]
  51. lines_with_filling += 1
  52. print("Removed %d bytes with DDR filling" % (lines_with_filling*512))
  53. print(data.shape)
  54. #####################################################################################
  55. ### Data consistency check
  56. #####################################################################################
  57. orbits, pixels = np.shape(data)
  58. high = (data & 0xC000) >> 14
  59. data = (data & 0x3fff)
  60. #print (NUMBER_PIXELS)
  61. #print (data)
  62. print(data.shape)
  63. print("\n***********************")
  64. print("Checking data consistency on %d orbits: " % orbits)
  65. if (not np.any(high - long_pattern)):
  66. print("everything is ok!")
  67. return (data, 1, orbits)
  68. else:
  69. print("error in data consistency!")
  70. #sys.exit("Error in data Consistency!!")
  71. #print (high - long_pattern)
  72. #print (np.where((high - long_pattern) != 0)[0])
  73. return (data, 1, orbits)
  74. strip_data('f12345_2020-02-12T11h45m01s_Si01_m.bin',256,1000)