File size: 2,288 Bytes
246fca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
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)