2

As we know sentinel satellite data have many sub folder. I want to create composite band of each datasets with the band 2, band 3, band 4, band 8 which are located in one folder(10m) and band 11, band 12 which are located in other folder(20m).I am making a arcpy code for that but code was not run correctly. Here is the code

import arcpy, os

basepath = "L:\Arcpy\Data\Satellite_image_raw\New folder"

endswith = ("_B02_10m.jp2", "_B03_10m.jp2", "_B04_10m.jp2", "_B08_10m.jp2", "_B11_20m.jp2", "_B12_20m.jp2")

output = "L:\Arcpy\Data\Layer_stack"

rasterlist = []

for root, dirs, files in os.walk(basepath):
    for rasterlayer in files:
        if rasterlayer.endswith(endswith):
           #print(os.path.join(root, rasterlayer))
           rasterlist.append(rasterlayer)
           print "image:" + str(rasterlist)
name = os.path.join(output, rasterlist[1].split("_")[0] + ".img")
arcpy.CompositeBands_management(rasterlist, name)
print(rasterlist)

I was trying these code but output is not created. It gave this error:

 image:['T45QVD_20210104T044201_B02_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', n 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2', 
'T44QRJ_20210102T045209_B08_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2', 
'T44QRJ_20210102T045209_B08_10m.jp2', 'T44QRJ_20210102T045209_B11_20m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2', 
'T44QRJ_20210102T045209_B08_10m.jp2', 'T44QRJ_20210102T045209_B11_20m.jp2', 
'T44QRJ_20210102T045209_B12_20m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2', 
'T44QRJ_20210102T045209_B08_10m.jp2', 'T44QRJ_20210102T045209_B11_20m.jp2', 
'T44QRJ_20210102T045209_B12_20m.jp2', 'T45QUD_20210102T045209_B02_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2', 
'T44QRJ_20210102T045209_B08_10m.jp2', 'T44QRJ_20210102T045209_B11_20m.jp2', 
'T44QRJ_20210102T045209_B12_20m.jp2', 'T45QUD_20210102T045209_B02_10m.jp2', 
'T45QUD_20210102T045209_B03_10m.jp2']
 image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2', 
'T44QRJ_20210102T045209_B08_10m.jp2', 'T44QRJ_20210102T045209_B11_20m.jp2', 
'T44QRJ_20210102T045209_B12_20m.jp2', 'T45QUD_20210102T045209_B02_10m.jp2', 
'T45QUD_20210102T045209_B03_10m.jp2', 'T45QUD_20210102T045209_B04_10m.jp2']
image:['T45QVD_20210104T044201_B02_10m.jp2', 
'T45QVD_20210104T044201_B03_10m.jp2', 'T45QVD_20210104T044201_B04_10m.jp2', 
'T45QVD_20210104T044201_B08_10m.jp2', 'T45QVD_20210104T044201_B11_20m.jp2', 
'T45QVD_20210104T044201_B12_20m.jp2', 'T44QRJ_20210102T045209_B02_10m.jp2', 
'T44QRJ_20210102T045209_B03_10m.jp2', 'T44QRJ_20210102T045209_B04_10m.jp2', 
'T44QRJ_20210102T045209_B08_10m.jp2', 'T44QRJ_20210102T045209_B11_20m.jp2', 
'T44QRJ_20210102T045209_B12_20m.jp2', 'T45QUD_20210102T045209_B02_10m.jp2', 
'T45QUD_20210102T045209_B03_10m.jp2', 'T45QUD_20210102T045209_B04_10m.jp2', 
'T45QUD_20210102T045209_B08_10m.jp2']

 Traceback (most recent call last):
 File "L:\Arcpy\Py_script\composite_band_script_1.py", line 35, in <module>
 arcpy.CompositeBands_management(rasterlist, name)
 File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", 
 line 13647, in CompositeBands
 raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Rasters:

How can I solve that?

1
1

A few lines of code you should take another look at:

  1. Check the input parameters for file.endswith(). I suspect it expects a single string as input, rather than a tuple with many strings.

  2. The first parameter of arcpy.CompositeBands_managment() expects all the rasters you are joining as a list, not one raster at a time. I would recommend at this point in the for loops you add the names to a list, and then after the for loops are finished run arcpy.CompositeBands_managment(input_list, newname).

  3. Check if name = os.path.join() is outputting what you want it to. Specifically, whether file[1] gives you what you think it does.

14
  • 1. When I am print these line: print(os.path.join(root, file)) without re-meaning code, It is gave selected bands from every folder. 2. I am using tuple because endswith() doesn't except list. 3. Data set is not opneing that's why name = os.path.join() is not working. – Siddhartha Dhara Jan 20 at 18:08
  • Your arcpy command is expecting a list of strings (file paths - directories and filenames) for its first parameter, and the second parameter should be a single string of the output file path. The error says that it cannot open the input datasets which means there is a problem with file. "file" is also a builtin in python, so if you are trying to use it as a variable name (as "for file in files" would suggest) this may cause problems. – ycartwhelen Jan 20 at 20:17
  • As per you instruction I was making a empty list and assign the variable which have selected bands and for second parameter I would converted the list into string. Also I would change "file" to rasterlayer. But still I am getting the same error. – Siddhartha Dhara Jan 22 at 16:06
  • Please edit your question to explain how you've changed your code. That will help other potential answerers see what you are currently working with. – ycartwhelen Jan 22 at 17:55
  • I have done changes in above code for your reference. – Siddhartha Dhara Jan 23 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.