Skip to content

Commit

Permalink
try to get unit string right so pyaerocom understands it
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Griesfeller committed May 3, 2024
1 parent ee412a9 commit eb08e87
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pyaro_readers
version = 0.0.5dev
version = 0.0.5dev1
author = MET Norway
description = implementations of pyaerocom reading plugings using pyaro as interface
long_description = file: README.md
Expand Down
5 changes: 5 additions & 0 deletions src/pyaro_readers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from importlib import metadata

__version__ = metadata.version(__package__)

from .units_helpers import *
3 changes: 3 additions & 0 deletions src/pyaro_readers/harpreader/harpreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pathlib import Path
from tqdm import tqdm
import cfunits
from ..units_helpers import UALIASES

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -116,6 +117,8 @@ def _read_file_variables(self, filename) -> dict[str, str]:
# we can make sure that cfunits understands them
# otherwise variables[vname] = var.attrs["units"] should work as well
variables[vname] = str(cfunits.Units(var.attrs["units"]))
if variables[vname] in UALIASES:
variables[vname] = UALIASES[variables[vname]]

return variables

Expand Down
104 changes: 104 additions & 0 deletions src/pyaro_readers/units_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import pandas as pd


class UnitConversionError(ValueError):
pass


# from pyaerocom.tstype import TsType
# from pyaerocom.variable_helpers import get_variable

#: default frequency for rates variables (e.g. deposition, precip)
RATES_FREQ_DEFAULT = "d"

# 1. DEFINITION OF ATOM and MOLECULAR MASSES

# Atoms
M_O = 15.999 # u
M_S = 32.065 # u
M_N = 14.0067 # u
M_H = 1.00784 # u

# Molecules
M_SO2 = M_S + 2 * M_O
M_SO4 = M_S + 4 * M_O

M_NO2 = M_N + 2 * M_O
M_NO3 = M_N + 3 * M_O

M_NH3 = M_N + 3 * M_H
M_NH4 = M_N + 4 * M_H

# Unit conversion and custom units definitions

# 2.1 Other conversion factors
HA_TO_SQM = 10000 # hectar to square metre.

# 3. LOOKUP TABLE FOR CONVERSION FACTORS

#: Custom unit conversion factors for certain variables
#: columns: variable -> from unit -> to_unit -> conversion
#: factor
UCONV_MUL_FACS = pd.DataFrame(
[
# ["dryso4", "mg/m2/d", "mgS m-2 d-1", M_S / M_SO4],
# ["drynh4", "mg/m2/d", "mgN m-2 d-1", M_N/ M_NH4],
# ["concso4", "ug S/m3", "ug m-3", M_SO4 / M_S],
# ["SO4ugSm3", "ug/m3", "ug S m-3", M_S / M_SO4],
# ["concso4pm25", "ug S/m3", "ug m-3", M_SO4 / M_S],
# ["concso4pm10", "ug S/m3", "ug m-3", M_SO4 / M_S],
["concso2", "ug S/m3", "ug m-3", M_SO2 / M_S],
["concbc", "ug C/m3", "ug m-3", 1.0],
["concoa", "ug C/m3", "ug m-3", 1.0],
["concoc", "ug C/m3", "ug m-3", 1.0],
["conctc", "ug C/m3", "ug m-3", 1.0],
# a little hacky for ratpm10pm25...
# ["ratpm10pm25", "ug m-3", "1", 1.0],
["concpm25", "ug m-3", "1", 1.0],
["concpm10", "ug m-3", "1", 1.0],
["concno2", "ug N/m3", "ug m-3", M_NO2 / M_N],
# ["concno3", "ug N/m3", "ug m-3", M_NO3 / M_N],
["concnh3", "ug N/m3", "ug m-3", M_NH3 / M_N],
# ["concnh4", "ug N/m3", "ug m-3", M_NH4 / M_N],
["wetso4", "kg S/ha", "kg m-2", M_SO4 / M_S / HA_TO_SQM],
["concso4pr", "mg S/L", "g m-3", M_SO4 / M_S],
],
columns=["var_name", "from", "to", "fac"],
).set_index(["var_name", "from"])

# may be used to specify alternative names for custom units defined
# in UCONV_MUL_FACS

UALIASES = {
# mass concentrations
# "ug S m-3": "ug S/m3",
# "ug C m-3": "ug C/m3",
# "ug N m-3": "ug N/m3",
"ugC/m3": "ug C m-3",
"ug/m3": "ug m-3",
# deposition rates (implicit)
## sulphur species
"mgS/m2": "mg S m-2",
"mgSm-2": "mg S m-2",
## nitrogen species
"mgN/m2": "mg N m-2",
"mgNm-2": "mg N m-2",
# deposition rates (explicit)
## sulphur species
"mgS/m2/h": "mg S m-2 h-1",
"mg/m2/h": "mg m-2 h-1",
"mgS/m**2/h": "mg S m-2 h-1",
"mgSm-2h-1": "mg S m-2 h-1",
"mgSm**-2h-1": "mg S m-2 h-1",
"mgS/m2/d": "mg S m-2 d-1",
## nitrogen species
"mgN/m2/h": "mg N m-2 h-1",
"mgN/m**2/h": "mg N m-2 h-1",
"mgNm-2h-1": "mg N m-2 h-1",
"mgNm**-2h-1": "mg N m-2 h-1",
"mgN/m2/d": "mg N m-2 d-1",
## others
"MM/H": "mm h-1",
# others
"/m": "m-1",
}
2 changes: 1 addition & 1 deletion tests/test_HARPReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestHARPReader(unittest.TestCase):
"/lustre/storeB/project/aerocom/aerocom1/AEROCOM_OBSDATA/CNEMC/aggregated/"
)
test_vars = ["PM10_density", "CO_volume_mixing_ratio", "PM2p5_density"]
test_units = ["ug/m3", "ppm", "ug/m3"]
test_units = ["ug m-3", "ppm", "ug m-3"]

def test_1read(self):
with pyaro.open_timeseries(
Expand Down

0 comments on commit eb08e87

Please sign in to comment.