How do I exclude no data values when doing a raster calculation with rasterio. I realise that the files are read into an array, and that "0" is a nodata value. However, when I do a simple sum like "image + 1" it applies the math to the nodata value as well. So what is the best way to 'ignore' nodata values in the calculation?
FYI: I am calibrating the Landsat bands to reflectance and this is what my current snippet of the band math looks like:
if "B1.TIF" in band:
print band
image = "%s" % (band)
with rasterio.open(image) as src:
image_read = src.read(1)
#np.seterr(divide='ignore', invalid='ignore')
constant = 0.01745329251994444444444444444444 #Constant is calculated (3.14/180) which is converting the sun-angle to sun_radians which was suggested by WOlfgang
set_mb = "{:.16f}".format(float(mult_band1))
set_ab = "{:.16f}".format(float(add_band1))
toa_1 = (float(set_mb) * image_read.astype(float)) + (float(set_ab))
solar_z = np.cos((90-float(sun_elevation))*float(constant))
toa_2 = (toa_1.astype(float) / solar_z) * 100000
kwargs = src.meta
with rasterio.open(outfile, 'w', **kwargs) as dst:
dst.write_band(1, toa_2.astype(rasterio.uint16))
#print image_read.mask, toa_2
Is there a simple way to ignore nodata values in the calculation?