Skip to content

Commit

Permalink
Updates following review.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-i-berry committed Jan 6, 2025
1 parent 993c55e commit 4091577
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 66 deletions.
23 changes: 12 additions & 11 deletions bufr2geojson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
NULLIFY_INVALID = os.environ.get("BUFR2GEOJSON_NULLIFY_INVALID", True)
THISDIR = os.path.dirname(os.path.realpath(__file__))
RESOURCES = f"{THISDIR}{os.sep}resources"
ASSOCIATED_FIELDS_FILE = f"{RESOURCES}{os.sep}031021.json"
CODETABLES = {}
FLAGTABLES = {}
ECCODES_DEFINITION_PATH = codes_definition_path()
Expand All @@ -68,7 +69,7 @@
raise EnvironmentError('Cannot find ecCodes definition path')
TABLEDIR = Path(ECCODES_DEFINITION_PATH) / 'bufr' / 'tables' / '0' / 'wmo'

# ToDo - read preferred units from config file
# TODO - read preferred units from config file
# PREFERRED UNITS
PREFERRED_UNITS = {
"K": "Celsius",
Expand All @@ -78,7 +79,7 @@
# The following is required as the code table from ECMWF is incomplete
# and that from github/wmo-im not very usable.
try:
with open(f"{RESOURCES}{os.sep}031021.json") as fh:
with open(ASSOCIATED_FIELDS_FILE) as fh:
ASSOCIATED_FIELDS = json.load(fh)
except Exception as e:
LOGGER.error(f"Error loading associated field table (031021) - {e}")
Expand Down Expand Up @@ -358,7 +359,7 @@ def get_qualifiers(self) -> dict:

return result

def get_location(self, BUFRclass=None) -> Union[dict, None]:
def get_location(self, bufr_class: int = None) -> Union[dict, None]:
"""
Function to get location from qualifiers and to apply any displacements
or increments
Expand Down Expand Up @@ -401,7 +402,7 @@ def get_location(self, BUFRclass=None) -> Union[dict, None]:
# round to avoid extraneous digits
longitude = round(longitude["value"], longitude["attributes"]["scale"]) # noqa

z = self.get_zcoordinate(BUFRclass)
z = self.get_zcoordinate(bufr_class)
height = z.get('z_amsl', {}).get('value')

# check for increments, not yet implemented
Expand All @@ -424,7 +425,7 @@ def get_location(self, BUFRclass=None) -> Union[dict, None]:
"coordinates": location
}

def get_zcoordinate(self, BUFRclass=None):
def get_zcoordinate(self, bufr_class: int = None) -> Union[dict, None]:
# class 07 gives vertical coordinate
result = {}

Expand All @@ -441,7 +442,7 @@ def get_zcoordinate(self, BUFRclass=None):
station_ground = self.qualifiers["07"].get("height_of_station_ground_above_mean_sea_level",None) # noqa

abs_height = []
if BUFRclass == 10:
if bufr_class == 10:
if "height_of_barometer_above_mean_sea_level" in self.qualifiers["07"]: # noqa
abs_height.append("height_of_barometer_above_mean_sea_level")
else:
Expand Down Expand Up @@ -1011,15 +1012,15 @@ def as_geojson(self, bufr_handle: int, id: str,
# next decoded value if from code table
description = None
observation_type = "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement" # noqa default type
if (attributes["units"] == "CODE TABLE") and (value is not None):
if attributes["units"] == "CODE TABLE" and value is not None:
description = self.get_code_value(attributes["code"], value)
observation_type = "http//www.opengis.net/def/observationType/OGC-OM/2.0/OM_CategoryObservation" # noqa
_value = {
'codetable': f"http://codes.wmo.int/bufr4/codeflag/{f:1}-{xx:02}-{yyy:03}", # noqa
'entry': f"{value}", # noqa
'description': description
}
elif (attributes["units"] == "FLAG TABLE") and (value is not None):
elif attributes["units"] == "FLAG TABLE" and value is not None:
observation_type = "http//www.opengis.net/def/observationType/OGC-OM/2.0/OM_CategoryObservation" # noqa
nbits = attributes['width']
description = self.get_flag_value(attributes["code"], "{0:0{1}b}".format(value, nbits)) # noqa
Expand Down Expand Up @@ -1090,7 +1091,7 @@ def as_geojson(self, bufr_handle: int, id: str,
# self.get_identification()
metadata = self.get_qualifiers()
metadata["BUFR_element"] = fxxyyy
z = self.get_zcoordinate(BUFRclass=xx)
z = self.get_zcoordinate(bufr_class=xx)
if z is not None:
metadata["zCoordinate"] = z.get('z')
metadata['BUFRheaders'] = headers
Expand Down Expand Up @@ -1122,9 +1123,9 @@ def as_geojson(self, bufr_handle: int, id: str,
data = {
"geojson": {
"id": feature_id,
"conformsTo": ["https://schemas.wmo.int/wccdm-obs/2024/wccdm-obs.json"], # noqa
"conformsTo": ["https://wis.wmo.int/spec/wccdm-obs/1"], # noqa
"type": "Feature",
"geometry": self.get_location(BUFRclass=xx),
"geometry": self.get_location(bufr_class=xx),
"properties": {
"host": host_id, # noqa
"observer": None,
Expand Down
59 changes: 4 additions & 55 deletions tests/test_bufr2geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,60 +80,9 @@ def geojson_schema():

@pytest.fixture
def geojson_output():
return {
'id': 'WIGOS_0-20000-0-03951_20220320T210000-0-13',
'conformsTo': ['http://www.wmo.int/spec/om-profile-1/1.0/req/geojson'],
'reportId': 'WIGOS_0-20000-0-03951_20220320T210000-0',
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-9.42,
51.47,
20.0
]
},
'properties': {
'wigos_station_identifier': '0-20000-0-03951',
'phenomenonTime': '2022-03-20T21:00:00Z',
'resultTime': '2022-03-20T21:00:00Z',
'name': 'non_coordinate_pressure',
'value': 1019.3,
'units': 'hPa',
'description': None,
'metadata': [
{
'name': 'station_or_site_name',
'value': None,
'units': 'CCITT IA5',
'description': 'SHERKIN ISLAND'
},
{
'name': 'station_type',
'value': 0,
'units': 'CODE TABLE',
'description': 'AUTOMATIC STATION'
},
{
'name': 'height_of_barometer_above_mean_sea_level',
'value': 21.0,
'units': 'm',
'description': None
}
],
'index': 13,
'fxxyyy': '010004'
}
}


@pytest.fixture
def geojson_output_new():
return {
"id": "1ec58338aab209c8ab22f05309315b71-0",
"conformsTo": [
"https://schemas.wmo.int/wccdm-obs/2024/wccdm-obs.json"
],
"conformsTo": ["https://wis.wmo.int/spec/wccdm-obs/1"],
"type": "Feature",
"geometry": {
"type": "Point",
Expand Down Expand Up @@ -289,7 +238,7 @@ def test_multi(multimsg_bufr):
assert icount == 48


def test_transform(geojson_schema, geojson_output_new):
def test_transform(geojson_schema, geojson_output):
test_bufr_file = 'A_ISIA21EIDB202100_C_EDZW_20220320210902_11839953.bin'
with open(test_bufr_file, 'rb') as fh:
messages1, messages2 = itertools.tee(transform(fh.read(),
Expand All @@ -312,8 +261,8 @@ def test_transform(geojson_schema, geojson_output_new):
geojson['properties']['parameter']['hasProvenance']['activity']['_:bufr2geojson']['prov:endTime'] = "2024-12-19 00:00:00" # noqa
geojson['properties']['resultTime'] = "2024-12-19 00:00:00"
for k, v in geojson.items():
assert v == geojson_output_new[k]
assert geojson == geojson_output_new
assert v == geojson_output[k]
assert geojson == geojson_output

print("Message matches expected value")

Expand Down

0 comments on commit 4091577

Please sign in to comment.