Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enhancement for VIIRS flood reader #532

Merged
merged 10 commits into from
May 9, 2019
25 changes: 25 additions & 0 deletions satpy/enhancements/viirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from trollimage.colormap import Colormap
import numpy as np
from satpy.enhancements import apply_enhancement


def water_detection(img, **kwargs):
djhoese marked this conversation as resolved.
Show resolved Hide resolved
"""Palettizes images from VIIRS flood data.

This modifies the image's data so the correct colors
can be applied to it, and then palettizes the image.
"""
palette = kwargs['palettes']
palette['colors'] = tuple(map(tuple, palette['colors']))

def func(img_data):
data = np.asarray(img_data)
data[data == 150] = 31
data[data == 199] = 18
data[data >= 200] = data[data >= 200] - 100

return data

apply_enhancement(img.data, func, pass_dask=True)
cm = Colormap(*palette['colors'])
img.palettize(cm)
71 changes: 71 additions & 0 deletions satpy/etc/enhancements/viirs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
enhancements:
water_detection:
name: WaterDetection
operations:
- name: WaterDetection
method: !!python/name:satpy.enhancements.viirs.water_detection
kwargs:
palettes: {colors:
[[14, [0.0, 0.0, 0.0]],
[15, [0.0, 0.0, 0.39215686274509803]],
[16, [0.7686274509803922, 0.6352941176470588, 0.4470588235294118]],
[17, [0.7686274509803922, 0.6352941176470588, 0.4470588235294118]],
[18, [0.0, 0.0, 1.0]],
[20, [1.0, 1.0, 1.0]],
[27, [0.0, 1.0, 1.0]],
[30, [0.7843137254901961, 0.7843137254901961, 0.7843137254901961]],
[31, [0.39215686274509803, 0.39215686274509803, 0.39215686274509803]],
[88, [0.7058823529411765, 0.0, 0.9019607843137255]],
[100, [0.19607843137254902, 1.0, 0.39215686274509803]],
[120, [0.19607843137254902, 1.0, 0.39215686274509803]],
[121, [0.0, 1.0, 0.0]],
[130, [0.0, 1.0, 0.0]],
[131, [0.7843137254901961, 1.0, 0.0]],
[140, [0.7843137254901961, 1.0, 0.0]],
[141, [1.0, 1.0, 0.5882352941176471]],
[150, [1.0, 1.0, 0.5882352941176471]],
[151, [1.0, 1.0, 0.0]],
[160, [1.0, 1.0, 0.0]],
[161, [1.0, 0.7843137254901961, 0.0]],
[170, [1.0, 0.7843137254901961, 0.0]],
[171, [1.0, 0.5882352941176471, 0.19607843137254902]],
[180, [1.0, 0.5882352941176471, 0.19607843137254902]],
[181, [1.0, 0.39215686274509803, 0.0]],
[190, [1.0, 0.39215686274509803, 0.0]],
[191, [1.0, 0.0, 0.0]],
[200, [1.0, 0.0, 0.0]],
[201, [0.0, 0.0, 0.0]]],
min_value: 0,
max_value: 201}
# palettes: {colors:
# [[14, [0.0, 0.0, 0.0, 0.0]],
# [15, [0.0, 0.0, 0.39215686274509803, 1.0]],
# [16, [0.7686274509803922, 0.6352941176470588, 0.4470588235294118, 1.0]],
# [17, [0.7686274509803922, 0.6352941176470588, 0.4470588235294118, 1.0]],
# [18, [0.0, 0.0, 1.0, 1.0]],
# [20, [1.0, 1.0, 1.0, 1.0]],
# [27, [0.0, 1.0, 1.0, 1.0]],
# [30, [0.7843137254901961, 0.7843137254901961, 0.7843137254901961, 1.0]],
# [31, [0.39215686274509803, 0.39215686274509803, 0.39215686274509803, 1.0]],
# [88, [0.7058823529411765, 0.0, 0.9019607843137255, 1.0]],
# [100, [0.19607843137254902, 1.0, 0.39215686274509803, 1.0]],
# [120, [0.19607843137254902, 1.0, 0.39215686274509803, 1.0]],
# [121, [0.0, 1.0, 0.0, 1.0]],
# [130, [0.0, 1.0, 0.0, 1.0]],
# [131, [0.7843137254901961, 1.0, 0.0, 1.0]],
# [140, [0.7843137254901961, 1.0, 0.0, 1.0]],
# [141, [1.0, 1.0, 0.5882352941176471, 1.0]],
# [150, [1.0, 1.0, 0.5882352941176471, 1.0]],
# [151, [1.0, 1.0, 0.0, 1.0]],
# [160, [1.0, 1.0, 0.0, 1.0]],
# [161, [1.0, 0.7843137254901961, 0.0, 1.0]],
# [170, [1.0, 0.7843137254901961, 0.0, 1.0]],
# [171, [1.0, 0.5882352941176471, 0.19607843137254902, 1.0]],
# [180, [1.0, 0.5882352941176471, 0.19607843137254902, 1.0]],
# [181, [1.0, 0.39215686274509803, 0.0, 1.0]],
# [190, [1.0, 0.39215686274509803, 0.0, 1.0]],
# [191, [1.0, 0.0, 0.0, 1.0]],
# [200, [1.0, 0.0, 0.0, 1.0]],
# [201, [0.0, 0.0, 0.0, 0.0]]],
# min_value: 0,
# max_value: 201}
1 change: 0 additions & 1 deletion satpy/etc/readers/viirs_edr_flood.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ datasets:
water_detection:
name: 'WaterDetection'
file_type: viirs_edr
sensor: viirs
8 changes: 4 additions & 4 deletions satpy/readers/viirs_edr_flood.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def end_time(self):
def sensor_name(self):
sensor = self['/attr/SensorIdentifyCode']
if isinstance(sensor, np.ndarray):
return str(sensor.astype(str))
return sensor
return str(sensor.astype(str)).lower()
return sensor.lower()

@property
def platform_name(self):
platform_name = self['/attr/Satellitename']
if isinstance(platform_name, np.ndarray):
return str(platform_name.astype(str))
return platform_name
return str(platform_name.astype(str)).lower()
return platform_name.lower()

def get_metadata(self, data, ds_info):
metadata = {}
Expand Down
4 changes: 2 additions & 2 deletions satpy/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
test_readers, test_resample, test_demo,
test_scene, test_utils, test_writers,
test_yaml_reader, writer_tests,
test_enhancements, compositor_tests, test_multiscene,
enhancement_tests, compositor_tests, test_multiscene,
test_crefl_utils, test_config)


Expand All @@ -56,7 +56,7 @@ def suite():
mysuite.addTests(writer_tests.suite())
mysuite.addTests(test_file_handlers.suite())
mysuite.addTests(test_utils.suite())
mysuite.addTests(test_enhancements.suite())
mysuite.addTests(enhancement_tests.suite())
mysuite.addTests(compositor_tests.suite())
mysuite.addTests(test_multiscene.suite())
mysuite.addTests(test_crefl_utils.suite())
Expand Down
20 changes: 20 additions & 0 deletions satpy/tests/enhancement_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""The enhancements tests package.
"""

import sys

from satpy.tests.enhancement_tests import test_enhancements, test_viirs

if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest


def suite():
"""Test suite for all enhancement tests"""
mysuite = unittest.TestSuite()
mysuite.addTests(test_enhancements.suite())
mysuite.addTests(test_viirs.suite())

return mysuite
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


class TestEnhancementStretch(unittest.TestCase):
"""Class for testing enhancements in satpy.enhancements."""
"""Class for testing enhancements in satpy.enhancements"""

def setUp(self):
"""Setup the test"""
Expand Down Expand Up @@ -96,14 +96,14 @@ def test_colorize(self):
4.35952940e-06, 4.35952940e-06],
[4.35952940e-06, 4.35952940e-06, 4.35952940e-06,
4.35952940e-06, 4.35952940e-06]],
[[np.nan, 1.88249866e-01, 1.88249866e-01,
2.35302110e-01, 2.35302110e-01],
[2.35302110e-01, 2.35302110e-01, 2.35302110e-01,
2.35302110e-01, 2.35302110e-01]],
[[np.nan, 1.96102817e-02, 1.96102817e-02,
1.88238767e-01, 1.88238767e-01],
[1.88238767e-01, 1.88238767e-01, 1.88238767e-01,
1.88238767e-01, 1.88238767e-01]]])
[[np.nan, 1.88249866e-01, 1.88249866e-01,
2.35302110e-01, 2.35302110e-01],
[2.35302110e-01, 2.35302110e-01, 2.35302110e-01,
2.35302110e-01, 2.35302110e-01]],
[[np.nan, 1.96102817e-02, 1.96102817e-02,
1.88238767e-01, 1.88238767e-01],
[1.88238767e-01, 1.88238767e-01, 1.88238767e-01,
1.88238767e-01, 1.88238767e-01]]])
self._test_enhancement(colorize, self.ch1, expected, palettes=brbg)

def test_palettize(self):
Expand Down
83 changes: 83 additions & 0 deletions satpy/tests/enhancement_tests/test_viirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Unit testing for the VIIRS enhancement function
"""

import unittest
import numpy as np
import xarray as xr
import dask.array as da


class TestVIIRSEnhancement(unittest.TestCase):
"""Class for testing the VIIRS enhancement function in satpy.enhancements.viirs"""

def setUp(self):
"""Setup the test"""
data = np.arange(15, 301, 15).reshape(2, 10)
self.da = xr.DataArray(data, dims=('y', 'x'), attrs={'test': 'test'})
self.palette = {'colors':
[[14, [0.0, 0.0, 0.0]],
[15, [0.0, 0.0, 0.39215]],
[16, [0.76862, 0.63529, 0.44705]],
[17, [0.76862, 0.63529, 0.44705]],
[18, [0.0, 0.0, 1.0]],
[20, [1.0, 1.0, 1.0]],
[27, [0.0, 1.0, 1.0]],
[30, [0.78431, 0.78431, 0.78431]],
[31, [0.39215, 0.39215, 0.39215]],
[88, [0.70588, 0.0, 0.90196]],
[100, [0.19607, 1.0, 0.39215]],
[120, [0.19607, 1.0, 0.39215]],
[121, [0.0, 1.0, 0.0]],
[130, [0.0, 1.0, 0.0]],
[131, [0.78431, 1.0, 0.0]],
[140, [0.78431, 1.0, 0.0]],
[141, [1.0, 1.0, 0.58823]],
[150, [1.0, 1.0, 0.58823]],
[151, [1.0, 1.0, 0.0]],
[160, [1.0, 1.0, 0.0]],
[161, [1.0, 0.78431, 0.0]],
[170, [1.0, 0.78431, 0.0]],
[171, [1.0, 0.58823, 0.19607]],
[180, [1.0, 0.58823, 0.19607]],
[181, [1.0, 0.39215, 0.0]],
[190, [1.0, 0.39215, 0.0]],
[191, [1.0, 0.0, 0.0]],
[200, [1.0, 0.0, 0.0]],
[201, [0.0, 0.0, 0.0]]],
'min_value': 0,
'max_value': 201}

def test_viirs(self):
from satpy.enhancements.viirs import water_detection
expected = [[[1, 7, 8, 8, 8, 9, 10, 11, 14, 8],
[20, 23, 26, 10, 12, 15, 18, 21, 24, 27]]]
self._test_enhancement(water_detection, self.da, expected,
palettes=self.palette)

def _test_enhancement(self, func, data, expected, **kwargs):
from trollimage.xrimage import XRImage

pre_attrs = data.attrs
img = XRImage(data)
func(img, **kwargs)

self.assertIsInstance(img.data.data, da.Array)
self.assertListEqual(sorted(pre_attrs.keys()),
sorted(img.data.attrs.keys()),
"DataArray attributes were not preserved")

np.testing.assert_allclose(img.data.values, expected, atol=1.e-6, rtol=0)

def tearDown(self):
"""Clean up"""
pass


def suite():
"""The test suite for test_viirs.
"""
loader = unittest.TestLoader()
mysuite = unittest.TestSuite()
mysuite.addTest(loader.loadTestsFromTestCase(TestVIIRSEnhancement))

return mysuite