Specify set of base units to use in UnitConvert
Based on one of the solutions in the second relevant source cited, the following function will convert all units to SI, except the base units you specify in the second argument. The base units can be given as Quantity
or strings that Quantity
can convert
units::notbase = "The argument `1` is not a base unit.";
baserule[base_Quantity] :=
Module[{ud},
If[
MatchQ[ud = UnitDimensions[base], {{_, 1}}],
ud[[1,1]] -> QuantityUnit[base],
Message[units::notbase, base]; Nothing
]
];
baserule[base_String] := baserule[Quantity[base]];
unitConvert[q_Quantity, bases_List] :=
UnitConvert[q,
Quantity[Times @@ Apply[Power, UnitDimensions[q] /. baserule /@ bases /.
{"LengthUnit" -> "Meters", "MassUnit" -> "Kilograms", "TimeUnit" -> "Seconds",
"ElectricCurrentUnit" -> "Amperes", "TemperatureUnit" -> "Kelvins",
"TemperatureDifferenceUnit" -> "KelvinsDifference", "AmountUnit" -> "Moles",
"LuminousIntensityUnit" -> "Candelas"}, {1}]
]
]
unitConvert[
Quantity[1, "Watts"]/Quantity[2, "meters"],
{"cm", "g", "K difference", Quantity[1, "Seconds"]}
]
(* Quantity[50000, ("Centimeters"*"Grams")/"Seconds"^3] *)
Based on @DanielW's answer, here's a simplified conversion that assumes you use a fixed unit system:
toCGS[x_Quantity] := UnitConvert[x, Times @@ Power @@@ (UnitDimensions[x] /.
{"LengthUnit" -> "Centimeters",
"MassUnit" -> "Grams",
"TimeUnit" -> "Seconds",
"ElectricCurrentUnit" -> "Amperes",
"TemperatureUnit" -> "Kelvins",
"TemperatureDifferenceUnit" -> "KelvinsDifference",
"AmountUnit" -> "Moles",
"LuminousIntensityUnit" -> "Candelas"})]