def load(fname, sme): """ Load the SME Structure from disk Parameters ---------- fname : str file to load sme : SME_Structure empty sme structure with default values set Returns ------- sme : SME_Structure loaded sme structure """ try: ff = FlexFile.read(fname) sme = from_flex(ff, sme) ff.close() return sme except Exception as ex: logger.error(ex) try: sme = load_v1(fname, sme) except: raise ex return sme
def test_read_write(tmp_fname): file = FlexFile() file.header["Hello"] = "World" file.write(tmp_fname) del file f2 = FlexFile.read(tmp_fname) assert isinstance(f2.header, dict) assert f2.header["Hello"] == "World"
def test_header_numpy_arrays(tmp_fname): file = FlexFile() file.header["Hello"] = np.ones(3) file.write(tmp_fname) del file f2 = FlexFile.read(tmp_fname) assert isinstance(f2.header, dict) assert np.all(np.asarray(f2.header["Hello"]) == 1)
def test_empty_data(tmp_fname): file = FlexFile() ext = BinaryDataExtension(data=[]) file.extensions["img"] = ext file.write(tmp_fname) del file f2 = FlexFile.read(tmp_fname) assert f2["img"].data.size == 0
def test_read_write_multi(tmp_fname): file = FlexFile() ext = MultipleDataExtension(data={"bla": np.zeros(10)}) file.extensions["img"] = ext file.write(tmp_fname) del file f2 = FlexFile.read(tmp_fname) assert f2["img"].data["bla"].size == 10 assert np.all(f2["img"].data["bla"] == 0)
def test_read_write(tmp_fname): file = FlexFile() ext = BinaryDataExtension(data=np.zeros(10)) file.extensions["img"] = ext file.write(tmp_fname) del file f2 = FlexFile.read(tmp_fname) assert f2["img"].data.size == 10 assert np.all(f2["img"].data == 0)
def test_read_write(tableclass, tmp_fname, table): file = FlexFile() ext = tableclass(data=table) file.extensions["tab"] = ext file.write(tmp_fname) del file f2 = FlexFile.read(tmp_fname) assert f2["tab"].data.size == 10 * 2 assert np.all(f2["tab"].data == 1) assert len(f2["tab"].data.columns) == 2 assert "A" in f2["tab"].data.columns assert "B" in f2["tab"].data.columns
def read(cls, filename, format=None): if format is None: format = cls.determine_filetype_based_on_filename(filename) if format == "flex": ff = FlexFile.read(filename) spec = ff["spectrum"] return spec elif format == "fits": hdulist = fits.open(filename) spec = cls.read_fits_hdu(hdulist[1]) return spec elif format == "npz": data = np.load(filename, allow_pickle=True) data = {k: v[()] for k, v in data.items()} spec = cls.from_dict(data) return spec else: raise ValueError( f"Format not understood expected one of ('flex', 'fits') but got {format}" )
file.header["np_str"] = np.str("World") # FITS can handle either long header keys, or long entries # But not both at the same time... file.header["really_long_entry"] = "bla" file.header["really_l"] = "bla" * 100 file.header["really_long_text_with_HIERARCH"] = "bla" * 100 file.extensions["np"] = ext file.extensions["tab"] = tab file.extensions["bin"] = ext_bin d = file.to_json() d2 = FlexFile.from_json(d) d = file.to_json("test.json") d2 = FlexFile.from_json("test.json") d = file.to_fits("test.fits", overwrite=True) d2 = FlexFile.from_fits("test.fits") file.write("test.flex") f2 = FlexFile.read("test.flex") f2["np"].data["data"][:5] = 1 print(f2["np"].data) del f2 f2 = FlexFile.read("test.flex") print(f2["np"].data)
def load(self, filename=None): if filename is None: filename = self.savefilename ff = FlexFile.read(filename) return ff.header