参考自ECMWF网站https://confluence.ecmwf.int/display/OPTR/ecCodes:+GRIB+and+BUFR+data+decoding+and+encoding+software+2019的ppt
引言
eccodes高端命令行工具在输入文件中遍历所有messages对于每个message采用一个用户定义的规则该规则使用ecCodes规定的宏语言的格式注意该宏语言并没有一个全面的(full-blown)编程的能力在grib_filter和bufr_filter中的宏语言的语法是相同的
在一个message中通过关键字keys访问数据打印一个message的内容在一个message中保存值使用控制结构(if,switch)将消息写入磁盘
grib_filter 用法
grib_filter [-o out_file ] rules_file in_file1 in_file2 …
输入文件中每一个场都被处理,在规则文件rules_file中的规则被应用在其中仅当有一个写的指令被应用时,一个GRIB message被写在一个输出文件中在rules_file中每一个指令必须以一个分号“;”结尾rules_file中的语法错误会报告,连同错误的行号永远都要将-o out_file 放在其它选项之前!或者,可以从标准输入中读取规则:
cat rules_file | grib_filter in_file1 in_file2 …
echo ‘print “Hello”;’ | grib_filter in_file1 in_file2 …
规则语法-print声明
print “some text”; # this is a commentprint “some text [key]";
-打印到标准输出 Print to the standard output
-检索方括号中的关键字的值 Retrieve the value of the keys in squared brackets.
-如果在消息中没有找到关键字的值,将会被赋值为"undef" If a key is not found in the message then the value of [key] will be displayed as "undef"
-[key] --> native type
-[key:i ] --> integer
-[key:s ] --> string
-[key:d ] --> double
-[key!c%F'S'] --> arrays: c -->columns F -->format (C style) S -->separator
print (“filename”) “some text [key]";
例子1——使用print (注:分割线上面的是rule.filter的内容,下面是命令及输出)
# A simple print
print "ed = [edition] centre is [centre:s] = [centre:i]";
----------------------------------------------------------------------------------------------
>
grib_filter rule.filter x.grib1
ed =
1 centre is ecmf =
98
例子2——使用有格式的print
# one column
3 decimal digits
print "[distinctLatitudes!1%.3f]";
----------------------------------------------------------------------------------------------
-
90.000
-
88.500
-
87.000
-
85.500
…
例子3——用分隔符输出
# three columns
5 decimal digits comma separated
print "[latLonValues!3%.5f',']";
------------------------------------------------------------------------
>
grib_filter rule.filter x.grib1
90.00000,
0.00000,
1.00000,
90.00000,
1.50000,
1.00000,
90.00000,
3.00000,
1.00000,
…
规则语法——write声明
write;
- 在命令行中用-o选项定义输出文件,将现有消息写到该输出文件中
grib_filter -o outfile rules_file grib_file
如果-o选项没有指定,使用缺省值”filter.out“
write "filename_[key]";
-将当前message写入文件"filename_[key]"中,其中方括号内的key用message中检索到的值代替
-如果两个message对于[key]有不同的值,它们同样写到不同文件中
例子4——write 声明
# Creating multiple files
write "[centre]_[dataDate]_[step].grib[edition]";
-------------------------------------------------------------
>
grib_filter rule.filter x.grib1
>
ls
ecmf_20080213_0.grib1
ecmf_20080213_6.grib1
ecmf_20080213_12.grib1
ecmf_20080213_24.grib1
规则语法——append声明
append;
- 在命令行中用-o选项定义输出文件,将现有消息追加到该输出文件中
grib_filter -o outfile rules_file grib_file
如果-o选项没有指定,使用缺省值”filter.out“
append "filename_[key]";
-将当前message追加写入文件"filename_[key]"中,其中方括号内的key用message中检索到的值代替
-如果文件不存在则创建它
-如果两个message对于[key]有不同的值,它们同样写到不同文件中
例子5——append 声明
append;
---------------------------------------------------------------------
>
grib_count out.grib
>
1
>
> grib_filter o out.grib rule.filter
in.grib
>
>
grib_count out.grib
>
2
规则语法——设置关键字
set key1 = key2 ; # 将key1的值设为key2 set key1 to the value of key2set key = {val1,val2,val3,val4} ; # 设置一个关键字数组 set an array keyset key = "string" ; # 将关键字设成一个字符串 set key to a stringset key = expression ; # 将关键字设置成一个表达式 set key to an expressionset key = MISSING ; # 将关键字的值设置成缺失 set value of key to missing表达式运算符:
== 等于 equal to!= 不等于 not equal tois 等于字符串 equals to for strings|| 或 or&& 且 and! 非 not* / + - 算术运算符 arithmetic operators( )
例子6——设置关键字
set _edition =2; write "[file][edition]";
---------------------------------------------------------------------
> grib_filter rule.filter x.grib
> ls
x.gribx.grib2
例子7——设置一个数组关键字
set values = {
12.2,
14.8,
13.7,
72.3};
print "values = { [values] }";
write "[file].[edition]";-----------------------------------------------------------------------
>
grib_filter rule.filter x.grib
values = {
12.2 14.8 13.7 72.3 }
规则语法——临时关键字(transient keys)
transient key1 = key2; - 定义一个新的关键字key1并将它的值设置为key2 Defines the new key1 and assigns to it the value of key2
transient key1 = "string";transient key1 = expression; 表达式运算符:
== 等于 equal to!= 不等于 not equal tois 等于字符串 equals to for strings|| 或 or&& 且 and! 非 not* / + - 算术运算符 arithmetic operators( )
例子8——临时关键字
transient mystep = step +
24;
print "step = [step] mystep = [mystep]";
-----------------------------------------------------
>
grib_filter rule.filter x.grib
step =
24 mystep =
48
实例(略)
规则语法——if 声明
if ( expression ) { instructions } 没有'else if'-你需要创建一个新的'if'块if ( expression ) { instructions }else { instructions } 表达式运算符:
== 等于 equal to!= 不等于 not equal tois 等于字符串 equals to for strings|| 或 or&& 且 and! 非 not* / + - 算术运算符 arithmetic operators( )
例子9——if声明
if (localDefinitionNumber ==
1) {
set edition =
2;
write;
}
--------------------------------------------------------
>
grib_filter o out.grib2 rule.filter x.grib1
>
ls
out.grib2
规则语法——swich 声明
是'if-else'声明的替代版
当你有代码需要从许多要跟随的路径中选择一个时,更方便
switch (var) {
case val1:
# set of actions
case val2:
# set of actions
default
# default block of actions
}
默认:case是强制的,即使if是空的
例子10——switch声明
print
"processing [paramId] [shortName] [stepType]";
switch (shortName) {
case "tp" :
set stepType accum";
case "sp" :
set typeOfLevel =
"surface";
default:
print "Unexpected parameter";
}
write;
例子11
if (centre is
"lfpw" &&
(indicatorOfParameter ==
6 ||
indicatorOfParameter ==
11 ||
indicatorOfParameter ==
8) )
{
if (step!=
0) {
set typeOfGeneratingProcess=
0;
set typeOfProcessedData=
0;
} else {
# Other steps
set typeOfProcessedData=
1;
…
…
switch (typeOfLevel) {
case "hybrid":
set changeDecimalPrecision=
1;
case "surface":
set changeDecimalPrecision=
2;
case "isobaricInhPa":
if (level >
300) {
print "level > 300);
set level = level*
2 +
15;
}# end if (level >
300)
default:
print "Unknown level type!";
}# end switch (typeOfLevel)
}# end if (step!=
0)
write;
}# end main if
规则语法——assert 声明
assert(condition);如果状态评估是假则filter会丢弃
# This filter should be run on GRIB edition
1 only;
# abort otherwise
assert (edition ==
1) ;
...
>
grib_filter o out.grib2 rule.filter x.grib2
ECCODES ERROR : Assertion failure:
binop (access('edition=2'),
long(
2))
转载于:https://www.cnblogs.com/jiangleads/p/11043969.html