ECC-11: Python interface with codes_ prefix

This commit is contained in:
Shahram Najm 2015-01-13 18:34:04 +00:00
parent 363150002d
commit 5566658825
19 changed files with 267 additions and 185 deletions

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -20,16 +20,16 @@ def example():
out = open(sys.argv[2],'w') out = open(sys.argv[2],'w')
while 1: while 1:
gid = grib_new_from_file(f) gid = codes_new_from_file(f)
if gid is None: break if gid is None: break
message = grib_get_message(gid) message = codes_get_message(gid)
newgid = grib_new_from_message(message) newgid = codes_new_from_message(message)
grib_write(newgid,out) codes_write(newgid,out)
grib_release(newgid) codes_release(newgid)
grib_release(gid) codes_release(gid)
out.close() out.close()
f.close() f.close()
@ -37,7 +37,7 @@ def example():
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -12,7 +12,7 @@ import traceback
import sys import sys
import random import random
from gribapi import * from eccodes import *
INPUT='../../data/constant_field.grib1' INPUT='../../data/constant_field.grib1'
OUTPUT='out.grib' OUTPUT='out.grib'
@ -22,23 +22,23 @@ def example():
fin = open(INPUT) fin = open(INPUT)
fout = open(OUTPUT,'w') fout = open(OUTPUT,'w')
gid = grib_new_from_file(fin) gid = codes_new_from_file(fin)
nx = grib_get(gid,'Ni') nx = codes_get(gid,'Ni')
ny = grib_get(gid,'Nj') ny = codes_get(gid,'Nj')
for step in range(0,24,6): for step in range(0,24,6):
clone_id = grib_clone(gid) clone_id = codes_clone(gid)
grib_set(clone_id,'step',step) codes_set(clone_id,'step',step)
values = [random.random() for i in range(nx*ny)] values = [random.random() for i in range(nx*ny)]
grib_set_values(clone_id,values) codes_set_values(clone_id,values)
grib_write(clone_id,fout) codes_write(clone_id,fout)
grib_release(clone_id) codes_release(clone_id)
grib_release(gid) codes_release(gid)
fin.close() fin.close()
fout.close() fout.close()
@ -46,7 +46,7 @@ def example():
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/tigge_pf_ecmwf.grib2' INPUT='../../data/tigge_pf_ecmwf.grib2'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -19,8 +19,8 @@ VERBOSE=1 # verbose error reporting
def example(): def example():
f = open(INPUT) f = open(INPUT)
mcount = grib_count_in_file(f) mcount = codes_count_in_file(f)
gid_list = [grib_new_from_file(f) for i in range(mcount)] gid_list = [codes_new_from_file(f) for i in range(mcount)]
f.close() f.close()
@ -41,24 +41,24 @@ def example():
print "processing message number",i+1 print "processing message number",i+1
for key in keys: for key in keys:
print '%s=%g' % (key,grib_get(gid,key)) print '%s=%g' % (key,codes_get(gid,key))
print 'There are %d, average is %g, min is %g, max is %g' % ( print 'There are %d, average is %g, min is %g, max is %g' % (
grib_get_size(gid,'values'), codes_get_size(gid,'values'),
grib_get(gid,'average'), codes_get(gid,'average'),
grib_get(gid,'min'), codes_get(gid,'min'),
grib_get(gid,'max') codes_get(gid,'max')
) )
print '-'*100 print '-'*100
grib_release(gid) codes_release(gid)
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -9,7 +9,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/reduced_latlon_surface.grib1' INPUT='../../data/reduced_latlon_surface.grib1'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -27,30 +27,30 @@ def example():
] ]
while 1: while 1:
gid = grib_new_from_file(f) gid = codes_new_from_file(f)
if gid is None: break if gid is None: break
for key in keys: for key in keys:
if not grib_is_defined(gid,key): raise Exception("Key was not defined") if not codes_is_defined(gid,key): raise Exception("Key was not defined")
print '%s=%s' % (key,grib_get(gid,key)) print '%s=%s' % (key,codes_get(gid,key))
if grib_is_defined(gid,"A_very_silly_girl"): raise Exception("Key was defined") if codes_is_defined(gid,"A_very_silly_girl"): raise Exception("Key was defined")
print 'There are %d values, average is %f, min is %f, max is %f' % ( print 'There are %d values, average is %f, min is %f, max is %f' % (
grib_get_size(gid,'values'), codes_get_size(gid,'values'),
grib_get(gid,'average'), codes_get(gid,'average'),
grib_get(gid,'min'), codes_get(gid,'min'),
grib_get(gid,'max') codes_get(gid,'max')
) )
grib_release(gid) codes_release(gid)
f.close() f.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys,os import sys,os
from gribapi import * from eccodes import *
INPUT='../../data/index.grib' INPUT='../../data/index.grib'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -33,45 +33,45 @@ def example():
iid = None iid = None
if (os.path.exists(index_file)): if (os.path.exists(index_file)):
iid = grib_index_read(index_file) iid = codes_index_read(index_file)
else: else:
iid = grib_index_new_from_file(INPUT,index_keys) iid = codes_index_new_from_file(INPUT,index_keys)
# multiple files can be added to an index: # multiple files can be added to an index:
# grib_index_add_file(iid,"grib file to add") # codes_index_add_file(iid,"grib file to add")
grib_index_write(iid,index_file) codes_index_write(iid,index_file)
index_vals = [] index_vals = []
for key in index_keys: for key in index_keys:
print "%sSize=%d" % ( print "%sSize=%d" % (
key, key,
grib_index_get_size(iid,key) codes_index_get_size(iid,key)
) )
key_vals = grib_index_get(iid,key) key_vals = codes_index_get(iid,key)
print " ".join(key_vals) print " ".join(key_vals)
index_vals.append(key_vals) index_vals.append(key_vals)
for prod in product(*index_vals): for prod in product(*index_vals):
for i in range(len(index_keys)): for i in range(len(index_keys)):
grib_index_select(iid,index_keys[i],prod[i]) codes_index_select(iid,index_keys[i],prod[i])
while 1: while 1:
gid = grib_new_from_index(iid) gid = codes_new_from_index(iid)
if gid is None: break if gid is None: break
print " ".join(["%s=%s" % (key,grib_get(gid,key)) for key in index_keys]) print " ".join(["%s=%s" % (key,codes_get(gid,key)) for key in index_keys])
grib_release(gid) codes_release(gid)
grib_index_release(iid) codes_index_release(iid)
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -19,16 +19,16 @@ def example(INPUT):
f = open(INPUT) f = open(INPUT)
while 1: while 1:
gid = grib_new_from_file(f) gid = codes_new_from_file(f)
if gid is None: break if gid is None: break
iterid = grib_iterator_new(gid,0) iterid = codes_iterator_new(gid,0)
missingValue = grib_get_double(gid,"missingValue") missingValue = codes_get_double(gid,"missingValue")
i=0 i=0
while 1: while 1:
result = grib_iterator_next(iterid) result = codes_iterator_next(iterid)
if not result: break if not result: break
[lat,lon,value] = result [lat,lon,value] = result
@ -42,15 +42,15 @@ def example(INPUT):
i += 1 i += 1
grib_iterator_delete(iterid) codes_iterator_delete(iterid)
grib_release(gid) codes_release(gid)
f.close() f.close()
def main(): def main():
try: try:
example(sys.argv[1]) example(sys.argv[1])
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/reduced_latlon_surface.grib1' INPUT='../../data/reduced_latlon_surface.grib1'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -20,33 +20,33 @@ def example():
f = open(INPUT) f = open(INPUT)
while 1: while 1:
gid = grib_new_from_file(f) gid = codes_new_from_file(f)
if gid is None: break if gid is None: break
iterid = grib_keys_iterator_new(gid,'ls') iterid = codes_keys_iterator_new(gid,'ls')
# Different types of keys can be skipped # Different types of keys can be skipped
# grib_skip_computed(iterid) # codes_skip_computed(iterid)
# grib_skip_coded(iterid) # codes_skip_coded(iterid)
# grib_skip_edition_specific(iterid) # codes_skip_edition_specific(iterid)
# grib_skip_duplicates(iterid) # codes_skip_duplicates(iterid)
# grib_skip_read_only(iterid) # codes_skip_read_only(iterid)
# grib_skip_function(iterid) # codes_skip_function(iterid)
while grib_keys_iterator_next(iterid): while codes_keys_iterator_next(iterid):
keyname = grib_keys_iterator_get_name(iterid) keyname = codes_keys_iterator_get_name(iterid)
keyval = grib_get_string(iterid,keyname) keyval = codes_get_string(iterid,keyname)
print "%s = %s" % (keyname,keyval) print "%s = %s" % (keyname,keyval)
grib_keys_iterator_delete(iterid) codes_keys_iterator_delete(iterid)
grib_release(gid) codes_release(gid)
f.close() f.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/gts.bufr' INPUT='../../data/gts.bufr'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -23,30 +23,30 @@ def example():
bid = bufr_new_from_file(f) bid = bufr_new_from_file(f)
if bid is None: break if bid is None: break
iterid = grib_keys_iterator_new(bid) iterid = codes_keys_iterator_new(bid)
# Different types of keys can be skipped # Different types of keys can be skipped
# grib_skip_computed(iterid) # codes_skip_computed(iterid)
# grib_skip_coded(iterid) # codes_skip_coded(iterid)
# grib_skip_edition_specific(iterid) # codes_skip_edition_specific(iterid)
# grib_skip_duplicates(iterid) # codes_skip_duplicates(iterid)
# grib_skip_read_only(iterid) # codes_skip_read_only(iterid)
# grib_skip_function(iterid) # codes_skip_function(iterid)
while grib_keys_iterator_next(iterid): while codes_keys_iterator_next(iterid):
keyname = grib_keys_iterator_get_name(iterid) keyname = codes_keys_iterator_get_name(iterid)
keyval = grib_get_string(iterid,keyname) keyval = codes_get_string(iterid,keyname)
print "%s = %s" % (keyname,keyval) print "%s = %s" % (keyname,keyval)
grib_keys_iterator_delete(iterid) codes_keys_iterator_delete(iterid)
grib_release(bid) codes_release(bid)
f.close() f.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/gts.bufr' INPUT='../../data/gts.bufr'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -23,27 +23,27 @@ def example():
bid = gts_new_from_file(f) bid = gts_new_from_file(f)
if bid is None: break if bid is None: break
iterid = grib_keys_iterator_new(bid) iterid = codes_keys_iterator_new(bid)
# Different types of keys can be skipped # Different types of keys can be skipped
# grib_skip_computed(iterid) # codes_skip_computed(iterid)
# grib_skip_coded(iterid) # codes_skip_coded(iterid)
# grib_skip_read_only(iterid) # codes_skip_read_only(iterid)
while grib_keys_iterator_next(iterid): while codes_keys_iterator_next(iterid):
keyname = grib_keys_iterator_get_name(iterid) keyname = codes_keys_iterator_get_name(iterid)
keyval = grib_get_string(iterid,keyname) keyval = codes_get_string(iterid,keyname)
print "%s = %s" % (keyname,keyval) print "%s = %s" % (keyname,keyval)
grib_keys_iterator_delete(iterid) codes_keys_iterator_delete(iterid)
grib_release(bid) codes_release(bid)
f.close() f.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/sample.grib2' INPUT='../../data/sample.grib2'
OUTPUT='out.grib' OUTPUT='out.grib'
@ -21,25 +21,25 @@ def example():
fin = open(INPUT) fin = open(INPUT)
fout = open(OUTPUT,'w') fout = open(OUTPUT,'w')
gid = grib_new_from_file(fin) gid = codes_new_from_file(fin)
mgid = grib_multi_new() mgid = codes_multi_new()
for step in range(12,132,12): for step in range(12,132,12):
grib_set(gid,"step",step) codes_set(gid,"step",step)
grib_multi_append(gid,4,mgid) codes_multi_append(gid,4,mgid)
grib_multi_write(mgid,fout) codes_multi_write(mgid,fout)
grib_multi_release(mgid) codes_multi_release(mgid)
grib_release(gid) codes_release(gid)
fin.close() fin.close()
fout.close() fout.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/reduced_gaussian_lsm.grib1' INPUT='../../data/reduced_gaussian_lsm.grib1'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -20,27 +20,27 @@ def example():
points = ((30,-20),(13,234)) points = ((30,-20),(13,234))
f = open(INPUT) f = open(INPUT)
gid = grib_new_from_file(f) gid = codes_new_from_file(f)
for lat,lon in points: for lat,lon in points:
nearest = grib_find_nearest(gid,lat,lon)[0] nearest = codes_find_nearest(gid,lat,lon)[0]
print lat,lon print lat,lon
print nearest.lat,nearest.lon,nearest.value,nearest.distance,nearest.index print nearest.lat,nearest.lon,nearest.value,nearest.distance,nearest.index
four = grib_find_nearest(gid,lat,lon,is_lsm = False,npoints = 4) four = codes_find_nearest(gid,lat,lon,is_lsm = False,npoints = 4)
for i in range(len(four)): for i in range(len(four)):
print "- %d -" % i print "- %d -" % i
print four[i] print four[i]
print "-"*100 print "-"*100
grib_release(gid) codes_release(gid)
f.close() f.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,31 +11,31 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/regular_latlon_surface.grib1' INPUT='../../data/regular_latlon_surface.grib1'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
def example(): def example():
f = open(INPUT) f = open(INPUT)
gid = grib_new_from_file(f) gid = codes_new_from_file(f)
values = grib_get_values(gid) values = codes_get_values(gid)
for i in xrange(len(values)): for i in xrange(len(values)):
print "%d %.10e" % (i+1,values[i]) print "%d %.10e" % (i+1,values[i])
print '%d values found in %s' % (len(values),INPUT) print '%d values found in %s' % (len(values),INPUT)
for key in ('max','min','average'): for key in ('max','min','average'):
print '%s=%.10e' % (key,grib_get(gid,key)) print '%s=%.10e' % (key,codes_get(gid,key))
grib_release(gid) codes_release(gid)
f.close() f.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,14 +11,14 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/tp_ecmwf.grib' INPUT='../../data/tp_ecmwf.grib'
OUTPUT='out.grib' OUTPUT='out.grib'
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
def example(): def example():
sample_id = grib_new_from_samples("regular_ll_sfc_grib1") sample_id = codes_new_from_samples("regular_ll_sfc_grib1")
fin = open(INPUT) fin = open(INPUT)
fout = open(OUTPUT,'w') fout = open(OUTPUT,'w')
@ -34,10 +34,10 @@ def example():
prev_vals = None prev_vals = None
while 1: while 1:
gid = grib_new_from_file(fin) gid = codes_new_from_file(fin)
if gid is None: break if gid is None: break
curr_vals = grib_get_values(gid) curr_vals = codes_get_values(gid)
if prev_vals is None: if prev_vals is None:
result = prev_vals = curr_vals result = prev_vals = curr_vals
@ -54,16 +54,16 @@ def example():
keys['startStep'] += 12 keys['startStep'] += 12
keys['endStep'] += 12 keys['endStep'] += 12
clone_id = grib_clone(sample_id) clone_id = codes_clone(sample_id)
for key in keys: for key in keys:
grib_set(clone_id,key,keys[key]) codes_set(clone_id,key,keys[key])
grib_set_values(clone_id,result * 1000) codes_set_values(clone_id,result * 1000)
grib_write(clone_id,fout) codes_write(clone_id,fout)
grib_release(gid) codes_release(gid)
fin.close() fin.close()
fout.close() fout.close()
@ -71,7 +71,7 @@ def example():
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -9,7 +9,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
from datetime import date from datetime import date
INPUT='../../data/regular_latlon_surface_constant.grib1' INPUT='../../data/regular_latlon_surface_constant.grib1'
@ -19,16 +19,16 @@ VERBOSE=1 # verbose error reporting
def example(): def example():
fin = open(INPUT) fin = open(INPUT)
fout = open(OUTPUT,'w') fout = open(OUTPUT,'w')
gid = grib_new_from_file(fin) gid = codes_new_from_file(fin)
dt = date.today() dt = date.today()
today = "%d%02d%02d" % (dt.year,dt.month,dt.day) today = "%d%02d%02d" % (dt.year,dt.month,dt.day)
grib_set(gid,'dataDate',int(today)) codes_set(gid,'dataDate',int(today))
grib_set(gid,'centre',80) codes_set(gid,'centre',80)
centreIntVal = grib_get(gid,'centre',int) centreIntVal = codes_get(gid,'centre',int)
centreStrVal = grib_get(gid,'centre',str) centreStrVal = codes_get(gid,'centre',str)
dateStrVal = grib_get(gid,'dataDate',str) dateStrVal = codes_get(gid,'dataDate',str)
assert(centreIntVal == 80) assert(centreIntVal == 80)
assert(centreStrVal == 'cnmc') assert(centreStrVal == 'cnmc')
assert(dateStrVal == today) assert(dateStrVal == today)
@ -37,30 +37,30 @@ def example():
print 'get date as a string - date = %s' % dateStrVal print 'get date as a string - date = %s' % dateStrVal
# Now do the same but using set_key_vals, setting keys all at once # Now do the same but using set_key_vals, setting keys all at once
grib_set_key_vals(gid, 'level=1,centre=98') # with a String codes_set_key_vals(gid, 'level=1,centre=98') # with a String
assert(grib_get(gid,'centre',str) == 'ecmf') assert(codes_get(gid,'centre',str) == 'ecmf')
assert(grib_get(gid,'level',int) == 1) assert(codes_get(gid,'level',int) == 1)
grib_set_key_vals(gid, ['level=2', 'centre=kwbc']) # with a Tuple codes_set_key_vals(gid, ['level=2', 'centre=kwbc']) # with a Tuple
assert(grib_get(gid,'centre',int) == 7) assert(codes_get(gid,'centre',int) == 7)
assert(grib_get(gid,'level',int) == 2) assert(codes_get(gid,'level',int) == 2)
grib_set_key_vals(gid, {'level': 3, 'centre': 84}) # with a Dictionary codes_set_key_vals(gid, {'level': 3, 'centre': 84}) # with a Dictionary
assert(grib_get(gid,'centre',str) == 'lfpw') assert(codes_get(gid,'centre',str) == 'lfpw')
assert(grib_get(gid,'level',int) == 3) assert(codes_get(gid,'level',int) == 3)
grib_gts_header(True) codes_gts_header(True)
grib_gts_header(False) codes_gts_header(False)
grib_write(gid,fout) codes_write(gid,fout)
grib_release(gid) codes_release(gid)
fin.close() fin.close()
fout.close() fout.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -8,7 +8,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT = '../../data/regular_latlon_surface.grib1' INPUT = '../../data/regular_latlon_surface.grib1'
OUTPUT = 'out.grib' OUTPUT = 'out.grib'
@ -18,33 +18,33 @@ VERBOSE = 1 # verbose error reporting
def example(): def example():
fin = open(INPUT) fin = open(INPUT)
fout = open(OUTPUT,'w') fout = open(OUTPUT,'w')
gid = grib_new_from_file(fin) gid = codes_new_from_file(fin)
grib_set(gid,'missingValue', MISSING) codes_set(gid,'missingValue', MISSING)
values = grib_get_values(gid) values = codes_get_values(gid)
grib_set(gid, 'bitmapPresent', 1) codes_set(gid, 'bitmapPresent', 1)
# Change some data values to be missing # Change some data values to be missing
num_missing = 0 num_missing = 0
for i in range(100): for i in range(100):
if i % 2 == 0: if i % 2 == 0:
values[i] = MISSING values[i] = MISSING
num_missing += 1 num_missing += 1
grib_set_values(gid, values) codes_set_values(gid, values)
# Check counts of missing and non-missing values # Check counts of missing and non-missing values
num_data = grib_get(gid,'numberOfDataPoints',int) num_data = codes_get(gid,'numberOfDataPoints',int)
assert(grib_get(gid,'numberOfCodedValues',int) == num_data-num_missing) assert(codes_get(gid,'numberOfCodedValues',int) == num_data-num_missing)
assert(grib_get(gid,'numberOfMissing',int) == num_missing) assert(codes_get(gid,'numberOfMissing',int) == num_missing)
grib_write(gid,fout) codes_write(gid,fout)
grib_release(gid) codes_release(gid)
fin.close() fin.close()
fout.close() fout.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
INPUT='../../data/tigge/tigge_ecmf_pl_t.grib' INPUT='../../data/tigge/tigge_ecmf_pl_t.grib'
OUTPUT='out.grib' OUTPUT='out.grib'
@ -21,28 +21,28 @@ def example():
fin = open(INPUT) fin = open(INPUT)
fout = open(OUTPUT,'w') fout = open(OUTPUT,'w')
gid = grib_new_from_file(fin) gid = codes_new_from_file(fin)
grib_set_long(gid, "scaledValueOfFirstFixedSurface", 15); codes_set_long(gid, "scaledValueOfFirstFixedSurface", 15);
grib_set_long(gid, "scaleFactorOfFirstFixedSurface", 1) codes_set_long(gid, "scaleFactorOfFirstFixedSurface", 1)
level=grib_get_double(gid, "level") level=codes_get_double(gid, "level")
assert( level == 1.5 ) assert( level == 1.5 )
# set type of level to surface # set type of level to surface
grib_set(gid,'typeOfFirstFixedSurface','sfc') codes_set(gid,'typeOfFirstFixedSurface','sfc')
grib_set_missing(gid,'scaleFactorOfFirstFixedSurface') codes_set_missing(gid,'scaleFactorOfFirstFixedSurface')
grib_set_missing(gid,'scaledValueOfFirstFixedSurface') codes_set_missing(gid,'scaledValueOfFirstFixedSurface')
grib_write(gid,fout) codes_write(gid,fout)
grib_release(gid) codes_release(gid)
fin.close() fin.close()
fout.close() fout.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -11,7 +11,7 @@
import traceback import traceback
import sys import sys
from gribapi import * from eccodes import *
VERBOSE=1 # verbose error reporting VERBOSE=1 # verbose error reporting
@ -29,22 +29,22 @@ def example():
assert(len(pv) == numberOfCoefficients) assert(len(pv) == numberOfCoefficients)
fout = open('out.grib1','w') fout = open('out.grib1','w')
gid = grib_new_from_samples('reduced_gg_sfc_grib1') gid = codes_new_from_samples('reduced_gg_sfc_grib1')
grib_set(gid,'typeOfLevel','hybrid') codes_set(gid,'typeOfLevel','hybrid')
grib_set(gid,'level',2) codes_set(gid,'level',2)
grib_set(gid,'PVPresent',1) codes_set(gid,'PVPresent',1)
grib_set_array(gid,'pv',pv) codes_set_array(gid,'pv',pv)
grib_write(gid,fout) codes_write(gid,fout)
grib_release(gid) codes_release(gid)
fout.close() fout.close()
def main(): def main():
try: try:
example() example()
except GribInternalError,err: except CodesInternalError,err:
if VERBOSE: if VERBOSE:
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
else: else:

View File

@ -65,6 +65,7 @@ if( HAVE_PYTHON )
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/gribapi_swig.py DESTINATION ${PYTHON_DEST}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/gribapi_swig.py DESTINATION ${PYTHON_DEST})
install(FILES gribapi.py DESTINATION ${PYTHON_DEST}) install(FILES gribapi.py DESTINATION ${PYTHON_DEST})
install(FILES eccodes.py DESTINATION ${PYTHON_DEST})
endif() endif()

81
python/eccodes.py Normal file
View File

@ -0,0 +1,81 @@
from gribapi import GRIB_CHECK as CODES_CHECK
from gribapi import grib_new_from_file as codes_new_from_file
from gribapi import grib_count_in_file as codes_count_in_file
from gribapi import grib_multi_support_on as codes_multi_support_on
from gribapi import grib_multi_support_off as codes_multi_support_off
from gribapi import grib_release as codes_release
from gribapi import grib_get_string as codes_get_string
from gribapi import grib_set_string as codes_set_string
from gribapi import grib_gribex_mode_on as codes_gribex_mode_on
from gribapi import grib_gribex_mode_off as codes_gribex_mode_off
from gribapi import grib_write as codes_write
from gribapi import grib_multi_write as codes_multi_write
from gribapi import grib_multi_append as codes_multi_append
from gribapi import grib_get_size as codes_get_size
from gribapi import grib_get_string_length as codes_get_string_length
from gribapi import grib_skip_computed as codes_skip_computed
from gribapi import grib_skip_coded as codes_skip_coded
from gribapi import grib_skip_edition_specific as codes_skip_edition_specific
from gribapi import grib_skip_duplicates as codes_skip_duplicates
from gribapi import grib_skip_read_only as codes_skip_read_only
from gribapi import grib_skip_function as codes_skip_function
from gribapi import grib_iterator_new as codes_iterator_new
from gribapi import grib_iterator_delete as codes_iterator_delete
from gribapi import grib_iterator_next as codes_iterator_next
from gribapi import grib_keys_iterator_new as codes_keys_iterator_new
from gribapi import grib_keys_iterator_next as codes_keys_iterator_next
from gribapi import grib_keys_iterator_delete as codes_keys_iterator_delete
from gribapi import grib_keys_iterator_get_name as codes_keys_iterator_get_name
from gribapi import grib_keys_iterator_rewind as codes_keys_iterator_rewind
from gribapi import grib_get_long as codes_get_long
from gribapi import grib_get_double as codes_get_double
from gribapi import grib_set_long as codes_set_long
from gribapi import grib_set_double as codes_set_double
from gribapi import grib_new_from_samples as codes_new_from_samples
from gribapi import grib_clone as codes_clone
from gribapi import grib_set_double_array as codes_set_double_array
from gribapi import grib_get_double_array as codes_get_double_array
from gribapi import grib_set_long_array as codes_set_long_array
from gribapi import grib_get_long_array as codes_get_long_array
from gribapi import grib_multi_new as codes_multi_new
from gribapi import grib_multi_release as codes_multi_release
from gribapi import grib_copy_namespace as codes_copy_namespace
from gribapi import grib_index_new_from_file as codes_index_new_from_file
from gribapi import grib_index_add_file as codes_index_add_file
from gribapi import grib_index_release as codes_index_release
from gribapi import grib_index_get_size as codes_index_get_size
from gribapi import grib_index_get_long as codes_index_get_long
from gribapi import grib_index_get_string as codes_index_get_string
from gribapi import grib_index_get_double as codes_index_get_double
from gribapi import grib_index_select_long as codes_index_select_long
from gribapi import grib_index_select_double as codes_index_select_double
from gribapi import grib_index_select_string as codes_index_select_string
from gribapi import grib_new_from_index as codes_new_from_index
from gribapi import grib_get_message_size as codes_get_message_size
from gribapi import grib_get_message_offset as codes_get_message_offset
from gribapi import grib_get_double_element as codes_get_double_element
from gribapi import grib_get_double_elements as codes_get_double_elements
from gribapi import grib_get_elements as codes_get_elements
from gribapi import grib_set_missing as codes_set_missing
from gribapi import grib_set_key_vals as codes_set_key_vals
from gribapi import grib_is_missing as codes_is_missing
from gribapi import grib_is_defined as codes_is_defined
from gribapi import grib_find_nearest as codes_find_nearest
from gribapi import grib_get_native_type as codes_get_native_type
from gribapi import grib_get as codes_get
from gribapi import grib_get_array as codes_get_array
from gribapi import grib_get_values as codes_get_values
from gribapi import grib_set_values as codes_set_values
from gribapi import grib_set as codes_set
from gribapi import grib_set_array as codes_set_array
from gribapi import grib_index_get as codes_index_get
from gribapi import grib_index_select as codes_index_select
from gribapi import grib_index_write as codes_index_write
from gribapi import grib_index_read as codes_index_read
from gribapi import grib_no_fail_on_wrong_length as codes_no_fail_on_wrong_length
from gribapi import grib_gts_header as codes_gts_header
from gribapi import grib_get_api_version as codes_get_api_version
from gribapi import grib_get_message as codes_get_message
from gribapi import grib_new_from_message as codes_new_from_message
from gribapi import GribInternalError as CodesInternalError