Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

utils.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import sys
  2. import argparse
  3. def reduce_file_size(filename, new_filename, n_packages, header_size=0, packet_size=32):
  4. """
  5. Create a smaller version of a binary file containing a defined number of data packages
  6. The file structure has to consist of a header (header_size) and multiple data packages of
  7. size packet_size. This preserves the header and only reduces the number of data packages.
  8. Parameters
  9. ----------
  10. filename:
  11. The source filename to load from.
  12. new_filename:
  13. The target filename to save to. Will be overwritten if it exists.
  14. n_packages:
  15. The number of packages to preserve in the file
  16. header_size:
  17. The size of the file header in byte. Default 0
  18. packet_size:
  19. The size of a single data package in byte. Default 32
  20. Returns
  21. -------
  22. """
  23. with open(filename, 'rb') as f:
  24. data = f.read(header_size + n_packages * packet_size)
  25. with open(new_filename, 'wb') as f:
  26. f.write(data)
  27. param_help = """
  28. tdt-tev': {'header_size': 0, 'packet_size': 8}, # uint8
  29. 'tdt-tsq': {'header_size': 0, 'packet_size': 40*8},
  30. 'tdt-sev': {'header_size': 0, 'packet_size': 8}, # uint8
  31. 'edf': {'header_size': 256 (number_of_signals + 1), # 256 byte per channel and header
  32. 'packet_size': number_of_signals * samples_in_datarecord * 2}} # int16
  33. """
  34. if __name__ == '__main__':
  35. parser = argparse.ArgumentParser(description='Bit-based file manipulation')
  36. subparsers = parser.add_subparsers(help='cut file', dest='subparser_name')
  37. # create the parser for the "cut" command
  38. parser_cut = subparsers.add_parser('cut', help='shorten file to limited bits')
  39. parser_cut.add_argument('filename', type=str, help='name of the file to load')
  40. parser_cut.add_argument('new_filename', type=str, help='name of the file to save')
  41. parser_cut.add_argument('n_packages', type=int, help='number of packages to preserve')
  42. parser_cut.add_argument('--header_size', metavar='header_size', type=int, nargs=1,
  43. help='the size of the file header in bits')
  44. parser_cut.add_argument('--packet_size', metavar='packet_size', type=int, nargs=1,
  45. help='the size of the data packets in bits')
  46. # add parser for printing help options for parameters
  47. parser.add_argument('--parameter_help', help='show suggested parameters', action='store_true')
  48. args = parser.parse_args()
  49. if args.parameter_help:
  50. print(param_help)
  51. elif args.subparser_name == 'cut':
  52. kwargs = {k: getattr(args, k)[0] for k in ['header_size', 'packet_size'] if k in args}
  53. reduce_file_size(filename=args.filename, new_filename=args.new_filename,
  54. n_packages=args.n_packages, **kwargs)