/
rellipse_photometry.py
441 lines (382 loc) · 16.2 KB
/
rellipse_photometry.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#import Interpol
#import getImages
import pyfits
import math
import os
import string
import gzip
import csv
import utils
import inpaint
from astLib import astWCS
import numpy as np
import sdss_photo_check as sdss
import imtools
import plot_survey as plot
#import photometry as phot
import readAtlas
import ellipse
import db
import getTSFieldParameters
import plotGrowthCurve
from math import isnan
import scipy.misc
import numpy.lib.recfunctions
import sys
class GalaxyParameters:
@staticmethod
def SDSS(listFile, ID):
ret = GalaxyParameters()
CALIFAID_col = 0
ra_col = 1
dec_col = 2
run_col = 7
rerun_col = 8
camcol_col = 9
field_col = 10
with open(listFile, 'rb') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
ret.CALIFAID = string.strip(mycsv[ID][CALIFAID_col])
ret.ra = string.strip(mycsv[ID][ra_col])
ret.dec = string.strip(mycsv[ID][dec_col])
ret.run = string.strip(mycsv[ID][run_col])
ret.rerun = string.strip(mycsv[ID][rerun_col])
ret.camcol = string.strip(mycsv[ID][camcol_col])
ret.field = string.strip(mycsv[ID][field_col])
ret.runstr = utils.run2string(ret.run)
ret.field_str = utils.field2string(ret.field)
return ret
@staticmethod
def getSDSSUrl(listFile, dataDir, ID):
camcol = GalaxyParameters.SDSS(listFile, ID).camcol
field = GalaxyParameters.SDSS(listFile, ID).field
field_str = GalaxyParameters.SDSS(listFile, ID).field_str
runstr = GalaxyParameters.SDSS(listFile, ID).runstr
band = setBand()
fpCFile = dataDir+'/SDSS/'+band+'/fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fit.gz'
return fpCFile
@staticmethod
def getFilledUrl(listFile, dataDir, ID):
camcol = GalaxyParameters.SDSS(listFile, ID).camcol
field = GalaxyParameters.SDSS(listFile, ID).field
field_str = GalaxyParameters.SDSS(listFile, ID).field_str
runstr = GalaxyParameters.SDSS(listFile, ID).runstr
band = setBand()
dupeList = [162, 164, 249, 267, 319, 437, 445, 464, 476, 477, 480, 487, 498, 511, 537, 570, 598, 616, 634, 701, 767, 883, 939]
if band == 'r':
fpCFile = dataDir+'/filled2/fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fits'
if (ID +1) in dupeList:
fpCFile = dataDir+'/filled3/fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fits'
else:
if(ID + 1) in dupeList:
fpCFile = dataDir+'/filled_'+band+'/fpC-'+runstr+'-'+band+camcol+'-'+field_str+'.fitsB'
else:
fpCFile = fpCFile+'B'
return fpCFile
@staticmethod
def getMaskUrl(listFile, dataDir, simpleFile, ID):
NedName = GalaxyParameters.getNedName(listFile, simpleFile, ID).NedName
print NedName
maskFile = dataDir+'/MASKS/'+NedName+'_mask_r.fits'
return maskFile
@staticmethod
def getNedName(listFile, simpleFile, ID):
ret = GalaxyParameters()
with open(simpleFile, 'rb') as f:
NEDNAME_col = 2
mycsv = csv.reader(f)
mycsv = list(mycsv)
ret.NedName = string.strip(mycsv[ID][NEDNAME_col])
return ret
#a set of methods for swapping between pixel coordinates and ra, dec
class Astrometry():
@staticmethod
def getCenterCoords(listFile, ID):
centerCoords = (GalaxyParameters.SDSS(listFile, ID).ra, GalaxyParameters.SDSS(listFile, ID).dec)
return centerCoords
@staticmethod
def getPixelCoords(listFile, ID, dataDir):
WCS=astWCS.WCS(GalaxyParameters.getSDSSUrl(listFile, dataDir, ID)) #changed -- was filledUrl. I don't write coords to my masks..
centerCoords = Astrometry.getCenterCoords(listFile, ID)
print 'centerCoords', centerCoords
pixelCoords = WCS.wcs2pix(centerCoords[0], centerCoords[1])
print 'pixCoords', pixelCoords
out = [ID, centerCoords[0], centerCoords[1], pixelCoords[0], pixelCoords[1]]
#utils.writeOut(out, 'coords.csv')
return (pixelCoords[1], pixelCoords[0]) #y -- first, x axis -- second
@staticmethod
def distance2origin(y, x, center):
deltaY = y - center[0]
deltaX = x - center[1]
r = (deltaY**2 + deltaX**2)
return r
@staticmethod
def makeDistanceArray(img, center):
distances = np.zeros(img.shape)
print Astrometry.distance2origin(0,0, center), 'squared distance to origin'
for i in range(0, img.shape[0]):
for j in range(0, img.shape[1]):
distances[i,j] = Astrometry.distance2origin(i,j, center)
return np.round(np.sqrt(distances), 0)
class Photometry():
pixelScale = 0.396
iso25D = 40 / 0.396
@staticmethod
def getCenter(listFile, i, dataDir):
ra = Astrometry.getCenterCoords(listFile, i)[0]
dec = Astrometry.getCenterCoords(listFile, i)[1]
return Astrometry.getPixelCoords(listFile, i, dataDir)
@staticmethod
def findClosestEdge(distances, center):
#finds the closest distance from the center to the edge. Used for sky gradient calculation, as we want to avoid using a small number of pixels in a ring.
#works by finding minimum [index] distance to one of the edges of array.
maxy = distances.shape[0]
maxx = distances.shape[1]
print center, maxy, maxx
yUp = center[0]
yDown = maxy - center[0]
xLeft = center[1]
xRight = maxx - center[1]
return int(math.floor(min(yUp, yDown, xLeft, xRight)))
@staticmethod
def createDistanceArray(listFile, i, dataDir):
center = Photometry.getCenter(listFile, i, dataDir)
#print 'center coords', center, 'coords', center[0], center[1]
inputImage = Photometry.getInputFile(listFile, dataDir, i)
distances = Astrometry.makeDistanceArray(inputImage, Astrometry.getPixelCoords(listFile, i, dataDir))
return distances
@staticmethod
def getInputFile(listFile, dataDir, i):
#print 'filename:', GalaxyParameters.getFilledUrl(listFile, dataDir, i)
inputFile = pyfits.open(GalaxyParameters.getFilledUrl(listFile, dataDir, i))
inputImage = inputFile[0].data
#print 'opened the input file'
return inputImage
@staticmethod
def getInputHeader(listFile, dataDir, i):
inputFile = pyfits.open(GalaxyParameters.getFilledUrl(listFile, dataDir, i))
head = inputFile[0].header
return head
@staticmethod
def calculateFlux(flux, listFile, i):
tsFieldParams = getTSFieldParameters.getParams(listFile, i, getFilterNumber())
zpt = tsFieldParams[0]
ext_coeff = tsFieldParams[1]
airmass = tsFieldParams[2]
print zpt, ext_coeff, airmass, 'tsparams'
fluxRatio = flux/(53.9075*10**(-0.4*(zpt+ext_coeff*airmass)))
mag = -2.5 * np.log10(fluxRatio)
#mag2 = -2.5 * np.log10(fluxRatio2)
print 'full magnitude', mag
return mag
@staticmethod
def buildGrowthCurve(inputImage, center, distances, skyMean, pa, ba, CALIFA_ID, e=False):
ellipseMask = np.zeros((inputImage.shape))
sky = inputImage[np.where(distances > int(round(Photometry.iso25D)))]
fluxData = np.empty((np.max(distances), 7))
fluxData[0,0] = 0
fluxData[0,1] = inputImage[center]
fluxData[0,2] = inputImage[center]
fluxData[0,3] = 200
fluxData[0,4] = inputImage[center]
fluxData[0,5] = 1
fluxData[0,6] = 1
currentPixels = center
currentFlux = inputImage[center] - skyMean
isoA = 1 #initialising
Npix = 1
totalNpix = 1
oldFlux = inputImage[center[0], center[1]]
growthSlope = 200
outputImage = inputImage
skySD = np.std(sky)
limitCriterion = 0.0001*skySD
width = 20
output = inputImage.copy()
while Photometry.checkLimitCriterion(fluxData, isoA-1, limitCriterion, width) != 1:
previousNpix = Npix
oldFlux = currentFlux
currentPixels = ellipse.draw_ellipse(inputImage.shape, center[0], center[1], pa, isoA, ba)
ellipse.getPixelEllipseLength(isoA, ba)
ellipseMask[currentPixels] = 1
Npix = inputImage[currentPixels].shape[0]
totalNpix = inputImage[np.where(ellipseMask == 1)].shape[0]
currentFlux = np.sum(inputImage[currentPixels])
output[currentPixels] = 1
growthSlope = utils.getSlope(oldFlux, currentFlux, isoA-1, isoA)
#print 'isoA', isoA, 'Npix', Npix
fluxData[isoA, 0] = isoA
fluxData[isoA, 1] = np.sum(inputImage[np.where(ellipseMask == 1)])# cumulative flux
fluxData[isoA, 2] = currentFlux/Npix
fluxData[isoA, 3] = growthSlope/Npix
fluxData[isoA, 4] = currentFlux #current flux
fluxData[isoA, 5] = Npix
fluxData[isoA, 6] = totalNpix
isoA = isoA +1
gc_sky = np.mean(fluxData[isoA-width:isoA-1, 2])
flux = np.sum(inputImage[np.where(ellipseMask == 1)]) - gc_sky*inputImage[np.where(ellipseMask == 1)].shape[0]
fluxData = fluxData[0:isoA-1,:] #the last isoA value was incremented, so it should be subtracted
fluxData[:, 1] = fluxData[:, 1] - gc_sky*fluxData[:, 6]#cumulative flux, _sky_subtracted
#print fluxData[-1, 1], gc_sky, 'SKY', fluxData[-1, 6], gc_sky*totalNpix
fluxData[:, 4] = fluxData[:, 4] #current flux
fluxData[:, 6] = fluxData[:, 4] - gc_sky*fluxData[:, 5] #current flux, sky subtracted
#print inputImage[np.where(ellipseMask == 1)].shape[0], 'shape', Npix, 'npix', np.mean(fluxData[isoA-width:isoA-1, 2]), 'sky'
fluxData[:, 2] = fluxData[:, 2] - gc_sky #sky-subtracted flux per pixel
#print inputImage[np.where(ellipseMask == 1)].shape[0], '***************************************'
# --------------------------------------- writing an ellipse of counted points, testing only
#if e:
# hdu = pyfits.PrimaryHDU(output)
# hdu.writeto('ellipseMask'+CALIFA_ID+'.fits')
np.savetxt('growth_curves/'+setBand()+'/gc_profile'+CALIFA_ID+'.csv', fluxData)
return (flux, fluxData, gc_sky)
@staticmethod
def checkLimitCriterion(fluxData, distance, limitCriterion, width):
out = 0
try:
n = fluxData[distance-width:distance+1, 3].shape[0]
nPix = np.sum(fluxData[distance-width:distance+1, 5])
slope = np.sum(np.abs(fluxData[distance-width:distance+1, 3]))/nPix
print 'slope: ', slope, 'limit: ', limitCriterion, distance, 'dist'
if slope <= limitCriterion:
mean = np.mean(fluxData[distance-width:distance+1, 2])
print 'limit reached!', distance, nPix, mean, 'mean'
out = 1
except IndexError as e:
print 'indexError', distance, e
out = 0
return out
@staticmethod
def calculateGrowthCurve(listFile, dataDir, i):
CALIFA_ID = str(i+1)
inputImage = Photometry.getInputFile(listFile, dataDir, i)
dbDir = '../db/'
imgDir = 'img/'+setBand()+'/'
center = Photometry.getCenter(listFile, i, dataDir)
distances = Photometry.createDistanceArray(listFile, i, dataDir)
#hdu = pyfits.PrimaryHDU(distances)
#hdu.writeto('distances.fits')
sky = inputImage[np.where(distances > int(round(Photometry.iso25D)))]
skyMean = np.mean(sky)
skySD = np.std(sky)
#i+1 in the next line reflects the fact that CALIFA id's start with 1
pa = db.dbUtils.getFromDB('PA', dbDir+'CALIFA.sqlite', 'nadine', ' where califa_id = '+ CALIFA_ID)[0][0] #parsing tuples
ba = db.dbUtils.getFromDB('ba', dbDir+'CALIFA.sqlite', 'nadine', ' where califa_id = '+ CALIFA_ID)[0][0]#parsing tuples
#ba = 1
#r_mag = db.dbUtils.getFromDB('r_mag', dbDir+'CALIFA.sqlite', 'nadine', ' where califa_id = '+ CALIFA_ID)[0][0]#parsing tuples
#r_e = db.dbUtils.getFromDB('re', dbDir+'CALIFA.sqlite', 'nadine', ' where califa_id = '+ CALIFA_ID)[0][0]#parsing tuples
#lucy_re = db.dbUtils.getFromDB('re', dbDir+'CALIFA.sqlite', 'lucie', ' where id = '+ CALIFA_ID)[0][0]#parsing tuples
#l_SkyMean = db.dbUtils.getFromDB('sky', dbDir+'CALIFA.sqlite', 'lucie', ' where id = '+ CALIFA_ID)[0][0] - 1000#parsing tuples
# print 'ba', ba
#fluxData = Photometry.buildGrowthCurve(inputImage, center, distances, skyMean, pa, ba)
#isoA = fluxData.shape[0]
# --------------------------------------- starting GC photometry in circular annuli
print 'CIRCULAR APERTURE'
#circFlux, circFluxData = Photometry.circularFlux(inputImage, center, distances, skyMean)
circFlux, circFluxData, gc_sky = Photometry.buildGrowthCurve(inputImage, center, distances, skyMean, pa, 1, str(i+1))
circRadius = circFluxData.shape[0]
#print circRadius, 'circle radius'
#otherFlux = circFluxData[-1, 1]
#print 'fluxes: mask:', circFlux, 'sum', otherFlux
try:
circHLR = circFluxData[np.where(np.floor(circFlux/circFluxData[:,1]) == 1)][0][0] - 1 #Floor() -1 -- last element where the ratio is 2
except IndexError as e:
circHLR = str(e)
circMag = Photometry.calculateFlux(circFlux, listFile, i)
# --------------------------------------- starting ellipse GC photometry
print 'ELLIPTICAL APERTURE'
totalFlux, fluxData, gc_sky = Photometry.buildGrowthCurve(inputImage, center, distances, skyMean, pa, ba, CALIFA_ID, e=True)
otherFlux = fluxData[fluxData.shape[0]-1, 6]
elMajAxis = fluxData.shape[0]
#print totalFlux - otherFlux, 'flux diff'
#print 't', totalFlux, 'o', otherFlux
diff = [CALIFA_ID, totalFlux - otherFlux]
utils.writeOut(diff, 'fluxdiff.txt')
elMag = Photometry.calculateFlux(totalFlux, listFile, i)
try:
elHLR = fluxData[np.where(np.floor(totalFlux/fluxData[:, 1]) == 1)][0][0] - 1 #Floor() -1 -- last element where the ratio is 2
print elHLR
except IndexError as e:
print 'err'
elHLR = e
plotGrowthCurve.plotGrowthCurve(fluxData, CALIFA_ID)
# --------------------- writing output jpg file with both outermost annuli
outputImage = inputImage
circpix = ellipse.draw_ellipse(inputImage.shape, center[0], center[1], pa, circRadius, 1)
elPix = ellipse.draw_ellipse(inputImage.shape, center[0], center[1], pa, elMajAxis, ba)
outputImage[circpix] = 0
outputImage[elPix] = 0
outputImage, cdf = imtools.histeq(outputImage)
#scipy.misc.imsave('img/output/'+CALIFA_ID+'.jpg', outputImage)
scipy.misc.imsave(imgDir+'snapshots/'+CALIFA_ID+'_gc.jpg', outputImage)
#hdu = pyfits.PrimaryHDU(outputImage)
#outputName = 'CALIFA'+CALIFA_ID+'.fits'
#hdu.writeto(outputName)
# ------------------------------------- formatting output row
output = [CALIFA_ID, elMag, elHLR, circMag, circHLR, np.mean(sky), gc_sky]
print output
#print skyMean, oldSky, 'sky'
return output
def getDuplicates(listFile, dataDir):
#for i in range(0, 939):
# print i+1, GalaxyParameters.getFilledUrl(listFile, dataDir, i)
fnames = np.genfromtxt('outputNames.txt', dtype='object')
#ndtype = [('id', int), ( 'fname', str)]
#fnames = fnames.view(ndtype)
#du = np.lib.recfunctions.find_duplicates(fnames, key='fname', ignoremask=True, return_index=False)
ids = list(fnames[:, 0])
fnames = list(fnames[:, 1])
u, indices = np.unique(fnames, return_index=True)
#print indices.shape, type(indices)
dupes = [int(item) for item in ids if int(item) not in list(indices)]
print dupes
def setBand():
return 'r'
def getFilterNumber():
if setBand() == 'u':
return 0
elif setBand() == 'g':
return 1
elif setBand() == 'r':
return 2
elif setBand() == 'i':
return 3
elif setBand() == 'z':
return 4
def main():
iso25D = 40 / 0.396
band = setBand()
dataDir = '../data'
# dataDir = '/media/46F4A27FF4A2713B_/work2/data'
fitsdir = dataDir+'SDSS'+band
# fitsDir = '../data/SDSS/'
# dataDir = '../data'
listFile = dataDir+'/SDSS_photo_match.csv'
outputFile = dataDir+'/gc_out.csv'
imgDir = 'img/'+band+'/'
simpleFile = dataDir+'/CALIFA_mother_simple.csv'
maskFile = dataDir+'maskFilenames.csv'
noOfGalaxies = 939
lim_lo = int(sys.argv[1])
lim_hi = int(sys.argv[2])
#missing = np.genfromtxt('r_wrong_skies.csv', delimiter = ',', dtype = object) [:, 0]
#print missing
for i in range(lim_lo, lim_hi):
print i, lim_lo, lim_hi
pass
i = int(i) - 1
try:
print 'filename', GalaxyParameters.getSDSSUrl(listFile, dataDir, i)
print 'filledFilename', GalaxyParameters.getFilledUrl(listFile, dataDir, i)
print i, 'i'
#output = Photometry.calculateGrowthCurve(listFile, dataDir, i)
#utils.writeOut(output, band+'_ellipse_log'+str(lim_lo)+'.csv')
except IOError as err:
print 'err', err
#output = [str(i+1), 'File not found', err]
#utils.writeOut(output, band+'_ellipseErrors.csv')
pass
if __name__ == "__main__":
main()