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.

31 lines
998 B

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