Example #1
0
def convert_frame_to_counts(path, counts_path):
    """
    This function ...
    :param path:
    :param counts_path:
    :return:
    """

    # Open the HDUList
    hdulist = open_fits(path)

    # Get calibrated image in nanomaggies/pixel
    img = hdulist[0].data
    nrowc = img.shape[0]  # ysize    (x = columns, y = rows)

    # Get the sky HDU (header)
    sky = hdulist[2].data
    allsky = sky.field("allsky")[
        0]  # for some reason, "allsky" has 3 axes (1, 192, 256), therefore [0]
    xinterp = sky.field(
        "xinterp"
    )[0]  # columns (xsize = 2048)    # for some reason, "xinterp" has 2 axes (1, 2048)
    yinterp = sky.field(
        "yinterp"
    )[0]  # rows    (ysize = 1489)    # for some reason, "yinterp" has 2 axes (1, 1489)

    # Split x, y and z values that are not masked
    # x_values, y_values, z_values = split_xyz(allsky, arrays=True)

    allsky_xrange = np.arange(allsky.shape[1], dtype=float)
    allsky_yrange = np.arange(allsky.shape[0], dtype=float)

    # INTERPOLATE SKY
    skyf = interp2d(allsky_xrange, allsky_yrange, allsky)
    simg = skyf(xinterp, yinterp)

    # Get the calibration HDU (header)
    calib = hdulist[1].data
    replicate = np.ones(nrowc)
    cimg = np.outer(replicate, calib)

    # DECALIBRATE
    dn = img / cimg + simg

    # CREATE FRAME AND SAVE

    dn_frame = Frame(dn)
    dn_frame.unit = "count"

    # Set the WCS of THE DN FRAME
    dn_frame.wcs = CoordinateSystem(hdulist[0].header)

    # Save
    name = fs.name(path)
    dn_path = fs.join(counts_path, name)
    dn_frame.saveto(dn_path)
Example #2
0
def get_gain_and_dark_variance_from_photofield(path, band):
    """
    This function ...
    :param path:
    :param band:
    :return:
    """

    # Get the hdulist
    hdulist = open_fits(path)

    # HDU0: Empty Header
    # HDU1: photoField Table

    # Data Model: photoField: https://data.sdss.org/datamodel/files/BOSS_PHOTOOBJ/RERUN/RUN/photoField.html

    column_names = hdulist[1].columns

    tbdata = hdulist[1].data

    gain_2d = tbdata.field("gain")
    dark_variance_2d = tbdata.field("dark_variance")

    gain_1d = gain_2d[:, sdss_filter_number[band]]
    dark_variance_1d = dark_variance_2d[:, sdss_filter_number[band]]

    if not is_constant_array(gain_1d):
        raise ValueError("Gain 1D not constant: " + str(gain_1d))
    if not is_constant_array(dark_variance_1d):
        raise ValueError("Dark variance 1D not constant: " +
                         str(dark_variance_1d))

    gain = gain_1d[0]
    dark_variance = dark_variance_1d[0]

    # Return the values
    return gain, dark_variance
Example #3
0
def _make_pretty_from_fits(fname=None,
                           title=None,
                           figsize=(10, 10 / 1.325),
                           dpi=150,
                           alpha=0.2,
                           number_ticks=7,
                           clip_percent=99.9,
                           **kwargs):

    with open_fits(fname) as hdu:
        header = hdu[0].header
        data = hdu[0].data
        data = focus_utils.mask_saturated(data)
        wcs = WCS(header)

    if not title:
        field = header.get('FIELD', 'Unknown field')
        exptime = header.get('EXPTIME', 'Unknown exptime')
        filter_type = header.get('FILTER', 'Unknown filter')

        try:
            date_time = header['DATE-OBS']
        except KeyError:
            # If we don't have DATE-OBS, check filename for date
            try:
                basename = os.path.splitext(os.path.basename(fname))[0]
                date_time = date_parser.parse(basename).isoformat()
            except Exception:
                # Otherwise use now
                date_time = current_time(pretty=True)

        date_time = date_time.replace('T', ' ', 1)

        title = '{} ({}s {}) {}'.format(field, exptime, filter_type, date_time)

    norm = ImageNormalize(interval=PercentileInterval(clip_percent), stretch=LogStretch())

    fig = Figure()
    FigureCanvas(fig)
    fig.set_size_inches(*figsize)
    fig.dpi = dpi

    if wcs.is_celestial:
        ax = fig.add_subplot(1, 1, 1, projection=wcs)
        ax.coords.grid(True, color='white', ls='-', alpha=alpha)

        ra_axis = ax.coords['ra']
        ra_axis.set_axislabel('Right Ascension')
        ra_axis.set_major_formatter('hh:mm')
        ra_axis.set_ticks(
            number=number_ticks,
            color='white',
            exclude_overlapping=True
        )

        dec_axis = ax.coords['dec']
        dec_axis.set_axislabel('Declination')
        dec_axis.set_major_formatter('dd:mm')
        dec_axis.set_ticks(
            number=number_ticks,
            color='white',
            exclude_overlapping=True
        )
    else:
        ax = fig.add_subplot(111)
        ax.grid(True, color='white', ls='-', alpha=alpha)

        ax.set_xlabel('X / pixels')
        ax.set_ylabel('Y / pixels')

    im = ax.imshow(data, norm=norm, cmap=palette, origin='lower')
    fig.colorbar(im)
    fig.suptitle(title)

    new_filename = fname.replace('.fits', '.jpg')
    fig.savefig(new_filename, bbox_inches='tight')

    # explicitly close and delete figure
    fig.clf()
    del fig

    return new_filename
Example #4
0
 def test_fitsblob_pickle(self):
     fits = open_fits(AARTFAAC_FITS)
     accessor = FitsImageBlob(fits)
     pickled = cPickle.dumps(accessor)
     unpickled = cPickle.loads(pickled)
     self.assertEqual(type(unpickled), type(accessor))
Example #5
0
def convert_frame_to_nanomaggies(path, name, fields_path, photofields_path,
                                 poisson_path):
    """
    This function ...
    :param path:
    :param name:
    :param fields_path:
    :param photofields_path:
    :param poisson_path:
    :return:
    """

    # Inform the user
    log.info("Converting the ")

    # EXAMPLE FILE NAME: frame-u-004294-4-0231.fits

    # FIELD URL = field_url_start / RERUN / RUN    + / photoField-6digits-CAMCOL.fits
    # example: http://data.sdss3.org/sas/dr12/env/BOSS_PHOTOOBJ/301/4294/photoField-004294-5.fits

    # FRAME URL = $BOSS_PHOTOOBJ / frames / RERUN / RUN / CAMCOL    +   /frame-[ugriz]-6digits-CAMCOL-FRAMESEQ.fits.bz2
    # example: http://data.sdss3.org/sas/dr10/boss/photoObj/frames/301/4294/5/frame-i-004294-5-0229.fits.bz2

    splitted = name.split("-")

    band = splitted[1]
    digits = splitted[2]
    camcol = splitted[3]
    frameseq = splitted[4]

    # Determine the path to the corresponding field file
    field_file_path = fs.join(photofields_path,
                              "photoField-" + digits + "-" + camcol + ".fits")

    # Get the gain and dark variance
    gain, dark_variance = get_gain_and_dark_variance_from_photofield(
        field_file_path, band)

    # Calculate the error

    # dn_err = sqrt(dn/gain+darkVariance)   # NOISE IN DN

    # img_err = dn_err*cimg                 # NOISE IN NANOMAGGIES / PIXEL

    # Load the image in DN
    dn_frame = Frame.from_file(path)

    # COMPUTE THE DN ERROR
    dn_error = np.sqrt(dn_frame.data / gain + dark_variance)

    # CONVERT ERROR MAP IN DN TO ERROR MAP IN NANOMAGGIES PER PIXEL

    # COMPUTE THE CALIBRATION FRAME AGAIN
    raw_frame_path = fs.join(fields_path, name + ".fits")
    hdulist = open_fits(raw_frame_path)
    img = hdulist[0].data
    nrowc = img.shape[0]  # ysize    (x = columns, y = rows)
    # Get the calibration HDU (header)
    calib = hdulist[1].data
    replicate = np.ones(nrowc)
    cimg = np.outer(replicate, calib)

    # IMAGE ERROR MAP IN NANOMAGGIES PER PIXEL
    img_error = dn_error * cimg
    error_map = Frame(img_error)

    # SET THE WCS OF THE IMAGE ERROR MAP
    error_map.wcs = dn_frame.wcs

    # SAVE the error map
    poisson_error_map_path = fs.join(poisson_path, name + ".fits")
    error_map.saveto(poisson_error_map_path)
Example #6
0
 def test_fitsblob_pickle(self):
     fits = open_fits(AARTFAAC_FITS)
     accessor = FitsImageBlob(fits)
     pickled = cPickle.dumps(accessor)
     unpickled = cPickle.loads(pickled)
     self.assertEqual(type(unpickled), type(accessor))