Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Unitful #53

Closed
Sbozzolo opened this issue Aug 5, 2024 · 5 comments · Fixed by #70
Closed

Add support for Unitful #53

Sbozzolo opened this issue Aug 5, 2024 · 5 comments · Fixed by #70

Comments

@Sbozzolo
Copy link
Member

Sbozzolo commented Aug 5, 2024

Unitful.jl is a package that handles physical units. It would be interesting to explore a possible integration with ClimaAnalysis.

@AlexisRenchon
Copy link
Member

I haven't made any functionality or modules for it, but I did some stuff in ClimaLandSimulations here

# inside Fluxnet.jl module
using Unitful: R, L, mol, K, kJ, °C, m, g, cm, hr, mg, s, μmol, Pa, W, mm, kPa
using UnitfulMoles: molC
using Unitful, UnitfulMoles

# Register Fluxnet with Unitful.jl
function __init__()
    Unitful.register(Fluxnet)
end

    # inside inputs_dataframe.jl
    # make a Unitful dataframe with SI units and one with commonly used units
    columns = variables_name[2:end]
    units = [
        molCO₂ * m^-2 * s^-1,
        K,
        Pa,
        Pa,
        m * s^-1,
        m * s^-1,
        W * m^-2,
        W * m^-2,
        molCO₂,
        m^3 * m^-3,
        K,
        molCO₂ * m^-2 * s^-1,
        W * m^-2,
        W * m^-2,
        W * m^-2,
        W * m^-2,
    ]
    inputs_SI = copy(inputs)
    foreach(
        (col, unit) -> inputs_SI[!, col] .= first([inputs_SI[!, col]]unit),
        columns,
        units,
    )

    units_to = [
        μmolCO₂ * m^-2 * s^-1,
        °C,
        kPa,
        Pa,
        mm * s^-1,
        m * s^-1,
        W * m^-2,
        W * m^-2,
        μmolCO₂,
        m^3 * m^-3,
        °C,
        μmolCO₂ * m^-2 * s^-1,
        W * m^-2,
        W * m^-2,
        W * m^-2,
        W * m^-2,
    ]
    inputs_commonly_used = copy(inputs_SI)
    foreach(
        (col, unit_to) ->
            inputs_commonly_used[!, col] =
                uconvert.(unit_to, inputs_SI[!, col]),
        columns,
        units_to,
    )

@AlexisRenchon
Copy link
Member

and also in ParamViz

@AlexisRenchon
Copy link
Member

This makes me think:
we currently give units with implicit multiplication (e.g., m s^-1, without *), which is not compatible with unitful convention, but I guess that's an easy conversion so not a problem?

@Sbozzolo
Copy link
Member Author

Sbozzolo commented Aug 7, 2024

It would not be a bad idea to require units to be formatted like that, it would make them parsable by other programs as well.

@AlexisRenchon
Copy link
Member

there's other unitful / unitfulmoles conventions, e.g., molCO2 (not mol CO2, no space), kPa (but I guess we usually don't have multiplier indices)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants