-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwingtips.py
279 lines (234 loc) · 9.49 KB
/
wingtips.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
#! /usr/bin/env python
'''
WFIRST Infrared Nearby Galaxies Test Image Product Simulator
Produces input files for the WFIRST STIPS simulator
'''
import time
import numpy as np
from astropy import wcs
from astropy.io import fits, ascii
from astropy.table import Table
class WingTips:
'''
Initialize WingTips object
'''
def __init__(self,infile=[],center=[0,0]):
if len(infile)==0:
self.tab = np.array([])
else:
if isinstance(infile,str):
infile = [infile]
self.tab = WingTips.read_stips(infile[0])
if len(infile)>1:
for i in range(1,len(infile)):
_tab = WingTips.read_stips(infile[i])
self.tab = np.vstack((self.tab,_tab))
center = WingTips.get_center(self.tab[:,0],self.tab[:,1])
self.center = center
self.n = self.tab.shape[0]
self.infile = infile
return None
''' Strip coordinates from WingTips object '''
def strip_radec(self,hasID=False):
_i = int(hasID)
self.tab = np.delete(self.tab,[_i,_i+1],1)
return None
''' Attach given RA-DEC to WingTips object'''
def attach_radec(self,radec,hasID=False):
if self.n != radec.shape[0]:
raise ValueError('Number of RA-DEC does not match sources')
_i = int(hasID)
self.tab = np.insert(self.tab,_i,radec.T,1)
self.center = WingTips.get_center(radec[:,0+_i],radec[:,1+_i])
return None
''' Replace RA-DEC of WingTips object '''
def replace_radec(self,radec,hasID=False):
self.strip_radec(hasID)
self.attach_radec(radec,hasID)
return None
'''
Return random RA-DEC for given image or WingTips object
Optionally, specify center and image size desired
'''
def random_radec_for(self,other,shape=(4096,4096),sample=False,n=0,hasID=False):
_i = int(hasID)
try:
if other.endswith('.fits'):
return WingTips.random_radec(self.n,imfile=other)
except AttributeError:
if not sample:
return WingTips.random_radec(self.n,center=other.center)
elif not bool(n):
return WingTips.sample_radec(n=self.n,radec1=False,radec2=other.tab[:,_i:_i+1])
else:
return WingTips.sample_radec(n=n,radec1=self.tab[:,_i:_i+1],radec2=other.tab[:,_i:_i+1])
''' Merge two WingTips objects '''
def merge_with(self,other,hasRADEC=True,hasID=False):
if self.tab.shape[1]!=other.tab.shape[1]:
raise ValueError('Number of columns does not match',self.tab.shape[1],other.tab.shape[1])
self.tab = np.vstack((self.tab,other.tab))
self.n = self.tab.shape[0]
self.infile.append(other.infile)
_i = int(hasID)
if hasRADEC:
self.center = WingTips.get_center(self.tab[:,0+_i],self.tab[:,1+_i])
return None
''' Convert flux to surface brightness for sersic profile galaxies '''
def flux_to_Sb(self,hasRADEC=True,hasID=False):
_i = int(hasID)
if hasRADEC:
_i = _i+2
_f = self.tab[:,_i].astype(float)
_r = self.tab[:,_i+3].astype(float)
_a = self.tab[:,_i+5].astype(float)
_s = (0.5*_f) / (np.pi * _r**2 * _a)
self.tab = np.delete(self.tab,_i,1)
self.tab = np.insert(self.tab,_i,_s.T,1)
return None
''' Write out a STIPS input file '''
def write_stips(self,outfile='temp.txt',hasID=False,hasCmnt=False,saveID=False,ipac=False):
_tab = WingTips.get_tabular(self.tab,hasID,hasCmnt,saveID)
_nms = ('id', 'ra', 'dec', 'flux', 'type', 'n', 're', 'phi', 'ratio', 'notes')
_fmt = ('%10d','%15.7f','%15.7f','%15.7f','%8s','%10.3f','%15.7f','%15.7f','%15.7f','%8s')
_t = Table(_tab, names=_nms)
if ipac:
ascii.write(_t, outfile, format='ipac', formats=dict(zip(_nms,_fmt)))
else:
ascii.write(_t, outfile, format='fixed_width', delimiter='', formats=dict(zip(_nms,_fmt)))
return print('Wrote out %s \n' % outfile)
''' Build a WingTips class object from scratch '''
@staticmethod
def from_scratch(flux, ra=[], dec=[], center=[], ID=[], Type=[], n=[], re=[], phi=[], ratio=[], notes=[], outfile=''):
_temp = WingTips()
_temp.n = len(flux)
_temp.infile = ['fromScratch']
if len(center)>0:
_temp.center = center
if len(ra)==0:
radec = _temp.random_radec_for(_temp)
ra,dec = radec[:,0],radec[:,1]
elif ((len(ra)==len(dec))&(len(ra)>0)):
_temp.center = WingTips.get_center(np.array(ra),np.array(dec))
else:
raise ValueError('Provide valid coordinate or center')
if ((len(Type)==0)|(Type is 'point')|(Type is 'sersic')):
if ((len(Type)==0)|(Type is 'point')):
Type = np.repeat(np.array(['point']),len(flux))
_ones = np.ones_like(flux)
n, re, phi, ratio = _ones, _ones, _ones, _ones
elif (Type=='sersic'):
Type = np.repeat(np.array(['sersic']),len(flux))
elif (len(Type)==len(flux)):
Type = np.array(Type)
_tab = np.array([ra,dec,flux,Type,n,re,phi,ratio]).T
if (len(ID)==len(flux)):
_tab=np.hstack((np.array(ID,ndmin=2).T,_tab))
if (len(notes)==len(flux)):
_tab=np.hstack((_tab,np.array(notes,ndmin=2).T))
_temp.tab = np.array(_tab)
if outfile is '':
return _temp
else:
_temp.write_stips(outfile,hasID=bool(ID),hasCmnt=bool(notes),saveID=bool(ID))
return None
'''
Read in a STIPS input file in ascii format and
return corrsponding NumPy array
'''
@staticmethod
def read_stips(infile,getRADEC=True,getID=False,getCmnt=False):
_tab = []
_infile = ascii.read(infile)
print('\nRead in %s \n' % infile)
if getID:
_tab.append(_infile['id'])
if getRADEC:
_tab.append(_infile['ra'])
_tab.append(_infile['dec'])
_tab.append(_infile['flux'])
_tab.append(_infile['type'])
_tab.append(_infile['n'])
_tab.append(_infile['re'])
_tab.append(_infile['phi'])
_tab.append(_infile['ratio'])
if getCmnt:
_tab.append(_infile['comment'])
return np.array(_tab).T
''' Return tabular lists for STIPS input file columns '''
@staticmethod
def get_tabular(_tab,hasID=False,hasCmnt=False,saveID=False):
_i = int(hasID)
if ~saveID:
_n = _tab.shape[0]
_ID = np.array(np.linspace(1,_n,_n),ndmin=2).T
_tab = np.hstack((_ID,_tab[:,_i:]))
if ~hasCmnt:
_cmnt = np.array(np.repeat(np.array(['comment']),_tab.shape[0],),ndmin=2).T
_tab = np.hstack((_tab,_cmnt))
return [_tab[:,0].astype(float), _tab[:,1].astype(float), _tab[:,2].astype(float), \
_tab[:,3].astype(float), _tab[:,4], _tab[:,5].astype(float), \
_tab[:,6].astype(float), _tab[:,7].astype(float), \
_tab[:,8].astype(float), _tab[:,9]]
''' Build WCS coordinate system from scratch '''
@staticmethod
def create_wcs(centers=[0,0],crpix=[2048,2048],cdelt=[-0.11/3600,0.11/3600],cunit=['deg','deg'],\
ctype=['RA---TAN','DEC--TAN'],lonpole=180,latpole=24.333335,\
equinox=2000.0,radesys='ICRS'):
_w = wcs.WCS()
_w.wcs.cdelt = cdelt
_w.wcs.crpix = crpix
_w.wcs.crval = centers
_w.wcs.cunit = cunit
_w.wcs.ctype = ctype
_w.wcs.lonpole = lonpole
_w.wcs.latpole = latpole
_w.wcs.radesys = radesys
_w.wcs.equinox = equinox
return _w
''' Return coordinate system for given image file'''
@staticmethod
def read_wcs(imfile):
print('Getting coordinates from %s \n' % imfile)
return wcs.WCS(fits.open(imfile)[1].header)
''' Return 'n' random radec for given image file or coordinate list '''
@staticmethod
def random_radec(n=10,center=[0,0],shape=(4096,4096),imfile=''):
_xy = np.random.rand(n,2)*shape
if imfile is not '':
_w = WingTips.read_wcs(imfile)
else:
_w = WingTips.create_wcs(center)
return _w.wcs_pix2world(_xy,1)
'''
Return a random sample of 'n' RA-DEC coordinates from 'radec2'
If radec1 is specified, then replace 'n' radom coordinates
in 'radec1' with random sample from 'radec2'
'''
@staticmethod
def sample_radec(n=10,radec1=False,radec2=[]):
in2 = np.random.randint(0,radec2.shape[0],n)
if ~radec1:
return radec2[in2,:]
else:
in1 = np.random.randint(0,radec1.shape[0],n)
radec1[in1,:] = radec2[in2,:]
return radec1
''' Return mean of RA-DEC positions given '''
@staticmethod
def get_center(ra,dec):
return [ra.astype(float).mean(),dec.astype(float).mean()]
'''
Convert mags to WFI instrument counts
Default is apparent AB mags
Specify 'dist' if absolute mags
Specify AB_Vega if Vega mags
'''
@staticmethod
def get_counts(mag,ZP,dist=0,AB_Vega=0):
if bool(dist):
print('\nDistance is d = %4.2f Mpc\n' % dist)
u = 25+5*np.log10(dist)
mag = mag+u
if bool(AB_Vega):
mag = mag + AB_Vega
return 10**((mag-ZP)/(-2.5))