Changing Reflectance Values in Python/Rasterio Modu

I’m needing some help. I’m attempting to use the Python modules rasterio and numpy to change the reflectance values. First I created a list of my bands, assigned variables, and then layer stacked the 4 bands. I need to do this again but I need to change the reflectance values by dividing all reflectance values by 10000 when you supply the individual bands’ arrays to the write function of the new layer stack. This is because surface reflectance values for the Sentinel 2 data are scaled by 10000. So, a value of 5000 in one of the bands (uint16 dtype) is actually a reflectance value of 0.5 (i.e. 5000/10000). My question is where exactly do I divide 10000 at? I’ve tried simply inserting “/10000” in the stacked.write lines. Ultimately, I keep coming up with either numpy.array issues or data types not matching other types. Below is the code for the original stack without modified values. Thank you so much in advance.

inlistB = [
‘T13TDF_20211107T175541_B02_10m.jp2’,
‘T13TDF_20211107T175541_B03_10m.jp2’,
‘T13TDF_20211107T175541_B04_10m.jp2’,
‘T13TDF_20211107T175541_B08_10m.jp2’
]

b1 = rasterio.open(os.path.join(intdat, inlistB [0]), mode=‘r’)
b2 = rasterio.open(os.path.join(intdat, inlistB [1]), mode=‘r’)
b3 = rasterio.open(os.path.join(intdat, inlistB [2]), mode=‘r’)
b4 = rasterio.open(os.path.join(intdat, inlistB [3]), mode=‘r’)

stacked = rasterio.open(
os.path.join(outdat, ‘stacked.tiff’), mode = ‘w’,
driver = ‘GTiff’,
height = b1.height,
width = b1.width,
count = len(inlistB),
dtype = b1.dtypes[0],
crs = b1.crs,
transform=b1.transform
)

stacked.write(b1.read()[0], 1)
stacked.write(b2.read()[0], 2)
stacked.write(b3.read()[0], 3)
stacked.write(b4.read()[0], 4)
stacked.close()

stacked = rasterio.open(os.path.join(outdat, ‘stacked.tiff’), mode = ‘r’)

1 Like