00001 ! Copyright 2005-2015 ECMWF
00002 ! This software is licensed under the terms of the Apache Licence Version 2.0
00003 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
00004 !
00005 ! In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
00006 ! virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
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 logical :: grib1Example
00024
00025 grib1Example=.true.
00026
00027 if (grib1Example) then
00028 ! GRIB 1 example
00029 call grib_open_file(infile,'../../data/regular_latlon_surface.grib1','r')
00030 else
00031 ! GRIB 2 example
00032 call grib_open_file(infile,'../../data/regular_latlon_surface.grib2','r')
00033 end if
00034
00035 call grib_open_file(outfile,'out.grib','w')
00036
00037 ! a new grib message is loaded from file
00038 ! igrib is the grib id to be used in subsequent calls
00039 call grib_new_from_file(infile,igrib)
00040
00041 ! The missingValue is not coded in the message.
00042 ! It is a value we define as a placeholder for a missing value
00043 ! in a point of the grid.
00044 ! It should be choosen in a way that it cannot be confused
00045 ! with a valid field value
00046 missingValue=9999
00047 call grib_set(igrib, 'missingValue',missingValue)
00048 write(*,*) 'missingValue=',missingValue
00049
00050 ! get the size of the values array
00051 call grib_get_size(igrib,'values',numberOfValues)
00052 write(*,*) 'numberOfValues=',numberOfValues
00053
00054 allocate(values(numberOfValues), stat=iret)
00055
00056 ! get data values
00057 call grib_get(igrib,'values',values)
00058
00059 ! enable bitmap
00060 call grib_set(igrib,"bitmapPresent",1)
00061
00062 ! some values are missing
00063 values(1:10) = missingValue
00064
00065 ! set the values (the bitmap will be automatically built)
00066 call grib_set(igrib,'values', values)
00067
00068 ! write modified message to a file
00069 call grib_write(igrib,outfile)
00070
00071 ! FREE MEMORY
00072 call grib_release(igrib)
00073
00074 call grib_close_file(infile)
00075
00076 call grib_close_file(outfile)
00077
00078 deallocate(values)
00079
00080 end program set_bitmap