diff --git a/.github/workflows/testing_checks.yaml b/.github/workflows/testing_checks.yaml index ae782a7..6efbffe 100644 --- a/.github/workflows/testing_checks.yaml +++ b/.github/workflows/testing_checks.yaml @@ -41,8 +41,6 @@ jobs: run: python3 -m pip install --upgrade --no-cache-dir pip - name: Fetch/update setuptools run: python3 -m pip install --upgrade --no-cache-dir setuptools - - name: Install python-apt - run: sudo apt-get install -y python-apt - name: HACK to fix apt-get update problem w/ different python versions run: 'cd /usr/lib/python3/dist-packages && sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so' - name: Update apt-get diff --git a/README.md b/README.md index 7dd300a..94783a7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +[![license](https://img.shields.io/badge/license-BSD%203-green?logo=Open-Source-Initiative)](https://github.com/AgPipeline/transformer-canopycover/blob/master/LICENSE) + +[![Enforcing testing](https://github.com/AgPipeline/transformer-canopycover/workflows/Enforcing%20tests/badge.svg)](https://github.com/AgPipeline/transformer-canopycover/actions?query=workflow%3A%22Enforcing+tests%22) +[![testing](https://github.com/AgPipeline/transformer-canopycover/workflows/Testing%20Docker%20image/badge.svg)](https://github.com/AgPipeline/transformer-canopycover/actions?query=workflow%3A%22Testing+Docker+image%22) + # Transformer Canopy Cover Calculates canopy cover (the percentage pixels identified as a plant) on a plot level for one or more images that have been processed by the [soilmask transformer](https://github.com/AgPipeline/transformer-soilmask) to mask the soil. diff --git a/canopycover.py b/canopycover.py index 9b04e08..4fd327b 100755 --- a/canopycover.py +++ b/canopycover.py @@ -4,13 +4,13 @@ import argparse import copy +import datetime import logging import os import subprocess import tempfile from typing import Union import numpy as np -import dateutil.parser from agpypeline import entrypoint, algorithm, geoimage from agpypeline.environment import Environment from osgeo import gdal, ogr @@ -249,6 +249,24 @@ def get_plot_species(plot_name: str, full_md: list, args: argparse.Namespace) -> return args.species if args.species is not None else optional if optional is not None else "Unknown" +def get_time_stamps(iso_timestamp: str, args: argparse.Namespace) -> list: + """Returns the date and the local time (offset is stripped) derived from the passed in timestamp + Args: + iso_timestamp: the timestamp string + args: the command line parameters + Return: + A list consisting of the date (YYYY-MM-DD) and a local timestamp (YYYY-MM-DDTHH:MM:SS) + """ + if 'timestamp' in args and args.timestamp: + timestamp = datetime.datetime.fromisoformat(args.timestamp) + elif iso_timestamp: + timestamp = datetime.datetime.fromisoformat(iso_timestamp) + else: + return ['', ''] + + return [timestamp.strftime('%Y-%m-%d'), timestamp.strftime('%Y-%m-%dT%H:%M:%S')] + + class CanopyCover(algorithm.Algorithm): """Calculates canopy cover percentage on soil-masked image""" @@ -265,6 +283,7 @@ def add_parameters(self, parser: argparse.ArgumentParser) -> None: # pylint: disable=no-self-use parser.add_argument('--species', dest="species", type=str, nargs='?', help="name of the species associated with the canopy cover") + parser.add_argument('--timestamp', help='the timestamp to use in ISO 8601 format (eg:YYYY-MM-DDTHH:MM:SS') def check_continue(self, environment: Environment, check_md: dict, transformer_md: list, full_md: list) -> tuple: """Checks if conditions are right for continuing processing @@ -309,11 +328,7 @@ def perform_process(self, environment: Environment, check_md: dict, transformer_ # Disable pylint checks that would reduce readability # pylint: disable=unused-argument,too-many-locals,too-many-branches,too-many-statements # Setup local variables - localtime = "" - if check_md['timestamp']: - timestamp = dateutil.parser.parse(check_md['timestamp']) - if timestamp: - localtime = timestamp.strftime("%Y-%m-%dT%H:%M:%S") + (_, localtime) = get_time_stamps(check_md['timestamp'], environment.args) save_csv_filename = os.path.join(check_md['working_folder'], "canopycover.csv") save_file = open(save_csv_filename, 'w')