Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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, (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)
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
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
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
"""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, savedir='/tmp', name=None, mode='L',
resize=None, ptsize=None, save=False):
"""Add synoptical reports from stations to provided geolocated image array
"""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, savedir='/tmp', name=None, mode='L',
resize=None, ptsize=None, save=False):
"""Add synoptical reports from stations to provided geolocated image array
"""
# Create array image
arrshp = arr.shape[:2]
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,
def total_precipitable_water(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']))
cm = Colormap(*palette['colors'])
img.palettize(cm)
area=None):
"""Plotting function for additional filter attributes.
.. Note:: Masks should be correctly setup to be plotted:
**True** (1) mask values are not shown, **False** (0) mask
values are displayed.
"""
raise NotImplementedError(
"plotting is disabled until fogpy supports dask, "
"see https://github.com/gerritholl/fogpy/issues/6")
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)))
# Get save directory
attrdir = os.path.join(dir, self.name + '_' + name + '_' +
datetime.strftime(self.time,
'%Y%m%d%H%M') + '.' + type)
logger.info("Plotting filter attribute {} to {}".format(name, attrdir))
# Generate image
attr = getattr(self, name)
if attr.dtype == 'bool':
attr = np.ma.masked_where(attr == 1, attr)
attr_img = Image(attr.squeeze(), mode='L', fill_value=None)
attr_img.colorize(fogcol)
# Get background image
bg_img = Image(self.bg_img.squeeze(), mode='L', fill_value=None)
bg_img.stretch("crude")
bg_img.convert("RGB")
bg_img.invert()
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))
sqpalette = np.asanyarray(palette).squeeze() / 255.0
tups = [(val, tuple(tup))
for (val, tup) in zip(palette_indices, sqpalette)]
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, sqpalette
def _merge_colormaps(kwargs):
"""Merge colormaps listed in kwargs."""
from trollimage.colormap import Colormap
full_cmap = None
palette = kwargs['palettes']
if isinstance(palette, Colormap):
full_cmap = palette
else:
for itm in palette:
cmap = create_colormap(itm)
cmap.set_range(itm["min_value"], itm["max_value"])
if full_cmap is None:
full_cmap = cmap
else:
full_cmap = full_cmap + cmap
return full_cmap