netcdfgribnetcdf-java

Reading row and column index values from GRIB file via netcdf


Recently I have been working on netcdf files and I am using this library. I am able to open and read the data like this:

NetcdfFile ncfile = NetcdfFile.open(inputPath);

I am able to list variables and get desired variable inside the data:

List<Variable> variables = ncfile.getVariables();
Variable tcc = ncFile.findVariable("tcc_0");

I am able to get rank and shape of variable too, and I can get data of the table by this:

int[] readOrigin = new int[2];
int[] readShape = new int[2]; 

readOrigin[0] = desiredRow;
readOrigin[1] = 0;

readShape[0] = 1;
readShape[1] = numberOfColumns;

Array arr = tcc.read(readOrigin, readShape);

This code gets all the values of 'desiredRow'th row and I can iterate over arr and find each specific value for each column.

However, I want to get all the values for columns and row indexes. When I want to achieve table's [0][0] value, I can achieve that. By I am not able to achieve all the row and column index values. I need to get 32.035, 32.08, ... for the row index values and same for the columns.

Any help is appreciated.

enter image description here


Solution

  • After some research I found a way to get them. They are called root group and has 1 dimensional data inside it which is exactly what I needed.

    Variable rootGroupLatitudeVar = ncFile.getRootGroup().findVariableLocal("latitude");
    Array latitudeArr = rootGroupLatitudeVar.read();
    int latitudeArrSize = rootGroupLatitudeVar.getShape(0);
    
    ..iteration