title: 多要素场消息

weight: 4

GRIB 2 的消息中第2段到第7段都可以重复,见下图。

译者注:NWPC 的 GRIB 2 数据尚未使用该功能,下面示例只是教程中的文字,尚未实际验证。

多要素场消息 - 图1

一个示例文件如下图所示:

多要素场消息 - 图2

示例

考虑 数值预报模式预报的 12 小时和 24小时的 500 hPa 高度场。

  1. Section 0: Indicator Section
  2. Section 1: Identification Section
  3. Section 2: Local Use Section (optional)
  4. Section 3: Grid Definition Section
  5. Section 4: Product Definition Section (hour = 12) | repetition 1
  6. Section 5: Data Representation Section |
  7. Section 6: Bit-Map Section |
  8. Section 7: Data Section |
  9. Section 4: Product Definition Section (hour = 24) | repetition 2
  10. Section 5: Data Representation Section |
  11. Section 6: Bit-Map Section |
  12. Section 7: Data Section |
  13. Section 8: End Section

注意:网格定义段(Grid Definition Section)未重复,对所有的预报时效都有效。

练习

创建一个多要素场的 GRIB 2 文件。

  1. #include <iostream>
  2. #include <eccodes.h>
  3. using namespace std;
  4. int main(int argc, char** argv)
  5. {
  6. if(argc < 2)
  7. {
  8. cout<<"Usage: "<<argv[0]<<" grib_file_path";
  9. return 1;
  10. }
  11. const char* file_path = argv[1];
  12. const char* sample_filename = "regular_ll_pl_grib2";
  13. int err = 0;
  14. codes_handle *h = codes_grib_handle_new_from_samples(nullptr, sample_filename);
  15. if(h == nullptr)
  16. {
  17. cout<<"ERROR: unable to create handle from file "<<file_path<<endl;
  18. return 1;
  19. }
  20. size_t value_size;
  21. codes_get_size(h, "values", &value_size);
  22. auto values = new double[value_size];
  23. double v = 0;
  24. for(auto i=0; i<value_size; i++)
  25. {
  26. values[i] = v;
  27. v++;
  28. }
  29. // codes_set_long(h, "bitsPerValue", 16);
  30. codes_set_double_array(h, "values", values, value_size);
  31. codes_multi_handle* multi_handle = codes_grib_multi_handle_new(nullptr);
  32. const int start_section = 4;
  33. for (int step = 12; step <= 120; step += 12)
  34. {
  35. codes_set_long(h, "step", step);
  36. codes_grib_multi_handle_append(h, start_section, multi_handle);
  37. }
  38. FILE* output_file=fopen(file_path, "wb");
  39. if(!output_file)
  40. {
  41. cerr<<"ERROR: unable to open output file " << file_path <<endl;
  42. exit(1);
  43. }
  44. codes_grib_multi_handle_write(multi_handle, output_file);
  45. fclose(output_file);
  46. delete [] values;
  47. codes_handle_delete(h);
  48. codes_grib_multi_handle_delete(multi_handle);
  49. return 0;
  50. }

使用 grib_ls 查看生成的文件,ecCodes 将多变量消息处理成多个单变量消息。

  1. $ grib_ls multi.grib2
  2. multi.grib2
  3. edition centre date dataType gridType stepRange typeOfLevel level shortName packingType
  4. 2 ecmf 20070323 af regular_ll 12 isobaricInhPa 850 t grid_simple
  5. 2 ecmf 20070323 af regular_ll 24 isobaricInhPa 850 t grid_simple
  6. 2 ecmf 20070323 af regular_ll 36 isobaricInhPa 850 t grid_simple
  7. 2 ecmf 20070323 af regular_ll 48 isobaricInhPa 850 t grid_simple
  8. 2 ecmf 20070323 af regular_ll 60 isobaricInhPa 850 t grid_simple
  9. 2 ecmf 20070323 af regular_ll 72 isobaricInhPa 850 t grid_simple
  10. 2 ecmf 20070323 af regular_ll 84 isobaricInhPa 850 t grid_simple
  11. 2 ecmf 20070323 af regular_ll 96 isobaricInhPa 850 t grid_simple
  12. 2 ecmf 20070323 af regular_ll 108 isobaricInhPa 850 t grid_simple
  13. 2 ecmf 20070323 af regular_ll 120 isobaricInhPa 850 t grid_simple
  14. 10 of 10 messages in multi.grib2
  15. 10 of 10 total messages in 1 files