Skip to content

Commit

Permalink
ninjotiff.save now supports palette ('P') mode
Browse files Browse the repository at this point in the history
  • Loading branch information
loerum committed Jun 17, 2016
1 parent 6756fe7 commit f269e48
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
27 changes: 19 additions & 8 deletions mpop/imageo/formats/ninjotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ def _finalize(geo_image, dtype=np.uint8, value_range_measurement_unit=None, data
channels[3].filled(fill_value[3])))
return data, 1.0, 0.0, fill_value[0]

elif geo_image.mode == 'P':
fill_value = 0
data = geo_image.channels[0]
if isinstance(data, np.ma.core.MaskedArray):
data = data.filled(fill_value)
data = data.astype(dtype)
log.debug("Value range: %.2f, %.2f, %.2f" % (data.min(), data.mean(), data.max()))
return data, 1.0, 0.0, fill_value

else:
raise ValueError("Don't known how til handle image mode '%s'" %
str(geo_image.mode))
Expand All @@ -408,14 +417,14 @@ def save(geo_image, filename, ninjo_product_name=None, **kwargs):
* 8 bits grayscale with a colormap (if specified, inverted for IR channels).
* 16 bits grayscale with no colormap (if specified, MinIsWhite is set for IR).
* min value will be reserved for transparent color.
* RGB images will use mpop.imageo.image's standard finalize.
* If possible mpop.imageo.image's standard finalize will be used.
"""

dtype = np.uint8 # @UndefinedVariable
dtype = np.uint8
if 'nbits' in kwargs:
nbits = int(kwargs['nbits'])
if nbits == 16:
dtype = np.uint16 # @UndefinedVariable
dtype = np.uint16

try:
value_range_measurement_unit = (float(kwargs["ch_min_measurement_unit"]),
Expand All @@ -427,15 +436,17 @@ def save(geo_image, filename, ninjo_product_name=None, **kwargs):

data, scale, offset, fill_value = _finalize(geo_image, dtype=dtype, data_is_scaled_01=data_is_scaled_01,
value_range_measurement_unit=value_range_measurement_unit,)

area_def = geo_image.area
time_slot = geo_image.time_slot

# Some Ninjo tiff names
kwargs['image_dt'] = time_slot
kwargs['transparent_pix'] = fill_value
kwargs['gradient'] = scale
kwargs['axis_intercept'] = offset
kwargs['transparent_pix'] = fill_value
kwargs['image_dt'] = time_slot
kwargs['is_calibrated'] = True

write(data, filename, area_def, ninjo_product_name, **kwargs)


Expand Down Expand Up @@ -467,15 +478,15 @@ def write(image_data, output_fn, area_def, product_name=None, **kwargs):
if len(image_data.shape) == 3:
if image_data.shape[2] == 4:
shape = (area_def.y_size, area_def.x_size, 4)
log.info("Will generate RGBA product '%s'" % product_name)
log.info("Will generate RGBA product")
else:
shape = (area_def.y_size, area_def.x_size, 3)
log.info("Will generate RGB product '%s'" % product_name)
log.info("Will generate RGB product")
write_rgb = True
else:
shape = (area_def.y_size, area_def.x_size)
write_rgb = False
log.info("Will generate product '%s'" % product_name)
log.info("Will generate single band product")

if image_data.shape != shape:
raise ValueError("Raster shape %s does not correspond to expected shape %s" % (
Expand Down
20 changes: 11 additions & 9 deletions mpop/imageo/formats/ninjotiff_example
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Saving an image for 'chn' will then go like:
image.save(filename,
fformat='mpop.imageo.formats.ninjotiff',
physic_unit=physic_unit,
value_range=value_range,
ch_min_measurement_unit=min_value,
ch_max_measurement_unit=max_value,
**ninjotiff_config[chn])
"""
import sys
Expand Down Expand Up @@ -80,7 +81,6 @@ CHANNEL_DICT = {
}

BITS_PER_SAMPLE = 8

DO_CONVECTION = False

for area_name, area_in, area_out in AREAS:
Expand Down Expand Up @@ -108,17 +108,20 @@ for area_name, area_in, area_out in AREAS:
physic_unit = scene[chn].unit = 'C'

# Value range as DWD
value_range = None
if physic_unit in ('C', 'CELSIUS'):
# IR
value_range = [-88.5, 40.]
value_range = (-88.5, 40.)
else:
# VIS
value_range = [0., 125.]
value_range = (0., 125.)


# GeoImage without any data scaling or enhancement.
img = scene.image(chn, mode="L")
#
# A GeoImage specifying a color range.
#
# If no color_range specified, MPOP will not scaled the data into the [0., 1.] range.
# In that case set the data_is_scaled_01 option to False in img.save
#
img = scene.image(chn, mode="L", crange=[value_range])
LOG.info("%s (%s, %s) %.2f %.2f %.2f" % (chn_name, physic_unit,
img.channels[0].dtype,
img.channels[0].min(),
Expand All @@ -140,7 +143,6 @@ for area_name, area_in, area_out in AREAS:
ninjo_product_name=chn_name,
ch_min_measurement_unit=value_range[0],
ch_max_measurement_unit=value_range[1],
data_is_scaled_01=False, # Default is True
nbits=BITS_PER_SAMPLE)

# Cleanup.
Expand Down

0 comments on commit f269e48

Please sign in to comment.