def read_blondin_toymodel(fname):
    header = '''
    vel[km/s]  rad[cm]     dvol[cm^3]  dens[g/cm^3] dmass[g]    temp[K]     c           o           ne          na
    mg          al          si          s           cl          ar          k           ca          sc          ti
    v           cr          mn          fe          co          ni          ni56        co56        ni57        co57
    cr48        v48         cr49        v49         mn51        cr51        co55        fe55        k37         ar37
    fe52        mn52        ti44        sc44        ar41        k42         k43         sc43        sc47        co61
    ni56_0      co56_0      ni57_0      co57_0      cr48_0      v48_0       cr49_0      v49_0       mn51_0      cr51_0
    co55_0      fe55_0      k37_0       ar37_0      fe52_0      mn52_0      ti44_0      sc44_0      ar41_0      k42_0
    k43_0       sc43_0      sc47_0      co61_0
    '''
    header = header.replace('_0', '')
    header = header.split()
    print(header)

    with open(fname, 'r') as fh:
        for line in fh:
            if line.startswith("#idx"):
                break
        else:
            raise ValueError('File {0} does not conform to Toy Model format as it does not contain #idx')
    columns = [pattern_remove_bracket.sub('', item) for item in line[1:].split()]



    raw_blondin_csv =  pd.read_csv(fname, delim_whitespace=True, comment='#', header=None, names=columns)
    raw_blondin_csv.set_index('idx', inplace=True)
    blondin_csv = raw_blondin_csv.loc[:, ['vel', 'dens', 'temp', 'X_56Ni0', 'X_Ti', 'X_Ca', 'X_S', 'X_Si', 'X_O', 'X_C']]
    rename_col_dict = {'vel':'velocity', 'dens':'density', 'temp':'t_rad'}
    rename_col_dict.update({item:item[2:] for item in blondin_csv.columns[3:]})
    rename_col_dict['X_56Ni0'] = 'Ni56'
    blondin_csv.rename(columns=rename_col_dict, inplace=True)
    blondin_csv.iloc[:, 3:] = blondin_csv.iloc[:,3:].divide(blondin_csv.iloc[:,3:].sum(axis=1), axis=0)


    #changing velocities to outer boundary
    new_velocities = 0.5 * (blondin_csv.velocity.iloc[:-1].values + blondin_csv.velocity.iloc[1:].values)
    new_velocities = np.hstack((new_velocities, [2 * new_velocities[-1] - new_velocities[-2]]))
    blondin_csv['velocity'] = new_velocities



    with open(fname, 'r') as fh:
        t0_string = t0_pattern.findall(fh.read())[0]

    t0 = parse_quantity(t0_string.replace('DAYS', 'day'))
    blondin_dict = {}
    blondin_dict['model_density_time_0'] = str(t0)
    blondin_dict['description'] = 'Converted {0} to csvy format'.format(fname)
    blondin_dict['tardis_model_config_version'] = 'v1.0'
    blondin_dict_fields = [dict(name='velocity', unit='km/s', desc='velocities of shell outer bounderies.')]
    blondin_dict_fields.append(dict(name='density', unit='g/cm^3', desc='mean density of shell.'))
    blondin_dict_fields.append(dict(name='t_rad', unit='K', desc='radiative temperature.'))

    for abund in blondin_csv.columns[3:]:
        blondin_dict_fields.append(dict(name=abund, desc='Fraction {0} abundance'.format(abund)))
    blondin_dict['datatype'] = {'fields':blondin_dict_fields}

    return blondin_dict, blondin_csv
Beispiel #2
0
def read_cmfgen_density(fname):
    """
    Reading a density file of the following structure (example; lines starting with a hash will be ignored):
    The first density describes the mean density in the center of the model and is not used.
    The file consists of a header row and next row contains unit of the respective attributes
    Note that the first column has to contain a running index

    Example:

    index velocity densities electron_densities temperature
    - km/s g/cm^3 /cm^3 K
    0 871.66905 4.2537191e-09 2.5953807e+14 7.6395577
    1 877.44269 4.2537191e-09 2.5953807e+14 7.6395577

    Rest columns contain abundances of elements and isotopes

    Parameters
    ----------
    fname : str
        filename or path with filename

    Returns
    -------
    time_of_model : astropy.units.Quantity
        time at which the model is valid
    velocity : np.ndarray
    mean_density : np.ndarray
    electron_densities : np.ndarray
    temperature : np.ndarray
    """
    warnings.warn("The current CMFGEN model parser is deprecated",
                  DeprecationWarning)

    df = pd.read_csv(fname, comment="#", delimiter=r"\s+", skiprows=[0, 2])

    with open(fname) as fh:
        for row_index, line in enumerate(fh):
            if row_index == 0:
                time_of_model_string = line.strip().replace("t0:", "")
                time_of_model = parse_quantity(time_of_model_string)
            elif row_index == 2:
                quantities = line.split()

    velocity = u.Quantity(df["velocity"].values, quantities[1]).to("cm/s")
    temperature = u.Quantity(df["temperature"].values, quantities[2])[1:]
    mean_density = u.Quantity(df["densities"].values, quantities[3])[1:]
    electron_densities = u.Quantity(df["electron_densities"].values,
                                    quantities[4])[1:]

    return (
        time_of_model,
        velocity,
        mean_density,
        electron_densities,
        temperature,
    )
Beispiel #3
0
def read_cmfgen_density(fname):
    """
    Reading a density file of the following structure (example; lines starting with a hash will be ignored):
    The first density describes the mean density in the center of the model and is not used.
    The file consists of a header row and next row contains unit of the respective attributes
    Note that the first column has to contain a running index

    Example:

    index velocity densities electron_densities temperature
    - km/s g/cm^3 /cm^3 K
    0 871.66905 4.2537191e-09 2.5953807e+14 7.6395577
    1 877.44269 4.2537191e-09 2.5953807e+14 7.6395577

    Rest columns contain abundances of elements and isotopes

    Parameters
    ----------

    fname: str
        filename or path with filename


    Returns
    -------

    time_of_model: ~astropy.units.Quantity
        time at which the model is valid

    velocity: ~np.ndarray
    mean_density: ~np.ndarray
    electron_densities: ~np.ndarray
    temperature: ~np.ndarray

    """
    warnings.warn("The current CMFGEN model parser is deprecated",
                  DeprecationWarning)

    df = pd.read_csv(fname, comment='#', delimiter='\s+', skiprows=[0, 2])

    with open(fname) as fh:
        for row_index, line in enumerate(fh):
            if row_index == 0:
                time_of_model_string = line.strip().replace('t0:', '')
                time_of_model = parse_quantity(time_of_model_string)
            elif row_index == 2:
                quantities = line.split()

    velocity = u.Quantity(df['velocity'].values, quantities[1]).to('cm/s')
    temperature = u.Quantity(df['temperature'].values, quantities[2])[1:]
    mean_density = u.Quantity(df['densities'].values, quantities[3])[1:]
    electron_densities = u.Quantity(
        df['electron_densities'].values, quantities[4])[1:]

    return time_of_model, velocity, mean_density, electron_densities, temperature
Beispiel #4
0
def test_parse_quantity():
    q1 = parse_quantity('5 km/s')
    assert q1.value == 5.
    assert q1.unit == u.Unit('km/s')

    with pytest.raises(MalformedQuantityError):
        parse_quantity(5)

    with pytest.raises(MalformedQuantityError):
        parse_quantity('abcd')

    with pytest.raises(MalformedQuantityError):
        parse_quantity('a abcd')

    with pytest.raises(MalformedQuantityError):
        parse_quantity('5 abcd')
Beispiel #5
0
def test_parse_quantity():
    q1 = parse_quantity('5 km/s')
    assert q1.value == 5.
    assert q1.unit == u.Unit('km/s')

    with pytest.raises(MalformedQuantityError):
        parse_quantity(5)

    with pytest.raises(MalformedQuantityError):
        parse_quantity('abcd')

    with pytest.raises(MalformedQuantityError):
        parse_quantity('a abcd')

    with pytest.raises(MalformedQuantityError):
        parse_quantity('5 abcd')
Beispiel #6
0
def test_parse_quantity():
    q1 = parse_quantity("5 km/s")
    assert q1.value == 5.0
    assert q1.unit == u.Unit("km/s")

    with pytest.raises(MalformedQuantityError):
        parse_quantity(5)

    with pytest.raises(MalformedQuantityError):
        parse_quantity("abcd")

    with pytest.raises(MalformedQuantityError):
        parse_quantity("a abcd")

    with pytest.raises(MalformedQuantityError):
        parse_quantity("5 abcd")
Beispiel #7
0
def read_simple_ascii_density(fname):
    """
    Reading a density file of the following structure (example; lines starting with a hash will be ignored):
    The first density describes the mean density in the center of the model and is not used.
    5 s
    #index velocity [km/s] density [g/cm^3]
    0 1.1e4 1.6e8
    1 1.2e4 1.7e8

    Parameters
    ----------

    fname: str
        filename or path with filename


    Returns
    -------

    time_of_model: ~astropy.units.Quantity
        time at which the model is valid

    data: ~pandas.DataFrame
        data frame containing index, velocity (in km/s) and density
    """

    with open(fname) as fh:
        time_of_model_string = fh.readline().strip()
        time_of_model = parse_quantity(time_of_model_string)

    data = recfromtxt(
        fname,
        skip_header=1,
        names=("index", "velocity", "density"),
        dtype=(int, float, float),
    )
    velocity = (data["velocity"] * u.km / u.s).to("cm/s")
    mean_density = (data["density"] * u.Unit("g/cm^3"))[1:]

    return time_of_model, velocity, mean_density
Beispiel #8
0
def read_simple_ascii_density(fname):
    """
    Reading a density file of the following structure (example; lines starting with a hash will be ignored):
    The first density describes the mean density in the center of the model and is not used.
    5 s
    #index velocity [km/s] density [g/cm^3]
    0 1.1e4 1.6e8
    1 1.2e4 1.7e8

    Parameters
    ----------

    fname: str
        filename or path with filename


    Returns
    -------

    time_of_model: ~astropy.units.Quantity
        time at which the model is valid

    data: ~pandas.DataFrame
        data frame containing index, velocity (in km/s) and density
    """

    with open(fname) as fh:
        time_of_model_string = fh.readline().strip()
        time_of_model = parse_quantity(time_of_model_string)

    data = recfromtxt(fname, skip_header=1,
                      names=('index', 'velocity', 'density'),
                      dtype=(int, float, float))
    velocity = (data['velocity'] * u.km / u.s).to('cm/s')
    mean_density = (data['density'] * u.Unit('g/cm^3'))[1:]

    return time_of_model, velocity, mean_density
Beispiel #9
0
def read_blondin_toymodel(fname):
    """
    Reading the Blondin toy-model format and returns a dictionary and a
    dataframe

    Parameters
    ----------
    fname: str
        path or filename to blondin toymodel

    Returns
    -------

    blondin_dict: dict
        dictionary containing most of the meta data of the model

    blondin_csv: pandas.DataFrame
        DataFrame containing the csv part of the toymodel
    """
    with open(fname, "r") as fh:
        for line in fh:
            if line.startswith("#idx"):
                break
        else:
            raise ValueError(
                "File {0} does not conform to Toy Model format as it does "
                "not contain #idx")
    columns = [
        PATTERN_REMOVE_BRACKET.sub("", item) for item in line[1:].split()
    ]

    raw_blondin_csv = pd.read_csv(fname,
                                  delim_whitespace=True,
                                  comment="#",
                                  header=None,
                                  names=columns)
    raw_blondin_csv.set_index("idx", inplace=True)

    blondin_csv = raw_blondin_csv.loc[:, [
        "vel",
        "dens",
        "temp",
        "X_56Ni0",
        "X_Ti",
        "X_Ca",
        "X_S",
        "X_Si",
        "X_O",
        "X_C",
    ], ]
    rename_col_dict = {
        "vel": "velocity",
        "dens": "density",
        "temp": "t_electron",
    }
    rename_col_dict.update(
        {item: item[2:]
         for item in blondin_csv.columns[3:]})
    rename_col_dict["X_56Ni0"] = "Ni56"
    blondin_csv.rename(columns=rename_col_dict, inplace=True)
    blondin_csv.iloc[:, 3:] = blondin_csv.iloc[:, 3:].divide(
        blondin_csv.iloc[:, 3:].sum(axis=1), axis=0)

    # changing velocities to outer boundary
    new_velocities = 0.5 * (blondin_csv.velocity.iloc[:-1].values +
                            blondin_csv.velocity.iloc[1:].values)
    new_velocities = np.hstack(
        (new_velocities, [2 * new_velocities[-1] - new_velocities[-2]]))
    blondin_csv["velocity"] = new_velocities

    with open(fname, "r") as fh:
        t0_string = T0_PATTERN.findall(fh.read())[0]

    t0 = parse_quantity(t0_string.replace("DAYS", "day"))
    blondin_dict = {}
    blondin_dict["model_density_time_0"] = str(t0)
    blondin_dict["description"] = "Converted {0} to csvy format".format(fname)
    blondin_dict["tardis_model_config_version"] = "v1.0"
    blondin_dict_fields = [
        dict(
            name="velocity",
            unit="km/s",
            desc="velocities of shell outer bounderies.",
        )
    ]
    blondin_dict_fields.append(
        dict(name="density", unit="g/cm^3", desc="mean density of shell."))
    blondin_dict_fields.append(
        dict(name="t_electron", unit="K", desc="electron temperature."))

    for abund in blondin_csv.columns[3:]:
        blondin_dict_fields.append(
            dict(name=abund, desc="Fraction {0} abundance".format(abund)))
    blondin_dict["datatype"] = {"fields": blondin_dict_fields}

    return blondin_dict, blondin_csv
Beispiel #10
0
def read_blondin_toymodel(fname):
    """
    Reading the Blondin toy-model format and returns a dictionary and a
    dataframe

    Parameters
    ----------
    fname: str
        path or filename to blondin toymodel

    Returns
    -------

    blondin_dict: dict
        dictionary containing most of the meta data of the model

    blondin_csv: pandas.DataFrame
        DataFrame containing the csv part of the toymodel

    """
    with open(fname, 'r') as fh:
        for line in fh:
            if line.startswith("#idx"):
                break
        else:
            raise ValueError(
                'File {0} does not conform to Toy Model format as it does '
                'not contain #idx')
    columns = [
        PATTERN_REMOVE_BRACKET.sub('', item) for item in line[1:].split()
    ]

    raw_blondin_csv = pd.read_csv(fname,
                                  delim_whitespace=True,
                                  comment='#',
                                  header=None,
                                  names=columns)
    raw_blondin_csv.set_index('idx', inplace=True)

    blondin_csv = raw_blondin_csv.loc[:, [
        'vel', 'dens', 'temp', 'X_56Ni0', 'X_Ti', 'X_Ca', 'X_S', 'X_Si', 'X_O',
        'X_C'
    ]]
    rename_col_dict = {
        'vel': 'velocity',
        'dens': 'density',
        'temp': 't_electron'
    }
    rename_col_dict.update(
        {item: item[2:]
         for item in blondin_csv.columns[3:]})
    rename_col_dict['X_56Ni0'] = 'Ni56'
    blondin_csv.rename(columns=rename_col_dict, inplace=True)
    blondin_csv.iloc[:, 3:] = blondin_csv.iloc[:, 3:].divide(
        blondin_csv.iloc[:, 3:].sum(axis=1), axis=0)

    # changing velocities to outer boundary
    new_velocities = 0.5 * (blondin_csv.velocity.iloc[:-1].values +
                            blondin_csv.velocity.iloc[1:].values)
    new_velocities = np.hstack(
        (new_velocities, [2 * new_velocities[-1] - new_velocities[-2]]))
    blondin_csv['velocity'] = new_velocities

    with open(fname, 'r') as fh:
        t0_string = T0_PATTERN.findall(fh.read())[0]

    t0 = parse_quantity(t0_string.replace('DAYS', 'day'))
    blondin_dict = {}
    blondin_dict['model_density_time_0'] = str(t0)
    blondin_dict['description'] = 'Converted {0} to csvy format'.format(fname)
    blondin_dict['tardis_model_config_version'] = 'v1.0'
    blondin_dict_fields = [
        dict(name='velocity',
             unit='km/s',
             desc='velocities of shell outer bounderies.')
    ]
    blondin_dict_fields.append(
        dict(name='density', unit='g/cm^3', desc='mean density of shell.'))
    blondin_dict_fields.append(
        dict(name='t_electron', unit='K', desc='electron temperature.'))

    for abund in blondin_csv.columns[3:]:
        blondin_dict_fields.append(
            dict(name=abund, desc='Fraction {0} abundance'.format(abund)))
    blondin_dict['datatype'] = {'fields': blondin_dict_fields}

    return blondin_dict, blondin_csv