diff --git a/Python/DMPFile.py b/Python/DMPFile.py new file mode 100644 index 0000000..9785b98 --- /dev/null +++ b/Python/DMPFile.py @@ -0,0 +1,44 @@ +import numpy as np + +def readDMPFile(fileName): + ''' + Opens and reads DMP file. Returns numpy ndarray. + ''' + + fd = open(fileName, 'rb') + + #datatype is unsigned short + datatype = 'h' + numberOfHeaderValues = 3 + + headerData = np.fromfile(fd, datatype, numberOfHeaderValues) + + imageShape = (headerData[1], headerData[0]) + + imageData = np.fromfile(fd, np.float32, -1) + imageData = imageData.reshape(imageShape) + + return imageData + +def writeDMPFile(fileName, np_array): + ''' + Writes numpy ndarray to DMP file. + ndArray.dtype should be np.float32. If it not, it will be converted before saving. + ''' + + # opens file for writing (binary) + fd = open(fileName, 'wb') + + # create header - must be dtype np.uint16 !!! + width = np.array(np_array.shape[1]) + height = np.array(np_array.shape[0]) + + header = np.array([width, height, 0], np.uint16) + header.tofile(fd) + + # img data/numpy ndarray + # dtype has to be np.float32 + if np_array.dtype != 'float32': + np_array = np_array.astype(np.float32) + + np_array.tofile(fd) diff --git a/Python/README.md b/Python/README.md new file mode 100644 index 0000000..8c4cfcf --- /dev/null +++ b/Python/README.md @@ -0,0 +1,3 @@ +# Reading TOMCAT DMP files with Python + +The python script in this folder reads and writes DMP files.