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
#1
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].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!
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [WORKED AROUND] Problem installing elitech-datareader, 'cannot import build_py_2to3' NeilUK 4 1,561 Jul-09-2023, 10:01 AM
Last Post: NeilUK
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,172 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,531 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 2,167 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  embold code checking site zahhak 1 1,642 Sep-08-2021, 02:39 PM
Last Post: Larz60+
  Dictionary within html code ebolisa 4 2,541 Aug-09-2021, 11:36 AM
Last Post: ebolisa
  checking for last item in for loop Skaperen 11 4,139 Jul-09-2021, 11:40 PM
Last Post: Skaperen
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,650 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Help! I accidentally ran a file without checking the code scaryzane 5 2,914 Dec-30-2020, 09:21 AM
Last Post: DeaD_EyE
  Error while checking for key in Dictionary onenessboy 5 2,593 Aug-14-2020, 01:06 PM
Last Post: onenessboy

Forum Jump:

User Panel Messages

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