Пример #1
0
def main(args):
    from astropy import coordinates
    from astropy import units as u

    from ESOAsg import archive_observations
    from ESOAsg import msgs
    from ESOAsg.ancillary import cleaning_lists

    ra_list = cleaning_lists.from_element_to_list(args.ra_degree,
                                                  element_type=float)
    dec_list = cleaning_lists.from_element_to_list(args.dec_degree,
                                                   element_type=float)
    if args.radius is None:
        radius = None
    else:
        radius = cleaning_lists.from_element_to_list(args.radius,
                                                     element_type=float)[0]
    instruments = cleaning_lists.from_element_to_list(args.instruments,
                                                      element_type=str)
    data_types = cleaning_lists.from_element_to_list(args.data_types,
                                                     element_type=str)

    msgs.start()
    for ra, dec in zip(ra_list, dec_list):
        position = coordinates.SkyCoord(ra=ra * u.degree,
                                        dec=dec * u.degree,
                                        frame='fk5')
        msgs.newline()
        msgs.info('Query for ESO archival data around the position: {}'.format(
            position.to_string('hmsdms')))
        result_from_query = archive_observations.query_from_radec(
            position,
            radius=radius,
            instruments=instruments,
            data_types=data_types,
            verbose=False)
        if len(result_from_query['dp_id']) > 0:
            archive_observations.download(result_from_query['dp_id'])
    msgs.end()
Пример #2
0
EXAMPLES = r"""
        Example:
        fix_muse_p197.py --input_fits 
        """

if __name__ == '__main__':
    args = parse_arguments()

    # getting fits names
    input_fits = np.str(args.input_fits[0])
    if args.output_fits is None:
        output_fits = input_fits.replace('.fits', '_fixed.fits')
    else:
        output_fits = np.str(args.output_fits[0])

    msgs.start()

    hdul_original = fits.open(input_fits)




    msgs.work('Updating checksum and datasum')
    hdul[0].add_datasum()
    hdul[1].add_datasum()
    hdul[0].add_checksum(override_datasum=True)
    hdul[1].add_checksum(override_datasum=True)
    msgs.work('Flushing changes.')
    hdul.flush()
    hdul.close()
Пример #3
0
def main(args):
    import numpy as np
    import os
    import shutil
    from astropy.coordinates import SkyCoord
    from astropy.coordinates import name_resolve
    from astropy import units as u
    from astropy.io import fits
    from ESOAsg.ancillary import cleaning_lists
    from ESOAsg.core import fitsfiles
    from ESOAsg import msgs

    # Cleaning input lists
    input_fits_files = cleaning_lists.make_list_of_fits_files(args.input_fits)
    # Make whitelight images
    if args.whitelight:
        make_whitelight_image = True
    else:
        make_whitelight_image = False
    # Creating output list
    if args.suffix is None:
        overwrite = False
        msgs.warning('The file will overwrite the input files')
    else:
        overwrite = True
    suffix_string = cleaning_lists.make_string(args.suffix)
    output_fits_files = cleaning_lists.make_list_of_fits_files_and_append_suffix(
        input_fits_files, suffix=suffix_string)
    if make_whitelight_image:
        output_whitelight_files = cleaning_lists.make_list_of_fits_files_and_append_suffix(
            input_fits_files, suffix=suffix_string + '_WL')
    else:
        output_whitelight_files = [None] * len(input_fits_files)
    '''
    # reference
    if args.referenc is not None:
        reference = str(args.referenc[0])
    else:
        reference = str(' ')

    # fluxcal
    if args.fluxcal == 'ABSOLUTE':
        fluxcal = 'ABSOLUTE'
    elif args.fluxcal == 'UNCALIBRATED':
        fluxcal = 'UNCALIBRATED'
    else:
        msgs.error('Possible values for fluxcal are: `ABSOLUTE` or `UNCALIBRATED`')

    # abmaglim
    if args.abmaglim is not None:
        abmaglim = args.abmaglim
        assert isinstance(abmaglim, (int, np.float_)), 'ABMAGLIM must be a float'
        if abmaglim < 0:
            msgs.error('ABMAGLIM must be positive')
    else:
        abmaglim = np.float_(-1.)
    '''

    msgs.start()

    for fits_in, fits_out, image_out in zip(input_fits_files,
                                            output_fits_files,
                                            output_whitelight_files):
        if os.path.exists(fits_out):
            shutil.copy(fits_out, fits_out.replace('.fit', '_old.fit'))
            msgs.warning('{} already exists. Backup created.'.format(fits_out))
        if image_out is not None:
            if os.path.exists(image_out):
                shutil.copy(image_out, image_out.replace('.fit', '_old.fit'))
                msgs.warning(
                    '{} already exists. Backup created.'.format(image_out))

        full_hdul = fitsfiles.get_hdul(fits_in)
        try:
            instrument = full_hdul[0].header['HIERARCH ESO SEQ ARM']
        except KeyError:
            msgs.error(
                'Failed to read the keyword HIERARCH ESO SEQ ARM from the primary header'
            )
        finally:
            if instrument in SUPPORTED_INSTRUMENT:
                msgs.info(
                    'The input file is from SPHERE/{}'.format(instrument))
            else:
                msgs.warning(
                    'Instrument SPHERE/{} not supported'.format(instrument))

        # ToDo
        # These needs to be transformed in objects
        if instrument.startswith('IFS'):
            msgs.work('Fixing header for SPHERE/{} file {}'.format(
                instrument, fits_in))

            # Create a copy of the file where there is a primary HDU and data are in the 'DATA" HDU
            msgs.work('Reshaping cube into PrimaryHEADER and Data Header')
            fitsfiles.new_fits_like(fits_in, [0],
                                    fits_out,
                                    overwrite=overwrite,
                                    fix_header=True)
            hdul = fitsfiles.get_hdul(fits_out, 'update', checksum=True)
            hdr0 = hdul[0].header
            hdr1 = hdul[1].header

            # Check for HISTORY
            # Primary Header
            if 'HISTORY' in hdr0.keys():
                history_cards_hdr0 = [
                    history_card_hdr0 for history_card_hdr0 in hdr0
                    if history_card_hdr0.startswith('HISTORY')
                ]
                history_values_hdr0 = [
                    hdr0[history_card_hdr0] for history_card_hdr0 in hdr0
                    if history_card_hdr0.startswith('HISTORY')
                ]
                for history_card_hdr0, history_value_hdr0 in zip(
                        history_cards_hdr0, history_values_hdr0):
                    msgs.work('Cleaning cards: {} = {}'.format(
                        history_card_hdr0, history_value_hdr0))
                del hdr0['HISTORY'][:]
            # Data Header
            if 'HISTORY' in hdr1.keys():
                history_values_hdr1 = hdr1['HISTORY'][:]
                for history_number in range(0, len(history_values_hdr1)):
                    clean_history = cleaning_lists.remove_non_ascii(
                        history_values_hdr1[history_number])
                    if len(clean_history) > 0:
                        hdr1['HISTORY'][history_number] = str(clean_history)
                    else:
                        hdr1['HISTORY'][history_number] = str(' ')

            # Update cards for headers:
            # Updating values with different CARD in the header
            cards_input = [
                'CRPIX4', 'CRVAL4', 'CTYPE4', 'CUNIT4', 'CD4_4', 'CD1_4',
                'CD2_4', 'CD4_1', 'CD4_2'
            ]
            cards_output = [
                'CRPIX3', 'CRVAL3', 'CTYPE3', 'CUNIT3', 'CD3_3', 'CD1_3',
                'CD2_3', 'CD3_1', 'CD3_2'
            ]
            fitsfiles.transfer_header_cards(hdr1,
                                            hdr1,
                                            cards_input,
                                            output_cards=cards_output,
                                            delete_card=True)
            # Remove not used values
            cards_to_be_removed_hdr1 = ['CD4_3', 'CD3_4']
            for card_to_be_removed_hdr1 in cards_to_be_removed_hdr1:
                hdr1.remove(card_to_be_removed_hdr1, ignore_missing=True)

            # Transfer cards from HDU1 to the PrimaryHDU
            not_to_be_transfer = [
                hdr1_card for hdr1_card in hdr1
                if hdr1_card.startswith('COMMENT') or hdr1_card.startswith(
                    'EXTNAME') or hdr1_card.startswith('BITPIX') or
                hdr1_card.startswith('NAXIS') or hdr1_card.startswith('CRPIX')
                or hdr1_card.startswith('CRVAL') or hdr1_card.startswith(
                    'CDELT') or hdr1_card.startswith('CTYPE')
                or hdr1_card.startswith('CD1_') or hdr1_card.startswith('CD2_')
                or hdr1_card.startswith('CD3_') or hdr1_card.startswith(
                    'CUNIT') or hdr1_card.startswith('CSYER') or hdr1_card.
                startswith('HDUCLAS') or hdr1_card.startswith('XTENSION')
                or hdr1_card.startswith('PCOUNT') or hdr1_card.startswith(
                    'GCOUNT') or hdr1_card.startswith('HDUDOC') or hdr1_card.
                startswith('HDUVER') or hdr1_card.startswith('HISTORY')
            ]
            cards_to_be_transfer = []
            for hdr1_card in hdr1:
                if hdr1_card not in not_to_be_transfer:
                    cards_to_be_transfer.append(hdr1_card)
            fitsfiles.transfer_header_cards(hdr1,
                                            hdr0,
                                            cards_to_be_transfer,
                                            with_comment=True,
                                            delete_card=True)

            # Try to guess coordinates
            if 'CRVAL1' not in hdr1.keys():
                msgs.warning('CRVAL position keywords not preset')
                if 'OBJECT' in hdr0.keys():
                    try:
                        object_coordinate = SkyCoord.from_name(
                            str(hdr0['OBJECT']).strip())
                        ra_obj, dec_obj = object_coordinate.ra.degree, object_coordinate.dec.degree
                        if 'RA' in hdr0.keys() and 'DEC' in hdr0.keys():
                            pointing_coordinate = SkyCoord(float(hdr0['RA']),
                                                           float(hdr0['DEC']),
                                                           unit='deg')
                            msgs.work(
                                'Testing from separation from pointing position'
                            )
                            separation = object_coordinate.separation(
                                pointing_coordinate).arcsec
                            if separation < 120.:
                                msgs.info('Object - Pointing separation is {}'.
                                          format(separation))
                                msgs.info(
                                    'Updating CRVAL1 = {}'.format(ra_obj))
                                msgs.info(
                                    'Updating CRVAL2 = {}'.format(dec_obj))
                                hdr1['CRVAL1'] = ra_obj
                                hdr1['CRVAL2'] = dec_obj
                                msgs.work('Updating CUNIT')
                                hdr1['CUNIT1'] = 'deg'
                                hdr1['CUNIT2'] = 'deg'
                                msgs.work('Updating CTYPE')
                                hdr1['CTYPE1'] = 'RA---TAN'
                                hdr1['CTYPE2'] = 'DEC--TAN'
                                msgs.work('Updating CRPIX')
                                hdr1['CRPIX1'] = float(
                                    hdul[1].data.shape[2]) / 2.
                                hdr1['CRPIX2'] = float(
                                    hdul[1].data.shape[1]) / 2.
                                msgs.info('Updating CD1 and CD2')
                                hdr1['CD1_1'] = 2.06E-06
                                hdr1['CD2_2'] = 2.06E-06
                                hdr1['CD1_2'] = 0.
                                hdr1['CD2_1'] = 0.
                                msgs.work('Updating RA, DEC')
                                hdr0['RA'] = ra_obj
                                hdr0.comments[
                                    'RA'] = object_coordinate.ra.to_string(
                                        u.hour)
                                hdr0['DEC'] = dec_obj
                                hdr0.comments[
                                    'DEC'] = object_coordinate.dec.to_string(
                                        u.degree, alwayssign=True)
                            else:
                                msgs.warning(
                                    'Object - Pointing separation is {}'.
                                    format(separation))
                                msgs.warning(
                                    'This is suspicious, CRVAL not updated')
                    except name_resolve.NameResolveError:
                        msgs.warning('Object {} not recognized'.format(
                            str(hdr0['OBJECT']).strip()))
                        msgs.warning('CRVAL not updated')

            # Updating file prodcatg
            msgs.work('Updating PRODCATG to SCIENCE.CUBE.IFS')
            hdr0['PRODCATG'] = str('SCIENCE.CUBE.IFS')
            # Some more updates
            msgs.work('Setting NAXIS = 0 in primary header')
            hdr0['NAXIS'] = 0
            if 'OBSTECH' not in hdr0.keys():
                msgs.warning('OBSTECH missing')
                if 'ESO PRO TECH' in hdr0.keys():
                    msgs.info('Deriving OBSTECH from HIERARCH ESO PRO TECH')
                    msgs.work('Updating OBSTECH to {}'.format(
                        str(hdr0['HIERARCH ESO PRO TECH'])))
                    hdr0['OBSTECH'] = str(hdr0['HIERARCH ESO PRO TECH'])
            if 'EXPTIME' not in hdr0.keys():
                msgs.warning('EXPTIME missing')
                if 'ESO DET SEQ1 REALDIT' in hdr0.keys(
                ) and 'ESO DET NDIT' in hdr0.keys():
                    msgs.info('Deriving EXPTIME and TEXPTIME as REALDIT * DIT')
                    hdr0['EXPTIME'] = hdr0[
                        'HIERARCH ESO DET SEQ1 REALDIT'] * hdr0[
                            'HIERARCH ESO DET NDIT']
                    hdr0['TEXPTIME'] = hdr0[
                        'HIERARCH ESO DET SEQ1 REALDIT'] * hdr0[
                            'HIERARCH ESO DET NDIT']
                    msgs.work('Updating EXPTIME to {}'.format(
                        str(hdr0['EXPTIME'])))
                    msgs.work('Updating TEXPTIME to {}'.format(
                        str(hdr0['TEXPTIME'])))
            if 'WAVELMIN' not in hdr0.keys():
                msgs.warning('WAVELMIN missing')
                z_pixel = np.arange(int(hdul[1].data.shape[0]))
                z_wave = float(
                    hdr1['CRVAL3']) + (z_pixel * float(hdr1['CD3_3']))
                if str(hdr1['CUNIT3']).strip().upper() == 'MICRONS':
                    msgs.info('Deriving WAVELMIN and WAVELMAX from CRVAL1')
                    z_wave = z_wave * 1000.  # convert to nanometers
                    hdr0['WAVELMIN'] = np.nanmin(z_wave)
                    hdr0['WAVELMAX'] = np.nanmax(z_wave)
                    msgs.work('Updating WAVELMIN to {}'.format(
                        str(hdr0['WAVELMIN'])))
                    msgs.work('Updating WAVELMAX to {}'.format(
                        str(hdr0['WAVELMAX'])))
                else:
                    msgs.warning(
                        'Unknown units {}. WAVELMIN and WAVELMAX not calculated'
                        .format(str(hdr1['CUNIT3'])))
            if 'SPEC_RES' not in hdr0.keys():
                msgs.warning('SPEC_RES missing')
                if 'WAVELMAX' in hdr0.keys():
                    msgs.info('Deriving SPEC_RES from WAVELMAX')
                    if (float(hdr0['WAVELMAX']) > 1300.) and (float(
                            hdr0['WAVELMAX']) < 1400.):
                        hdr0['SPEC_RES'] = 50.
                        msgs.work('Updating SPEC_RES to {}'.format(
                            str(hdr0['SPEC_RES'])))
                    elif (float(hdr0['WAVELMAX']) > 1600.) and (float(
                            hdr0['WAVELMAX']) < 1700.):
                        hdr0['SPEC_RES'] = 30.
                        msgs.work('Updating SPEC_RES to {}'.format(
                            str(hdr0['SPEC_RES'])))
                    else:
                        msgs.warning('WAVELMAX = {} is not in the expected ' /
                                     + 'range of possible values'.format(
                                         str(hdr0['WAVELMAX'])))
            if 'PROGID' not in hdr0.keys():
                msgs.warning('PROG_ID missing')
                if 'ESO OBS PROG ID' in hdr0.keys():
                    msgs.info('Deriving PROG_ID from HIERARCH ESO OBS PROG ID')
                    msgs.work('Updating PROG_ID to {}'.format(
                        str(hdr0['HIERARCH ESO OBS PROG ID'])))
                    hdr0['PROG_ID'] = str(hdr0['HIERARCH ESO OBS PROG ID'])
            if 'MJD-END' not in hdr0.keys():
                msgs.warning('MJD-END missing')
                if 'TEXPTIME' in hdr0.keys():
                    msgs.info('Deriving MJD-END from MJD-OBS and TEXPTIME')
                    texptime_sec = float(hdr0['TEXPTIME'])
                    texptime_day = texptime_sec / (60. * 60. * 24.)
                    mjdend = float(hdr0['MJD-OBS']) + texptime_day
                    fitsfiles.add_header_card(hdr0, 'MJD-END', mjdend,
                                              'End of observation')
                    msgs.work('MJD-OBS = {} and TEXPTIME = {} days'.format(
                        str(hdr0['MJD-OBS']), str(texptime_day)))
                    msgs.work('Updating MJD-END to {}'.format(
                        str(hdr0['MJD-END'])))

            # Remove not used values
            cards_to_be_removed_hdr0 = ['ERRDATA', 'QUALDATA', 'SCIDATA']
            for card_to_be_removed_hdr0 in cards_to_be_removed_hdr0:
                hdr0.remove(card_to_be_removed_hdr0, ignore_missing=True)
            cards_to_be_removed_hdr1 = ['HDUCLASS3']
            for card_to_be_removed_hdr1 in cards_to_be_removed_hdr1:
                hdr1.remove(card_to_be_removed_hdr1, ignore_missing=True)

            # Updating the FITS file definition comment line
            hdr0.add_comment(
                "  FITS (Flexible Image Transport System) format is defined in 'Astronomy"
                + "  and "
                "Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H",
                after='EXTEND')
            if 'COMMENT' in hdr1.keys():
                comment_values_hdr1 = hdr1['COMMENT'][:]
                for index, comment_value_hdr1 in enumerate(
                        comment_values_hdr1):
                    msgs.work('Removing COMMENT card : {}'.format(
                        comment_value_hdr1))
                hdr1.remove('COMMENT', ignore_missing=True, remove_all=True)

            # Creating white light image keyword:
            if make_whitelight_image:
                fitsfiles.add_header_card(
                    hdr0, 'ASSON1',
                    image_out.split('/')[-1],
                    'ANCILLARY.IMAGE.WHITELIGHT filename')
                msgs.work('Updating ASSON1 to {}'.format(hdr0['ASSON1']))

            # Actually creating the white-light image
            if make_whitelight_image:
                msgs.info('Making white light image')
                image_hdu = fits.PrimaryHDU()
                image_hdul = fits.HDUList([image_hdu])
                if str(hdr1['CUNIT3']).strip().upper() == 'MICRONS':
                    to_ang = 10000.
                else:
                    msgs.error('Spectral unit: {} not recognized'.format(
                        hdr1['CUNIT3']))
                delta_wave_bin = hdr1['CD3_3']
                image_hdul.append(
                    fits.ImageHDU(
                        to_ang * delta_wave_bin *
                        np.nansum(hdul[1].data, axis=0, dtype=np.float_)))
                image_hdr0 = image_hdul[0].header
                image_hdr1 = image_hdul[1].header
                card_for_image0 = [
                    'WAVELMIN', 'WAVELMAX', 'OBJECT', 'TELESCOP', 'INSTRUME',
                    'RADECSYS', 'RA', 'DEC', 'EQUINOX'
                ]
                fitsfiles.transfer_header_cards(hdr0,
                                                image_hdr0,
                                                card_for_image0,
                                                with_comment=True,
                                                delete_card=False)
                image_hdr0['PRODCATG'] = str('ANCILLARY.IMAGE.WHITELIGHT')

                card_for_image1 = [
                    'CRPIX1', 'CRPIX2', 'CRVAL1', 'CRVAL2', 'CUNIT1', 'CUNIT2',
                    'NAXIS1', 'NAXIS2', 'EXTNAME', 'CD1_1', 'CD1_2', 'CD2_1',
                    'CD2_2', 'CTYPE1', 'CTYPE2'
                ]
                fitsfiles.transfer_header_cards(hdr1,
                                                image_hdr1,
                                                card_for_image1,
                                                with_comment=True,
                                                delete_card=False)

            # Update checksum and datasum
            msgs.work('Updating checksum and datasum')
            hdul[0].add_checksum(override_datasum=False)
            hdul[1].add_datasum()
            hdul[1].add_checksum(override_datasum=True)
            hdul.flush(output_verify='fix')
            hdul.close()
            msgs.info('File {} produced.'.format(fits_out))
            if make_whitelight_image:
                image_hdul[0].add_datasum()
                image_hdul[1].add_datasum()
                image_hdul[0].add_checksum(override_datasum=True)
                image_hdul[1].add_checksum(override_datasum=True)
                image_hdul.writeto(image_out,
                                   overwrite=True,
                                   output_verify='fix')
            msgs.info('Image {} produced.'.format(image_out))

        elif instrument.startswith('IRDIS'):
            msgs.work('Fixing header for SPHERE/{} file {}'.format(
                instrument, fits_in))
            hdr = fitsfiles.header_from_fits_file(fits_in)
            if 'ESO DPR TECH' in hdr.keys():
                if str(hdr['ESO DPR TECH']).strip(
                ) == 'IMAGE,DUAL,CORONOGRAPHY':
                    msgs.work('Working with {} as observing technique'.format(
                        str(hdr['ESO DPR TECH']).strip()))
                elif 'DUAL' in str(hdr['ESO DPR TECH']).strip():
                    msgs.error('{} needs to be tested'.format(
                        str(hdr['ESO DPR TECH']).strip()))
                else:
                    msgs.error('Only DUAL imaging currently implemented')
            else:
                msgs.error('Cannot recognize the observing technique')

            # defining the two fits_out files:
            fits_out_index = [0, 1]
            fits_out_files = []
            for index in fits_out_index:
                fits_out_file = fits_out.replace('.fit',
                                                 '_' + str(index) + '.fit')
                if os.path.exists(fits_out_file):
                    shutil.copy(fits_out_file,
                                fits_out_file.replace('.fit', '_old.fit'))
                    msgs.warning(
                        '{} already exists. Backup created.'.format(fits_out))
                fitsfiles.new_fits_like(fits_in, [0],
                                        fits_out_file,
                                        overwrite=overwrite,
                                        fix_header=True,
                                        empty_primary_hdu=False)
                fits_out_files.append(fits_out_file)

            for index, fits_out_file in zip(fits_out_index, fits_out_files):
                hdul = fitsfiles.get_hdul(fits_out_file,
                                          'update',
                                          checksum=True)
                hdr0 = hdul[0].header
                hdul[0].data = hdul[0].data[index, :, :]
                # Check for HISTORY
                # Primary Header
                if 'HISTORY' in hdr0.keys():
                    history_cards_hdr0 = [
                        history_card_hdr0 for history_card_hdr0 in hdr0
                        if history_card_hdr0.startswith('HISTORY')
                    ]
                    history_values_hdr0 = [
                        hdr0[history_card_hdr0] for history_card_hdr0 in hdr0
                        if history_card_hdr0.startswith('HISTORY')
                    ]
                    for history_card_hdr0, history_value_hdr0 in zip(
                            history_cards_hdr0, history_values_hdr0):
                        msgs.work('Cleaning cards: {} = {}'.format(
                            history_card_hdr0, history_value_hdr0))
                    del hdr0['HISTORY'][:]
                # Try to guess coordinates
                if 'CRVAL1' not in hdr0.keys():
                    msgs.warning('CRVAL position keywords not preset')
                    if 'OBJECT' in hdr0.keys():
                        try:
                            object_coordinate = SkyCoord.from_name(
                                str(hdr0['OBJECT']).strip())
                            ra_obj, dec_obj = object_coordinate.ra.degree, object_coordinate.dec.degree
                            if 'RA' in hdr0.keys() and 'DEC' in hdr0.keys():
                                pointing_coordinate = SkyCoord(
                                    float(hdr0['RA']),
                                    float(hdr0['DEC']),
                                    unit='deg')
                                msgs.work(
                                    'Testing from separation from pointing position'
                                )
                                separation = object_coordinate.separation(
                                    pointing_coordinate).arcsec
                                if separation < 120.:
                                    msgs.info(
                                        'Object - Pointing separation is {}'.
                                        format(separation))
                                    msgs.info(
                                        'Updating CRVAL1 = {}'.format(ra_obj))
                                    msgs.info(
                                        'Updating CRVAL2 = {}'.format(dec_obj))
                                    hdr0['CRVAL1'] = ra_obj
                                    hdr0['CRVAL2'] = dec_obj
                                    msgs.work('Updating CUNIT')
                                    hdr0['CUNIT1'] = 'deg'
                                    hdr0['CUNIT2'] = 'deg'
                                    msgs.work('Updating CTYPE')
                                    hdr0['CTYPE1'] = 'RA---TAN'
                                    hdr0['CTYPE2'] = 'DEC--TAN'
                                    msgs.work('Updating CRPIX')
                                    hdr0['CRPIX1'] = float(
                                        hdul[0].data.shape[1]) / 2.
                                    hdr0['CRPIX2'] = float(
                                        hdul[0].data.shape[0]) / 2.
                                    msgs.info('Updating CD1 and CD2')
                                    hdr0['CD1_1'] = hdr0[
                                        'PIXSCAL'] * 2.778E-4 / 1000.
                                    hdr0['CD2_2'] = hdr0[
                                        'PIXSCAL'] * 2.778E-4 / 1000.
                                    hdr0['CD1_2'] = 0.
                                    hdr0['CD2_1'] = 0.
                                    msgs.work('Updating RA, DEC')
                                    hdr0['RA'] = ra_obj
                                    hdr0.comments[
                                        'RA'] = object_coordinate.ra.to_string(
                                            u.hour)
                                    hdr0['DEC'] = dec_obj
                                    hdr0.comments[
                                        'DEC'] = object_coordinate.dec.to_string(
                                            u.degree, alwayssign=True)
                                else:
                                    msgs.warning(
                                        'Object - Pointing separation is {}'.
                                        format(separation))
                                    msgs.warning(
                                        'This is suspicious, CRVAL not updated'
                                    )
                        except name_resolve.NameResolveError:
                            msgs.warning('Object {} not recognized'.format(
                                str(hdr0['OBJECT']).strip()))
                            msgs.warning('CRVAL not updated')

                # Updating file prodcatg
                msgs.work('Updating PRODCATG to SCIENCE.IMAGE')
                hdr0['PRODCATG'] = str('SCIENCE.IMAGE')

                if 'PROGID' not in hdr0.keys():
                    msgs.warning('PROG_ID missing')
                    if 'ESO OBS PROG ID' in hdr0.keys():
                        msgs.info(
                            'Deriving PROG_ID from HIERARCH ESO OBS PROG ID')
                        msgs.work('Updating PROG_ID to {}'.format(
                            str(hdr0['HIERARCH ESO OBS PROG ID'])))
                        hdr0['PROG_ID'] = str(hdr0['HIERARCH ESO OBS PROG ID'])

                # Update checksum and datasum
                msgs.work('Updating checksum and datasum')
                hdul[0].add_checksum(override_datasum=False)
                hdul.flush(output_verify='fix')
                hdul.close()
        else:
            msgs.warning(
                'The Instrument {} is not supported \nThe file {} will not be processed'
                .format(instrument, fits_in))

    msgs.end()
    '''
Пример #4
0
def main(args):
    from ESOAsg.ancillary import astro
    from ESOAsg.ancillary import cleaning_lists
    from ESOAsg.ancillary import polygons
    from ESOAsg import archive_science_portal
    from ESOAsg import archive_observations
    from ESOAsg import msgs

    # Cleaning input lists
    input_fits_files = cleaning_lists.make_list_of_fits_files(args.input_fits)
    instruments = cleaning_lists.from_element_to_list(args.instruments,
                                                      element_type=str)
    data_types = cleaning_lists.from_element_to_list(args.data_types,
                                                     element_type=str)

    # Cleaning input values
    confidence_level = cleaning_lists.from_element_to_list(
        args.confidence_level, element_type=float)[0]
    maxrec = cleaning_lists.from_element_to_list(args.maxrec,
                                                 element_type=int)[0]
    max_vertices = cleaning_lists.from_element_to_list(args.max_vertices,
                                                       element_type=int)[0]

    # Cleaning bool
    show_figure = args.show_sky
    # Show link
    show_asp = args.asp_link
    # Download data
    download_data = args.download_data

    msgs.start()

    for fits_file in input_fits_files:
        msgs.info(' ')
        msgs.info('Working on file: {}'.format(fits_file))
        msgs.info(' ')
        contours = astro.contours_from_gw_bayestar(
            fits_file, credible_level=confidence_level)
        astro.show_contours_from_gw_bayestar(
            fits_file,
            contours=contours,
            cmap='afmhot',
            contours_color='white',
            show_figure=show_figure,
            matplotlib_backend=STARTING_MATPLOTLIB_BACKEND)
        polygons_list = polygons.contours_to_polygons(
            contours, max_vertices=max_vertices)

        if show_asp:
            msgs.info('Opening links to ASP')
            archive_science_portal.query_from_polygons(polygons=polygons_list,
                                                       open_link=True)

        results_from_query = archive_observations.query_from_polygons(
            polygons=polygons_list,
            maxrec=maxrec,
            instruments=instruments,
            data_types=data_types,
            verbose=False)
        msgs.info(' ')
        if download_data:
            if results_from_query is None:
                msgs.warning('No data retrieved')
            elif isinstance(results_from_query, list):
                for idx_poly, result_from_query in enumerate(
                        results_from_query):
                    msgs.info(
                        'Downloading data for polygon N.{}'.format(idx_poly +
                                                                   1))
                    archive_observations.download(result_from_query['dp_id'])
            else:
                msgs.info('Downloading data')
                archive_observations.download(results_from_query['dp_id'])
        msgs.info(' ')

    msgs.end()