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

ticket56 - add ProductCounter #68

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/opera/pge/base_pge.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def _core_filename(self, inter_filename=None):

The core file name component of the Base PGE consists of:

<PROJECT>_<LEVEL>_<PGE NAME>_<TIMETAG>
<PROJECT>_<LEVEL>_<PGE NAME>_<TIMETAG>_<PRODUCT_COUNTER>

Callers of this function are responsible for assignment of any other
product-specific fields, such as the file extension.
Expand All @@ -297,7 +297,8 @@ def _core_filename(self, inter_filename=None):
"""
time_tag = get_time_for_filename(self.production_datetime)

return f"{self.PROJECT}_{self.LEVEL}_{self.NAME}_{time_tag}"
return f"{self.PROJECT}_{self.LEVEL}_{self.NAME}_" \
f"{time_tag}_{str(self.runconfig.product_counter).zfill(3)}"

def _geotiff_filename(self, inter_filename):
"""
Expand Down Expand Up @@ -379,7 +380,6 @@ def _assign_filename(self, input_filepath, output_dir):

"""
file_extension = splitext(input_filepath)[-1]

# Lookup the specific rename function configured for the current file extension
try:
rename_function = self.rename_by_extension_map[file_extension]
Expand Down
7 changes: 4 additions & 3 deletions src/opera/pge/dswx_pge.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _core_filename(self, inter_filename=None):

The core file name component of the DSWx PGE consists of:

<PROJECT>_<LEVEL>_<PGE NAME>_<SOURCE>_<SENSOR>_<TILE ID>_<TIMETAG>_<PRODUCT VERSION>
<PROJECT>_<LEVEL>_<PGE NAME>_<SOURCE>_<SPACECRAFT_NAME>_<TILE ID>_<TIMETAG>_<PRODUCT VERSION>_<PRODUCT_COUNTER>

Callers of this function are responsible for assignment of any other
product-specific fields, such as the file extension.
Expand Down Expand Up @@ -180,15 +180,16 @@ def _core_filename(self, inter_filename=None):
dataset_fields = get_hls_filename_fields(f"{dataset}.BAND.tif")
collinss-jpl marked this conversation as resolved.
Show resolved Hide resolved

source = dataset_fields['product']
sensor = get_geotiff_spacecraft_name(inter_filename)
spacecraft_name = get_geotiff_spacecraft_name(inter_filename)
tile_id = dataset_fields['tile_id']
timetag = dataset_fields['acquisition_time']
version = dataset_fields['collection_version']
subversion = dataset_fields['sub_version']

# Assign the core file to the cached class attribute
self._cached_core_filename = (
f"{self.PROJECT}_{self.LEVEL}_{self.NAME}_{source}_{sensor}_{tile_id}_{timetag}_{version}.{subversion}"
f"{self.PROJECT}_{self.LEVEL}_{self.NAME}_{source}_{spacecraft_name}_"
f"{tile_id}_{timetag}_{version}.{subversion}_{str(self.runconfig.product_counter).zfill(3)}"
)

return self._cached_core_filename
Expand Down
21 changes: 15 additions & 6 deletions src/opera/test/pge/test_base_pge.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Unit tests for the pge/base_pge.py module.
"""
import os
import re
import tempfile
import unittest
from io import StringIO
Expand All @@ -35,6 +36,9 @@
class BasePgeTestCase(unittest.TestCase):
"""Base test class using unittest"""

test_dir = None
starting_dir = None

@classmethod
def setUpClass(cls) -> None:
"""Set up class method: set up directories for testing"""
Expand Down Expand Up @@ -173,12 +177,6 @@ def test_base_pge_w_failing_sas(self):

self.assertIn(f"failed with exit code {expected_error_code}", log_contents)

def test_geotiff_filename(self):
"""Test _geotiff_filename() method"""
runconfig_path = join(self.data_dir, 'test_sas_qa_config.yaml')
pge = PgeExecutor(pge_name='PgeQATest', runconfig_path=runconfig_path)
pge._geotiff_filename("TestName.ext")

def test_sas_qa_execution(self):
"""
Test execution of the PgeExecutor class using a test RunConfig that invokes
Expand Down Expand Up @@ -211,6 +209,17 @@ def test_sas_qa_execution(self):
self.assertIn('sas.elapsed_seconds:', log_contents)
self.assertIn('sas.qa.elapsed_seconds:', log_contents)

def test_geotiff_filename(self):
"""Test _geotiff_filename() method"""
runconfig_path = join(self.data_dir, 'test_sas_qa_config.yaml')
pge = PgeExecutor(pge_name='BasePgeFilenameTest', runconfig_path=runconfig_path)
pge._initialize_logger()
pge._load_runconfig()
name = "TestName.tif"
file_name = pge._geotiff_filename(name)
file_name_regex = rf'{pge.PROJECT}_{pge.LEVEL}_BasePge_\d{{8}}T\d{{6}}_\d{{3}}_{name}{{1,2}}?'
self.assertEqual(re.match(file_name_regex, file_name).group(), file_name)


if __name__ == "__main__":
unittest.main()
21 changes: 20 additions & 1 deletion src/opera/test/pge/test_dswx_pge.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""
import glob
import os
import re
import tempfile
import unittest
from io import StringIO
Expand All @@ -35,7 +36,7 @@
import opera.util.img_utils
from opera.pge import DSWxExecutor, RunConfig
from opera.util import PgeLogger
from opera.util.img_utils import MockGdal
from opera.util.img_utils import MockGdal, get_geotiff_metadata


class DSWxPgeTestCase(unittest.TestCase):
Expand Down Expand Up @@ -294,6 +295,24 @@ def test_dswx_pge_output_validation(self):
if os.path.exists(test_runconfig_path):
os.unlink(test_runconfig_path)

@patch.object(opera.util.img_utils, "get_geotiff_metadata", get_geotiff_metadata_patch)
def test_geotiff_filename(self):
collinss-jpl marked this conversation as resolved.
Show resolved Hide resolved
"""Test _geotiff_filename() method"""
runconfig_path = join(self.data_dir, 'test_dswx_hls_config.yaml')

pge = DSWxExecutor(pge_name="DSWxPgeTest", runconfig_path=runconfig_path)

pge.run()

image_files = glob.glob(join(pge.runconfig.output_product_path, "*.tif"))
for i in range(len(image_files)):
file_name = pge._geotiff_filename(image_files[i])
md = self.get_geotiff_metadata_patch()
file_name_regex = rf"{pge.PROJECT}_{pge.LEVEL}_{md['PRODUCT_TYPE']}_{md['PRODUCT_SOURCE']}_" \
rf"{md['SPACECRAFT_NAME']}_{md['HLS_DATASET'].split('.')[2]}_\d{{8}}T\d{{6}}_" \
rf"{'.'.join(md['HLS_DATASET'].split('.')[-2:])}_\d{{3}}.tif?"
self.assertEqual(re.match(file_name_regex, file_name).group(), file_name)


if __name__ == "__main__":
unittest.main()