Python Forum
Iterating over a dictionary in a for loop - checking code has worked
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating over a dictionary in a for loop - checking code has worked
Hello, Smile

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]
        if bn in ['1', '2', '3', '4']:
            i = int(bn)
            value = node.getElementsByTagName("ps:reflectanceCoefficient")[0]
            coeffs[i] = float(value)
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 as src:
        band_blue_radiance =
    with as src:
        band_green_radiance =
    with as src:
        band_red_radiance =
    with as src:
        band_nir_radiance =
    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
    kwargs = meta
    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"{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!
I expect many here aren't going to be familiar with whichever field this is (atmospheric physics?) and are likely to find it difficult to know what your code is doing. Is it possible to formulate the problem you have in terms of an input and an expected output (into and from a function, say)? That way, you could at least write a unit test to verify that given that input, the output is what you expect.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python: re.findall to find multiple instances don't work but search worked Secret 1 392 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 429 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 1,164 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  embold code checking site zahhak 1 1,100 Sep-08-2021, 02:39 PM
Last Post: Larz60+
  Dictionary within html code ebolisa 4 1,573 Aug-09-2021, 11:36 AM
Last Post: ebolisa
  checking for last item in for loop Skaperen 11 2,605 Jul-09-2021, 11:40 PM
Last Post: Skaperen
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 1,711 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Help! I accidentally ran a file without checking the code scaryzane 5 2,002 Dec-30-2020, 09:21 AM
Last Post: DeaD_EyE
  Error while checking for key in Dictionary onenessboy 5 1,719 Aug-14-2020, 01:06 PM
Last Post: onenessboy
  Line of code to show dictionary doesn't work MaartenRo 2 1,749 Jul-28-2020, 03:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020