GRIB API学习笔记04——GRIB命令行工具

目录

本节大部分包含在内容在《GRIB API学习笔记01》中,仅补充新增的部分,重复部分请参阅笔记01.

整理自2A3-grib_api_tools.pdf

1.   使用GRIB Tools

1.1.  基本概念
命令行工具,用于交互或批处理grib数据,提供最通用的操作,避免写代码。具有一套相同的参数选项,可以将相同的条件用于不同工具。
推荐首先考虑使用GRIB命令行工具。
以下复制自参见1.2.2.2节
基本语法:

grib_ [options] grib_file grib_file … [output_grib]

命令概述:
提供GRIB API本身消息:
girb_info, grib_keys
查看、比较GRIB消息
grib_dump, grib_ls, grib_get, grib_get_data, grib_compare
统计数目、复制消息
grib_count, grib_copy
修改消息内容
grib_set, grib_filter
直接输入命令就会打印帮助信息。

1.2.  信息工具

1.2.1.   grib_info

提供:
版本
定义文件路径:GRIB_DEFINITION_PATH
示例文件路径:GRIB_SAMPLES_PATH
例:

>grib_info
grib_api Version 1.11.0
Default definition files path is used: /usr/local/share/grib_api/definitions
Definition files path can be changed setting GRIB_DEFINITION_PATH environment variable
Default SAMPLES path is used: /usr/local/share/grib_api/samples
SAMPLES path can be changed setting GRIB_SAMPLES_PATH environment variable

1.2.2.   grib_keys

查看可用的key
指定一个GRIB文件,或者指定一个GRIB类型,如GRIB1,GRIB2,reduced_gg_pl_grib1等。
语法:

grib_keys [options]

选项

-TPrint the keys available in the GRIB type
-LPrint list of available GRIB types
-FPrint the keys available in the GRIB file
-xPrint the extended set of keys
-cPrint only the coded keys
-tPrint the type information
-aDump aliases

 
例:

>grib_keys -T GRIB2
=================== GRIB2
discipline
editionNumber
====> SECTION 1 <====
centre
subCentre
significanceOfReferenceTime
dataDate
dataTime
productionStatusOfProcessedData
typeOfProcessedData
====> SECTION 2 <====
====> SECTION 3 <====
numberOfDataPoints
interpretationOfNumberOfPoints
gridDefinitionTemplateNumber
shapeOfTheEarth
Ni
Nj
iScansNegatively
jScansPositively
jPointsAreConsecutive
alternativeRowScanning
latitudeOfFirstGridPointInDegrees
longitudeOfFirstGridPointInDegrees
latitudeOfLastGridPointInDegrees
longitudeOfLastGridPointInDegrees
iDirectionIncrementInDegrees
jDirectionIncrementInDegrees
gridType
====> SECTION 4 <====
NV
productDefinitionTemplateNumber
parameterCategory
parameterNumber
parameterUnits (read only)
parameterName (read only)
typeOfGeneratingProcess
generatingProcessIdentifier
indicatorOfUnitOfTimeRange
stepUnits
forecastTime
stepRange
typeOfFirstFixedSurface
unitsOfFirstFixedSurface (read only)
nameOfFirstFixedSurface (read only)
scaleFactorOfFirstFixedSurface
scaledValueOfFirstFixedSurface
typeOfSecondFixedSurface
unitsOfSecondFixedSurface (read only)
nameOfSecondFixedSurface (read only)
scaleFactorOfSecondFixedSurface
scaledValueOfSecondFixedSurface
level
shortNameECMF
shortName
nameECMF
name
cfNameECMF
cfName
cfVarNameECMF
cfVarName
====> SECTION 5 <====
numberOfValues
packingType
====> SECTION 6 <====
bitMapIndicator
bitmapPresent
====> SECTION 7 <====
values
maximum (read only)
minimum (read only)
average (read only)
numberOfMissing (read only)
standardDeviation (read only)
skewness (read only)
kurtosis (read only)
isConstant (read only)
getNumberOfValues (read only)
====> SECTION 8 <====

1.3.  查看工具

1.3.1.   grib_count

快速计算GRIB文件中的消息个数
语法:

grib_count grib_file1 [grib_file2 …]

       可以使用通配符?

1.3.2.   grib_dump

显示GRIB文件包含的GRIB消息内容
支持多种显示方式:
l  Octet mode:WMO文档格式,支持十六进制
l  Debug mode:打印所有GRIB文件中可用的key
以上两个模式无法同时使用。
可以打印Key的别名和类型信息。
还可以输出一个C程序示例,生成带数据或不带数据的GRIB文件。该代码可以用作生成类似GRIB文件的模板。

1.3.3.   grib_ls

参见第二章

1.4.  修改

1.5.  获取键值对

1.5.1.   grib_get

参见第二章

1.6.  获取数据

1.6.1.   grib_get_data

参见第二章

1.7.  消息比较

1.7.1.   grib_compare

比较两个文件的GRIB消息。默认按相同顺序,逐位比较,精确比较浮点数。
数据的允许偏差由绝对、相对或打包误差指定。
默认为绝对误差为0
找到差别时:
切换到基于key的模式,找出哪个key不同
返回非0值的错误码
提供只比较特定keys的选项
用法:

grib_compare [options] grib_file grib_file

参数:

-b key,key,…All keys in this list are skipped when comparing files
-c key[:{s|l|d|n}],…Compare these keys only
-HCompare message headers only
-eEdition-independent compare
-w key[:{s|l|d}]{=/!=}value,…Where option
-fDo not fail on error
-rMessages not in the same order
-vVerbose
...

例1:

> grib_compare rmf.gra.2013103112001.grb2 rmf.gra.2013103112002.grb2
-- GRIB #1 -- shortName=u paramId=131 stepRange=1 levelType=pl level=100 packingType=grid_jpeg gridType=regular_ll --
long [totalLength]: [125352] != [130748]
long [forecastTime]: [1] != [2]
double [referenceValue]: [-1.80471925000000000000e+06] != [-1.82889737500000000000e+06]
    absolute diff. = 24178.1, relative diff. = 0.0132201
    tolerance=0.125
long [section7Length]: [125176] != [130572]
double [codedValues]: 165660 out of 165660 different
 max absolute diff. = 8.2010987499999999e+00, relative diff. = 0.326066
    max diff. element 117161: 1.69505675000000017860e+01 2.51516662500000016678e+01
    tolerance=0.0000000000000000e+00
    values max= [41.2194]  [41.6317]         min= [-18.0472] [-18.289]
> echo $?
1

比较失败,返回错误码1。
例2:使用backlist(-b)排除指定键

> grib_compare -b P1 f1.grib1 f2.grib1
> echo $?
0

例3:只比较给定键,使用-c指定

$grib_compare -c centre rmf.gra.2013103112001.grb2 rmf.gra.2013103112002.grb2
$ echo $?
0

例4:比较指定namespace下的key

$ grib_compare -c time:n rmf.gra.2013103112001.grb2 rmf.gra.2013103112002.grb2
-- GRIB #1 -- shortName=u paramId=131 stepRange=1 levelType=pl level=100 packingType=grid_jpeg gridType=regular_ll --
long [startStep]: [1] != [2]
long [endStep]: [1] != [2]
string [stepRange]: [1] != [2]
long [validityTime]: [1300] != [1400]

例5:显示比较结果 –v

$ grib_compare -c centre -v rmf.gra.2013103112001.grb2
rmf.gra.2013103112002.grb2
rmf.gra.2013103112002.grb2
  comparing centre as string
  comparing centre as string
  …
  comparing centre as string
175 of 175 grib messages in rmf.gra.2013103112002.grb2
175 of 175 total grib messages in 1 files

例6:只比较头部 –H,不能与-c混合使用
$ grib_compare -H rmf.gra.2013103112001.grb2 rmf.gra.2013103112002.grb2

$ grib_compare -H rmf.gra.2013103112001.grb2 rmf.gra.2013103112002.grb2
-- GRIB #1 -- shortName=u paramId=131 stepRange=1 levelType=pl level=100 packingType=grid_jpeg gridType=regular_ll --
long [totalLength]: [125352] != [130748]
long [forecastTime]: [1] != [2]
double [referenceValue]: [-1.80471925000000000000e+06] != [-1.82889737500000000000e+06]
    absolute diff. = 24178.1, relative diff. = 0.0132201
    tolerance=0.125

例7:版本独立性 –e
只比较两个版本通用的高层信息

> grib_compare –e sp.grib1 sp.grib2
-- GRIB #1 -- shortName=sp paramId=134 stepRange=0
 levelType=sfc level=0 packingType=grid_simple
 gridType=reduced_gg –
string [param]: [134.128] != [134]

除MARS参数外,上面两个文件包含相同的信息。
例8:区别汇总 –f

$ grib_compare -w count=1 -c time:n -f rmf.gra.2013103112001.grb2
rmf.gra.2013103112002.grb2
-- GRIB #1 -- shortName=u paramId=131 stepRange=1 levelType=pl level=100 packingType=grid_jpeg gridType=regular_ll --
long [startStep]: [1] != [2]
long [endStep]: [1] != [2]
string [stepRange]: [1] != [2]
long [validityTime]: [1300] != [1400]
## ERRORS SUMMARY #######
##
## Different number of messages
## 174 more messages in rmf.gra.2013103112001.grb2 than in rmf.gra.2013103112002.grb2
##
## Summary of different key values
## startStep ( 1 different )
## endStep ( 1 different )
## stepRange ( 1 different )
## validityTime ( 1 different )
##
## 1 different messages out of 1

例9:顺序无关比较 –r
默认按顺序比较两个文件,使用-r可以比较不同顺序的文件,但非常耗时。
例10:比较数据值
默认比较精确值
用以下方式设置不同的允许偏差:

-A absolute_error

Use absolute error as tolerance

-R key=rel_error,…

Use relative error as tolerance for key

-P

Use packing error as tolerance

-T factor

Compare data values using tolerance specified in options –A, -R, -P multiplied by an integer factor

设置允许偏差值:

grib_compare –A 2.0 –c data:n f1.grib1 f2.grib1

 
对于下面的数据:

> grib_compare –c data:n f1.grib1 f2.grib1
-- GRIB #1 -- shortName=2t paramId=167 stepRange=0 levelType=sfc level=0
 packingType=grid_simple gridType=reduced_gg –
 double [packedValues]: 1 out of 7 different
 max absolute diff. = 2.0000000000000000e+00, relative diff. = 0.4
      max diff. element 2: 3.00000000000000000000e+00
 5.00000000000000000000e+00
 tolerance=0.0000000000000000e+00 packingError: [0.0625005] [0.0625005]
values max= [70] [70] min= [1] [1]
values max= [70] [70] min= [1] [1]

使用相对误差

grib_compare –R packedValues=0.4 –c data:n f1.grib1 f2.grib1

对于下面的数据

> grib_compare –c data:n f1.grib1 f3.grib1
-- GRIB #1 -- shortName=2t paramId=167 stepRange=0 levelType=sfc level=0
 packingType=grid_simple gridType=reduced_gg –
double [packedValues]: 1 out of 7 different
 max absolute diff. = 5.0000000000000000e-01, relative diff. = 0.166667
     max diff. element 1: 2.50000000000000000000e+00
3.00000000000000000000e+00
         tolerance=0.0000000000000000e+00 packingError: [0.0625005] [0.5]
 values max= [70] [70] min= [1] [1]
values max= [70] [70] min= [1] [1]

使用打包误差:

grib_compare –P –c data:n f1.grib1 f3.grib1

1.8.  消息拷贝

1.8.1.   grib_copy

参照第二章

1.9.  设置键值对

1.9.1.   grib_set

参见第二章