ECC-117: Eliminate function codes_is_defined (Part 1)

This commit is contained in:
Shahram Najm 2015-05-13 13:00:31 +01:00
parent 22e641ba08
commit 5b36903347
5 changed files with 68 additions and 94 deletions

View File

@ -26,98 +26,84 @@ program grib_get_keys
real, dimension(:), allocatable :: values
integer :: numberOfValues
real :: average,min_val, max_val
integer :: is_missing, is_defined
integer :: is_missing
call codes_open_file(ifile, &
'../../data/reduced_latlon_surface.grib1','r')
! Loop on all the messages in a file.
! a new grib message is loaded from file
! igrib is the grib id to be used in subsequent calls
call codes_grib_new_from_file(ifile,igrib, iret)
LOOP: DO WHILE (iret /= CODES_END_OF_FILE)
!check if the value of the key is MISSING
is_missing=0;
call codes_is_missing(igrib,'Ni',is_missing);
if ( is_missing /= 1 ) then
!check if the value of the key is MISSING
is_missing=0;
call codes_is_missing(igrib,'Ni',is_missing);
if ( is_missing /= 1 ) then
! get as a integer
call codes_get(igrib,'Ni',numberOfPointsAlongAParallel)
write(*,*) 'numberOfPointsAlongAParallel=', &
numberOfPointsAlongAParallel
else
else
write(*,*) 'numberOfPointsAlongAParallel is missing'
endif
!check for existence of keys
is_defined=0;
call codes_is_defined(igrib,'edition',is_defined);
if ( is_defined == 0 ) then
write(0,*) 'ERROR: An expected key was not defined!!'
call exit(1)
endif
call codes_is_defined(igrib,'ThisIsNoLoveSong',is_defined);
if ( is_defined == 1 ) then
write(0,*) 'ERROR: An unexpected key was defined!!'
call exit(1)
endif
endif
! get as a integer
call codes_get(igrib,'Nj',numberOfPointsAlongAMeridian)
write(*,*) 'numberOfPointsAlongAMeridian=', &
numberOfPointsAlongAMeridian
! get as a real
call codes_get(igrib, 'latitudeOfFirstGridPointInDegrees', &
! get as a integer
call codes_get(igrib,'Nj',numberOfPointsAlongAMeridian)
write(*,*) 'numberOfPointsAlongAMeridian=', &
numberOfPointsAlongAMeridian
! get as a real
call codes_get(igrib, 'latitudeOfFirstGridPointInDegrees', &
latitudeOfFirstPointInDegrees)
write(*,*) 'latitudeOfFirstGridPointInDegrees=', &
write(*,*) 'latitudeOfFirstGridPointInDegrees=', &
latitudeOfFirstPointInDegrees
! get as a real
call codes_get(igrib, 'longitudeOfFirstGridPointInDegrees', &
longitudeOfFirstPointInDegrees)
write(*,*) 'longitudeOfFirstGridPointInDegrees=', &
longitudeOfFirstPointInDegrees
! get as a real
call codes_get(igrib, 'latitudeOfLastGridPointInDegrees', &
latitudeOfLastPointInDegrees)
write(*,*) 'latitudeOfLastGridPointInDegrees=', &
latitudeOfLastPointInDegrees
! get as a real
call codes_get(igrib, 'longitudeOfLastGridPointInDegrees', &
longitudeOfLastPointInDegrees)
write(*,*) 'longitudeOfLastGridPointInDegrees=', &
longitudeOfLastPointInDegrees
! get the size of the values array
call codes_get_size(igrib,'values',numberOfValues)
write(*,*) 'numberOfValues=',numberOfValues
allocate(values(numberOfValues), stat=iret)
! get data values
call codes_get(igrib,'values',values)
call codes_get(igrib,'min',min_val) ! can also be obtained through minval(values)
call codes_get(igrib,'max',max_val) ! can also be obtained through maxval(values)
call codes_get(igrib,'average',average) ! can also be obtained through maxval(values)
deallocate(values)
! get as a real
call codes_get(igrib, 'longitudeOfFirstGridPointInDegrees', &
longitudeOfFirstPointInDegrees)
write(*,*) 'longitudeOfFirstGridPointInDegrees=', &
longitudeOfFirstPointInDegrees
! get as a real
call codes_get(igrib, 'latitudeOfLastGridPointInDegrees', &
latitudeOfLastPointInDegrees)
write(*,*) 'latitudeOfLastGridPointInDegrees=', &
latitudeOfLastPointInDegrees
! get as a real
call codes_get(igrib, 'longitudeOfLastGridPointInDegrees', &
longitudeOfLastPointInDegrees)
write(*,*) 'longitudeOfLastGridPointInDegrees=', &
longitudeOfLastPointInDegrees
! get the size of the values array
call codes_get_size(igrib,'values',numberOfValues)
write(*,*) 'numberOfValues=',numberOfValues
allocate(values(numberOfValues), stat=iret)
! get data values
call codes_get(igrib,'values',values)
call codes_get(igrib,'min',min_val) ! can also be obtained through minval(values)
call codes_get(igrib,'max',max_val) ! can also be obtained through maxval(values)
call codes_get(igrib,'average',average) ! can also be obtained through maxval(values)
deallocate(values)
write(*,*)'There are ',numberOfValues, &
write(*,*)'There are ',numberOfValues, &
' average is ',average, &
' min is ', min_val, &
' max is ', max_val
call codes_release(igrib)
call codes_grib_new_from_file(ifile,igrib, iret)
call codes_release(igrib)
call codes_grib_new_from_file(ifile,igrib, iret)
end do LOOP
call codes_close_file(ifile)
end program grib_get_keys

View File

@ -34,16 +34,14 @@ def example():
try:
print ' %s: %s' % (key,codes_get(gid,key))
except CodesInternalError,err:
print 'Error with key="%s" : %s' % (key,err.msg)
if codes_is_defined(gid,"A_very_silly_key"): raise Exception("Key was defined")
print 'Error with key="%s" : %s' % (key,err.msg)
print 'There are %d values, average is %f, min is %f, max is %f' % (
codes_get_size(gid,'values'),
codes_get(gid,'average'),
codes_get(gid,'min'),
codes_get(gid,'max')
)
codes_get_size(gid,'values'),
codes_get(gid,'average'),
codes_get(gid,'min'),
codes_get(gid,'max')
)
codes_release(gid)

View File

@ -67,8 +67,6 @@
external grib_get_string
integer grib_is_missing
external grib_is_missing
integer grib_is_defined
external grib_is_defined
integer grib_set_string
external grib_copy_namespace
integer grib_copy_namespace

View File

@ -136,11 +136,6 @@ def test():
new_Ni = grib_get(gid,"Ni")
print "Set Ni back to its original value --> ",new_Ni
assert Ni == new_Ni
print "Check some keys to see if they are defined"
assert grib_is_defined(gid,"Ni")
assert grib_is_defined(gid,"edition")
assert not grib_is_defined(gid,"DarkThrone")
#grib_multi_write(gid,multi)

View File

@ -177,18 +177,15 @@ grib_handle* grib_new_handle ( grib_context* c )
static void check_definitions_version(grib_handle* h)
{
/* Check version of definition files is compatible with the engine */
int ret = 0;
long defs_file_version = 0;
if (grib_is_defined(h, "internalVersion")) {
ret = grib_get_long_internal(h, "internalVersion", &defs_file_version);
if (ret == GRIB_SUCCESS && (defs_file_version > LATEST_VERSION)) {
grib_context_log(h->context, GRIB_LOG_FATAL,
"Definition files version (%d) is greater than engine version (%d)!\n"
"These definition files are for a later version of the grib api engine.\n",
defs_file_version, LATEST_VERSION);
/* Check version of definition files is compatible with the engine */
long defs_file_version = 0;
int ret = grib_get_long(h, "internalVersion", &defs_file_version);
}
if (ret == GRIB_SUCCESS && (defs_file_version > LATEST_VERSION)) {
grib_context_log(h->context, GRIB_LOG_FATAL,
"Definition files version (%d) is greater than engine version (%d)!\n"
"These definition files are for a later version of the grib api engine.\n",
defs_file_version, LATEST_VERSION);
}
}