Skip to content

Clip/Mask data below threshold value #638

@siddharth0248

Description

@siddharth0248

Some data providers request clipping data below a specific threshold value so that the colormap starts at this threshold and extends to an upper limit. For example, in TM5 Var, we clipped the values below 0.48 and then set the min-max range in the metadata to 0.48 and 12. If we hadn’t clipped the data, we would have seen many more pixels, and we also want to avoid designating them as "no-data."
Currently, my approach involves masking out these values during the data transformation using the provided script. Could this functionality be incorporated into rio-tiler to enhance sustainability?

import rasterio
import numpy as np

url_original = "s3://ghgc-data-store/tm54dvar-ch4flux-monthgrid-v1/methane_emis_total_201601.tif"
url_masked = "s3://ghgc-data-store/tm54dvar-ch4flux-mask-monthgrid-v1/methane_emis_total_201601.tif"

with rasterio.open(url_original) as src:
    metadata = src.meta.copy()
    no_data_value = -9999  
    metadata.update({
        'dtype': 'float32',
        'nodata': no_data_value
    })

    # Create an array to hold the clipped data for all bands
    clipped_data = np.empty((metadata['count'], metadata['height'], metadata['width']), dtype='float32')

    # Iterate through each band
    for i in range(metadata['count']):
        band_data = src.read(i + 1)

        # Create a mask for values below the threshold
        mask = band_data >= threshold

        # Clip pixels and assign no-data value
        clipped_data[i] = np.where(mask, band_data, no_data_value)

    with rasterio.open(output_tiff, 'w', **metadata) as dst:
        dst.write(clipped_data)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions