Exemple #1
0
def create_colormap(palette):
    """Create colormap of the given numpy file, color vector or colormap."""
    from trollimage.colormap import Colormap
    fname = palette.get('filename', None)
    if fname:
        data = np.load(fname)
        cmap = []
        num = 1.0 * data.shape[0]
        for i in range(int(num)):
            cmap.append((i / num, (data[i, 0] / 255., data[i, 1] / 255.,
                                   data[i, 2] / 255.)))
        return Colormap(*cmap)

    colors = palette.get('colors', None)
    if isinstance(colors, list):
        cmap = []
        values = palette.get('values', None)
        for idx, color in enumerate(colors):
            if values:
                value = values[idx]
            else:
                value = idx / float(len(colors) - 1)
            cmap.append((value, tuple(color)))
        return Colormap(*cmap)

    if isinstance(colors, str):
        from trollimage import colormap
        import copy
        return copy.copy(getattr(colormap, colors))

    return None
Exemple #2
0
    def build_colormap(palette, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap

        palette = np.asanyarray(palette).squeeze()
        tups = [(val, tuple(tup)) for (val, tup) in enumerate(palette)]
        colormap = Colormap(*tups)

        sf = info.get('scale_factor', np.array(1))
        colormap.set_range(*(np.array(info['valid_range']) * sf +
                             info.get('add_offset', 0)))

        return colormap
Exemple #3
0
    def build_colormap(palette, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap

        palette = np.asanyarray(palette).squeeze()
        tups = [(val, tuple(tup))
                for (val, tup) in enumerate(palette)]
        colormap = Colormap(*tups)

        sf = info.get('scale_factor', np.array(1))
        colormap.set_range(
            *(np.array(info['valid_range']) * sf + info.get('add_offset', 0)))

        return colormap
Exemple #4
0
    def test_merge_colormaps(self, create_colormap):
        """Test merging colormaps."""
        from trollimage.colormap import Colormap
        from satpy.enhancements import _merge_colormaps as mcp
        ret_map = mock.MagicMock()
        create_colormap.return_value = ret_map

        cmap1 = Colormap((1, (1., 1., 1.)))
        kwargs = {'palettes': cmap1}
        res = mcp(kwargs)
        self.assertTrue(res is cmap1)
        create_colormap.assert_not_called()

        cmap1 = {'colors': 'foo', 'min_value': 0, 'max_value': 1}
        kwargs = {'palettes': [cmap1]}
        res = mcp(kwargs)
        create_colormap.assert_called_once()
        ret_map.reverse.assert_not_called()
        ret_map.set_range.assert_called_with(0, 1)

        cmap2 = {
            'colors': 'bar',
            'min_value': 2,
            'max_value': 3,
            'reverse': True
        }
        kwargs = {'palettes': [cmap2]}
        res = mcp(kwargs)
        ret_map.reverse.assert_called_once()
        ret_map.set_range.assert_called_with(2, 3)

        kwargs = {'palettes': [cmap1, cmap2]}
        res = mcp(kwargs)
        ret_map.__add__.assert_called_once()
Exemple #5
0
def _create_colormap_from_file(filename, palette, color_scale):
    from trollimage.colormap import Colormap
    data = _read_colormap_data_from_file(filename)
    cols = data.shape[1]
    default_modes = {
        3: 'RGB',
        4: 'VRGB',
        5: 'VRGBA'
    }
    default_mode = default_modes.get(cols)
    mode = palette.setdefault('colormap_mode', default_mode)
    if mode is None or len(mode) != cols:
        raise ValueError(
            "Unexpected colormap shape for mode '{}'".format(mode))
    rows = data.shape[0]
    if mode[0] == 'V':
        colors = data[:, 1:]
        if color_scale != 1:
            colors = data[:, 1:] / float(color_scale)
        values = data[:, 0]
    else:
        colors = data
        if color_scale != 1:
            colors = colors / float(color_scale)
        values = np.arange(rows) / float(rows - 1)
    return Colormap(*zip(values, colors))
Exemple #6
0
def test_write_and_read_file_P(test_image_small_arctic_P, tmp_path):
    """Test writing and reading P image."""
    import rasterio
    from trollimage.colormap import Colormap

    from satpy.writers.ninjogeotiff import NinJoGeoTIFFWriter
    fn = os.fspath(tmp_path / "test.tif")
    ngtw = NinJoGeoTIFFWriter()
    ngtw.save_image(
        test_image_small_arctic_P,
        filename=fn,
        fill_value=255,
        PhysicUnit="N/A",
        PhysicValue="N/A",
        SatelliteNameID=6400014,
        ChannelID=900015,
        DataType="PPRN",
        DataSource="dowsing rod",
        keep_palette=True,
        cmap=Colormap(*enumerate(zip(*([np.linspace(0, 1, 256)] * 3)))))
    src = rasterio.open(fn)
    assert len(src.indexes) == 1  # mode P
    assert src.colorinterp[0] == rasterio.enums.ColorInterp.palette
    tgs = src.tags()
    assert tgs["ninjo_FileName"] == fn
    assert tgs["ninjo_DataSource"] == "dowsing rod"
Exemple #7
0
    def __call__(self, projectables, **info):
        if len(projectables) != 2:
            raise ValueError("Expected 2 datasets, got %d" %
                             (len(projectables), ))

        # TODO: support datasets with palette to delegate this to the image
        # writer.

        data, palette = projectables
        palette = palette / 255.0

        from trollimage.colormap import Colormap
        if data.dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)
        elif 'valid_range' in data.info:
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)
            colormap.set_range(*data.info['valid_range'])
        r, g, b = colormap.colorize(data)
        r[data.mask] = palette[-1][0]
        g[data.mask] = palette[-1][1]
        b[data.mask] = palette[-1][2]
        r = Dataset(r, copy=False, mask=data.mask, **data.info)
        g = Dataset(g, copy=False, mask=data.mask, **data.info)
        b = Dataset(b, copy=False, mask=data.mask, **data.info)

        return super(PaletteCompositor, self).__call__((r, g, b), **data.info)
Exemple #8
0
    def build_colormap(palette, info):
        """Create the colormap from the `raw_palette` and the valid_range."""
        from trollimage.colormap import Colormap
        if 'palette_meanings' in palette.attrs:
            palette_indices = palette.attrs['palette_meanings']
        else:
            palette_indices = range(len(palette))

        squeezed_palette = np.asanyarray(palette).squeeze() / 255.0
        tups = [(val, tuple(tup))
                for (val, tup) in zip(palette_indices, squeezed_palette)]
        colormap = Colormap(*tups)
        if 'palette_meanings' not in palette.attrs:
            sf = info.get('scale_factor', np.array(1))
            colormap.set_range(*(np.array(info['valid_range']) * sf +
                                 info.get('add_offset', 0)))

        return colormap, squeezed_palette
Exemple #9
0
def total_precipitable_water(img, **kwargs):
    """Palettizes images from MIMIC TPW 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']))

    cm = Colormap(*palette['colors'])
    img.palettize(cm)
Exemple #10
0
def create_colormap(fname):
    """Create colormap of the given numpy file."""

    from trollimage.colormap import Colormap

    data = np.load(fname)
    cmap = []
    num = 1.0 * data.shape[0]
    for i in range(int(num)):
        cmap.append((i / num, (data[i, 0] / 255., data[i, 1] / 255.,
                               data[i, 2] / 255.)))
    return Colormap(*cmap)
Exemple #11
0
    def build_colormap(palette, dtype, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap

        palette = np.asanyarray(palette).squeeze()
        if dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

        elif 'valid_range' in info:
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

            sf = info.get('scale_factor', np.array(1))
            colormap.set_range(
                *info['valid_range'] * sf + info.get('add_offset', 0))

        return colormap
Exemple #12
0
def _create_colormap_from_sequence(colors, palette, color_scale):
    from trollimage.colormap import Colormap
    cmap = []
    values = palette.get('values', None)
    for idx, color in enumerate(colors):
        if values is not None:
            value = values[idx]
        else:
            value = idx / float(len(colors) - 1)
        if color_scale != 1:
            color = tuple(elem / float(color_scale) for elem in color)
        cmap.append((value, tuple(color)))
    return Colormap(*cmap)
Exemple #13
0
    def test_merge_colormaps(self):
        """Test merging colormaps."""
        from trollimage.colormap import Colormap

        from satpy.enhancements import _merge_colormaps as mcp
        from satpy.enhancements import create_colormap
        ret_map = mock.MagicMock()

        create_colormap_mock = mock.Mock(wraps=create_colormap)
        cmap1 = Colormap((1, (1., 1., 1.)))
        kwargs = {'palettes': cmap1}

        with mock.patch('satpy.enhancements.create_colormap',
                        create_colormap_mock):
            res = mcp(kwargs)
        assert res is cmap1
        create_colormap_mock.assert_not_called()
        create_colormap_mock.reset_mock()
        ret_map.reset_mock()

        cmap1 = {'colors': 'blues', 'min_value': 0, 'max_value': 1}
        kwargs = {'palettes': [cmap1]}
        with mock.patch('satpy.enhancements.create_colormap', create_colormap_mock),\
                mock.patch('trollimage.colormap.blues', ret_map):
            _ = mcp(kwargs)
        create_colormap_mock.assert_called_once()
        ret_map.reverse.assert_not_called()
        ret_map.set_range.assert_called_with(0, 1)
        create_colormap_mock.reset_mock()
        ret_map.reset_mock()

        cmap2 = {
            'colors': 'blues',
            'min_value': 2,
            'max_value': 3,
            'reverse': True
        }
        kwargs = {'palettes': [cmap2]}
        with mock.patch('trollimage.colormap.blues', ret_map):
            _ = mcp(kwargs)
        ret_map.reverse.assert_called_once()
        ret_map.set_range.assert_called_with(2, 3)
        create_colormap_mock.reset_mock()
        ret_map.reset_mock()

        kwargs = {'palettes': [cmap1, cmap2]}
        with mock.patch('trollimage.colormap.blues', ret_map):
            _ = mcp(kwargs)
        ret_map.__add__.assert_called_once()
Exemple #14
0
def get_colormap(band_dtype, band_ct, band_count):
    from trollimage.colormap import Colormap

    max_val = np.iinfo(band_dtype).max
    # if we have an alpha band then include the entire colormap
    # otherwise assume it is using 0 as a fill value
    start_idx = 1 if band_count == 1 else 0
    if band_ct is None:
        # NOTE: the comma is needed to make this a tuple
        color_iter = ((idx / float(max_val), (int(idx / float(max_val)),) * 3 + (1.0,)) for idx in range(max_val))
    else:
        color_iter = ((idx / float(max_val), color) for idx, color in sorted(band_ct.items())[start_idx:])
        color_iter = ((idx, tuple(x / float(max_val) for x in color)) for idx, color in color_iter)
    cmap = Colormap(*color_iter)
    return cmap
def get_colormap(band, band_count):
    from trollimage.colormap import Colormap
    ct = band.GetRasterColorTable()
    data = band.ReadAsArray()
    max_val = np.iinfo(data.dtype).max
    # if we have an alpha band then include the entire colormap
    # otherwise assume it is using 0 as a fill value
    start_idx = 1 if band_count == 1 else 0
    if ct is None:
        # NOTE: the comma is needed to make this a tuple
        color_iter = (
            (idx / float(max_val), (int(idx / float(max_val)),) * 3 + (1.0,)) for idx in range(max_val))
    else:
        color_iter = ((idx / float(max_val), ct.GetColorEntry(idx))
                      for idx in range(start_idx, ct.GetCount()))
        color_iter = ((idx, tuple(x / float(max_val) for x in color)) for idx, color in color_iter)
    cmap = Colormap(*color_iter)
    return cmap
Exemple #16
0
def water_detection(img, **kwargs):
    """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)
Exemple #17
0
def load_color_table_file_to_colormap(ct_file):
    """Load a text colormap file and create a trollimage.Colormap object.

    If the provided pathname includes ``$POLAR2GRID_HOME`` it will be replaced
    with the current environment variable value of ``POLAR2GRID_HOME``.

    """
    from trollimage.colormap import Colormap
    p2g_home = os.getenv('POLAR2GRID_HOME', '')
    ct_file = ct_file.replace("$POLAR2GRID_HOME", p2g_home)
    ct_file = ct_file.replace("$GEO2GRID_HOME", p2g_home)
    if ct_file.endswith('.npy') or ct_file.endswith('.npz'):
        # binary colormap files (RGB, RGBA, VRGB, VRGBA)
        from satpy.enhancements import create_colormap
        cmap = create_colormap({'filename': ct_file})
    else:
        # P2G style colormap text files
        ct = parse_color_table_file(ct_file)
        entries = ((e[0] / 255., [x / 255. for x in e[1:]]) for e in ct)
        cmap = Colormap(*entries)
    return cmap
Exemple #18
0
def _make_cmap(colors, position=None, bit=False):
    '''
    _make_cmap takes a list of tuples which contain RGB values. The RGB
    values may either be in 8-bit [0 to 255] (in which bit must be set to
    True when called) or arithmetic [0 to 1] (default). _make_cmap returns
    a cmap with equally spaced colors.
    Arrange your tuples so that the first color is the lowest value for the
    colorbar and the last is the highest.
    position contains values from 0 to 1 to dictate the location of each color.
    '''
    bit_rgb = np.linspace(0, 1, 256)
    if position == None:
        position = np.linspace(0, 1, len(colors))
    else:
        if len(position) != len(colors):
            sys.exit("position length must be the same as colors")
        elif position[0] != 0 or position[-1] != 1:
            sys.exit("position must start with 0 and end with 1")
    palette = [(i, (float(r), float(g), float(b), float(a)))
               for i, (r, g, b, a) in enumerate(colors)]
    cmap = Colormap(*palette)
    return cmap
Exemple #19
0
    def build_colormap(palette, dtype, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap
        if dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

        elif 'valid_range' in info:
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

            sf = info['scale_factor']
            colormap.set_range(*info['valid_range'] * sf + info['add_offset'])

        return colormap
Exemple #20
0
    def build_colormap(palette, dtype, info):
        """Create the colormap from the `raw_palette` and the valid_range.

        Colormaps come in different forms, but they are all supposed to have
        color values between 0 and 255. The following cases are considered:
        - Palettes comprised of only a list on colors. If *dtype* is uint8,
          the values of the colormap are the enumaration of the colors.
          Otherwise, the colormap values will be spread evenly from the min
          to the max of the valid_range provided in `info`.
        - Palettes that have a palette_meanings attribute. The palette meanings
          will be used as values of the colormap.

        """
        from trollimage.colormap import Colormap
        squeezed_palette = np.asanyarray(palette).squeeze() / 255.0
        set_range = True
        if hasattr(palette, 'attrs') and 'palette_meanings' in palette.attrs:
            set_range = False
            meanings = palette.attrs['palette_meanings']
            iterator = zip(meanings, squeezed_palette)
        else:
            iterator = enumerate(squeezed_palette[:-1])

        if dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in iterator]
            colormap = Colormap(*tups)

        elif 'valid_range' in info:
            tups = [(val, tuple(tup))
                    for (val, tup) in iterator]
            colormap = Colormap(*tups)

            if set_range:
                sf = info.get('scale_factor', np.array(1))
                colormap.set_range(
                    *(np.array(info['valid_range']) * sf
                      + info.get('add_offset', 0)))
        else:
            raise AttributeError("Data needs to have either a valid_range or be of type uint8" +
                                 " in order to be displayable with an attached color-palette!")

        return colormap, squeezed_palette
Exemple #21
0
""" This module implements satellite image based fog and low stratus
detection and forecasting algorithm as a PyTROLL custom composite object.
"""

import logging

from algorithms import DayFogLowStratusAlgorithm
from algorithms import NightFogLowStratusAlgorithm
from mpop.imageo.geo_image import GeoImage
from trollimage.colormap import Colormap

logger = logging.getLogger(__name__)

# Define custom fog colormap
fogcol = Colormap((1., (0.0, 0.0, 0.0)),
                  (0., (250 / 255.0, 200 / 255.0, 40 / 255.0)))


def fls_day(self, elevation, cot, reff, lwp=None, cth=None, validate=False,
            plot=False, plotdir='/tmp', single=False):
    """This method defines a composite for fog and low stratus detection
    and forecasting at daytime.

    The fog algorithm is optimized for the Meteosat Second Generation
    - SERVIRI instrument.

    Args:
        | elevation (:obj:`ndarray`): Ditital elevation model as array.
        | cot (:obj:`ndarray`): Cloud optical thickness(depth) as array.
        | reff (:obj:`ndarray`): Cloud particle effective radius as array.
        | lwp (:obj:`ndarray`): Liquid water path as array.
from trollimage.colormap import Colormap
 
spectral = Colormap(
 #(170.0,(0.0, 0.0, 0.0)),
 (0,(0.0, 0.0, 0.0)),
 (250,(0.36862745, 0.30980392, 0.63529412)),
 (255,(0.21176471, 0.51301248, 0.7315508 )),
 (160,(0.36292335, 0.71942959, 0.66417112)),
 (265,(0.59679144, 0.83778966, 0.64420677)),
 (270,(0.81782531, 0.92655971, 0.61319073)),
 (275,(0.95543672, 0.98217469, 0.67950089)),
 (280,(0.99821747, 0.94474153, 0.65632799)),
 (285,(0.99465241, 0.80713012, 0.48520499)),
 (290,(0.98253119, 0.61283422, 0.3483066)), 
 (295,(0.93475936, 0.39393939, 0.27130125)),
 (300,(0.81568627, 0.22139037, 0.30516934)),
 (305,(0.61960784, 0.00392157, 0.25882353)),)
    
scene_map = Colormap((1, (0.00392156862745098, 0.24705882352941178, 0.011764705882352941)),
(2, (0.00392156862745098, 0.24705882352941178, 0.011764705882352941)),
(3, (0.1568627450980392, 0.403921568627451, 0.10196078431372549)),
(4, (0.1568627450980392, 0.403921568627451, 0.10196078431372549)),
(5, (0.1568627450980392, 0.403921568627451, 0.10196078431372549)),
(6, (0.8352941176470589, 0.7686274509803922, 0.34509803921568627)),
(7, (0.8980392156862745, 0.8470588235294118, 0.5215686274509804)),
(8, (0.4235294117647059, 0.4470588235294118, 0.09019607843137255)),
(9, (0.7372549019607844, 0.6901960784313725, 0.17647058823529413)),
(10, (0.8352941176470589, 0.7686274509803922, 0.34509803921568627)),
(11, (0.00784313725490196, 0.0196078431372549, 0.27450980392156865)),
(12, (0.7372549019607844, 0.6901960784313725, 0.17647058823529413)),
(13, (0.27450980392156865, 0.27450980392156865, 0.27450980392156865)),
Exemple #23
0
def nrl_colors(img, **kwargs):
    """TPW color table based on NRL Color table (0-76 mm)."""
    nrl_tpw_colors = {
        "colors": [
            [0.0, [188, 132, 98]],
            [0.27472527472527475, [188, 130, 99]],
            [0.5494505494505495, [187, 128, 100]],
            [0.8241758241758242, [186, 125, 101]],
            [1.098901098901099, [185, 124, 102]],
            [1.3736263736263736, [184, 122, 103]],
            [1.6483516483516485, [183, 120, 103]],
            [1.9230769230769234, [182, 119, 104]],
            [2.197802197802198, [182, 118, 106]],
            [2.4725274725274726, [181, 116, 107]],
            [2.7472527472527473, [180, 114, 108]],
            [3.0219780219780223, [179, 114, 108]],
            [3.296703296703297, [178, 113, 109]],
            [3.5714285714285716, [177, 111, 110]],
            [3.8461538461538467, [177, 110, 111]],
            [4.120879120879121, [176, 108, 111]],
            [4.395604395604396, [176, 106, 110]],
            [4.670329670329671, [175, 104, 110]],
            [4.945054945054945, [174, 103, 111]],
            [5.21978021978022, [174, 101, 111]],
            [5.4945054945054945, [173, 99, 111]],
            [5.76923076923077, [172, 97, 111]],
            [6.043956043956045, [171, 95, 112]],
            [6.318681318681319, [171, 93, 112]],
            [6.593406593406594, [171, 91, 113]],
            [6.868131868131869, [170, 90, 113]],
            [7.142857142857143, [169, 88, 114]],
            [7.417582417582418, [169, 86, 114]],
            [7.692307692307693, [168, 85, 115]],
            [7.967032967032968, [167, 83, 115]],
            [8.241758241758243, [166, 81, 116]],
            [8.516483516483516, [166, 80, 118]],
            [8.791208791208792, [165, 78, 119]],
            [9.065934065934067, [165, 76, 120]],
            [9.340659340659341, [164, 75, 120]],
            [9.615384615384617, [164, 74, 121]],
            [9.89010989010989, [163, 72, 123]],
            [10.164835164835166, [162, 70, 124]],
            [10.43956043956044, [161, 69, 125]],
            [10.714285714285715, [160, 67, 126]],
            [10.989010989010989, [160, 66, 128]],
            [11.263736263736265, [159, 64, 130]],
            [11.53846153846154, [159, 63, 131]],
            [11.813186813186814, [158, 61, 132]],
            [12.08791208791209, [158, 60, 134]],
            [12.362637362637363, [157, 58, 136]],
            [12.637362637362639, [156, 57, 137]],
            [12.912087912087912, [155, 56, 139]],
            [13.186813186813188, [155, 54, 141]],
            [13.461538461538463, [154, 52, 142]],
            [13.736263736263737, [154, 52, 144]],
            [14.010989010989013, [153, 50, 146]],
            [14.285714285714286, [153, 49, 148]],
            [14.560439560439562, [152, 47, 150]],
            [14.835164835164836, [150, 46, 151]],
            [15.109890109890111, [147, 45, 150]],
            [15.384615384615387, [144, 44, 150]],
            [15.65934065934066, [142, 44, 152]],
            [15.934065934065936, [138, 48, 156]],
            [16.20879120879121, [135, 50, 159]],
            [16.483516483516485, [132, 52, 161]],
            [16.75824175824176, [131, 56, 164]],
            [17.032967032967033, [126, 60, 168]],
            [17.30769230769231, [123, 62, 171]],
            [17.582417582417584, [121, 65, 173]],
            [17.857142857142858, [117, 69, 177]],
            [18.131868131868135, [114, 71, 180]],
            [18.40659340659341, [111, 74, 182]],
            [18.681318681318682, [109, 77, 185]],
            [18.956043956043956, [104, 82, 190]],
            [19.230769230769234, [101, 84, 193]],
            [19.505494505494507, [98, 86, 195]],
            [19.78021978021978, [96, 89, 198]],
            [20.05494505494506, [93, 92, 200]],
            [20.329670329670332, [90, 95, 204]],
            [20.604395604395606, [87, 98, 207]],
            [20.87912087912088, [83, 103, 211]],
            [21.153846153846157, [80, 105, 214]],
            [21.42857142857143, [77, 108, 216]],
            [21.703296703296704, [74, 110, 220]],
            [21.978021978021978, [71, 114, 222]],
            [22.252747252747255, [68, 116, 225]],
            [22.52747252747253, [65, 120, 228]],
            [22.802197802197803, [61, 125, 233]],
            [23.07692307692308, [57, 127, 235]],
            [23.351648351648354, [55, 130, 239]],
            [23.626373626373628, [52, 133, 242]],
            [23.9010989010989, [49, 137, 245]],
            [24.17582417582418, [47, 139, 247]],
            [24.450549450549453, [44, 142, 250]],
            [24.725274725274726, [40, 146, 255]],
            [25.000000000000004, [40, 148, 255]],
            [25.274725274725277, [42, 150, 255]],
            [25.54945054945055, [46, 154, 255]],
            [25.824175824175825, [50, 158, 255]],
            [26.098901098901102, [52, 159, 255]],
            [26.373626373626376, [55, 163, 255]],
            [26.64835164835165, [59, 167, 255]],
            [26.923076923076927, [61, 169, 255]],
            [27.1978021978022, [65, 173, 255]],
            [27.472527472527474, [70, 178, 255]],
            [27.747252747252748, [73, 182, 255]],
            [28.021978021978025, [76, 185, 255]],
            [28.2967032967033, [79, 188, 255]],
            [28.571428571428573, [82, 192, 255]],
            [28.84615384615385, [86, 195, 255]],
            [29.120879120879124, [88, 199, 255]],
            [29.395604395604398, [91, 201, 255]],
            [29.67032967032967, [95, 205, 255]],
            [29.94505494505495, [97, 207, 255]],
            [30.219780219780223, [101, 210, 255]],
            [30.494505494505496, [104, 213, 255]],
            [30.769230769230774, [107, 216, 255]],
            [31.043956043956047, [110, 218, 255]],
            [31.31868131868132, [114, 222, 255]],
            [31.593406593406595, [115, 223, 255]],
            [31.868131868131872, [119, 227, 255]],
            [32.142857142857146, [123, 231, 255]],
            [32.41758241758242, [125, 233, 255]],
            [32.69230769230769, [127, 236, 255]],
            [32.96703296703297, [133, 241, 255]],
            [33.24175824175825, [136, 244, 255]],
            [33.51648351648352, [139, 247, 255]],
            [33.791208791208796, [143, 252, 255]],
            [34.065934065934066, [145, 254, 255]],
            [34.34065934065934, [148, 255, 254]],
            [34.61538461538462, [148, 255, 247]],
            [34.89010989010989, [148, 255, 241]],
            [35.16483516483517, [148, 255, 235]],
            [35.439560439560445, [148, 255, 229]],
            [35.714285714285715, [148, 255, 223]],
            [35.98901098901099, [148, 255, 217]],
            [36.26373626373627, [148, 255, 210]],
            [36.53846153846154, [148, 255, 205]],
            [36.81318681318682, [148, 255, 199]],
            [37.08791208791209, [148, 255, 193]],
            [37.362637362637365, [148, 255, 187]],
            [37.63736263736264, [148, 255, 181]],
            [37.91208791208791, [148, 255, 174]],
            [38.18681318681319, [148, 255, 168]],
            [38.46153846153847, [148, 255, 162]],
            [38.73626373626374, [148, 255, 156]],
            [39.010989010989015, [148, 255, 150]],
            [39.28571428571429, [151, 255, 148]],
            [39.56043956043956, [157, 255, 148]],
            [39.83516483516484, [163, 255, 148]],
            [40.10989010989012, [169, 255, 148]],
            [40.38461538461539, [175, 255, 148]],
            [40.659340659340664, [181, 255, 148]],
            [40.934065934065934, [188, 255, 148]],
            [41.20879120879121, [197, 255, 148]],
            [41.48351648351649, [203, 255, 148]],
            [41.75824175824176, [209, 255, 148]],
            [42.032967032967036, [215, 255, 148]],
            [42.307692307692314, [221, 255, 148]],
            [42.582417582417584, [227, 255, 148]],
            [42.85714285714286, [233, 255, 148]],
            [43.13186813186814, [239, 255, 148]],
            [43.40659340659341, [244, 255, 148]],
            [43.681318681318686, [250, 255, 148]],
            [43.956043956043956, [254, 254, 146]],
            [44.23076923076923, [255, 251, 143]],
            [44.50549450549451, [255, 249, 141]],
            [44.78021978021978, [255, 247, 139]],
            [45.05494505494506, [255, 242, 134]],
            [45.329670329670336, [255, 239, 131]],
            [45.604395604395606, [255, 236, 128]],
            [45.87912087912088, [255, 233, 125]],
            [46.15384615384616, [255, 231, 122]],
            [46.42857142857143, [255, 227, 120]],
            [46.70329670329671, [255, 225, 117]],
            [46.978021978021985, [255, 221, 113]],
            [47.252747252747255, [255, 218, 110]],
            [47.52747252747253, [255, 216, 108]],
            [47.8021978021978, [255, 211, 103]],
            [48.07692307692308, [255, 209, 101]],
            [48.35164835164836, [255, 206, 98]],
            [48.62637362637363, [255, 204, 96]],
            [48.901098901098905, [255, 199, 91]],
            [49.17582417582418, [255, 196, 87]],
            [49.45054945054945, [255, 193, 85]],
            [49.72527472527473, [255, 191, 82]],
            [50.00000000000001, [255, 188, 80]],
            [50.27472527472528, [255, 185, 77]],
            [50.549450549450555, [255, 182, 74]],
            [50.82417582417583, [255, 179, 70]],
            [51.0989010989011, [255, 176, 68]],
            [51.37362637362638, [255, 173, 64]],
            [51.64835164835165, [255, 171, 61]],
            [51.92307692307693, [255, 167, 58]],
            [52.197802197802204, [255, 164, 55]],
            [52.472527472527474, [255, 161, 52]],
            [52.74725274725275, [255, 158, 49]],
            [53.02197802197803, [255, 154, 46]],
            [53.2967032967033, [255, 151, 42]],
            [53.57142857142858, [255, 148, 40]],
            [53.846153846153854, [252, 144, 39]],
            [54.120879120879124, [249, 140, 39]],
            [54.3956043956044, [246, 136, 39]],
            [54.67032967032967, [243, 132, 39]],
            [54.94505494505495, [240, 128, 39]],
            [55.219780219780226, [237, 125, 39]],
            [55.494505494505496, [234, 121, 39]],
            [55.769230769230774, [231, 118, 39]],
            [56.04395604395605, [227, 114, 39]],
            [56.31868131868132, [225, 111, 39]],
            [56.5934065934066, [222, 108, 39]],
            [56.868131868131876, [219, 104, 39]],
            [57.142857142857146, [216, 101, 39]],
            [57.41758241758242, [213, 97, 39]],
            [57.6923076923077, [210, 95, 39]],
            [57.96703296703297, [206, 91, 39]],
            [58.24175824175825, [204, 89, 39]],
            [58.51648351648352, [200, 86, 39]],
            [58.791208791208796, [198, 83, 39]],
            [59.06593406593407, [194, 80, 39]],
            [59.34065934065934, [192, 78, 39]],
            [59.61538461538462, [188, 75, 39]],
            [59.8901098901099, [185, 73, 39]],
            [60.16483516483517, [182, 70, 39]],
            [60.439560439560445, [179, 68, 39]],
            [60.71428571428572, [176, 66, 39]],
            [60.98901098901099, [173, 63, 39]],
            [61.26373626373627, [171, 62, 39]],
            [61.53846153846155, [169, 59, 39]],
            [61.81318681318682, [167, 57, 40]],
            [62.087912087912095, [165, 56, 40]],
            [62.362637362637365, [165, 54, 40]],
            [62.63736263736264, [163, 52, 40]],
            [62.91208791208792, [161, 50, 41]],
            [63.18681318681319, [159, 48, 42]],
            [63.46153846153847, [159, 47, 42]],
            [63.736263736263744, [157, 46, 43]],
            [64.01098901098902, [155, 44, 43]],
            [64.28571428571429, [154, 44, 45]],
            [64.56043956043956, [156, 45, 48]],
            [64.83516483516485, [157, 46, 52]],
            [65.10989010989012, [159, 48, 55]],
            [65.38461538461539, [160, 50, 58]],
            [65.65934065934067, [162, 52, 62]],
            [65.93406593406594, [164, 53, 65]],
            [66.20879120879121, [165, 55, 69]],
            [66.4835164835165, [167, 57, 72]],
            [66.75824175824177, [169, 59, 76]],
            [67.03296703296704, [171, 61, 80]],
            [67.3076923076923, [172, 63, 83]],
            [67.58241758241759, [174, 65, 87]],
            [67.85714285714286, [176, 67, 91]],
            [68.13186813186813, [177, 69, 95]],
            [68.40659340659342, [179, 71, 98]],
            [68.68131868131869, [181, 73, 102]],
            [68.95604395604396, [182, 75, 106]],
            [69.23076923076924, [184, 78, 109]],
            [69.50549450549451, [186, 80, 114]],
            [69.78021978021978, [188, 82, 117]],
            [70.05494505494507, [189, 85, 121]],
            [70.32967032967034, [191, 87, 125]],
            [70.6043956043956, [193, 90, 129]],
            [70.87912087912089, [194, 92, 132]],
            [71.15384615384616, [196, 95, 137]],
            [71.42857142857143, [198, 97, 140]],
            [71.70329670329672, [199, 100, 144]],
            [71.97802197802199, [201, 103, 148]],
            [72.25274725274726, [203, 105, 152]],
            [72.52747252747254, [205, 108, 155]],
            [72.80219780219781, [206, 110, 159]],
            [73.07692307692308, [208, 114, 163]],
            [73.35164835164836, [210, 116, 167]],
            [73.62637362637363, [211, 120, 171]],
            [73.9010989010989, [213, 122, 174]],
            [74.17582417582418, [215, 125, 178]],
            [74.45054945054946, [216, 128, 182]],
            [74.72527472527473, [218, 131, 185]],
            [75.0, [220, 135, 189]],
        ]
    }
    kwargs['palettes'].update(nrl_tpw_colors)
    palette = kwargs['palettes']
    palette['colors'] = tuple(map(tuple, palette['colors']))

    cm = Colormap(*palette['colors'])
    img.palettize(cm)
Exemple #24
0
cot = inputs[8]
reff = inputs[9]
cwp = inputs[10]
lat = inputs[11]
lon = inputs[12]

msg_con_quick = image.ImageContainerQuick(ir108.squeeze(), area_def)
area_con_quick = msg_con_quick.resample(euro_areadef)
result_data_quick = area_con_quick.image_data

# Create satpy scene
testscene = Scene(platform_name="msg",
                  sensor="seviri",
                  start_time=datetime(2013, 11, 12, 8, 30),
                  end_time=datetime(2013, 11, 12, 8, 45),
                  area=area_def)
array_kwargs = {'area': area_def}

testscene['ir108'] = Dataset(ir108.squeeze(), **array_kwargs)
print(testscene['ir108'])
testscene.show(
        'ir108',
        overlay={'coast_dir': '/home/mastho/data/', 'color': 'gray'})
resampscene = testscene.resample('germ')
print(resampscene.shape)

# Define custom fog colormap
fogcol = Colormap((0., (250 / 255.0, 200 / 255.0, 40 / 255.0)),
                  (1., (1.0, 1.0, 229 / 255.0)))
maskcol = (250 / 255.0, 200 / 255.0, 40 / 255.0)
Exemple #25
0
def add_to_image(image,
                 area,
                 time,
                 bufr,
                 savedir='/tmp',
                 name=None,
                 bgimg=None,
                 resize=None,
                 ptsize=None,
                 save=False):
    """Add synoptical visibility reports from station data to provided
    geolocated image array
    """
    arrshp = image.shape[:2]
    # Add optional background image
    if bgimg is not None:
        # Get background image
        bg_img = Image(bgimg.squeeze(), mode='L', fill_value=None)
        bg_img.stretch("crude")
        bg_img.convert("RGB")
        #         bg_img.invert()
        image.merge(bg_img)
    # Import bufr
    stations = read_synop(bufr, 'visibility')
    currentstations = stations[time.strftime("%Y%m%d%H0000")]
    lats = [i[2] for i in currentstations]
    lons = [i[3] for i in currentstations]
    vis = [i[4] for i in currentstations]

    # Create array for synop parameter
    visarr = np.empty(arrshp)
    visarr.fill(np.nan)
    # Red - Violet - Blue - Green
    vis_colset = Colormap((0, (228 / 255.0, 26 / 255.0, 28 / 255.0)),
                          (1000, (152 / 255.0, 78 / 255.0, 163 / 255.0)),
                          (5000, (55 / 255.0, 126 / 255.0, 184 / 255.0)),
                          (10000, (77 / 255.0, 175 / 255.0, 74 / 255.0)))
    x, y = (area.get_xy_from_lonlat(lons, lats))
    vis_ma = np.ma.array(vis, mask=x.mask)
    if ptsize:
        xpt = np.array([])
        ypt = np.array([])
        for i, j in zip(x, y):
            xmesh, ymesh = np.meshgrid(
                np.linspace(i - ptsize, i + ptsize, ptsize * 2 + 1),
                np.linspace(j - ptsize, j + ptsize, ptsize * 2 + 1))
            xpt = np.append(xpt, xmesh.ravel())
            ypt = np.append(ypt, ymesh.ravel())
        vispt = np.ma.array(
            [np.full(((ptsize * 2 + 1, ptsize * 2 + 1)), p) for p in vis_ma])
        visarr[ypt.astype(int), xpt.astype(int)] = vispt.ravel()
    else:
        visarr[y.compressed(), x.compressed()] = vis_ma.compressed()
    visarr_ma = np.ma.masked_invalid(visarr)
    station_img = Image(visarr_ma, mode='L')
    station_img.colorize(vis_colset)
    image.convert("RGB")
    station_img.merge(image)
    if resize is not None:
        station_img.resize((arrshp[0] * resize, arrshp[1] * resize))
    if name is None:
        timestr = time.strftime("%Y%m%d%H%M")
        name = "fog_filter_example_stations_{}.png".format(timestr)
    if save:
        savepath = os.path.join(savedir, name)
        station_img.save(savepath)

    return (station_img)
Exemple #26
0
def create_colormap(palette):
    """Create colormap of the given numpy file, color vector, or colormap.

    Args:
        palette (dict): Information describing how to create a colormap
            object. See below for more details.

    **From a file**

    Colormaps can be loaded from ``.npy`` files as 2D raw arrays with rows for
    each color. The filename to load can be provided with the ``filename`` key
    in the provided palette information. The colormap is interpreted as 1 of 4
    different "colormap modes": ``RGB``, ``RGBA``, ``VRGB``, or ``VRGBA``. The
    colormap mode can be forced with the ``colormap_mode`` key in the provided
    palette information. If it is not provided then a default will be chosen
    based on the number of columns in the array (3: RGB, 4: VRGB, 5: VRGBA).

    The "V" in the possible colormap modes represents the control value of
    where that color should be applied. If "V" is not provided in the colormap
    data it defaults to the row index in the colormap array (0, 1, 2, ...)
    divided by the total number of colors to produce a number between 0 and 1.
    See the "Set Range" section below for more information.
    The remaining elements in the colormap array represent the Red (R),
    Green (G), and Blue (B) color to be mapped to.

    See the "Color Scale" section below for more information on the value
    range of provided numbers.

    **From a list**

    Colormaps can be loaded from lists of colors provided by the ``colors``
    key in the provided dictionary. Each element in the list represents a
    single color to be mapped to and can be 3 (RGB) or 4 (RGBA) elements long.
    By default the value or control point for a color is determined by the
    index in the list (0, 1, 2, ...) divided by the total number of colors
    to produce a number between 0 and 1. This can be overridden by providing a
    ``values`` key in the provided dictionary. See the "Set Range" section
    below for more information.

    See the "Color Scale" section below for more information on the value
    range of provided numbers.

    **From a builtin colormap**

    Colormaps can be loaded by name from the builtin colormaps in the
    ``trollimage``` package. Specify the name with the ``colors``
    key in the provided dictionary (ex. ``{'colors': 'blues'}``).
    See :doc:`trollimage:colormap` for the full list of available colormaps.

    **Color Scale**

    By default colors are expected to be in a 0-255 range. This
    can be overridden by specifying ``color_scale`` in the provided colormap
    information. A common alternative to 255 is ``1`` to specify floating
    point numbers between 0 and 1. The resulting Colormap uses the normalized
    color values (0-1).

    **Set Range**

    By default the control points or values of the Colormap are between 0 and
    1. This means that data values being mapped to a color must also be
    between 0 and 1. When this is not the case, the expected input range of
    the data can be used to configure the Colormap and change the control point
    values. To do this specify the input data range with ``min_value`` and
    ``max_value``. See :meth:`trollimage.colormap.Colormap.set_range` for more
    information.

    """
    from trollimage.colormap import Colormap
    fname = palette.get('filename', None)
    colors = palette.get('colors', None)
    # are colors between 0-255 or 0-1
    color_scale = palette.get('color_scale', 255)
    if fname:
        data = np.load(fname)
        cols = data.shape[1]
        default_modes = {3: 'RGB', 4: 'VRGB', 5: 'VRGBA'}
        default_mode = default_modes.get(cols)
        mode = palette.setdefault('colormap_mode', default_mode)
        if mode is None or len(mode) != cols:
            raise ValueError(
                "Unexpected colormap shape for mode '{}'".format(mode))

        rows = data.shape[0]
        if mode[0] == 'V':
            colors = data[:, 1:]
            if color_scale != 1:
                colors = data[:, 1:] / float(color_scale)
            values = data[:, 0]
        else:
            colors = data
            if color_scale != 1:
                colors = colors / float(color_scale)
            values = np.arange(rows) / float(rows - 1)
        cmap = Colormap(*zip(values, colors))
    elif isinstance(colors, (tuple, list)):
        cmap = []
        values = palette.get('values', None)
        for idx, color in enumerate(colors):
            if values is not None:
                value = values[idx]
            else:
                value = idx / float(len(colors) - 1)
            if color_scale != 1:
                color = tuple(elem / float(color_scale) for elem in color)
            cmap.append((value, tuple(color)))
        cmap = Colormap(*cmap)
    elif isinstance(colors, str):
        from trollimage import colormap
        import copy
        cmap = copy.copy(getattr(colormap, colors))
    else:
        raise ValueError("Unknown colormap format: {}".format(palette))

    if palette.get("reverse", False):
        cmap.reverse()
    if 'min_value' in palette and 'max_value' in palette:
        cmap.set_range(palette["min_value"], palette["max_value"])
    elif 'min_value' in palette or 'max_value' in palette:
        raise ValueError("Both 'min_value' and 'max_value' must be specified")

    return cmap
Exemple #27
0
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""This script adds synope data for visibility to given geolocated image"""

import fogpy
import numpy as np
import os
from .import_synop import read_synop
from datetime import datetime
from trollimage.image import Image
from trollimage.colormap import Colormap

# Define custom fog colormap
fogcol = Colormap((0., (250 / 255.0, 200 / 255.0, 40 / 255.0)),
                  (1., (1.0, 1.0, 229 / 255.0)))

maskcol = Colormap((1., (250 / 255.0, 200 / 255.0, 40 / 255.0)))

viscol = Colormap((0., (1.0, 0.0, 0.0)), (5000, (0.7, 0.7, 0.7)))
# Red - Violet - Blue - Green
vis_colset = Colormap((0, (228 / 255.0, 26 / 255.0, 28 / 255.0)),
                      (1000, (152 / 255.0, 78 / 255.0, 163 / 255.0)),
                      (5000, (55 / 255.0, 126 / 255.0, 184 / 255.0)),
                      (10000, (77 / 255.0, 175 / 255.0, 74 / 255.0)))


def add_to_array(arr,
                 area,
                 time,
                 bufr,