def test_layout_grid_colrow(): ordering = [[-1, 3, 0], [1, -1, -1], [-1, 2, -1]] expected_row = [[-1, 0, 0], [1, -1, -1], [-1, 2, -1]] expected_col = [[-1, 1, 2], [0, -1, -1], [-1, 1, -1]] grid = LayoutGrid((3, 3), 1.2, ordering=ordering) assert_equal(grid.all.column, expected_col) assert_equal(grid.all.row, expected_row)
def test_layout_grid(): shape = (3, 2) spacing = Quantity(0.1, 'mm') xreflection = True yreflection = True origin = (1, 1) angle = 10 center = create_grid(shape, spacing, center=origin, xreflection=xreflection, yreflection=yreflection, angle=angle) layout = LayoutGrid(shape, spacing, origin=origin, xreflection=xreflection, yreflection=yreflection, angle=angle) assert not hasattr(layout, 'nvertices') assert_same(layout.all.center, center) assert_same(layout.all.removed, np.zeros(shape, bool))
def test_layout_grid_circles(): shape = (3, 2) spacing = 0.1 xreflection = True yreflection = True origin = (1, 1) angle = 10 center = create_grid(shape, spacing, center=origin, xreflection=xreflection, yreflection=yreflection, angle=angle) layout = LayoutGrid(shape, spacing, origin=origin, xreflection=xreflection, yreflection=yreflection, angle=angle, radius=spacing / 2) assert not hasattr(layout, 'nvertices') assert_same(layout.radius, spacing / 2) assert_same(layout.all.center, center) assert_same(layout.all.removed, np.zeros(shape, bool))
def func(s, r): layout = LayoutGrid(shape, s, radius=r, selection=selection) if (not isinstance(s, Quantity) or s.unit == '') and \ isinstance(r, Quantity) and r.unit == 'm': expectedval = 0.4e-3 else: expectedval = 0.4 expected = np.empty(shape) expected[...] = expectedval expected[0, 1] = np.nan assert_same(layout.radius, expectedval, broadcasting=True) assert_same(layout.all.radius, expected) vals = (layout.radius, layout.all.radius, layout.center, layout.all.center) unit = getattr(s, 'unit', '') or getattr(r, 'unit', '') if unit: for val in vals: assert_is_instance(val, Quantity) assert_equal(val.unit, unit) else: r = np.asanyarray(r) expecteds = (type(r), type(r), np.ndarray, np.ndarray) for val, e in zip(vals, expecteds): assert_is_type(val, e)
def get(self, name, *args): """ Access calibration files. Parameters ---------- name : str One of the following: - 'detarray' - 'hornarray' - 'optics' - 'primbeam' """ if name == 'detarray': hdus = fits.open(self.detarray) version = hdus[0].header['format version'] vertex = hdus[2].data frame = hdus[0].header['FRAME'] if frame == 'ONAFP': # Make a pi/2 rotation from ONAFP -> GRF referential frame vertex[..., [0, 1]] = vertex[..., [1, 0]] vertex[..., 1] *= -1 shape = vertex.shape[:-2] removed = hdus[3].data.view(bool) ordering = hdus[4].data quadrant = hdus[5].data efficiency = hdus[6].data return shape, vertex, removed, ordering, quadrant, efficiency elif name == 'hornarray': hdus = fits.open(self.hornarray) version = hdus[0].header['format version'] if version == '1.0': h = hdus[0].header spacing = h['spacing'] center = hdus[1].data shape = center.shape[:-1] layout = Layout(shape, center=center, radius=h['innerrad'], open=None) layout.spacing = spacing elif version == '2.0': h = hdus[0].header spacing = h['spacing'] xreflection = h['xreflection'] yreflection = h['yreflection'] radius = h['radius'] selection = ~hdus[1].data.view(bool) layout = LayoutGrid(removed.shape, spacing, selection=selection, radius=radius, xreflection=xreflection, yreflection=yreflection, open=None) else: h = hdus[1].header spacing = h['spacing'] xreflection = h['xreflection'] yreflection = h['yreflection'] angle = h['angle'] radius = h['radius'] selection = ~hdus[2].data.view(bool) shape = selection.shape layout = HornLayout(shape, spacing, selection=selection, radius=radius, xreflection=xreflection, yreflection=yreflection, angle=angle, startswith1=True, id=None, open=None) layout.id = np.arange(len(layout)) layout.center = np.concatenate( [layout.center, np.full_like(layout.center[..., :1], 0)], -1) layout.open = np.ones(len(layout), bool) return layout elif name == 'optics': dtype = [('name', 'S16'), ('temperature', float), ('transmission', float), ('emissivity', float), ('nstates_pol', int)] if self.optics.endswith('fits'): header = fits.open(self.optics)[0].header return { 'focal length': header['flength'], 'detector efficiency': 1., 'components': np.empty(0, dtype=dtype) } parser = ConfigParser() parser.read(self.optics) # ### The 2 next lines are commented as there is nothing in the section # ### "general" in the optics calibration file. Focal length has been moved to the dictionary. # keys = 'focal length', # out = dict((key, parser.getfloat('general', key)) for key in keys) out = {} raw = parser.items('components') components = np.empty(len(raw), dtype=dtype) for i, r in enumerate(raw): component = (r[0], ) + tuple( float(_) for _ in r[1].split(', ')) components[i] = component out['components'] = components return out elif name == 'primbeam': hdu = fits.open(self.primbeam) header = hdu[0].header # Gaussian beam if header['format version'] == '1.0': fwhm0_deg = header['fwhm'] return fwhm0_deg # Fitted beam elif header['format version'] == '2.0': if (self.nu < 170 and self.nu > 130): omega = hdu[1].header['omega'] par = hdu[1].data else: omega = hdu[2].header['omega'] par = hdu[2].data return par, omega # Multi frequency beam else: parth = hdu[1].data parfr = hdu[2].data parbeam = hdu[3].data alpha = hdu[4].data xspl = hdu[5].data return parth, parfr, parbeam, alpha, xspl raise ValueError('Invalid primary beam calibration version') elif name == 'synthbeam': hdu = fits.open(self.synthbeam) header = hdu[0].header theta = hdu[0].data phi = hdu[1].data val = hdu[2].data freqs = hdu[3].data return theta, phi, val, freqs, header elif name == 'synthbeam_jc': hdu = fits.open(self.synthbeam) header = hdu[0].header theta = hdu[0].data phi = hdu[1].data val = hdu[2].data numpeaks = hdu[3].data return theta, phi, val, numpeaks, header raise ValueError("Invalid calibration item: '{}'".format(name))
def get(self, name, *args): """ Access calibration files. Parameters ---------- name : str One of the following: - 'detarray' - 'hornarray' - 'optics' - 'primbeam' """ if name == 'detarray': hdus = fits.open(self.detarray) version = hdus[0].header['format version'] corner = hdus[2].data shape = corner.shape[:-2] n = shape[0] * shape[1] if version == '1.0': removed = np.zeros(shape, bool) index = np.arange(n, dtype=np.int32).reshape(shape) quadrant = np.zeros(shape, np.int8) efficiency = np.ones(shape) else: removed = hdus[3].data.view(bool) index = hdus[4].data quadrant = hdus[5].data if version > '2.0': efficiency = hdus[6].data else: efficiency = np.ones(shape) return shape, corner, removed, index, quadrant, efficiency elif name == 'hornarray': hdus = fits.open(self.hornarray) version = hdus[0].header['format version'] if version == '1.0': h = hdus[0].header spacing = h['spacing'] center = hdus[1].data shape = center.shape[:-1] layout = Layout(shape, center=center, radius=h['innerrad'], open=None) layout.spacing = spacing elif version == '2.0': h = hdus[0].header spacing = h['spacing'] xreflection = h['xreflection'] yreflection = h['yreflection'] radius = h['radius'] selection = ~hdus[1].data.view(bool) layout = LayoutGrid(removed.shape, spacing, selection=selection, radius=radius, xreflection=xreflection, yreflection=yreflection, open=None) else: h = hdus[1].header spacing = h['spacing'] xreflection = h['xreflection'] yreflection = h['yreflection'] angle = h['angle'] radius = h['radius'] selection = ~hdus[2].data.view(bool) shape = selection.shape layout = HornLayout(shape, spacing, selection=selection, radius=radius, xreflection=xreflection, yreflection=yreflection, angle=angle, startswith1=True, id=None, open=None) layout.id = np.arange(len(layout)) layout.center = np.concatenate( [layout.center, np.full_like(layout.center[..., :1], 0)], -1) layout.open = np.ones(len(layout), bool) return layout elif name == 'optics': dtype = [('name', 'S16'), ('temperature', float), ('transmission', float), ('emissivity', float), ('nstates_pol', int)] if self.optics.endswith('fits'): header = fits.open(self.optics)[0].header return { 'focal length': header['flength'], 'detector efficiency': 1., 'components': np.empty(0, dtype=dtype) } parser = ConfigParser() parser.read(self.optics) keys = 'focal length', out = dict((key, parser.getfloat('general', key)) for key in keys) raw = parser.items('components') components = np.empty(len(raw), dtype=dtype) for i, r in enumerate(raw): component = (r[0], ) + tuple( float(_) for _ in r[1].split(', ')) components[i] = component out['components'] = components return out elif name == 'primbeam': header = fits.open(self.primbeam)[0].header fwhm0_deg = header['fwhm'] return fwhm0_deg # nu0 = hdu['freq'] # return lambda nu: fwhm0_deg * (nu0 / nu) raise ValueError("Invalid calibration item: '{}'".format(name))