Ejemplo n.º 1
0
def cloudyness(diff_image,
               fwhm=5.,
               sigma_cut=3.,
               sigma_second=3.,
               airmass_map=None,
               airmass_limit=None,
               skyRMS_max=None,
               grow_iter=3,
               grow_fwhm=5.,
               grow_lower_limit=0.1):
    """
    Parameters
    ----------
    diff_image: np.array
        An array that is the difference of two all-sky images

    sigma_cut: float
        blah

    Returns
    -------

    out_area:  float
        area (sq degrees) that are flagged as cloudy
    cloud_mask:  np.array
        Array same size as diff_image with values of -1, 0, 1 to show which 
        pixels have been flagged as negative or positive.
    """

    # XXX.  Maybe the solution is to set a minimum angular scale, but then filter on the
    # scale that has the most power (if that's larger)?

    # Or, there should be some relation between the brightness level of the frame and the
    # RMS of the frame--such that if the median of the frame - dark_sky is ~0, the sigma ~= 0.06,
    # but if frame - dark >> dark, then sigma should scale down.  So that's how I could set skyRMS_max.

    # Also look at imposing some continuity

    unmasked = np.where(diff_image != hp.UNSEEN)[0]
    skyRMS = robustRMS(diff_image[unmasked])
    if skyRMS_max is not None:
        skyRMS = np.min([skyRMS, skyRMS_max])

    # Try to find the angular scale of any clouds?

    smooth_map = hp.sphtfunc.smoothing(diff_image,
                                       fwhm=np.radians(fwhm),
                                       verbose=False,
                                       iter=1)
    smooth_map[unmasked] = smooth_map[unmasked] - np.median(
        smooth_map[unmasked])

    # outliers = np.where(np.abs(smooth_map[unmasked]) > sigma_cut*skyRMS)[0]
    # Can think about going back to the original map and growing the region that got flagged

    cloud_mask = np.zeros(diff_image.size, dtype=int)
    highOutliers = np.where((smooth_map > sigma_cut * skyRMS)
                            & (diff_image > sigma_second * skyRMS)
                            & (diff_image != hp.UNSEEN))
    lowOutliers = np.where((smooth_map < -1 * sigma_cut * skyRMS)
                           & (diff_image < -1 * sigma_second * skyRMS)
                           & (diff_image != hp.UNSEEN))

    cloud_mask[highOutliers] = 1
    cloud_mask[lowOutliers] = -1

    # rather than loop, let's just use smoothing in clever ways!
    if np.max(np.abs(cloud_mask)) != 0:
        for ack in np.arange(grow_iter):
            expanded_mask = hp.sphtfunc.smoothing(cloud_mask,
                                                  fwhm=np.radians(grow_fwhm),
                                                  verbose=False,
                                                  iter=1)
            cloud_mask[np.where((expanded_mask > grow_lower_limit)
                                & (diff_image > 0))] = 1
            cloud_mask[np.where((expanded_mask < -1. * grow_lower_limit)
                                & (diff_image < 0))] = -1

    # What is the most efficient way to loop over this stuff?
# if np.max(np.abs(cloud_mask)) != 0:
#     changed_pix =1
#     while changed_pix != 0:
#         clear_pix = np.where(cloud_mask == 0)[0]
#         for i in clear_pix:

    nside = hp.npix2nside(np.size(diff_image))
    pix_area = hp.nside2pixarea(nside)
    outliers = np.where(cloud_mask != 0)[0]
    out_area = outliers.size * pix_area * (180. / np.pi)**2

    return out_area, cloud_mask
    raw_flux = aperture_photometry(data, apertures)
    bkgflux_table = aperture_photometry(data, annulus_apertures)
    phot_table = hstack([raw_flux, bkgflux_table], table_names=['raw', 'bkg'])

    bkg_mean = phot_table['aperture_sum_bkg'] / annulus_apertures.area()

    bkg_sum = bkg_mean * apertures.area()
    final_sum = phot_table['aperture_sum_raw'] - bkg_sum
    phot_table['residual_aperture_sum'] = final_sum
    print(phot_table['residual_aperture_sum'])
    ack = np.array(positions).T
    #plt.plot(ack[0], ack[1], 'o')
    fluxes.append(phot_table['residual_aperture_sum'].data.tolist())
    backgrounds.append(
        [robustRMS(data[10:30, 10:30]),
         robustRMS(data[1000:1100, 1500:1600])])

fluxes = np.array(fluxes)
backgrounds = np.array(backgrounds)
print 'stars'
for i in np.arange(2):
    print 'mean, std, mean^0.5 = %f, %f, %f' % (
        fluxes[:, i].mean(), fluxes[:, i].std(), fluxes[:, i].mean()**0.5)

print 'background stats (unilluminated section and blankish area)'
for i in np.arange(2):
    print 'mean (of robust RMS), std = %f, %f' % (backgrounds[:, i].mean(),
                                                  backgrounds[:, i].std())

# Take away message, I think the standard deviation of the flux values is a factor of 2-3 more than what
Ejemplo n.º 3
0
        alt, az = stupidFast_RaDec2AltAz(ra, dec, site.latitude_rad,
                                         site.longitude_rad, mjd)

        out = np.where((np.isnan(frame)) | (np.isnan(previous))
                       | (frame == hp.UNSEEN) | (previous == hp.UNSEEN)
                       | (alt < np.radians(10.)))
        previous = frame.copy()
        diff[out] = hp.UNSEEN
        frame[out] = hp.UNSEEN
        diff_frac[out] = hp.UNSEEN
        diff_frac[np.where(previous == 0)] = hp.UNSEEN
        # maybe rotate based on LMST and latitude?
        gdiff = np.where((diff != hp.UNSEEN) & (~np.isnan(diff)))[0]

        if np.size(gdiff) > 0:
            rms = robustRMS(diff_frac[gdiff])
            median_value = np.median(diff_frac[gdiff])
            nout = np.size(
                np.where((
                    np.abs(diff[gdiff] - np.median(diff[gdiff])) > outlier_mag)
                         & (alt[gdiff] > alt_limit))[0])
            nabove = float(np.size(np.where(alt[gdiff] > alt_limit)[0])) * 100
            cf, cloud_mask = cloudyness(diff_frac, skyRMS_max=0.05)
            cf = cf / (gdiff.size * hp.nside2pixarea(nside) *
                       (180. / np.pi)**2)

            if nabove != 0:
                nout = nout / nabove
            else:
                nout = -666
        else: