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

The following examples show how to use ucar.unidata.io.RandomAccessFile#readBytes() . 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: Cinrad2IOServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isValidFile(RandomAccessFile raf) {
  try {
    if (isCINRAD(raf)) {
      Cinrad2Record.MISSING_DATA = (byte) 0;
      return true;
    } else {
      Cinrad2Record.MISSING_DATA = (byte) 1;
    }
    raf.order(RandomAccessFile.LITTLE_ENDIAN);
    raf.seek(0);
    raf.skipBytes(14);
    short message_type = raf.readShort();
    if (message_type != 1)
      return false;

    raf.skipBytes(12);
    // data header
    byte[] b4 = raf.readBytes(4);
    int data_msecs = bytesToInt(b4, true);
    byte[] b2 = raf.readBytes(2);
    short data_julian_date = (short) bytesToShort(b2, true);
    if (data_msecs > 86400000)
      return false;
    java.util.Date dd = Cinrad2Record.getDate(data_julian_date, data_msecs);

    Calendar cal = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
    cal.clear();
    cal.setTime(dd);
    int year = cal.get(Calendar.YEAR);
    cal.setTime(new Date());
    int cyear = cal.get(Calendar.YEAR);
    return year >= 1990 && year <= cyear;
  } catch (IOException ioe) {
    return false;
  }

}
 
Example 2
Source File: Cinrad2IOServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isCINRAD(RandomAccessFile raf) {
  int data_msecs = 0;

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

    byte[] b128 = raf.readBytes(136);
    String radarT = new String(b128);

    if (radarT.contains("CINRAD/SC") || radarT.contains("CINRAD/CD")) {
      isSC = true;
      isCC = false;
      isCC20 = false;
      return true;
    } else if (radarT.contains("CINRADC")) {
      isCC = true;
      isSC = false;
      isCC20 = false;
      return true;
    } else if (!radarT.contains("CINRADC") && radarT.contains("CINRAD/CC")) {
      isCC20 = true;
      isSC = false;
      isCC = false;
      return true;
    } else {
      isSC = false;
      isCC = false;
      isCC20 = false;
      return false;
    }
  } catch (IOException ioe) {
    return false;
  }

}
 
Example 3
Source File: NmcObsLegacy.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 (raf.length() < 60)
    return false;
  byte[] h = raf.readBytes(60);

  // 32 - 56 are X's
  for (int i = 32; i < 56; i++)
    if (h[i] != (byte) 'X')
      return false;

  try {
    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));

    if ((hour < 0) || (hour > 24))
      return false;
    if ((minute < 0) || (minute > 60))
      return false;
    if ((year < 0) || (year > 100))
      return false;
    if ((month < 0) || (month > 12))
      return false;
    if ((day < 0) || (day > 31))
      return false;

  } catch (Exception e) {
    return false;
  }

  return true;
}
 
Example 4
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 5
Source File: GRIB2DataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
private int readSectionNumber(RandomAccessFile br) throws IOException {
    byte[] bytes = br.readBytes(4);
    if (new String(bytes).trim().equals("GRIB")) {
        br.seek(br.getFilePointer() - 4);
        return 0;
    } else if (bytes[0] == '7' && bytes[1] == '7' && bytes[2] == '7' && bytes[3] == '7') {
        br.seek(br.getFilePointer() - 4);
        return 8;
    } else {
        int sectionNum = br.readByte();
        br.seek(br.getFilePointer() - 5);
        return sectionNum;
    }
}
 
Example 6
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 7
Source File: NmcObsLegacy.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void show(RandomAccessFile raf) throws IOException {
  raf.seek(filePos);
  byte[] b = raf.readBytes(40);
  System.out.println(new String(b, StandardCharsets.UTF_8));
}