File: test_index.py

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (83 lines) | stat: -rwxr-xr-x 1,941 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python

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 = 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 "------------------------------------"