00001 C Copyright 2005-2016 ECMWF 00002 C This software is licensed under the terms of the Apache Licence Version 2.0 00003 C which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. 00004 C 00005 C In applying this licence, ECMWF does not waive the privileges and immunities granted to it by 00006 C virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction. 00007 C 00008 C Fortran 77 Implementation: get_fortran 00009 C 00010 C Description: how to get values using keys. 00011 C 00012 C Author: Enrico Fucile 00013 C 00014 C 00015 C 00016 program get 00017 implicit none 00018 integer maxNumberOfValues 00019 parameter( maxNumberOfValues = 10000 ) 00020 include 'grib_api_f77.h' 00021 integer ifile 00022 integer iret 00023 integer igrib 00024 integer i 00025 real*8 latitudeOfFirstPointInDegrees 00026 real*8 longitudeOfFirstPointInDegrees 00027 real*8 latitudeOfLastPointInDegrees 00028 real*8 longitudeOfLastPointInDegrees 00029 real*8 jDirectionIncrementInDegrees 00030 real*8 iDirectionIncrementInDegrees 00031 integer*4 numberOfPointsAlongAParallel 00032 integer*4 numberOfPointsAlongAMeridian 00033 real*8 values(maxNumberOfValues) 00034 integer*4 numberOfValues 00035 real*8 average 00036 character*256 error 00037 integer*4 size 00038 00039 size=maxNumberOfValues 00040 ifile=5 00041 00042 iret=grib_open_file(ifile 00043 X,'../../data/regular_latlon_surface.grib1','r') 00044 call grib_check(iret) 00045 00046 C a new grib message is loaded from file 00047 C igrib is the grib id to be used in subsequent calls 00048 call grib_check( grib_new_from_file(ifile,igrib) ) 00049 00050 C get as a integer 00051 call grib_check(grib_get_int(igrib,'numberOfPointsAlongAParallel' 00052 X,numberOfPointsAlongAParallel) ) 00053 write(*,*) 'numberOfPointsAlongAParallel=' 00054 X,numberOfPointsAlongAParallel 00055 00056 C get as a integer 00057 call grib_check( grib_get_int(igrib,'numberOfPointsAlongAMeridian' 00058 X,numberOfPointsAlongAMeridian) ) 00059 write(*,*) 'numberOfPointsAlongAMeridian=' 00060 X,numberOfPointsAlongAMeridian 00061 00062 C get as a real8 00063 call grib_check( grib_get_real8(igrib 00064 X,'latitudeOfFirstGridPointInDegrees' 00065 X,latitudeOfFirstPointInDegrees) ) 00066 write(*,*) 'latitudeOfFirstGridPointInDegrees=' 00067 X,latitudeOfFirstPointInDegrees 00068 00069 C get as a real8 00070 call grib_check( grib_get_real8(igrib 00071 X,'longitudeOfFirstGridPointInDegrees' 00072 X,longitudeOfFirstPointInDegrees) ) 00073 write(*,*) 'longitudeOfFirstGridPointInDegrees=' 00074 X,longitudeOfFirstPointInDegrees 00075 00076 C get as a real8 00077 call grib_check( grib_get_real8(igrib 00078 X,'latitudeOfLastGridPointInDegrees' 00079 X,latitudeOfLastPointInDegrees) ) 00080 write(*,*) 'latitudeOfLastGridPointInDegrees=' 00081 X,latitudeOfLastPointInDegrees 00082 00083 C get as a real8 00084 call grib_check( grib_get_real8(igrib 00085 X,'longitudeOfLastGridPointInDegrees' 00086 X,longitudeOfLastPointInDegrees) ) 00087 write(*,*) 'longitudeOfLastGridPointInDegrees=' 00088 X,longitudeOfLastPointInDegrees 00089 00090 C get as a real8 00091 call grib_check( grib_get_real8(igrib 00092 X,'jDirectionIncrementInDegrees' 00093 X,jDirectionIncrementInDegrees) ) 00094 write(*,*) 'jDirectionIncrementInDegrees=' 00095 X,jDirectionIncrementInDegrees 00096 00097 C get as a real8 00098 call grib_check( grib_get_real8(igrib 00099 X,'iDirectionIncrementInDegrees' 00100 X,iDirectionIncrementInDegrees) ) 00101 write(*,*) 'iDirectionIncrementInDegrees=' 00102 X,iDirectionIncrementInDegrees 00103 00104 C get the size of the values array 00105 call grib_check(grib_get_size(igrib,'values',numberOfValues)) 00106 write(*,*) 'numberOfValues=',numberOfValues 00107 00108 C get data values 00109 call grib_check(grib_get_real8_array(igrib,'values',values,size)) 00110 if ( size .ne. numberOfValues ) then 00111 write(*,*) 'ERROR: wrong numberOfValues' 00112 stop 00113 endif 00114 00115 average = 0 00116 do i=1,numberOfValues 00117 average = average + values(i); 00118 enddo 00119 00120 average =average / numberOfValues 00121 00122 write(*,*)'There are ',numberOfValues 00123 X,' average is ',average 00124 00125 call grib_check(grib_release(igrib)) 00126 00127 call grib_check(grib_close_file(ifile)) 00128 00129 end