You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.1 KiB

  1. import re
  2. from nd2reader.exc import InvalidVersionError
  3. def get_version(filename):
  4. """
  5. Determines what version the ND2 is.
  6. :param filename: the path (absolute or relative) to the ND2
  7. :type filename: str
  8. """
  9. with open(filename, 'rb') as f:
  10. # the first 16 bytes seem to have no meaning, so we skip them
  11. f.seek(16)
  12. # the next 38 bytes contain the string that we want to parse. Unlike most of the ND2, this is in UTF-8
  13. data = f.read(38).decode("utf8")
  14. return parse_version(data)
  15. def parse_version(data):
  16. """
  17. Parses a string with the version data in it.
  18. :param data: the 19th through 54th byte of the ND2, representing the version
  19. :type data: unicode
  20. """
  21. match = re.search(r"""^ND2 FILE SIGNATURE CHUNK NAME01!Ver(?P<major>\d)\.(?P<minor>\d)$""", data)
  22. if match:
  23. # We haven't seen a lot of ND2s but the ones we have seen conform to this
  24. return int(match.group('major')), int(match.group('minor'))
  25. raise InvalidVersionError("The version of the ND2 you specified is not supported.")