Commit dd5ee94a authored by Mario Chirinos Colunga's avatar Mario Chirinos Colunga 💬

L3A

parent 6f4b168c
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import gdal
import numpy as np
from collections import Counter
CLOUDS = [8,9,10]
#-------------------------------------------------------------------------------
def imageCountSCLData(filename, classlist):
inputImage = gdal.Open(filename)
rows, cols, geotransform = inputImage.RasterYSize, inputImage.RasterXSize, inputImage.GetGeoTransform()
data = inputImage.GetRasterBand(1).ReadAsArray(0,0,cols,rows)
inputImage = None
return (np.isin(data,classlist)>0).sum()/float((rows*cols))
#-------------------------------------------------------------------------------
def getNewSCLpixel(row, col, fileList):
clist = [ filelist[i]["SCL"][row,col] for i in range(0,len(fileList)) ]
newClass = Counter(clist).most_common(1)[0]
for i in range(len(fileList):
fileList
return (newClass, newPixel)
#-------------------------------------------------------------------------------
def L2AtoL3A(directory):
os.chdir(directory+"SCL/")
files = {f:{"path":os.path.abspath(f)} for f in os.listdir(".")}
print(files)
for f in files:
files[f]["score"]=imageCountSCLData(files[f]["path"], CLOUDS)
filesList = sorted(files.items(), key=lambda (k,v): v["score"], reverse=False)
for f in files:
inputImage = gdal.Open(files[f]["path"])
rows, cols, geotransform = inputImage.RasterYSize, inputImage.RasterXSize, inputImage.GetGeoTransform()
files[f]["SCL"] = inputImage.GetRasterBand(1).ReadAsArray(0,0,cols,rows)
inputImage = None
tci = files[f]["path"].replace("SCL", "TCI")
inputImage = gdal.Open(tci)
rows, cols, geotransform = inputImage.RasterYSize, inputImage.RasterXSize, inputImage.GetGeoTransform()
files[f]["TCI"] = [inputImage.GetRasterBand(1).ReadAsArray(0,0,cols,rows), inputImage.GetRasterBand(2).ReadAsArray(0,0,cols,rows), inputImage.GetRasterBand(3).ReadAsArray(0,0,cols,rows)]
inputImage = None
newSCL = files[filesList[0][0]]["SCL"]
newTCI = files[filesList[0][0]]["TCI"]
newMASK = np.isin(newSCL, CLOUDS)
for row in range(len(newSCL)):
for col in range(len(row)):
if newMASK[row][col]:
newSCL[row][col]=
print("file", filesList)
#-------------------------------------------------------------------------------
def main(argv):
if len(sys.argv) != 2:
print("Usage: " + argv[0] + "<directory>")
else:
L2AtoL3A(argv[1])
#-------------------------------------------------------------------------------
if __name__ == "__main__":
main(sys.argv)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment