Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
lucadelu committed Mar 21, 2024
1 parent db01910 commit 8dd5bc7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 92 deletions.
44 changes: 11 additions & 33 deletions sadasadam/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,13 @@ def main():
east = config.get("east")
west = config.get("west")
if not north or not south or not east or not west:
raise Exception(
"Please provide a bounding box for your area of interest"
)
raise Exception("Please provide a bounding box for your area of interest")
start = config.get("start")
if not start:
raise Exception(
"Please provide a start date for the temporal extent"
)
raise Exception("Please provide a start date for the temporal extent")
end = config.get("end")
if not end:
raise Exception(
"Please provide an end date for the temporal extent"
)
raise Exception("Please provide an end date for the temporal extent")
cloud_cover = config.get("cloud_cover")
if not cloud_cover:
raise Exception("Please provide a maximum cloud cover")
Expand All @@ -94,20 +88,14 @@ def main():
download_dir = os.path.join(output_dir, "download")
if not os.path.exists(download_dir):
os.mkdir(download_dir)
print(
"A download directory will be created "
"under the output directory"
)
print("A download directory will be created " "under the output directory")
temp_force_dir = config.get("temp_force_dir")
if not temp_force_dir:
# create a temporary directory under the output directory
temp_force_dir = os.path.join(output_dir, "temp")
if not os.path.exists(temp_force_dir):
os.mkdir(temp_force_dir)
print(
"A temporary directory will be created "
"under the output directory"
)
print("A temporary directory will be created " "under the output directory")
wvdb_dir = config.get("wvdb_dir")
if not wvdb_dir:
raise Exception("Please provide a path to the wvdb directory")
Expand Down Expand Up @@ -155,15 +143,13 @@ def main():
cloud_buffer = config.get("cloud_buffer")
if not cloud_buffer:
raise Exception(
"Please provide a cloud buffer, "
"or define a force_param_file"
"Please provide a cloud buffer, " "or define a force_param_file"
)

n_procs_postprocessing = config.get("n_procs_postprocessing")
if not n_procs_postprocessing:
raise Exception(
"Please provide the number of "
"processes to use for postprocessing"
"Please provide the number of " "processes to use for postprocessing"
)

save_qai = config.get("save_qai")
Expand All @@ -190,8 +176,7 @@ def main():
check_bool(force_only)
if force_only:
print(
"Process will skip downloading "
"and only run FORCE and postprocessing"
"Process will skip downloading " "and only run FORCE and postprocessing"
)
indices_only = config.get("indices_only")
check_bool(indices_only)
Expand All @@ -207,10 +192,7 @@ def main():
indices_dir = os.path.join(output_dir, "indices")
if not os.path.exists(indices_dir):
os.mkdir(indices_dir)
print(
"A indices directory will be created "
"under the output directory"
)
print("A indices directory will be created " "under the output directory")
indices_list = config.get("indices_list")
if force_only is True and download_only is True:
raise Exception(
Expand Down Expand Up @@ -243,9 +225,7 @@ def main():
if download_only is False and indices_only is False:
print("Setting up FORCE processing...")
# start FORCE process
force_proc = ForceProcess(
temp_dir=temp_force_dir, level1_dir=download_dir
)
force_proc = ForceProcess(temp_dir=temp_force_dir, level1_dir=download_dir)
force_proc.setup_wvdb(target_dir=wvdb_dir)
force_proc.create_force_queue_file()
if use_param_file is False:
Expand Down Expand Up @@ -285,9 +265,7 @@ def main():
if download_only is False and force_only is False:
for index in indices_list:
indices_proc = CreateIndices(
indir=output_dir,
outdir=indices_dir,
index=index
indir=output_dir, outdir=indices_dir, index=index
)
indices_proc.calculate(n_procs=n_procs_postprocessing)

Expand Down
4 changes: 1 addition & 3 deletions sadasadam/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def extract_and_delete_tar_gz_files(directory):
if file.endswith((".SAFE.zip", ".tar.gz", ".SAFE")):
file_path = os.path.join(directory, file)
warning_text = (
"Warning: - "
f"Unable to extract: {file_path}. "
"Retrying Download..."
"Warning: - " f"Unable to extract: {file_path}. " "Retrying Download..."
)
landsat_extract_dir = None
remove = True
Expand Down
50 changes: 12 additions & 38 deletions sadasadam/force.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def __init__(self, temp_dir, level1_dir=None, wvdb_dir=None):
"""
now_time = datetime.now()
now_time_str = now_time.strftime("%Y%m%d_%H%M%S")
self.force_dir = makedirs(
os.path.join(temp_dir, f"force_dir_{now_time_str}")
)
self.force_dir = makedirs(os.path.join(temp_dir, f"force_dir_{now_time_str}"))
if not level1_dir:
self.level1_dir = makedirs(os.path.join(self.force_dir, "level1"))
else:
Expand All @@ -120,19 +118,15 @@ def __init__(self, temp_dir, level1_dir=None, wvdb_dir=None):
self.log_dir = makedirs(os.path.join(self.force_dir, "log"))
self.misc_dir = makedirs(os.path.join(self.force_dir, "misc"))
self.param_dir = makedirs(os.path.join(self.force_dir, "param"))
self.provenance_dir = makedirs(
os.path.join(self.force_dir, "provenance")
)
self.provenance_dir = makedirs(os.path.join(self.force_dir, "provenance"))
self.temp_dir = makedirs(os.path.join(self.force_dir, "temp_dir"))
self.wvdb_dir = wvdb_dir
self.queue_file = None
self.config_file = None
# the mosaic tool just takes a relative path as
# input so both attributes are stored
self.mosaic_dir_name = "mosaic"
self.mosaic_path = makedirs(
os.path.join(self.level2_dir, self.mosaic_dir_name)
)
self.mosaic_path = makedirs(os.path.join(self.level2_dir, self.mosaic_dir_name))

def create_force_queue_file(self):
"""Creates a queue file needed for L2 processing for
Expand All @@ -149,15 +143,11 @@ def create_force_queue_file(self):
file.writelines(lines_per_string)
self.queue_file = queue_file

def __replace_in_config_file(
self, old_config_file, new_config_file, replace_lines
):
def __replace_in_config_file(self, old_config_file, new_config_file, replace_lines):
"""Replaces lines in a FORCE config file"""
replace_lines_keys = [item.split("= ")[0] for item in replace_lines]
# create dict
replace_dict = dict(
map(lambda i, j: (i, j), replace_lines_keys, replace_lines)
)
replace_dict = dict(map(lambda i, j: (i, j), replace_lines_keys, replace_lines))
# open file and replace lines
updated_conf = ""
with open(old_config_file, "r") as file:
Expand Down Expand Up @@ -216,9 +206,7 @@ def create_force_level2_config_file(
replace_lines=replace_lines,
)

def update_force_level2_config_file(
self, user_file, target_proj_epsg=None
):
def update_force_level2_config_file(self, user_file, target_proj_epsg=None):
"""Updates a config file needed for L2 processing.
It only updates the config file with the
FORCE internal directories, the wkt for the projection string,
Expand Down Expand Up @@ -246,10 +234,7 @@ def download_wvdb(self, target_dir):
"""Downloads the Water Vapor Database 2000-2020 for
Landsat atmospheric correction.
"""
url = (
"https://zenodo.org/records/4468701/"
"files/wvp-global.tar.gz?download=1"
)
url = "https://zenodo.org/records/4468701/" "files/wvp-global.tar.gz?download=1"
response = requests.get(url)
if response.status_code != 200:
raise Exception(
Expand All @@ -266,10 +251,7 @@ def download_wvdb(self, target_dir):
tfile.extractall(target_dir)
os.remove(target_file_path)
self.wvdb_dir = target_dir
print(
"Water Vapor Database downloaded and "
f"extracted to {target_dir}"
)
print("Water Vapor Database downloaded and " f"extracted to {target_dir}")

def setup_wvdb(self, target_dir):
"""Downloads the Water Vapor Database 2000-2020 for Landsat
Expand Down Expand Up @@ -370,9 +352,7 @@ def postprocess(
for file in files_in_mosaic_dir:
if file.endswith("BOA.vrt") or file.endswith("QAI.vrt"):
outfile_name_tmp = file.replace("BOA.vrt", "BOA_clipped.vrt")
outfile_name = outfile_name_tmp.replace(
"QAI.vrt", "QAI_clipped.vrt"
)
outfile_name = outfile_name_tmp.replace("QAI.vrt", "QAI_clipped.vrt")
file_list = [
os.path.join(self.mosaic_path, file),
os.path.join(self.mosaic_path, outfile_name),
Expand Down Expand Up @@ -419,9 +399,7 @@ def postprocess(
]
clearsky_cmd_list.append(clearsky_cmd)
clearsky_files.append(clearsky_name)
run_subprocess_parallel(
cmd_list_list=clearsky_cmd_list, num_processes=n_procs
)
run_subprocess_parallel(cmd_list_list=clearsky_cmd_list, num_processes=n_procs)

# then: use the binary clearsky map to mask out
# non-clearsky areas in the BOA.tif
Expand All @@ -438,9 +416,7 @@ def postprocess(
# clearsky files (cloud masks) are required
# in the output as well
shutil.copy(clearsky_file, target_dir)
boa_file = clearsky_file.replace(
"clearsky.tif", "BOA_clipped.vrt"
)
boa_file = clearsky_file.replace("clearsky.tif", "BOA_clipped.vrt")
out_boa_filename = os.path.basename(boa_file).replace(
"BOA_clipped.vrt", "BOA_clearsky.tif"
)
Expand All @@ -466,9 +442,7 @@ def postprocess(
# (need to be transformed from .vrt to Gtiff)
if save_qai is True:
qai_file = clipped_qai_files[i]
qai_output_name = os.path.basename(qai_file).replace(
".vrt", ".tif"
)
qai_output_name = os.path.basename(qai_file).replace(".vrt", ".tif")
qai_output_file = os.path.join(target_dir, qai_output_name)
ds_gdal = gdal.Open(qai_file)
ds_gdal = gdal.Translate(
Expand Down
42 changes: 24 additions & 18 deletions sadasadam/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@
from sadasadam.force import run_subprocess_parallel

INDICES = {
"NDVI": {"name": "Normalized Difference Vegetation Index",
"formula": "((A.astype(float)-B)/(A.astype(float)+B))",
"Sentinel-2": {"A": 4, "B": 8},
"LANDSAT": {"A": 3, "B": 5}},
"NDSI": {"name": "Normalized Difference Snow Index",
"formula": "((A.astype(float)-B)/(A.astype(float)+B))",
"Sentinel-2": {"A": 3, "B": 11},
"LANDSAT": {"A": 4, "B": 6}},
"NDMI": {"name": "Normalized Difference Moisture Index",
"formula": {"((A.astype(float)-B)/(A.astype(float)+B))"},
"Sentinel-2": {"A": 8, "B": 11},
"LANDSAT": {"A": 5, "B": 6}}
"NDVI": {
"name": "Normalized Difference Vegetation Index",
"formula": "((A.astype(float)-B)/(A.astype(float)+B))",
"Sentinel-2": {"A": 4, "B": 8},
"LANDSAT": {"A": 3, "B": 5},
},
"NDSI": {
"name": "Normalized Difference Snow Index",
"formula": "((A.astype(float)-B)/(A.astype(float)+B))",
"Sentinel-2": {"A": 3, "B": 11},
"LANDSAT": {"A": 4, "B": 6},
},
"NDMI": {
"name": "Normalized Difference Moisture Index",
"formula": {"((A.astype(float)-B)/(A.astype(float)+B))"},
"Sentinel-2": {"A": 8, "B": 11},
"LANDSAT": {"A": 5, "B": 6},
},
}

class CreateIndices(object):

class CreateIndices(object):
def __init__(self, indir, outdir, index="NDVI"):
"""Create folder to store indices
Expand All @@ -51,7 +57,9 @@ def __init__(self, indir, outdir, index="NDVI"):
self.indir = indir
self.outdir = makedirs(outdir)
if index not in INDICES.keys():
raise Exception(f"Index {index} not available in the list of supported indices")
raise Exception(
f"Index {index} not available in the list of supported indices"
)
else:
self.index = index

Expand All @@ -63,7 +71,7 @@ def calculate(self, n_procs=1):
files_to_process.append(file)
indices_cmd_list = []
index = INDICES[self.index]
formula = index['formula']
formula = index["formula"]
for file in files_to_process:
if "SEN2" in file:
bands = INDICES[self.index]["Sentinel-2"]
Expand All @@ -90,6 +98,4 @@ def calculate(self, n_procs=1):
]
print(" ".join(index_calc))
indices_cmd_list.append(index_calc)
run_subprocess_parallel(
cmd_list_list=indices_cmd_list, num_processes=n_procs
)
run_subprocess_parallel(cmd_list_list=indices_cmd_list, num_processes=n_procs)

0 comments on commit 8dd5bc7

Please sign in to comment.