GBI-16-2D-Legacy / raw_to_fits.py
jbloom
initial commit
d5fc6a6
"""
Converts RAW files to FITS files
adapted from:
https://github.com/OscarMaireles/experiment-notebook/tree/398832acf73c31edb97c3c5055a6394d89ed96a3/plugins
plugins/plugin_fits/raw_to_fits.py
pip install enb astropy
"""
import os
import numpy as np
import glob
from pathlib import Path
from astropy.io import fits
import enb.sets as sets
class RawToFits(sets.FileVersionTable, sets.FilePropertiesTable):
def __init__(self, original_base_dir, version_base_dir):
super().__init__(
original_base_dir=original_base_dir,
version_base_dir=version_base_dir,
original_properties_table=sets.FilePropertiesTable(),
version_name=self.version_name)
def Version(input_path):
RAWS=glob.glob(str(input_path / "*.raw"))
for i in range(len(RAWS)):
file=RAWS[i]
fname = file.split("/")[-1]
print(file)
params = fname[0:-4].split("-")
dimensions=params[-1].split('x')
telescope = file.split("/")[2]
frames=int(dimensions[-3])
columns=int(dimensions[-2])
rows=int(dimensions[-1])
astype=params[-2]
swap = False
if astype == "u16be":
astype = "uint16"
swap = True
else:
raise ValueError(f"Unknown data type: {astype}")
name=params[0]
if os.path.exists(f'./fits_data/{name}.fits'):
continue
img=np.fromfile(open(file), dtype = f'{astype}', count = -1)
if swap:
img.byteswap(inplace=True)
array=np.reshape(img,(frames,columns,rows))
hdr = fits.Header()
hdr['NAME'] = name
hdr['TELESCOP'] = telescope
hdr['COMMENT'] = "made from RAW data from Maireles-Gonzalez et al."
hdu = fits.PrimaryHDU(array, header=hdr)
hdu.writeto(f'./fits_data/{telescope}/{name}.fits', overwrite=True)
if __name__ == '__main__':
for telescope in ["INT", "WHT", "TJO", "LCO", "JKT"]:
print(f"Converting {telescope}...")
input_path=Path(f'./data/RAW/{telescope}/u16be/')
RawToFits.Version(input_path)