Skip to content

Commit

Permalink
Merge pull request #52 from wmo-im/env_vars
Browse files Browse the repository at this point in the history
Changed env var warning to info + updated unit test + using PyPI csv2bufr
  • Loading branch information
RoryPTB authored Mar 27, 2024
2 parents 5fedefe + b0a992b commit 9530d90
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
env:
BUFR_ORIGINATING_CENTRE: 123
BUFR_ORIGINATING_SUBCENTRE: 123
BUFR_ORIGINATING_CENTRE: 65535
BUFR_ORIGINATING_SUBCENTRE: 65535
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Dependencies are listed in [requirements.txt](https://github.com/wmo-im/synop2bu

### Setting Environment Variables

Before using synop2bufr, we highly encourage you to set the `BUFR_ORIGINATING_CENTRE` and `BUFR_ORIGINATING_SUBCENTRE` environment variables. These variables are used to specify the originating centre and subcentre of the SYNOP messages. **Without these set, they will default to missing (255).**
Before using synop2bufr, we highly encourage you to set the `BUFR_ORIGINATING_CENTRE` and `BUFR_ORIGINATING_SUBCENTRE` environment variables. These variables are used to specify the originating centre and subcentre of the SYNOP messages. **Without these set, they will default to missing (65535).**

You can set these environment variables in your shell if you want to run synop2bufr on your local machine. Here's how you can do it in a Bash shell:

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
click
pymetdecoder-wmo
csv2bufr @ git+https://github.com/wmo-im/csv2bufr.git
csv2bufr
4 changes: 0 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import os
import re
from setuptools import Command, find_packages, setup
import subprocess


class PyTest(Command):
Expand Down Expand Up @@ -74,9 +73,6 @@ def get_package_version():
if (os.path.exists('MANIFEST')):
os.unlink('MANIFEST')

# Install dependencies not on PyPI
subprocess.check_call("pip install https://github.com/wmo-im/csv2bufr/archive/main.zip", shell=True) # noqa

setup(
name='synop2bufr',
version=get_package_version(),
Expand Down
47 changes: 25 additions & 22 deletions synop2bufr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ def transform(data: str, metadata: str, year: int,
# each report necessitates this approach, because
# we want to ensure the warning is only appended
# to the first conversion
can_var_warning_be_displayed = True
can_var_info_be_displayed = True

# ===================
# First parse metadata file
Expand Down Expand Up @@ -1544,34 +1544,37 @@ def truncate_to_twenty(name: str) -> str:
# and subcentre codes are present
missing_env_vars = []

if os.environ.get("BUFR_ORIGINATING_CENTRE") is None:
ORIGINATING_CENTRE = os.environ.get("BUFR_ORIGINATING_CENTRE", None) # noqa
ORIGINATING_SUBCENTRE = os.environ.get("BUFR_ORIGINATING_SUBCENTRE", None) # noqa

if ORIGINATING_CENTRE is None:
missing_env_vars.append("BUFR_ORIGINATING_CENTRE")
else:
# Add the BUFR header centre and subcentre to mappings
mapping["header"].append({
"eccodes_key": "bufrHeaderCentre",
"value": f"const:{os.environ.get('BUFR_ORIGINATING_CENTRE')}" # noqa
})
ORIGINATING_CENTRE = 65535

if os.environ.get("BUFR_ORIGINATING_SUBCENTRE") is None:
if ORIGINATING_SUBCENTRE is None:
missing_env_vars.append("BUFR_ORIGINATING_SUBCENTRE")
else:
mapping["header"].append({
"eccodes_key": "bufrHeaderSubCentre",
"value": f"const:{os.environ.get('BUFR_ORIGINATING_SUBCENTRE')}" # noqa
})
ORIGINATING_SUBCENTRE = 65535

# Add the BUFR header centre and subcentre to mappings
mapping["header"].append({
"eccodes_key": "bufrHeaderCentre",
"value": f"const:{ORIGINATING_CENTRE}" # noqa
})
mapping["header"].append({
"eccodes_key": "bufrHeaderSubCentre",
"value": f"const:{ORIGINATING_SUBCENTRE}" # noqa
})

# If either of these environment variables are not set,
# we will default to missing and warn the user once
if missing_env_vars and can_var_warning_be_displayed:
# Display ewarning messages
# we will default to missing and inform the user once
if missing_env_vars and can_var_info_be_displayed:
# Display info messages
for var in missing_env_vars:
var_warning = f"The {var} environment variable is not set, will default to missing!" # noqa
LOGGER.warning(var_warning)
warning_msgs.append(var_warning)
can_var_warning_be_displayed = False
var_info = f"The {var} environment variable is not set, will default to missing!" # noqa
LOGGER.info(var_info)
can_var_info_be_displayed = False
# Stop duplicated warnings
can_var_warning_be_displayed = False
can_var_info_be_displayed = False

# Define a new method which handles the updating of
# the mapping file with section 3 and 4 cloud data
Expand Down
2 changes: 0 additions & 2 deletions synop2bufr/resources/synop-mappings-307080.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"header":[
{"eccodes_key": "edition", "value": "const:4"},
{"eccodes_key": "masterTableNumber", "value": "const:0"},
{"eccodes_key": "bufrHeaderCentre", "value": "const:65535"},
{"eccodes_key": "bufrHeaderSubCentre", "value": "const:65535"},
{"eccodes_key": "updateSequenceNumber", "value": "const:0"},
{"eccodes_key": "dataCategory", "value": "const:0"},
{"eccodes_key": "internationalDataSubCategory", "value": "const:2"},
Expand Down
2 changes: 0 additions & 2 deletions synop2bufr/resources/synop-mappings-307096.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"header":[
{"eccodes_key": "edition", "value": "const:4"},
{"eccodes_key": "masterTableNumber", "value": "const:0"},
{"eccodes_key": "bufrHeaderCentre", "value": "const:65535"},
{"eccodes_key": "bufrHeaderSubCentre", "value": "const:65535"},
{"eccodes_key": "updateSequenceNumber", "value": "const:0"},
{"eccodes_key": "dataCategory", "value": "const:0"},
{"eccodes_key": "internationalDataSubCategory", "value": "const:2"},
Expand Down
2 changes: 1 addition & 1 deletion tests/test_synop2bufr.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def multiple_reports_307096():
def single_report():
return """AAXX 21121
15001 05515 32931 10103 21090 39765 42250 57020 60071 72006 82110 91155
333 10178 21073 34101 55055 00010 20003 30002 50001 60004
222// 06070 20502 333 10178 21073 34101 55055 00010 20003 30002 50001 60004
60035 70500 83145 81533 91008 91111
444 18031 22053
"""
Expand Down

0 comments on commit 9530d90

Please sign in to comment.