Skip to content

Commit

Permalink
- Implemented logic to use get_dmrpp or get_dmrpp_h4 depending on th…
Browse files Browse the repository at this point in the history
…e type of file.
  • Loading branch information
Michaellh0079 committed Sep 18, 2024
1 parent 1978353 commit 41aaf7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM opendap/besd:3.21.0-388 AS base
FROM opendap/besd:3.21.0-501 AS base
HEALTHCHECK NONE

RUN yum -y update && \
Expand Down Expand Up @@ -36,6 +36,7 @@ RUN coverage run -m pytest && \

RUN pip install --target $BUILD awslambdaric
COPY site.conf /etc/bes/
COPY bes.conf /etc/bes/

CMD ["python", "generate_dmrpp.py"]
ENTRYPOINT []
33 changes: 29 additions & 4 deletions dmrpp_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, **kwargs):
self.processing_regex = self.dmrpp_meta.get(
'dmrpp_regex', '.*\\.(((?i:(h|hdf)))(e)?5|nc(4)?)(\\.bz2|\\.gz|\\.Z)?'
)

kwargs.update({'path': '/tmp'})
super().__init__(**kwargs)
self.path = self.path.rstrip('/') + "/"
# Enable logging the default is True
Expand Down Expand Up @@ -159,6 +159,25 @@ def strip_old_dmrpp_files(granule):
else:
i += 1

def dmrpp_type(self, filename):
"""
Attempts to open and read bytes from the file to process and determine if it is HDF4 or HDF5
"""
ret = 'get_dmrpp'
try:
with open(filename, 'rb') as file:
bts = file.read(4)
if bts == b'\x89HDF':
self.logger_to_cw.info(f'{filename}: HDF5')
elif bts == b'\x0e\x03\x13\x01':
self.logger_to_cw.info(f'{filename}: HDF4')
ret = f'{ret}_h4'
else:
self.logger_to_cw.info(f'{filename}: not HDF4 or HDF5')
except FileNotFoundError:
self.logger_to_cw.warning('Unable to read file type. Using default command: {ret}')
return ret

def get_dmrpp_command(self, dmrpp_meta, input_path, output_filename, local=False):
"""
Getting the command line to create DMRPP files
Expand All @@ -170,10 +189,16 @@ def get_dmrpp_command(self, dmrpp_meta, input_path, output_filename, local=False

s_option = ''
if os.getenv('AWS_LAMBDA_FUNCTION_NAME'):
s_option = '-s /etc/bes/site.conf'
s_option = '-c /etc/bes/bes.conf'

base_dmrpp_cmd = self.dmrpp_type(output_filename)

if 'h4' not in base_dmrpp_cmd:
dmrpp_cmd = f"{base_dmrpp_cmd} {s_option} {options} {input_path} -o {output_filename}.dmrpp" \
f" {local_option} {os.path.basename(output_filename)}"
else:
dmrpp_cmd = f"{base_dmrpp_cmd} -i {output_filename} {s_option}"

dmrpp_cmd = f"get_dmrpp {s_option} {options} {input_path} -o {output_filename}.dmrpp" \
f" {local_option} {os.path.basename(output_filename)}"
return " ".join(dmrpp_cmd.split())

def add_missing_files(self, dmrpp_meta, file_name):
Expand Down

0 comments on commit 41aaf7c

Please sign in to comment.