def main(cube_filename, sigma_lim, l1, l2, output_name, k1=1000, k2=1000, tN=15, overwrite=False): cube = SpectralCube.read(cube_filename) noise_cube, variance_cube = compute_errors(cube, sigma_lim, l1, l2, k1, k2, tN) noisy_cube = cube + noise_cube write_cube(noisy_cube, variance_cube, output_name, overwrite)
def create_single_spectra_cube(bins, mask=None, header=None, wcs=None): from simifucube.generate_spectra import STD_MET, STD_AGE, to_spectrum1d from simifucube.spectra import Spectrum mass = 8000 # Msol print('Creating cube from a particle with age{}, met={}, mass={} Msol'. format(STD_AGE, STD_MET, mass)) sp = Spectrum.from_met_age(STD_MET, STD_AGE) L_sol = 3.839e33 # erg s-1 kpc_in_cm = 3.086e+21 # cm pos = np.array([0.0, 0.0, 0.0]) z_dist = 20000 # kpc dist_sq = np.linalg.norm(pos - np.array([0, 0, z_dist]))**2 sp.flux *= mass * L_sol / (4 * np.pi * dist_sq * kpc_in_cm**2) sp_list = [sp] * bins**2 spectra1d = to_spectrum1d(sp_list) if header is None: _header = get_header() wvl = spectra1d.spectral_axis.value wvl_diff = np.diff(wvl) # Check if first diff is actually the same everywhere if not np.allclose(wvl_diff[0] * np.ones_like(wvl[:-1]), wvl_diff, rtol=1e-05, atol=1e-08): raise RuntimeError('Spectral axis has varying resolution') _header['CD3_3'] = wvl_diff[0] _header['CRVAL3'] = wvl[0] _header['CRPIX1'] = _header['CRPIX2'] = bins / 2 header = _header if wcs is None: wcs = WCS(header) data = spectra1d.data.reshape(bins, bins, spectra1d.shape[1]).astype( np.float32).transpose(2, 0, 1) if mask is None: mask = LazyMask(np.isfinite, data=data, wcs=wcs) cube = SpectralCube(data=data * spectra1d.flux.unit, wcs=wcs, mask=mask, header=header) print("Writing output...") output_name = contract_name("ssp_cube_b{}{}.fits".format( bins, '_nods' if not doppler_shift else '')) write_cube(final_cube, variance_cube, output_name, overwrite_output) sys.exit(0)
print('Adding noise to signal') cg.datacube += noise cube_sph = cg.create_spectral_cube() if do_spectral_rebinning: muse_cube = muse_rebin(snsp.last_valid_freq, cube_sph) else: muse_cube = cube_sph print('Computing STAT HDU') s = np.shape(muse_cube._data) print(s) just_spectra = np.reshape(muse_cube._data, [s[0], s[1] * s[2]]) n_spectra = just_spectra.shape[1] print("Estimating the error spectra with the DER_SNR algorithm") error_spectra = np.zeros(just_spectra.shape) for i in range(n_spectra): error_spectra[:, i] = DER_SNR(just_spectra[:, i]) # np.abs(np.nanmedian(np.sqrt(error_spectra),axis=0)) stat_data = np.reshape(error_spectra, muse_cube.shape).astype(np.float32) variance_cube = SpectralCube(data=stat_data**2 * (muse_cube.unit)**2, wcs=muse_cube.wcs, header=muse_cube.header) # muse_cube.write(out_name+'pix{}.fits'.format(bins), overwrite=True) write_cube(muse_cube, variance_cube=variance_cube, filename=out_name + '_r{}pix{}.fits'.format(size_cuboid, bins), overwrite=True)
final_cube = muse_cube + noise_cube else: print("\nComputing cube errors...") noise_cube, variance_cube = compute_errors(cube=muse_cube, sigma_lim=sigma_inst, l1=limits[0], l2=limits[1], k1=k, k2=k, tN=tN) print('Adding noise...') final_cube = muse_cube + noise_cube else: final_cube = muse_cube variance_cube = None print("Writing output...") output_name = contract_name("cube", sim, isnap, peri, size_cuboid, ext='fits', bins=bins, fix=fix_star_met_age, doppler_shift=doppler_shift) write_cube(final_cube, variance_cube, output_name, overwrite_output)
def generate_cube(config): out_name = config['out_name'] bins = config.getint('bins') size_cuboid = config.getfloat('size_cuboid') if out_name is None or out_name == '': out_name = os.path.splitext( config['snap_name'])[0] + 'r{}pix{}.fits'.format( size_cuboid, bins) snsp = SnapSpectra(config['snap_name'], size_cuboid=size_cuboid, do_preprocessing=config.getboolean('do_preprocessing')) if config.getboolean('use_saved_spectra'): snsp.generate_spectra_from_pickle(config['pickle_name']) else: snsp.generate_spectra( save_as=config['pickle_name'], use_template_star=config.getboolean('use_template_star')) # print(snsp._preprocessed_snap['smooth']) # print(snsp._preprocessed_snap['mass']) cg = CubeGenerator(snsp, bins=bins) if config.getboolean('output_LOS_sum'): im_los = cg.sum_on_line_of_sigth() print('datacube max flux', np.max(cg.datacube)) cube = cg.create_spectral_cube() sum_outname = os.path.splitext(out_name)[0] + 'sum.fits' print(f'Writing cube {sum_outname}') cube.write(sum_outname, overwrite=True) if config.getboolean('output_SPH_projection'): im = cg.sph_projection_direct(num_threads=config.getint('num_threads')) print('datacube max flux', np.max(cg.datacube)) # # Do noise: # if config.getboolean('do_noise'): # print('Computing noise') # # We defined the contamination range (the dispersion ) in a way that the residuals # # between the constructed spectrum and the original spectrum of # # the library to be less than ~ 0.073. # sigma = config.getfloat('sigma') # noise = np.random.normal(loc=0.0, scale=sigma*cg.datacube) # print('Adding noise to signal') # cg.datacube += noise # Creating actual cube cube_sph = cg.create_spectral_cube() if config.getboolean('do_spectral_smoothing'): cube_sph = muse_spectral_smooth(cube_sph) cube_sph._data = cube_sph._data.astype(np.float32) # Do noise: if config.getboolean('do_noise'): print('Computing noise') # We defined the contamination range (the dispersion ) in a way that the residuals # between the constructed spectrum and the original spectrum of # the library to be less than ~ 0.073. sigma = config.getfloat('sigma') noise = np.random.normal(loc=0.0, scale=sigma * cube_sph._data).astype( np.float32) print('Adding noise to signal') cube_sph._data += noise if config.getboolean('do_spectral_rebinning'): muse_cube = muse_rebin(snsp.last_valid_freq, cube_sph) else: muse_cube = cube_sph if config.getboolean('STAT_HDU'): print('Computing STAT HDU') s = np.shape(muse_cube._data) print('shape muse_cube:', s) just_spectra = np.reshape(muse_cube._data, [s[0], s[1] * s[2]]) n_spectra = just_spectra.shape[1] print("Estimating the error spectra with the DER_SNR algorithm") error_spectra = np.zeros(just_spectra.shape) for i in range(n_spectra): error_spectra[:, i] = DER_SNR(just_spectra[:, i]) # np.abs(np.nanmedian(np.sqrt(error_spectra),axis=0)) stat_data = np.reshape(error_spectra, muse_cube.shape).astype(np.float32) variance_cube = SpectralCube(data=stat_data**2 * (muse_cube.unit)**2, wcs=muse_cube.wcs, header=muse_cube.header) else: variance_cube = None # muse_cube.write(out_name+'pix{}.fits'.format(bins), overwrite=True) # this is for ignoring the HIERARCH keywords warning warnings.simplefilter('ignore', category=VerifyWarning) write_cube(muse_cube, variance_cube=variance_cube, filename=out_name, meta=dict(config), overwrite=True)
overwrite=False): cube = SpectralCube.read(cube_filename) noise_cube, variance_cube = compute_errors(cube, sigma_lim, l1, l2, k1, k2, tN) noisy_cube = cube + noise_cube write_cube(noisy_cube, variance_cube, output_name, overwrite) MUSE_SP_AX_RES = 1.25 ## Angstrom if __name__ == '__main__': cube_name = 'cube_produced/published/cube_69p2_0227_r3_b80_err.fits' cube = SpectralCube.read(cube_name) filename = 'prova.fits' k = 1000 # limits = 6000, 8500 limits = -100000, 30000 # No limits tN = 10 # from Figure 21 Law et al.(2016) # sigma_lim = 4 * 1000 /(MUSE_SP_AX_RES * 5 ) = 640 # BAD! # 1e-20 erg s-1 cm-2 # From median of the variance in a real datacube: sigma_lim = 4 print("sigma_lim =", sigma_lim) plot_sigmas(cube, *limits, k, k, tN) sigmas = compute_sigmas(cube, sigma_lim, *limits, k, k, tN) noise_cube = get_error_cube(cube, sigmas) variance_cube = get_variance_cube(cube, sigmas) noisy_cube = cube + noise_cube write_cube(noisy_cube, variance_cube, filename, overwrite=True) plt.show()