Home > other >  Python: Change color of bad pixels
Python: Change color of bad pixels

Time:12-27

I have an HDF file that contains satellite data and when plotted, looks like the attached image below. It has a lot of bad pixels (they're colored black when using this particular colormap). How can I change the color of bad pixels to white for example? Thank you.

enter image description here

Link to HDF file: enter image description here enter image description here

CodePudding user response:

The Flags never seem to get a value higher than 3, so I don't think that will help you out for this specific issue. I don't see any attributes specifying the nodata value, or the documentation mentioning it, but it's quite obvious that the maximum float32 value is used for this.

The coordinates outside the swath are filled with this value, and also the extreme low (?) values in the temperature.

As @Michael Delgado already mentioned, cmap.set_bad() is a good way to assign a specific color (or alpha!) to these values. But it requires Matplotlib to know what is "bad". In the current form, you could use cmap.set_over() but that will loose the distinction with valid values that are clipped (temp > vmax).

It's usually best to parse a nodata value from the metadata of the file, but I don't see it right away (didn't look that careful). When hard coding it:

import h5py
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib as mpl

fn = 'NmHRIR1H.19660626_14-57-45_564_005.hdf'
nodata_value = np.finfo(np.float32).max

with h5py.File(fn) as f:
    temp = np.ma.masked_values(f['HRIR-Temp'][:], nodata_value) - 273.15
    lat = np.ma.masked_values(f['latitude'][:], nodata_value)
    lon = np.ma.masked_values(f['longitude'][:], nodata_value)

cmap = mpl.colormaps["binary"].copy()
cmap.set_bad("r", alpha=1)
norm = mpl.colors.Normalize(vmin=-60, vmax=40)

fig, ax = plt.subplots(
    figsize=(6,6), dpi=86, facecolor="w", layout="compressed", 
    subplot_kw=dict(projection=ccrs.PlateCarree()),
)

im = ax.pcolormesh(lon, lat, temp, cmap=cmap, norm=norm)
cb = fig.colorbar(im, ax=ax, shrink=.5, extend="both")

ax.coastlines(resolution='10m', color='gold', linewidth=0.5)

extent = [124,139,12,27]
ax.set_extent(extent, crs=ccrs.PlateCarree())

enter image description here

  • Related