Java Code Examples for ucar.unidata.io.RandomAccessFile#read()

The following examples show how to use ucar.unidata.io.RandomAccessFile#read() . 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: Grib1SectionBitMap.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Read the bitmap array when needed, return null if none.
 */
@Nullable
public byte[] getBitmap(RandomAccessFile raf) throws IOException {
  if (startingPosition <= 0) {
    throw new IllegalStateException("Grib1 Bit map has bad starting position");
  }

  raf.seek(startingPosition);

  // octet 1-3 (length of section)
  int length = GribNumbers.uint3(raf);

  // octet 4 unused bits
  raf.read(); // unused

  // octets 5-6
  int bm = raf.readShort();
  if (bm != 0) {
    logger.warn("Grib1 Bit map section pre-defined (provided by center) bitmap number = {}", bm);
    return null;
  }

  // not sure if length is set correctly when pre-define bitmap is used, so wait until that to test
  // seeing a -1, bail out
  if (length <= 6 || length > 10e6) { // look max ??
    return null;
  }

  // read the bits as integers
  int n = length - 6;
  byte[] data = new byte[n];
  raf.readFully(data);
  return data;
}
 
Example 2
Source File: BufrNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Read 3 bytes and convert into an unsigned integer. */
public static int uint3(RandomAccessFile raf) throws IOException {
  int a = raf.read();
  int b = raf.read();
  int c = raf.read();

  return uint3(a, b, c);
}
 
Example 3
Source File: BufrNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Convert 2 bytes into an unsigned integer. */
static int uint2(RandomAccessFile raf) throws IOException {
  int a = raf.read();
  int b = raf.read();

  return uint2(a, b);
}
 
Example 4
Source File: GribNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert 4 bytes into a signed integer.
 *
 * @param raf read from here
 * @return integer value
 * @throws IOException on read error
 */
public static int int4(RandomAccessFile raf) throws IOException {
  int a = raf.read();
  int b = raf.read();
  int c = raf.read();
  int d = raf.read();

  return int4(a, b, c, d);
}
 
Example 5
Source File: GribNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert 3 bytes into a signed integer.
 *
 * @param raf read from here
 * @return integer value
 * @throws IOException on read error
 */
public static int int3(RandomAccessFile raf) throws IOException {
  int a = raf.read();
  int b = raf.read();
  int c = raf.read();

  return int3(a, b, c);
}
 
Example 6
Source File: BufrDataDescriptionSection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructs a BufrDataDescriptionSection object by reading section 3 from a BUFR file.
 *
 * @param raf RandomAccessFile, position must be on a BUFR section 3
 * @throws IOException on read error
 */
public BufrDataDescriptionSection(RandomAccessFile raf) throws IOException {
  offset = raf.getFilePointer();
  int length = BufrNumbers.uint3(raf);
  long EOS = offset + length;

  // reserved byte
  raf.read();

  // octets 5-6 number of datasets
  ndatasets = BufrNumbers.uint2(raf);

  // octet 7 data type bit 2 is for compressed data 192 or 64,
  // non-compressed data is 0 or 128
  datatype = raf.read();

  // get descriptors
  int ndesc = (length - 7) / 2;
  for (int i = 0; i < ndesc; i++) {
    int ch1 = raf.read();
    int ch2 = raf.read();
    short fxy = (short) ((ch1 << 8) + (ch2));
    descriptors.add(fxy);
  }

  // reset for any offset discrepancies
  raf.seek(EOS);
}
 
Example 7
Source File: BufrIdentificationSection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public final byte[] getOptiondsalSection(RandomAccessFile raf) throws IOException {
  if (!hasOptionalSection)
    return null;

  byte[] optionalSection = new byte[optionalSectionLen - 4];
  raf.seek(optionalSectionPos);
  int nRead = raf.read(optionalSection);
  if (nRead != optionalSection.length)
    log.warn("Error reading optional section -- expected " + optionalSection.length + " but read " + nRead);
  return optionalSection;
}
 
Example 8
Source File: Grib2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void check(RandomAccessFile raf, Formatter f) throws IOException {
  long messLen = is.getMessageLength();
  long startPos = is.getStartPos();
  long endPos = is.getEndPos();

  if (endPos > raf.length()) {
    f.format("End of GRIB message (start=%d len=%d) end=%d > file.length=%d for %s%n", startPos, messLen, endPos,
        raf.length(), raf.getLocation());
    return;
  }

  raf.seek(endPos - 4);
  for (int i = 0; i < 4; i++) {
    if (raf.read() != 55) {
      String clean = StringUtil2.cleanup(header);
      if (clean.length() > 40)
        clean = clean.substring(0, 40) + "...";
      f.format("Missing End of GRIB message (start=%d len=%d) end=%d header= %s for %s (len=%d)%n", startPos, messLen,
          endPos, clean, raf.getLocation(), raf.length());
      break;
    }
  }

  long dataLen = dataSection.getMsgLength();
  long dataStart = dataSection.getStartingPosition();
  long dataEnd = dataStart + dataLen;

  if (dataEnd > raf.length()) {
    f.format("GRIB data section (start=%d len=%d) end=%d > file.length=%d for %s%n", dataStart, dataLen, dataEnd,
        raf.length(), raf.getLocation());
    return;
  }

  if (dataEnd > endPos) {
    f.format("GRIB data section (start=%d len=%d) end=%d > message end=%d for %s%n", dataStart, dataLen, dataEnd,
        endPos, raf.getLocation());
  }

}
 
Example 9
Source File: Grib2Drs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Type0(RandomAccessFile raf) throws IOException {
  this.referenceValue = raf.readFloat();
  this.binaryScaleFactor = GribNumbers.int2(raf);
  this.decimalScaleFactor = GribNumbers.int2(raf);
  this.numberOfBits = raf.read();
  this.originalType = raf.read();
}
 
Example 10
Source File: NcStreamIosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int readVInt(RandomAccessFile raf) throws IOException {
  byte b = (byte) raf.read();
  int i = b & 0x7F;
  for (int shift = 7; (b & 0x80) != 0; shift += 7) {
    b = (byte) raf.read();
    i |= (b & 0x7F) << shift;
  }
  return i;
}
 
Example 11
Source File: NmcObsLegacy.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void readHeader(RandomAccessFile raf) throws IOException {
  byte[] h = raf.readBytes(60);

  // 12 00 070101
  short hour = Short.parseShort(new String(h, 0, 2, StandardCharsets.UTF_8));
  short minute = Short.parseShort(new String(h, 2, 2, StandardCharsets.UTF_8));
  short year = Short.parseShort(new String(h, 4, 2, StandardCharsets.UTF_8));
  short month = Short.parseShort(new String(h, 6, 2, StandardCharsets.UTF_8));
  short day = Short.parseShort(new String(h, 8, 2, StandardCharsets.UTF_8));

  int fullyear = (year > 30) ? 1900 + year : 2000 + year;

  if (cal == null) {
    cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
  }
  cal.clear();
  cal.set(fullyear, month - 1, day, hour, minute);
  refDate = cal.getTime();

  if (showHeader)
    System.out.println(
        "\nhead=" + new String(h, StandardCharsets.UTF_8) + " date= " + dateFormatter.toDateTimeString(refDate));

  int b, count = 0;
  while ((b = raf.read()) == (int) 'X')
    count++;
  char c = (char) b;
  if (showSkip)
    System.out.println(" b=" + b + " c=" + c + " at " + raf.getFilePointer() + " skipped= " + count);
  raf.skipBytes(-1); // go back one
}
 
Example 12
Source File: Grib2SectionBitMap.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Grib2SectionBitMap(RandomAccessFile raf) throws IOException {
  startingPosition = raf.getFilePointer();

  // octets 1-4 (Length of section)
  int length = GribNumbers.int4(raf);

  // octet 5
  int section = raf.read();
  if (section != 6)
    throw new IllegalArgumentException("Not a GRIB-2 Bitmap section");

  // octet 6
  bitMapIndicator = raf.read();
  raf.seek(startingPosition + length);
}
 
Example 13
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void readData0(RandomAccessFile raf, int datatype, Range gateRange, IndexIterator ii) throws IOException {
  long offset = message_offset;
  offset += MESSAGE_HEADER_SIZE; // offset is from "start of digital radar data message header"
  offset += getDataOffset(datatype);
  raf.seek(offset);
  if (logger.isDebugEnabled()) {
    logger.debug("  read recno " + recno + " at offset " + offset + " count= " + getGateCount(datatype));
    logger.debug(
        "   offset: reflect= " + reflect_offset + " velocity= " + velocity_offset + " spWidth= " + spectWidth_offset);
  }

  int dataCount = getGateCount(datatype);
  byte[] data = new byte[dataCount];
  byte[] b4 = new byte[4];
  int j = 0;
  if (datatype == REFLECTIVITY)
    j = 0;
  else if (datatype == VELOCITY_LOW)
    j = 1;
  else if (datatype == SPECTRUM_WIDTH)
    j = 3;

  // raf.readFully(data);

  for (int gateIdx : gateRange) {
    if (gateIdx >= dataCount)
      ii.setByteNext(MISSING_DATA);
    else {
      raf.read(b4);
      data[gateIdx] = b4[j];
      ii.setByteNext(data[gateIdx]);
    }
  }

}
 
Example 14
Source File: GribNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert 4 bytes into a float value.
 *
 * @param raf read from here
 * @return float value
 * @throws IOException on read error
 */
public static float float4(RandomAccessFile raf) throws IOException {
  int a = raf.read();
  int b = raf.read();
  int c = raf.read();
  int d = raf.read();

  return float4(a, b, c, d);
}
 
Example 15
Source File: UspLightning1.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isValidFile(RandomAccessFile raf) throws IOException {
  raf.seek(0);
  int n = MAGIC.length();
  byte[] b = new byte[n];
  raf.read(b);
  String got = new String(b);
  return got.equals(MAGIC);
}
 
Example 16
Source File: Grib1DataReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void showComplexPackingInfo(Formatter f, RandomAccessFile raf, long startPos) throws IOException {
  GribData.Info info = Grib1SectionBinaryData.getBinaryDataInfo(raf, startPos);

  if (!info.isGridPointData() || info.isSimplePacking())
    return;

  int N1 = GribNumbers.uint2(raf);
  int flagExt = raf.read();
  boolean hasBitmap2 = GribNumbers.testGribBitIsSet(flagExt, 3);
  boolean hasDifferentWidths = GribNumbers.testGribBitIsSet(flagExt, 4);
  boolean useGeneralExtended = GribNumbers.testGribBitIsSet(flagExt, 5);
  boolean useBoustOrdering = GribNumbers.testGribBitIsSet(flagExt, 6);

  int N2 = GribNumbers.uint2(raf);
  int codedNumberOfGroups = GribNumbers.uint2(raf);
  int numberOfSecondOrderPackedValues = GribNumbers.uint2(raf);
  int extraValues = raf.read();
  int NG = codedNumberOfGroups + 65536 * extraValues;
  int widthOfWidths = raf.read();
  int widthOfLengths = raf.read();
  int NL = GribNumbers.uint2(raf);

  f.format("%n");
  f.format("       ----flagExt = %s%n", Long.toBinaryString(flagExt));
  f.format("        hasBitmap2 = %s%n", hasBitmap2);
  f.format("hasDifferentWidths = %s%n", hasDifferentWidths);
  f.format("useGeneralExtended = %s%n", useGeneralExtended);
  f.format("  useBoustOrdering = %s%n", useBoustOrdering);
  f.format("                NL = %d%n", NL);
  f.format("                N1 = %d%n", N1);
  f.format("                N2 = %d%n", N2);
  f.format("    numberOfGroups = %d%n", NG);
  f.format("     widthOfWidths = %d%n", widthOfWidths);
  f.format("    widthOfLengths = %d%n", widthOfLengths);
  f.format("%n");

  int groupWidthsSizeBits = widthOfWidths * NG;
  int groupWidthsSizeBytes = (groupWidthsSizeBits + 7) / 8;
  int skipBytes = NL - groupWidthsSizeBytes - 26;
  raf.skipBytes(skipBytes);

  BitReader reader = new BitReader(raf, raf.getFilePointer());
  int[] groupWidth = new int[NG];
  for (int group = 0; group < NG; group++) {
    groupWidth[group] = (int) reader.bits2UInt(widthOfWidths);
  }
  reader.incrByte(); // assume on byte boundary
  showOffset(f, "GroupLength", raf, startPos, NL - 1);

  // try forcing to NL
  // reader = new BitReader(raf, this.startPos + NL - 1);

  // meta groupLengths unsigned_bits(widthOfLengths,numberOfGroups) : read_only;
  int[] groupLength = new int[NG];
  for (int group = 0; group < NG; group++)
    groupLength[group] = (int) reader.bits2UInt(widthOfLengths);
  showOffset(f, "FirstOrderValues", raf, startPos, N1 - 1);

  // meta countOfGroupLengths sum(groupLengths);
  int countOfGroupLengths = 0;
  for (int group = 0; group < NG; group++)
    countOfGroupLengths += groupLength[group];
  f.format("countOfGroupLengths = %d%n", countOfGroupLengths);

  // try forcing to N1
  // reader = new BitReader(raf, this.startPos + N1 - 1);

  // First-order descriptors width stored at the equivalent place of bit number for ordinary packing
  int foWidth = info.numberOfBits;

  // meta firstOrderValues unsigned_bits(widthOfFirstOrderValues,numberOfGroups) : read_only;
  reader.incrByte(); // assume on byte boundary
  int[] firstOrderValues = new int[NG];
  for (int group = 0; group < NG; group++)
    firstOrderValues[group] = (int) reader.bits2UInt(foWidth);

  showOffset(f, "SecondOrderValues", raf, startPos, N2 - 1);

  int total_nbits = 0;
  for (int group = 0; group < NG; group++) {
    int nbits = groupLength[group] * groupWidth[group];
    total_nbits += nbits;
  }
  int data_bytes = (total_nbits + 7) / 8;
  f.format(" total_nbits=%d, nbytes=%d%n", total_nbits, data_bytes);
  f.format(" expect msgLen=%d, actual=%d%n", N2 - 1 + data_bytes, info.dataLength);
  // int simplepackSizeInBits = nPts * info.numberOfBits;
  // int simplepackSizeInBytes = (simplepackSizeInBits +7) / 8;
  // f.format(" simplepackSizeInBits=%d, simplepackSizeInBytes=%d%n", simplepackSizeInBits, simplepackSizeInBytes);
  logger.debug("{}", f);
}
 
Example 17
Source File: Grib1SectionBinaryData.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static GribData.Info getBinaryDataInfo(RandomAccessFile raf, long start) throws IOException {
  raf.seek(start); // go to the data section

  GribData.Info info = new GribData.Info();

  info.dataLength = GribNumbers.uint3(raf); // // octets 1-3 (section length)

  // octet 4, 1st half (packing flag)
  info.flag = raf.read();

  // Y × 10^D = R + X × 2^E
  // octets 5-6 (E = binary scale factor)
  info.binaryScaleFactor = GribNumbers.int2(raf);

  // octets 7-10 (R = reference point = minimum value)
  info.referenceValue = GribNumbers.float4(raf);

  // octet 11 (number of bits per value)
  info.numberOfBits = raf.read();

  return info;
}
 
Example 18
Source File: Grib2SectionIdentification.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read Grib2SectionIndicator from raf.
 *
 * @param raf RandomAccessFile, with pointer at start of indicator section
 * @throws java.io.IOException on I/O error
 * @throws IllegalArgumentException if not a GRIB-2 record
 */
public Grib2SectionIdentification(RandomAccessFile raf) throws IOException {
  long sectionEnd = raf.getFilePointer();

  // section 1 octet 1-4 (length of section)
  int length = GribNumbers.int4(raf);
  sectionEnd += length;

  int section = raf.read();
  if (section != 1)
    throw new IllegalArgumentException("Not a GRIB-2 Identification section");

  // Center octet 6-7
  center_id = GribNumbers.int2(raf);

  // subCenter octet 8-9
  subcenter_id = GribNumbers.int2(raf);

  // master table octet 10 (code table 1.0)
  master_table_version = raf.read();

  // local table octet 11 (code table 1.1)
  local_table_version = raf.read();

  // significanceOfRT octet 12 (code table 1.1)
  significanceOfRT = raf.read();

  // octets 13-19 (reference time of forecast)
  year = GribNumbers.int2(raf);
  month = raf.read();
  day = raf.read();
  hour = raf.read();
  minute = raf.read();
  second = raf.read();
  // refTime = CalendarDate.of(null, year, month, day, hour, minute, second);

  productionStatus = raf.read();
  processedDataType = raf.read();

  raf.seek(sectionEnd);
}
 
Example 19
Source File: GribNumbers.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Convert 2 bytes into a signed integer.
 *
 * @param raf read from here
 * @return integer value
 * @throws IOException on read error
 */
public static int int2(RandomAccessFile raf) throws IOException {
  int a = raf.read();
  int b = raf.read();

  return int2(a, b);
}
 
Example 20
Source File: BufrIndicatorSection.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Constructs a <tt>BufrIndicatorSection</tt> object from a raf.
 *
 * @param raf RandomAccessFile with IndicatorSection content
 * @throws IOException on read error
 */
public BufrIndicatorSection(RandomAccessFile raf) throws IOException {
  this.startPos = raf.getFilePointer() - 4; // start of BUFR message, including "BUFR"
  bufrLength = BufrNumbers.uint3(raf);
  edition = raf.read();
}