imagej / imagej-common

ImageJ core data model
https://imagej.net/libs/imagej-common
BSD 2-Clause "Simplified" License
9 stars 18 forks source link

UnitService cannot parse mu symbol #86

Closed kephale closed 4 years ago

kephale commented 4 years ago

I'm trying to convert units specified in image files that have $\mu m$ as their unit, but I get:

[ERROR] Cannot parse unit: \u00B5m
ucar.units.UnitParseException: Specification error: \u00B5m
    at ucar.units.StandardUnitFormat.parse(StandardUnitFormat.java:145)
    at ucar.units.UnitFormatImpl.parse(UnitFormatImpl.java:74)
    at net.imagej.units.DefaultUnitService.parseUnit(DefaultUnitService.java:151)
    at net.imagej.units.DefaultUnitService.findConversion(DefaultUnitService.java:133)
    at net.imagej.units.DefaultUnitService.value(DefaultUnitService.java:77)
    at sc.iview.SciView.addVolume(SciView.java:1312)
    at sc.iview.SciView.open(SciView.java:1039)
    at sc.iview.commands.file.Open.run(Open.java:67)
    at org.scijava.command.CommandModule.run(CommandModule.java:199)
    at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
    at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
    at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
    at org.scijava.thread.DefaultThreadService$3.call(DefaultThreadService.java:238)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: ucar.units.TokenMgrError: Lexical error at line 1, column 1.  Encountered: "\\" (92), after : ""
    at ucar.units.StandardUnitFormatTokenManager.getNextToken(StandardUnitFormatTokenManager.java:570)
    at ucar.units.StandardUnitFormat.jj_ntk(StandardUnitFormat.java:1693)
    at ucar.units.StandardUnitFormat.unitSpec(StandardUnitFormat.java:456)
    at ucar.units.StandardUnitFormat.parse(StandardUnitFormat.java:140)
    ... 16 more
kephale commented 4 years ago

For reference, there is a unit format guide here https://www.unidata.ucar.edu/software/udunits/udunits-2.1.24/udunits2lib.html

ImageJ does allow the unit to be set to "μm" in Image>Properties, so ImageJ can also be the cause of this issue (even if other software could be the source of nonstandard characters in some cases)

ctrueden commented 4 years ago

Probably easiest would be to add some sanitation layer to the DefaultUnitService, no?

kephale commented 4 years ago

Agreed, I'll poke a bit and link at what I find here

kephale commented 4 years ago

This might be too lazy, but the only other nonstandard character that seems to be possible is the degree symbol and it is not clear that we need to consider that (except maybe for imaging devices that use a rotating axis).

https://github.com/scenerygraphics/sciview/commit/046468544178e3566e1416a05bbea87b1673d56e#diff-328c895a4e03bed5729f445492f1b269R1307

ctrueden commented 4 years ago

Fixed by #87.