grib-api-1.14.4/ 0000740 0001750 0001750 00000000000 12642620053 013447 5 ustar alastair alastair grib-api-1.14.4/html/ 0000740 0001750 0001750 00000000000 12642617500 014416 5 ustar alastair alastair grib-api-1.14.4/html/set__pv_8f90-example.html 0000640 0001750 0001750 00000012172 12642617500 021147 0 ustar alastair alastair
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 pv values. 00010 ! 00011 ! 00012 ! Author: Anne Fouilloux 00013 ! 00014 ! 00015 program set_pv 00016 use grib_api 00017 implicit none 00018 integer :: numberOfLevels 00019 integer :: numberOfCoefficients 00020 integer :: outfile, igrib 00021 integer :: i, ios 00022 real, dimension(:),allocatable :: pv 00023 00024 numberOfLevels=60 00025 numberOfCoefficients=2*(numberOfLevels+1) 00026 00027 allocate(pv(numberOfCoefficients)) 00028 00029 ! read the model level coefficients from file 00030 open( unit=1, file="../../data/60_model_levels", & 00031 form="formatted",action="read") 00032 00033 do i=1,numberOfCoefficients,2 00034 read(unit=1,fmt=*, iostat=ios) pv(i), pv(i+1) 00035 if (ios /= 0) then 00036 print *, "I/O error: ",ios 00037 exit 00038 end if 00039 end do 00040 00041 ! print coefficients 00042 !do i=1,numberOfCoefficients,2 00043 ! print *," a=",pv(i)," b=",pv(i+1) 00044 !end do 00045 00046 close(unit=1) 00047 00048 call grib_open_file(outfile, 'out.grib1','w') 00049 00050 ! a new grib message is loaded from file 00051 ! igrib is the grib id to be used in subsequent calls 00052 call grib_new_from_samples(igrib, "reduced_gg_sfc_grib1") 00053 00054 ! set levtype to ml (model level) 00055 call grib_set(igrib,'levtype','ml') 00056 00057 ! set level 00058 call grib_set(igrib,'level',2) 00059 00060 ! set PVPresent as an integer 00061 call grib_set(igrib,'PVPresent',1) 00062 00063 call grib_set(igrib,'pv',pv) 00064 00065 ! write modified message to a file 00066 call grib_write(igrib,outfile) 00067 00068 ! FREE MEMORY 00069 call grib_release(igrib) 00070 deallocate(pv) 00071 00072 call grib_close_file(outfile) 00073 00074 end program set_pv
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 decode grib messages containing multiple 00010 ! fields. Try to turn on and off multi support to 00011 ! see the difference. Default is OFF. 00012 ! For all the tools defalut is multi support ON. 00013 ! 00014 ! 00015 ! Author: Enrico Fucile 00016 ! 00017 ! 00018 program multi 00019 use grib_api 00020 implicit none 00021 00022 integer :: iret 00023 character(len = 256) :: error 00024 integer(kind = 4) :: step 00025 integer :: ifile,igrib 00026 00027 call grib_open_file(ifile, '../../data/multi_created.grib2','r') 00028 00029 ! turn on support for multi fields messages */ 00030 call grib_multi_support_on() 00031 00032 ! turn off support for multi fields messages */ 00033 !call grib_multi_support_off() 00034 00035 call grib_new_from_file(ifile,igrib, iret) 00036 ! Loop on all the messages in a file. 00037 00038 write(*,*) 'step' 00039 do while (iret /= GRIB_END_OF_FILE) 00040 00041 call grib_get(igrib,'step', step) 00042 write(*,'(i3)') step 00043 00044 call grib_new_from_file(ifile,igrib, iret) 00045 00046 end do 00047 call grib_close_file(ifile) 00048 00049 end program multi 00050
00001 C Copyright 2005-2015 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 00009 C Fortran 77 Implementation: keys_iterator 00010 C 00011 C Description: 00012 C Example on how to use keys_iterator functions and the 00013 C grib_keys_iterator structure to get all the available 00014 C keys in a message. 00015 C 00016 C Author: Enrico Fucile 00017 C 00018 C 00019 C 00020 program keys_iterator 00021 implicit none 00022 include 'grib_api_f77.h' 00023 character*20 name_space 00024 integer kiter,ifile,igrib,iret 00025 character*256 key 00026 character*256 value 00027 character*512 all 00028 integer len,strlen 00029 integer grib_count 00030 len=256 00031 00032 ifile=5 00033 00034 call grib_check(grib_open_file(ifile, 00035 X'../../data/regular_latlon_surface.grib1','r')) 00036 00037 grib_count=0 00038 C Loop on all the messages in a file. 00039 10 iret=grib_new_from_file(ifile,igrib) 00040 if (igrib .eq. -1 ) then 00041 if (iret .ne.0) then 00042 call grib_check(iret) 00043 endif 00044 stop 00045 endif 00046 00047 grib_count=grib_count+1 00048 write(*,'("-- GRIB N.",I4," --")') grib_count 00049 00050 C valid name_spaces are ls and mars 00051 name_space='ls' 00052 C name_space=' ' to get all the keys */ 00053 C name_space=' ' 00054 00055 call grib_check( 00056 Xgrib_keys_iterator_new(igrib,kiter,name_space)) 00057 C call grib_check(grib_keys_iterator_skip_read_only(kiter)) 00058 C call grib_check(grib_keys_iterator_skip_function(kiter)) 00059 C call grib_check(grib_keys_iterator_skip_not_coded(kiter)) 00060 00061 20 if (grib_keys_iterator_next(kiter) .ne. 1) goto 10 00062 00063 call grib_check(grib_keys_iterator_get_name(kiter,key)) 00064 call grib_check(grib_get_string(igrib,key,value)) 00065 all='|' // trim(key)//'|' // ' = ' //'|' // trim(value) // '|' 00066 write(*,*) trim(all) 00067 00068 goto 20 00069 00070 call grib_check(grib_keys_iterator_delete(kiter)) 00071 00072 call grib_check(grib_release(igrib)) 00073 00074 call grib_check(grib_close_file(ifile)) 00075 00076 end 00077
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 get values using keys. 00010 ! 00011 ! Author: Enrico Fucile 00012 ! 00013 ! 00014 program get 00015 use grib_api 00016 implicit none 00017 00018 integer :: ifile 00019 integer :: iret 00020 integer :: igrib 00021 real :: latitudeOfFirstPointInDegrees 00022 real :: longitudeOfFirstPointInDegrees 00023 real :: latitudeOfLastPointInDegrees 00024 real :: longitudeOfLastPointInDegrees 00025 integer :: numberOfPointsAlongAParallel 00026 integer :: numberOfPointsAlongAMeridian 00027 real, dimension(:), allocatable :: values 00028 integer :: numberOfValues 00029 real :: average,min_val, max_val 00030 integer :: is_missing 00031 00032 call grib_open_file(ifile, & 00033 '../../data/reduced_latlon_surface.grib1','r') 00034 00035 ! Loop on all the messages in a file. 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(ifile,igrib, iret) 00040 00041 LOOP: DO WHILE (iret /= GRIB_END_OF_FILE) 00042 00043 !check if the value of the key is MISSING 00044 is_missing=0; 00045 call grib_is_missing(igrib,'numberOfPointsAlongAParallel', & 00046 is_missing); 00047 if ( is_missing /= 1 ) then 00048 ! get as a integer 00049 call grib_get(igrib,'numberOfPointsAlongAParallel', & 00050 numberOfPointsAlongAParallel) 00051 write(*,*) 'numberOfPointsAlongAParallel=', & 00052 numberOfPointsAlongAParallel 00053 else 00054 write(*,*) 'numberOfPointsAlongAParallel is missing' 00055 endif 00056 ! get as a integer 00057 call grib_get(igrib,'numberOfPointsAlongAMeridian', & 00058 numberOfPointsAlongAMeridian) 00059 write(*,*) 'numberOfPointsAlongAMeridian=', & 00060 numberOfPointsAlongAMeridian 00061 00062 ! get as a real 00063 call grib_get(igrib, 'latitudeOfFirstGridPointInDegrees', & 00064 latitudeOfFirstPointInDegrees) 00065 write(*,*) 'latitudeOfFirstGridPointInDegrees=', & 00066 latitudeOfFirstPointInDegrees 00067 00068 ! get as a real 00069 call grib_get(igrib, 'longitudeOfFirstGridPointInDegrees', & 00070 longitudeOfFirstPointInDegrees) 00071 write(*,*) 'longitudeOfFirstGridPointInDegrees=', & 00072 longitudeOfFirstPointInDegrees 00073 00074 ! get as a real 00075 call grib_get(igrib, 'latitudeOfLastGridPointInDegrees', & 00076 latitudeOfLastPointInDegrees) 00077 write(*,*) 'latitudeOfLastGridPointInDegrees=', & 00078 latitudeOfLastPointInDegrees 00079 00080 ! get as a real 00081 call grib_get(igrib, 'longitudeOfLastGridPointInDegrees', & 00082 longitudeOfLastPointInDegrees) 00083 write(*,*) 'longitudeOfLastGridPointInDegrees=', & 00084 longitudeOfLastPointInDegrees 00085 00086 00087 ! get the size of the values array 00088 call grib_get_size(igrib,'values',numberOfValues) 00089 write(*,*) 'numberOfValues=',numberOfValues 00090 00091 allocate(values(numberOfValues), stat=iret) 00092 ! get data values 00093 call grib_get(igrib,'values',values) 00094 call grib_get(igrib,'min',min_val) ! can also be obtained through minval(values) 00095 call grib_get(igrib,'max',max_val) ! can also be obtained through maxval(values) 00096 call grib_get(igrib,'average',average) ! can also be obtained through maxval(values) 00097 00098 write(*,*)'There are ',numberOfValues, & 00099 ' average is ',average, & 00100 ' min is ', min_val, & 00101 ' max is ', max_val 00102 00103 call grib_release(igrib) 00104 00105 call grib_new_from_file(ifile,igrib, iret) 00106 00107 end do LOOP 00108 00109 call grib_close_file(ifile) 00110 00111 deallocate(values) 00112 end program get
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: 00010 ! How to use keys_iterator to get all the available 00011 ! keys in a message. 00012 ! 00013 ! Author: Enrico Fucile 00014 ! 00015 ! 00016 program keys_iterator 00017 use grib_api 00018 implicit none 00019 character(len=20) :: name_space 00020 integer :: kiter,ifile,igrib,iret 00021 character(len=256) :: key 00022 character(len=256) :: value 00023 character(len=512) :: all 00024 integer :: grib_count 00025 00026 call grib_open_file(ifile, & 00027 '../../data/regular_latlon_surface.grib1','r') 00028 00029 ! Loop on all the messages in a file. 00030 00031 call grib_new_from_file(ifile,igrib, iret) 00032 00033 do while (iret /= GRIB_END_OF_FILE) 00034 00035 grib_count=grib_count+1 00036 write(*,*) '-- GRIB N. ',grib_count,' --' 00037 00038 ! valid name_spaces are ls and mars 00039 name_space='ls' 00040 00041 call grib_keys_iterator_new(igrib,kiter,name_space) 00042 00043 do 00044 call grib_keys_iterator_next(kiter, iret) 00045 00046 if (iret .ne. 1) exit 00047 00048 call grib_keys_iterator_get_name(kiter,key) 00049 call grib_get(igrib,trim(key),value) 00050 all=trim(key)// ' = ' // trim(value) 00051 write(*,*) trim(all) 00052 00053 end do 00054 00055 call grib_keys_iterator_delete(kiter) 00056 call grib_release(igrib) 00057 call grib_new_from_file(ifile,igrib, iret) 00058 end do 00059 00060 00061 call grib_close_file(ifile) 00062 00063 end program keys_iterator 00064
>grib_dump -H ../data/reduced_gaussian_model_level.grib1
> grib_dump -D ../data/regular_latlon_surface.grib1
>grib_dump -C ../data/regular_latlon_surface.grib1 #include <grib_api.h> /* This code was generated automatically */ int main(int argc,const char** argv) { grib_handle *h = NULL; size_t size = 0; double* vdouble = NULL; long* vlong = NULL; FILE* f = NULL; const char* p = NULL; const void* buffer = NULL; if(argc != 2) { fprintf(stderr,"usage: %s out\n",argv[0]); exit(1); } h = grib_handle_new_from_samples(NULL,"GRIB1"); if(!h) { fprintf(stderr,"Cannot create grib handle\n"); exit(1); } GRIB_CHECK(grib_set_long(h,"editionNumber",1),0); GRIB_CHECK(grib_set_long(h,"table2Version",128),0); /* 98 = European Center for Medium-Range Weather Forecasts (grib1/0.table) */ GRIB_CHECK(grib_set_long(h,"centre",98),0); GRIB_CHECK(grib_set_long(h,"generatingProcessIdentifier",130),0); GRIB_CHECK(grib_set_long(h,"gridDefinition",255),0); /* 128 = 10000000 (1=1) Section 2 included (2=0) Section 3 omited See grib1/1.table */ GRIB_CHECK(grib_set_long(h,"section1Flags",128),0); /* 167 = 2 metre temperature (K) (grib1/2.98.128.table) */ GRIB_CHECK(grib_set_long(h,"indicatorOfParameter",167),0); /* 1 = Surface (of the Earth, which includes sea surface) (grib1/3.table) */ GRIB_CHECK(grib_set_long(h,"indicatorOfTypeOfLevel",1),0); GRIB_CHECK(grib_set_long(h,"level",0),0); GRIB_CHECK(grib_set_long(h,"yearOfCentury",8),0); GRIB_CHECK(grib_set_long(h,"month",2),0); GRIB_CHECK(grib_set_long(h,"day",6),0); GRIB_CHECK(grib_set_long(h,"hour",12),0); GRIB_CHECK(grib_set_long(h,"minute",0),0); /* 1 = Hour (grib1/4.table) */ GRIB_CHECK(grib_set_long(h,"unitOfTimeRange",1),0); GRIB_CHECK(grib_set_long(h,"P1",0),0); GRIB_CHECK(grib_set_long(h,"P2",0),0); /* 0 = Forecast product valid at reference time + P1 (P1>0) (grib1/5.table) */ GRIB_CHECK(grib_set_long(h,"timeRangeIndicator",0),0); GRIB_CHECK(grib_set_long(h,"numberIncludedInAverage",0),0); GRIB_CHECK(grib_set_long(h,"numberMissingFromAveragesOrAccumulations",0),0); GRIB_CHECK(grib_set_long(h,"centuryOfReferenceTimeOfData",21),0); /* 0 = Unknown code table entry (grib1/0.ecmf.table) */ GRIB_CHECK(grib_set_long(h,"subCentre",0),0); GRIB_CHECK(grib_set_long(h,"decimalScaleFactor",0),0); /* 1 = MARS labelling or ensemble forecast data (grib1/localDefinitionNumber.98.table) */ GRIB_CHECK(grib_set_long(h,"localDefinitionNumber",1),0); /* 1 = Operational archive (mars/class.table) */ GRIB_CHECK(grib_set_long(h,"marsClass",1),0); /* 2 = Analysis (mars/type.table) */ GRIB_CHECK(grib_set_long(h,"marsType",2),0); /* 1025 = Atmospheric model (mars/stream.table) */ GRIB_CHECK(grib_set_long(h,"marsStream",1025),0); p = "0001"; size = strlen(p)+1; GRIB_CHECK(grib_set_string(h,"experimentVersionNumber",p,&size),0); GRIB_CHECK(grib_set_long(h,"perturbationNumber",0),0); GRIB_CHECK(grib_set_long(h,"numberOfForecastsInEnsemble",0),0); GRIB_CHECK(grib_set_long(h,"numberOfVerticalCoordinateValues",0),0); GRIB_CHECK(grib_set_long(h,"pvlLocation",255),0); /* 0 = Latitude/Longitude Grid (grib1/6.table) */ GRIB_CHECK(grib_set_long(h,"dataRepresentationType",0),0); GRIB_CHECK(grib_set_long(h,"Ni",16),0); GRIB_CHECK(grib_set_long(h,"Nj",31),0); GRIB_CHECK(grib_set_long(h,"latitudeOfFirstGridPoint",60000),0); GRIB_CHECK(grib_set_long(h,"longitudeOfFirstGridPoint",0),0); /* 128 = 10000000 (1=1) Direction increments given (2=0) Earth assumed spherical with radius = 6367.47 km (5=0) u and v components resolved relative to easterly and northerly directions See grib1/7.table */ GRIB_CHECK(grib_set_long(h,"resolutionAndComponentFlags",128),0); GRIB_CHECK(grib_set_long(h,"latitudeOfLastGridPoint",0),0); GRIB_CHECK(grib_set_long(h,"longitudeOfLastGridPoint",30000),0); GRIB_CHECK(grib_set_long(h,"iDirectionIncrement",2000),0); GRIB_CHECK(grib_set_long(h,"jDirectionIncrement",2000),0); /* 0 = 00000000 (1=0) Points scan in +i direction (2=0) Points scan in -j direction (3=0) Adjacent points in i direction are consecutive See grib1/8.table */ GRIB_CHECK(grib_set_long(h,"scanningMode",0),0); /* ITERATOR */ /* NEAREST */ GRIB_CHECK(grib_set_long(h,"bitsPerValue",16),0); GRIB_CHECK(grib_set_long(h,"sphericalHarmonics",0),0); GRIB_CHECK(grib_set_long(h,"complexPacking",0),0); GRIB_CHECK(grib_set_long(h,"integerPointValues",0),0); GRIB_CHECK(grib_set_long(h,"additionalFlagPresent",0),0); /* gribSection5 */ /* Save the message */ f = fopen(argv[1],"w"); if(!f) { perror(argv[1]); exit(1); } GRIB_CHECK(grib_get_message(h,&buffer,&size),0); if(fwrite(buffer,1,size,f) != size) { perror(argv[1]); exit(1); } if(fclose(f)) { perror(argv[1]); exit(1); } grib_handle_delete(h); return 0; }
With the grib_api library, that is written entirely in C, some command line tools are provided to give a quick way to manipulate grib data. Moreover a Fortran interface 90 is available giving access to the main features of the C library.
The library is designed to access and modify messages in both editions with the same function calls using a set of Grib API keys to access the coded information ( examples: get.f90 set.f90, get.c, set.c, grib_get, grib_set ).
The keys available for a message are different depending not only on the edition but also and mainly on the type of each message and the information it contains. A list of all the available keys in a message can be obtained dynamically using the library as shown in keys_iterator.c or using the Grib tools as shown in grib_dump or grib_keys.
GRIB API will replace the GRIBEX function and a table of conversion between the numeric encoding of GRIBEX and the alphanumeric keys of GRIB API is provided to help the migration.
To learn how to use the grib_api we recommend the user works through the Grib API examples.
Reference manuals are also provided for the C library (organized in C interface) and for the Fortran 90 interface.
Installation instructions are also provided.
> xlc -o foo foo.c $GRIB_API_INCLUDE $GRIB_API_LIB -lm
> xlf90 -o foo foo.f90 $GRIB_API_INCLUDE $GRIB_API_LIB
> gcc -m32 -o foo foo.c $GRIB_API_INCLUDE $GRIB_API_LIB
> gcc -o foo foo.c $GRIB_API_INCLUDE $GRIB_API_LIB
> use pgf90 > pgf90 -o foo foo.f90 $GRIB_API_INCLUDE $GRIB_API_LIB
editionNumber = 2;
if( indicatorOfParameter == 11 && indicatorOfTypeOfLevel == 105)
{
productDefinitionTemplateNumber = 1;
typeOfFirstFixedSurface = 103;
scaleFactorOfFirstFixedSurface = 0;
scaledValueOfFirstFixedSurface = 2;
}
The only required package for a standard installation is jasper which enables the jpeg2000 packing/unpacking algorithm. It is possible to build grib_api without jasper, by using the --disable-jpeg configure option, but to install a fully functional library, its download is recommended.
> gunzip grib_api-X.X.X.tar.gz > tar xf grib_api-X.X.X.tar
> mkdir grib_api_dir
> cd grib_api-X.X.X > ./configure --prefix=grib_api_dir
> make ... > make check ... > make install ...
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 copy a message in memory 00010 ! 00011 ! 00012 ! Author: Anne Fouilloux 00013 ! 00014 ! 00015 program copy 00016 use grib_api 00017 implicit none 00018 integer :: err, centre 00019 integer(kind=kindOfSize) :: byte_size 00020 integer :: infile,outfile 00021 integer :: igrib_in,iret 00022 integer :: igrib_out 00023 character(len=1), dimension(:), allocatable :: message 00024 00025 00026 call grib_open_file(infile,'../../data/constant_field.grib1','r') 00027 call grib_open_file(outfile,'out.grib1','w') 00028 00029 ! a new grib message is loaded from file 00030 ! igrib is the grib id to be used in subsequent calls 00031 call grib_new_from_file(infile,igrib_in) 00032 00033 call grib_get_message_size(igrib_in, byte_size) 00034 allocate(message(byte_size), stat=err) 00035 00036 call grib_copy_message(igrib_in,message) 00037 00038 call grib_new_from_message(igrib_out, message) 00039 00040 centre=80 00041 call grib_set(igrib_out,"centre",centre) 00042 00043 ! write messages to a file 00044 call grib_write(igrib_out,outfile) 00045 00046 call grib_release(igrib_out) 00047 00048 call grib_release(igrib_in) 00049 00050 call grib_close_file(infile) 00051 call grib_close_file(outfile) 00052 deallocate(message) 00053 00054 end program copy