diff --git a/galvani/BioLogic.py b/galvani/BioLogic.py index 44b1d07..4e6624c 100644 --- a/galvani/BioLogic.py +++ b/galvani/BioLogic.py @@ -86,6 +86,7 @@ def fieldname_to_dtype(fieldname): "/F", "/mF", "/uF", + "/µF", "/C", "/Ohm", ) @@ -230,7 +231,7 @@ def MPTfileCSV(file_or_path): return mpt_csv, comments -VMPmodule_hdr = np.dtype( +VMPmodule_hdr_v1 = np.dtype( [ ("shortname", "S10"), ("longname", "S25"), @@ -240,15 +241,27 @@ VMPmodule_hdr = np.dtype( ] ) +VMPmodule_hdr_v2 = np.dtype( + [ + ("shortname", "S10"), + ("longname", "S25"), + ("max points", " ?? 9: ("Ece/V", "/mA", "/mA", "/V", "/V", "= 11.50 + if hdr_bytes[35:39] == b"\xff\xff\xff\xff": + VMPmodule_hdr = VMPmodule_hdr_v2 + hdr_bytes += fileobj.read(VMPmodule_hdr_v2.itemsize - VMPmodule_hdr_v1.itemsize) + hdr = np.frombuffer(hdr_bytes, dtype=VMPmodule_hdr, count=1) hdr_dict = dict(((n, hdr[n][0]) for n in VMPmodule_hdr.names)) hdr_dict["offset"] = fileobj.tell() @@ -456,7 +498,11 @@ def read_VMP_modules(fileobj, read_module_data=True): current module: %s length read: %d length expected: %d""" - % (hdr_dict["longname"], len(hdr_dict["data"]), hdr_dict["length"]) + % ( + hdr_dict["longname"], + len(hdr_dict["data"]), + hdr_dict["length"], + ) ) yield hdr_dict else: @@ -494,6 +540,8 @@ class MPRfile: raise ValueError("Invalid magic for .mpr file: %s" % magic) modules = list(read_VMP_modules(mpr_file)) + mpr_file.close() # close the mpr.file after reading all of it + self.modules = modules (settings_mod,) = (m for m in modules if m["shortname"] == b"VMP Set ") (data_module,) = (m for m in modules if m["shortname"] == b"VMP data ") @@ -504,15 +552,22 @@ class MPRfile: n_columns = np.frombuffer(data_module["data"][4:5], dtype="u1").item() if data_module["version"] == 0: - column_types = np.frombuffer( - data_module["data"][5:], dtype="u1", count=n_columns - ) - remaining_headers = data_module["data"][5 + n_columns:100] - main_data = data_module["data"][100:] + # If EC-Lab version >= 11.50, column_types is [0 1 0 3 0 174...] instead of [1 3 174...] + if np.frombuffer(data_module["data"][5:6], dtype="u1").item(): + column_types = np.frombuffer(data_module["data"][5:], dtype="u1", count=n_columns) + remaining_headers = data_module["data"][5 + n_columns:100] + main_data = data_module["data"][100:] + else: + column_types = np.frombuffer( + data_module["data"][5:], dtype="u1", count=n_columns * 2 + ) + column_types = column_types[1::2] # suppressing zeros in column types array + # remaining headers should be empty except for bytes 5 + n_columns * 2 + # and 1006 which are sometimes == 1 + remaining_headers = data_module["data"][6 + n_columns * 2:1006] + main_data = data_module["data"][1007:] elif data_module["version"] in [2, 3]: - column_types = np.frombuffer( - data_module["data"][5:], dtype="