Java – Extracts weather forecast data from NetCDF 4.5 Grib2Record

Extracts weather forecast data from NetCDF 4.5 Grib2Record… here is a solution to the problem.

Extracts weather forecast data from NetCDF 4.5 Grib2Record

Update: Changed this issue 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 prediction data for that variable from the object by prediction time (if the record contains predictions for that variable). Because of the default behavior of writing to disk index files, I don’t want to use the higher-level NetCDFFile interface.

I’ve tried looking at lower-level code (Grib2Rectilyser, Grib2Customizer, etc.) but the code is too dense and I’m looking for help to know where to start.

I would appreciate any instructions on how to get Grib2Record 1. Check if it contains specific predictors, and 2. If so, extract the forecast data grid points and z-levels based on the prediction valid time for a given x-y.

Solution

I

used grib2 files for wind forecasting, and that’s how I get the record and how to handle it to get wind power (V-U 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;
}

Related Problems and Solutions