def test_dlambda_extra_wide(self):
        """
        Compare auto velocity against setting same dlambda with
        min and max set extra wide.
        """

        sg_auto = SpectrumGenerator(lambda_min='auto',
                                    lambda_max='auto',
                                    dlambda=1.0,
                                    bin_space='velocity')
        sg_auto.make_spectrum("ray.h5",
                              lines=self.line_list,
                              store_observables=True)

        sg_comp = SpectrumGenerator(lambda_min=sg_auto.lambda_field[0],
                                    lambda_max=sg_auto.lambda_field[-1],
                                    n_lambda=sg_auto.lambda_field.size,
                                    bin_space='velocity')
        sg_comp.make_spectrum("ray.h5",
                              lines=self.line_list,
                              store_observables=True)

        assert_allclose(
            sg_auto.tau_field.sum(),
            sg_comp.tau_field.sum(),
            rtol=1e-7,
            err_msg='Total tau disagrees with extra wide spectrum.')
    def test_setting_lambda_max(self):
        """
        Test setting an arbitrary lambda_max with auto-lambda.
        """

        sg_auto = SpectrumGenerator(lambda_min='auto',
                                    lambda_max='auto',
                                    dlambda=1.0,
                                    bin_space='velocity')
        sg_auto.make_spectrum("ray.h5",
                              lines=self.line_list,
                              ly_continuum=False)

        sg_comp = SpectrumGenerator(lambda_min='auto',
                                    lambda_max=-10000.,
                                    dlambda=1.0,
                                    bin_space='velocity')
        sg_comp.make_spectrum("ray.h5",
                              lines=self.line_list,
                              ly_continuum=False)

        comp_lambda = sg_auto.lambda_field <= sg_comp.lambda_field[-1]

        assert_allclose(sg_auto.tau_field[comp_lambda].sum(),
                        sg_comp.tau_field.sum())
    def test_absorption_spectrum_with_continuum(self):
        """
        This test generates an absorption spectrum then does a default fit.
        Uses the example in the docs as a template:
        https://trident.readthedocs.io/en/latest/absorption_spectrum_fit.html
        """

        ds = load(ISO_GALAXY)
        line_list = ['O VI']
        ray = make_simple_ray(ds,
                              start_position=ds.domain_left_edge,
                              end_position=ds.domain_right_edge,
                              data_filename='ray.h5',
                              lines=line_list,
                              ftype='gas')
        sg = SpectrumGenerator('COS')
        sg.make_spectrum(ray, lines=line_list)
        wavelength = sg.lambda_field
        flux = sg.flux_field

        OVI_parameters = {
            'name': 'OVI',
            'f': [.1325, .06580],
            'Gamma': [4.148E8, 4.076E8],
            'wavelength': [1031.9261, 1037.6167],
            'numLines': 2,
            'maxN': 1E17,
            'minN': 1E11,
            'maxb': 300,
            'minb': 1,
            'maxz': 6,
            'minz': 0,
            'init_b': 20,
            'init_N': 1E12
        }

        speciesDicts = {'OVI': OVI_parameters}

        orderFits = ['OVI']
        fitted_lines, fitted_flux = generate_total_fit(wavelength, flux,
                                                       orderFits, speciesDicts)
示例#4
0
    def test_n_lambda(self):
        """
        Compare auto velocity against setting same min, max, and n_lambda
        """

        sg_auto = SpectrumGenerator(
            lambda_min='auto', lambda_max='auto',
            dlambda=1.0, bin_space='velocity')
        sg_auto.make_spectrum("ray.h5", lines=self.line_list,
                              store_observables=True)

        sg_comp = SpectrumGenerator(
            lambda_min=sg_auto.lambda_field[0],
            lambda_max=sg_auto.lambda_field[-1],
            n_lambda=sg_auto.lambda_field.size,
            bin_space='velocity')
        sg_comp.make_spectrum("ray.h5", lines=self.line_list,
                              store_observables=True)
        compare_spectra(sg_auto, sg_comp, 'manually setting n_lambda')
示例#5
0
    def test_dlambda(self):
        """
        Compare auto wavelength against setting same min, max, and dlambda
        """

        sg_auto = SpectrumGenerator(lambda_min='auto',
                                    lambda_max='auto',
                                    dlambda=0.01)
        sg_auto.make_spectrum("ray.h5",
                              lines=self.line_list,
                              store_observables=True)

        sg_comp = SpectrumGenerator(lambda_min=sg_auto.lambda_field[0],
                                    lambda_max=sg_auto.lambda_field[-1],
                                    dlambda=sg_auto.bin_width)
        sg_comp.make_spectrum("ray.h5",
                              lines=self.line_list,
                              store_observables=True)
        compare_spectra(sg_auto, sg_comp, 'manually setting dlambda')
示例#6
0
    def test_setting_lambda_min(self):
        """
        Test setting an arbitrary lambda_min with auto-lambda.
        """

        sg_auto = SpectrumGenerator(lambda_min='auto',
                                    lambda_max='auto',
                                    dlambda=0.01)
        sg_auto.make_spectrum("ray.h5",
                              lines=self.line_list,
                              ly_continuum=False)

        sg_comp = SpectrumGenerator(lambda_min=1100,
                                    lambda_max='auto',
                                    dlambda=0.01)
        sg_comp.make_spectrum("ray.h5",
                              lines=self.line_list,
                              ly_continuum=False)

        comp_lambda = sg_auto.lambda_field >= sg_comp.lambda_field[0]

        assert_allclose(sg_auto.tau_field[comp_lambda].sum(),
                        sg_comp.tau_field.sum())
示例#7
0
    def test_empty_spectrum(self):
        """
        Test we can make an empty spectrum without crashing.
        """

        sg = SpectrumGenerator(lambda_min=3000,
                               lambda_max='auto',
                               dlambda=1.0,
                               bin_space='velocity')
        sg.make_spectrum("ray.h5", lines=['H I 1216'],
                         output_file='blank.h5',
                         output_absorbers_file='blank.txt',
                         store_observables=True, ly_continuum=True)
        sg.plot_spectrum('spec_blank.png')
        assert sg.lambda_field is None
        assert sg.tau_field is None
        assert sg.flux_field is None
示例#8
0
    def test_enzo_small_simple(self):
        """
        This is an answer test, which compares the results of this test
        against answers generated from a previous version of the code.

        This test generates a COS spectrum from a single Enzo dataset
        using a simple ray and compare the ray and spectral output data
        against a known answer.

        """

        # Set the dataset filename, load it into yt and define the trajectory
        # of the LightRay to cross the box from one corner to the other.
        ds = load(os.path.join(enzo_small, 'RD0009/RD0009'))
        ray_start = ds.domain_left_edge
        ray_end = ds.domain_right_edge

        # Make a LightRay object including all necessary fields so you can add
        # all H, C, N, O, and Mg fields to the resulting spectrum from your dataset.
        # Save LightRay to ray.h5 and use it locally as ray object.
        ray_fn = 'enzo_small_simple_ray.h5'
        ray = make_simple_ray(ds,
                              start_position=ray_start,
                              end_position=ray_end,
                              data_filename=ray_fn,
                              lines=['H', 'C', 'N', 'O', 'Mg'],
                              ftype='gas')

        # Now use the ray object to actually generate an absorption spectrum
        # Use the settings (spectral range, LSF, and spectral resolution) for COS
        # And save it as an output hdf5 file and plot it to an image.
        sg = SpectrumGenerator('COS')
        sg.make_spectrum(ray, lines=['H', 'C', 'N', 'O', 'Mg'])
        raw_file = 'enzo_small_simple_spec_raw.h5'
        raw_file_compare = os.path.join(test_results_dir, raw_file)
        sg.save_spectrum(raw_file)
        sg.plot_spectrum('enzo_small_simple_spec_raw.png')

        # "Final" spectrum with added quasar, MW background, applied line-spread
        # function, and added gaussian noise (SNR=30)
        # Save as a text file and plot it to an image.
        sg.add_qso_spectrum()
        sg.add_milky_way_foreground()
        sg.apply_lsf()
        sg.add_gaussian_noise(30, seed=1)
        final_file = 'enzo_small_simple_spec_final.h5'
        final_file_compare = os.path.join(test_results_dir, final_file)
        sg.save_spectrum(final_file)
        sg.plot_spectrum('enzo_small_simple_spec_final.png')

        if generate_results:
            os.rename(raw_file, raw_file_compare)
            os.rename(final_file, final_file_compare)

        else:
            old_spec = h5py.File(raw_file_compare, 'r')
            new_spec = h5py.File(raw_file, 'r')
            for key in old_spec.keys():
                assert_almost_equal(new_spec[key].value, old_spec[key].value, \
                                    decimal=err_precision,
                                    err_msg='Raw spectrum array does not match '+\
                                    'for enzo_small_simple answer test')
            old_spec.close()
            new_spec.close()

            old_spec = h5py.File(final_file_compare, 'r')
            new_spec = h5py.File(final_file, 'r')
            for key in old_spec.keys():
                assert_almost_equal(new_spec[key].value, old_spec[key].value, \
                                    decimal=err_precision,
                                    err_msg='Final spectrum array does not match '+\
                                    'for enzo_small_simple answer test')
            old_spec.close()
            new_spec.close()
示例#9
0
    def test_enzo_small_compound(self):
        """
        This is an answer test, which compares the results of this test
        against answers generated from a previous version of the code.

        This test generates a COS spectrum from a single Enzo dataset
        using a compound ray and compare the ray and spectral output data
        against a known answer.
        """

        # Set the dataset filename, the redshift range to be covered, and the
        # ions in which we're interested, and give the random number generator
        # a seed (for determining the ray trajectory).  Use these to create a
        # compound ray spanning multiple datasets to cover the desired redshift
        # range.
        fn = os.path.join(enzo_small, 'AMRCosmology.enzo')
        redshift_start = 0.00
        redshift_end = 0.015
        lines = ['H', 'C', 'N', 'O', 'Mg']
        seed = 1
        ray_fn = 'enzo_small_compound_ray.h5'
        ray = make_compound_ray(fn,
                                'Enzo',
                                redshift_start,
                                redshift_end,
                                lines=lines,
                                seed=seed,
                                ftype='gas',
                                data_filename=ray_fn)

        # Now use the ray object to actually generate an absorption spectrum
        # Use the settings (spectral range, LSF, and spectral resolution) for COS
        # And save it as an output hdf5 file and plot it to an image.
        sg = SpectrumGenerator('COS')
        sg.make_spectrum(ray, lines=lines)
        raw_file = 'enzo_small_compound_spec_raw.h5'
        raw_file_compare = os.path.join(test_results_dir, raw_file)
        sg.save_spectrum(raw_file)
        sg.plot_spectrum('enzo_small_compound_spec_raw.png')

        # "Final" spectrum with added quasar, MW background, applied line-spread
        # function, and added gaussian noise (SNR=30)
        # Save as a text file and plot it to an image.
        sg.add_qso_spectrum()
        sg.add_milky_way_foreground()
        sg.apply_lsf()
        sg.add_gaussian_noise(30, seed=1)
        final_file = 'enzo_small_compound_spec_final.h5'
        sg.save_spectrum(final_file)
        final_file_compare = os.path.join(test_results_dir, final_file)
        sg.plot_spectrum('enzo_small_compound_spec_final.png')

        if generate_results:
            os.rename(raw_file, raw_file_compare)
            os.rename(final_file, final_file_compare)

        else:
            old_spec = h5py.File(raw_file_compare, 'r')
            new_spec = h5py.File(raw_file, 'r')
            for key in old_spec.keys():
                assert_almost_equal(new_spec[key].value, old_spec[key].value, \
                                    decimal=err_precision,
                                    err_msg='Raw spectrum array does not match '+\
                                    'for enzo_small_compound answer test')
            old_spec.close()
            new_spec.close()

            old_spec = h5py.File(final_file_compare, 'r')
            new_spec = h5py.File(final_file, 'r')
            for key in old_spec.keys():
                assert_almost_equal(new_spec[key].value, old_spec[key].value, \
                                    decimal=err_precision,
                                    err_msg='Final spectrum array does not match '+\
                                    'for enzo_small_compound answer test')
            old_spec.close()
            new_spec.close()