Skip to content

Commit

Permalink
add LandSeaFalseColorCompositor
Browse files Browse the repository at this point in the history
  • Loading branch information
Trygve Aspenes committed Jan 17, 2024
1 parent bca22b4 commit adb4505
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions satpy/composites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,3 +1955,155 @@ def __call__(self, projectables, nonprojectables=None, **info):

masked_projectable = projectable.where(lon_min_max)
return super().__call__([masked_projectable], **info)

def rgb_land_sea():

# split is the split value between sea/land and clouds
split = 130
start = 320
max_size = 256

Check warning on line 1964 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L1962-L1964

Added lines #L1962 - L1964 were not covered by tests

sea_colour_red = np.zeros((max_size), dtype=np.uint8)
sea_colour_green = np.zeros((max_size), dtype=np.uint8)
sea_colour_blue = np.zeros((max_size), dtype=np.uint8)

Check warning on line 1968 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L1966-L1968

Added lines #L1966 - L1968 were not covered by tests

# Sea area, clouds
for i in range(0, split):
val = (start - i)
if (val > (max_size - 1)):
val = max_size - 1
sea_colour_red[i] = val
sea_colour_green[i] = val
sea_colour_blue[i] = val

Check warning on line 1977 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L1971-L1977

Added lines #L1971 - L1977 were not covered by tests
# print sea_colour_red[i], sea_colour_green[i],sea_colour_blue[i]

# Sea area, Sea
for i in range(0, (max_size - split)):
red = start - split - int(6.7 * float(i))
if (red < 0):
red = 0
green = start - split - int(3.5 * float(i))
if (green < 0):
green = 0
blue = start - split - int(1.5 * float(i))
if (blue < 0):
blue = 0

Check warning on line 1990 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L1981-L1990

Added lines #L1981 - L1990 were not covered by tests

sea_colour_red[i + split] = red
sea_colour_green[i + split] = green
sea_colour_blue[i + split] = blue

Check warning on line 1994 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L1992-L1994

Added lines #L1992 - L1994 were not covered by tests

land_colour_red = np.zeros((max_size), dtype=np.uint8)
land_colour_green = np.zeros((max_size), dtype=np.uint8)
land_colour_blue = np.zeros((max_size), dtype=np.uint8)

Check warning on line 1998 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L1996-L1998

Added lines #L1996 - L1998 were not covered by tests

# land area, cloud
for i in range(0, split):
val = (start - i)
if (val > (max_size - 1)):
val = max_size - 1
land_colour_red[i] = val
land_colour_green[i] = val
land_colour_blue[i] = val

Check warning on line 2007 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2001-L2007

Added lines #L2001 - L2007 were not covered by tests

# Palette for Land areas. LAND
for i in range(0, max_size - split):
red = start - split - int(2.0 * float(i))
if (red < 0):
red = 0
green = start - split - int(1.0 * float(i))
if (green < 0):
green = 0
blue = start - split - int(4.0 * float(i))
if (blue < 0):
blue = 0

Check warning on line 2019 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2010-L2019

Added lines #L2010 - L2019 were not covered by tests

land_colour_red[i + split] = red
land_colour_green[i + split] = green
land_colour_blue[i + split] = blue

Check warning on line 2023 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2021-L2023

Added lines #L2021 - L2023 were not covered by tests

return land_colour_red, land_colour_green, land_colour_blue, sea_colour_red, sea_colour_green, sea_colour_blue

Check warning on line 2025 in satpy/composites/__init__.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

rgb_land_sea has a cyclomatic complexity of 13, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 2025 in satpy/composites/__init__.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Bumpy Road Ahead

rgb_land_sea has 4 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is one single, nested block per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.

Check warning on line 2025 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2025

Added line #L2025 was not covered by tests

class LandSeaFalseColorCompositor(GenericCompositor):

def __init__(self, name,
ref_lat=45.,
**kwargs):
"""Init info.
Collect custom configuration values.
"""
print("Inside init")
self.ref_lat = ref_lat
super().__init__(name, **kwargs)

Check warning on line 2038 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2036-L2038

Added lines #L2036 - L2038 were not covered by tests

def __call__(self, projectables, **attrs):
if len(projectables) != 2:
raise ValueError(f"Expected 2 datasets, got {len(projectables)}")

Check warning on line 2042 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2041-L2042

Added lines #L2041 - L2042 were not covered by tests

projectables = self.match_data_arrays(projectables)
btd, lsm = projectables
lsm = lsm.squeeze(drop=True)
lsm = lsm.round() # Make sure to have whole numbers in case of smearing from resampling

Check warning on line 2047 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2044-L2047

Added lines #L2044 - L2047 were not covered by tests

(lr, lg, lb, sr, sg, sb) = rgb_land_sea()

Check warning on line 2049 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2049

Added line #L2049 was not covered by tests

# Correct data north/south of the latitude
# ref_lat = 45
_latitude = btd.attrs['area'].get_lonlats()[1]

Check warning on line 2053 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2053

Added line #L2053 was not covered by tests

# Filter due to various criteria
min_low_bt = 130
max_low_bt = 173
print("Start colourize ... ")

Check warning on line 2058 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2056-L2058

Added lines #L2056 - L2058 were not covered by tests
# Make low bt. For latitude larger that the ref_lat the low.bt will be lesser the further north or south.
# TODO: check if south also is covered here.

Check notice on line 2060 in satpy/composites/__init__.py

View check run for this annotation

codefactor.io / CodeFactor

satpy/composites/__init__.py#L2060

unresolved comment '# TODO: check if south also is covered here.' (C100)
low_bt = np.where(_latitude > self.ref_lat,

Check warning on line 2061 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2061

Added line #L2061 was not covered by tests
min_low_bt + ((80. - _latitude) / (80. - self.ref_lat) * (max_low_bt - min_low_bt)),
max_low_bt)
# Do the last calibration and stretch to 0..255 and filter data larger and less the given values
bt = (255. * ((btd - low_bt) / (343. - low_bt))).astype(int)
bt = np.where(bt > 255, 255, bt)
bt = np.where(bt < 0, 0, bt)

Check warning on line 2067 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2065-L2067

Added lines #L2065 - L2067 were not covered by tests

# Stack the various masks and colors
y_size, x_size = lr[bt].shape
land_rgb = da.vstack((lr[bt].reshape((1, y_size, x_size)),

Check warning on line 2071 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2070-L2071

Added lines #L2070 - L2071 were not covered by tests
lg[bt].reshape((1, y_size, x_size)),
lb[bt].reshape((1, y_size, x_size))))

sea_rgb = da.vstack((sr[bt].reshape((1, y_size, x_size)),

Check warning on line 2075 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2075

Added line #L2075 was not covered by tests
sg[bt].reshape((1, y_size, x_size)),
sb[bt].reshape((1, y_size, x_size))))

lsm__ = da.reshape(lsm.data, (1, y_size, x_size))
img_rgb = da.vstack((lsm__, lsm__, lsm__))

Check warning on line 2080 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2079-L2080

Added lines #L2079 - L2080 were not covered by tests

# Do the masking on data
rgb = np.where(img_rgb > 190, land_rgb, sea_rgb)
print("end colourize ... ")

Check warning on line 2084 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2083-L2084

Added lines #L2083 - L2084 were not covered by tests

new_attrs = combine_metadata(*projectables)

Check warning on line 2086 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2086

Added line #L2086 was not covered by tests
# remove metadata that shouldn't make sense in a composite
new_attrs["wavelength"] = None
new_attrs.pop("units", None)
new_attrs.pop("calibration", None)
new_attrs.pop("modifiers", None)

Check warning on line 2091 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2088-L2091

Added lines #L2088 - L2091 were not covered by tests

new_attrs.update({key: val

Check warning on line 2093 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2093

Added line #L2093 was not covered by tests
for (key, val) in attrs.items()
if val is not None})
resolution = new_attrs.get("resolution", None)
new_attrs.update(self.attrs)
if resolution is not None:
new_attrs["resolution"] = resolution
new_attrs["sensor"] = self._get_sensors(projectables)
new_attrs["mode"] = 'RGB'
new_attrs["start_time"] = btd.attrs['start_time']

Check warning on line 2102 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2096-L2102

Added lines #L2096 - L2102 were not covered by tests

res = xr.DataArray(data=rgb,

Check warning on line 2104 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2104

Added line #L2104 was not covered by tests
attrs=new_attrs,
dims=('bands', 'y', 'x'),
coords=btd.coords)
res = res.assign_coords(bands=['R', 'G', 'B'])
return res

Check warning on line 2109 in satpy/composites/__init__.py

View check run for this annotation

Codecov / codecov/patch

satpy/composites/__init__.py#L2108-L2109

Added lines #L2108 - L2109 were not covered by tests

0 comments on commit adb4505

Please sign in to comment.