Ejemplo n.º 1
0
def test_e():
    """Tests for #572 demonstrating how EM constants should behave."""

    from astropy.constants import e

    # A test quantity
    E = Q(100, 'V/m')

    # Without specifying a system e should not combine with other quantities
    pytest.raises(TypeError, lambda: e * E)
    # Try it again (as regression test on a minor issue mentioned in #745 where
    # repeated attempts to use e in an expression resulted in UnboundLocalError
    # instead of TypeError)
    pytest.raises(TypeError, lambda: e * E)

    # e.cgs is too ambiguous and should not work at all
    pytest.raises(TypeError, lambda: e.cgs * E)

    assert isinstance(e.si, Q)
    assert isinstance(e.gauss, Q)
    assert isinstance(e.esu, Q)

    assert e.si * E == Q(100, 'eV/m')
    assert e.gauss * E == Q(e.gauss.value * E.value, 'Fr V/m')
    assert e.esu * E == Q(e.esu.value * E.value, 'Fr V/m')
Ejemplo n.º 2
0
def test_view():
    """Check that Constant and Quantity views can be taken (#3537, #3538)."""
    from astropy.constants import c
    c2 = c.view(Constant)
    assert c2 == c
    assert c2.value == c.value
    # make sure it has the necessary attributes and they're not blank
    assert c2.uncertainty == 0  # c is a *defined* quantity
    assert c2.name == c.name
    assert c2.reference == c.reference
    assert c2.unit == c.unit

    q1 = c.view(Q)
    assert q1 == c
    assert q1.value == c.value
    assert type(q1) is Q
    assert not hasattr(q1, 'reference')

    q2 = Q(c)
    assert q2 == c
    assert q2.value == c.value
    assert type(q2) is Q
    assert not hasattr(q2, 'reference')

    c3 = Q(c, subok=True)
    assert c3 == c
    assert c3.value == c.value
    # make sure it has the necessary attributes and they're not blank
    assert c3.uncertainty == 0  # c is a *defined* quantity
    assert c3.name == c.name
    assert c3.reference == c.reference
    assert c3.unit == c.unit

    c4 = Q(c, subok=True, copy=False)
    assert c4 is c
Ejemplo n.º 3
0
def test_exponential_cutoff_power_law():
    model = ExponentialCutoffPowerLaw(
        amplitude=Q(1e-11, 'cm^-2 s^-1 TeV^-1'),
        e_0=Q(1, 'TeV'),
        alpha=2,
        e_cutoff=Q('3 TeV'),
    )
Ejemplo n.º 4
0
def test_power_law():
    model = PowerLaw(
        amplitude=Q(1e-11, 'cm^-2 s^-1 TeV^-1'),
        reference=Q(1, 'TeV'),
        index=2,
    )
    test_model(model)
Ejemplo n.º 5
0
def test_e():

    from astropy.constants.astropyconst13 import e as e_13

    # A test quantity
    E = Q(100.00000348276221, 'V/m')

    # e.cgs is too ambiguous and should not work at all
    with pytest.raises(TypeError):
        e_13.cgs * E

    assert isinstance(e_13.si, Q)
    assert isinstance(e_13.gauss, Q)
    assert isinstance(e_13.esu, Q)

    assert e_13.gauss * E == Q(e_13.gauss.value * E.value, 'Fr V/m')
    assert e_13.esu * E == Q(e_13.esu.value * E.value, 'Fr V/m')
Ejemplo n.º 6
0
def test_model(model):
    print(model)
    print(model(energy=Q(10, 'TeV')))
    print(model.integral(emin=Q(1, 'TeV'), emax=Q(2, 'TeV')))

    # plot
    # butterfly
    # npred
    reco_bins = 5
    true_bins = 10
    e_reco = Q(np.logspace(-1, 1, reco_bins + 1), 'TeV')
    e_true = Q(np.logspace(-1.5, 1.5, true_bins + 1), 'TeV')
    livetime = Q(26, 'min')
    aeff_data = Q(np.ones(true_bins) * 1e5, 'cm2')
    aeff = EffectiveAreaTable(energy=e_true, data=aeff_data)
    edisp_data = make_perfect_resolution(e_true, e_reco)
    edisp = EnergyDispersion(edisp_data, EnergyBounds(e_true),
                             EnergyBounds(e_reco))
    npred = calculate_predicted_counts(model=model,
                                       livetime=livetime,
                                       aeff=aeff,
                                       edisp=edisp)
    print(npred.data)
See Table 3.4, p. 33
"""
import numpy as np
import pandas as pd
from astropy.units import Quantity as Q

# c_all = 2e5
# a_open = Q(13.781, 'm2')

table_3_4 = pd.DataFrame({
    'tilt/deg': [0.00, 1.06, 2.12],
    'c_gapd': [121117, 119549, 114776],
    'c_support': [18584, 19574,
                  23488],  # c_support = c_all - c_gapd - c_shadow
    'c_shadow': [60299, 60877, 61736],
    'a_telescope/m2': [8.345, 8.237, 7.908],
    'a_support/m2': [1.280, 1.349, 1.618],
})

camera_body_diameter = Q(53, 'cm')
camera_depth = Q(0, 'cm')

a_camera = np.pi * (camera_body_diameter / 2)**2
a_sup_only = Q(table_3_4['a_support/m2'], 'm2') - a_camera

shadowing = np.mean(a_sup_only / Q(table_3_4['a_telescope/m2'], 'm2'))
transmission = 1 - shadowing

print(f"Transmission: {transmission:.4f}")
Ejemplo n.º 8
0
def main():
    geom = CameraGeometry.from_name("FACT")

    df = pd.DataFrame(
        {
            "pix_id": geom.pix_id,
            "pix_x": geom.pix_x,
            "pix_y": geom.pix_y
        }, ).set_index("pix_id")

    cols = ["CHID", "trigger_patch_id"]
    fact_pixels = get_pixel_dataframe()
    fact_pixels = fact_pixels[cols].set_index("CHID")

    sim_tel_camera = df.join(fact_pixels)

    general_cmt = """
# FACT Camera

# File generated with `config.py`
"""

    pixtype_cmt = """
# PixType format:
# Par.  1: pixel type (here always 1)
#       2: PMT type (must be 0)
#       3: cathode shape type (see below)
#       4: visible cathode diameter [cm]
#       5: funnel shape type (see below)
#       6: funnel diameter (flat-to-flat for hexagons and squares) [cm]
#       7: depth of funnel [cm]
#       8: a) funnel efficiency "filename",   b) funnel plate transparency
#       9: a) optional wavelength "filename", b) funnel wall reflectivity
# Cases a) and b) are distinguished by the format of parameter 8: a quoted
# string indicates case a) while a real number indicates case b).
# In case a) in column 8, columns 3, 4, and 7 are not used. If in case a)
# the optional file name for the wavelength dependence is provided, the
# overall scale in the file provided as parameter 8 is ignored because
# it is rescaled such that the average over all mirrors is equal to
# the given wavelength dependent value.
#
# Shape types: 0: circ., 1: hex(flat x), 2: sq., 3: hex(flat y)
"""

    shape = {
        "circ": 0,
        "hex(flat x)": 1,
        "sq": 2,
        "hex(flat y)": 3,
    }

    pixtype = {
        1: 1,  # pixel type (here always 1)
        2: 0,  # PMT type (must be 0)
        3: shape["sq"],  # cathode shape type (see below)
        4: Q(2.8, u.mm).to_value(u.cm),  # visible cathode diameter [cm]
        5: shape["hex(flat y)"],  # funnel shape type (see below)
        6:
        Q(9.5,
          u.mm).to_value(u.cm),  # funnel diameter (flat-to-flat for hex.) [cm]
        7: Q(20, u.mm).to_value(u.cm),  # depth of funnel [cm]
        # double quotes needed!
        8: '"cone-angular-acceptance.txt"',
        9: '"cone-transmission.txt"',
    }

    # pixtype_str = 'PixType {} {} {} {} {} {} {} {} {}'.format(*pixtype.values())
    pixtype_str = "PixType"
    for val in pixtype.values():
        pixtype_str += f" {val}"

    pixel_cmt = """
# Pixel format:
# Par.  1: pixel number (starting at 0)
#       2: x position [cm]
#       3: y position [cm]
#       4: drawer/module number
#       5: board number in module
#       5: channel number n board
#       6: board Id number ('0x....')
#       7: pixel on (is on if parameter is missing)
"""

    pixel = pd.DataFrame({
        "keyword":
        "Pixel",
        "pix_num":
        sim_tel_camera.index,
        "pix_type":
        1,  # set to pixel type above
        "x_pos":
        Q(sim_tel_camera["pix_x"], u.m).to_value(u.cm),
        "y_pos":
        Q(sim_tel_camera["pix_y"], u.m).to_value(u.cm),
        "module_number":
        sim_tel_camera["trigger_patch_id"],
        "board_number_in_module":
        sim_tel_camera["trigger_patch_id"],
        "channel_number_in_board":
        0,
        "board_id_number":
        sim_tel_camera["trigger_patch_id"],
        "pixel_on":
        1,
    })

    trigger_cmt = """"""

    trigger_str = ""
    trigger_str_prefix = "Trigger * of "
    for idx, grp in sim_tel_camera.groupby("trigger_patch_id"):
        trigger_str += trigger_str_prefix
        trigger_str += " ".join(map(str, list(grp.index))) + "\n"

    with open(output_path, "w") as f:
        f.write(general_cmt + "\n")
        f.write(pixtype_cmt + "\n")
        f.write(pixtype_str + "\n")
        f.write(pixel_cmt + "\n")

    pixel.to_csv(output_path, sep="\t", header=False, index=False, mode="a")

    with open(output_path, "a") as f:
        f.write(trigger_cmt + "\n")
        f.write(trigger_str + "\n")