利用python和Sen2cor对Sentinel2进行批量大气校正
p.s本文只适合大量的批量校正,一般两三幅影像的那种就直接搜批量校正就好了。
废话不多说,直接上代码
import os
import shutil
import subprocess
file_path = "E:\\ESRIRS\\Sentinel2Imag\\"
year_list = ["2016", "2017", "2018", "2019", "2020", "2021", "2022"]
satellite_list = ["Sentinel2A", "Sentinel2B"]
def readNameWriteInTxt():
for year in year_list:
for satellite in satellite_list:
file_path2 = file_path + year + "\\" + satellite
txt_file_path = file_path + satellite + year + ".txt"
names = os.listdir(file_path2)
if names is None:
names += "Null"
else:
with open(txt_file_path, "w+") as log:
log.write(file_path2 + "\n")
for name in names:
name = name + "\n"
log.write(name)
def AutomationAtmosphericCorrectionC(satellite_listA, year_listA, bat_sen2cor):
for satellite in satellite_listA:
for year in year_listA:
txt_file_path = foder_path + satellite + year + ".txt"
with open(txt_file_path, "r+") as txt_file:
txt_first_line = next(txt_file)
lines = txt_file.readlines()
#index = 1
for line in lines:
if line != "":
cmd_args = [bat_sen2cor, txt_first_line.strip("\n") + "\\" + line.strip("\n")]
print("{} processing begin!".format(line.strip("\n")))
subprocess.call(cmd_args)
print("{} processing finished!\n".format(line.strip("\n")))
line = ""
files = open(txt_file_path, "w+")
files.write(txt_first_line)
line = files.writelines(line)
files.close()
readNameWriteInTxt是读取file_path 目录下所有文件夹,并将其保存在txt文件中。txt第一行是影像父文件夹的绝对路径,除第一行外是影像的文件名。.
AutomationAtmosphericCorrectionC是逐行读取之前创建的txt文件,并对影像进行校正,每校正一次影像就会删除一条影像的文件名,这样第一天没校正完的位置也会被记录,第二天再运行脚本即可继续校正。
bat_sen2cor是Sen2cor的绝对路径
楼主电脑是九代i5标压版,一般跑十一二个小时能校正完二十幅影像。
p.s2016及以前的sentinel影像的校正好像有问题,不能用05版sne2cor,目前搜到的时要使用10版的,不过楼主没试验过。