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

The following examples show how to use ucar.unidata.io.RandomAccessFile#readInt() . 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: 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 2
Source File: V5DStruct.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read a compressed grid from a COMP* file.
 * 
 * @return true = ok, false = error.
 */
boolean read_comp_grid(int time, int vr, float[] ga, float[] gb, byte[] compdata1)
    throws IOException, BadFormException {
  long pos;
  short bias;
  int i, n, nl;
  RandomAccessFile f = FileDesc;

  // move to position in file
  pos = grid_position(time, vr);
  f.seek(pos);

  if (FileFormat == 0x80808083) {
    // read McIDAS grid and file numbers
    int mcfile, mcgrid;
    mcfile = f.readInt();
    mcgrid = f.readInt();
    McFile[time][vr] = (short) mcfile;
    McGrid[time][vr] = (short) mcgrid;
  }

  nl = Nl[vr];

  if (FileFormat == 0x80808080 || FileFormat == 0x80808081) {
    // single ga, gb pair for whole grid
    float a, b;
    a = f.readFloat();
    b = f.readFloat();
    // convert a, b to new v5d ga, gb values
    for (i = 0; i < nl; i++) {
      if (a == 0.0) {
        ga[i] = gb[i] = 0.0f;
      } else {
        gb[i] = (b + 128.0f) / -a;
        ga[i] = 1.0f / a;
      }
    }
    bias = 128;
  } else {
    // read ga, gb arrays
    read_float4_array(f, ga, Nl[vr]);
    read_float4_array(f, gb, Nl[vr]);

    // convert ga, gb values to v5d system
    for (i = 0; i < nl; i++) {
      if (ga[i] == 0.0) {
        ga[i] = gb[i] = 0.0f;
      } else {
        // gb[i] = (gb[i]+125.0) / -ga[i];
        gb[i] = (gb[i] + 128.0f) / -ga[i];
        ga[i] = 1.0f / ga[i];
      }
    }
    bias = 128; // 125 ???
  }

  // read compressed grid data
  n = Nr * Nc * Nl[vr];
  if (f.read(compdata1, 0, n) != n)
    return false;

  // convert data values to v5d system
  n = Nr * Nc * Nl[vr];
  for (i = 0; i < n; i++)
    compdata1[i] += bias;

  return true;
}
 
Example 3
Source File: GempakGridReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Grib2Record makeGribRecord(RandomAccessFile raf, long start) throws IOException {
  Grib2SectionIndicator is = new Grib2SectionIndicator(start, 0, 0); // apparently not in GEMPAK file (!)

  Grib2SectionIdentification ids = null;
  Grib2SectionLocalUse lus = null;
  Grib2SectionGridDefinition gds = null;
  Grib2SectionProductDefinition pds = null;
  Grib2SectionDataRepresentation drs = null;
  Grib2SectionBitMap bms = null;
  Grib2SectionData dataSection = null;

  raf.seek(start);
  raf.order(RandomAccessFile.BIG_ENDIAN);

  int secLength = raf.readInt();
  if (secLength > 0) {
    ids = new Grib2SectionIdentification(raf);
  }

  secLength = raf.readInt();
  if (secLength > 0) {
    lus = new Grib2SectionLocalUse(raf);
  }

  secLength = raf.readInt();
  if (secLength > 0) {
    gds = new Grib2SectionGridDefinition(raf);
  }

  secLength = raf.readInt();
  if (secLength > 0) {
    pds = new Grib2SectionProductDefinition(raf);
  }

  secLength = raf.readInt();
  if (secLength > 0) {
    drs = new Grib2SectionDataRepresentation(raf);
  }

  secLength = raf.readInt();
  if (secLength > 0) {
    bms = new Grib2SectionBitMap(raf);
  }

  secLength = raf.readInt();
  if (secLength > 0) {
    dataSection = new Grib2SectionData(raf);
    if (dataSection.getMsgLength() > secLength) // presumably corrupt
      throw new IllegalStateException("Illegal Grib2SectionData Message Length");
  }

  // LOOK - not dealing with repeated records
  return new Grib2Record(null, is, ids, lus, gds, pds, drs, bms, dataSection, false, Grib2Index.ScanModeMissing);
}
 
Example 4
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 5
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 6
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 7
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;
}