Skip to content

Oil Concentration: "g/km²" synonym is attached to "kilogram per square kilometer" — conversions off by exactly 1000x #39

@consigcody94

Description

@consigcody94

In nucos/unit_data.py, the Oil Concentration entry "kilogram per square kilometer": (.0010526315789473684, ["g/km²", "kg/km^2"]) lists the gram symbol g/km² as a synonym for the kilogram unit. Any conversion entered with the Unicode superscript gram spelling is exactly 1000x too large:

>>> import nucos
>>> nucos.convert("Oil Concentration", "g/km²", "micron", 1.0)
0.0010526315789473684          # correct value for g/km² is 1.0526e-06
>>> nucos.get_primary_name("g/km²", "Oil Concentration")
'kilogram per square kilometer'

Environment

  • NOAA-ORR-ERD/PyNUCOS @ 0583706dee5e0df6cc387ab5f759f80265a30414 (nucos 3.4.1, editable install from source)
  • Windows 11 Pro (10.0.26200), Python 3.14.5, pytest 9.0.3
  • Full shipped test suite passes (494 passed) — this defect is not covered by any test

Reproduction

Save as repro.py and run python repro.py (the \N{SUPERSCRIPT TWO} escape avoids any copy/paste encoding ambiguity):

import nucos

u = "g/km\N{SUPERSCRIPT TWO}"   # i.e. "g/km²" -- the spelling NUCOS itself displays

print(repr(u), "->", nucos.get_primary_name(u, "Oil Concentration"))
got = nucos.convert("Oil Concentration", u, "micron", 1.0)
print("convert('Oil Concentration', 'g/km²', 'micron', 1.0) =", got)
print("correct value for g/km² =", 1.0526315789473684e-06)
print("ratio =", got / 1.0526315789473684e-06)

Observed output

'g/km²' -> kilogram per square kilometer
convert('Oil Concentration', 'g/km²', 'micron', 1.0) = 0.0010526315789473684
correct value for g/km² = 1.0526315789473683e-06
ratio = 1000.0000000000001

Expected

With the hard-coded density of 0.95 g/cm³ that this part of the table uses (per the comment at unit_data.py:51-52), 1 g/km² = 1e-3 kg / 1e6 m² / 950 kg/m³ = 1.0526e-12 m = 1.0526315789473684e-06 micron. get_primary_name("g/km²") should resolve to a gram-based unit, not kilogram per square kilometer.

This matters in practice: Oil Concentration is the unit type used for spill surface-coverage estimates, and the Unicode-superscript spelling is what NUCOS itself displays in its unit list (NUCOS_unit_list.rst) — so a user who copies the displayed symbol gets a 1000x overestimate, silently.

Root cause

nucos/unit_data.py:55:

"kilogram per square kilometer": (.0010526315789473684, ["g/km\N{SUPERSCRIPT TWO}", "kg/km^2" ]),

The first synonym is the gram symbol, but the factor is for kilograms per square kilometer. Almost certainly a typo for "kg/km\N{SUPERSCRIPT TWO}" — every other unit in the table pairs the Unicode-superscript spelling with the matching ASCII ^ spelling (e.g. g/m²/g/m^2, kg/m²/kg/m^2 on the two lines just above).

Proposed fix

One character:

-        "kilogram per square kilometer": (.0010526315789473684, ["g/km\N{SUPERSCRIPT TWO}", "kg/km^2" ]),
+        "kilogram per square kilometer": (.0010526315789473684, ["kg/km\N{SUPERSCRIPT TWO}", "kg/km^2" ]),

Optionally also add a true "gram per square kilometer" unit (factor 1.0526315789473684e-06) with synonyms g/km² / g/km^2, so the gram spelling keeps working — but correctly.

No existing test expectations encode the bug: the shipped suite (nucos/tests/test_unit_conversion.py:66) only exercises the ASCII kg/km^2 synonym, which is why it stays green either way. A test for the Unicode synonym should be added with the fix.

Related

Distinct from #24, which is the change family that introduced the Unicode superscript synonyms in the first place (this entry appears to be the one typo in that batch). Not reported in any existing issue (all 38 open/closed issues reviewed).

Happy to submit a PR for this if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions