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.

32 lines
1.0 KiB

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