@Test public void testUcumToUdunits() throws Exception { testUcumToUdunits("A", "A"); testUcumToUdunits("[acr_us]", "acre"); testUcumToUdunits("deg", "degree"); testUcumToUdunits("deg{east}", "degree_east"); testUcumToUdunits("deg{north}", "degree_north"); testUcumToUdunits("deg{true}", "degree_true"); testUcumToUdunits("deg{west}", "degree_west"); testUcumToUdunits("KiBd", "1024.baud"); testUcumToUdunits("dk{count}", "dkcount"); testUcumToUdunits("kiloBobs", "kiloBobs"); testUcumToUdunits("deg{bob}", "degree{bob}"); testUcumToUdunits("^", "^"); testUcumToUdunits(".", " "); testUcumToUdunits("''", "arc_second"); testUcumToUdunits("'", "arc_minute"); testUcumToUdunits("deg", "degree"); testUcumToUdunits("[degF]", "degree_F"); testUcumToUdunits("5/9.K", "5/9 degree_K"); testUcumToUdunits("Cel", "degree_C"); testUcumToUdunits("K", "degree_K"); testUcumToUdunits("s{since 1970-01-01T00:00:00Z}", "s since 1970-01-01T00:00:00Z"); testUcumToUdunits("Gb{since 1970-01-01T00}", "gilbert since 1970-01-01T00"); testUcumToUdunits("s.m{ since 1970-01-01T00}", "s m{ since 1970-01-01T00}"); }
public static String ucumToUdunits(String ucum) { if (ucum == null) { return null; } StringBuilder udunits = new StringBuilder(); int ucLength = ucum.length(); if (ucLength == 0) { return ""; } if (ucum.charAt(ucLength - 1) == '}' && ucum.indexOf('}') == ucLength - 1) { int sincePo = ucum.indexOf("{since "); if (sincePo > 0) { String tUdunits = ucHashMap.get(ucum.substring(0, sincePo)); if (tUdunits != null) { return tUdunits + " " + ucum.substring(sincePo + 1, ucLength - 1); } } } int po = 0; while (po < ucLength) { char ch = ucum.charAt(po); if (isUcumLetter(ch)) { int po2 = po + 1; while (po2 < ucLength && (isUcumLetter(ucum.charAt(po2)) || ucum.charAt(po2) == '_' || ErddapString2.isDigit(ucum.charAt(po2)))) { po2++; } String tUcum = ucum.substring(po, po2); po = po2; int firstDigit = tUcum.length(); while (firstDigit >= 1 && ErddapString2.isDigit(tUcum.charAt(firstDigit - 1))) { firstDigit--; } String exponent = tUcum.substring(firstDigit); tUcum = tUcum.substring(0, firstDigit); String tUdunits = oneUcumToUdunits(tUcum); if (tUdunits.equals("/")) { char lastUdunits = udunits.length() == 0 ? '\u0000' : udunits.charAt(udunits.length() - 1); if (lastUdunits == '/') { udunits.setCharAt(udunits.length() - 1, '.'); } else if (lastUdunits == '.') { udunits.setCharAt(udunits.length() - 1, '/'); } else { udunits.append('/'); } } else { udunits.append(tUdunits); } udunits.append(exponent); continue; } if (ch == '-' || ErddapString2.isDigit(ch)) { int po2 = po + 1; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } if (po2 < ucLength - 1 && Character.toLowerCase(ucum.charAt(po2)) == '^' && (ucum.charAt(po2 + 1) == '-' || ErddapString2.isDigit(ucum.charAt(po2 + 1)))) { po2 += 2; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } } String num = ucum.substring(po, po2); po = po2; udunits.append(num); continue; } if (ch == '.') { po++; udunits.append(' '); continue; } if (ch == '*') { po++; udunits.append('^'); continue; } if (ch == '\'') { po++; if (po < ucLength && ucum.charAt(po) == '\'') { udunits.append("arc_second"); po++; } else { udunits.append("arc_minute"); } continue; } udunits.append(ch); po++; } return udunits.toString(); }
ErddapEDUnits { public static String ucumToUdunits(String ucum) { if (ucum == null) { return null; } StringBuilder udunits = new StringBuilder(); int ucLength = ucum.length(); if (ucLength == 0) { return ""; } if (ucum.charAt(ucLength - 1) == '}' && ucum.indexOf('}') == ucLength - 1) { int sincePo = ucum.indexOf("{since "); if (sincePo > 0) { String tUdunits = ucHashMap.get(ucum.substring(0, sincePo)); if (tUdunits != null) { return tUdunits + " " + ucum.substring(sincePo + 1, ucLength - 1); } } } int po = 0; while (po < ucLength) { char ch = ucum.charAt(po); if (isUcumLetter(ch)) { int po2 = po + 1; while (po2 < ucLength && (isUcumLetter(ucum.charAt(po2)) || ucum.charAt(po2) == '_' || ErddapString2.isDigit(ucum.charAt(po2)))) { po2++; } String tUcum = ucum.substring(po, po2); po = po2; int firstDigit = tUcum.length(); while (firstDigit >= 1 && ErddapString2.isDigit(tUcum.charAt(firstDigit - 1))) { firstDigit--; } String exponent = tUcum.substring(firstDigit); tUcum = tUcum.substring(0, firstDigit); String tUdunits = oneUcumToUdunits(tUcum); if (tUdunits.equals("/")) { char lastUdunits = udunits.length() == 0 ? '\u0000' : udunits.charAt(udunits.length() - 1); if (lastUdunits == '/') { udunits.setCharAt(udunits.length() - 1, '.'); } else if (lastUdunits == '.') { udunits.setCharAt(udunits.length() - 1, '/'); } else { udunits.append('/'); } } else { udunits.append(tUdunits); } udunits.append(exponent); continue; } if (ch == '-' || ErddapString2.isDigit(ch)) { int po2 = po + 1; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } if (po2 < ucLength - 1 && Character.toLowerCase(ucum.charAt(po2)) == '^' && (ucum.charAt(po2 + 1) == '-' || ErddapString2.isDigit(ucum.charAt(po2 + 1)))) { po2 += 2; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } } String num = ucum.substring(po, po2); po = po2; udunits.append(num); continue; } if (ch == '.') { po++; udunits.append(' '); continue; } if (ch == '*') { po++; udunits.append('^'); continue; } if (ch == '\'') { po++; if (po < ucLength && ucum.charAt(po) == '\'') { udunits.append("arc_second"); po++; } else { udunits.append("arc_minute"); } continue; } udunits.append(ch); po++; } return udunits.toString(); } }
ErddapEDUnits { public static String ucumToUdunits(String ucum) { if (ucum == null) { return null; } StringBuilder udunits = new StringBuilder(); int ucLength = ucum.length(); if (ucLength == 0) { return ""; } if (ucum.charAt(ucLength - 1) == '}' && ucum.indexOf('}') == ucLength - 1) { int sincePo = ucum.indexOf("{since "); if (sincePo > 0) { String tUdunits = ucHashMap.get(ucum.substring(0, sincePo)); if (tUdunits != null) { return tUdunits + " " + ucum.substring(sincePo + 1, ucLength - 1); } } } int po = 0; while (po < ucLength) { char ch = ucum.charAt(po); if (isUcumLetter(ch)) { int po2 = po + 1; while (po2 < ucLength && (isUcumLetter(ucum.charAt(po2)) || ucum.charAt(po2) == '_' || ErddapString2.isDigit(ucum.charAt(po2)))) { po2++; } String tUcum = ucum.substring(po, po2); po = po2; int firstDigit = tUcum.length(); while (firstDigit >= 1 && ErddapString2.isDigit(tUcum.charAt(firstDigit - 1))) { firstDigit--; } String exponent = tUcum.substring(firstDigit); tUcum = tUcum.substring(0, firstDigit); String tUdunits = oneUcumToUdunits(tUcum); if (tUdunits.equals("/")) { char lastUdunits = udunits.length() == 0 ? '\u0000' : udunits.charAt(udunits.length() - 1); if (lastUdunits == '/') { udunits.setCharAt(udunits.length() - 1, '.'); } else if (lastUdunits == '.') { udunits.setCharAt(udunits.length() - 1, '/'); } else { udunits.append('/'); } } else { udunits.append(tUdunits); } udunits.append(exponent); continue; } if (ch == '-' || ErddapString2.isDigit(ch)) { int po2 = po + 1; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } if (po2 < ucLength - 1 && Character.toLowerCase(ucum.charAt(po2)) == '^' && (ucum.charAt(po2 + 1) == '-' || ErddapString2.isDigit(ucum.charAt(po2 + 1)))) { po2 += 2; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } } String num = ucum.substring(po, po2); po = po2; udunits.append(num); continue; } if (ch == '.') { po++; udunits.append(' '); continue; } if (ch == '*') { po++; udunits.append('^'); continue; } if (ch == '\'') { po++; if (po < ucLength && ucum.charAt(po) == '\'') { udunits.append("arc_second"); po++; } else { udunits.append("arc_minute"); } continue; } udunits.append(ch); po++; } return udunits.toString(); }  }
ErddapEDUnits { public static String ucumToUdunits(String ucum) { if (ucum == null) { return null; } StringBuilder udunits = new StringBuilder(); int ucLength = ucum.length(); if (ucLength == 0) { return ""; } if (ucum.charAt(ucLength - 1) == '}' && ucum.indexOf('}') == ucLength - 1) { int sincePo = ucum.indexOf("{since "); if (sincePo > 0) { String tUdunits = ucHashMap.get(ucum.substring(0, sincePo)); if (tUdunits != null) { return tUdunits + " " + ucum.substring(sincePo + 1, ucLength - 1); } } } int po = 0; while (po < ucLength) { char ch = ucum.charAt(po); if (isUcumLetter(ch)) { int po2 = po + 1; while (po2 < ucLength && (isUcumLetter(ucum.charAt(po2)) || ucum.charAt(po2) == '_' || ErddapString2.isDigit(ucum.charAt(po2)))) { po2++; } String tUcum = ucum.substring(po, po2); po = po2; int firstDigit = tUcum.length(); while (firstDigit >= 1 && ErddapString2.isDigit(tUcum.charAt(firstDigit - 1))) { firstDigit--; } String exponent = tUcum.substring(firstDigit); tUcum = tUcum.substring(0, firstDigit); String tUdunits = oneUcumToUdunits(tUcum); if (tUdunits.equals("/")) { char lastUdunits = udunits.length() == 0 ? '\u0000' : udunits.charAt(udunits.length() - 1); if (lastUdunits == '/') { udunits.setCharAt(udunits.length() - 1, '.'); } else if (lastUdunits == '.') { udunits.setCharAt(udunits.length() - 1, '/'); } else { udunits.append('/'); } } else { udunits.append(tUdunits); } udunits.append(exponent); continue; } if (ch == '-' || ErddapString2.isDigit(ch)) { int po2 = po + 1; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } if (po2 < ucLength - 1 && Character.toLowerCase(ucum.charAt(po2)) == '^' && (ucum.charAt(po2 + 1) == '-' || ErddapString2.isDigit(ucum.charAt(po2 + 1)))) { po2 += 2; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } } String num = ucum.substring(po, po2); po = po2; udunits.append(num); continue; } if (ch == '.') { po++; udunits.append(' '); continue; } if (ch == '*') { po++; udunits.append('^'); continue; } if (ch == '\'') { po++; if (po < ucLength && ucum.charAt(po) == '\'') { udunits.append("arc_second"); po++; } else { udunits.append("arc_minute"); } continue; } udunits.append(ch); po++; } return udunits.toString(); }  static String udunitsToUcum(String udunits); static String ucumToUdunits(String ucum); static HashMap<String, String> getHashMapStringString(InputStream inputStream, String charset); }
ErddapEDUnits { public static String ucumToUdunits(String ucum) { if (ucum == null) { return null; } StringBuilder udunits = new StringBuilder(); int ucLength = ucum.length(); if (ucLength == 0) { return ""; } if (ucum.charAt(ucLength - 1) == '}' && ucum.indexOf('}') == ucLength - 1) { int sincePo = ucum.indexOf("{since "); if (sincePo > 0) { String tUdunits = ucHashMap.get(ucum.substring(0, sincePo)); if (tUdunits != null) { return tUdunits + " " + ucum.substring(sincePo + 1, ucLength - 1); } } } int po = 0; while (po < ucLength) { char ch = ucum.charAt(po); if (isUcumLetter(ch)) { int po2 = po + 1; while (po2 < ucLength && (isUcumLetter(ucum.charAt(po2)) || ucum.charAt(po2) == '_' || ErddapString2.isDigit(ucum.charAt(po2)))) { po2++; } String tUcum = ucum.substring(po, po2); po = po2; int firstDigit = tUcum.length(); while (firstDigit >= 1 && ErddapString2.isDigit(tUcum.charAt(firstDigit - 1))) { firstDigit--; } String exponent = tUcum.substring(firstDigit); tUcum = tUcum.substring(0, firstDigit); String tUdunits = oneUcumToUdunits(tUcum); if (tUdunits.equals("/")) { char lastUdunits = udunits.length() == 0 ? '\u0000' : udunits.charAt(udunits.length() - 1); if (lastUdunits == '/') { udunits.setCharAt(udunits.length() - 1, '.'); } else if (lastUdunits == '.') { udunits.setCharAt(udunits.length() - 1, '/'); } else { udunits.append('/'); } } else { udunits.append(tUdunits); } udunits.append(exponent); continue; } if (ch == '-' || ErddapString2.isDigit(ch)) { int po2 = po + 1; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } if (po2 < ucLength - 1 && Character.toLowerCase(ucum.charAt(po2)) == '^' && (ucum.charAt(po2 + 1) == '-' || ErddapString2.isDigit(ucum.charAt(po2 + 1)))) { po2 += 2; while (po2 < ucLength && ErddapString2.isDigit(ucum.charAt(po2))) { po2++; } } String num = ucum.substring(po, po2); po = po2; udunits.append(num); continue; } if (ch == '.') { po++; udunits.append(' '); continue; } if (ch == '*') { po++; udunits.append('^'); continue; } if (ch == '\'') { po++; if (po < ucLength && ucum.charAt(po) == '\'') { udunits.append("arc_second"); po++; } else { udunits.append("arc_minute"); } continue; } udunits.append(ch); po++; } return udunits.toString(); }  static String udunitsToUcum(String udunits); static String ucumToUdunits(String ucum); static HashMap<String, String> getHashMapStringString(InputStream inputStream, String charset); static String metricName; static String metricAcronym; static int nMetric; static String twoAcronym; static String twoValue; static int nTwo; }