Skip to content

Commit

Permalink
[DM-39735] Add validator for ucds and units
Browse files Browse the repository at this point in the history
  • Loading branch information
cbanek committed Jun 26, 2023
1 parent a0e8f0e commit f8fa31d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions python/felis/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
from collections.abc import Iterable, Mapping, MutableSet
from typing import Any

from astropy import units as u # type: ignore
from astropy.io.votable import ucd # type: ignore

from .types import FelisType
from .visitor import Visitor

Expand Down Expand Up @@ -106,6 +109,32 @@ def check_column(self, column_obj: _Mapping, table_obj: _Mapping) -> None:
if not length and (felis_type.is_sized or felis_type.is_timestamp):
# This is not a warning, because it's usually fine
logger.info(f"No length defined for {_id} for type {datatype_name}")

# Check UCDs of columns
ivoa_ucd = column_obj.get("ivoa:ucd")
if not ucd.check_ucd(ivoa_ucd, check_controlled_vocabulary=True):
logger.error(f"invalid ucd for {_id}: {ivoa_ucd}")

# Check Units of columns
fits_unit = column_obj.get("fits:tunit")
ivoa_unit = column_obj.get("ivoa:unit")

# There should only be one type of unit
if fits_unit and ivoa_unit:
logger.error("two types of units")
elif fits_unit:
unit = fits_unit
elif ivoa_unit:
unit = ivoa_unit
else:
unit = ""

# Check the unit using astropy
try:
u.Unit(unit)
except ValueError as e:
logger.error(f"invalid unit for {_id} " + str(e))

self._check_visited(_id)

def check_primary_key(self, primary_key_obj: str | Iterable[str], table: _Mapping) -> None:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
astropy
sqlalchemy >= 1.4
click
pyyaml
Expand Down

0 comments on commit f8fa31d

Please sign in to comment.