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

The following examples show how to use ucar.unidata.io.RandomAccessFile#seek() . 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: Grib2Record.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read data array: use when you want to be independent of the GribRecord
 *
 * @param raf from this RandomAccessFile
 * @param drsPos Grib2SectionDataRepresentation starts here
 * @param bmsPos if non-zero, use the bms that starts here
 * @param gdsNumberPoints gdss.getNumberPoints()
 * @param scanMode gds.scanMode
 * @param nx gds.nx
 * @return data as float[] array
 * @throws IOException on read error
 */
public static float[] readData(RandomAccessFile raf, long drsPos, long bmsPos, int gdsNumberPoints, int scanMode,
    int nx, int ny, int[] nptsInLine) throws IOException {
  raf.seek(drsPos);
  Grib2SectionDataRepresentation drs = new Grib2SectionDataRepresentation(raf);
  Grib2SectionBitMap bms = new Grib2SectionBitMap(raf);
  Grib2SectionData dataSection = new Grib2SectionData(raf);

  if (bmsPos > 0)
    bms = Grib2SectionBitMap.factory(raf, bmsPos);

  Grib2DataReader reader = new Grib2DataReader(drs.getDataTemplate(), gdsNumberPoints, drs.getDataPoints(), scanMode,
      nx, dataSection.getStartingPosition(), dataSection.getMsgLength());

  Grib2Drs gdrs = drs.getDrs(raf);

  float[] data = reader.getData(raf, bms, gdrs);

  if (nptsInLine != null)
    data = QuasiRegular.convertQuasiGrid(data, nptsInLine, nx, ny, GribData.getInterpolationMethod());

  if (getlastRecordRead)
    lastRecordRead = Grib2RecordScanner.findRecordByDrspos(raf, drsPos);
  return data;
}
 
Example 2
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 3
Source File: GribDataReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void show(RandomAccessFile rafData, long dataPos) throws IOException {
  rafData.seek(dataPos);
  Grib1Record gr = new Grib1Record(rafData);
  Formatter f = new Formatter();
  f.format("File=%s%n", rafData.getLocation());
  Grib1SectionProductDefinition pds = gr.getPDSsection();
  Grib1Parameter param =
      cust.getParameter(pds.getCenter(), pds.getSubCenter(), pds.getTableVersion(), pds.getParameterNumber());
  f.format("  Parameter=%s%n", param);
  f.format("  ReferenceDate=%s%n", gr.getReferenceDate());
  Grib1ParamTime ptime = gr.getParamTime(cust);
  f.format("  ForecastTime=%d%n", ptime.getForecastTime());
  if (ptime.isInterval()) {
    int[] tinv = ptime.getInterval();
    f.format("  TimeInterval=(%d,%d)%n", tinv[0], tinv[1]);
  }
  f.format("%n");
  gr.getPDSsection().showPds(cust, f);
  System.out.printf("%nGrib1Record.readData at drsPos %d = %s%n", dataPos, f.toString());
}
 
Example 4
Source File: GradsDataDescriptorFile.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static boolean failFast(RandomAccessFile raf) throws IOException {
  raf.seek(0);
  boolean ok = raf.searchForward(matchDSET, 1000); // look in first 1K
  if (!ok) {
    raf.seek(0);
    ok = raf.searchForward(matchdset, 1000); // look in first 1K
    if (!ok)
      return true;
  }

  long pos = raf.getFilePointer();
  ok = raf.searchForward(matchENDVARS, 20000); // look in next 20K
  if (!ok) {
    raf.seek(pos);
    ok = raf.searchForward(matchendvars, 20000); // look in next 20K
  }
  return !ok;
}
 
Example 5
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 6
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 7
Source File: MessageScanner.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MessageScanner(RandomAccessFile raf, long startPos, boolean useEmbeddedTables) throws IOException {
  startPos = (startPos < 30) ? 0 : startPos - 30; // look for the header
  this.raf = raf;
  lastPos = startPos;
  this.useEmbeddedTables = useEmbeddedTables;
  raf.seek(startPos);
  raf.order(RandomAccessFile.BIG_ENDIAN);
}
 
Example 8
Source File: Grib2RecordScanner.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Grib2RecordScanner(RandomAccessFile raf) throws IOException {
  this.raf = raf;
  raf.seek(0);
  raf.order(RandomAccessFile.BIG_ENDIAN);
  lastPos = 0;

  if (debugRepeat)
    logger.debug(" Grib2RecordScanner {}", raf.getLocation());
}
 
Example 9
Source File: GempakFileReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Initialize the file, read in all the metadata (ala DM_OPEN)
 *
 * @param raf RandomAccessFile to read.
 * @param fullCheck if true, check entire structure
 * @throws IOException problem reading file
 */
public boolean init(RandomAccessFile raf, boolean fullCheck) throws IOException {
  setByteOrder();
  rf = raf;
  fileSize = rf.length();
  raf.seek(0);

  return init(fullCheck);
}
 
Example 10
Source File: UAMIVServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Check if this is a valid file for this IOServiceProvider.
 *
 * @param raf RandomAccessFile
 * @return true if valid.
 */
public boolean isValidFile(RandomAccessFile raf) {
  try {
    raf.order(RandomAccessFile.BIG_ENDIAN);
    raf.seek(0);
    raf.skipBytes(4);
    String test = raf.readString(40);
    return test.equals(EMISSIONS) || test.equals(AVERAGE) || test.equals(AIRQUALITY) || test.equals(INSTANT);
  } catch (IOException ioe) {
    return false;
  }
}
 
Example 11
Source File: Grib1DataReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
int[] getDataRaw(RandomAccessFile raf, byte[] bitmap) throws IOException {
  raf.seek(startPos); // go to the data section
  int msgLength = GribNumbers.uint3(raf);

  // octet 4, 1st half (packing flag)
  int unusedbits = raf.read();
  if ((unusedbits & 192) != 0) {
    logger.error("Grib1BinaryDataSection: (octet 4, 1st half) not grid point data and simple packing for {} len={}",
        raf.getLocation(), msgLength);
    throw new IllegalStateException(
        "Grib1BinaryDataSection: (octet 4, 1st half) not grid point data and simple packing ");
  }

  GribNumbers.int2(raf); // octets 5-6 (binary scale factor)
  GribNumbers.float4(raf); // octets 7-10 (reference point = minimum value)

  // octet 11 (number of bits per value)
  int numbits = raf.read();

  // *** read int values *******************************************************
  BitReader reader = new BitReader(raf, startPos + 11);
  int[] ivals = new int[nPts];
  for (int i = 0; i < nPts; i++) {
    ivals[i] = (int) reader.bits2UInt(numbits);
  }

  return ivals;
}
 
Example 12
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 13
Source File: UspLightning1.java    From tds 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 14
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 15
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 16
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 17
Source File: Grib2SectionBitMap.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Grib2SectionBitMap factory(RandomAccessFile raf, long startingPos) throws IOException {
  raf.seek(startingPos);
  return new Grib2SectionBitMap(raf);
}
 
Example 18
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 19
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 20
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;
}