Skip to content

earthlib.read

Functions for reading specifically formatted data, mostly spectral libraries.

check_file(path)

Verifies whether a file exists and can be read.

Parameters:

Name Type Description Default
path str

the file path to check.

required

Returns:

Type Description
bool

file status.

Source code in earthlib/read.py
105
106
107
108
109
110
111
112
113
114
def check_file(path: str) -> bool:
    """Verifies whether a file exists and can be read.

    Args:
        path: the file path to check.

    Returns:
        file status.
    """
    return os.path.isfile(path) and os.access(path, os.R_OK)

find_envi_header(path)

Generates the file paths for an ENVI spectral library and its header file.

Source code in earthlib/read.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def find_envi_header(path: str) -> tuple[str, str]:
    """Generates the file paths for an ENVI spectral library and its header file."""
    base, ext = os.path.splitext(path)

    if ext == ".hdr":
        hdr = path
    else:
        if check_file(base + ".hdr"):
            hdr = base + ".hdr"
        else:
            if check_file(path + ".hdr"):
                hdr = path + ".hdr"
            else:
                raise FileNotFoundError(f"No header file found for {path}")

    return hdr

jfsp(path)

Reads JFSP-formatted ASCII files.

Reads the ASCII format spectral data from the Joint Fire Science Program and returns an object with the mean and ± standard deviation reflectance.

www.frames.gov/assessing-burn-severity/spectral-library/overview

Parameters:

Name Type Description Default
path str

file path to the JFSP spectra text file.

required

Returns:

Type Description
Spectra

an earthlib Spectra with the JFSP reflectance data.

Source code in earthlib/read.py
 69
 70
 71
 72
 73
 74
 75
 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
102
def jfsp(path: str) -> Spectra:
    """Reads JFSP-formatted ASCII files.

    Reads the ASCII format spectral data from the Joint Fire Science Program and returns an object with the mean and +/- standard deviation reflectance.

    https://www.frames.gov/assessing-burn-severity/spectral-library/overview

    Args:
        path: file path to the JFSP spectra text file.

    Returns:
        an earthlib Spectra with the JFSP reflectance data.
    """

    # create the spectral object
    s = Spectra(data=None, sensor=ASD)
    print(s.data)
    s.spectra_stdevm = np.zeros(s.data.shape)
    s.spectra_stdevp = np.zeros(s.data.shape)

    print(s.data.shape)
    print(ASD.band_count)
    # print(s.sensor)

    # open the file and read the data
    with open(path, "r") as f:
        f.readline()
        for i, line in enumerate(f):
            line = line.strip().split()
            s.data[0, i] = line[1]
            s.spectra_stdevp[0, i] = line[2]
            s.spectra_stdevm[0, i] = line[3]

        return s

spectral_library(path, sensor=None, metadata=None)

Reads an ENVI-format spectral library into memory.

Parameters:

Name Type Description Default
path str

path to the spectral library file. Searches for a .hdr sidecar file.

required
sensor Sensor | None

an earthlib.sensors.Sensor object specifying sensor information not included in the .hdr file.

None

Returns:

Type Description
Spectra

endmembers from the spectral library

Source code in earthlib/read.py
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
def spectral_library(
    path: str,
    sensor: Sensor | None = None,
    metadata: pd.DataFrame | None = None,
) -> Spectra:
    """Reads an ENVI-format spectral library into memory.

    Args:
        path: path to the spectral library file.
            Searches for a .hdr sidecar file.
        sensor: an earthlib.sensors.Sensor object specifying
            sensor information not included in the .hdr file.

    Returns:
        endmembers from the spectral library
    """
    # get the header file path
    hdr = find_envi_header(path)

    sli = envi.open(hdr)

    if sensor is None:
        sensor = Sensor(
            name=os.path.basename(path),
            band_centers=sli.bands.centers,
            wavelength_unit=sli.bands.band_unit,
        )

    endmembers = Spectra(
        data=sli.spectra,
        sensor=sensor,
        names=sli.names,
        metadata=metadata,
    )

    return endmembers