11package mil .army .usace .hec .vortex .util ;
22
3+ import hec .heclib .dss .HecDataConversion ;
34import systems .uom .common .USCustomary ;
5+ import tech .units .indriya .format .SimpleUnitFormat ;
6+ import tech .units .indriya .unit .TransformedUnit ;
47
58import javax .measure .Unit ;
9+ import javax .measure .format .MeasurementParseException ;
10+ import javax .measure .format .UnitFormat ;
611import java .util .Optional ;
712
813import static javax .measure .MetricPrefix .*;
9- import static systems .uom .common .USCustomary .FAHRENHEIT ;
10- import static systems .uom .common .USCustomary .INCH ;
14+ import static systems .uom .common .USCustomary .*;
1115import static tech .units .indriya .AbstractUnit .ONE ;
16+ import static tech .units .indriya .unit .Units .HOUR ;
17+ import static tech .units .indriya .unit .Units .MINUTE ;
1218import static tech .units .indriya .unit .Units .*;
1319
1420public class UnitUtil {
21+ static {
22+ USCustomary .getInstance ();
23+ }
24+
25+ private UnitUtil () {
26+ // Utility Class
27+ }
28+
1529 public static Unit <?> getUnits (String units ) {
30+ if (units == null ) {
31+ return ONE ;
32+ }
33+
1634 return switch (units .toLowerCase ()) {
1735 case "kg.m-2.s-1" , "kg m-2 s-1" , "kg/m2s" , "mm/s" , "mm s-1" -> MILLI (METRE ).divide (SECOND );
1836 case "mm hr^-1" , "mm/hr" -> MILLI (METRE ).divide (HOUR );
1937 case "mm/day" , "mm/d" -> MILLI (METRE ).divide (DAY );
20- case "kg.m-2" , "kg/m^2" , "kg m^-2" , "kg m-2" , "mm" , "millimeters h20 " , "millimeters snow thickness" ->
21- MILLI (METRE );
38+ case "kg.m-2" , "kg/m^2" , "kg m^-2" , "kg m-2" , "mm" , "millimeter " , "millimeters" , "millimeters h20" ,
39+ "millimeters snow thickness" -> MILLI (METRE );
2240 case "in" , "inch" , "inches" -> INCH ;
23- case "1/1000 in" -> ONE .divide (INCH .multiply (1000 ));
41+ case "ft" , "foot" , "feet" -> FOOT ;
42+ case "1/1000 in" -> TransformedUnit .parse ("in/1000" );
43+ case "in/hr" -> INCH .divide (HOUR );
2444 case "celsius" , "degrees c" , "deg c" , "deg_c" , "degc" , "c" -> CELSIUS ;
2545 case "degc-d" -> CELSIUS .multiply (DAY );
26- case "degf-d" -> FAHRENHEIT .multiply (DAY );
46+ case "degf-d" -> FAHRENHEIT .multiply (DAY );
2747 case "fahrenheit" , "deg f" , "deg_f" , "degf" , "f" -> FAHRENHEIT ;
2848 case "kelvin" , "k" -> KELVIN ;
2949 case "watt/m2" , "w m-2" -> WATT .divide (SQUARE_METRE );
@@ -34,11 +54,12 @@ public static Unit<?> getUnits(String units) {
3454 case "hpa" -> HECTO (PASCAL );
3555 case "kpa" -> KILO (PASCAL );
3656 case "pa" -> PASCAL ;
37- case "m" , "meter" , "metre" -> METRE ;
57+ case "m" , "meter" , "metre" , "meters" -> METRE ;
3858 case "min" -> MINUTE ;
3959 case "km" -> KILO (METRE );
4060 case "degrees" , "degrees_east" , "degrees_north" -> USCustomary .DEGREE_ANGLE ;
41- default -> ONE ;
61+ case "hr" -> HOUR ;
62+ default -> parseSimpleUnitFormat (units );
4263 };
4364 }
4465
@@ -47,4 +68,29 @@ public static boolean equals(String units1, String units2) {
4768 .map (u -> u .equalsIgnoreCase (units2 ))
4869 .orElse (units2 == null );
4970 }
71+
72+ private static Unit <?> parseSimpleUnitFormat (String units ) {
73+ if (units == null || units .isBlank ()) {
74+ return null ;
75+ }
76+
77+ try {
78+ UnitFormat format = SimpleUnitFormat .getInstance ();
79+ return format .parse (units );
80+ } catch (MeasurementParseException mpe ) {
81+ return ONE ;
82+ }
83+ }
84+
85+ public static boolean isSquareFeet (String label ) {
86+ return label .equalsIgnoreCase ("ft2" ) || label .equalsIgnoreCase ("sqft" );
87+ }
88+
89+ public static boolean isAcre (String label ) {
90+ return label .equalsIgnoreCase ("ac" ) || label .equalsIgnoreCase ("acre" );
91+ }
92+
93+ public static boolean isEnglishUnitSystem (int unitSystem ) {
94+ return unitSystem == HecDataConversion .ENGLISH_UNITS ;
95+ }
5096}
0 commit comments