From 3e6499518fc00e8b75b61da78f7e1621faa7c701 Mon Sep 17 00:00:00 2001
From: Marko Mecina <marko.mecina@univie.ac.at>
Date: Tue, 19 Mar 2024 16:02:26 +0100
Subject: [PATCH] add functions to handle the SMILE SXI bad pixel mask

---
 Ccs/calibrations_SMILE.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/Ccs/calibrations_SMILE.py b/Ccs/calibrations_SMILE.py
index adaaf2d..b607d78 100644
--- a/Ccs/calibrations_SMILE.py
+++ b/Ccs/calibrations_SMILE.py
@@ -716,6 +716,33 @@ def calibrate_ext(adu, signal, exception=False):
     # return adu if not exception else None
 
 
+class BadPixelMask:
+    """
+    Convenience functions for handling the SMILE SXI bad pixel mask stored in MRAM
+    """
+
+    NROWS = 639
+    NCOLS = 384
+
+    @classmethod
+    def from_bytes(cls, buffer):
+        return np.unpackbits(bytearray(buffer)).reshape((cls.NROWS, cls.NCOLS))
+
+    @classmethod
+    def to_bytes(cls, mask: np.ndarray):
+
+        assert isinstance(mask, np.ndarray)
+
+        if mask.size != cls.NROWS * cls.NCOLS:
+            raise ValueError("Mask must be array of size {}, is {}.".format(cls.NROWS * cls.NCOLS, mask.size))
+
+        return bytes(np.packbits(mask))
+
+    @classmethod
+    def gen_mask_array(cls):
+        return np.zeros((cls.NROWS, cls.NCOLS), dtype=int)
+
+
 if __name__ == '__main__':
 
     import matplotlib.pyplot as plt
-- 
GitLab