Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the check_landsat_orphans script #151

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions scripts/misc/check_landsat_orphans.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Read latest landsat gap reports(i.e. Landsat 5, Landsat 7 and Landsat 8),
check orphan scenes and generate report for cleanup
"""

import json
from datetime import datetime
from odc.aws import s3_client, s3_fetch, s3_ls, s3_dump, s3_ls_dir
import re

DEAFRICA_AWS_REGION = "af-south-1"
DEAFRICA_LANDSAT_BUCKET_NAME = "deafrica-landsat"
Expand All @@ -19,6 +21,12 @@
PUBLISH_TO_S3 = True


def find_date(text):
date_pattern = re.compile(r"(\d{4}-\d{2}-\d{2})")
date = re.search(date_pattern, text).group(0)
return date


def get_orphans():
s3 = s3_client(region_name=DEAFRICA_AWS_REGION)

Expand All @@ -30,15 +38,30 @@ def get_orphans():

# fetch the latest report: Landsat 5, Landsat 7 and Landsat 8
report_files_json.sort()
landsat_8_report = [
report_file for report_file in report_files_json if "landsat_8" in report_file
][-1]
landsat_7_report = [
report_file for report_file in report_files_json if "landsat_7" in report_file
][-1]
landsat_5_report = [
report_file for report_file in report_files_json if "landsat_5" in report_file
][-1]
landsat_8_report = sorted(
[
report_file
for report_file in report_files_json
if "landsat_8" in report_file.lower()
],
key=lambda x: find_date(x),
)[-1]
landsat_7_report = sorted(
[
report_file
for report_file in report_files_json
if "landsat_7" in report_file.lower()
],
key=lambda x: find_date(x),
)[-1]
landsat_5_report = sorted(
[
report_file
for report_file in report_files_json
if "landsat_5" in report_file.lower()
],
key=lambda x: find_date(x),
)[-1]

# collect orphan paths
list_orphan_paths = []
Expand Down Expand Up @@ -75,12 +98,12 @@ def publish_to_s3(data: list, output_filename: str, content_type: str = "text/pl
s3 = s3_client(region_name=DEAFRICA_AWS_REGION)
s3_dump(
data=data,
url=str(DEAFRICA_ORPHAN_REPORT_S3_PATH / output_filename),
url=str(DEAFRICA_ORPHAN_REPORT_S3_PATH + output_filename),
s3=s3,
ContentType=content_type,
)
print(
f"Report can be accessed from {DEAFRICA_ORPHAN_REPORT_S3_PATH / output_filename}"
f"Report can be accessed from {DEAFRICA_ORPHAN_REPORT_S3_PATH + output_filename}"
)


Expand Down
Loading