mirror of
https://github.com/noerw/sentinel_fire
synced 2025-03-12 18:00:28 +01:00
73 lines
2.9 KiB
Python
Executable file
73 lines
2.9 KiB
Python
Executable file
#!/usr/bin/env python2
|
|
|
|
import argparse
|
|
from os import path
|
|
from subprocess import Popen, PIPE
|
|
import logging
|
|
import sys
|
|
|
|
logger = logging.getLogger()
|
|
logger.setLevel(logging.WARN)
|
|
logger.addHandler(logging.StreamHandler())
|
|
|
|
# argparse
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("-i", "--input", nargs="+", help="input S2 L2A orbit files", required = True)
|
|
parser.add_argument("-o", "--outputdir", help="Output folder", required = True)
|
|
parser.add_argument("-p", "--process", help="Select single algorithm. Possible is 'dnbr' or 'bais2'.", default=["bais2", "dnbr"])
|
|
args = parser.parse_args()
|
|
|
|
in_files = args.input
|
|
in_files.sort()
|
|
out_dir = path.abspath(args.outputdir)
|
|
scriptdir = path.abspath(path.dirname(__file__))
|
|
|
|
processes_available = [
|
|
# 0 ID 1 executable 2 resultsuffix 3 num of required input files
|
|
("bais2", path.abspath(path.join(scriptdir, "..", "changeDetection", "BAIS2.R")), "_bais2.tif", 1),
|
|
("dnbr", path.abspath(path.join(scriptdir, "..", "changeDetection", "dNBR.R")), "_dnbr.tif", 2),
|
|
]
|
|
|
|
processes = [p for p in processes_available if p[0] in args.process]
|
|
if not len(processes):
|
|
logger.critical("Invalid process. Only {} are allowed.".format([p[0] for p in processes_available]))
|
|
sys.exit()
|
|
|
|
for i in range(0, len(in_files)):
|
|
for process_id, script, suffix, inputs_required in processes:
|
|
if len(in_files) < inputs_required:
|
|
logger.warn("Not enough input files for {}; skipping!".format(process_id))
|
|
continue
|
|
|
|
fname = path.basename(in_files[i]).replace('.tif', suffix)
|
|
outfile = path.join(out_dir, fname)
|
|
infile = path.abspath(in_files[i])
|
|
# check if output file already exists. If so, skip loop
|
|
if path.isfile(outfile):
|
|
logger.warn("{} already exists. Skipping {}.".format(outfile, process_id))
|
|
continue
|
|
|
|
cmd = "Rscript --vanilla {} {}".format(script, outfile)
|
|
|
|
# adding input files to cmd
|
|
restfiles = len(in_files) - i
|
|
if (restfiles >= inputs_required):
|
|
print "Running {} with:".format(process_id)
|
|
for j in range(0, inputs_required):
|
|
cmd += " " + path.abspath(in_files[i+j])
|
|
print " {}".format(in_files[i+j])
|
|
print " Saving to {}".format(outfile)
|
|
else:
|
|
print "Not enough input files for {}; skipping!".format(process_id)
|
|
continue
|
|
|
|
# TODO
|
|
# temporary solution until default threshold value is fixed in dNBR.R
|
|
if process_id == "dnbr":
|
|
cmd += " FALSE"
|
|
out = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True, cwd=path.dirname(script))
|
|
stdout, stderr = out.communicate()
|
|
logger.warn(stdout) # DEBUG: rscript also puts errors to stdout
|
|
if stderr:
|
|
logger.critical(stderr)
|
|
#sys.exit(1)
|