IndexError: list index out of range In extract the mode, product type, and the polarizations from filename in snappy sent1

Hello everybody, thanks in advance for you’re insight !!

After installing Snappy on my environment, I’am trying to preprocess sentinel 1 images,
my code bug when I try to ,extract the mode, product type, and the polarizations from filename,

This is my code:

## Extract mode, product type, and polarizations from filename
       modestamp = folder.split("_")[1]
       productstamp = folder.split("_")[2]
       polstamp = folder.split("_")[3]

and the error :

Traceback (most recent call last):
  File "C:/Users/Mine_Finder/PycharmProjects/Snap/test.py", line 162, in <module>
    main()
  File "C:/Users/Mine_Finder/PycharmProjects/Snap/test.py", line 103, in main
    modestamp = folder.split("_")[1]
IndexError: list index out of range

I have this error for the 3 lines :

        modestamp = folder.split("_")[1]
        productstamp = folder.split("_")[2]
        polstamp = folder.split("_")[3]

If someone have a tips,
Thanks in adavance,

Thomas

You need to provide more detail (Python version, how folder was created). If you used pathlib there was a version that required `str(filepath)’ before using string operations.

1 Like

thanks for your answer, i’am using:

Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] on win32,

this is how i’ve created, folder :
for folder in os.listdir(path): gc.enable() gc.collect() sentinel_1 = ProductIO.readProduct(path + "\\" + folder + "\\manifest.safe") print(sentinel_1)

this is all my code :

import datetime
import time
from snappy import ProductIO
from snappy import HashMap
import os, gc
from snappy import GPF


def do_apply_orbit_file(source):
    print('\tApply orbit file...')
    parameters = HashMap()
    parameters.put('Apply-Orbit-File', True)
    output = GPF.createProduct('Apply-Orbit-File', parameters, source)
    return output

def do_thermal_noise_removal(source):
    print('\tThermal noise removal...')
    parameters = HashMap()
    parameters.put('removeThermalNoise', True)
    output = GPF.createProduct('ThermalNoiseRemoval', parameters, source)
    return output

def do_calibration(source, polarization, pols):
    print('\tCalibration...')
    parameters = HashMap()
    parameters.put('outputSigmaBand', True)
    if polarization == 'DH':
        parameters.put('sourceBands', 'Intensity_HH,Intensity_HV')
    elif polarization == 'DV':
        parameters.put('sourceBands', 'Intensity_VH,Intensity_VV')
    elif polarization == 'SH' or polarization == 'HH':
        parameters.put('sourceBands', 'Intensity_HH')
    elif polarization == 'SV':
        parameters.put('sourceBands', 'Intensity_VV')
    else:
        print("different polarization!")
    parameters.put('selectedPolarisations', pols)
    parameters.put('outputImageScaleInDb', False)
    output = GPF.createProduct("Calibration", parameters, source)
    return output

def do_speckle_filtering(source):
    print('\tSpeckle filtering...')
    parameters = HashMap()
    parameters.put('filter', 'Lee')
    parameters.put('filterSizeX', 5)
    parameters.put('filterSizeY', 5)
    output = GPF.createProduct('Speckle-Filter', parameters, source)
    return output

def do_terrain_correction(source, proj, downsample):
    print('\tTerrain correction...')
    parameters = HashMap()
    parameters.put('demName', 'GETASSE30')
    parameters.put('imgResamplingMethod', 'BILINEAR_INTERPOLATION')
    parameters.put('mapProjection', proj)       # comment this line if no need to convert to UTM/WGS84, default is WGS84
    parameters.put('saveProjectedLocalIncidenceAngle', True)
    parameters.put('saveSelectedSourceBand', True)
    while downsample == 1:                      # downsample: 1 -- need downsample to 40m, 0 -- no need to downsample
        parameters.put('pixelSpacingInMeter', 40.0)
        break
    output = GPF.createProduct('Terrain-Correction', parameters, source)
    return output

def do_subset(source, wkt):
    print('\tSubsetting...')
    parameters = HashMap()
    parameters.put('geoRegion', wkt)
    output = GPF.createProduct('Subset', parameters, source)
    return

def main():
    ## All Sentinel-1 data sub folders are located within a super folder (make sure the data is already unzipped and each sub folder name ends with '.SAFE'):
    path = r'C:/Users/***/Desktop/***/sentinel1_kolwezi/S1A_IW_SLC__1SDV_20211222T035608_20211222T035635_041116_04E29A_38B0.SAFE'
    outpath = r'C:/Users/***/Desktop/***/sentinel1_kolwezi/S1A_IW_SLC__1SDV_20211222T035608_20211222T035635_041116_04E29A_38B0.SAFE'
    if not os.path.exists(outpath):
        os.makedirs(outpath)
    ## well-known-text (WKT) file for subsetting (can be obtained from SNAP by drawing a polygon)
    wkt = 'POLYGON((12326.513206992082 3680.862745456959, 13003.057784562978 3680.862745456959, 13003.057784562978 4259.028357609269, / 12326.513206992082 4259.028357609269, 12326.513206992082 3680.862745456959))'
    ## UTM projection parameters
    proj = '''PROJCS["UTM Zone 4 / World Geodetic System 1984",GEOGCS["World Geodetic System 1984",DATUM["World Geodetic System 1984",SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]],UNIT["degree", 0.017453292519943295],AXIS["Geodetic longitude", EAST],AXIS["Geodetic latitude", NORTH]],PROJECTION["Transverse_Mercator"],PARAMETER["central_meridian", -159.0],PARAMETER["latitude_of_origin", 0.0],PARAMETER["scale_factor", 0.9996],PARAMETER["false_easting", 500000.0],PARAMETER["false_northing", 0.0],UNIT["m", 1.0],AXIS["Easting", EAST],AXIS["Northing", NORTH]]'''



    for folder in os.listdir(path):
        gc.enable()
        gc.collect()
        sentinel_1 = ProductIO.readProduct(path + "\\" + folder + "\\manifest.safe")
        print(sentinel_1)

        loopstarttime=str(datetime.datetime.now())
        print('Start time:', loopstarttime)
        start_time = time.time()

        ## Extract mode, product type, and polarizations from filename
        modestamp = folder.split("_")[1]
        productstamp = folder.split("_")[2]
        polstamp = folder.split("_")[3]

        polarization = polstamp[2:4]
        if polarization == 'DV':
            pols = 'VH,VV'
        elif polarization == 'DH':
            pols = 'HH,HV'
        elif polarization == 'SH' or polarization == 'HH':
            pols = 'HH'
        elif polarization == 'SV':
            pols = 'VV'
        else:
            print("Polarization error!")

        ## Start preprocessing:
        applyorbit = do_apply_orbit_file(sentinel_1)
        thermaremoved = do_thermal_noise_removal(applyorbit)
        calibrated = do_calibration(thermaremoved, polarization, pols)
        down_filtered = do_speckle_filtering(calibrated)
        del applyorbit
        del thermaremoved
        del calibrated
        ## IW images are downsampled from 10m to 40m (the same resolution as EW images).
        if (modestamp == 'IW' and productstamp == 'GRDH') or (modestamp == 'EW' and productstamp == 'GRDH'):
            down_tercorrected = do_terrain_correction(down_filtered, proj, 1)
            down_subset = do_subset(down_tercorrected, wkt)
            del down_filtered
            del down_tercorrected
        elif modestamp == 'EW' and productstamp == 'GRDM':
            tercorrected = do_terrain_correction(down_filtered, proj, 0)
            subset = do_subset(tercorrected, wkt)
            del down_filtered
            del tercorrected
        else:
            print("Different spatial resolution is found.")

        down = 1
        try: down_subset
        except NameError:
            down = None
        if down is None:
            print("Writing...")
            ProductIO.writeProduct(subset, outpath + '\\' + folder[:-5], 'GeoTIFF')
            del subset
        elif down == 1:
            print("Writing undersampled image...")
            ProductIO.writeProduct(down_subset, outpath + '\\' + folder[:-5] + '_40', 'GeoTIFF')
            del down_subset
        else:
            print("Error.")

        print('Done.')
        sentinel_1.dispose()
        sentinel_1.closeIO()
        print("--- %s seconds ---" % (time.time() - start_time))

if __name__== "__main__":
    main()

Many thanks

Aslo I dont think I use “pathlib”

I think you need to be more careful with gc – you should not run gc.enable() in a loop, and gc.collect() may be destroying folder. There is a gc.isenabled(). I’m not sure garbage
collection will be much help – python often get references to memory managed by Java, so
you may be better off using python to construct and run a gpt command-line so java gets a fresh
start.

Thanks for the answer,
how would you change the code?
Any exemples
The response of print(sentinel_1) is None

T.

I generally only use snappy when some processing steps need python libraries (e.g., numpy). When developing Python scripts for snappy it is best to start with small scripts that do one step in the processing chain for one file and save to BEAM-DIMAP so I can check the results. Once those are working you can try doing longer processing chains.

I think Python is at a disadvantage because you are calling GPF() multiple times. GPF with a multi-step graph it is often very efficient (but needs care to get each step right, so again it is best to start with one step at a time) You may get some ideas from snapista.

1 Like