[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDFJava #QDJ-903556]: error when reading grib2 files



Hiya Riva,

Here a sample program using netCDF API to write out all the variables names in 
the file that was downloaded as test case this morning. It also writes out all 
the data for the Temperature variable.
Included in the code, there is an answer to your question about the coordinates



import ucar.nc2.Variable;
import ucar.nc2.NetcdfFile;
import ucar.nc2.dt.grid.GridDataset;
import ucar.nc2.dt.GridDatatype;
import ucar.nc2.dt.GridCoordSystem;
import ucar.nc2.dataset.NetcdfDataset;
import ucar.nc2.dataset.CoordinateSystem;
import ucar.nc2.util.IO;
import ucar.nc2.dods.DODSNetcdfFile;
import ucar.ma2.Array;
import ucar.ma2.IndexIterator;
import ucar.unidata.geoloc.LatLonPoint;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.net.URL;

public class TestGMU {

public static void main(String[] args) {
      
       try {
         NetcdfFile ncf = NetcdfFile.open( 
"C:/data/grib/ruc2.t00z.bgrb13anl.grib2" );
         System.out.println( "Variable names are:");
         List<Variable> vars = ncf.getVariables();
         for( Variable var : vars ) {
           System.out.println( var.getName() );
           // once you get the var for a particular variable name, all 
information is available
           // look at: 
http://www.unidata.ucar.edu/software/netcdf-java/v2.2.20/javadoc/ucar/nc2/class-use/Variable.html
         }

         //"All the data records for the same parameter on the same grid will 
be in a single 3-D 
         //(x,y,t)  or 4-D (x,y,z,t) variable depending on the data. The grid 
information is available   
         // in several coordinate variables".


         // For example, the coordinate variables for Temperature are:
         //             time   level   y          x
         // Temperature(0:0:1, 0:0:1, 0:336:1, 0:450:1)
         // each coordinate variable has a start, end, stride separated by a  : 
so time starts at 0,
         // ends at 0 and has a stride of 1. This information is available in 
the above javadoc. 
         // Also, if you are using ToolsUI, click on a variable name and then
         // ncdump tab and it will open a window show the coordinates and 
permit you to dump the data

         Variable Temperature = ncf.findVariable( "Temperature");
         Array data = Temperature.read( "0:0, 0:0, 0:336,0:450");
         IndexIterator iter = data.getIndexIterator();
         System.out.println( "Temperature = ");
         while(iter.hasNext()) {
                float val = iter.getFloatNext();
                System.out.println( val );
        }
        ncf.close();
        System.out.println( "Success");
       } catch (Exception exc) {
              exc.printStackTrace();
       }
    }
}

Robb...



Ticket Details
===================
Ticket ID: QDJ-903556
Department: Support netCDF Java
Priority: Normal
Status: Open