-
Notifications
You must be signed in to change notification settings - Fork 1
/
GHOSTDR_tutorial_scripts.py
140 lines (133 loc) · 6.98 KB
/
GHOSTDR_tutorial_scripts.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
from mpl_toolkits.axes_grid1 import make_axes_locatable
from astropy.io import fits
from astropy.visualization import ZScaleInterval
from scipy.interpolate import interp1d
def tryMakePath(path):
try:
os.mkdir(path)
except FileExistsError:
print('Directory {} already exists'.format(path))
else:
print('Directory {} created'.format(path))
def makeDirectoryStructure(DATA_parent, obj_name, readmodes):
"""Create's Fletcher's favourite GHOSTDR directory structure for easy trouble-shooting. Does not add your data"""
dirpath = os.path.join('./', DATA_parent,)
tryMakePath(dirpath)
for i in obj_name:
objpath = os.path.join(dirpath, i)
tryMakePath(objpath)
for j in readmodes:
readmodepath = os.path.join(objpath, j)
tryMakePath(readmodepath)
rawpath = os.path.join(readmodepath, 'raw')
tryMakePath(rawpath)
tryMakePath(os.path.join(readmodepath, 'intermediate'))
tryMakePath(os.path.join(rawpath, 'packed'))
tryMakePath(os.path.join(rawpath, 'obj'))
def show2dCutout(wmin,wmax,path='',file='',flat='',wvl=True,pxl=True, weight=False, camera='blue',debug=False):
orderref = {'blue':35, 'red':34}
mrefdict = {'blue': 80, 'red': 50}
ordermindict = {'blue': 64, 'red': 33}
wavemodfile = {'blue': '/Users/fwaller/Documents/GHOST/GHOSTDR/ghostdr/ghost/lookups/Polyfit/blue/std/220620/wavemod.fits',
'red': '/Users/fwaller/Documents/GHOST/GHOSTDR/ghostdr/ghost/lookups/Polyfit/red/std/220620/wavemod.fits'}
echelle = fits.open(path+file)
xprimes = echelle[1].data.shape[0]
orderwave=[]
m_ref = mrefdict[camera]
wavemodhdu = fits.open(wavemodfile[camera])
wavemod = wavemodhdu[0].data
yprimes = np.arange(echelle[1].data.shape[1])
for i in range(orderref[camera]):
m = i + ordermindict[camera]
poly_w_model = np.poly1d([np.poly1d(wavemod[i,:])((m_ref/m)-1) for i in range(len(wavemod[:,0]))])
wave = poly_w_model(yprimes-(yprimes[-1]+1)//2)
orderwave.append(wave)
# print(orderwave)
ooi=[]
ooilim = {'min':[], 'mid':[], 'max':[], 'num':[]}
for i,a in enumerate(orderwave):
if (a[0] < wmax and a[-1] > wmax): #lower portion is in range
rang = a[(a < wmax) & (a>wmin)]
ooi.append(rang)
ooilim['min'].append(rang[0])
ooilim['mid'].append(rang[len(rang)//2])
ooilim['max'].append(rang[-1])
ooilim['num'].append(i)
elif a[0] > wmin and a[-1] < wmax: # entire order is in range
rang = a
ooi.append(rang)
ooilim['min'].append(rang[0])
ooilim['mid'].append(rang[len(rang)//2])
ooilim['max'].append(rang[-1])
ooilim['num'].append(i)
elif a[0] < wmin and a[-1] > wmin: #upper portion is in range
rang = a[(a > wmin) & (a<wmax)]
ooi.append(rang)
ooilim['min'].append(rang[0])
ooilim['mid'].append(rang[len(rang)//2])
ooilim['max'].append(rang[-1])
ooilim['num'].append(i)
if debug:
print(ooilim)
flatpath = '/Users/fwaller/Documents/GHOST/DATA/BPSCS31082/NORM/'
flatfile = flat
flat = fits.open(flatpath+flatfile)
xmod = flat[4].data
print(camera, 'camera')
print('The wavelengths here are an approxmation at best!!! do not worry if they only vaguely line up.')
if wvl:
wfig = plt.figure(figsize=(15,len(ooi)))
if pxl:
pfig = plt.figure(figsize=(15,len(ooi)))
echelle = fits.open(path+file)
m_ref = mrefdict[camera]
yprimes = np.arange(echelle[1].data.shape[1])
for g,a in enumerate(ooi):
if camera == 'red':
j = -(g+1)
if camera == 'blue':
j = g
k = ooilim['num'][j]
m = k + ordermindict[camera]
poly_x_model = np.poly1d([np.poly1d(xmod[l,:])((m_ref/m)-1) for l in range(len(xmod[:,0]))])
poly_w_model = np.poly1d([np.poly1d(wavemod[n,:])((m_ref/m)-1) for n in range(len(wavemod[:,0]))])
wave = poly_w_model(yprimes-(yprimes[-1]+1)//2)
xx = poly_x_model(yprimes- (yprimes[-1]+1)//2) +xprimes//2
if camera == 'red':
spatmin = xx[wave==ooilim['min'][j]]
spatmax = xx[wave==ooilim['max'][j]]
if camera == 'blue':
spatmin = xx[wave==ooilim['max'][j]]
spatmax = xx[wave==ooilim['min'][j]]
ymin = yprimes[wave==ooilim['min'][j]]
ymax = yprimes[wave==ooilim['max'][j]]
vmin=-1
vmax=6
buf = 50
if weight:
if wvl:
wax=wfig.add_axes([(ooilim['min'][j]-wmin)/(wmax-wmin), float(np.abs(j)/len(ooi)), (ooilim['max'][j]-ooilim['min'][j])/(wmax-wmin), 0.9/float(len(ooi))]) #left bottom width height
wax.imshow(echelle[3].data[0,int(round(spatmin[0]))-buf:int(round(spatmax[0]))+buf,ymin[0]:ymax[0]],origin='lower',vmin=vmin,vmax=vmax,extent=[ooilim['min'][j],ooilim['max'][j],spatmin[0]-buf,spatmax[0]+buf],aspect='auto')
wax.plot(wave[(wave > ooilim['min'][j]) & (wave < ooilim['max'][j])],xx[(wave > ooilim['min'][j]) & (wave < ooilim['max'][j])])
if pxl:
pax=pfig.add_axes([float(ymin/(yprimes[-1]+1)), float(np.abs(j)/len(ooi)), float((ymax-ymin)/(yprimes[-1]+1)), 0.9/float(len(ooi))]) #left bottom width height
pax.imshow(echelle[3].data[0,int(round(spatmin[0]))-buf:int(round(spatmax[0]))+buf,ymin[0]:ymax[0]],origin='lower',vmin=vmin,vmax=vmax,extent=[ymin[0],ymax[0],spatmin[0]-buf,spatmax[0]+buf])
pax.plot(yprimes[(yprimes<ymax) & (yprimes>ymin)],xx[(yprimes<ymax) & (yprimes>ymin)])
else:
if wvl:
wax=wfig.add_axes([(ooilim['min'][j]-wmin)/(wmax-wmin), float(np.abs(j)/len(ooi)), (ooilim['max'][j]-ooilim['min'][j])/(wmax-wmin), 0.9/float(len(ooi))]) #left bottom width height
wax.imshow(echelle[1].data[int(round(spatmin[0]))-buf:int(round(spatmax[0]))+buf,ymin[0]:ymax[0]],origin='lower',vmin=vmin,vmax=vmax,extent=[ooilim['min'][j],ooilim['max'][j],spatmin[0]-buf,spatmax[0]+buf],aspect='auto')
wax.plot(wave[(wave > ooilim['min'][j]) & (wave < ooilim['max'][j])],xx[(wave > ooilim['min'][j]) & (wave < ooilim['max'][j])])
if pxl:
pax=pfig.add_axes([float(ymin/(yprimes[-1]+1)), float(np.abs(j)/len(ooi)), float((ymax-ymin)/(yprimes[-1]+1)), 0.9/float(len(ooi))]) #left bottom width height
pax.imshow(echelle[1].data[int(round(spatmin[0]))-buf:int(round(spatmax[0]))+buf,ymin[0]:ymax[0]],origin='lower',vmin=vmin,vmax=vmax,extent=[ymin[0],ymax[0],spatmin[0]-buf,spatmax[0]+buf])
pax.plot(yprimes[(yprimes<ymax) & (yprimes>ymin)],xx[(yprimes<ymax) & (yprimes>ymin)])
if (pxl==False) & (wvl==False):
print('Error: enable pxl=True or wvl=True')
plt.show()
#return