Esempio n. 1
0
def test_full_fit():
    # Finally check everything

    # Create our fitter object
    fitter = TemplateFitter(min_fit_pixels=0, training_library="KNN")
    # Get our example data file (10 events of 1 TeV at 0 Alt, 0 Az)
    data_dir = pkg_resources.resource_filename('template_builder', 'data/')
    # Which needs to actually be there
    data_dir += "/gamma_HESS_example.simhess.gz"

    # Run full template generation
    template, var_template = fitter.generate_templates(
        [data_dir], "./test.template.gz", "./test_var.template.gz", True)

    # Make sure we get something out
    assert template is not None
    assert var_template is not None

    import os.path
    os.path.isfile("./test.template.gz")
    os.path.isfile("./test_var.template.gz")

    # Open our output files
    import pickle, gzip
    template_fromfile = pickle.load(gzip.open("./test.template.gz", "r"))
    var_template_fromfile = pickle.load(
        gzip.open("./test_var.template.gz", "r"))

    # And check the contents are the same
    for key in template:
        assert template[key].all() == template_fromfile[key].all()
        assert var_template[key].all() == var_template_fromfile[key].all()

    os.remove("./test.template.gz")
    os.remove("./test_var.template.gz")
Esempio n. 2
0
def generate_templates():
    """
    main() function to call all steps of template production in series

    :return: None
    """

    # First Lets parse the command line
    parser = ArgumentParser()
    parser.add_argument('-c', '--config', action='append', nargs=1,
                        metavar="config file",
                        help='Configuration YAML file locations')
    parser.add_argument('-o', '--output', default="test.templates.gz",
                        metavar="output file",
                        help='Name of output file')

    parser.add_argument('--simulate-only', dest='simulate_only', action='store_true')
    parser.add_argument('--SGE', dest='SGE', action='store_true')

    args = parser.parse_args()

    # Followed by any config files
    corsika_input, simulation_input, telescope_input, fit_input = \
        parse_config(args.config)
    output_file = args.output

    # Generate our range of CORSIKA input cards
    corsika = CORSIKAInput(input_parameters=corsika_input,
                           min_events=simulation_input["min_events"])

    cards = corsika.get_input_cards(simulation_input["event_number"],
                                    simulation_input["altitude"],
                                    simulation_input["azimuth"],
                                    simulation_input["energy_bins"],
                                    simulation_input["core_bins"],
                                    simulation_input["rotation_angle"],
                                    simulation_input["diameter"],
                                    get_run_script(telescope_input["config_name"])
                                    )
    # And write them in the sim_telarray directory
    corsika_input_file_names = \
        write_corsika_input_cards(telescope_input["sim_telarray_directory"], cards)

    # Then create the required sim_telearray telescope config files
    sim_telarray_config = SimTelArrayConfig(telescope_input["config_name"],
                                            telescope_input["config_file"],
                                            float(corsika_input["OBSLEV"])/100,
                                            telescope_input["atmosphere"],
                                            telescope_input["optical_efficiency"],
                                            telescope_input["extra_options"]
                                            )

    run_commands, output_paths = \
        sim_telarray_config.run_setup(telescope_input["sim_telarray_directory"],
                                      corsika_input_file_names)

    # Annoyingly sim_telarray doesn't let us choose our output file name (at least in
    # this script setup). So we instead look in output directory now and after our
    # simulations are complete and take the new files
    files_before = get_file_list(output_paths)

    # Submit to SGE cluster if we can
    if args.SGE:
        try:
            from submit_SGE import SubmitSGE
        except ImportError:
            print("submit_SGE package required for cluster submission")
        submit = SubmitSGE()
        submit.submit_job_list(run_commands,
                               telescope_input["config_name"]+"_temp")
    # Otherwise run on the command line
    else:
        for command in run_commands:
            print("Running", command)
            os.system(command)

    print("Simulations complete")

    files_after = get_file_list(output_paths)
    # Create a list of newly created files
    files_after = list(set(files_after) - set(files_before))

    if len(files_after) == 0:
        print("No new simulation files created! Quitting before fit")
        return

    # Then generate our templates from these
    fitter = TemplateFitter(min_fit_pixels=2000)
    fitter.generate_templates(files_after, output_file, max_events=50000)

    return