from ncepgrib2 import Grib2Decode, Grib2Encode
import numpy as N
# open a GRIB2 file, create a Grib2 class instance.
filein = '../sampledata/gfs.grb'
print 'input grib file:',filein
grbs = Grib2Decode(filein)
# open a file for output.
fileout = 'test.grb'
print 'output grib file:',fileout
f=open(fileout,'wb')
# test encoding sxn0, sxn1.
print 'message number,field number,bitmap flag,min,max:'
print '------------------------------------------------'
for nmsg,grb in enumerate(grbs):
    grbo = Grib2Encode(grb.discipline_code,grb.identification_section)
    # add grid definition template
    if hasattr(grb,'grid_definition_list'):
       grbo.addgrid(grb.grid_definition_info,grb.grid_definition_template,deflist=grb.grid_definition_list)
    else:
       grbo.addgrid(grb.grid_definition_info,grb.grid_definition_template)
    field = grb.data()
    bitmapflag = grb.bitmap_indicator_flag
    if bitmapflag == 0:
        bitmap = grb._bitmap
    else:
        bitmap = None
    if bitmap is not None:
        fieldcompress = N.compress(N.ravel(bitmap),N.ravel(field))
        fieldmin = fieldcompress.min(); fieldmax = fieldcompress.max()
    else:
        fieldmin = field.min(); fieldmax = field.max()
    print nmsg+1,bitmapflag,fieldmin,fieldmax
    # reset data to match scanning mode flags.
    # (scanning mode madness is undone when data is extracted
    #  from grib message - to write it back it correctly it
    #  must be reset to be consistent with scanning model flags).
    # rows scan in the -x direction (so flip)
    if grb.scanmodeflags[0]:
        fieldsave = field.astype('f') # casting makes a copy
        field[:,:] = fieldsave[:,::-1]
    # columns scan in the -y direction (so flip)
    if not grb.scanmodeflags[1]:
        fieldsave = field.astype('f') # casting makes a copy
        field[:,:] = fieldsave[::-1,:]
    # adjacent rows scan in opposite direction.
    # (flip every other row)
    if grb.scanmodeflags[3]:
        fieldsave = field.astype('f') # casting makes a copy
        field[1::2,:] = fieldsave[1::2,::-1]
    # add product definition template, data representation template
    # and data (field and optional bitmap).
    if hasattr(grb,'extra_vertical_coordinate_info'):
        grbo.addfield(grb.product_definition_template_number,grb.product_definition_template,grb.data_representation_template_number,grb.data_representation_template,field,coordlist=grb.extra_vertical_coordinate_info,bitmapflag=bitmapflag,bitmap=bitmap)
    else:
        grbo.addfield(grb.product_definition_template_number,grb.product_definition_template,grb.data_representation_template_number,grb.data_representation_template,field,bitmapflag=bitmapflag,bitmap=bitmap)
    # finalize the grib message.
    grbo.end()
    # write it to the file.
    f.write(grbo.msg)
# close the output file
f.close()
# close the input GRIB2 file.
print 'done! '+filein+' and '+fileout+' should have identical data'
print '(run grib_list on both and compare output)'

@@ -58,16 +58,27 @@
 py as N%0A
+import sys%0A
 # open a
@@ -154,22 +154,33 @@
 fs.grb'%0A
-print 
+sys.stdout.write(
 'input g
@@ -192,16 +192,24 @@
 ile:
-',
+ %25s%5Cn' %25 
 filein
+)
 %0Agrb
@@ -279,22 +279,33 @@
 st.grb'%0A
-print 
+sys.stdout.write(
 'output 
@@ -318,17 +318,25 @@
 ile:
-',
+ %25s%5Cn' %25 
 fileout
+)
 %0Af=o
@@ -381,22 +381,33 @@
 , sxn1.%0A
-print 
+sys.stdout.write(
 'message
@@ -447,24 +447,38 @@
 min,max:
-'%0Aprint 
+%5Cn')%0Asys.stdout.write(
 '-------
@@ -518,17 +518,20 @@
 --------
-'
+%5Cn')
 %0Afor nms
@@ -1284,22 +1284,52 @@
 x()%0A    
-print 
+sys.stdout.write('%25s %25s %25s %25s%5Cn' %25 (
 nmsg+1,b
@@ -1355,16 +1355,18 @@
 fieldmax
+))
 %0A    # r
@@ -2963,22 +2963,33 @@
 2 file.%0A
-print 
+sys.stdout.write(
 'done! '
@@ -3044,16 +3044,30 @@
 data
-'%0Aprint 
+%5Cn')%0Asys.stdout.write(
 '(ru
@@ -3105,10 +3105,13 @@
  output)
-'
+%5Cn')
 %0A

update for python3