![]() |
Iterating over a dictionary in a for loop - checking code has worked - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Iterating over a dictionary in a for loop - checking code has worked (/thread-31716.html) |
Iterating over a dictionary in a for loop - checking code has worked - sallyjc81 - Dec-29-2020 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: 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! RE: Iterating over a dictionary in a for loop - checking code has worked - ndc85430 - Dec-29-2020 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. |