Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Posthuma committed Sep 3, 2020
1 parent 6238009 commit 6c7302c
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
140 changes: 140 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

*.txt
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# airborne-dsa-lite
Data shipping script for airborne products

## Getting started
* Make sure you have python 3 installed
* `pip install awscli boto3`
* `aws configure`
* Set the correct bucket name in `settings.py`

Run: `python main.py` or `intterra-airborne-dsa.bat`

Drop KMLs or tifs (*.tif and *.tiff) into this directory


1 change: 1 addition & 0 deletions intterra-airborne-dsa.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python main.py
135 changes: 135 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env python3

# README
# pip install awscli
# aws configure

import boto3
from datetime import datetime
import sys
import glob
import os
import time
import re
import settings
from signal import signal, SIGINT
s3 = boto3.client('s3')

def run():
# init
bucket = settings.BUCKET
now = datetime.utcnow()
dir_path = os.path.dirname(os.path.realpath(__file__))
welcome()

# Get current working directory (CWD) and mission name from args
mission_name, mission_timestamp, mission_file_path = find_mission()
if mission_name == None:
mission_name = get_mission_from_input()
create_mission(mission_name, bucket)
else:
answer = ''
while answer != 'yes' and answer != 'no':
print(f'Use current mission (yes/no): "{mission_name}" from {mission_timestamp.ctime()}?', end=' ')
answer = sys.stdin.readline().rstrip('\n').rstrip('\n')
if answer == 'no':
mission_name = get_mission_from_input()
os.remove(mission_file_path)
create_mission(mission_name, bucket)

# listen for files
print(f'Listening for files in mission "{mission_name}" at {now.ctime()} UTC in bucket "{bucket}"...')
print('(CTRL + C to exit)')
while True:
upload_kmls(mission_name, dir_path, bucket)
upload_tifs(mission_name, dir_path, bucket)
time.sleep(15)


def get_mission_from_input():
name = ''
while not re.match(r'^[0-9a-z]+$', name, re.IGNORECASE):
print('Please enter mission name (alphanumeric): ', end='')
name = sys.stdin.readline().rstrip('\n').rstrip('\n')
return name

def create_mission(mission_name, bucket):
# create mission
now = datetime.utcnow()
dir_path = os.path.dirname(os.path.realpath(__file__))
mission_file_name = f'{mission_name}_{now.strftime("%Y%m%d")}_{now.strftime("%H%M")}Z.txt'
mission_path = f'{dir_path}/{mission_file_name}'
with open(mission_path, 'w+') as f:
f.write('')
s3.upload_file(mission_path, bucket, f'MISSION/{mission_file_name}')
time.sleep(1)

def find_mission():
dir_path = os.path.dirname(os.path.realpath(__file__))
for file_path in glob.iglob(f'{dir_path}/*.txt', recursive=False):
file_name = os.path.basename(file_path)
m = re.match(r'([a-z0-9]+)_([0-9]{8})_([0-9]{4})z\.txt', file_name, re.IGNORECASE)
if m != None:
mission_timestamp = datetime.strptime(f'{m.group(2)}{m.group(3)}', '%Y%m%d%H%M')
return (m.group(1), mission_timestamp, file_name)
return None, None, None

def upload_kmls(mission_name, path, bucket):
# find KMLs (eg: 20200831_193612Z_Crawl1_IntenseHeat.kml)
for file_path in glob.iglob(f'{path}/*.kml', recursive=False):

now = datetime.utcnow()
new_obj = f'TACTICAL/{now.strftime("%Y%m%d")}_{now.strftime("%H%M%S")}Z_{mission_name}_IntenseHeat.kml'
print(f'Uploading {file_path} to {new_obj}...')

# Strip points with regex
with open(file_path, 'w+') as f:
contents = f.read()
modified_contents = re.sub(
r'<Point>[-.\n\t<>a-z0-9,\/]+<\/Point>', '', contents, flags=re.MULTILINE)
f.write(modified_contents)

s3.upload_file(file_path, bucket, new_obj)
os.remove(file_path)
print('done!')
time.sleep(1)


def upload_tifs(mission_name, path, bucket):
# find IRs (eg: 20200818_031612Z_Crawl1_IRimage.tif)
for file_path in glob.iglob(f'{path}/*.tif', recursive=False):
now = datetime.utcnow()
new_obj = f'IMAGERY/{now.strftime("%Y%m%d")}_{now.strftime("%H%M%S")}Z_{mission_name}_IRimage.tif'
print(f'Uploading {file_path} to {new_obj}...')
s3.upload_file(file_path, bucket, new_obj)
os.remove(file_path)
print('done!')
time.sleep(1)

for file_path in glob.iglob(f'{path}/*.tiff', recursive=False):
now = datetime.utcnow()
new_obj = f'IMAGERY/{now.strftime("%Y%m%d")}_{now.strftime("%H%M%S")}Z_{mission_name}_IRimage.tif'
print(f'Uploading {file_path} to {new_obj}...')
s3.upload_file(file_path, bucket, new_obj)
os.remove(file_path)
print('done!')
time.sleep(1)

def welcome():
print(" _____ .__ ___. ________ _________ _____ ")
print(" / _ \\ |__|_____\\_ |__ ___________ ____ ____ \\______ \\ / _____/ / _ \\ ")
print(" / /_\\ \\| \\_ __ \\ __ \\ / _ \\_ __ \\/ \\_/ __ \\ | | \\ \\_____ \\ / /_\\ \\ ")
print("/ | \\ || | \\/ \\_\\ ( <_> ) | \\/ | \\ ___/ | ` \\/ \\/ | \\")
print("\\____|__ /__||__| |___ /\\____/|__| |___| /\\___ > /_______ /_______ /\\____|__ /")
print(" \\/ \\/ \\/ \\/ \\/ \\/ \\/ ")
print("Airborne API Data Shipping App powered by Intterra")
print('\n')


def handler(signal_received, frame):
print('Goodbye!')
sys.exit(0)

if __name__ == "__main__":
signal(SIGINT, handler)
run()
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BUCKET = "intterrademoinputs"

0 comments on commit 6c7302c

Please sign in to comment.