def test_flux_calib_corr(datadir, plot=False): """ Test the handling of the flux calibration """ filename = datadir.join("test_sensitivity_cube.fits").strpath #filename = datadir.join("test.fits").strpath fcalib_corr = datadir.join("polyvals.txt").strpath wavelengths = [3500.0, 5500.0] alphas = [-3.5, -3.5] scube = SensitivityCube.from_file(filename, wavelengths, alphas) scube_corr1 = SensitivityCube.from_file(filename, wavelengths, alphas) scube_corr1.apply_flux_recalibration( 1.0, flux_calib_correction_file=fcalib_corr) # The aper_corr should be multiplied though in the cubes ratio = scube.sigmas.filled() / scube_corr1.sigmas.filled() if plot: import matplotlib.pyplot as plt ra, dec, wls = scube.wcs.wcs_pix2world(0, 0, range(scube.sigmas.shape[0]), 0) plt.plot(wls, (scube.f50vals[:, 10, 10] - scube_corr1.sigmas[:, 10, 10]) / scube.sigmas[:, 10, 10]) plt.show() # Check something happened assert ratio != pytest.approx(1.0)
def sensitivity_cube(datadir): """ A sensitivity cube to add or read""" filename = datadir.join("test_sensitivity_cube.fits").strpath wavelengths = [3500.0, 5500.0] alphas = [-3.5, -3.5] return SensitivityCube.from_file(filename, wavelengths, alphas)
def test_aper_corr(datadir, aper_corr): """ Test the handling of the aperture correction """ filename = datadir.join("test_sensitivity_cube.fits").strpath wavelengths = [3500.0, 5500.0] alphas = [-3.5, -3.5] scube = SensitivityCube.from_file(filename, wavelengths, alphas, aper_corr=aper_corr) scube_corr1 = SensitivityCube.from_file(filename, wavelengths, alphas, aper_corr=1.0) # The aper_corr should be multiplied though in the cubes ratio = scube.sigmas.filled() / scube_corr1.sigmas.filled() assert ratio == pytest.approx(aper_corr)
def test_completeness_func(datadir, flux, model, sncut, expected): """ Test that a value is returned """ filename = datadir.join("test_sensitivity_cube.fits").strpath wavelengths = [3500.0, 5500.0] alphas = [-3.5, -3.5] if model == "hdr1": scube = SensitivityCube.from_file(filename, wavelengths, alphas, nsigma=1.0, flim_model=model, aper_corr=None) else: scube = SensitivityCube.from_file(filename, wavelengths, alphas, nsigma=1.0, flim_model=model) c = scube.return_completeness(flux, 161.4201, 50.8822, 3478, sncut) assert c == pytest.approx(expected)
def add_sensitivity_cube_to_hdf5(args=None): """ Command line tool to add a sensitivity cube(s) to an HDF5 containter """ import argparse parser = argparse.ArgumentParser( description="Add sensitivty cubes to HDF5 container", formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) parser.add_argument( "--regex", default=".*(2[0-9]{7}v[0-9]{3})_[0-9]{3}_([0-9]{3})", help="""Regex with two capture groups, the first for datevshot the second for IFU slot""", ) parser.add_argument("--append", action="store_true", help="Append to existing HDF5") parser.add_argument( "--alpha", type=float, help="Alpha for Fleming function", default=-3.5 ) parser.add_argument( "fits_files", type=str, nargs="+", help="Files to add, filename must follow convention set out in --regex option", ) parser.add_argument("hdf5_out", type=str, help="HDF5 container to add to") opts = parser.parse_args(args=args) if opts.append: hdfcont = SensitivityCubeHDF5Container(opts.hdf5_out, mode="a") else: hdfcont = SensitivityCubeHDF5Container(opts.hdf5_out, mode="w") # Compile the regex so it's faster regex = compile(opts.regex) for fn in opts.fits_files: m = regex.match(fn) datevshot = m.group(1) ifuslot = m.group(2) _logger.info( "Inserting {:s} with dateshot {:s} and IFU slot {:s}".format( fn, datevshot, ifuslot ) ) scube = SensitivityCube.from_file( fn, [3500.0, 5500.0], [opts.alpha, opts.alpha] ) hdfcont.add_sensitivity_cube("virus_" + datevshot, "ifuslot_" + ifuslot, scube) hdfcont.close()
def collapse_datacubes_command(args=None): """ Produce combined flux limit versus wavelength estimates for sensitivity cubes passed on command line """ import argparse # Command line options parser = argparse.ArgumentParser(description=""" Collapse the RA and DEC of a single or set of sensitivity cube(s) to produce one file of 50% flux limit versus wavelength. """) parser.add_argument("--plot", type=str, help="Filename for optional plot", default="") parser.add_argument( "--fmin", help="Minimum flux to consider when interpolating for 50% limit", type=float, default=1e-17) parser.add_argument( "--fmax", help="""Maximum flux to consider when interpolating for 50% limit. Regions not 99% at this flux ignored!""", type=float, default=1e-15) parser.add_argument( "--nbins", help= "Number of flux bin to use when interpolating to measure 50% limit", type=int, default=100) parser.add_argument( "--alpha", help="The alpha of the Fleming function fit (default=-3.5)", default=-3.5, type=float) parser.add_argument( "scubes", nargs='+', help="The sensitivity cube(s) you want to collapse and combine") parser.add_argument("output", help="(Ascii) file to output to", type=str) opts = parser.parse_args(args=args) # Stores a list of the completeness versus flux and lambda for # all the cubes compl_cube_list = [] # Flux bins to compute completeness in fluxes = linspace(opts.fmin, opts.fmax, opts.nbins) # A list of the number of good pixels in each cube as # a function of wavelength npixes_list = [] for cube_name in opts.scubes: # Currently fix the alpha versus wavelength tscube = SensitivityCube.from_file(cube_name, [3500.0, 5500.0], [opts.alpha, opts.alpha]) lambda_, npix, compls = return_spatially_collapsed_cube(tscube, fluxes) compl_cube_list.append(compls) npixes_list.append(npix) # Produce a combined cube of completness versus flux and lambda, # weighted by the number of good pixels in each cube combined_cube = compl_cube_list[0] * npixes_list[0] for npix, compl_cube in zip(npixes_list[1:], compl_cube_list[1:]): combined_cube += npix * compl_cube # This should give the correct completeness versus lambda and flux, ignoring empty pixels combined_cube /= sum(array(npixes_list), axis=0) # This should give is the 50% flux limits and the new alpha values f50vals, alphas = compute_new_fleming_fits(lambda_, fluxes, combined_cube) if opts.plot: plot_collapsed_cube(f50vals, alphas, lambda_, fluxes, combined_cube, opts.plot) # Write out table = Table([lambda_, f50vals, alphas], names=["wavelength", "f50", "alpha"]) table.write(opts.output, format="ascii")