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

working plugin that returns cat.jpg for all inputs #3

Merged
merged 11 commits into from
Mar 11, 2024
2 changes: 2 additions & 0 deletions .github/workflows/test-and-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
jobs:
call-version-info-workflow:
# Docs: https://github.com/ASFHyP3/actions
with:
python_version: 3.12
uses: ASFHyP3/actions/.github/workflows/[email protected]

call-docker-ghcr-workflow:
Expand Down
34 changes: 9 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/osgeo/gdal:alpine-small-latest
FROM mambaorg/micromamba:latest

# For opencontainers label definitions, see:
# https://github.com/opencontainers/image-spec/blob/master/annotations.md
Expand All @@ -9,32 +9,16 @@ LABEL org.opencontainers.image.authors="tools-bot <[email protected]>"
LABEL org.opencontainers.image.licenses="BSD-3-Clause"
LABEL org.opencontainers.image.url="https://github.com/ASFHyP3/harmony-opera-rtc-reproject"
LABEL org.opencontainers.image.source="https://github.com/ASFHyP3/harmony-opera-rtc-reproject"
LABEL org.opencontainers.image.documentation="https://hyp3-docs.asf.alaska.edu"
LABEL org.opencontainers.image.documentation="https://harmony.earthdata.nasa.gov/"

ARG DEBIAN_FRONTEND=noninteractive
ENV PYTHONDONTWRITEBYTECODE=true
WORKDIR /home/mambauser

RUN apk add bash build-base gcc g++ gfortran openblas-dev cmake python3 python3-dev libffi-dev netcdf-dev libxml2-dev libxslt-dev libjpeg-turbo-dev zlib-dev hdf5 hdf5-dev gdal-dev gdal-tools
COPY --chown=$MAMBA_USER:$MAMBA_USER environment.yml /tmp/environment.yml

RUN python -m venv /opt/venv
RUN micromamba install -y -n base -f /tmp/environment.yml && \
micromamba clean --all --yes

ENV PATH="/opt/venv/bin:$PATH"
COPY --chown=$MAMBA_USER:$MAMBA_USER opera-rtc-reproject.py /home/mambauser/opera-rtc-reproject.py
COPY --chown=$MAMBA_USER:$MAMBA_USER cat.jpg /home/mambauser/cat.jpg

RUN python -m ensurepip --upgrade
RUN pip3 install gdal numpy netCDF4 matplotlib harmony-service-lib

# Create a new user
RUN adduser -D -s /bin/sh -h /home/dockeruser -g "" -u 1000 dockeruser
USER dockeruser
ENV HOME /home/dockeruser

USER root
RUN mkdir -p /worker && chown dockeruser /worker
USER dockeruser
WORKDIR /worker

COPY --chown=dockeruser opera-rtc-reproject.py .

# Run the service
ENTRYPOINT ["python3", "-m", "opera-rtc-reproject"]
CMD ["-h"]
ENTRYPOINT ["/usr/local/bin/_entrypoint.sh", "python", "-m", "opera-rtc-reproject"]
Binary file added cat.jpg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the high-res here.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 6 additions & 14 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
name: harmony-opera-rtc-reproject
name: opera-rtc-reproject
channels:
- conda-forge
- nodefaults
dependencies:
- python>=3.8
- python>=3.12
- pip
# For packaging, and testing
- flake8
- flake8-import-order
- flake8-blind-except
- flake8-builtins
- setuptools
- gdal
- pystac
- setuptools_scm
- wheel
- pytest
- pytest-console-scripts
- pytest-cov
# For running
- hyp3lib>=3,<4
- pip:
- harmony-service-lib
66 changes: 10 additions & 56 deletions opera-rtc-reproject.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
"""
==================
example_service.py
==================

An example service adapter implementation and example CLI parser
"""

import argparse
import shutil
import os
from tempfile import mkdtemp
from pystac import Asset

import harmony
from harmony.util import generate_output_filename, stage, download
import pystac
from harmony.util import stage

class ExampleAdapter(harmony.BaseHarmonyAdapter):
"""
Shows an example of what a service adapter implementation looks like
"""
def process_item(self, item, source):
"""
Processes a single input item. Services that are not aggregating multiple input files
should prefer to implement this method rather than #invoke

This example copies its input to the output, marking "dpi" and "variables" message
attributes as having been processed
def process_item(self, item: pystac.Item, source) -> pystac.Item:
"""
Processes a single input item.

Parameters
----------
Expand All @@ -42,40 +25,11 @@ def process_item(self, item, source):
result = item.clone()
result.assets = {}

# Create a temporary dir for processing we may do
workdir = mkdtemp()
try:
# Get the data file
asset = next(v for k, v in item.assets.items() if 'data' in (v.roles or []))
input_filename = download(asset.href, workdir, logger=self.logger, access_token=self.message.accessToken)

# Mark any fields the service processes so later services do not repeat work
dpi = self.message.format.process('dpi')
# Variable subsetting
variables = source.process('variables')

# Do the work here!
var_names = [v.name for v in variables]
print('Processing item %s, vars=[%s]' % (item.id, ', '.join(var_names)))
working_filename = os.path.join(workdir, 'tmp.txt')
shutil.copyfile(input_filename, working_filename)

# Stage the output file with a conventional filename
output_filename = generate_output_filename(asset.href, ext=None, variable_subset=None,
is_regridded=False, is_subsetted=False)
url = stage(working_filename, output_filename, 'text/plain', location=self.message.stagingLocation,
logger=self.logger)

# Update the STAC record
result.assets['data'] = Asset(url, title=output_filename, media_type='text/plain', roles=['data'])
# Other metadata updates may be appropriate, such as result.bbox and result.geometry
# if a spatial subset was performed

# Return the STAC record
return result
finally:
# Clean up any intermediate resources
shutil.rmtree(workdir)
filename = 'cat.jpg'
mimetype = 'image/jpeg'
url = stage(filename, filename, mime=mimetype, location=self.message.stagingLocation, logger=self.logger)
result.assets['data'] = pystac.Asset(url, title=filename, media_type=mimetype, roles=['data'])
return result


def run_cli(args):
Expand Down
Loading