Java Code Examples for ucar.ma2.DataType#unsignedByteToShort()

The following examples show how to use ucar.ma2.DataType#unsignedByteToShort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GribData.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static double entropy(byte[] data) {
  int[] p = new int[256];

  // count occurrences
  for (byte b : data) {
    short s = DataType.unsignedByteToShort(b);
    p[s]++;
  }

  double n = data.length;
  double iln2 = 1.0 / Math.log(2.0);
  double sum = 0.0;
  for (int i = 0; i < 256; i++) {
    if (p[i] == 0)
      continue;
    double prob = ((double) p[i]) / n;
    sum += Math.log(prob) * prob * iln2;
  }

  return (sum == 0) ? 0.0 : -sum;
}
 
Example 2
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array convertEnums(Map<Integer, String> map, DataType dataType, Array values) {
  Array result = Array.factory(DataType.STRING, values.getShape());
  IndexIterator ii = result.getIndexIterator();
  values.resetLocalIterator();
  while (values.hasNext()) {
    int ival;
    if (dataType == DataType.ENUM1)
      ival = (int) DataType.unsignedByteToShort(values.nextByte());
    else if (dataType == DataType.ENUM2)
      ival = DataType.unsignedShortToInt(values.nextShort());
    else
      ival = values.nextInt();
    String sval = map.get(ival);
    if (sval == null)
      sval = "Unknown enum value=" + ival;
    ii.setObjectNext(sval);
  }
  return result;
}
 
Example 3
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public long readVariableSizeUnsigned(int size) throws IOException {
  long vv;
  if (size == 1) {
    vv = DataType.unsignedByteToShort(raf.readByte());
  } else if (size == 2) {
    if (debugPos) {
      log.debug("position={}", raf.getFilePointer());
    }
    short s = raf.readShort();
    vv = DataType.unsignedShortToInt(s);
  } else if (size == 4) {
    vv = DataType.unsignedIntToLong(raf.readInt());
  } else if (size == 8) {
    vv = raf.readLong();
  } else {
    vv = readVariableSizeN(size);
  }
  return vv;
}
 
Example 4
Source File: Grib1DataTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void showBitmap(Grib1RecordBean bean1, Formatter f) throws IOException {
  byte[] bitmap;
  try (ucar.unidata.io.RandomAccessFile raf = bean1.getRaf()) {
    Grib1SectionBitMap bms = bean1.gr.getBitMapSection();
    f.format("%s%n", bms);
    bitmap = bms.getBitmap(raf);
  }

  if (bitmap == null) {
    f.format(" no bitmap%n");
    return;
  }

  int count = 0;
  int bits = 0;
  for (byte b : bitmap) {
    short s = DataType.unsignedByteToShort(b);
    bits += Long.bitCount(s);
    f.format("%8s", Long.toBinaryString(s));
    if (++count % 10 == 0)
      f.format("%n");
  }
  f.format("%n%n#bits on = %d%n", bits);
  f.format("bitmap size = %d%n", 8 * count);
}
 
Example 5
Source File: Grib2DataPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void showBitmap(Grib2RecordBean bean1, Formatter f) throws IOException {
  byte[] bitmap;
  try (ucar.unidata.io.RandomAccessFile raf = bean1.getRaf()) {
    Grib2SectionBitMap bms = bean1.gr.getBitmapSection();
    f.format("%s%n", bms);
    bitmap = bms.getBitmap(raf);
  }

  if (bitmap == null) {
    f.format(" no bitmap%n");
    return;
  }

  int count = 0;
  int bits = 0;
  for (byte b : bitmap) {
    short s = DataType.unsignedByteToShort(b);
    bits += Long.bitCount(s);
    f.format("%8s", Long.toBinaryString(s));
    if (++count % 10 == 0)
      f.format("%n");
  }
  f.format("%n%n#bits on = %d%n", bits);
  f.format("total nbits = %d%n", 8 * count);
  f.format("bitmap nbytes = %d%n", bitmap.length);
}
 
Example 6
Source File: Giniheader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private double readScaledInt(ByteBuffer buf) {
  // Get the first two bytes
  short s1 = buf.getShort();

  // And the last one as unsigned
  short s2 = DataType.unsignedByteToShort(buf.get());

  // Get the sign bit, converting from 0 or 2 to +/- 1.
  int posneg = 1 - ((s1 & 0x8000) >> 14);

  // Combine the first two bytes (without sign bit) with the last byte.
  // Multiply by proper factor for +/-
  int nn = (((s1 & 0x7FFF) << 8) | s2) * posneg;
  return (double) nn / 10000.0;
}
 
Example 7
Source File: GribNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int countBits(byte[] bitmap) {
  int bits = 0;
  for (byte b : bitmap) {
    short s = DataType.unsignedByteToShort(b);
    bits += Long.bitCount(s);
  }
  return bits;
}
 
Example 8
Source File: GribIndexPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void toRawGdsString(Formatter f) {
  byte[] bytes = gds.getRawBytes();
  int count = 1;
  for (byte b : bytes) {
    short s = DataType.unsignedByteToShort(b);
    f.format(" %d : %d%n", count++, s);
  }
}
 
Example 9
Source File: Grib2DataPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void toRawPdsString(Formatter f) {
  byte[] bytes = gr.getPDSsection().getRawBytes();
  int count = 1;
  for (byte b : bytes) {
    short s = DataType.unsignedByteToShort(b);
    f.format(" %d : %d%n", count++, s);
  }
}
 
Example 10
Source File: Grib2CollectionPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void toRawPdsString(Formatter f) {
  byte[] bytes = gr.getPDSsection().getRawBytes();
  int count = 1;
  for (byte b : bytes) {
    short s = DataType.unsignedByteToShort(b);
    f.format(" %d : %d%n", count++, s);
  }
}
 
Example 11
Source File: Grib2CollectionPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void toRawGdsString(Formatter f) {
  byte[] bytes = gds.getRawBytes();
  int count = 1;
  for (byte b : bytes) {
    short s = DataType.unsignedByteToShort(b);
    f.format(" %d : %d%n", count++, s);
  }
}
 
Example 12
Source File: Misc.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String showBits(byte[] bytes) {
  try (Formatter f = new Formatter()) {
    int count = 0;
    for (byte b : bytes) {
      short s = DataType.unsignedByteToShort(b);
      f.format("%8s", Long.toBinaryString(s));
      if (++count % 10 == 0)
        f.format("%n");
    }
    return f.toString();
  }
}
 
Example 13
Source File: TestBufferedImage.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public byte[] convert(String srcPath, double a, double b) throws IOException {
  try (NetcdfFile ncfile = NetcdfFiles.open(srcPath)) {
    Variable v = ncfile.findVariable("image1/image_data");
    Array array = v.read();

    int[] cmap = new int[256]; // palette
    cmap[0] = 0x00FFFFFF; // transparent and white
    for (int i = 1; i != 256; i++) {
      // 1 to 255 renders as (almost) white to black
      cmap[i] = 0xFF000000 | ((0xFF - i) * 0x010101);
    }
    IndexColorModel colorModel =
        new IndexColorModel(8, cmap.length, cmap, 0, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

    int[] shape = array.getShape();
    BufferedImage bi = new BufferedImage(shape[1], shape[0], BufferedImage.TYPE_BYTE_INDEXED, colorModel);

    Index index = array.getIndex();
    for (int y = 0; y < shape[0]; y++) {
      for (int x = 0; x < shape[1]; x++) {
        index.set(y, x);

        byte bval = array.getByte(index);
        double dval = v.getDataType().isUnsigned() ? (double) DataType.unsignedByteToShort(bval) : (double) bval;

        // double dval = array.getDouble(index);
        // Fix for NetCDF returning all values larger than 127 as (value - 256):
        // if (dval < -1) {
        // dval += 256;
        // }
        int pval = (int) Math.round(a * dval + b);
        pval = Math.min(Math.max(pval, 0), 255);
        bi.getRaster().setSample(x, y, 0, pval);
      }
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(bi, "png", os);
    return os.toByteArray();
  }
}
 
Example 14
Source File: H5objects.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private int makeUnsignedIntFromBytes(byte upper, byte lower) {
  return DataType.unsignedByteToShort(upper) * 256 + DataType.unsignedByteToShort(lower);
}
 
Example 15
Source File: Giniheader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
int[] getCalibrationInfo(ByteBuffer bos, int phys_elem, int ent_id) {

    bos.position(46);
    byte nv = bos.get(); /* Cal indicator */
    int navcal = DataType.unsignedByteToShort(nv);
    int[] calcods = null;
    if (navcal == 128) { /* Unidata Cal block found; unpack values */
      int scale = 10000;
      int jscale = 100000000;
      byte[] unsb = new byte[8];
      bos.get(unsb);
      bos.position(55);
      nv = bos.get();
      int calcod = DataType.unsignedByteToShort(nv);

      if (calcod > 0) {
        calcods = new int[5 * calcod + 1];
        calcods[0] = calcod;
        for (int i = 0; i < calcod; i++) {

          bos.position(56 + i * 16);
          int minb = bos.getInt() / 10000; /* min brightness values */
          int maxb = bos.getInt() / 10000; /* max brightness values */
          int mind = bos.getInt(); /* min data values */
          int maxd = bos.getInt(); /* max data values */

          int idscal = 1;
          while (mind % idscal == 0 && maxd % idscal == 0) {
            idscal *= 10;
          }
          idscal /= 10;

          if (idscal < jscale)
            jscale = idscal;

          calcods[1 + i * 5] = mind;
          calcods[2 + i * 5] = maxd;
          calcods[3 + i * 5] = minb;
          calcods[4 + i * 5] = maxb;
          calcods[5 + i * 5] = 0;

        }

        if (jscale > scale)
          jscale = scale;
        scale /= jscale;

        if (gini_GetPhysElemID(phys_elem, ent_id).contains("Precipitation")) {
          if (scale < 100) {
            jscale /= (100 / scale);
            scale = 100;
          }
        }

        for (int i = 0; i < calcod; i++) {
          calcods[1 + i * 5] /= jscale;
          calcods[2 + i * 5] /= jscale;
          calcods[5 + i * 5] = scale;
        }

      }

    }

    return calcods;
  }
 
Example 16
Source File: TestGribNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testConvertUnsigned() {
  int val = (int) DataType.unsignedByteToShort((byte) -200);
  int val2 = DataType.unsignedShortToInt((short) -200);
  assertNotEquals(val, val2);
}
 
Example 17
Source File: GribNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Convert unsigned bytes into an integer.
 *
 * @param raf read one byte from here
 * @return integer value
 * @throws IOException on read error
 */
public static int uint(RandomAccessFile raf) throws IOException {
  int a = raf.read();
  return (int) DataType.unsignedByteToShort((byte) a);
}