generated from linz/template-python-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new script to translate ascii files
also generalise existing functions for resuse fix: formatting feat: write output so doesn't get removed with tmpdir fix: needs credentials fix: remove unnecessary code fix: tidy and move to util folder fix: rebase fix fix: format
- Loading branch information
1 parent
2dfe650
commit e96c88a
Showing
6 changed files
with
103 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Util | ||
|
||
This folder contains utiliy scripts used to fix datasets, but are not expexted to be regularly required. | ||
|
||
## 1. translate_ascii.py | ||
|
||
10/08/2023 - [TDE-814](https://toitutewhenua.atlassian.net/browse/TDE-814?atlOrigin=eyJpIjoiMDRhODBiOTcyMTQ2NDM2NDk4ZjJkZmY5ODg3MDdlZDUiLCJwIjoiaiJ9) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import argparse | ||
import os | ||
import tempfile | ||
|
||
from scripts.aws.aws_helper import get_session | ||
from scripts.files.fs import download_files_parallel_multithreaded, read, write | ||
from scripts.files.fs_s3 import bucket_name_from_path, list_uri | ||
from scripts.gdal.gdal_helper import run_gdal | ||
|
||
|
||
def main() -> None: | ||
"""- Downloads all ascii files in a given s3 path, | ||
- Translates the files to tiffs, | ||
- Writes them to the specified target. | ||
Arguments: | ||
--source - s3 path to source data | ||
--target - local or s3 path to write converted tiffs | ||
example: | ||
python translate_ascii.py --source s3://bucket/input-path/ -- target s3://bucket/output-path/ | ||
""" | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--source", dest="source", required=True, help="S3 path to the input ascii files") | ||
parser.add_argument("--target", dest="target", required=True, help="Output location path") | ||
arguments = parser.parse_args() | ||
|
||
uri = arguments.source | ||
|
||
if not uri.startswith("s3://"): | ||
msg = f"uri is not a s3 path: {uri}" | ||
raise argparse.ArgumentTypeError(msg) | ||
|
||
# list ascii files | ||
s3 = get_session(uri).client("s3") | ||
files = list_uri(uri, s3, extension=".asc") | ||
# reformat output to full s3 path | ||
files = [f"s3://{bucket_name_from_path(uri)}/{file}" for file in files] | ||
|
||
# download ascii files | ||
with tempfile.TemporaryDirectory() as tmp_path: | ||
source_ascii = download_files_parallel_multithreaded(inputs=files, target=tmp_path, preserve_name=True) | ||
|
||
# translate from ascii to geotiff | ||
for asc in source_ascii: | ||
output = os.path.join(tmp_path, f"{asc.split('/')[-1].split('.')[0]}.tif") | ||
command = ["gdal_translate", "-of", "GTiff", asc, output] | ||
run_gdal(command) | ||
write(os.path.join(arguments.target, f"{asc.split('/')[-1].split('.')[0]}.tif"), read(output)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |