Skip to content

Commit

Permalink
Changed env var warning to info + using PyPI csv2bufr
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryPTB committed Mar 25, 2024
1 parent 5fedefe commit 8cfcdb2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
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
28 changes: 15 additions & 13 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,36 @@ 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", 65535) # noqa
ORIGINATING_SUBCENTRE = os.environ.get("BUFR_ORIGINATING_SUBCENTRE", 65535) # noqa

if ORIGINATING_CENTRE == 65535:
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
"value": f"const:{ORIGINATING_CENTRE}" # noqa
})

if os.environ.get("BUFR_ORIGINATING_SUBCENTRE") is None:
if ORIGINATING_SUBCENTRE == 65535:
missing_env_vars.append("BUFR_ORIGINATING_SUBCENTRE")
else:
mapping["header"].append({
"eccodes_key": "bufrHeaderSubCentre",
"value": f"const:{os.environ.get('BUFR_ORIGINATING_SUBCENTRE')}" # noqa
"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: 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 8cfcdb2

Please sign in to comment.