LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
genDistortedImage.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 import math
24 
25 import lsst.afw.image as afwImg
26 import lsst.afw.table as afwTable
27 
28 def noDistort(src):
29  """Do no distortion. Used for sanity checking"""
30 
31  out = src.table.copyRecord(src)
32  return out
33 
34 
35 def linearXDistort(src, frac=.001):
36  """Increase the x value in a Source object by frac. E.g
37  src.x = 1000 --> 1001 if frac=.001
38 
39  Input:
40  src A Source object
41  frac How much to change X by
42 
43  Output:
44  A deep copy of src, with the value of x changed
45  """
46 
47 
48  out = src.table.copyRecord(src)
49  out.set(out.table.getCentroidKey().getX(), out.getX()*(1+frac) )
50  return out
51 
52 
53 def quadraticDistortX(src, frac=1e-6):
54  """Distort image by terms with power <=2
55  i.e y, y^2, x, xy, x^2
56  """
57 
58  out = src.table.copyRecord(src)
59  x = out.getX()
60  y = out.getY()
61  val = x**2
62 
63  out.set(out.table.getCentroidKey().getX(), x + val*frac)
64  out.set(out.table.getCentroidKey().getY(), y)
65  return out
66 
67 
68 def cubicDistortX(src, frac=1e-9):
69  """Distort image by terms with power <=2
70  i.e y, y^2, x, xy, x^2
71  """
72 
73  out = src.table.copyRecord(src)
74  x = out.getX()
75  y = out.getY()
76  val = x**3
77 
78  out.set(out.table.getCentroidKey().getX(), x + val*frac)
79  out.set(out.table.getCentroidKey().getY(), y)
80  return out
81 
82 
83 def manyTermX(src, frac=1e-9):
84  out = src.table.copyRecord(src)
85  x = out.getX()
86  y = out.getY()
87  val = x**3 - 2*x**2 + 4*x - 9
88 
89  out.set(out.table.getCentroidKey().getX(), x + val*frac)
90  out.set(out.table.getCentroidKey().getY(), y)
91  return out
92 
93 
94 
95 def linearYDistort(src, frac=.001):
96  """Increase the y value in a Source object by frac. E.g
97  src.x = 1000 --> 1001 if frac=.001
98 
99  Input:
100  src A Source object
101  frac How much to change Y by
102 
103  Output:
104  A deep copy of src, with the value of y changed
105  """
106 
107  out = src.table.copyRecord(src)
108  out.set(out.table.getCentroidKey().getY(), out.getY()*(1+frac) )
109  return out
110 
111 
112 def quadraticDistortY(src, frac=1e-6):
113  """Distort image by terms with power <=2
114  i.e y, y^2, x, xy, x^2
115  """
116 
117  out = src.table.copyRecord(src)
118  x = out.getX()
119  y = out.getY()
120  val = y**2
121 
122  out.set(out.table.getCentroidKey().getX(), x)
123  out.set(out.table.getCentroidKey().getY(), y + val*frac)
124  return out
125 
126 
127 def cubicDistortY(src, frac=1e-9):
128  """Distort image by terms with power <=2
129  i.e y, y^2, x, xy, x^2
130  """
131 
132  out = src.table.copyRecord(src)
133  x = out.getX()
134  y = out.getY()
135  val = x**3
136 
137  out.set(out.table.getCentroidKey().getX(), x)
138  out.set(out.table.getCentroidKey().getY(), y + val*frac)
139  return out
140 
141 
142 def manyTermY(src, frac=1e-9):
143  out = src.table.copyRecord(src)
144  x = out.getX()
145  y = out.getY()
146  val = y**3 - 2*y**2 + 4*y - 9
147 
148  out.set(out.table.getCentroidKey().getX(), x)
149  out.set(out.table.getCentroidKey().getY(), y + val*frac)
150  return out
151 
152 
153 def crossTerms1(src, frac=1e-11):
154  out = src.table.copyRecord(src)
155  x = out.getX()
156  y = out.getY()
157  val = x**3 - 2*x**2 #+ 4*x - 9
158 
159  out.set(out.table.getCentroidKey().getX(), x)
160  out.set(out.table.getCentroidKey().getY(), y + val*frac)
161  return out
162 
163 
164 def crossTerms2(src, frac=1e-11):
165  out = src.table.copyRecord(src)
166  x = out.getX()
167  y = out.getY()
168  val = y**3 - 2*y**2 + 4*y - 9
169 
170  out.set(out.table.getCentroidKey().getX(), x+ val*frac)
171  out.set(out.table.getCentroidKey().getY(), y)
172  return out
173 
174 
175 def crossTerms3(src, frac=1e-9):
176  out = src.table.copyRecord(src)
177  x = out.getX()
178  y = out.getY()
179  valx = x**3 - 2*x**2 + 4*x - 9
180  valy = y**3 - 2*y**2 + 4*y - 9
181 
182  out.set(out.table.getCentroidKey().getX(), x + valy*frac)
183  out.set(out.table.getCentroidKey().getY(), y + valx*frac)
184  return out
185 
186 
187 def quadraticDistort(src, frac=1e-6):
188  """Distort image by terms with power <=2
189  i.e y, y^2, x, xy, x^2
190  """
191 
192  out = src.table.copyRecord(src)
193  x = out.getX()
194  y = out.getY()
195  val = y + 2*y**2
196  val += 3*x + 4*x*y
197  val += x**2
198 
199  out.set(out.table.getCentroidKey().getX(), x + val*frac)
200  out.set(out.table.getCentroidKey().getY(), y)
201  return out
202 
203 def T2DistortX(src, frac=1e-6):
204  """Distort image by a 2nd order Cheby polynomial"""
205 
206  out = src.table.copyRecord(src)
207  x = src.getX()
208  val = 2*(x**2) -1
209  out.set(out.table.getCentroidKey().getX(), x + frac*val)
210  return out
211 
212 
213 def distortList(srcList, function):
214  """Create a copy of srcList, and apply function to distort the
215  values of x and y.
216 
217  Input:
218  srcList a SourceSet object
219  function: A function that does a deep copy of a single Source
220  """
221 
222  out = afwTable.SourceCatalog(srcList.table)
223 
224  for src in srcList:
225  out.append( function(src) )
226 
227  maxDiff=0
228  for i in range(len(srcList)):
229  s = srcList[i]
230  o = out[i]
231 
232  x1, y1 = s.getX(), s.getY()
233  x2, y2 = o.getX(), o.getY()
234 
235  diff = math.hypot(x1-x2, y1-y2)
236  maxDiff = max(diff, maxDiff)
237 
238  print "Max deviation is %e pixels" %(maxDiff)
239 
240  return out
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:55