r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
173 stars 27 forks source link

new unit for MOL #304

Closed Schuch666 closed 1 year ago

Schuch666 commented 2 years ago

I got this 'error' related to install a unit MOL for the case of '1 MOL = 1 g', this is the error:

Error in ud_map_symbols(symbol, ut_unit) : Unit already maps to "g"

This happen in the new version (used to work in previous version), why there is this new limitation?

This reproduces the expected results

library('units')
mm = 12
e = set_units(12,"g m-2 s-1")
remove_unit("MOL")
install_unit("MOL", paste(mm,"g"))
e <- set_units(e,"MOL km-2 h-1")

Changing to mm = 1 make the error in install_unit.

This is the workaround:

library('units')
mm = 1
e = set_units(12,"g m-2 s-1")
remove_unit("MOL")
install_unit("MOL", "666 g")                     # can be any constant here
e <- 666 * set_units(e,"MOL km-2 h-1")  # the same constant

I got this solution but there is a better way to do that?

Thanks

Enchufa2 commented 2 years ago

Are you on Windows, right? This is an upstream bug for that platform. On Unix, your example works just fine. See #301.

Enchufa2 commented 2 years ago

A better workaround would be to handle moles properly. I.e., a mole is not a gram; instead, a mole of something could be a gram of that something. Thus, a better approach would be to define the conversion from a new unit g_something to the existing mole.

Enchufa2 commented 2 years ago

Quick example:

library(units)
#> udunits database from /usr/share/udunits/udunits2.xml

install_unit("g_oxigen", "1/16 mol")
install_unit("g_carbon", "1/12 mol")

(x <- set_units(1, mol))
#> 1 [mol]
(y <- set_units(x, g_oxigen))
#> 16 [g_oxigen]
set_units(y, g_carbon)
#> 12 [g_carbon]
Schuch666 commented 2 years ago

Hi @Enchufa2

Thanks for the reply.

I will check the possibility to implement this in the future, currently this is part of a function inside a package that need to runs on all platforms and this part handle the conversion of emissions of aerosol and gases to the units needed to air quality models (ug m-2 s-1 and MOL km-1 h-1), this is a simple part but should work for any arbitrary value of mm (the user has the option to use 1 for special cases, for example if the input data is in a different unit or is already in MOL or is a mix of many species).

Currently this is a flexible function for any species and is the user that has to put the wanted mm value, more responsibility but more power

Schuch666 commented 2 years ago

Hi @Enchufa2 A quick update, this error also happen in Debian check https://www.r-project.org/nosvn/R.check/r-devel-linux-x86_64-debian-clang/EmissV-00check.html

Enchufa2 commented 2 years ago

This is quite strange. I am unable to reproduce this. Do we know by any chance what version of udunits2 is in there? Is there anything in common with the Windows machine?

Enchufa2 commented 1 year ago

This probably had something to do with the toolchain... I didn't manage to reproduce this, sorry. Please feel free to reopen if you find this issue again.