import arcpy
# Set the current workspace
arcpy.env.workspace = "D:/datasets/planetscope/2021/2021_09-2021_08_psscene_analytic_sr_udm2/copy raster udm"
location = "D:/datasets/planetscope/2021/2021_09-2021_08_psscene_analytic_sr_udm2/mosaic"
# Get and print a list of GRIDs from the workspace
#rasters = arcpy.ListRasters("*Analytic*", "TIF")
#for raster in rasters:
# print(raster)
rasters = arcpy.ListRasters("*udm2*", "TIF")
for raster in rasters:
print(raster)
values = set(map(lambda x:x.split('_')[-7], rasters)) #Find unique groups
values
newlist = [[y for y in rasters if y.split('_')[-7]==x] for x in values] #From the rasterlist, create a new list of
#lists of grouped rasters
for group in newlist:
if len(group)>0: #Dont mosaic single rasters
outname='udm2_Mosaic_{0}.tif'.format(group[0].split('_')[-7])
arcpy.MosaicToNewRaster_management(input_rasters=group,
output_location = location,raster_dataset_name_with_extension=outname,
pixel_type="8_BIT_UNSIGNED",cellsize="", number_of_bands="8",
mosaic_method="BLEND",mosaic_colormap_mode="MATCH")