How To Custom-convert Kg M-2 S-1 To Mm/day #129 - GitHub

Skip to content Dismiss alert {{ message }} / units Public
  • Notifications You must be signed in to change notification settings
  • Fork 28
  • Star 175
  • Code
  • Issues 15
  • Pull requests 1
  • Actions
  • Projects
  • Security
  • Insights
Additional navigation options 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.

Sign up for GitHub

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

Jump to bottom How to custom-convert kg m-2 s-1 to mm/day #129 Closed adrfantini opened this issue Mar 20, 2018 · 10 comments Closed How to custom-convert kg m-2 s-1 to mm/day #129 adrfantini opened this issue Mar 20, 2018 · 10 comments

Comments

@adrfantini Copy link

adrfantini commented Mar 20, 2018 edited Loading

I'm probably missing something really obvious, sorry in advance if this is the case. I'm trying to convert kg m-2 s-1 to mm/day, as is often the case for rain units. I can convert e.g. kg/s to mm/day, but not kg m-2 s-1:

install_conversion_constant("kg s-1", "mm/day", 86400) install_conversion_constant("mm/day", "kg s-1", 1/86400) set_units(1, "kg s-1") + set_units(1, "mm/day") ## Works install_conversion_constant("kg m-2 s-1", "mm/day", 86400) install_conversion_constant("mm/day", "kg m-2 s-1", 1/86400) set_units(1, "kg m-2 s-1") + set_units(1, "mm/day") ## Does not work: Error: cannot convert mm/d into 1/m/s

Actually I cannot even set:

set_units(1, "kg/m**2") Error: In ‘kg/m^2’ the numeric multiplier ‘2’ is invalid. Use `install_conversion_constant()` to define a new unit that is a multiple of another unit.

While set_units(1, "kg m-2") returns the wrong thing: 1/m.

What is the correct syntax here?

All reactions @Enchufa2 Enchufa2 added the duplicate label Mar 21, 2018 @Enchufa2 Copy link Member

Enchufa2 commented Mar 21, 2018 edited Loading

You are using the right syntax maybe not, but the current version of units is not able to handle conversions for combinations of standard and user-defined units (see #71, #84, #85). We are working on it (see #89).

All reactions @edzer Copy link Member

edzer commented Mar 21, 2018

I think you're on the wrong track, since units should do this kind of thing itself. What you're forgetting is to bring in the density of water, in order to go from mass to volume (to length).

👍 1 Aariq reacted with thumbs up emoji All reactions
  • 👍 1 reaction
@adrfantini Copy link Author

adrfantini commented Mar 21, 2018 edited Loading

@edzer Unfortunately the program I'm writing should be flexible and allow for different units to be used. Say the input units are kg/m^2/s and the user requests mm/d as output - it should work. But if the data is in K and the user requests celsius, it should also work.

So I cannot add a constant water density multiplication, and I'd very much like to avoid inserting custom switch or if. It'd be much cleaner defining custom conversion constants for these particular cases, imho.

All reactions @edzer Copy link Member

edzer commented Mar 21, 2018

You'll have to if you want to use the units logic. Your conversion depends on whether it rains water, or feathers.

All reactions @edzer edzer mentioned this issue Mar 21, 2018 install_conversion_constant("kg s-1", "mm/day", 86400) should be an error #130 Closed @Enchufa2 Copy link Member

Enchufa2 commented Mar 21, 2018 edited Loading

I saw a pattern and replied too fast. What I've said still holds though: if you try to define, for example, "mmperday" as a custom unit and try to install a conversion, it won't work with the current version.

But if you want to stick to standard units, @edzer is right: you are missing something for that conversion.

All reactions @Enchufa2 Enchufa2 removed the duplicate label Mar 21, 2018 @edzer Copy link Member

edzer commented Mar 21, 2018

mmwaterperday

👍 1 Enchufa2 reacted with thumbs up emoji All reactions
  • 👍 1 reaction
@Enchufa2 Copy link Member

Enchufa2 commented Mar 21, 2018

And you are right once more. ;-)

All reactions @adrfantini Copy link Author

adrfantini commented Mar 29, 2018

I see, I'll resort to a multiplication by a constant then, with a check flag.

Anyway, for future reference, I found that this worked:

specialUnits <- c("mm/d", "mm/day", "mm/h", "mm/3h", "mm/6h", "mm/12h", "kg/m2/s", "kg/m**2/s", "kg/m^2/s") for (u in specialUnits) { install_symbolic_unit(u, warn=FALSE) } for (kgm2s in c("kg/m2/s", "kg/m**2/s", "kg/m^2/s")) { for (mmday in c("mm/d", "mm/day")) { install_conversion_function(kgm2s, mmday, function(x) x*86400) #Apparently bi-directionality does not work install_conversion_function(mmday, kgm2s, function(x) x/86400) } install_conversion_function(kgm2s, "mm/h", function(x) x*86400/24) install_conversion_function(kgm2s, "mm/3h", function(x) x*86400/8) install_conversion_function(kgm2s, "mm/6h", function(x) x*86400/4) install_conversion_function(kgm2s, "mm/12h", function(x) x*86400/2) install_conversion_function("mm/h", kgm2s, function(x) x/86400*24) install_conversion_function("mm/3h", kgm2s, function(x) x/86400*8) install_conversion_function("mm/6h", kgm2s, function(x) x/86400*4) install_conversion_function("mm/12h", kgm2s, function(x) x/86400*2) }

Check that this works:

a <- NULL for (kgm2s in c("kg/m2/s", "kg/m**2/s", "kg/m^2/s")) { for (mmday in c("mm/d", "mm/day", "mm/h", "mm/3h", "mm/6h", "mm/12h")) { inu <- as_units(1, kgm2s) units(inu) <- mmday a <- rbind(a, data.frame(from=kgm2s, to=mmday, conversion=as.numeric(inu))) } }

Returns:

from to conversion 1 kg/m2/s mm/d 86400 2 kg/m2/s mm/day 86400 3 kg/m2/s mm/h 3600 4 kg/m2/s mm/3h 10800 5 kg/m2/s mm/6h 21600 6 kg/m2/s mm/12h 43200 7 kg/m**2/s mm/d 86400 8 kg/m**2/s mm/day 86400 9 kg/m**2/s mm/h 3600 10 kg/m**2/s mm/3h 10800 11 kg/m**2/s mm/6h 21600 12 kg/m**2/s mm/12h 43200 13 kg/m^2/s mm/d 86400 14 kg/m^2/s mm/day 86400 15 kg/m^2/s mm/h 3600 16 kg/m^2/s mm/3h 10800 17 kg/m^2/s mm/6h 21600 18 kg/m^2/s mm/12h 43200
All reactions @edzer Copy link Member

edzer commented Mar 29, 2018

Fine if this solves a problem for you now, but as I mentioned before, in upcoming versions of units we will make this an error; it is not the solution to your problem.

All reactions @adrfantini Copy link Author

adrfantini commented Mar 29, 2018 edited Loading

Indeed, I see the thinking behind this. In fact I already implemented something along these lines:

wd <- set_units(1000, "kg m-3") (1 %>% set_units("kg m-2 s-1") / wd) %>% set_units("mm/day")

This is of course much better than defining custom units. (or at least it does look so to me!)

The post above was just to underline that that approach currently works, even though it's against the design philosophy of the package.

👍 1 edzer reacted with thumbs up emoji All reactions
  • 👍 1 reaction
@adrfantini adrfantini closed this as completed Mar 29, 2018 Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment Assignees No one assigned Labels None yet Projects None yet Milestone No milestone Development

No branches or pull requests

3 participants @edzer @Enchufa2 @adrfantini You can’t perform that action at this time.

Từ khóa » Kg M2 S-1