Example #1
0
def build_position(hdf5_wcs, window, telescope):
    h = Header()
    # logging.error(hdf5_wcs)
    # logging.error(window)
    h['NAXIS1'] = window['X1'].data[telescope] - window['X0'].data[
        telescope] + 1
    h['NAXIS2'] = window['Y1'].data[telescope] - window['Y0'].data[
        telescope] + 1
    h['CRVAL1'] = hdf5_wcs['CRVAL1'].data[telescope]
    h['CRVAL2'] = hdf5_wcs['CRVAL2'].data[telescope]
    h['CRPIX1'] = hdf5_wcs['CRPIX1'].data[telescope]
    h['CRPIX2'] = hdf5_wcs['CRPIX2'].data[telescope]
    # h['CDELT1'] = 4.0 / 3600.0
    # h['CDELT2'] = 4.0 / 3600.0
    #
    # JJK - use only one of CD* or CDELT* - they are either conflicting or
    # redundant
    #
    h['CD1_1'] = hdf5_wcs['CD1_1'].data[telescope]
    h['CD1_2'] = hdf5_wcs['CD1_2'].data[telescope]
    h['CD2_1'] = hdf5_wcs['CD2_1'].data[telescope]
    h['CD2_2'] = hdf5_wcs['CD2_2'].data[telescope]
    w = wcs.WCS()
    bounds = CoordPolygon2D()
    result = w.calc_footprint(header=h)
    for ii in result:
        vertex = ValueCoord2D(ii[0], ii[1])
        bounds.vertices.append(vertex)

    # ref_coord_x = RefCoord(mc.to_float(pix1), ra)
    # ref_coord_y = RefCoord(mc.to_float(pix2), dec)

    # coord = Coord2D(ref_coord_x, ref_coord_y)
    # dimension = Dimension2D(2, 2)

    # pixscale = 4.0 / 3600.0 => 4", per JJK
    # VLASS takes the cd?? approach shown here

    # function = CoordFunction2D(dimension=dimension,
    #                            ref_coord=coord,
    #                            cd11=4.0/3600.0,
    #                            cd12=0.0,
    #                            cd21=0.0,
    #                            cd22=4.0/3600.0)
    axis = CoordAxis2D(axis1=Axis(ctype='RA---TAN', cunit='deg'),
                       axis2=Axis(ctype='DEC--TAN', cunit='deg'),
                       error1=None,
                       error2=None,
                       range=None,
                       bounds=bounds,
                       function=None)

    return SpatialWCS(axis=axis,
                      coordsys='FK5',
                      equinox=2000.0,
                      resolution=None)
Example #2
0
def test_exec_footprintfinder():
    test_obs_file = 'fpf_start_obs.xml'
    test_obs = mc.read_obs_from_file(
        os.path.join(tc.TEST_DATA_DIR, test_obs_file))
    test_chunk = \
        test_obs.planes[
            'VLASS1.2.T07t14.J084202-123000.quicklook.v1'
        ].artifacts[
            'ad:VLASS/VLASS1.2.ql.T07t14.J084202-123000.10.2048.v1.I.iter1.'
            'image.pbcor.tt0.subim.fits'
        ].parts[
            '0'
        ].chunks.pop()
    test_file_id = 'VLASS1.2.ql.T24t07.J065836+563000.10.2048.v1.I.iter1.' \
                   'image.pbcor.tt0.subim'
    test_file = os.path.join(tc.TEST_FILES_DIR, f'{test_file_id}.fits')
    if not os.path.exists(test_file):
        shutil.copy(
            f'/usr/src/app/vlass2caom2/int_test/test_files/'
            f'{test_file_id}.fits', test_file)
    test_log_dir = os.path.join(tc.TEST_DATA_DIR, 'logs')
    assert test_chunk is not None, 'chunk expected'
    assert test_chunk.position is not None, 'position expected'
    assert test_chunk.position.axis is not None, 'axis expected'
    assert test_chunk.position.axis.bounds is None, 'bounds not expected'

    cc.exec_footprintfinder(test_chunk, test_file, test_log_dir, test_file_id,
                            '-t 10')
    assert test_chunk is not None, 'chunk unchanged'
    assert test_chunk.position is not None, 'position unchanged'
    assert test_chunk.position.axis is not None, 'axis unchanged'
    assert test_chunk.position.axis.bounds is not None, 'bounds expected'
    assert (len(test_chunk.position.axis.bounds.vertices) == 17
            ), 'wrong number of vertices'
    assert (test_chunk.position.axis.bounds.vertices[0] == ValueCoord2D(
        coord1=105.188421, coord2=55.98216)), 'wrong first vertex'
    assert (test_chunk.position.axis.bounds.vertices[16] == ValueCoord2D(
        coord1=105.165491, coord2=56.050318)), 'wrong last vertex'

    if os.path.exists(test_file):
        os.unlink(test_file)
Example #3
0
def _update_position(chunk, science_fqn):
    """This function assumes that if the code got here, the science file is
    on disk."""
    logging.debug('Begin _update_position')
    assert isinstance(chunk, Chunk), 'Expecting type Chunk'

    if (chunk.position is not None and chunk.position.axis is not None):
        logging.debug('position exists, calculate footprints for {}.'.format(
            science_fqn))
        full_area, footprint_xc, footprint_yc, ra_bary, dec_bary, \
            footprintstring, stc = footprintfinder.main(
                '-r -f  {}'.format(science_fqn))
        logging.debug('footprintfinder result: full area {} '
                      'footprint xc {} footprint yc {} ra bary {} '
                      'dec_bary {} footprintstring {} stc {}'.format(
                          full_area, footprint_xc, footprint_yc, ra_bary,
                          dec_bary, footprintstring, stc))
        bounds = CoordPolygon2D()
        coords = None
        fp_results = stc.split('Polygon FK5')
        if len(fp_results) > 1:
            coords = fp_results[1].split()
        else:
            fp_results = stc.split('Polygon ICRS')
            if len(fp_results) > 1:
                coords = fp_results[1].split()

        if coords is None:
            raise mc.CadcException('Do not recognize footprint {}'.format(stc))

        index = 0
        while index < len(coords):
            vertex = ValueCoord2D(mc.to_float(coords[index]),
                                  mc.to_float(coords[index + 1]))
            bounds.vertices.append(vertex)
            index += 2
            logging.debug('Adding vertex\n{}'.format(vertex))
        chunk.position.axis.bounds = bounds
    else:
        logging.info('No position information for footprint generation.')
    logging.debug('Done _update_position.')
Example #4
0
def exec_footprintfinder(chunk,
                         science_fqn,
                         log_file_directory,
                         obs_id,
                         params='-f'):
    """Execute the footprintfinder on a file. All preconditions for successful
    execution should be in place i.e. the file exists, and is unzipped (because
    that is faster).

    :param chunk The CAOM Chunk that will have Position Bounds information
        added
    :param science_fqn A string of the fully-qualified file name for
        footprintfinder to run on
    :param log_file_directory A string of the fully-qualified name for the log
        directory, where footprintfinder output files will be moved to, after
        execution
    :param obs_id specifies location where footprintfinder log files end up
    :param params specific footprintfinder parameters by collection - default
        forces full-chip, regardless of illumination
    """
    logging.debug(f'Begin _update_position for {obs_id}')
    mc.check_param(chunk, Chunk)

    # local import because footprintfinder depends on matplotlib being
    # installed, which is not declared as a caom2pipe dependency
    import footprintfinder

    if (chunk.position is not None and chunk.position.axis is not None):
        logging.debug(
            f'position exists, calculate footprints for {science_fqn}.')
        for parameters in [params, f'{params} -m 0.2', '-f']:
            # try in decreasing fidelity to get a Polygon that is supported
            # by CAOM's Polygon/MultiPolygon structures
            #
            # -m 0.2 fewer points
            # -f full chip

            full_area, footprint_xc, footprint_yc, ra_bary, dec_bary, \
                footprintstring, stc = footprintfinder.main(
                    f'-r {parameters} {science_fqn}')
            logging.debug(f'footprintfinder result: full area {full_area} '
                          f'footprint xc {footprint_xc} footprint yc '
                          f'{footprint_yc} ra bary {ra_bary} dec_bary '
                          f'{dec_bary} footprintstring {footprintstring} '
                          f'stc {stc}')
            coords = None
            fp_results = stc.split('Polygon FK5')
            if len(fp_results) > 1:
                coords = fp_results[1].split()
            else:
                fp_results = stc.split('Polygon ICRS')
                if len(fp_results) > 1:
                    coords = fp_results[1].split()

            if coords is not None:
                break

        if coords is None:
            raise mc.CadcException(F'Do not recognize footprint {stc}')

        bounds = CoordPolygon2D()
        index = 0
        while index < len(coords):
            vertex = ValueCoord2D(mc.to_float(coords[index]),
                                  mc.to_float(coords[index + 1]))
            bounds.vertices.append(vertex)
            index += 2
            logging.debug(f'Adding vertex\n{vertex}')
        chunk.position.axis.bounds = bounds

        prefix = os.path.basename(science_fqn).replace('.fits', '')
        return_file = f'{prefix}_footprint.txt'
        return_string_file = f'{prefix}_footprint_returnstring.txt'
        _handle_footprint_logs(log_file_directory, return_file)
        _handle_footprint_logs(log_file_directory, return_string_file)

    else:
        logging.info('No position information for footprint generation.')
    logging.debug('Done _update_position.')
Example #5
0
tele = 0
h = Header()
h['NAXIS1'] = t_window['X1'].data[tele] - t_window['X0'].data[tele] + 1
h['NAXIS2'] = t_window['Y1'].data[tele] - t_window['Y0'].data[tele] + 1
h['CRVAL1'] = t_wcs['CRVAL1'].data[tele]
h['CRVAL2'] = t_wcs['CRVAL2'].data[tele]
# h['CRVAL1'] = t_c['RA'].data[0]
# h['CRVAL2'] = t_c['DEC'].data[0]
h['CRPIX1'] = t_wcs['CRPIX1'].data[tele]
h['CRPIX2'] = t_wcs['CRPIX2'].data[tele]
# h['CRPIX1'] = 1.0
# h['CRPIX2'] = 1.0
h['CDELT1'] = 4.0 / 3600.0
h['CDELT2'] = 4.0 / 3600.0
h['CD1_1'] = t_wcs['CD1_1'].data[tele]
h['CD1_2'] = t_wcs['CD1_2'].data[tele]
h['CD2_1'] = t_wcs['CD2_1'].data[tele]
h['CD2_2'] = t_wcs['CD2_2'].data[tele]

from caom2 import CoordPolygon2D, ValueCoord2D
import logging
w = WCS()
bounds = CoordPolygon2D()
result = w.calc_footprint(header=h)
for ii in result:
    logging.error(type(ii))
    vertex = ValueCoord2D(ii[0], ii[1])
    bounds.vertices.append(vertex)