-
Notifications
You must be signed in to change notification settings - Fork 2
/
fitsTools.py
executable file
·230 lines (191 loc) · 7.34 KB
/
fitsTools.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
import pyfits
import numpy
import stopwatch
import imagetools
def readFITS(fitsPath, use_mask=False, norm=False):
t1 = stopwatch.timer()
t1.start()
print "loading %s..." % fitsPath
hdulist = pyfits.open(fitsPath)
print "load:",t1.elapsed()
t1.start()
# get image data
raw_img_data = hdulist[1].data
if use_mask:
# get and apply mask
img_mask = hdulist[2].data
print "using img_mask"
fitsStats(img_mask)
# where ever the mask is "black" make the input image black aswell
# after poking around the fits files, it looks like the "black" threshold is around 5
maskIdx = numpy.where(img_mask < 5)
raw_img_data[maskIdx] = 0.0
hdulist.close()
# cast into float
raw_img_data = numpy.array(raw_img_data, dtype=numpy.float32)
# shall we cut out the low end?
lowCut = numpy.where(raw_img_data < 0)
raw_img_data[lowCut] = 0.0
if norm:
raw_img_data = raw_img_data/1e5
return raw_img_data.copy()
###########################################################
# asinhScale()
# scales the input image via asinh
# if fname is defined it will save the output image
# if rgb=True it will create an RGB image with
# values>maxCut are red
# values<minCut are blue
# minCut<values>maxcut white
# if fscale=True then the image will be scaled between 0-1
#
###########################################################
def asinhScale(data, nonlin, shift, minCut=None, maxCut=None, fname="", rgb=False, fscale=True):
print "Enter asinhScale.............................."
minX=data.min()
maxX=data.max()
if minCut == None:
minCut=minX
if maxCut == None:
maxCut=maxX
output = numpy.array(data, copy=True)
fact=numpy.arcsinh((maxCut-minCut)/nonlin)
print "factor:",fact
output = output + shift
lowCut = numpy.where(output < minCut)
data_i = numpy.where((output > minCut) & (output < maxCut))
hiCut = numpy.where(output > maxCut)
# Zero out low end to avoid negatives
output[lowCut] = 0.0
# perform asinh and scaling to 0.0-1.0
if fscale is True:
output[data_i] = numpy.arcsinh(((output[data_i])/nonlin))/fact
else:
output[data_i] = numpy.arcsinh(((output[data_i])/nonlin))
# Cut off high end
output[hiCut] = 1.0
if rgb is True:
rgbImg = numpy.zeros((output.shape[0], output.shape[1], 3), dtype=float)
# Make RGB gray scale
rgbImg[data_i[0],data_i[1],0] = output[data_i]
rgbImg[data_i[0],data_i[1],1] = output[data_i]
rgbImg[data_i[0],data_i[1],2] = output[data_i]
# Add in lows in Blue and highs in Red
# lows are set to 0.0 and therefore wont show up.
# to make low end visible uncomment:
#output[lowCut] = 1.0
rgbImg[lowCut[0],lowCut[1],2] = output[lowCut]
rgbImg[hiCut[0],hiCut[1],0] = output[hiCut]
# Write out image
if fname != "":
imagetools.imwrite(rgbImg, fname+"_RGB_"+(str(nonlin)+'_'+str(shift)+'_'+str(minCut)+'_'+str(maxCut))+".png")
print "Leaving asinhScale.........................RGB"
return numpy.array(rgbImg, copy=True)
else:
print "out min/max", output.min(), output.max()
# Write out image
if fname != "":
imagetools.imwrite(output, fname+"_"+(str(nonlin)+'_'+str(shift)+'_'+str(minCut)+'_'+str(maxCut))+".png")
print "Leaving asinhScale.........................GRAY"
return numpy.array(output, copy=True)
def fitsRGBtest():
# loads a fits file and then immediately does an asinh scale and saves an RGB .PNG
DATAPATH = '/home/madmaze/DATA/LSST/FITS'
RESPATH = '/home/madmaze/DATA/LSST/results';
BASE_N = 141
FILENAME = lambda i: '%s/v88827%03d-fz.R22.S11.fits' % (DATAPATH,(BASE_N+i))
xOffset=2000
yOffset=0
chunkSize=1000
yload = lambda i: 1. * readFITS(FILENAME(i))[yOffset:yOffset+chunkSize,xOffset:xOffset+chunkSize]
y = yload(0)
# current best seems 450/-50
#for nonlin in range(0,1000,50):
#for shift in range(-2,2):
# s=shift/10.0
# asinhScale(y, nonlin, -50, minCut=0, maxCut=y.max(),fname="out/testX")
#imagetools.imwrite(img_scale.asinh(y, scale_min=0.0),"out/test"+str(nonlin)+".png")
fitsStats(y)
#asinhScale(y, nonlin, shift, minCut=0, maxCut=40000, fname="test_new", rgb=True)
nonlin = 450
shift=-50
#asinhScale(y, nonlin, shift, 0, y.max(),show=True)
img = asinhScale(y, nonlin, shift, minCut=0, maxCut=40000, fname="test_orig")
fitsStats(img)
#imagetools.imwrite(img, "test_img.png")
def scaleTest():
# loads a fits file and then immediately does an asinh scale and saves an RGB .PNG
DATAPATH = '/home/madmaze/DATA/LSST/FITS'
RESPATH = '/home/madmaze/DATA/LSST/results';
BASE_N = 141
FILENAME = lambda i: '%s/v88827%03d-fz.R22.S11.fits' % (DATAPATH,(BASE_N+i))
xOffset=2000
yOffset=0
chunkSize=1000
yload = lambda i: 1. * readFITS(FILENAME(i), norm=True)[yOffset:yOffset+chunkSize,xOffset:xOffset+chunkSize]
y = yload(0)
print y.max()
fitsStats(y)
#asinhScale(y, nonlin, shift, minCut=0, maxCut=40000, fname="test_new", rgb=True)
nonlin = 450
shift=-50
y=y*1e5
#asinhScale(y, nonlin, shift, 0, y.max(),show=True)
img = asinhScale(y, nonlin, shift, minCut=0,maxCut=40000, fname="test_scaled", rgb=False)
fitsStats(img)
#imagetools.imwrite(img, "test_img_Scaled.png")
def maskTest():
# loads a fits file and then immediately does an asinh scale and saves an RGB .PNG
DATAPATH = '/home/madmaze/DATA/LSST/FITS'
RESPATH = '/home/madmaze/DATA/LSST/results';
BASE_N = 141
FILENAME = lambda i: '%s/v88827%03d-fz.R22.S11.fits' % (DATAPATH,(BASE_N+i))
xOffset=2000
yOffset=0
chunkSize=1000
yload = lambda i: 1. * readFITS(FILENAME(i), use_mask=True, norm=True)[yOffset:yOffset+chunkSize,xOffset:xOffset+chunkSize]
y = yload(0)
print y.max()
fitsStats(y)
#asinhScale(y, nonlin, shift, minCut=0, maxCut=40000, fname="test_new", rgb=True)
nonlin = 450
shift=-50
y=y*1e5
#asinhScale(y, nonlin, shift, 0, y.max(),show=True)
img = asinhScale(y, nonlin, shift, minCut=0,maxCut=40000, fname="test_scaled_masked", rgb=False)
fitsStats(img)
#imagetools.imwrite(img, "test_img_Scaled.png")
# making a histogram, deprecated.
def makeHist(inarr,nbins,outfile):
bins=[]
bins2=[]
maxX=max(inarr)
minX=min(inarr)
r=abs(minX)+abs(maxX)
step=r/nbins
s=minX
while s<maxX:
bins.append(s)
s=s+step
print "sorting into bins.."
for b in bins:
cnt=0
for x in inarr:
if x>= b and x < b+step:
cnt=cnt+1
bins2.append(cnt)
f=open(outfile,"w")
x=0
while x < len(bins):
f.write(str(bins[x])+","+str(bins2[x])+"\n")
x+=1
f.close()
def fitsStats(X):
# this can be commented out for greater speed..
print "IM stats (min/max/ave/median):", X.min(), X.max(), numpy.mean(X), numpy.median(X)
print "-/+:",len(numpy.where(X < 0)[0]),len(numpy.where(X > 0)[0])
if __name__ == "__main__":
#fitsRGBtest()
scaleTest()
maskTest()