Okay, so let’s take these UCUM APIs out for a spin.
https://ucum.nlm.nih.gov/
Although, for those who are not always connected to the internet we may want to actually go ahead and use the ucum-lhc javascript library.
https://ucum.nlm.nih.gov/ucum-lhc/
Looks like it’s on npm. You can also clone it from github.
Anyways, let’s start with the validation and conversion api.
https://ucum.nlm.nih.gov/ucum-service.html
So there’s three services available at this time
- UCUM unit Conversion web service
- UCUM unit validation web service
- UCUM unit to base units conversion
Looks like it returns xml by default. But you can change it to json or plaintext by specifying in the query.
Also you have to do percent encoding. This changes things from square and curly brackets {} to a percent sign with numbers after it that tell you what special character it is.
There’s some pre-existing php code to do percent encoding. I talk about this in this thread looking into GUDID
In a nutshell, you take whatever text you need percent encoded, and use the below command
$UDI= urlencode($DI);
Here’s a few examples where the brackets are replaced with percent encoding.
2 inches → centimeters
https://ucum.nlm.nih.gov/ucum-service/v1/ucumtransform/2/from/[in_i]/to/cm
3 and a half feet to micrometers
I’m not sure how you know where to put the braces
You can also do conversions where it’s two units multiplied or divided.
It should be okay to use a / for division and . for multiplication since it automatically knows to grab everything after the from until the /before the to.
So if you’re not sure where to put the square braces or how to represent the units you’re converting from or to, it looks like you just have to trial and error using the validation service.
https://ucum.nlm.nih.gov/ucum-service.html#validation