1

I am trying to create a small library to read grib data from a flight simulator using Java.

Created a code to read a binary file and convert it into a string.

public class MainClass 
{
    final static String FILE_NAME = "PATH\\global_winds.grib";

    public static void main(String... aArgs) throws IOException
    {

        MainClass binary = new MainClass();
        byte[] bytes = binary.readFile(FILE_NAME);

        String myString = asciiBytesToString(bytes);

        System.out.println(myString);
    }

    byte[] readFile(String aFileName) throws IOException 
    {
        Path path = Paths.get(aFileName);
        return Files.readAllBytes(path);
    }


    public static String asciiBytesToString( byte[] bytes )
    {
          if ( (bytes == null) || (bytes.length == 0 ) )
          {
              return "";
          }

          char[] result = new char[bytes.length];

          for ( int i = 0; i < bytes.length; i++ )
          {
              result[i] = (char)bytes[i];
          }

          return new String( result );
      }
}

The file is read and returns gibberish. Is anyone familiar with the grib format? I thought it is using ASCII...

I do know about third-party libraries like NetCDF. The grib file I am trying to process includes limited data and I really wanted to make a small API for reading this file.

Thank you!

4

1 回答 1

2

GRIB 文件是二进制文件,而不是 ASCII 格式。此外,数据是打包的,您需要做一些数学运算才能获得原始值。

为了回答您的问题,GRIB2 使用

  • 参考值 R(IEEE 32 位浮点格式)
  • 二进制比例因子 E(有符号整数,请参见下面的注释)
  • 十进制比例因子 D(有符号整数,请参阅下面的注释)

打包原始数据。参考值 R 用于消除负数,比例因子用于达到所需的精度。根据规范,使用公式v从打包值中恢复原始值p

v = R + p * 2^E / 10^D

请注意,GRIB2 对有符号整数使用了一种特殊的表示,它与二进制补码不兼容。相反,GRIB2 使用最高有效位表示符号,其余位表示有符号整数的大小。如果设置了最高有效位,则该数字为负数。

如果您想了解更多详细信息,可以在http://www.theweatherserver.com/#grib2找到相关文献和规范的列表。

如果您不想查看详细信息,可以使用https://github.com/philippphb/GRIB2Tools中的 GRIB2Tools Java 库,它会完成所有数学运算并允许使用经度和纬度访问数据。

于 2017-10-18T08:26:28.740 回答