简体   繁体   中英

Extract weather forecast data from NetCDF 4.5 Grib2Record

Update : Changed this question to better reflect my current understanding.

I have a NetCDF version 4.5 Grib2Record object. Given a (x,y) grid point and a variable name I want to extract all the forecast data for that variable by forecast-time from the object (if the record contains the forecast for that variable). Because of the default behavior of writing a disk index file I do not want to use the higher level NetCDFFile interface.

I have tried looking at lower level code (Grib2Rectilyser, Grib2Customizer etc.) But the code is too dense and I am looking for help with where to start.

I would appreciate any pointers on how to take a Grib2Record and 1. check whether a particular forecast variable is contained in it, and 2. if it is, then extract the forecast data by forecast-valid-time for a given xy grid point and z-level.

I have worked with grib2 files for wind predictions, this is how I get the records and how to process it to get wind (VU components)

Grib2Input input = new Grib2Input(getRandomAccessFile());
if (!input.scan(false, false)) {
    logger.error("Failed to successfully scan grib file");
    return;
}
Grib2Data data = new Grib2Data(getRandomAccessFile());

List<Grib2Record> records = input.getRecords();

for (Grib2Record record : records) {    
    Grib2IndicatorSection is = record.getIs();
    Grib2IdentificationSection id = record.getId();
    Grib2Pds pdsv = record.getPDS().getPdsVars();
    Grib2GDSVariables gdsv = record.getGDS().getGdsVars();

    long time = id.getRefTime() + (record.getPDS().getForecastTime() * 3600000);

    logger.debug("Record description at " + pdsv.getReferenceDate() + " forecast "
    + new Date(time)    + ": " + ParameterTable.getParameterName(is.getDiscipline(), pdsv.getParameterCategory(), pdsv.getParameterNumber()));

    float[] values = data.getData(record.getGdsOffset(), record.getPdsOffset(), 0);

     if ((is.getDiscipline() == 0) && (pdsv.getParameterCategory() == 2) && (pdsv.getParameterNumber() == 2)) {
        // U-component_of_wind
        int c = 0;
        for (double lat = gdsv.getLa1(); lat >= gdsv.getLa2(); lat = lat - gdsv.getDy()) {
            for (double lon = gdsv.getLo1(); lon <= gdsv.getLo2(); lon = lon + gdsv.getDx()) {
                logger.debug(lat + "\t" + lon + "\t" +
                values[c]);
                c++;
            }
        }
    } else if ((is.getDiscipline() == 0) && (pdsv.getParameterCategory() == 2) && (pdsv.getParameterNumber() == 3)) {
        // V-component_of_wind
        int c = 0;
        for (double lat = gdsv.getLa1(); lat >= gdsv.getLa2(); lat = lat - gdsv.getDy()) {
            for (double lon = gdsv.getLo1(); lon <= gdsv.getLo2(); lon = lon + gdsv.getDx()) {
                logger.debug(lat + "\t" + lon + "\t" +
                values[c]);
                c++;
            }
        }
    }
}

private RandomAccessFile getRandomAccessFile() {
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(path, "r");
        raf.order(RandomAccessFile.BIG_ENDIAN);
    } catch (IOException e) {
        logger.error("Error opening file " + path, e);
    }
    return raf;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM