Skip to content

nirwals.physics.utils

Utility functions.

read_from_file(file, unit=u.dimensionless_unscaled)

Read wavelengths and corresponding values from a file.

The file must be in Numpy's .npz format, and it must contain arrays called "x" and "y". The x array is used for the wavelengths, which are supposed to be given in Angstrom. The values in the y array are supposed to be given in the unit specified by the unit parameter. If no unit is given, the values are assumed to be dimensionless.

The wavelengths in the x array are supposed to be sorted in ascending order, but this is not checked.

If the minimum wavelength defined in the file is greater than 1 A (or, more precisely, greater than 1.02 A), it is assumed that below the minimum wavelength in the file the values drop to 0 within 0.01 A, and the wavelength and value array are extended accordingly.

Similarly, if the maximum wavelength defined in the file is less than 50000 A, it is assumed that above the maximum wavelength the file the values drop to 0 within 0.01 A, and the wavelength and value array are extended accordingly.

These extensions ensure that the data returned by this function can safely be used across the whole wavelength range used by NIRWALS, even if the file data cover only part of that range.

Use the numpyfy.py script for converting csv files into data files that can be used with this function.

Parameters:

Name Type Description Default
file BinaryIO

The data file, in .npz format.

required
unit Unit

The unit to use for the values in the file's y array.

dimensionless_unscaled

Returns:

Type Description
tuple of Quantity

The wavelengths and corresponding values.

Source code in nirwals/physics/utils.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def read_from_file(
    file: BinaryIO, unit: Unit = u.dimensionless_unscaled
) -> Tuple[Quantity, Quantity]:
    """
    Read wavelengths and corresponding values from a file.

    The file must be in Numpy's .npz format, and it must contain arrays called "x" and
    "y". The x array is used for the wavelengths, which are supposed to be given in
    Angstrom. The values in the y array are supposed to be given in the unit specified
    by the unit parameter. If no unit is given, the values are assumed to be
    dimensionless.

    The wavelengths in the x array are supposed to be sorted in ascending order, but
    this is not checked.

    If the minimum wavelength defined in the file is greater than 1 A (or, more
    precisely, greater than 1.02 A), it is assumed that below the minimum wavelength in
    the file the values drop to 0 within 0.01 A, and the wavelength and value array are
    extended accordingly.

    Similarly, if the maximum wavelength defined in the file is less than 50000 A, it is
    assumed that above the maximum wavelength the file the values drop to 0 within
    0.01 A, and the wavelength and value array are extended accordingly.

    These extensions ensure that the data returned by this function can safely be used
    across the whole wavelength range used by NIRWALS, even if the file data cover only
    part of that range.

    Use the numpyfy.py script for converting csv files into data files that can be used
    with this function.

    Parameters
    ----------
    file: BinaryIO
        The data file, in .npz format.
    unit: Unit, optional
        The unit to use for the values in the file's y array.

    Returns
    -------
    tuple of Quantity
        The wavelengths and corresponding values.
    """

    # Read in the data from the file
    npzfile = np.load(file)

    # Extract the wavelengths and values from the table returned by the ASCII reader.
    wavelengths = npzfile["x"]
    values = npzfile["y"]

    # Assume the value is 0 for wavelengths not covered by the data file.
    if wavelengths[0] > 1.02:
        wavelengths = np.concatenate(
            (np.array([1, wavelengths[0] - 0.01]), wavelengths)
        )
        values = np.concatenate((np.array([0, 0]), values))
    if wavelengths[-1] < 50000:
        wavelengths = np.concatenate(
            (wavelengths, np.array([wavelengths[-1] + 0.01, 50000]))
        )
        values = np.concatenate((values, np.array([0, 0])))

    return wavelengths * u.AA, values * unit

shift(a, k)

Shift an array by k places.

A negative shift is to the left, a positive shift is to the right. "Gaps" resulting from the shift are filled with zeroes.

Parameters:

Name Type Description Default
a ndarray

Array to shift.

required
k int

Size of the shift.

required

Returns:

Type Description
ndarray

The shifted array.

Source code in nirwals/physics/utils.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def shift(a: np.ndarray, k: int) -> np.ndarray:
    """
    Shift an array by k places.

    A negative shift is to the left, a positive shift is to the right. "Gaps" resulting
    from the shift are filled with zeroes.

    Parameters
    ----------
    a: np.ndarray
        Array to shift.
    k: int
        Size of the shift.

    Returns
    -------
    np.ndarray
        The shifted array.
    """
    shifted_a = np.roll(a, k)
    if k < 0:
        shifted_a[k:] = 0
    if k > 0:
        shifted_a[:k] = 0

    return shifted_a