-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreducescience.py
85 lines (77 loc) · 2.66 KB
/
reducescience.py
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
75
76
77
78
79
80
81
82
83
84
import numpy as np
import glob as glob
from astropy.io import fits
# This is a python program to reduce the science frames
print('Script running')
ysize = 2102
xsize = 500
BIASframe = fits.open('../rawbias/BIAS.fits')
BIAS = np.array(BIASframe[0].data)
FLATframe = fits.open('../rawflats/Flat.fits')
FLAT = np.array(FLATframe[0].data)
rawimages = sorted(glob.glob("crr*.fits"))
print('Number of raw images:',len(rawimages))
outnames = ['sub1.fits','sub2.fits']
centers = [247, 269]
#Read the raw file, subtract overscan, bias and divide by the flat
for n in range(0,2):
spec = fits.open(rawimages[n])
print('Info on file:')
print(spec.info())
specdata = np.array(spec[1].data)
mean = np.mean(specdata[2066:ysize-5,0:xsize-1])
specdata = specdata - mean
print('Subtracted the median value of the overscan :',mean)
specdata = (specdata-BIAS)/FLAT
hdr = spec[0].header
specdata1 = specdata[50:1750,centers[n]-100:centers[n]+100]
print(outnames[n])
fits.writeto(outnames[n],specdata1,hdr,overwrite=True)
#Add and rotate
sub1 = fits.open(outnames[0])
sub2 = fits.open(outnames[1])
sum = sub1[0].data+sub2[0].data
rot = np.rot90(sum, k=3)
hduout = fits.PrimaryHDU(rot)
hduout.header.extend(hdr, strip=True, update=True,
update_first=False, useblanks=True, bottom=False)
hduout.header['DISPAXIS'] = 1
hduout.header['NEXP'] = len(rawimages)
hduout.header['CRVAL1'] = 1
hduout.header['CRVAL2'] = 1
hduout.header['CRPIX1'] = 1
hduout.header['CRPIX2'] = 1
hduout.header['CRVAL1'] = 1
hduout.header['CRVAL1'] = 1
hduout.header['CDELT1'] = 1
hduout.header['CDELT2'] = 1
hduout.writeto("../obj.fits", overwrite=True)
#Arcframe
arclist = glob.glob("../rawarcs/*.fits")
specdata = np.zeros((ysize,xsize),float)
for frames in arclist:
spec = fits.open(str(frames))
data = spec[1].data
if ((len(data[0,:]) != xsize) or (len(data[:,0]) != ysize)): sys.exit(frame + ' has wrong image size')
specdata += data
mean = np.mean(specdata[2066:ysize-5,0:xsize-1])
specdata = specdata - mean
print('Subtracted the median value of the overscan :',mean)
specdata = (specdata-BIAS)/FLAT
hdr = spec[0].header
center = int((centers[0])/1.)
specdata1 = specdata[50:1750,center-100:center+100]
rot = np.rot90(specdata1, k=3)
hduout = fits.PrimaryHDU(rot)
hduout.header.extend(hdr, strip=True, update=True,
update_first=False, useblanks=True, bottom=False)
hduout.header['DISPAXIS'] = 1
hduout.header['CRVAL1'] = 1
hduout.header['CRVAL2'] = 1
hduout.header['CRPIX1'] = 1
hduout.header['CRPIX2'] = 1
hduout.header['CRVAL1'] = 1
hduout.header['CRVAL1'] = 1
hduout.header['CDELT1'] = 1
hduout.header['CDELT2'] = 1
hduout.writeto("../arcsub.fits", overwrite=True)