File: test_index.py

package info (click to toggle)
eccodes 2.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 480,184 kB
  • sloc: ansic: 163,815; makefile: 21,266; sh: 8,507; python: 6,026; f90: 5,762; perl: 2,891; yacc: 818; lex: 356; cpp: 305; fortran: 116; awk: 66
file content (84 lines) | stat: -rwxr-xr-x 1,996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python

from __future__ import print_function
import sys
from .gribapi import *
from array import array
import random
import traceback
import itertools

VERBOSE=1
WRITE=0

class Usage(Exception):
    def __init__(self):
        pass

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = list(map(tuple, args)) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

def test():
    # test new from sample
    #grib_release(grib_new_from_samples("GRIB2"))

    if len(sys.argv) < 2:
        raise Usage

    infile = sys.argv[1]
    index_keys = ["shortName","level","number","step"]

    print ("indexing...")
    iid = grib_index_new_from_file(infile,index_keys)
    print ("end indexing...")

    index_vals = []

    for key in index_keys:
        print ("%sSize=%d" % (
            key,
            grib_index_get_size(iid,key)
        ))

        key_vals = grib_index_get_string(iid,key)
        print (" ".join(key_vals))

        index_vals.append(key_vals)

    for prod in product(*index_vals):
        for i in range(len(index_keys)):
            grib_index_select_string(iid,index_keys[i],str(prod[i]))

        while 1:
            gid = grib_new_from_index(iid)
            if gid is None: break
            print (" ".join(["%s=%s" % (key,grib_get_string(gid,key)) for key in index_keys]))
            grib_release(gid)

    grib_index_release(iid)


def main():
    try:
        test()
    except GribInternalError as err:
        if VERBOSE:
            traceback.print_exc(file=sys.stderr)
        else:
            print >>sys.stderr,err.msg

        return 1
    except Usage:
        print "Usage: %s infile" % sys.argv[0]
        sys.exit(2)

if __name__ == "__main__":
    main()
    #print "------------------------------------"