Skip to content

Commit

Permalink
Fix issue with radiance conversion of L1 thermal
Browse files Browse the repository at this point in the history
The L1 thermal bands don't have conversions to surface temperature. This
meant that we couldn't find the right mult/add numbers for conversion
and the code broke when they were present in the archive. Convert them
to radiance instead and refactor the code that finds these parameters.
  • Loading branch information
leouieda committed Apr 9, 2024
1 parent 6b50db7 commit 9da93db
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions xlandsat/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
10: "thermal",
11: "thermal 2",
}
BAND_UNITS = {
BAND_UNITS_L2 = {
1: "reflectance",
2: "reflectance",
3: "reflectance",
Expand All @@ -52,6 +52,19 @@
10: "Kelvin",
11: "Kelvin",
}
BAND_UNITS_L1 = {
1: "reflectance",
2: "reflectance",
3: "reflectance",
4: "reflectance",
5: "reflectance",
6: "reflectance",
7: "reflectance",
8: "reflectance",
9: "reflectance",
10: "radiance",
11: "radiance",
}


def load_scene(path, bands=None, region=None, dtype="float16"):
Expand Down Expand Up @@ -265,14 +278,18 @@ def read_and_scale_band(fname, reader, dtype, number, coords, metadata, region):
mult, add = scaling_parameters(metadata, number)
band_data *= mult
band_data += add
if metadata["processing_level"] == "L1TP":
units = BAND_UNITS_L1[number]

Check warning on line 282 in xlandsat/_io.py

View check run for this annotation

Codecov / codecov/patch

xlandsat/_io.py#L282

Added line #L282 was not covered by tests
else:
units = BAND_UNITS_L2[number]
band = xr.DataArray(
data=band_data,
dims=("northing", "easting"),
name=BAND_NAMES[number],
coords=coords,
attrs={
"long_name": BAND_TITLES[number],
"units": BAND_UNITS[number],
"units": units,
"number": number,
"filename": pathlib.Path(fname).name,
"scaling_mult": mult,
Expand All @@ -287,13 +304,18 @@ def scaling_parameters(metadata, number):
Get the scaling parameters for the band of the given number.
"""
mult, add = None, None
mult_entries = [f"mult_band_{number}", f"mult_band_st_b{number}"]
add_entries = [f"add_band_{number}", f"add_band_st_b{number}"]
for key in metadata:
if any(key.endswith(entry) for entry in mult_entries):
mult = metadata[key]
if any(key.endswith(entry) for entry in add_entries):
add = metadata[key]
if number in set(range(1, 10)):
mult_entry = f"reflectance_mult_band_{number}"
add_entry = f"reflectance_add_band_{number}"
else:
if metadata["processing_level"] == "L1TP":
mult_entry = f"radiance_mult_band_{number}"
add_entry = f"radiance_add_band_{number}"

Check warning on line 313 in xlandsat/_io.py

View check run for this annotation

Codecov / codecov/patch

xlandsat/_io.py#L312-L313

Added lines #L312 - L313 were not covered by tests
else:
mult_entry = f"temperature_mult_band_st_b{number}"
add_entry = f"temperature_add_band_st_b{number}"

Check warning on line 316 in xlandsat/_io.py

View check run for this annotation

Codecov / codecov/patch

xlandsat/_io.py#L315-L316

Added lines #L315 - L316 were not covered by tests
mult = metadata[mult_entry]
add = metadata[add_entry]
return mult, add


Expand Down Expand Up @@ -401,6 +423,8 @@ def parse_metadata(text):
"CORNER_",
"REFLECTANCE_MULT_BAND_",
"REFLECTANCE_ADD_BAND_",
"RADIANCE_MULT_BAND_",
"RADIANCE_ADD_BAND_",
"TEMPERATURE_MULT_BAND_",
"TEMPERATURE_ADD_BAND_",
]
Expand Down

0 comments on commit 9da93db

Please sign in to comment.