Beispiel #1
0
def main(epoch, origin, destination):
    print("\nExecuting Python script pipeline_fors2/4.1-insert_test_synth.py, with:")
    print(f"\tepoch {epoch}")
    print(f"\torigin directory {origin}")
    print(f"\tdestination directory {destination}")
    print()

    epoch_params = p.object_params_fors2(obj=epoch)
    outputs = p.object_output_params(obj=epoch, instrument='FORS2')

    data_dir = epoch_params['data_dir']

    insert = epoch_params['test_synths']

    origin_path = data_dir + "analysis/sextractor/" + origin
    destination_path = data_dir + destination

    u.mkdir_check(destination_path)
    u.mkdir_check(destination_path + "science/")
    u.mkdir_check(destination_path + "backgrounds/")

    filters = outputs['filters']

    for fil in filters:
        f = fil[0]
        path_fil_output = destination_path + "science/" + fil + "/"
        path_fil_input = origin_path + fil + "/"
        u.mkdir_check(path_fil_output)
        u.mkdir_check(destination_path + "backgrounds/" + fil)
        zeropoint, _, airmass, _, extinction, _ = ph.select_zeropoint(obj=epoch,
                                                                      filt=fil,
                                                                      instrument='fors2',
                                                                      outputs=outputs)

        print(path_fil_input)
        # print(os.listdir(path_fil_input))

        for fits_file in filter(lambda f: f.endswith("_norm.fits"), os.listdir(path_fil_input)):
            print(fits_file)
            path_fits_file_input = path_fil_input + fits_file
            path_fits_file_output = path_fil_output + fits_file
            path_psf_model = path_fits_file_input.replace(".fits", "_psfex.psf")

            try:
                ph.insert_point_sources_to_file(file=path_fits_file_input,
                                                x=array(insert["ra"]),
                                                y=array(insert["dec"]),
                                                mag=insert[f"{f}_mag"],
                                                output=path_fits_file_output,
                                                zeropoint=zeropoint,
                                                extinction=extinction,
                                                airmass=airmass,
                                                world_coordinates=True,
                                                psf_model=path_psf_model
                                                )
            except ValueError:
                ph.insert_point_sources_to_file(file=path_fits_file_input,
                                                x=array(insert["ra"]),
                                                y=array(insert["dec"]),
                                                mag=insert[f"{f}_mag"],
                                                output=path_fits_file_output,
                                                zeropoint=zeropoint,
                                                extinction=extinction,
                                                airmass=airmass,
                                                world_coordinates=True,
                                                fwhm=fits.open(path_psf_model)[1].header['PSF_FWHM']
                                                )

    if os.path.isfile(origin_path + epoch + '.log'):
        copyfile(origin_path + epoch + '.log', destination_path + epoch + ".log")
    u.write_log(path=destination_path + epoch + ".log", action=f'Divided by exposure time.')
Beispiel #2
0
def main(field, subtraction_path, epoch, instrument, instrument_template, show):
    comparison_name = f'{field}_{epoch}'

    params = p.object_params_frb(field)
    burst_ra = params['burst_ra']
    burst_dec = params['burst_dec']
    burst_err_a, burst_err_b, theta = am.calculate_error_ellipse(frb=params, error='systematic')
    hg_ra = params['hg_ra']
    hg_dec = params['hg_dec']

    burst_err_theta = 0.0  # params['burst_err_theta']
    # comparison_params = p.object_params_instrument(comparison_name, instrument)
    # comparison_outputs = p.object_output_params(comparison_name, instrument)

    subtraction_path = f'{params["data_dir"]}subtraction/{subtraction_path}'

    filters = params['filters']

    folders = list(filter(lambda file: os.path.isdir(subtraction_path + file), os.listdir(subtraction_path)))
    folders.sort()

    output_table = table.Table(names=['name'], dtype=[f'S{len(folders[-1])}'])

    first = True

    sextractor_names = p.sextractor_names()

    print(subtraction_path)

    for i, folder in enumerate(folders):
        print()
        print(folder)
        subtraction_path_spec = subtraction_path + folder + '/'
        output_table.add_row()
        output_table['name'][i] = folder

        for f in filters:
            print()
            print(f)

            f_0 = f[0]
            destination_path_filter = f'{subtraction_path_spec}{f}/'

            template_epoch = params['template_epoch_' + instrument_template.lower()]
            template_name = f'{field}_{template_epoch}'

            print(destination_path_filter)
            print('Finding files...')

            cats = list(filter(lambda file: file[-15:] == '_comparison.csv',
                               os.listdir(f'{destination_path_filter}')))
            comparisons = list(filter(lambda file: file[-24:] == '_comparison_aligned.fits',
                                      os.listdir(f'{destination_path_filter}')))
            differences = list(filter(lambda file: file[-16:] == '_difference.fits',
                                      os.listdir(f'{destination_path_filter}')))

            if cats:  # If the generated catalogue was successfully copied.
                print('Loading generated table...')
                cat_generated_file = cats[0]
                cat_generated_path = f'{destination_path_filter}{cat_generated_file}'
                cat_generated = table.Table.read(cat_generated_path, format='ascii.csv')

                if comparisons and differences:  # If the subtraction was successful.

                    comparison_image_file = comparisons[0]
                    difference_image_file = differences[0]

                    print('Loading SExtractor table...')
                    cat_sextractor_path = f'{destination_path_filter}difference.cat'
                    comparison_image_path = f'{destination_path_filter}{comparison_image_file}'
                    difference_image_path = f'{destination_path_filter}{difference_image_file}'

                    cat_sextractor = table.Table(np.genfromtxt(cat_sextractor_path, names=sextractor_names))
                    if len(cat_sextractor) > 0:
                        difference_image = fits.open(difference_image_path)
                        comparison_image = fits.open(comparison_image_path)

                        comparison_header = comparison_image[0].header

                        exp_time = comparison_header['EXPTIME']

                        print('Selecting zeropoint...')

                        comparison_zeropoint, _, airmass, _, comparison_extinction, _ = ph.select_zeropoint(
                            comparison_name,
                            f,
                            instrument=instrument)

                        print('Matching coordinates...')

                        match_ids_generated, match_ids_sextractor, match_distances = u.match_cat(
                            x_cat=cat_sextractor['ra'],
                            y_cat=cat_sextractor[
                                'dec'],
                            x_match=cat_generated[
                                'ra'],
                            y_match=cat_generated[
                                'dec'],
                            tolerance=2 / 3600,
                            world=True,
                            return_dist=True)

                        matches_sextractor = cat_sextractor[match_ids_sextractor]
                        matches_generated = cat_generated[match_ids_generated]

                        w = wcs.WCS(header=difference_image[0].header)
                        hg_x, hg_y = w.all_world2pix(hg_ra, hg_dec, 0)

                        # norm = plotting.nice_norm(difference_image[0].data)
                        # print('Generating full-image plot...')
                        # plt.figure(figsize=(30, 20))
                        # plt.imshow(difference_image[0].data, norm=norm, origin='lower')
                        # plotting.plot_all_params(image=difference_image, cat=cat_sextractor, show=False)
                        # # plt.legend()
                        # plt.savefig(f'{destination_path_filter}recovered_all.png')
                        # if show:
                        #     plt.show()
                        # plt.close()

                        plt.figure(figsize=(6, 8))

                        print('Generating cutout plot...')
                        cutout = ff.trim(difference_image, int(hg_x) - 20, int(hg_x) + 20, int(hg_y) - 20,
                                         int(hg_y) + 20)
                        plotting.plot_all_params(image=difference_image, cat=matches_sextractor, show=False)
                        plotting.plot_gal_params(hdu=difference_image, ras=[burst_ra], decs=[burst_dec],
                                                 a=[burst_err_a],
                                                 b=[burst_err_b], theta=[burst_err_theta], colour='blue',
                                                 show_centre=True,
                                                 label='Burst')

                        norm = plotting.nice_norm(cutout[0].data)
                        plt.imshow(cutout[0].data, norm=norm, origin='lower')
                        plt.scatter(hg_x - (int(hg_x) - 20), hg_y - (int(hg_y) - 20), c='orange',
                                    label='Galaxy centroid')
                        for obj in matches_generated:
                            plt.scatter(obj['x_0'] - (int(hg_x) - 20), obj['y_0'] - (int(hg_y) - 20), c='white',
                                        label='Generated')
                        plt.legend()
                        plt.savefig(f'{destination_path_filter}recovered.png')
                        if show:
                            plt.show()
                        plt.close()

                        matches_sextractor['mag_sextractor'], _, _ = ph.magnitude_complete(
                            flux=matches_sextractor['flux_auto'],
                            exp_time=exp_time, airmass=airmass,
                            zeropoint=comparison_zeropoint,
                            ext=comparison_extinction)

                        matches = table.hstack([matches_generated, matches_sextractor],
                                               table_names=['generated', 'sextracted'])

                        matches['delta_mag'] = matches['mag_sextractor'] - matches['mag']

                        delta_mag = np.median(matches['delta_mag'])

                        matches.write(
                            f'{destination_path_filter}{field}_{epoch}-{template_epoch}_difference_matched_sources.csv',
                            format='ascii.csv', overwrite=True)

                        print(f'{len(matches)} matches / {len(cat_generated)} generated = '
                              f'{100 * len(matches) / len(cat_generated)} % ')

                        # TODO: Will need to rewrite this if you want to insert more than one synthetic.

                        output_table = table_setup(f, first, output_table, cat_generated)

                        output_table[f + '_subtraction_successful'][i] = True
                        if len(matches) > 0:
                            print(f'Faintest recovered: {max(matches["mag"])} generated; '
                                  f'{max(matches_sextractor["mag_sextractor"])} sextracted')

                            print('Median delta mag:', delta_mag)

                            if show:
                                plt.scatter(matches['mag'], matches['delta_mag'])
                                plt.show()

                            arg_faintest = np.argmax(matches["mag"])
                            output_table[f + '_mag_generated'][i] = cat_generated["mag"][arg_faintest]
                            output_table[f + '_mag_recovered'][i] = matches['mag_sextractor'][arg_faintest]
                            output_table[f + '_difference'][i] = delta_mag
                            output_table[f + '_matching_distance_arcsec'][i] = match_distances[arg_faintest] * 3600
                            output_table[f + '_nuclear_offset_pix_x'][i] = matches['x'][arg_faintest] - hg_x
                            output_table[f + '_nuclear_offset_pix_y'][i] = matches['y'][arg_faintest] - hg_y
                            for col in cat_generated.colnames:
                                output_table[f + '_' + col][i] = cat_generated[col][arg_faintest]

                        else:  # If there were no matches
                            output_table[f + '_mag_generated'][i] = cat_generated["mag"][0]
                            output_table[f + '_mag_recovered'][i] = np.nan
                            output_table[f + '_difference'][i] = np.nan
                            output_table[f + '_matching_distance_arcsec'][i] = np.nan
                            output_table[f + '_nuclear_offset_pix_x'][i] = np.nan
                            output_table[f + '_nuclear_offset_pix_y'][i] = np.nan
                            for col in cat_generated.colnames:
                                output_table[f + '_' + col][i] = cat_generated[col][0]
                    else:  # If SExtractor was not successful, probably because of a blank difference image

                        output_table = table_setup(f, first, output_table, cat_generated)
                        output_table[f + '_mag_generated'][i] = cat_generated["mag"][0]
                        output_table[f + '_subtraction_successful'][i] = False
                        output_table[f + '_mag_recovered'][i] = np.nan
                        output_table[f + '_difference'][i] = np.nan
                        output_table[f + '_matching_distance_arcsec'][i] = np.nan
                        output_table[f + '_nuclear_offset_pix_x'][i] = np.nan
                        output_table[f + '_nuclear_offset_pix_y'][i] = np.nan
                        for col in cat_generated.colnames:
                            output_table[f + '_' + col][i] = cat_generated[col][0]

                else:  # If the subtraction was not successful.

                    output_table = table_setup(f, first, output_table, cat_generated)
                    output_table[f + '_mag_generated'][i] = cat_generated["mag"][0]
                    output_table[f + '_subtraction_successful'][i] = False
                    output_table[f + '_mag_recovered'][i] = np.nan
                    output_table[f + '_difference'][i] = np.nan
                    output_table[f + '_matching_distance_arcsec'][i] = np.nan
                    output_table[f + '_nuclear_offset_pix_x'][i] = np.nan
                    output_table[f + '_nuclear_offset_pix_y'][i] = np.nan
                    for col in cat_generated.colnames:
                        output_table[f + '_' + col][i] = cat_generated[col][0]

            else:  # If the generated catalogue was not successfully copied.
                output_table[f + '_mag_generated'][i] = np.nan
                output_table[f + '_subtraction_successful'][i] = True
                output_table[f + '_mag_recovered'][i] = np.nan
                output_table[f + '_mag_generated'][i] = np.nan
                output_table[f + '_difference'][i] = np.nan
                output_table[f + '_matching_distance_arcsec'][i] = np.nan
                output_table[f + '_nuclear_offset_pix_x'][i] = np.nan
                output_table[f + '_nuclear_offset_pix_y'][i] = np.nan

        first = False
        

    plt.close()
    for f in filters:
        plt.scatter(output_table[f + '_mag_generated'], output_table[f + '_mag_recovered'],
                    label=f)
        plt.plot([min(output_table[f + '_mag_generated']), max(output_table[f + '_mag_generated'])],
                 [min(output_table[f + '_mag_generated']), max(output_table[f + '_mag_generated'])], c='red',
                 linestyle=':')
        plt.ylim(20, 30)
        plt.savefig(subtraction_path + 'genvrecovered_' + f + '.png')
        plt.close()

        plt.scatter(output_table[f + '_mag_generated'],
                    output_table[f + '_mag_recovered'] - output_table[f + '_mag_generated'],
                    label=f)
        plt.plot([min(output_table[f + '_mag_generated']), max(output_table[f + '_mag_generated'])],
                 [0, 0], c='red',
                 linestyle=':')
        plt.xlim(20, 30)
        plt.savefig(subtraction_path + 'residuals_' + f + '.png')
        plt.close()

    for f in filters:
        plt.scatter(output_table[f + '_mag_generated'],
                    output_table[f + '_mag_recovered'] - output_table[f + '_mag_generated'],
                    label=f)

        plt.plot([min(output_table[f + '_mag_generated']), max(output_table[f + '_mag_generated'])],
                 [0, 0], c='red',
                 linestyle=':')
    plt.xlim(20, 30)
    plt.ylabel('Recovered magnitudes')
    plt.legend()
    plt.savefig(subtraction_path + 'residuals.png')
    if show:
        plt.show()
    plt.close()

    for f in filters:
        plt.scatter(output_table[f + '_mag_generated'], output_table[f + '_mag_recovered'],
                    label=f)
        plt.plot([min(output_table[f + '_mag_generated']), max(output_table[f + '_mag_generated'])],
                 [min(output_table[f + '_mag_generated']), max(output_table[f + '_mag_generated'])], c='red',
                 linestyle=':')
    plt.xlim(20, 30)
    plt.ylim(20, 30)
    plt.xlabel('Generated magnitudes')
    plt.ylabel('Recovered magnitudes')
    plt.legend()
    plt.savefig(subtraction_path + 'genvrecovered.png')
    if show:
        plt.show()
    plt.close()

    output_table.write(subtraction_path + 'recovery_table.csv', format='ascii.csv', overwrite=True)
Beispiel #3
0
def main(obj, test, n, mag_lower, mag_upper, colour_upper, colour_lower):
    properties = p.object_params_fors2(obj)
    output = p.object_output_params(obj=obj, instrument='FORS2')
    paths = p.object_output_paths(obj)
    burst_properties = p.object_params_frb(obj[:-2])

    synth_path = properties['data_dir'] + 'synthetic/'

    u.mkdir_check(synth_path)
    synth_path = synth_path + 'random/'
    u.mkdir_check(synth_path)
    now = time.Time.now()
    now.format = 'isot'
    test = str(now) + '_' + test
    test_path = synth_path + test + '/'
    u.mkdir_check(test_path)

    filters = {}
    bluest = None
    bluest_lambda = np.inf
    for f in output['filters']:
        filter_properties = p.filter_params(f=f, instrument='FORS2')
        filters[f] = filter_properties
        lambda_eff = filter_properties['lambda_eff']
        if lambda_eff < bluest_lambda:
            bluest_lambda = lambda_eff
            bluest = f

    # Insert random sources in the bluest filter.

    f_0 = bluest[0]
    output_properties = p.object_output_params(obj)
    fwhm = output_properties[f_0 + '_fwhm_pix']
    zeropoint, _, airmass, _ = ph.select_zeropoint(obj,
                                                   bluest,
                                                   instrument='fors2')

    base_path = paths[f_0 + '_subtraction_image']

    output_path = test_path + f_0 + '_random_sources.fits'
    _, sources = ph.insert_random_point_sources_to_file(file=base_path,
                                                        fwhm=fwhm,
                                                        output=output_path,
                                                        n=n,
                                                        airmass=airmass,
                                                        zeropoint=zeropoint)

    p.add_output_path(obj=obj,
                      key=f_0 + '_subtraction_image_synth_random',
                      path=output_path)

    # Now insert sources at the same positions in other filters, but with magnitudes randomised.
    for f in filters:
        if f != bluest:
            f_0 = f[0]
            output_properties = p.object_output_params(obj)
            fwhm = output_properties[f_0 + '_fwhm_pix']
            zeropoint, _, airmass, _ = ph.select_zeropoint(obj,
                                                           f,
                                                           instrument='fors2')

            base_path = paths[f_0 + '_subtraction_image']

            mag = np.random.uniform(mag_lower, mag_upper, size=n)

            output_path = test_path + f_0 + '_random_sources.fits'
            ph.insert_point_sources_to_file(file=base_path,
                                            fwhm=fwhm,
                                            output=output_path,
                                            x=sources['x_0'],
                                            y=sources['y_0'],
                                            mag=mag,
                                            airmass=airmass,
                                            zeropoint=zeropoint)

            p.add_output_path(obj=obj,
                              key=f_0 + '_subtraction_image_synth_random',
                              path=output_path)
def main(field, subtraction_path, epoch, instrument):
    comparison_name = f'{field}_{epoch}'

    params = p.object_params_frb(field)
    # comparison_params = p.object_params_instrument(comparison_name, instrument)

    subtraction_path = f'{params["data_dir"]}subtraction/{subtraction_path}'

    filters = params['filters']

    burst_ra = params['burst_ra']
    burst_dec = params['burst_dec']
    hg_ra = params['hg_ra']
    hg_dec = params['hg_dec']
    burst_err_a = params['burst_err_stat_a'] / 3600
    burst_err_b = params['burst_err_stat_b'] / 3600
    burst_err_theta = params['burst_err_theta']

    for f in filters:
        print()
        print(f)
        f_0 = f[0]
        destination_path_filter = f'{subtraction_path}{f}/'

        # This relies on there only being one file with these suffixes in each directory, which, if previous scripts
        # ran correctly, should be true.

        comparison_image_file = filter(
            lambda file: file[-24:] == '_comparison_aligned.fits',
            os.listdir(f'{destination_path_filter}')).__next__()
        difference_image_file = filter(
            lambda file: file[-16:] == '_difference.fits',
            os.listdir(f'{destination_path_filter}')).__next__()

        cat_sextractor_path = f'{destination_path_filter}difference.cat'
        comparison_image_path = f'{destination_path_filter}{comparison_image_file}'
        difference_image_path = f'{destination_path_filter}{difference_image_file}'

        cat_sextractor = table.Table(
            np.genfromtxt(cat_sextractor_path, names=p.sextractor_names()))

        difference_image = fits.open(difference_image_path)
        comparison_image = fits.open(comparison_image_path)

        comparison_header = comparison_image[0].header

        exp_time = comparison_header['EXPTIME']

        comparison_zeropoint, _, airmass, _, comparison_extinction, _ = ph.select_zeropoint(
            comparison_name, f, instrument=instrument)

        cat_sextractor['mag'], _, _ = ph.magnitude_complete(
            flux=cat_sextractor['flux_aper'],
            exp_time=exp_time,
            airmass=airmass,
            zeropoint=comparison_zeropoint,
            ext=comparison_extinction)

        cat_sextractor['mag_auto'], _, _ = ph.magnitude_complete(
            flux=cat_sextractor['flux_auto'],
            exp_time=exp_time,
            airmass=airmass,
            zeropoint=comparison_zeropoint,
            ext=comparison_extinction)

        wcs_info = wcs.WCS(comparison_header)
        hg_x, hg_y = wcs_info.all_world2pix(hg_ra, hg_dec, 0)

        # norm = plotting.nice_norm(difference_image[0].data)
        # print('Generating full-image plot...')
        # plt.figure(figsize=(30, 20))
        # plt.imshow(difference_image[0].data, norm=norm, origin='lower')
        # plotting.plot_all_params(image=difference_image, cat=cat_sextractor, show=False)
        # # plt.legend()
        # plt.savefig(f'{destination_path_filter}recovered_all.pdf')
        # plt.close()

        closest_index, distance = u.find_object(burst_ra, burst_dec,
                                                cat_sextractor['ra'],
                                                cat_sextractor['dec'])

        closest = cat_sextractor[closest_index]
        x = closest['x']
        y = closest['y']

        print(closest)
        print('Magnitude:', closest['mag'])
        print('Threshold:', closest['threshold'])
        print('Flux max:', closest['flux_max'])
        print('RA:', closest['ra'], '; DEC:', closest['dec'])
        print('X:', x, '; Y:', y)
        print('Offset from HG (pixels):',
              np.sqrt((x - hg_x)**2 + (y - hg_y)**2))

        x = int(x)
        y = int(y)

        norm = plotting.nice_norm(difference_image[0].data)
        print('Generating full-image plot...')
        plt.figure(figsize=(30, 20))
        plt.imshow(difference_image[0].data, norm=norm, origin='lower')
        print(min(cat_sextractor['ra']), min(cat_sextractor['dec']))
        plotting.plot_all_params(image=difference_image,
                                 cat=cat_sextractor,
                                 show=False)
        # plt.legend()
        plt.savefig(f'{destination_path_filter}recovered_all.png')
        plt.close()

        print('Generating cutout plots...')

        plt.imshow(difference_image[0].data, norm=norm, origin='lower')
        # plt.legend()
        plotting.plot_gal_params(hdu=difference_image,
                                 ras=[burst_ra],
                                 decs=[burst_dec],
                                 a=[burst_err_a],
                                 b=[burst_err_b],
                                 theta=[burst_err_theta],
                                 colour='blue',
                                 show_centre=True,
                                 label='Burst')
        plt.scatter(hg_x, hg_y, c='orange', label='Galaxy centroid')
        plotting.plot_all_params(image=difference_image,
                                 cat=cat_sextractor,
                                 show=False)
        plt.xlim(hg_x - 50, hg_x + 50)
        plt.ylim(hg_y - 50, hg_y + 50)
        plt.legend()
        plt.savefig(f'{destination_path_filter}recovered_hg_cutout.png')
        plt.close()

        cutout_source = difference_image[0].data[y - 50:y + 50, x - 50:x + 50]

        plt.imshow(cutout_source, origin='lower')
        plt.savefig(f'{destination_path_filter}nearest_source_cutout.png')
        plt.show()
        plt.close()

        cutout_hg = ff.trim(difference_image,
                            int(hg_x) - 20,
                            int(hg_x) + 20,
                            int(hg_y) - 20,
                            int(hg_y) + 20)
        cutout_hg_data = cutout_hg[0].data

        midpoint = int(cutout_hg_data.shape[0] / 2)
        xx = np.arange(-midpoint, midpoint)
        data_slice = cutout_hg_data[midpoint, :]
        plt.plot(xx, data_slice)
        plt.savefig(f'{destination_path_filter}hg_x_slice.png')
        plt.show()
        plt.close()
        print()
        x_misalignment = np.argmax(data_slice) - np.argmin(data_slice)
        print('x peak - trough:', x_misalignment)

        midpoint = int(cutout_hg_data.shape[0] / 2)
        yy = np.arange(-midpoint, midpoint)
        data_slice = cutout_hg_data[:, midpoint]
        plt.plot(yy, data_slice)
        plt.savefig(f'{destination_path_filter}hg_y_slice.png')
        plt.show()
        plt.close()
        y_misalignment = np.argmax(data_slice) - np.argmin(data_slice)
        print('y peak - trough:', y_misalignment)
        print()

        print('Mean false positive mag:', np.nanmean(cat_sextractor['mag']))
        print('Median false positive mag:',
              np.nanmedian(cat_sextractor['mag']))

        plt.figure(figsize=(6, 8))

        sextractor_cutout = cat_sextractor[
            (cat_sextractor['x'] > int(hg_x) - 20)
            & (cat_sextractor['x'] < int(hg_x) + 20) &
            (cat_sextractor['y'] > int(hg_y) - 20) &
            (cat_sextractor['y'] < int(hg_y) + 20)]
        plotting.plot_all_params(image=difference_image,
                                 cat=sextractor_cutout,
                                 show=False)
        plotting.plot_gal_params(hdu=difference_image,
                                 ras=[burst_ra],
                                 decs=[burst_dec],
                                 a=[burst_err_a],
                                 b=[burst_err_b],
                                 theta=[burst_err_theta],
                                 colour='blue',
                                 show_centre=True,
                                 label='Burst')

        norm = plotting.nice_norm(cutout_hg[0].data)
        plt.imshow(cutout_hg[0].data, norm=norm, origin='lower')
        plt.scatter(hg_x - (int(hg_x) - 20),
                    hg_y - (int(hg_y) - 20),
                    c='orange',
                    label='Galaxy centroid')
        plt.legend()
        plt.savefig(f'{destination_path_filter}residuals_hg.png')
        plt.show()
        plt.close()
Beispiel #5
0
def main(field, subtraction_path, epoch, instrument):
    comparison_name = f'{field}_{epoch}'

    params = p.object_params_frb(field)
    ra_burst = params['burst_ra']
    dec_burst = params['burst_dec']
    burst_err_a = params['burst_err_a']
    burst_err_b = params['burst_err_b']
    burst_err_theta = params['burst_err_theta']
    comparison_params = p.object_params_instrument(comparison_name, instrument)

    subtraction_path = f'{params["data_dir"]}subtraction/{subtraction_path}'

    filters = params['filters']
    for f in filters:
        f_0 = f[0]
        destination_path_filter = f'{subtraction_path}{f}/'

        template_epoch = params['template_epoch']

        comparison_extinction = comparison_params[f_0 + '_ext_up']

        cat_generated_file = filter(lambda file: file[-15:] == '_comparison.csv',
                                    os.listdir(f'{destination_path_filter}')).__next__()
        comparison_image_file = filter(lambda file: file[-24:] == '_comparison_aligned.fits',
                                       os.listdir(f'{destination_path_filter}')).__next__()
        difference_image_file = filter(lambda file: file[-16:] == '_difference.fits',
                                       os.listdir(f'{destination_path_filter}')).__next__()

        cat_generated_path = f'{destination_path_filter}{cat_generated_file}'
        cat_sextractor_path = f'{destination_path_filter}difference.cat'
        comparison_image_path = f'{destination_path_filter}{comparison_image_file}'
        difference_image_path = f'{destination_path_filter}{difference_image_file}'

        cat_generated = table.Table.read(cat_generated_path, format='ascii.csv')
        cat_sextractor = table.Table(np.genfromtxt(cat_sextractor_path, names=p.sextractor_names()))
        difference_image = fits.open(difference_image_path)
        comparison_image = fits.open(comparison_image_path)

        comparison_header = comparison_image[0].header

        exp_time = comparison_header['EXPTIME']

        comparison_zeropoint, _, airmass, _ = ph.select_zeropoint(comparison_name, f, instrument=instrument)

        plotting.plot_all_params(image=difference_image, cat=cat_sextractor, show=False)
        for obj in cat_generated:
            plt.scatter(obj['x_0'], obj['y_0'], c='white')
        plt.show()

        _, pix_scale = ff.get_pixel_scale(comparison_image)

        match_ids_sextractor, match_ids_generated = u.match_cat(x_match=cat_sextractor['ra'],
                                                                y_match=cat_sextractor['dec'],
                                                                x_cat=cat_generated['ra'],
                                                                y_cat=cat_generated['dec'],
                                                                tolerance=3 * pix_scale)

        matches_sextractor = cat_sextractor[match_ids_sextractor]
        matches_generated = cat_generated[match_ids_generated]

        plotting.plot_all_params(image=difference_image, cat=matches_sextractor, show=False)
        # plt.scatter(x_burst, y_burst, c='white')
        plotting.plot_gal_params(hdu=difference_image, ras=[ra_burst], decs=[dec_burst], a=[burst_err_a],
                                 b=[burst_err_b], theta=[burst_err_theta], colour='blue', show_centre=True)
        for obj in matches_generated:
            plt.scatter(obj['x_0'], obj['y_0'], c='white')
        plt.show()

        matches_sextractor['mag_sextractor'], _, _ = ph.magnitude_complete(flux=matches_sextractor['flux_aper'],
                                                                           exp_time=exp_time, airmass=airmass,
                                                                           zeropoint=comparison_zeropoint,
                                                                           ext=comparison_extinction)

        matches = table.hstack([matches_generated, matches_sextractor], table_names=['generated', 'sextracted'])

        matches['delta_mag'] = matches['mag_sextractor'] - matches['mag']

        delta_mag = np.median(matches['delta_mag'])

        matches.write(f'{destination_path_filter}{field}_{epoch}-{template_epoch}_difference_matched_sources.csv',
                      format='ascii.csv', overwrite=True)

        print(f'{len(matches)} matches / {len(cat_generated)} generated = '
              f'{100 * len(matches) / len(cat_generated)} % ')

        print(f'Faintest recovered: {max(matches["mag"])} generated; '
              f'{max(matches_sextractor["mag_sextractor"])} sextracted')

        print('Median delta mag:', delta_mag)

        plt.scatter(matches['mag'], matches['delta_mag'])
        plt.show()

        difference_image.close()