Esempio n. 1
0
    def test_enhance_with_sensor_entry(self):
        """Test enhancing an image with a configuration section."""
        from satpy.writers import Enhancer, get_enhanced_image
        from xarray import DataArray
        import dask.array as da
        ds = DataArray(np.arange(1, 11.).reshape((2, 5)),
                       attrs=dict(name='test1', sensor='test_sensor', mode='L'),
                       dims=['y', 'x'])
        e = Enhancer()
        self.assertIsNotNone(e.enhancement_tree)
        img = get_enhanced_image(ds, enhancer=e)
        self.assertSetEqual(
            set(e.sensor_enhancement_configs),
            {self.ENH_FN, self.ENH_ENH_FN})
        np.testing.assert_almost_equal(img.data.isel(bands=0).max().values,
                                       1.)

        ds = DataArray(da.arange(1, 11., chunks=5).reshape((2, 5)),
                       attrs=dict(name='test1', sensor='test_sensor', mode='L'),
                       dims=['y', 'x'])
        e = Enhancer()
        self.assertIsNotNone(e.enhancement_tree)
        img = get_enhanced_image(ds, enhancer=e)
        self.assertSetEqual(set(e.sensor_enhancement_configs),
                            {self.ENH_FN, self.ENH_ENH_FN})
        np.testing.assert_almost_equal(img.data.isel(bands=0).max().values, 1.)
Esempio n. 2
0
 def test_enhance_with_sensor_no_entry(self):
     """Test enhancing an image that has no configuration sections."""
     from satpy.writers import Enhancer, get_enhanced_image
     from xarray import DataArray
     ds = DataArray(np.arange(1, 11.).reshape((2, 5)),
                    attrs=dict(sensor='test_sensor2', mode='L'),
                    dims=['y', 'x'])
     e = Enhancer()
     self.assertIsNotNone(e.enhancement_tree)
     get_enhanced_image(ds, enhancer=e)
     self.assertSetEqual(set(e.sensor_enhancement_configs),
                         {self.ENH_FN2, self.ENH_ENH_FN2})
Esempio n. 3
0
def enhance2dataset(dset):
    """Apply enhancements to dataset *dset* and return the resulting data
    array of the image."""
    attrs = dset.attrs
    img = get_enhanced_image(dset)
    # Clip image data to interval [0.0, 1.0] and replace nan values
    data = img.data.clip(0.0, 1.0).fillna(0.0)
    data.attrs = attrs

    return data
Esempio n. 4
0
 def test_enhance_with_sensor_entry2(self):
     """Test enhancing an image with a more detailed configuration section."""
     from satpy.writers import Enhancer, get_enhanced_image
     from xarray import DataArray
     ds = DataArray(np.arange(1, 11.).reshape((2, 5)),
                    attrs=dict(name='test1',
                               units='kelvin',
                               sensor='test_sensor',
                               mode='L'),
                    dims=['y', 'x'])
     e = Enhancer()
     self.assertIsNotNone(e.enhancement_tree)
     img = get_enhanced_image(ds, enhancer=e)
     self.assertSetEqual(set(e.sensor_enhancement_configs),
                         {self.ENH_FN, self.ENH_ENH_FN})
     np.testing.assert_almost_equal(
         img.data.isel(bands=0).max().values, 0.5)
Esempio n. 5
0
    def _get_single_frame(self, ds, enh_args, fill_value):
        """Get single frame from dataset.

        Yet a single image frame from a dataset.
        """
        enh_args = enh_args.copy()  # don't change caller's dict!
        if "decorate" in enh_args:
            enh_args["decorate"] = self._format_decoration(
                ds, enh_args["decorate"])
        img = get_enhanced_image(ds, **enh_args)
        data, mode = img.finalize(fill_value=fill_value)
        if data.ndim == 3:
            # assume all other shapes are (y, x)
            # we need arrays grouped by pixel so
            # transpose if needed
            data = data.transpose('y', 'x', 'bands')
        return data
Esempio n. 6
0
    def _get_animation_info(self, all_datasets, filename, fill_value=None):
        """Determine filename and shape of animation to be created."""
        valid_datasets = [ds for ds in all_datasets if ds is not None]
        first_dataset = valid_datasets[0]
        last_dataset = valid_datasets[-1]
        first_img = get_enhanced_image(first_dataset)
        first_img_data = first_img.finalize(fill_value=fill_value)[0]
        shape = tuple(first_img_data.sizes.get(dim_name)
                      for dim_name in ('y', 'x', 'bands'))
        if fill_value is None and filename.endswith('gif'):
            log.warning("Forcing fill value to '0' for GIF Luminance images")
            fill_value = 0
            shape = shape[:2]

        attrs = first_dataset.attrs.copy()
        if 'end_time' in last_dataset.attrs:
            attrs['end_time'] = last_dataset.attrs['end_time']
        this_fn = filename.format(**attrs)
        return this_fn, shape, fill_value
Esempio n. 7
0
 def _get_animation_frames(self, all_datasets, shape, fill_value=None,
                           ignore_missing=False):
     """Create enhanced image frames to save to a file."""
     for idx, ds in enumerate(all_datasets):
         if ds is None and ignore_missing:
             continue
         elif ds is None:
             log.debug("Missing frame: %d", idx)
             data = da.zeros(shape, dtype=np.uint8, chunks=shape)
             data = xr.DataArray(data)
         else:
             img = get_enhanced_image(ds)
             data, mode = img._finalize(fill_value=fill_value)
             if data.ndim == 3:
                 # assume all other shapes are (y, x)
                 # we need arrays grouped by pixel so
                 # transpose if needed
                 data = data.transpose('y', 'x', 'bands')
         yield data.data
Esempio n. 8
0
 def test_multisensor_exact(self):
     """Test that a DataArray with two sensors can match exactly."""
     from satpy.writers import Enhancer, get_enhanced_image
     from xarray import DataArray
     ds = DataArray(np.arange(1, 11.).reshape((2, 5)),
                    attrs={
                        'name': 'my_comp',
                        'sensor': {'test_sensor2', 'test_sensor1'},
                        'mode': 'L'
                    },
                    dims=['y', 'x'])
     e = Enhancer()
     assert e.enhancement_tree is not None
     img = get_enhanced_image(ds, enhance=e)
     # make sure that both sensor configs were loaded
     assert (set(e.sensor_enhancement_configs) == {
         os.path.abspath(self.ENH_FN),
         os.path.abspath(self.ENH_FN2)
     })
     # test_sensor1 config should have been used because it is
     # alphabetically first
     np.testing.assert_allclose(img.data.values[0], ds.data / 20.0)
Esempio n. 9
0
 def _get_enhanced_background_data(background_layer):
     img = get_enhanced_image(background_layer)
     img.data = img.data.clip(0.0, 1.0)
     img = img.convert('RGBA')
     return img.data
Esempio n. 10
0
    def _save_datasets_as_mitiff(self, datasets, image_description,
                                 gen_filename, **kwargs):
        """Put all togehter and save as a tiff file with the special tag
           making it a mitiff file.
        """
        from libtiff import TIFF

        tif = TIFF.open(gen_filename, mode='w')

        tif.SetField(IMAGEDESCRIPTION, (image_description).encode('utf-8'))

        cns = self.translate_channel_name.get(kwargs['sensor'], {})
        if isinstance(datasets, list):
            LOG.debug("Saving datasets as list")

            for _cn in self.channel_order[kwargs['sensor']]:
                for dataset in datasets:
                    if dataset.attrs['name'] == _cn:
                        reverse_offset = 0.
                        reverse_scale = 1.
                        if dataset.attrs[
                                'calibration'] == 'brightness_temperature':
                            reverse_offset = 255.
                            reverse_scale = -1.
                            dataset.data += KELVIN_TO_CELSIUS

                        # Need to possible translate channels names from satpy to mitiff
                        cn = cns.get(dataset.attrs['name'],
                                     dataset.attrs['name'])
                        _data = reverse_offset + reverse_scale * ((
                            (dataset.data - float(self.mitiff_config[
                                kwargs['sensor']][cn]['min-val'])) /
                            (float(self.mitiff_config[kwargs['sensor']][cn][
                                'max-val']) - float(self.mitiff_config[
                                    kwargs['sensor']][cn]['min-val']))) * 255.)
                        data = _data.clip(0, 255)

                        tif.write_image(data.astype(np.uint8),
                                        compression='deflate')
                        break
        elif 'dataset' in datasets.attrs['name']:
            LOG.debug("Saving %s as a dataset.", datasets.attrs['name'])
            for _cn in self.channel_order[kwargs['sensor']]:
                for i, band in enumerate(datasets['bands']):
                    if band == _cn:
                        chn = datasets.sel(bands=band)
                        reverse_offset = 0.
                        reverse_scale = 1.
                        if chn.attrs['prerequisites'][i][
                                4] == 'brightness_temperature':
                            reverse_offset = 255.
                            reverse_scale = -1.
                            chn.data += KELVIN_TO_CELSIUS

                        # Need to possible translate channels names from satpy to mitiff
                        cn = cns.get(chn.attrs['prerequisites'][i][0],
                                     chn.attrs['prerequisites'][i][0])
                        _data = reverse_offset + reverse_scale * ((
                            (chn.data - float(self.mitiff_config[
                                kwargs['sensor']][cn]['min-val'])) /
                            (float(self.mitiff_config[kwargs['sensor']][cn][
                                'max-val']) - float(self.mitiff_config[
                                    kwargs['sensor']][cn]['min-val']))) * 255.)
                        data = _data.clip(0, 255)

                        tif.write_image(data.astype(np.uint8),
                                        compression='deflate')
                        break

        else:
            LOG.debug("Saving datasets as enhanced image")
            img = get_enhanced_image(datasets.squeeze(), enhance=self.enhancer)
            for i, band in enumerate(img.data['bands']):
                chn = img.data.sel(bands=band)
                data = chn.values.clip(0, 1) * 254. + 1
                data = data.clip(0, 255)
                tif.write_image(data.astype(np.uint8), compression='deflate')

        del tif
Esempio n. 11
0
el = Path("/export/home/mbrewer/Documents/GMTED2010_15n030_0125deg.nc")
rad = Path(
    "/export/home/mbrewer/Documents/radar_files/KBBX20181108_213133_V06")

radar = pyart.io.read_nexrad_archive(rad)
#gf = pyart.filters.GateFilter(radar)
#gf.exclude_transition()
#gf.exclude_above('reflectivity', 100) #Mask out dBZ above 100
#gf.exclude_below('reflectivity', 5) #Mask out dBZ below 5
#despec = pyart.correct.despeckle_field(radar, 'reflectivity',gatefilter = gf, size = 20) #The despeckling mask routine that takes out small noisey reflectivity bits not near the main plume

elev = xr.open_dataset(el)
scn = Scene(filenames=glob("npp/*"), reader='viirs_l1b')
scn.load(['true_color', 'I02'])
new_scn = scn.resample('northamerica')
var = get_enhanced_image(new_scn['true_color']).data
var = var.transpose('y', 'x', 'bands')

st = str(scn.attrs['sensor'])[2:-2]

fig = plt.figure(figsize=(20, 10), dpi=200)
crs = new_scn['true_color'].attrs['area'].to_cartopy_crs()
ax = fig.add_subplot(1, 1, 1, projection=crs)

ax.imshow(var.data,
          extent=(var.x[0], var.x[-1], var.y[-1], var.y[0]),
          origin='upper')
#ax.add_feature(cfeature.COASTLINE.with_scale('10m'), edgecolor='orange')
#ax.add_feature(cfeature.STATES.with_scale('10m'), edgecolor='orange')
#ax.add_feature(USCOUNTIES.with_scale('500k'), edgecolor='orange', alpha = .75)
ax.set_extent([-122.5, -120.5, 39., 40.5], crs=ccrs.PlateCarree())
Esempio n. 12
0
    def show(self, dataset_id, overlay=None):
        """Show the *dataset* on screen as an image.
        """

        from satpy.writers import get_enhanced_image
        get_enhanced_image(self[dataset_id], overlay=overlay).show()
Esempio n. 13
0
    def _save_datasets_as_mitiff(self, datasets, image_description,
                                 gen_filename, **kwargs):
        """Put all togehter and save as a tiff file with the special tag
           making it a mitiff file.
        """
        from libtiff import TIFF

        tif = TIFF.open(gen_filename, mode='w')

        tif.SetField(IMAGEDESCRIPTION, (image_description).encode('utf-8'))

        cns = self.translate_channel_name.get(kwargs['sensor'], {})
        if isinstance(datasets, list):
            LOG.debug("Saving datasets as list")
            for _cn in self.channel_order[kwargs['sensor']]:
                for dataset in datasets:
                    if dataset.attrs['name'] == _cn:
                        # Need to possible translate channels names from satpy to mitiff
                        cn = cns.get(dataset.attrs['name'], dataset.attrs['name'])
                        data = self._calibrate_data(dataset, dataset.attrs['calibration'],
                                                    self.mitiff_config[kwargs['sensor']][cn]['min-val'],
                                                    self.mitiff_config[kwargs['sensor']][cn]['max-val'])
                        tif.write_image(data.astype(np.uint8), compression='deflate')
                        break
        elif 'dataset' in datasets.attrs['name']:
            LOG.debug("Saving %s as a dataset.", datasets.attrs['name'])
            if len(datasets.dims) == 2 and (all('bands' not in i for i in datasets.dims)):
                # Special case with only one channel ie. no bands

                # Need to possible translate channels names from satpy to mitiff
                # Note the last index is a tuple index.
                cn = cns.get(datasets.attrs['prerequisites'][0][0],
                             datasets.attrs['prerequisites'][0][0])
                data = self._calibrate_data(datasets, datasets.attrs['prerequisites'][0][4],
                                            self.mitiff_config[kwargs['sensor']][cn]['min-val'],
                                            self.mitiff_config[kwargs['sensor']][cn]['max-val'])

                tif.write_image(data.astype(np.uint8), compression='deflate')
            else:
                for _cn_i, _cn in enumerate(self.channel_order[kwargs['sensor']]):
                    for i, band in enumerate(datasets['bands']):
                        if band == _cn:
                            chn = datasets.sel(bands=band)
                            # Need to possible translate channels names from satpy to mitiff
                            # Note the last index is a tuple index.
                            cn = cns.get(chn.attrs['prerequisites'][_cn_i][0],
                                         chn.attrs['prerequisites'][_cn_i][0])
                            data = self._calibrate_data(chn, chn.attrs['prerequisites'][_cn_i][4],
                                                        self.mitiff_config[kwargs['sensor']][cn]['min-val'],
                                                        self.mitiff_config[kwargs['sensor']][cn]['max-val'])

                            tif.write_image(data.astype(np.uint8), compression='deflate')
                            break
        else:
            LOG.debug("Saving datasets as enhanced image")
            img = get_enhanced_image(datasets.squeeze(), enhance=self.enhancer)
            if 'bands' in img.data.sizes and 'bands' not in datasets.sizes:
                LOG.debug("Datasets without 'bands' become image with 'bands' due to enhancement.")
                LOG.debug("Needs to regenerate mitiff image description")
                image_description = self._make_image_description(img.data, **kwargs)
                tif.SetField(IMAGEDESCRIPTION, (image_description).encode('utf-8'))
            for i, band in enumerate(img.data['bands']):
                chn = img.data.sel(bands=band)
                data = chn.values.clip(0, 1) * 254. + 1
                data = data.clip(0, 255)
                tif.write_image(data.astype(np.uint8), compression='deflate')

        del tif
Esempio n. 14
0
 def show(self, dataset_id, overlay=None):
     """Show the *dataset* on screen as an image."""
     from satpy.writers import get_enhanced_image
     img = get_enhanced_image(self[dataset_id].squeeze(), overlay=overlay)
     img.show()
     return img
Esempio n. 15
0
    def show(self, dataset_id):
        """Show the *dataset* on screen as an image.
        """

        from satpy.writers import get_enhanced_image
        get_enhanced_image(self[dataset_id]).show()