Java GRIB-Decoder:从GRIB2文件中提取数据

时间:2016-07-22 13:23:51

标签: java binaryfiles netcdf grib

我从这里下载了一些grib数据文件:ftp://data-portal.ecmwf.int/20160721000000/ (文件类型为.bin,并希望在我的Java应用程序中从此文件中提取数据(我想稍后将提取的数据加载到数据库中)。我只是尝试使用文件ftp://wmo:essential@data-portal.ecmwf.int/20160721000000/A_HWXE85ECEM210000_C_ECMF_20160721000000_24h_em_ws_850hPa_global_0p5deg_grib2.bin

因此,我创建了一个新的Java项目,并添加了两个库grib-8.0.29.jarnetcdfAll-4.6.6.jar。可以在此处找到grib API的文档:http://www.unidata.ucar.edu/software/decoders/grib/javadoc/。我需要打开下载的文件来获取数据。通过Grib2Dump检索一些元数据似乎有效(见下文)。同样是Grib2Input实例sais,我有一个版本2的有效GRIB文件。

这是我检索一些元数据的工作代码:

public static void main(String[] args) throws IOException, InterruptedException {
        File srcDir = new File("C://test//");
        File[] localFiles = srcDir.listFiles();

        for (File tempFile : localFiles) {
             RandomAccessFile raf = new RandomAccessFile(tempFile.getAbsolutePath(), "r");

             System.out.println("======= Grib2GDSVariables ==========");
             Grib2GDSVariables gdsVariables = new Grib2GDSVariables(raf.readBytes(raf.read())); 
             System.out.println("Gds key : " + gdsVariables.getGdsKey());

             System.out.println("======= Grib2Input ==========");
             Grib2Input input = new Grib2Input(raf);
             System.out.println(Grib2Input.isValidFile(raf));
             System.out.println("scan : " + input.scan(true, true));
             System.out.println("getGDSs.size: " + input.getGDSs().size());
             System.out.println("getProducts.size: " + input.getProducts().size());
             System.out.println("getRecords.size: " + input.getRecords().size());
             System.out.println("edition: " + input.getEdition());

             System.out.println("======= Grib2Dump ==========");
             Grib2Dump dump = new Grib2Dump(); 
             dump.gribDump(new String[] {tempFile.getAbsolutePath()});

             System.out.println("======= Grib2ExtractRawData ==========");
             Grib2ExtractRawData extractRawData = new
             Grib2ExtractRawData(raf); extractRawData.main(new String[] {tempFile.getAbsolutePath()});
        }

        System.out.println("finished");
}

这会产生以下输出:

======= Grib2GDSVariables ==========
Gds key : -1732955898
======= Grib2Input ==========
true
scan : true
getGDSs.size: 0
getProducts.size: 0
getRecords.size: 0
edition: 2
======= Grib2Dump ==========
--------------------------------------------------------------------
                        Header : GRIB2
                    Discipline : 0 Meteorological products
                  GRIB Edition : 2
                   GRIB length : 113296
            Originating Center : 98 European Center for Medium-Range Weather Forecasts (RSMC)
        Originating Sub-Center : 0
Significance of Reference Time : 1 Start of forecast
                Reference Time : 2016-07-21T00:00:00Z
                Product Status : 0 Operational products
                  Product Type : 1 Forecast products
         Number of data points : 259920
                     Grid Name : 0 Latitude_Longitude
                     Grid Shape: 6 Earth spherical with radius of 6,371,229.0 m
Number of points along parallel: 720
Number of points along meridian: 361
                   Basic angle : 0
    Subdivisions of basic angle: -9999
  Latitude of first grid point : 90.0
 Longitude of first grid point : 0.0
  Resolution & Component flags : 48
                         Winds : True
   Latitude of last grid point : -90.0
  Longitude of last grid point : 359.5
         i direction increment : 0.5
         j direction increment : 0.5
                    Grid Units : degrees
                 Scanning mode : 0
            Product Definition : 2 Derived forecast on all ensemble members at a point in time
            Parameter Category : 2 Momentum
                Parameter Name : 1 Wind_speed
               Parameter Units : m s-1
       Generating Process Type : 4 Ensemble Forecast
                  ForecastTime : 24
            First Surface Type : 100 Isobaric surface
           First Surface value : 85000.0
           Second Surface Type : 255 Missing
          Second Surface value : -9.999E-252
======= Grib2ExtractRawData ==========
finished

我现在试了两天但却无法上班!我无法从文件中获取内容数据(lat,lon,value)...

有人可以用Java来举个例子吗?

1 个答案:

答案 0 :(得分:2)

您不应该直接在netCDF-java中使用GRIB类。相反,使用

NetcdfFile.open()

这将为您提供通过CDM的访问权限,为您提供包含变量和属性的简单界面。这里有一个教程:https://www.unidata.ucar.edu/software/thredds/current/netcdf-java/tutorial/NetcdfFile.html