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

The following examples show how to use ucar.unidata.io.RandomAccessFile#readFully() . 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: Ray.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read data from this ray.
 *
 * @param raf read from this file
 * @param abbrev which data type we want
 * @param gateRange handles the possible subset of data to return
 * @param ii put the data here
 */
public void readData(RandomAccessFile raf, String abbrev, Range gateRange, IndexIterator ii) throws IOException {
  long offset = rayOffset;
  offset += (getDataOffset(abbrev) * 2 - 2);
  raf.seek(offset);
  byte[] b2 = new byte[2];
  int dataCount = getGateCount(abbrev);
  byte[] data = new byte[dataCount * 2];
  raf.readFully(data);

  for (int gateIdx : gateRange) {
    if (gateIdx >= dataCount)
      ii.setShortNext(uf_header2.missing);
    else {
      b2[0] = data[gateIdx * 2];
      b2[1] = data[gateIdx * 2 + 1];
      short value = getShort(b2, 0);

      ii.setShortNext(value);
    }
  }

}
 
Example 2
Source File: Grib1SectionIndicator.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
 */
Grib1SectionIndicator(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");

  messageLengthNotFixed = GribNumbers.uint3(raf);
  int edition = raf.read();
  if (edition != 1)
    throw new IllegalArgumentException("Not a GRIB-1 record");
  messageLength = Grib1RecordScanner.getFixedTotalLengthEcmwfLargeGrib(raf, messageLengthNotFixed);
  if (messageLength != messageLengthNotFixed) {
    isMessageLengthFixed = true;
  }
}
 
Example 3
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 4
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 5
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 6
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 7
Source File: H5header.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read a zero terminated String at current position; advance file to a multiple of 8.
 *
 * @param raf from this file
 * @return String (dont include zero terminator)
 * @throws java.io.IOException on io error
 */
private String readString8(RandomAccessFile raf) throws IOException {
  long filePos = raf.getFilePointer();

  int count = 0;
  while (raf.readByte() != 0)
    count++;

  raf.seek(filePos);
  byte[] s = new byte[count];
  raf.readFully(s);

  // skip to 8 byte boundary, note zero byte is skipped
  count++;
  count += padding(count, 8);
  raf.seek(filePos + count);

  return new String(s, StandardCharsets.UTF_8); // all Strings are UTF-8 unicode
}
 
Example 8
Source File: Cinrad2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read data from this record.
 * 
 * @param raf read from this file
 * @param datatype which data type we want
 * @param gateRange handles the possible subset of data to return
 * @param ii put the data here
 */
public void readData(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];
  raf.readFully(data);

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

}
 
Example 9
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 10
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static boolean readAndTest(RandomAccessFile raf, byte[] test) throws IOException {
  byte[] b = new byte[test.length];
  raf.readFully(b);
  for (int i = 0; i < b.length; i++)
    if (b[i] != test[i])
      return false;
  return true;
}
 
Example 11
Source File: NcStreamIosp.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);
  if (!readAndTest(raf, NcStream.MAGIC_START))
    return false; // must start with these 4 bytes
  byte[] b = new byte[4];
  raf.readFully(b);
  return test(b, NcStream.MAGIC_HEADER) || test(b, NcStream.MAGIC_DATA); // immed followed by one of these
}
 
Example 12
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 13
Source File: BufrCdmIndex.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected boolean readIndex(RandomAccessFile raf) {
  this.idxFilename = raf.getLocation();

  try {
    raf.order(RandomAccessFile.BIG_ENDIAN);
    raf.seek(0);

    //// header message
    if (!NcStream.readAndTest(raf, MAGIC_START.getBytes(StandardCharsets.UTF_8))) {
      log.error("BufrCdmIndex {}: invalid index", raf.getLocation());
      return false;
    }

    int indexVersion = raf.readInt();
    boolean versionOk = (indexVersion == version);
    if (!versionOk) {
      log.warn("BufrCdmIndex {}: index found version={}, want version= {}", raf.getLocation(), indexVersion, version);
      return false;
    }

    int size = NcStream.readVInt(raf);
    if ((size < 0) || (size > 100 * 1000 * 1000)) {
      log.warn("BufrCdmIndex {}: invalid or empty index ", raf.getLocation());
      return false;
    }

    byte[] m = new byte[size];
    raf.readFully(m);

    BufrCdmIndexProto.BufrIndex proto = BufrCdmIndexProto.BufrIndex.parseFrom(m);
    bufrFilename = proto.getFilename();
    root = proto.getRoot();
    stations = proto.getStationsList();
    start = proto.getStart();
    end = proto.getEnd();
    nobs = proto.getNobs();

    // showProtoRoot(root);

  } catch (Throwable t) {
    log.error("Error reading index " + raf.getLocation(), t);
    return false;
  }

  return true;
}
 
Example 14
Source File: Grib2SectionData.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public byte[] getBytes(RandomAccessFile raf) throws IOException {
  raf.seek(startingPosition); // go to the data section
  byte[] data = new byte[msgLength];
  raf.readFully(data);
  return data;
}
 
Example 15
Source File: Grib2DataReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private float[] getData40(RandomAccessFile raf, Grib2Drs.Type40 gdrs) throws IOException {
  // 6-xx jpeg2000 data block to decode

  // dataPoints are number of points encoded, it could be less than the
  // totalNPoints in the grid record if bitMap is used, otherwise equal
  // int dataPoints = drs.getDataPoints();

  int nb = gdrs.numberOfBits;
  int D = gdrs.decimalScaleFactor;
  float DD = (float) java.lang.Math.pow((double) 10, (double) D);
  float R = gdrs.referenceValue;
  int E = gdrs.binaryScaleFactor;
  float EE = (float) java.lang.Math.pow(2.0, (double) E);
  float ref_val = R / DD;

  Grib2JpegDecoder g2j = null;
  // try {
  if (nb != 0) { // there's data to decode
    g2j = new Grib2JpegDecoder(nb, false);
    byte[] buf = new byte[dataLength - 5];
    raf.readFully(buf);
    g2j.decode(buf);
    gdrs.hasSignedProblem = g2j.hasSignedProblem();
  }

  float[] result = new float[totalNPoints];

  // no data to decode, set to reference value
  if (nb == 0) {
    for (int i = 0; i < dataNPoints; i++) {
      result[i] = ref_val;
    }
    return result;
  }

  int[] idata = g2j.getGdata();
  if (bitmap == null) { // must be one decoded value in idata for every expected data point
    if (idata.length != dataNPoints) {
      logger.debug("Number of points in the data record {} != {} expected from GDS", idata.length, dataNPoints);
      throw new IllegalStateException("Number of points in the data record {} != expected from GDS");
    }

    for (int i = 0; i < dataNPoints; i++) {
      // Y * 10^D = R + (X1 + X2) * 2^E ; // regulation 92.9.4
      // Y = (R + ( 0 + X2) * EE)/DD ;
      result[i] = (R + idata[i] * EE) / DD;
    }
    return result;

  } else { // use bitmap to skip missing values
    for (int i = 0, j = 0; i < totalNPoints; i++) {
      if (GribNumbers.testBitIsSet(bitmap[i / 8], i % 8)) {
        if (j >= idata.length) {
          logger.warn("HEY jj2000 data count {} < bitmask count {}, i={}, totalNPoints={}", idata.length, j, i,
              totalNPoints);
          break;
        }
        int indata = idata[j];
        result[i] = (R + indata * EE) / DD;
        j++;
      } else {
        result[i] = staticMissingValue;
      }
    }
  }
  return result;

  /*
   * } catch (NullPointerException npe) {
   * 
   * logger.error("Grib2DataReader2.jpeg2000Unpacking: bit rate too small nb =" + nb + " for file" +
   * raf.getLocation());
   * float[] data = new float[dataNPoints];
   * for (int i = 0; i < dataNPoints; i++) {
   * data[i] = staticMissingValue; // LOOK ??
   * }
   * return data;
   * }
   */
}
 
Example 16
Source File: Grib1SectionBinaryData.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public byte[] getBytes(RandomAccessFile raf) throws IOException {
  raf.seek(startingPosition); // go to the data section
  byte[] data = new byte[length];
  raf.readFully(data);
  return data;
}
 
Example 17
Source File: Grib2DataReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private float[] getData41(RandomAccessFile raf, Grib2Drs.Type0 gdrs) throws IOException {
  int nb = gdrs.numberOfBits;
  int D = gdrs.decimalScaleFactor;
  float DD = (float) java.lang.Math.pow((double) 10, (double) D);
  float R = gdrs.referenceValue;
  int E = gdrs.binaryScaleFactor;
  float EE = (float) java.lang.Math.pow(2.0, (double) E);

  // LOOK: can # datapoints differ from bitmap and data ?
  // dataPoints are number of points encoded, it could be less than the
  // totalNPoints in the grid record if bitMap is used, otherwise equal
  float[] data = new float[totalNPoints];

  // no data to decode, set to reference value
  if (nb == 0) {
    Arrays.fill(data, R);
    return data;
  }

  // Y * 10**D = R + (X1 + X2) * 2**E
  // E = binary scale factor
  // D = decimal scale factor
  // R = reference value
  // X1 = 0
  // X2 = scaled encoded value
  // data[ i ] = (R + ( X1 + X2) * EE)/DD ;

  byte[] buf = new byte[dataLength - 5];
  raf.readFully(buf);
  InputStream in = new ByteArrayInputStream(buf);
  BufferedImage image = ImageIO.read(in);

  if (nb != image.getColorModel().getPixelSize()) {
    logger.debug("PNG pixel size {} disagrees with grib number of bits {}", image.getColorModel().getPixelSize(), nb);
  }

  DataBuffer db = image.getRaster().getDataBuffer();
  if (bitmap == null) {
    for (int i = 0; i < dataNPoints; i++) {
      data[i] = (R + db.getElem(i) * EE) / DD;
    }
  } else {
    for (int bitPt = 0, dataPt = 0; bitPt < totalNPoints; bitPt++) {
      if (GribNumbers.testBitIsSet(bitmap[bitPt / 8], bitPt % 8)) {
        data[bitPt] = (R + db.getElem(dataPt++) * EE) / DD;
      } else {
        data[bitPt] = staticMissingValue;
      }
    }
  }

  return data;
}
 
Example 18
Source File: NcStreamIosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private boolean readAndTest(RandomAccessFile raf, byte[] test) throws IOException {
  byte[] b = new byte[test.length];
  raf.readFully(b);
  return test(b, test);
}
 
Example 19
Source File: FysatHeader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** Read the header and populate the ncfile */
boolean readPIB(RandomAccessFile raf) throws IOException {

  this.firstHeader = new AwxFileFirstHeader();

  int pos = 0;
  raf.seek(pos);

  // gini header process
  byte[] buf = new byte[FY_AWX_PIB_LEN];
  int count = raf.read(buf);
  EndianByteBuffer byteBuffer;
  if (count == FY_AWX_PIB_LEN) {
    byteBuffer = new EndianByteBuffer(buf, this.firstHeader.byteOrder);
    this.firstHeader.fillHeader(byteBuffer);

  } else {
    return false;
  }

  if (!((this.firstHeader.fileName.endsWith(".AWX") || this.firstHeader.fileName.endsWith(".awx"))
      && this.firstHeader.firstHeaderLength == FY_AWX_PIB_LEN)) {
    return false;
  }

  // skip the fills of the first record
  // raf.seek(FY_AWX_PIB_LEN + this.firstHeader.fillSectionLength);
  buf = new byte[this.firstHeader.secondHeaderLength];
  raf.readFully(buf);
  byteBuffer = new EndianByteBuffer(buf, this.firstHeader.byteOrder);
  switch (this.firstHeader.typeOfProduct) {
    case AwxFileFirstHeader.AWX_PRODUCT_TYPE_UNDEFINED:
    case AwxFileFirstHeader.AWX_PRODUCT_TYPE_POLARSAT_IMAGE:
    case AwxFileFirstHeader.AWX_PRODUCT_TYPE_GRAPH_ANALIYSIS:
    case AwxFileFirstHeader.AWX_PRODUCT_TYPE_DISCREET:
      throw new UnsupportedDatasetException();

    case AwxFileFirstHeader.AWX_PRODUCT_TYPE_GEOSAT_IMAGE:
      secondHeader = new AwxFileGeoSatelliteSecondHeader();
      secondHeader.fillHeader(byteBuffer);
      break;

    case AwxFileFirstHeader.AWX_PRODUCT_TYPE_GRID:
      secondHeader = new AwxFileGridProductSecondHeader();
      secondHeader.fillHeader(byteBuffer);
      break;
  }

  return true;
}
 
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);
}