Ejemplo n.º 1
0
def test_world_to_pixels_no_content():
    header_filename = os.path.join(TESTDATA_DIR, VLASS_4D_CUBE_HEADER)
    header = fits.Header.fromtextfile(header_filename)

    # circle no content
    query = [
        'CIRCLE -168.34719985367971 -76.18699791158396 0.01',
        'BAND 0.04456576 0.11662493', 'POL I'
    ]

    test_subject = Transform()
    try:
        test_subject.world_to_pixels(query, header)
        assert False, 'Should raise NoContentError'
    except NoContentError:
        assert True

    # polygon no content
    query = [
        'POLYGON -168.34 -76.18 -168.34 -76.19 -168.35 -76.19',
        'BAND 0.04456576 0.11662493', 'POL I'
    ]

    test_subject = Transform()
    try:
        test_subject.world_to_pixels(query, header)
        assert False, 'Should raise NoContentError'
    except NoContentError:
        assert True

    # energy no content
    query = [
        'CIRCLE 168.34719985367971 76.18699791158396 0.01',
        'BAND 0.14456576 0.21662493', 'POL I'
    ]

    test_subject = Transform()
    try:
        test_subject.world_to_pixels(query, header)
        assert False, 'Should raise NoContentError'
    except NoContentError:
        assert True

    # polarization no content
    query = [
        'CIRCLE 168.34719985367971 76.18699791158396 0.01',
        'BAND 0.04456576 0.11662493', 'POL LL'
    ]

    test_subject = Transform()
    try:
        test_subject.world_to_pixels(query, header)
        assert False, 'Should raise NoContentError'
    except NoContentError:
        assert True
Ejemplo n.º 2
0
def test_pos_band():
    header_filename = os.path.join(TESTDATA_DIR, CUBE_HEADER)
    header = fits.Header.fromtextfile(header_filename)
    header.append(('RESTFRQ', 1.420406E9))

    world = ['circle 25.0 60.0 0.5', 'BAND 211.0e-3 211.05e-3']

    test_subject = Transform()
    pixel_cutout_hdu = test_subject.world_to_pixels(world, header)

    # SODA returns [0][350:584,136:370,91:177,*]
    assert pixel_cutout_hdu is not None
    ranges = pixel_cutout_hdu.get_ranges()
    assert len(ranges) == 4
    assert ranges[0] == (367, 568)
    assert ranges[1] == (152, 353)
    assert ranges[2] == (91, 178)
    assert ranges[3] == (1, 1)
Ejemplo n.º 3
0
def test_world_to_pixels_vlass():
    """
    CIRCLE 168.34719985367971 76.18699791158396 0.01
    BAND 0.04456576 0.11662493 POL I
    cutout=[0][2938:3062,4191:4316,1:2,1:1]
    """
    header_filename = os.path.join(TESTDATA_DIR, VLASS_4D_CUBE_HEADER)
    header = fits.Header.fromtextfile(header_filename)

    world = [
        'circle 168.34719985367971 76.18699791158396 0.01',
        'BAND 0.04456576 0.11662493', 'POL I'
    ]

    test_subject = Transform()
    pixel_cutout_hdu = test_subject.world_to_pixels(world, header)

    assert pixel_cutout_hdu is not None
    ranges = pixel_cutout_hdu.get_ranges()
    assert len(ranges) == 4
    # assert ranges[0] == (2940, 3061)
    assert ranges[1] == (4193, 4314)
    assert ranges[2] == (1, 3)
    assert ranges[3] == (1, 1)
Ejemplo n.º 4
0
    def _check_hdu_list(self, cutout_dimensions, hdu_list):
        has_match = False
        pixel_matches_left = len(cutout_dimensions)
        for curr_extension_idx, hdu in enumerate(hdu_list):
            # If we encounter a PrimaryHDU, write it at the top and continue.
            if isinstance(hdu, PrimaryHDU) and hdu.data is None:
                logger.debug('Appending Primary from index {}'.format(
                    curr_extension_idx))
                fits.append(filename=self.output_writer,
                            header=hdu.header,
                            data=None,
                            overwrite=False,
                            output_verify='silentfix',
                            checksum='remove')
            elif hdu.is_image:
                header = hdu.header
                ext_name = header.get('EXTNAME')
                ext_ver = header.get('EXTVER', 0)
                curr_ext_name_ver = None

                if ext_name is not None:
                    curr_ext_name_ver = (ext_name, ext_ver)

                try:
                    if isinstance(cutout_dimensions[0], PixelCutoutHDU):
                        for cutout_dimension in cutout_dimensions:
                            if self._is_extension_requested(
                                    curr_extension_idx, curr_ext_name_ver,
                                    cutout_dimension):
                                logger.debug(
                                    '*** Extension {} does match ({} | {})'.
                                    format(cutout_dimension.get_extension(),
                                           curr_extension_idx,
                                           curr_ext_name_ver))
                                pixel_matches_left -= 1
                                self._pixel_cutout(header, hdu.data,
                                                   cutout_dimension)
                                has_match = True

                        if pixel_matches_left == 0:
                            return has_match
                    else:
                        logger.debug('Handling WCS transform.')
                        # Handle WCS transform.
                        transform = Transform()
                        transformed_cutout_dimension = \
                            transform.world_to_pixels(cutout_dimensions, header)
                        logger.debug('Transformed {} into {}'.format(
                            cutout_dimensions, transformed_cutout_dimension))
                        self._pixel_cutout(header, hdu.data,
                                           transformed_cutout_dimension)
                        has_match = True

                except NoContentError:
                    # Skip for now as we're iterating the loop.
                    logger.debug('No overlap with extension {}'.format(
                        curr_extension_idx))

            logger.debug('Finished extension {}'.format(curr_extension_idx))

        logger.debug('Has match in list? -- {}'.format(has_match))
        return has_match
Ejemplo n.º 5
0
    def _check_hdu_list(self, cutout_dimensions, hdu_list):
        has_match = False
        len_cutout_dimensions = len(cutout_dimensions)
        if len_cutout_dimensions > 0:
            result_hdu_list = None
            if self._require_primary_hdu(cutout_dimensions) and \
               hdu_list[0].header['NAXIS'] == 0:
                # add the PrimaryHDU from the original HDU list
                result_hdu_list = fits.HDUList([hdu_list[0]])
            # Check for a pixel cutout
            if isinstance(cutout_dimensions[0], PixelCutoutHDU):
                for cutout_dimension in cutout_dimensions:
                    ext = cutout_dimension.get_extension()
                    ext_idx = hdu_list.index_of(ext)
                    hdu = hdu_list[ext_idx]

                    # Entire extension was requested.
                    if not cutout_dimension.get_ranges():
                        logger.debug(
                            'Appending entire extension {}'.format(ext))
                        result_hdu_list = self._add_hdu(hdu, result_hdu_list)
                        has_match = True
                    else:
                        try:
                            result_hdu_list = self._add_hdu(
                                self._pixel_cutout(hdu, cutout_dimension),
                                result_hdu_list)
                            has_match = True
                            logger.debug(
                                'Successfully cutout from {} ({})'.format(
                                    ext, ext_idx))
                        except NoContentError:
                            logger.debug(
                                'Skipping non-overlapping cutout {}'.format(
                                    cutout_dimension))
            else:
                # Skip the primary as it should be written out already.
                for hdu in hdu_list:
                    if hdu.is_image and hdu.data is not None:
                        transform = Transform()
                        logger.debug(
                            'Transforming {}'.format(cutout_dimensions))
                        transformed_cutout_dimension = \
                            transform.world_to_pixels(
                                cutout_dimensions, hdu.header)
                        logger.debug('Transformed {} into {}'.format(
                            cutout_dimensions, transformed_cutout_dimension))
                        try:
                            result_hdu_list = self._add_hdu(self._pixel_cutout(
                                hdu,
                                transformed_cutout_dimension), result_hdu_list)
                            has_match = True
                        except NoContentError:
                            logger.debug(
                                'Skipping non-overlapping cutout {}'.format(
                                     cutout_dimensions))

            # time to write the cutout file
            result_hdu_list.writeto(
                self.output_writer, output_verify='ignore', checksum='remove')
        else:
            raise NoContentError('No overlap found (No cutout specified).')

        logger.debug('Has match in list? -- {}'.format(has_match))
        return has_match