/** * Grib edition number 1, 2 or 0 not a Grib file. * * @return int 0 not a Grib file, 1 Grib1, 2 Grib2 * @throws NotSupportedException * @throws IOException */ public final int getEdition() throws IOException, NotSupportedException { int check = 0; // Not a valid Grib file long length = (raf.length() < 4000L) ? raf.length() : 4000L; if (!seekHeader(raf, length)) { return 0; // not valid Grib file } // Read Section 0 Indicator Section to get Edition number Grib1IndicatorSection is = new Grib1IndicatorSection(raf); // section 0 return is.getGribEdition(); } // end getEdition
/** * scans a Grib file to gather information that could be used to create an index or dump the * metadata contents. * * @param parm * @throws NotSupportedException * @throws IOException if raf does not contain a valid GRIB record */ public final boolean scan(int[] parm) throws NotSupportedException, IOException { long start = System.currentTimeMillis(); // stores the number of times a particular GDS is used HashMap gdsCounter = new HashMap(); Grib1ProductDefinitionSection pds = null; Grib1GridDefinitionSection gds = null; long gdsOffset = 0; DataOutputStream dos = new DataOutputStream(System.out); // System.out.println("file position =" + raf.getFilePointer()); long SOR = raf.getFilePointer(); while (raf.getFilePointer() < raf.length()) { SOR = raf.getFilePointer(); if (seekHeader(raf, raf.length())) { // Read Section 0 Indicator Section Grib1IndicatorSection is = new Grib1IndicatorSection(raf); // System.out.println( "Grib record length=" + is.getGribLength()); // EOR (EndOfRecord) calculated so skipping data sections is faster long EOR = raf.getFilePointer() + is.getGribLength() - is.getLength(); // long SOR = raf.getFilePointer() - is.getLength(); // skip Grib 2 records in a Grib 1 file if (is.getGribEdition() == 2) { // System.out.println( "Error Grib 2 record in Grib1 file" ) ; raf.seek(EOR); continue; } if (parm[0] == -1) { // extract only 1st record raf.seek(SOR); byte[] oneRecord = new byte[(int) is.getGribLength()]; raf.read(oneRecord); dos.write(oneRecord, 0, oneRecord.length); dos.flush(); break; } long dataOffset = 0; try { // catch all exceptions and seek to EOR // Read Section 1 Product Definition Section PDS pds = new Grib1ProductDefinitionSection(raf); if (pds.getLengthErr()) { raf.seek(EOR); continue; } Grib1PDSVariables pdsv = pds.getPdsVars(); if (getParameter && parm[0] == pdsv.getParameterNumber()) { raf.seek(SOR); byte[] oneRecord = new byte[(int) is.getGribLength()]; raf.read(oneRecord); dos.write(oneRecord, 0, oneRecord.length); dos.flush(); } else if (parm[0] <= pdsv.getForecastTime() && parm[1] >= pdsv.getForecastTime()) { raf.seek(SOR); byte[] oneRecord = new byte[(int) is.getGribLength()]; raf.read(oneRecord); dos.write(oneRecord, 0, oneRecord.length); dos.flush(); } raf.seek(EOR); continue; } catch (Exception e) { // .println( "Caught Exception scannning record" ); e.printStackTrace(); raf.seek(EOR); continue; } } // end if seekHeader // System.out.println( "raf.getFilePointer()=" + raf.getFilePointer()); // System.out.println( "raf.length()=" + raf.length() ); } // end while raf.getFilePointer() < raf.length() // System.out.println("GribInput: processed in " + // (System.currentTimeMillis()- start) + " milliseconds"); dos.close(); return true; } // end scan