Python example for setting bitmap

This commit is contained in:
Shahram Najm 2013-06-07 16:47:47 +01:00
parent e3992d4fd9
commit 5b11a9c954
3 changed files with 69 additions and 2 deletions

View File

@ -1,7 +1,8 @@
if WITH_PYTHON
AM_CFLAGS = @WARN_PEDANTIC@ @WERROR@
TESTS = clone.sh count_messages.sh get.sh index.sh iterator.sh keys_iterator.sh multi_write.sh nearest.sh print_data.sh samples.sh set.sh set_missing.sh binary_message.sh
TESTS = clone.sh count_messages.sh get.sh index.sh iterator.sh keys_iterator.sh multi_write.sh nearest.sh print_data.sh \
samples.sh set.sh set_missing.sh binary_message.sh set_bitmap.sh
TESTS_ENVIRONMENT = TOPBUILDDIR=$(top_builddir) PYTHON=$(PYTHON)
noinst_PROGRAMS = keys_iterator print_data iterator count_messages
@ -13,5 +14,6 @@ INCLUDES = -I$(top_builddir)/src
LDADD = $(top_builddir)/src/libgrib_api.la
DEPENDENCIES = $(LDADD)
EXTRA_DIST = $(TESTS) include.sh clone.py count_messages.py get.py index.py iterator.py keys_iterator.py multi_write.py nearest.py print_data.py samples.py set.py set_missing.py binary_message.py set_pv.py
EXTRA_DIST = $(TESTS) include.sh clone.py count_messages.py get.py index.py iterator.py keys_iterator.py multi_write.py \
nearest.py print_data.py samples.py set.py set_missing.py binary_message.py set_pv.py set_bitmap.py
endif

View File

@ -0,0 +1,58 @@
import traceback
import sys
# Copyright 2005-2013 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
from eccode import *
INPUT = '../../data/regular_latlon_surface.grib1'
OUTPUT = 'out.grib'
MISSING = 9999
VERBOSE = 1 # verbose error reporting
def example():
fin = open(INPUT)
fout = open(OUTPUT,'w')
gid = new_from_file(fin)
set(gid,'missingValue', MISSING)
values = get_values(gid)
set(gid, 'bitmapPresent', 1)
# Change some data values to be missing
num_missing = 0
for i in range(100):
if i % 2 == 0:
values[i] = MISSING
num_missing += 1
set_values(gid, values)
# Check counts of missing and non-missing values
num_data = get(gid,'numberOfDataPoints',int)
assert(get(gid,'numberOfCodedValues',int) == num_data-num_missing)
assert(get(gid,'numberOfMissing',int) == num_missing)
write(gid,fout)
release(gid)
fin.close()
fout.close()
def main():
try:
example()
except InternalError,err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
print >> sys.stderr,err.msg
return 1
if __name__ == "__main__":
sys.exit(main())

7
examples/python/set_bitmap.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
. ./include.sh
REDIRECT=/dev/null
$PYTHON set_bitmap.py 2> $REDIRECT > $REDIRECT
rm out.grib || true