Coverage for src/susi/raw2fits/fits_handling.py: 82%

22 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2025-08-11 10:03 +0000

1""" 

2Functions for handling FITS files 

3 

4Authors: K. Heerlein, A. Feller 

5""" 

6 

7import os 

8from astropy.io import fits 

9 

10 

11def image2fits(image): 

12 """Create primary HDU with image data""" 

13 hdu1 = fits.PrimaryHDU(image) 

14 hdu1.header["BSCALE"] = (1, "default scaling factor") 

15 # add the primary HDU to the HDU list 

16 return fits.HDUList(hdu1) 

17 

18 

19def save_fits(file, file_name, overwrite=False): 

20 """Write FITS file""" 

21 directory = os.path.dirname(file_name) 

22 if not os.path.exists(directory): 

23 os.makedirs(directory) 

24 file.writeto(file_name, overwrite=overwrite) 

25 

26 

27def add_header_entry(hdr, pos, entry): 

28 """ 

29 Adds an entry to the given header (in place). 

30 If item exists it will overwrite value and comment. 

31 """ 

32 if entry[0] == '': 

33 hdr.insert(pos, entry) 

34 pos = pos + 1 

35 elif entry[0] in hdr: 

36 if len(entry) == 3: 

37 hdr[entry[0]] = '{:};{:}'.format(entry[1], entry[2]) 

38 else: 

39 hdr[entry[0]] = entry[1] 

40 else: 

41 hdr.insert(pos, entry) 

42 pos = pos + 1 

43 return pos