Beispiel #1
0
    def _get_detector_layout(self, ngrids, nep, fknee, fslope, ncorr, tau):
        shape, vertex, removed, index, quadrant, efficiency = \
            self.calibration.get('detarray')
        if ngrids == 2:
            shape = (2,) + shape
            vertex = np.array([vertex, vertex])
            removed = np.array([removed, removed])
            index = np.array([index, index + np.max(index) + 1], index.dtype)
            quadrant = np.array([quadrant, quadrant + 4], quadrant.dtype)
            efficiency = np.array([efficiency, efficiency])
        focal_length = self.calibration.get('optics')['focal length']
        vertex = np.concatenate([vertex, np.full_like(vertex[..., :1], -focal_length)], -1)

        def theta(self):
            return np.arctan2(
                np.sqrt(np.sum(self.center[..., :2]**2, axis=-1)),
                self.center[..., 2])

        def phi(self):
            return np.arctan2(self.center[..., 1], self.center[..., 0])

        layout = Layout(
            shape, vertex=vertex, selection=~removed, ordering=index,
            quadrant=quadrant, nep=nep, fknee=fknee, fslope=fslope,
            tau=tau, theta=theta, phi=phi, efficiency=efficiency)

        # assume all detectors have the same area
        layout.area = surface_simple_polygon(layout.vertex[0, :, :2])
        layout.ncorr = ncorr
        layout.ngrids = ngrids
        return layout
Beispiel #2
0
    def _get_detector_layout(self, ngrids, nep, fknee, fslope, ncorr, tau):
        shape, vertex, removed, index, quadrant, efficiency = \
            self.calibration.get('detarray')
        if ngrids == 2:
            shape = (2,) + shape
            vertex = np.array([vertex, vertex])
            removed = np.array([removed, removed])
            index = np.array([index, index + np.max(index) + 1], index.dtype)
            quadrant = np.array([quadrant, quadrant + 4], quadrant.dtype)
            efficiency = np.array([efficiency, efficiency])
        focal_length = self.calibration.get('optics')['focal length']

        vertex = np.concatenate(
            [vertex, np.full_like(vertex[..., :1], -focal_length)], -1)

        def theta(self):
            return np.arctan2(
                np.sqrt(np.sum(self.center[..., :2]**2, axis=-1)),
                self.center[..., 2])

        def phi(self):
            return np.arctan2(self.center[..., 1], self.center[..., 0])

        layout = Layout(
            shape, vertex=vertex, selection=~removed, ordering=index,
            quadrant=quadrant, nep=nep, fknee=fknee, fslope=fslope,
            tau=tau, theta=theta, phi=phi, efficiency=efficiency)

        # assume all detectors have the same area
        layout.area = surface_simple_polygon(layout.vertex[0, :, :2])
        layout.ncorr = ncorr
        layout.ngrids = ngrids
        return layout
Beispiel #3
0
def test_layout_vertex():
    shape = (4, 4)
    vertex = create_grid_squares(shape, 0.1, filling_factor=0.8)
    center = np.mean(vertex, axis=-2)
    get_vertex = lambda: vertex.reshape(-1, 4, 2)

    class Vertex1(Layout):
        @property
        def vertex(self):
            return get_vertex()

    class Vertex2(Layout):
        def vertex(self):
            return get_vertex()

    layouts = (Layout(shape, vertex=vertex), Layout(shape, vertex=get_vertex),
               Vertex1(shape), Vertex2(shape))

    def func(layout):
        assert_equal(layout.nvertices, 4)
        assert_same(layout.center, center.reshape((-1, 2)))
        assert_same(layout.all.center, center)
        assert_same(layout.vertex, vertex.reshape((-1, 4, 2)))
        assert_same(layout.all.vertex, vertex)

    for layout in layouts:
        yield func, layout
Beispiel #4
0
def test_layout():
    shape = (4, 4)
    center = create_grid(shape, 0.1)
    get_center = lambda: center.reshape(-1, 2)

    class Spatial1(Layout):
        @property
        def center(self):
            return get_center()

    class Spatial2(Layout):
        def center(self):
            return get_center()

    layouts = (Layout(shape, center=center), Layout(shape, center=get_center),
               Spatial1(shape), Spatial2(shape))

    def func(layout):
        assert_same(layout.center, center.reshape(-1, 2))
        assert_same(layout.all.center, center)

    for layout in layouts:
        yield func, layout
Beispiel #5
0
    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))
Beispiel #6
0
    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))
Beispiel #7
0
    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))