-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreGUI.py
More file actions
195 lines (171 loc) · 9.59 KB
/
preGUI.py
File metadata and controls
195 lines (171 loc) · 9.59 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import matplotlib
matplotlib.use('agg')
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from shutil import copyfile
import glob, re, urllib, PIL, os, astropy.stats
from PIL import Image, ImageDraw
#path to data directories containing 'source data' from online database for each target
pa = raw_input('Enter path to data directory (Ex- C:/Users/Claire/RoboAO/Data/):')
programnum = raw_input('Enter Program Number (Ex- 3):')
path = pa+programnum+'_*'
nwfl = pa+'preGUI'
nlist = [s for s in os.listdir(pa) if s.startswith(programnum+'_')] #make list of directory path
names = sorted([i.split(".")[0] for i in (nlist[0::1])]) #Create list of object names
cct = 0.10 #core cut
if not os.path.exists(nwfl):
os.mkdir(nwfl)
arcssp = 1.5 #arc seconds across total image for fits collage
sp = arcssp*(1000./(17.4*2.)) #converting arcsec to pixels on either side of target
trgtlst = sorted(glob.glob(path))
k=0 #Make sure all plots are seperate
m=1 #keep track of loop
for j in trgtlst: #loop over every target directory
print 'Working on: ' + j.split("/")[-1]+', '+str(m)+'/'+str(len(trgtlst)) #print current target and progress
if glob.glob(j+directory+'/strehl/*.txt') == []:
m+=1
print "no Strehl file"
continue
#ONLY LOOK AT OBJECTS WITH CORE >= 0.1
stinfo = sorted(glob.glob(j + '/automated/strehl/*.txt'))
with open(stinfo[0], 'r') as st:
tinf = st.readlines()[1].split() #target info
if float(tinf[2])>=cct:
ntd = nwfl+'/'+names[m-1] #new target directory
if not os.path.exists(ntd):
os.mkdir(ntd)
print j.split("/")[-1]+' has passed cut'
if float(tinf[2])<cct:
m+=1
continue
#LOADING FILES
fts = j+'/automated/100p.fits' #100p fits file
ftsim= fits.getdata(fts) #load fits file and store as array
old = np.seterr(invalid='ignore') #ignore invalid values for taking log in next line
ftslg = np.log(ftsim+.4) #for log plots
ftslgn = np.log(ftsim+50)
np.seterr(**old) #restore settings to not ignore invalid values
psf = fits.getdata((sorted(glob.glob(j+'/automated/pca/*.fits')))[0]) #load psf fits file and store as array
contcurv = sorted(glob.glob(j+'/automated/pca/*curve.png'))
copyfile(contcurv[0], ntd+'/contrast_curve.png')
#CREATE ONE TEXT FILE FOR GUI
tinfr = [] #Blank array to store target info
for i in tinf:
if i != tinf[-1] and i != tinf[-2]: #Remove tag and FWHM, last two items in strehl txt
tinfr.append(round(float(i), 2)) #round to two sf, convert back to string
hdulist = fits.open(fts)
tinfr.append(hdulist[0].header['MAGNITUD']) #Get object magnitude from header
np.savetxt(ntd+'/info4GUI.txt', tinfr)
xpix = int(tinfr[1]) #get pixel coordinates of target
ypix = int(tinfr[0])
xpsf = 100 #pixel coordinates of target for psf images
ypsf = 100 #..since psf has target centered
#PNG FROM FITS FOR POSITION FINDER
fig = plt.figure(k, frameon=False)
fig.set_size_inches(10,10)
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.imshow(ftslg, cmap='gray', vmin=-2.e0, vmax=2.e0) #log plot, vmin and max to adjust scaling
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.plot(xpix-220, ypix, 's', marker='_', mew=4, ms=20, color='lawngreen') #mark target's position by pixel coordinates
plt.plot(xpix+220, ypix, 's', marker='_', mew=4, ms=20, color='lawngreen') #(position, marker, thicknes, length, color)
plt.savefig(ntd+'/locda.png', dpi=100, bbox_inches='tight', pad_inches=0) #save plot
im = PIL.Image.open(ntd+'/locda.png') #reopen image to edit with pil
im = im.convert("RGB")
im = im.transpose(PIL.Image.FLIP_TOP_BOTTOM)
im.save(ntd+'/locda.png', "PNG")
im.close()
#IMAGE FROM DSS DATABASE
p=fits.open(fts) #load fits file
ra = p[0].header['OBJRA'].replace('h',':').replace('m', ':').replace('s','') #find ra and dec in header
dec = p[0].header['OBJDEC'].replace('d',':').replace('m', ':').replace('s','')
#url to image location, h & w are in arc minutes, Fov=roboaoFov*2.5
DSSad = "http://archive.stsci.edu/cgi-bin/dss_search?v=poss2ukstu_red&r="+ra+"&d="+dec+"&e=J2000&h=1.4848&w=1.4848&f=gif&c=none&fov=NONE&v3="
urllib.urlretrieve(DSSad, j+'/locdb.png') #save dss image as png
im = PIL.Image.open(j+'/locdb.png') #reopen image to edit with pil
im = im.convert("RGB")
im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT) #flip image to match roboao data
draw = ImageDraw.Draw(im)
cx = im.size[0]/2 #coordinates of image's center
cy = im.size[1]/2
draw.line((cx-10,cy,cx-7,cy),fill=(50, 205, 50)) #mark image center (target)
draw.line((cx+10,cy,cx+7,cy),fill=(50, 205, 50))
draw.rectangle([cx*3/5, cy*3/5, 7*cx/5, 7*cy/5], outline=(52, 40, 44)) #draw square around roboao field (2/5 dimnsns)
del draw
im.save(ntd+'/locdb.png', "PNG")
im.close()
#FITS COLLAGE (4 pngs)
fig = plt.figure(k+1, frameon=False)
fig.set_size_inches(10,10)
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.imshow(ftsim, cmap='gray') #black and white original fits
plt.xlim(xpix-sp, xpix+sp) #crop plot based on arcsec specifications
plt.ylim(ypix-sp, ypix+sp)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.savefig(ntd+'/fc1.png', dpi=100, bbox_inches='tight', pad_inches=0)
fig = plt.figure(k+2, frameon=False)
fig.set_size_inches(10,10)
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.imshow(-ftsim, cmap='RdYlBu') #color scale original fits
plt.xlim(xpix-sp, xpix+sp) #crop plot based on arcsec specifications
plt.ylim(ypix-sp, ypix+sp)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.savefig(ntd+'/fc2.png', dpi=100, bbox_inches='tight', pad_inches=0)
fig = plt.figure(k+6, frameon=False)
fig.set_size_inches(10,10)
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.imshow(psf, cmap='gray') #black and white psf
plt.xlim(xpsf-sp, xpsf+sp)
plt.ylim(ypsf-sp, ypsf+sp)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.savefig(ntd+'/fc3.png', dpi=100, bbox_inches='tight', pad_inches=0)
fig = plt.figure(k+7, frameon=False)
fig.set_size_inches(10,10)
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.imshow(psf, cmap='viridis') #color psf
plt.xlim(xpsf-sp, xpsf+sp)
plt.ylim(ypsf-sp, ypsf+sp)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.savefig(ntd+'/fc4.png', dpi=100, bbox_inches='tight', pad_inches=0)
#Larger Field Image (with bright pixels 'cut out')
ftsimcut = ftsim.copy()
fig = plt.figure(k+5, frameon=False)
fig.set_size_inches(10,10)
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.imshow(ftslgn, cmap='gist_ncar') #color scaling
plt.xlim(xpix-232, xpix+232) #Crop out to 8 arc seconds (total width of field): 8*1000/(17.4*2)
plt.ylim(ypix-232, ypix+232)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.savefig(ntd+'/flft.png', dpi=100, bbox_inches='tight', pad_inches=0)
#CREATE COLLAGE
ftslst=sorted(glob.glob(ntd+'/fc*.png')) #open 4 pngs for collage
images = map(Image.open, ftslst)
widths, heights = zip(*(i.size for i in images)) #get image dimmensions
total_width = sum(widths) #find total dimmensions of final collage
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height)) #create new image
new_im.paste(images[0], (0,0)) #add all 4 pngs to new image
new_im.paste(images[1], ((total_width/4),0))
new_im.paste(images[2], ((total_width/2),0))
new_im.paste(images[3], ((total_width*3/4),0))
new_im.save(ntd+'/ffc.png', "PNG") #save new image, the final collage
new_im.close()
os.remove(ntd+'/fc1.png') #delete all 4 images used to make collage
os.remove(ntd+'/fc2.png')
os.remove(ntd+'/fc3.png')
os.remove(ntd+'/fc4.png')
m+=1
k+=10 #keep plots for each target seperate
print 'Done'