00001 ! Copyright 2005-2016 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