Skip to content

Commit

Permalink
Updates to enforce BUFR_ORIGINATING_CENTRE and BUFR_ORIGINATING_SUBCE…
Browse files Browse the repository at this point in the history
…NTRE to be set.
  • Loading branch information
david-i-berry committed Feb 1, 2024
1 parent c714293 commit 68757dd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
cd /opt/csv2bufr
wget https://github.com/wmo-im/csv2bufr-templates/archive/refs/tags/v0.1.tar.gz
tar -zxf v0.1.tar.gz --strip-components=1 csv2bufr-templates-0.1/templates
export BUFR_ORIGINATING_CENTRE 0
export BUFR_ORIGINATING_SUBCENTRE 0
- name: run tests ⚙️
run: |
pytest
Expand Down
44 changes: 43 additions & 1 deletion csv2bufr/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@

_SUCCESS_ = True


# check env variables set
ORIGINATING_CENTRE = None
ORIGINATING_SUBCENTRE = None

ORIGINATING_CENTRE = os.environ.get('BUFR_ORIGINATING_CENTRE')
ORIGINATING_SUBCENTRE = os.environ.get('BUFR_ORIGINATING_SUBCENTRE')

if ORIGINATING_CENTRE is None:
msg = "Invalid BUFR originating centre, please ensure the BUFR_ORIGINATING_CENTRE is set to a valid value" # noqa
LOGGER.error(msg)
raise RuntimeError(msg)

if ORIGINATING_SUBCENTRE is None:
msg = "Invalid BUFR originating subcentre, please ensure the BUFR_ORIGINATING_SUBCENTRE is set to a valid value" # noqa
LOGGER.error(msg)
raise RuntimeError(msg)


if Path("/opt/csv2bufr/templates").exists():
TEMPLATE_DIRS.append(Path("/opt/csv2bufr/templates"))

Expand Down Expand Up @@ -68,14 +87,37 @@ def load_template(template_name: str) -> Union[dict, None]:
else:
fname = TEMPLATES[template_name].get('path')
if fname is None:
msg = f"Error loading template {template.name}, no path found"
msg = f"Error loading template {template_name}, no path found"
else:
with open(fname) as fh:
template = json.load(fh)

if msg:
raise RuntimeError(msg)
else:
# update template originating centre and subcentre
ocset = False
oscset = False
for hidx in range(len(template['header'])):
if template['header'][hidx]["eccodes_key"] == "bufrHeaderCentre":
template['header'][hidx]["eccodes_key"]["value"] = \
f"const:{ORIGINATING_CENTRE}"
ocset = True
if template['header'][hidx]["eccodes_key"] == "bufrHeaderSubCentre": # noqa
template['header'][hidx]["eccodes_key"]["value"] = \
f"const:{ORIGINATING_SUBCENTRE}"
oscset = True

if not ocset:
template['header'].append(
{"eccodes_key":"bufrHeaderCentre",
"value": f"const:{ORIGINATING_CENTRE}"})

if not oscset:
template['header'].append(
{"eccodes_key":"bufrHeaderSubCentre",
"value": f"const:{ORIGINATING_SUBCENTRE}"})

return template


Expand Down
18 changes: 18 additions & 0 deletions tests/test_csv2bufr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
###############################################################################

import csv
import os
from io import StringIO
import logging

Expand Down Expand Up @@ -251,3 +252,20 @@ def test_transform(data_dict, mapping_dict):

def test_templates():
assert c2bt.load_template('21327aac-46a6-437d-ae81-7a16a637dd2c') is not None # noqa
tmpl = c2bt.load_template('21327aac-46a6-437d-ae81-7a16a637dd2c')
# check header centre and sub centre set to env
ocset = False
ocenv = os.environ['BUFR_ORIGINATING_CENTRE']
oscset = False
oscenv = os.environ['BUFR_ORIGINATING_SUBCENTRE']
LOGGER.warning((tmpl['header']))
for hidx in range(len(tmpl['header'])):
LOGGER.warning(tmpl['header'][hidx]['eccodes_key'])
if tmpl['header'][hidx]['eccodes_key'] == 'bufrHeaderCentre':
assert tmpl['header'][hidx]['value'] == f"const:{ocenv}"
ocset = True
if tmpl['header'][hidx]['eccodes_key'] == 'bufrHeaderSubCentre':
assert tmpl['header'][hidx]['value'] == f"const:{oscenv}"
oscset = True
assert ocset
assert oscset

0 comments on commit 68757dd

Please sign in to comment.