private Record GetRecordFromGrib1File()

in odps-sdk-impl/odps-udf-example/src/main/java/com/aliyun/odps/udf/example/weather/NetCdfExtractor.java [186:273]


  private Record GetRecordFromGrib1File() {
    try {
      if (this.currentInputFile == null) {
        return null;
      }
      if (this.currentGrib1Reader == null) {
        this.currentRaf = new RandomAccessFile(this.currentInputFile.getPath(), "r");
        this.currentGrib1Reader = new Grib1RecordScanner(this.currentRaf);
        if (!this.currentGrib1Reader.isValidFile(this.currentRaf)) {
          throw new RuntimeException("No a valid grib1 file");
        }
      }

      if (this.currentGrib1Reader.hasNext()) {
        ucar.nc2.grib.grib1.Grib1Record gr1 = this.currentGrib1Reader.next();

        Grib1SectionGridDefinition gd = gr1.getGDSsection();
        Grib1Gds gds = gd.getGDS();
        Grib1Gds.LatLon latLon = (Grib1Gds.LatLon) gds;

        Grib1SectionProductDefinition pds = gr1.getPDSsection();

        Grib1Customizer cust = Grib1Customizer.factory(gr1, new Grib1ParamTables());
        Grib1Parameter parameter = cust.getParameter(pds.getCenter(),
                pds.getSubCenter(),
                pds.getTableVersion(),
                pds.getParameterNumber());

        //pds.showPds(cust, new Formatter(System.out));

        Grib1ParamLevel level = cust.getParamLevel(pds);
        Grib1SectionBinaryData bd = gr1.getDataSection();

        this.currentRaf.seek(bd.getStartingPosition());

        Grib1DataReader dataReader = new Grib1DataReader(pds.getDecimalScale(),
                gds.getScanMode(),
                gds.getNxRaw(),
                gds.getNyRaw(),
                gds.getNpts(),
                bd.getStartingPosition());

        byte[] bitmap = null;
        if (pds.bmsExists()) {
          Grib1SectionBitMap bitmapSec = gr1.getBitMapSection();
          bitmap = bitmapSec.getBitmap(this.currentRaf);
        }

        float[] values = dataReader.getData(this.currentRaf, bitmap);

        StringBuffer sb = new StringBuffer();
        if (values.length > 0) {
          sb.append(values[0]);
          for (int j = 1; j < values.length; j++) {
            sb.append(",");
            sb.append(values[j]);
          }
        }

        Record record = new ArrayRecord(this.outputColumns);
        record.setBigint(0, (long) gds.getNx());
        record.setBigint(1, (long) gds.getNy());
        record.setDouble(2, (double) latLon.la1);
        record.setDouble(3, (double) latLon.la2);
        record.setDouble(4, (double) latLon.lo1);
        record.setDouble(5, (double) latLon.lo2);
        record.setDouble(6, (double) latLon.deltaLat);
        record.setDouble(7, (double) latLon.deltaLon);
        record.setString(8, parameter.getName());
        record.setString(9, reportDate.toString());
        record.setString(10, pds.getReferenceDate().toDate().toString());
        record.setBigint(11, (long) (level.getLevelType()));
        record.setBigint(12, (long) (pds.getNmissing()));
        record.setString(13, sb.toString());
        this.outputRecordCount++;
        return record;
      } else {
        // close and set null to trigger processing of next file (if any)
        this.currentRaf.close();
        this.currentRaf = null;
        this.currentInputFile = null;
        this.currentGrib1Reader = null;
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return null;
  }