Dec-29-2020, 03:55 PM
Hello,
I am using the rasterio library to perform some a pre-processing corrections (top of atmosphere reflectance(TOA) conversion) to a set of satellite images in a folder.
The following code suitably writes the reflectance coefficient from all the corresponding metadata files in the folder to a dictionary, which is required to compute TOA:
Thank you in advance!

I am using the rasterio library to perform some a pre-processing corrections (top of atmosphere reflectance(TOA) conversion) to a set of satellite images in a folder.
The following code suitably writes the reflectance coefficient from all the corresponding metadata files in the folder to a dictionary, which is required to compute TOA:
for xml_file in glob.glob("*AnalyticMS_metadata_clip.xml"): xmldoc = minidom.parse(xml_file) nodes = xmldoc.getElementsByTagName("ps:bandSpecificMetadata") coeffs = {} for node in nodes: bn = node.getElementsByTagName("ps:bandNumber")[0].firstChild.data if bn in ['1', '2', '3', '4']: i = int(bn) value = node.getElementsByTagName("ps:reflectanceCoefficient")[0].firstChild.data coeffs[i] = float(value) print(coeffs)This code then calculates the TOA for each image, performs the conversion, and writes a new tif file with the original metadata. However I am unsure if the code is using the correct metadata coefficient which corresponds to the image:
for filename in glob.glob("*AnalyticMS_clip.tif"): with rasterio.open(filename) as src: band_blue_radiance = src.read(1) with rasterio.open(filename) as src: band_green_radiance = src.read(2) with rasterio.open(filename) as src: band_red_radiance = src.read(3) with rasterio.open(filename) as src: band_nir_radiance = src.read(4) band_blue_reflectance = band_blue_radiance * coeffs[1] band_green_reflectance = band_green_radiance * coeffs[2] band_red_reflectance = band_red_radiance * coeffs[3] band_nir_reflectance = band_nir_radiance * coeffs[4] meta = src.meta print(meta) kwargs = meta kwargs.update(dtype='uint16') scale = 10000 blue_ref_scaled = scale * band_blue_reflectance green_ref_scaled = scale * band_green_reflectance red_ref_scaled = scale * band_red_reflectance nir_ref_scaled = scale * band_nir_reflectance from rasterio import uint16 red = red_ref_scaled.astype(uint16) green = green_ref_scaled.astype(uint16) blue = blue_ref_scaled.astype(uint16) nir = nir_ref_scaled.astype(uint16) with rasterio.open(f"{output}reflectance{filename}.tif", 'w', **kwargs) as dst: dst.write_band(1, red) dst.write_band(2, green) dst.write_band(3, blue) dst.write_band(4, nir)My questions are: does my code iterate through the corresponding coefficient and how can this be checked? If not, what are the required steps to correctly incorporate the iteration through the coefficients into the code?
Thank you in advance!