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 - | import os |
70 - | import shutil |
71 - | import numpy |
72 - | import unittest |
73 - | |
74 - | from casatools import image, table |
75 - | from casatasks import imrebin |
76 - | |
77 - | _tb = table( ) |
78 - | _ia = image( ) |
79 - | |
80 - | def alleqnum(x,num,tolerance=0): |
81 - | if len(x.shape)==1: |
82 - | for i in range(x.shape[0]): |
83 - | if not (abs(x[i]-num) < tolerance): |
84 - | print("x[",i,"]=", x[i]) |
85 - | return False |
86 - | if len(x.shape)==2: |
87 - | for i in range(x.shape[0]): |
88 - | for j in range(x.shape[1]): |
89 - | if not (abs(x[i][j]-num) < tolerance): |
90 - | print("x[",i,"][",j,"]=", x[i][j]) |
91 - | return False |
92 - | if len(x.shape)==3: |
93 - | for i in range(x.shape[0]): |
94 - | for j in range(x.shape[1]): |
95 - | for k in range(x.shape[2]): |
96 - | if not (abs(x[i][j][k]-num) < tolerance): |
97 - | print("x[",i,"][",j,"][",k,"]=", x[i][j][k]) |
98 - | return False |
99 - | if len(x.shape)==4: |
100 - | for i in range(x.shape[0]): |
101 - | for j in range(x.shape[1]): |
102 - | for k in range(x.shape[2]): |
103 - | for l in range(x.shape[3]): |
104 - | if not (abs(x[i][j][k][l]-num) < tolerance): |
105 - | print("x[",i,"][",j,"][",k,"][",l,"]=", x[i][j][k]) |
106 - | return False |
107 - | if len(x.shape)>4: |
108 - | stop('unhandled array shape in alleq') |
109 - | return True |
110 - | |
111 - | class imrebin_test(unittest.TestCase): |
112 - | |
113 - | def setUp(self): |
114 - | self._myia = image() |
115 - | |
116 - | def tearDown(self): |
117 - | self._myia.done() |
118 - | self.assertTrue(len(_tb.showcache()) == 0) |
119 - | |
120 - | def test_stretch(self): |
121 - | """ ia.rebin(): Test stretch parameter""" |
122 - | yy = self._myia |
123 - | mymask = "maskim" |
124 - | yy.fromshape(mymask, [200, 200, 1, 1]) |
125 - | yy.addnoise() |
126 - | yy.done() |
127 - | shape = [200,200,1,10] |
128 - | imagename = "aa.im" |
129 - | yy.fromshape(imagename, shape) |
130 - | yy.addnoise() |
131 - | yy.done() |
132 - | outfile = "ab.im" |
133 - | self.assertRaises( |
134 - | Exception, imrebin, imagename=imagename, outfile=outfile, factor=[2,2,1,1], |
135 - | mask=mymask + ">0", stretch=False, overwrite=True |
136 - | ) |
137 - | imrebin( |
138 - | imagename=imagename, outfile=outfile, factor=[2,2,1,1], |
139 - | mask=mymask + ">0", stretch=True, overwrite=True |
140 - | ) |
141 - | yy.open(outfile) |
142 - | self.assertTrue((yy.shape() == [100, 100, 1, 10]).all()) |
143 - | yy.done() |
144 - | |
145 - | def test_general(self): |
146 - | """ ia.rebin(): General tests""" |
147 - | |
148 - | |
149 - | myia = self._myia |
150 - | shp2 = [20,40] |
151 - | d2 = myia.makearray(1.0, [shp2[0], shp2[1]]) |
152 - | |
153 - | imagename = "st.im" |
154 - | myim2 = myia.newimagefromarray(outfile=imagename, pixels=d2) |
155 - | myim2.done() |
156 - | outfile = "gk.im" |
157 - | self.assertRaises( |
158 - | Exception, imrebin, imagename=imagename, outfile=outfile, |
159 - | factor=[-100,2], overwrite=True |
160 - | ) |
161 - | imrebin( |
162 - | imagename=imagename, outfile=outfile, overwrite=True, |
163 - | factor=[2,2] |
164 - | ) |
165 - | myia.open(outfile) |
166 - | p = myia.getchunk() |
167 - | self.assertTrue(alleqnum(p,1.0,tolerance=0.0001)) |
168 - | myia.done() |
169 - | |
170 - | def test_multibeam(self): |
171 - | """Test multiple beams""" |
172 - | myia = self._myia |
173 - | imagename = "gd.im" |
174 - | myia.fromshape(imagename, [10, 10, 10]) |
175 - | myia.setrestoringbeam( |
176 - | major="4arcsec", minor="2arcsec", pa="0deg", |
177 - | channel=0, polarization=0 |
178 - | ) |
179 - | outfile = "dx.im" |
180 - | imrebin( |
181 - | imagename=imagename, outfile=outfile, |
182 - | factor=[2,2,1] |
183 - | ) |
184 - | |
185 - | self.assertRaises( |
186 - | Exception, imrebin, imagename=imagename, outfile=outfile, |
187 - | factor=[2,2,2] |
188 - | ) |
189 - | |
190 - | def test_crop(self): |
191 - | """Test crop parameter""" |
192 - | myia = self._myia |
193 - | imagename = "xxyy.im" |
194 - | myia.fromshape(imagename, [20, 20, 20]) |
195 - | factor = [3,3,3] |
196 - | myia.done() |
197 - | outfile = "outxdkd.im" |
198 - | imrebin(imagename=imagename, outfile=outfile, factor=factor, crop=True) |
199 - | myia.open(outfile) |
200 - | self.assertTrue((myia.shape() == [6,6,6]).all()) |
201 - | myia.done() |
202 - | imrebin(imagename=imagename, outfile=outfile, factor=factor, crop=False, overwrite=True) |
203 - | myia.open(outfile) |
204 - | self.assertTrue((myia.shape() == [7,7,7]).all()) |
205 - | myia.done() |
206 - | |
207 - | def test_dropdeg(self): |
208 - | """Test dropdeg parameter""" |
209 - | myia = self._myia |
210 - | imagename = "kjfasd.im" |
211 - | myia.fromshape(imagename, [20, 20, 1]) |
212 - | factor = [5,5] |
213 - | myia.done() |
214 - | outfile = "dkfajfas.im" |
215 - | imrebin(imagename=imagename, outfile=outfile, factor=factor, dropdeg=True) |
216 - | myia.open(outfile) |
217 - | self.assertTrue((myia.shape() == [4,4]).all()) |
218 - | myia.done() |
219 - | |
220 - | def test_box(self): |
221 - | """Test use of box""" |
222 - | myia = self._myia |
223 - | imagename = "erzvd.im" |
224 - | myia.fromshape(imagename, [30, 30, 1]) |
225 - | factor = [5,5] |
226 - | myia.done() |
227 - | outfile = "vcsfea.im" |
228 - | imrebin(imagename=imagename, outfile=outfile, factor=factor, box="5,5,25,25",crop=True) |
229 - | myia.open(outfile) |
230 - | self.assertTrue((myia.shape() == [4,4,1]).all()) |
231 - | myia.done() |
232 - | |
233 - | def test_dropdeg2(self): |
234 - | """ axes that become degenerate when regridded are dropped if dropdeg=True: CAS-5836""" |
235 - | myia = self._myia |
236 - | imagename = "kbesd.im" |
237 - | myia.fromshape(imagename, [20, 20, 20]) |
238 - | factor = [1, 1, 20] |
239 - | myia.done() |
240 - | outfile = "kyzb5.im" |
241 - | imrebin( |
242 - | imagename=imagename, outfile=outfile, |
243 - | factor=factor, dropdeg=True |
244 - | ) |
245 - | myia.open(outfile) |
246 - | self.assertTrue((myia.shape() == [20,20]).all()) |
247 - | myia.done() |
248 - | |
249 - | def test_history(self): |
250 - | """Test history writing""" |
251 - | myia = self._myia |
252 - | imagename = "zz.im" |
253 - | factor = [1, 1, 20] |
254 - | myia.fromshape(imagename,[20,20,20]) |
255 - | myia.done() |
256 - | outfile = "zz_out.im" |
257 - | imrebin(imagename=imagename, outfile=outfile, factor=factor) |
258 - | myia.open(outfile) |
259 - | msgs = myia.history() |
260 - | myia.done() |
261 - | teststr = "version" |
262 - | self.assertTrue(teststr in msgs[-2], "'" + teststr + "' not found") |
263 - | teststr = "imrebin" |
264 - | self.assertTrue(teststr in msgs[-1], "'" + teststr + "' not found") |
265 - | |
266 - | def suite(): |
267 - | return [imrebin_test] |
268 - | |
269 - | if __name__ == '__main__': |
270 - | unittest.main() |
271 - | |