set_bitmap.f90

How to set and use a bitmap.

00001 ! Copyright 2005-2007 ECMWF
00002 ! 
00003 ! Licensed under the GNU Lesser General Public License which
00004 ! incorporates the terms and conditions of version 3 of the GNU
00005 ! General Public License.
00006 ! See LICENSE and gpl-3.0.txt for details.
00007 !
00008 !
00009 !  Description: how to set a bitmap in a grib message 
00010 !
00011 !
00012 !  Author: Enrico Fucile 
00013 !
00014 !
00015 program set_bitmap
00016   use grib_api
00017   implicit none
00018   integer                         :: infile,outfile
00019   integer                         :: igrib, iret
00020   integer                         :: numberOfValues
00021   real, dimension(:), allocatable :: values
00022   real                            :: missingValue
00023   integer                         :: editionNumber
00024   logical                         :: grib1Example
00025 
00026   grib1Example=.true.
00027 
00028   if (grib1Example) then
00029     ! GRIB 1 example
00030     call grib_open_file(infile,'../../data/regular_latlon_surface.grib1','r')
00031   else
00032     ! GRIB 2 example
00033     call grib_open_file(infile,'../../data/regular_latlon_surface.grib2','r')
00034   end if
00035   
00036   call grib_open_file(outfile,'out.grib','w')
00037   
00038   !     a new grib message is loaded from file
00039   !     igrib is the grib id to be used in subsequent calls
00040   call grib_new_from_file(infile,igrib)
00041   
00042   ! The missingValue is not coded in the message. 
00043   ! It is a value we define as a placeholder for a missing value
00044   ! in a point of the grid.
00045   ! It should be choosen in a way that it cannot be confused 
00046   ! with a valid field value
00047   missingValue=9999
00048   call grib_set(igrib, 'missingValue',missingValue)
00049   write(*,*) 'missingValue=',missingValue
00050 
00051   ! get the size of the values array
00052   call grib_get_size(igrib,'values',numberOfValues)
00053   write(*,*) 'numberOfValues=',numberOfValues
00054   
00055   allocate(values(numberOfValues), stat=iret)
00056 
00057   ! get data values
00058   call grib_get(igrib,'values',values)
00059   
00060   call grib_get(igrib,'editionNumber',editionNumber);
00061   if (editionNumber == 1) then
00062     ! enable bitmap in a grib1
00063     call grib_set(igrib,"bitmapPresent",1)
00064   else
00065     ! enable bitmap in a grib2
00066     call grib_set(igrib,"bitMapIndicator",0)
00067   endif
00068 
00069   ! some values are missing
00070   values(1:10) = missingValue
00071 
00072   ! set the values (the bitmap will be automatically built)
00073   call grib_set(igrib,'values', values)
00074 
00075   !  write modified message to a file
00076   call grib_write(igrib,outfile)
00077   
00078   ! FREE MEMORY
00079   call grib_release(igrib)
00080   
00081   call grib_close_file(infile)
00082 
00083   call grib_close_file(outfile)
00084 
00085   deallocate(values)
00086 
00087 end program set_bitmap

Generated on Tue Jul 8 10:17:28 2008 for grib_api by  doxygen 1.5.4