/
vimos_get_GCs.py
352 lines (295 loc) · 11.2 KB
/
vimos_get_GCs.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
# -*- 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 as sdss
import imtools
import plot_survey as plot
#import photometry as phot
import readAtlas
import ellipse
import db
import plotGrowthCurve
from math import isnan
import scipy.misc
import sys
from galaxyParameters import *
import cProfile
import multiprocessing
#from do_GC_photometry import calculateFlux
import getTSFieldParameters
#a set of methods for swapping between pixel coordinates and ra, dec
class Astrometry():
@staticmethod
def getCenterCoords(ID):
centerCoords = (GalaxyParameters.SDSS(ID).ra, GalaxyParameters.SDSS(ID).dec)
return centerCoords
@staticmethod
def getPixelCoords(ID):
WCS=astWCS.WCS(GalaxyParameters.getSDSSUrl(ID)) #changed -- was filledUrl. I don't write coords to my masks..
centerCoords = Astrometry.getCenterCoords(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(shape, center):
grid = np.indices(shape)
y = grid[0] - center[0]
x = grid[1] - center[1]
r = np.round(np.sqrt(np.square(y) + np.square(x)), 0)
return r
@staticmethod
def makeDistanceArray(img, center):
distances = np.zeros(img.shape)
#print Astrometry.distance2origin(0,0, center), 'squared distance to origin'
distances = Astrometry.distance2origin(img.shape, center)
#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 distances
class Photometry():
pixelScale = 0.396
iso25D = 40 / 0.396
@staticmethod
def getCenter(image):
y = image.shape[0]
x = image.shape[1]
#if (y%2 == 1 and x%2 == 1):
yc = y/2
xc = x/2
center = (yc, xc)
return center
@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 getMask(ID):
dataDir = Settings.getConstants().dataDir
maskFilename = dataDir+utils.getMask(Settings.getConstants().maskFile, ID)
maskFile = pyfits.open(maskFilename)
mask = maskFile[0].data
print maskFilename
maskFile.close()
print mask.shape, 'mask'
return mask
@staticmethod
def createDistanceArray(i):
center = Photometry.getCenter(i)
#print 'center coords', center, 'coords', center[0], center[1]
inputImage = Photometry.getInputFile(i, band=Settings.getConstants().band)
distances = Astrometry.makeDistanceArray(inputImage, Astrometry.getPixelCoords(i))
return distances
@staticmethod
def getInputFile(i, band):
#print 'filename:', GalaxyParameters.getFilledUrl(listFile, dataDir, i)
inputFile = pyfits.open(GalaxyParameters.getFilledUrl(i, band))
inputImage = inputFile[0].data
if band != 'r':
inputImage-=1000
#print 'opened the input file'
return inputImage
@staticmethod
def getInputHeader(listFile, dataDir, i):
inputFile = pyfits.open(GalaxyParameters.getFilledUrl(i))
head = inputFile[0].header
return head
@staticmethod
def initFluxData(inputImage, center, distances):
print np.max(distances)
fluxData = np.empty((np.max(distances), 5))
fluxData[0,0] = 0 #isoA
fluxData[0,1] = inputImage[center] #currentFlux
fluxData[0,2] = 1
fluxData[0,3] = inputImage[center] #currentFluxM
fluxData[0,4] = 1 #NpixM
return fluxData
@staticmethod
def setLimitCriterion(i, band):
skySD = Photometry.getSkyParams(i, band).skySD
C = 0.00005
return C*skySD
@staticmethod
def buildGrowthCurve(name, center, distances, pa, ba, inputImage, mask, isoA_max):
band = Settings.getConstants().band
#masked input array
inputImageM = np.ma.masked_array(inputImage, mask=mask)
ellipseMask = np.zeros((inputImage.shape), dtype=np.uint8)
ellipseMaskM = ellipseMask.copy()
fluxData = Photometry.initFluxData(inputImage, center, distances)
#currentPixels = center
#currentFlux = inputImage[center]
Npix = 1 #init
growthSlope = 200 #init
md = np.max(distances)
#print md, 'MD'
for isoA in range(1, min(int(isoA_max), int(md))):
#draw ellipse for all pixels:
currentPixels = ellipse.draw_ellipse(inputImage.shape, center[0], center[1], pa, isoA, ba)
#Npix = inputImage[currentPixels].shape[0]
#currentFlux = np.sum(inputImage[currentPixels])
ellipseMask[currentPixels] = 1
Npix = inputImage[currentPixels].shape[0]
#draw ellipse for masked pixels only:
currentPixelsM = ellipse.draw_ellipse(inputImageM.shape, center[0], center[1], pa, isoA, ba)
ellipseMaskM[currentPixelsM] = 1
maskedPixels = inputImageM[np.where((ellipseMaskM == 1) & (mask == 0))]
#print np.sum(maskedPixels), np.sum(inputImage[np.where(ellipseMask == 1)])
fluxData[isoA, 0] = isoA
fluxData[isoA, 1] = np.sum(inputImage[np.where(ellipseMask == 1)])# cumulative flux
fluxData[isoA, 2] = inputImage[np.where(ellipseMask == 1)].shape[0]
fluxData[isoA, 3] = np.sum(maskedPixels)# - maskedPixels.shape[0]*sky# cumulative flux, without masked pixels
fluxData[isoA, 4] = maskedPixels.shape[0]
#print Npix, NpixM, fluxData[isoA, 2], fluxData[isoA, 4]
isoA = isoA +1
#gc_sky = np.mean(fluxData[isoA-width:isoA-1, 2])
#flux = np.sum(inputImage[np.where(ellipseMask == 1)]) - sky*inputImage[np.where(ellipseMask == 1)].shape[0]
fluxData = fluxData[0:isoA-1,:]
#fluxData[:, 3] = fluxData[:, 3] - fluxData[isoA-1, 4]*sky
#print fluxData[isoA-1, 4], 'no pix'
#fluxData[:, 3] = np.cumsum(fluxData[:, 3])
#fluxData[:, 1] = np.cumsum(fluxData[:, 1])
#the last isoA value was incremented, so it should be subtracted
#fluxData[:, 1] = fluxData[:, 1] - sky*fluxData[:, 5]#cumulative flux, _sky_subtracted
#fluxData[:, 3] = fluxData[:, 3] - sky*fluxData[:, 4]
#fluxData[:, 2] = fluxData[:, 2] - sky #sky-subtracted flux per pixel
#print inputImage[np.where(ellipseMask == 1)].shape[0], '***************************************'
# --------------------------------------- writing an ellipse of counted points, testing only
plotGrowthCurve.plotGrowthCurve(fluxData, Settings.getConstants().band, name)
#hdu = pyfits.PrimaryHDU(ellipseMask)
#hdu.writeto('vimos_masks/Mask'+name+"_"+Settings.getConstants().band+'.fits', clobber=True)
np.savetxt('vimos_growth_curves/el/'+Settings.getConstants().band+'/gc_profile_el_new_'+name+'.csv', fluxData)
@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 getFluxRatio(i, band):
ret = Photometry()
ret.fluxRatio = 0.5*np.sum(Photometry.getInputFile(i, band))/np.sum(Photometry.getInputFile(i, 'r'))
return ret
@staticmethod
def getSkyParams(i, band):
ret = Photometry()
inputImage = Photometry.getInputFile(i, band)
distances = Photometry.createDistanceArray(i)
sky = inputImage[np.where(distances > 2*int(round(Photometry.iso25D)))]
ret.skyMean = np.mean(sky)
ret.skySD = np.std(sky)
return ret
@staticmethod
def calculateGrowthCurve(name, ba, pa, isoA_max, sky):
band = Settings.getConstants().band
imgName = 'vimos/sdss/SDSS/'+name+"/galaxy_"+Settings.getConstants().band+'_large.fits'
dataFile = pyfits.open(imgName)
img = dataFile[0].data
img = img - sky
try:
mask = pyfits.open('vimos/sdss/SDSS/'+name+"/mask_large.fits")[0].data
except IOError:
mask = np.zeros(img.shape)
center = Photometry.getCenter(img)
distances = Astrometry.makeDistanceArray(img, center)
print 'Input shape:', img.shape, ' Max distance: ', np.max(distances)
Photometry.buildGrowthCurve(name, center, distances, pa, ba, img, mask, isoA_max)
class Settings():
@staticmethod
def getConstants():
ret = Settings()
ret.lo = sys.argv[1]
ret.hi = sys.argv[2]
ret.band = sys.argv[3]
ret.dataDir = '../data/'
ret.listFile = ret.dataDir+'/SDSS_photo_match.csv'
ret.simpleFile = ret.dataDir+'/CALIFA_mother_simple.csv'
ret.maskFile = ret.dataDir+'maskFilenames.csv'
ret.outputFile = ret.dataDir+'/gc_out.csv'
ret.imgDir = 'img/'
ret.dbDir = '../db/'
return ret
def getFluxUnderMask(i):
band = Settings.getConstants().band
maskFilename = 'masks/ellipseMask'+str(i+1)+'.fits'
image = Photometry.getInputFile(i, band)
print image.shape
imageR = Photometry.getInputFile(i, 'r')
ellipseMaskFile = pyfits.open(maskFilename)
ellipseMask = ellipseMaskFile[0].data
ellipseMask = getCroppedMask(ellipseMask, i)
print ellipseMask.shape
galaxy = image[np.where(ellipseMask == 1)]
fluxInEllipse = np.sum(galaxy)
sky = getEllipticalSky(image, i)
Npix = galaxy.shape[0]
skySubFlux = fluxInEllipse - Npix*sky
mag = calculateFlux(skySubFlux, i-1)
ellipseMaskFile.close()
return mag, sky
def measureFluxInOtherBands(galaxyList):
band = Settings.getConstants().band
out = []
for i in galaxyList:
i = int(i) - 1
try:
print 'filledFilename', GalaxyParameters.getFilledUrl(i, band)
mag, sky = getFluxUnderMask(i)
out.append((i+1, mag, sky))
except IOError as err:
print 'err', err
output = [str(i+1), 'File not found', err]
utils.writeOut(output, "ellipseErrors.csv")
pass
np.savetxt("gc_ellmask_"+band+".csv", out, fmt="%s, %f, %f")
def main():
mags = []
data = np.genfromtxt("vimos/sdss/ba_pa_values.csv", delimiter=",", dtype='object')
names = data[:, 0]
band = Settings.getConstants().band
res = np.genfromtxt("vimos_sky2_"+band+"_ell.csv", delimiter=",", dtype='object')
sky_values = res[:, 3]
isoA_values = res[:, 5]
for i, galaxy in enumerate(names):
#if i < 24:
# continue
ba = np.float(data[i, 1])
pa = np.float(data[i, 2]) + 90
sky = np.float(sky_values[i])
isoA = np.float(isoA_values[i])
print isoA, galaxy, sky
Photometry.calculateGrowthCurve(galaxy, ba, pa, isoA, sky)
#exit()
if __name__ == "__main__":
main()