GRIB API学习笔记06——grib_filter

目录

1.1.  介绍

高级命令行工具
迭代输入的所有消息,对每个消息应用一组用户定义的规则。
规则由GRIB API提供的微型语言构成,这种微语言不具有完整编程语言的能力。
 
通过key访问消息中的数据
打印消息的内容
设置消息中的数据值
使用控制结构
将消息写入硬盘
 

1.2.  用法

[shell]grib_filter _rulesfile [-o _outfile] _in_file1 in_file2…[/shell]
 
每个输入文件都按照rules_file中的规则进行处理。
只有在使用write语句时才将grib消息写入输出文件。
rules_file中的每个语句以分号“;”结尾。
rules_file中的语法错误按行号报错。例

$ grib_filter rules.filter uv500.grib2 | more
GRIB_API ERROR   :  grib_parser: syntax error at line 2 of rules.filter
rules.filter: error unable to create action

1.3.  规则语法

1.3.1.   print

print “输出的文本”; # 注释
print “some text [key]”;
打印到标准输出
输出方括号中的key的值
如果没有在消息中找到[key]的值,则显示”undef”
[key] -> 原始类型
[key:l] -> integer (the “el” is for “long”!)
[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:
以下例子中,我使用uv500.grib2作为示例grib文件。文件内容如下

>grib_ls uv500.grib2
uv500.grib2
edition      centre       date         dataType     gridType     stepRange    typeOfLevel  level        shortName    packingType
2            babj         20131031     fc           regular_ll   1            isobaricInhPa  500          u            grid_jpeg
2            babj         20131031     fc           regular_ll   1            isobaricInhPa  500          v            grid_jpeg
2 of 2 grib messages in uv500.grib2
2 of 2 total grib messages in 1 files
# A simple print
print “ed = [edition], centre is [centre:s] = [centre:l]”;

命令:

>grib_filter rules.filter uv500.grib2
ed=2,centre is babj=38
ed=2,centre is babj=38

例2

# one column 3 decimal digits
print “[distinctLatitudes!1%.3f]”;

命令:

$ grib_filter rules.filter uv500.grib2 | more
64.350
64.200
64.050
63.900

 
例3:

# three columns 5 decimal digits comma separated
print “[latLonValues!3%.5f’,’]”;
$ grib_filter rules.filter uv500.grib2 | more
64.35000,70.00000,11.97201,
64.35000,70.15000,11.96721,
64.35000,70.30000,11.95121,
64.35000,70.45000,11.92721,
...

1.3.2.   write

write;
将当前消息写入命令行中-o参数指定的输出文件( grib_filter –o outfile rules_file grib_file).
如果-o参数没设置,则默认输出到“filter.out” 文件。
write “filename__[key]_”;
将当前消息写入到文件“filename[key]_”中,其中方括号中的key用消息中该key的值代替。
需要注意的副作用:如果两个消息的键值不一样,将被写入不同的文件。
 
例4:

# Creating multiple files
write “[centre]_[dataDate]_[step].grib[edition]”;
$ grib_filter rules.filter uv500.grib2
$ ls
babj_20131031_1.grib2  data  rules.filter  uv500.grib2

1.3.3.   append

append;
将当前消息附加到输出文件(由-o定义),默认文件为filter.out。
append “filename_[key]”;
将当前消息附加到输出文件“filename[key]_”,其中[key]由相应key的值替换.
如果文件不存在,则创建。
如果两个消息key的值不一样,将附加到不同的文件中。
 
例5:
append;
命令:

$ grib_count uv500.grib2
2
$ grib_filter rules.filter -o out.grib2 uv500.grib2
$ grib_count out.grib2
2

1.3.4.   set

set key1 = key2;                            # key1设置为key2的值 ****
set key = {val1,val2,val3,val4};    # 设置数组 ****
set key = “string”;                         # 设置字符串 ****
set key = expression ;                   # 设置key为表达式的值
set key = MISSING ;                      # 设置为missing
表达式运算符:
==         equal to
is           equal to for strings
!=          not equal to
||         or
&&        and
!            Not
Arithmetic operators * / + –
()
 
例6:

set edition = 2;
write “[file][edition]”;
$ grib_filter rules.filter uv500.grib2
$ ls
babj_20131031_1.grib2  data  out.grib2  rules.filter  uv500.grib2  uv500.grib21

例7:

set values = {12.2,14.8,13.7,72.3};
print “values = { [values] }”;
write “[file][edition]”;
$ grib_filter rules.filter uv500.grib2
values = { 12.2 14.7996 13.7 72.2996 }
values = { 12.2 14.7996 13.7 72.2996 }
$ ls
data  rules.filter  uv500.grib2  uv500.grib22
$ $ grib_dump uv500.grib22
***** FILE: uv500.grib22
#==============   MESSAGE 1 ( length=304 )                 ==============
GRIB {
  …
  values(4) =  {
  1.2199999809e+01, 1.4799609184e+01, 1.3699999809e+01, 7.2299609184e+01
  }
  #-READ ONLY- maximum = 72.2996;
  #-READ ONLY- minimum = 12.2;
  #-READ ONLY- average = 28.2498;
  #-READ ONLY- numberOfMissing = 0;
  #-READ ONLY- standardDeviation = 25.4489;
  #-READ ONLY- skewness = -75825.8;
  #-READ ONLY- kurtosis = 3.90899e+06;
  #-READ ONLY- isConstant = 0;
  #-READ ONLY- getNumberOfValues = 4;
}
…

1.3.5.   transient

 
transient key1 = key2;
定义一个新的key1,并将key2的值赋给它
transient key1 = “string”;
transient key1 = expression ;
expression operators :
== equal to
is equal to for strings
!= not equal to
|| or
&& and
! not
Arithmetic operators * / + –
()
 
例8:

transient mystep = step + 24;
print “step = [step] mystep = [mystep]”;
$ grib_filter rules.filter uv500.grib2
step = 1 mystep = 25
step = 1 mystep = 25

1.3.6.   if语句

if ( expression ) { instructions }
if ( expression ) { instructions } else { instructions } ****
expression operators :
n  == equal to
n  is equal to for strings
n  != not equal to
n  || or
n  && and
n  ! not
n  Arithmetic operators * / + –
n  ()
例9:该例子没有实际实验

if (localDefinitionNumber == 1) {
set edition = 2;
write;
}
> grib_filter –o out.grib2 rule.filter x.grib1
> ls
out.grib2

使用如下
print “processing [shortName]”;
if(shortName == “u”){
print “shortName”;
}
会提示:
$ grib_filter rules.filter uv500.grib2
processing u
ERROR: Invalid key type
不知道原因,是否if中无法比较字符串?因为改成数字就正确

1.3.7.   switch语句

Switch statement
if-else语句的替换版本
在需要从多个选项中选择时十分方便
 
语法:

switch (var) {
case val1:
# set of actions
case val2:
# set of actions
…………………………...
default:
# default block of actions
}

例10:

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 shortName="2t";
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

1.4.  练习