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

The following examples show how to use ucar.unidata.io.RandomAccessFile#skipBytes() . 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: Grib2SectionGridDefinition.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read Grib Definition section from raf.
 *
 * @param raf RandomAccessFile, with pointer at start of section
 * @throws java.io.IOException on I/O error
 * @throws IllegalArgumentException if not a GRIB-2 record
 */
public Grib2SectionGridDefinition(RandomAccessFile raf) throws IOException {

  startingPosition = raf.getFilePointer();

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

  // octet 5
  int section = raf.read(); // This is section 3
  if (section != 3)
    throw new IllegalArgumentException("Not a GRIB-2 GDS section");

  // octets 13-14
  raf.skipBytes(7);
  templateNumber = GribNumbers.uint2(raf);

  // read in whole GDS as byte[]
  rawData = new byte[length];
  raf.seek(startingPosition);
  raf.readFully(rawData);
}
 
Example 2
Source File: Grib2SectionIndicator.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read Grib2SectionIndicator from raf.
 *
 * @param raf RandomAccessFile, with pointer at start (the "GRIB")
 * @throws java.io.IOException on I/O error
 * @throws IllegalArgumentException if not a GRIB-2 record
 */
public Grib2SectionIndicator(RandomAccessFile raf) throws IOException {
  startPos = raf.getFilePointer();
  byte[] b = new byte[4];
  raf.readFully(b);
  for (int i = 0; i < b.length; i++)
    if (b[i] != MAGIC[i])
      throw new IllegalArgumentException("Not a GRIB record");

  raf.skipBytes(2);
  discipline = raf.read();
  int edition = raf.read();
  if (edition != 2)
    throw new IllegalArgumentException("Not a GRIB-2 record");

  messageLength = GribNumbers.int8(raf);
}
 
Example 3
Source File: Grib2RecordScanner.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean isValidFile(RandomAccessFile raf) {
  try {
    raf.seek(0);
    boolean found = raf.searchForward(matcher, maxScan); // look in first 16K
    if (!found)
      return false;
    raf.skipBytes(7); // will be positioned on byte 0 of indicator section
    int edition = raf.read(); // read at byte 8
    if (edition != 2)
      return false;

    // check ending = 7777
    long len = GribNumbers.int8(raf);
    if (len > raf.length())
      return false;
    raf.skipBytes(len - 20);
    for (int i = 0; i < 4; i++) {
      if (raf.read() != 55)
        return false;
    }
    return true;

  } catch (IOException e) {
    return false;
  }
}
 
Example 4
Source File: Grib2SectionBitMap.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read the bit map array.
 *
 * @param raf read from here
 * @return bit map as array of byte values, or null if none.
 * @throws java.io.IOException on read error
 */
@Nullable
public byte[] getBitmap(RandomAccessFile raf) throws IOException {
  // no bitMap
  if (bitMapIndicator == 255)
    return null;

  // LOOK: bitMapIndicator=254 == previously defined bitmap
  if (bitMapIndicator == 254)
    logger.debug("HEY bitMapIndicator=254 previously defined bitmap");

  if (bitMapIndicator != 0) {
    throw new UnsupportedOperationException(
        "Grib2 Bit map section pre-defined (provided by center) = " + bitMapIndicator);
  }

  raf.seek(startingPosition);
  int length = GribNumbers.int4(raf);
  raf.skipBytes(2);

  byte[] data = new byte[length - 6];
  raf.readFully(data);

  return data;
}
 
Example 5
Source File: Grib2SectionProductDefinition.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read Product Definition section from raf.
 *
 * @param raf RandomAccessFile, with pointer at start of section
 * @throws java.io.IOException on I/O error
 * @throws IllegalArgumentException if not a GRIB-2 record
 */
public Grib2SectionProductDefinition(RandomAccessFile raf) throws IOException {

  long startingPosition = raf.getFilePointer();

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

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

  // octets 8-9
  raf.skipBytes(2);
  templateNumber = GribNumbers.int2(raf);

  // read in whole GDS as byte[]
  rawData = new byte[length];
  raf.seek(startingPosition);
  raf.readFully(rawData);
}
 
Example 6
Source File: Grib1SectionGridDefinition.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read Grib Definition section from raf.
 *
 * @param raf RandomAccessFile, with pointer at start of section
 * @throws java.io.IOException on I/O error
 * @throws IllegalArgumentException if not a GRIB-2 record
 */
public Grib1SectionGridDefinition(RandomAccessFile raf) throws IOException {

  startingPosition = raf.getFilePointer();

  // octets 1-3 (Length of GDS)
  int length = GribNumbers.int3(raf);

  // octet 6
  raf.skipBytes(2);
  gridTemplate = GribNumbers.uint(raf);

  // read in whole GDS as byte[]
  rawData = new byte[length];
  raf.seek(startingPosition);
  raf.readFully(rawData);

  predefinedGridDefinition = -1;
  predefinedGridDefinitionCenter = -1;
}
 
Example 7
Source File: NmcObsLegacy.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean endFile(RandomAccessFile raf) throws IOException {
  if (showSkip)
    System.out.println(" endFile start at " + raf.getFilePointer());

  String endRecord = raf.readString(10); // new String(raf.readBytes(10));
  while (endRecord.equals("ENDOF FILE")) {
    endRecord = raf.readString(10); // new String(raf.readBytes(10));
  }

  try {
    while (raf.read() != (int) 'X'); // find where X's start
    while (raf.read() == (int) 'X'); // skip X's till you run out
    raf.skipBytes(-1); // go back one
    readHeader(raf);
    return true;

  } catch (EOFException e) {
    return false;
  }
}
 
Example 8
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 9
Source File: Grib1RecordScanner.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static int getFixedTotalLengthEcmwfLargeGrib(RandomAccessFile raf, int len) throws IOException {
  int lenActual = len;
  if ((len & 0x800000) == 0x800000) {
    long pos0 = raf.getFilePointer(); // remember the actual pos
    int lenS1 = GribNumbers.uint3(raf); // section1Length
    raf.skipBytes(1); // table2Version
    if (GribNumbers.uint(raf) == 98) { // center (if ECMWF make the black magic)
      raf.skipBytes(2); // generatingProcessIdentifier, gridDefinition
      int s1f = GribNumbers.uint(raf); // section1Flags
      raf.skipBytes(lenS1 - (3 + 5)); // skips to next section
      int lenS2;
      int lenS3;
      if ((s1f & 128) == 128) { // section2 GDS exists
        lenS2 = GribNumbers.uint3(raf); // section2Length
        raf.skipBytes(lenS2 - 3); // skips to next section
      }
      if ((s1f & 64) == 64) { // section3 BMS exists
        lenS3 = GribNumbers.uint3(raf); // section3Length
        raf.skipBytes(lenS3 - 3); // skips to next section
      }
      int lenS4 = GribNumbers.uint3(raf); // section4Length
      if (lenS4 < 120) { // here we are!!!!
        lenActual = (len & 0x7FFFFF) * 120 - lenS4 + 4; // the actual totalLength
      }
    }
    raf.seek(pos0); // recall the pos
  }
  return lenActual;
}
 
Example 10
Source File: GribCdmIndex.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean openIndex(RandomAccessFile indexRaf, Logger logger) {
  try {
    indexRaf.order(RandomAccessFile.BIG_ENDIAN);
    indexRaf.seek(0);

    //// header message
    byte[] magic = new byte[Grib2CollectionWriter.MAGIC_START.getBytes(StandardCharsets.UTF_8).length]; // they are
                                                                                                        // all the
    // same
    indexRaf.readFully(magic);

    int version = indexRaf.readInt();

    long recordLength = indexRaf.readLong();
    if (recordLength > Integer.MAX_VALUE) {
      logger.error("Grib2Collection {}: invalid recordLength size {}", indexRaf.getLocation(), recordLength);
      return false;
    }
    indexRaf.skipBytes(recordLength);

    int size = NcStream.readVInt(indexRaf);
    if ((size < 0) || (size > 100 * 1000 * 1000)) {
      logger.warn("GribCdmIndex {}: invalid index size {}", indexRaf.getLocation(), size);
      return false;
    }

    byte[] m = new byte[size];
    indexRaf.readFully(m);
    gribCollectionIndex = GribCollectionProto.GribCollection.parseFrom(m);
    return true;

  } catch (Throwable t) {
    logger.error("Error reading index " + indexRaf.getLocation(), t);
    return false;
  }
}
 
Example 11
Source File: NmcObsLegacy.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
boolean readId(RandomAccessFile raf) throws IOException {

      filePos = raf.getFilePointer();
      byte[] reportId = raf.readBytes(40);
      String latS = new String(reportId, 0, 5, StandardCharsets.UTF_8);

      if (latS.equals("END R")) {
        raf.skipBytes(-40);
        endRecord(raf);

        filePos = raf.getFilePointer();
        reportId = raf.readBytes(40);
        latS = new String(reportId, 0, 5, StandardCharsets.UTF_8);
      }
      if (latS.equals("ENDOF")) {
        raf.skipBytes(-40);
        if (!endFile(raf))
          return false;

        filePos = raf.getFilePointer();
        reportId = raf.readBytes(40);
        latS = new String(reportId, 0, 5, StandardCharsets.UTF_8);
      }

      try {
        lat = (float) (.01 * Float.parseFloat(latS));
        lon = (float) (360.0 - .01 * Float.parseFloat(new String(reportId, 5, 5, StandardCharsets.UTF_8)));

        stationId = new String(reportId, 10, 6, StandardCharsets.UTF_8);
        obsTime = Short.parseShort(new String(reportId, 16, 4, StandardCharsets.UTF_8));
        System.arraycopy(reportId, 20, reserved, 0, 7);
        reportType = Short.parseShort(new String(reportId, 27, 3, StandardCharsets.UTF_8));
        elevMeters = Float.parseFloat(new String(reportId, 30, 5, StandardCharsets.UTF_8));
        instType = Short.parseShort(new String(reportId, 35, 2, StandardCharsets.UTF_8));
        reportLen = 10 * Integer.parseInt(new String(reportId, 37, 3, StandardCharsets.UTF_8));

        cal.setTime(refDate);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (obsTime / 100 > hour + 4) // if greater than 4 hours from reference time
          cal.add(Calendar.DAY_OF_MONTH, -1); // subtract a day LOOK
        cal.set(Calendar.HOUR_OF_DAY, obsTime / 100);
        cal.set(Calendar.MINUTE, 6 * (obsTime % 100));
        date = cal.getTime();

        if (showObs)
          System.out.println(this);
        else if (showTime)
          System.out.print("  time=" + obsTime + " date= " + dateFormatter.toDateTimeString(date));

        // nobs++;
        raf.skipBytes(reportLen - 40);
        return reportLen < 30000;

      } catch (IOException e) {
        throw new IOException("BAD reportId=" + new String(reportId, StandardCharsets.UTF_8) + " starts at " + filePos);
      }
    }
 
Example 12
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void readCC20Header(RandomAccessFile din) throws IOException {

    message_offset = 0;
    din.seek(message_offset);
    // site info 170
    din.skipBytes(62);

    // Message Header
    String stationId = din.readString(40);
    String stationNbr = din.readString(10);

    din.skipBytes(20);

    String clon = din.readString(16);
    String clat = din.readString(16);
    // latlon
    int lon = din.readInt();
    int lat = din.readInt();
    int hhh = din.readInt();

    din.skipBytes(40);

    // ObservationInfo
    short scanMode = convertunsignedByte2Short(din.readByte());
    if (scanMode == 10) {
      sweepN = 1;
    } else if (scanMode >= 100) {
      sweepN = scanMode - 100;
    } else {
      throw new IOException("Error reading CINRAD CC data: Unsupported product: RHI/FFT");
    }

    short syear = (short) din.readUnsignedShort();
    short smm = convertunsignedByte2Short(din.readByte());
    short sdd = convertunsignedByte2Short(din.readByte());
    short shh = convertunsignedByte2Short(din.readByte());
    short smi = convertunsignedByte2Short(din.readByte());
    short sss = convertunsignedByte2Short(din.readByte());

    dateTime0 = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

    din.skipBytes(14); // 14+ (35 - 21)

    // remain2[660]
    elev = new int[sweepN];
    recordNum = new int[sweepN];
    for (int i = 0; i < sweepN; i++) {
      din.skipBytes(14);
      int zbinWidth = din.readUnsignedShort();
      int vbinWidth = din.readUnsignedShort();
      int sbinWidth = din.readUnsignedShort();
      int zbinNum = din.readUnsignedShort();
      int vbinNum = din.readUnsignedShort();
      int sbinNum = din.readUnsignedShort();
      recordNum[i] = din.readUnsignedShort();
      // if(i > 0)
      // recordNum[i] = recordNum[i] + recordNum[i-1];
      elev[i] = din.readShort();
      cDataForm = din.readByte();
      if (cDataForm != 22 && cDataForm != 23 && cDataForm != 24)
        throw new IOException("Unsupported CC data format");
      int dataP = din.readInt();
      // din.skipBytes(2);
      // System.out.println("zbin num: " + zbinNum + " vbin num: " + vbinNum + " sbin num: " + sbinNum + " dataForm " +
      // cDataForm);
    }

    for (int i = sweepN; i < 32; i++) {
      din.skipBytes(35);
    }

    din.skipBytes(6);

    syear = (short) din.readUnsignedShort();
    smm = convertunsignedByte2Short(din.readByte());
    sdd = convertunsignedByte2Short(din.readByte());
    shh = convertunsignedByte2Short(din.readByte());
    smi = convertunsignedByte2Short(din.readByte());
    sss = convertunsignedByte2Short(din.readByte());

    dateTimeE = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

  }
 
Example 13
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void readCCHeader(RandomAccessFile din) throws IOException {

    message_offset = 0;
    din.seek(message_offset);
    // site info 170
    din.skipBytes(66);

    // Message Header
    String stationId = din.readString(40);
    String stationNbr = din.readString(10);

    din.skipBytes(20);

    String clon = din.readString(16);
    String clat = din.readString(16);
    // latlon
    int lon = din.readInt();
    int lat = din.readInt();
    int hhh = din.readInt();

    din.skipBytes(4);

    // ObservationInfo

    short syear1 = convertunsignedByte2Short(din.readByte());
    short syear2 = convertunsignedByte2Short(din.readByte());
    short syear = (short) (syear1 * 100 + syear2);
    short smm = convertunsignedByte2Short(din.readByte());
    short sdd = convertunsignedByte2Short(din.readByte());
    short shh = convertunsignedByte2Short(din.readByte());
    short smi = convertunsignedByte2Short(din.readByte());
    short sss = convertunsignedByte2Short(din.readByte());

    dateTime0 = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

    din.skipBytes(1);

    syear1 = convertunsignedByte2Short(din.readByte());
    syear2 = convertunsignedByte2Short(din.readByte());
    syear = (short) (syear1 * 100 + syear2);
    smm = convertunsignedByte2Short(din.readByte());
    sdd = convertunsignedByte2Short(din.readByte());
    shh = convertunsignedByte2Short(din.readByte());
    smi = convertunsignedByte2Short(din.readByte());
    sss = convertunsignedByte2Short(din.readByte());

    dateTimeE = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);
    short scanMode = convertunsignedByte2Short(din.readByte());
    if (scanMode == 10) {
      sweepN = 1;
    } else if (scanMode >= 100) {
      sweepN = scanMode - 100;
    } else {
      throw new IOException("Error reading CINRAD CC data: Unsupported product: RHI/FFT");
    }

    elev = new int[sweepN];
    din.skipBytes(4);
    short sRHIA = (short) din.readUnsignedShort();

    din.skipBytes(4);

    echoType = din.readUnsignedShort();
    if (echoType != 0x408a) // only support vppi at this moment
      throw new IOException("Error reading CINRAD CC data: Unsupported level 2 data");

    int prodCode = din.readUnsignedShort();

    if (prodCode != 0x8003) // only support vppi at this moment
      throw new IOException("Error reading CINRAD CC data: Unsupported product: RHI/FFT");
    din.skipBytes(4);

    // remain2[660]
    for (int i = 0; i < sweepN; i++) {
      int maxV = din.readUnsignedShort();
      int maxL = din.readUnsignedShort();
      int binWidth = din.readUnsignedShort();
      int binNum = din.readUnsignedShort();
      int recordTotalNum = din.readUnsignedShort();
      din.skipBytes(8);
      elev[i] = din.readUnsignedShort();
      din.skipBytes(2);
      // System.out.println("bin num: " + binNum + " maxL " + maxL + " totalRNumber " + recordTotalNum);
    }
    // din.seek(1020);
    // int doffset = din.readInt();
    // System.out.println(" Offset: " + doffset);
  }
 
Example 14
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void readSCHeader(RandomAccessFile din) throws IOException {

    message_offset = 0;
    din.seek(message_offset);
    // site info 170
    din.skipBytes(90);
    byte[] b10 = new byte[10];
    // Message Header
    din.read(b10);
    String stationNId = new String(b10);

    din.skipBytes(52);

    // latlon
    int lon = din.readInt();
    int lat = din.readInt();
    int hhh = din.readInt();

    din.skipBytes(6);
    //
    message_type = 1;

    // PerformanceInfo
    din.skipBytes(31);

    // ObservationInfo
    vcp = convertunsignedByte2Short(din.readByte());
    short syear = (short) din.readUnsignedShort();
    short smm = convertunsignedByte2Short(din.readByte());
    short sdd = convertunsignedByte2Short(din.readByte());
    short shh = convertunsignedByte2Short(din.readByte());
    short smi = convertunsignedByte2Short(din.readByte());
    short sss = convertunsignedByte2Short(din.readByte());

    dateTime0 = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

    din.skipBytes(8);
    long offset = din.getFilePointer();
    sweepInfo = new SweepInfo[30];
    for (int i = 0; i < 30; i++) {
      sweepInfo[i] = new SweepInfo(din, (int) offset);
      offset = offset + 21;
    }

    din.skipBytes(6);

    syear = (short) din.readUnsignedShort();
    smm = convertunsignedByte2Short(din.readByte());
    sdd = convertunsignedByte2Short(din.readByte());
    shh = convertunsignedByte2Short(din.readByte());
    smi = convertunsignedByte2Short(din.readByte());
    sss = convertunsignedByte2Short(din.readByte());

    dateTimeE = CalendarDate.of(null, syear, smm, sdd, shh, smi, sss);

  }
 
Example 15
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 16
Source File: Level2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private float getDataBlockValue1(RandomAccessFile raf, short offset, int skip) throws IOException {
  long off = offset + message_offset + MESSAGE_HEADER_SIZE;
  raf.seek(off);
  raf.skipBytes(skip);
  return raf.readFloat();
}
 
Example 17
Source File: Level2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private String getDataBlockStringValue(RandomAccessFile raf, short offset, int skip, int size) throws IOException {
  long off = offset + message_offset + MESSAGE_HEADER_SIZE;
  raf.seek(off);
  raf.skipBytes(skip);
  return raf.readString(size);
}
 
Example 18
Source File: Grib1RecordScanner.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static boolean isValidFile(RandomAccessFile raf) {
  try {
    raf.seek(0);
    boolean found = raf.searchForward(matcher, maxScan); // look in first 16K
    if (!found) {
      return false;
    }
    raf.skipBytes(4); // will be positioned on byte 0 of indicator section
    int len = GribNumbers.uint3(raf);
    int edition = raf.read(); // read at byte 8
    if (edition != 1) {
      return false;
    }

    /*
     * Due to a trick done by ECMWF's GRIBEX to support large GRIBs, we need a special treatment
     * to fix the length of the GRIB message. See:
     * https://software.ecmwf.int/wiki/display/EMOS/Changes+in+cycle+000281
     * https://github.com/Unidata/thredds/issues/445
     */
    len = getFixedTotalLengthEcmwfLargeGrib(raf, len);

    // check ending = 7777
    if (len > raf.length()) {
      return false;
    }
    if (allowBadIsLength) {
      return true;
    }

    raf.skipBytes(len - 12);
    for (int i = 0; i < 4; i++) {
      if (raf.read() != 55) {
        return false;
      }
    }
    return true;

  } catch (IOException e) {
    return false;
  }
}
 
Example 19
Source File: Level2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private short getDataBlockValue(RandomAccessFile raf, short offset, int skip) throws IOException {
  long off = offset + message_offset + MESSAGE_HEADER_SIZE;
  raf.seek(off);
  raf.skipBytes(skip);
  return raf.readShort();
}
 
Example 20
Source File: Grib1SectionProductDefinition.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Read Product Definition section from raf.
 *
 * @param raf RandomAccessFile, with pointer at start of section
 * @throws java.io.IOException on I/O error
 * @throws IllegalArgumentException if not a GRIB-2 record
 */
public Grib1SectionProductDefinition(RandomAccessFile raf) throws IOException {
  int length = GribNumbers.uint3(raf);
  rawData = new byte[length];
  raf.skipBytes(-3);
  raf.readFully(rawData);
}