Page tree

GRIB-API support has now been discontinued.

Please upgrade to ecCodes

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

How access a grib file through and index.

00001 ! Copyright 2005-2007 ECMWF
00002 ! 
00003 ! Licensed under the GNU Lesser General Public License which
00004 ! incorporates the terms and conditions of version 3 of the GNU
00005 ! General Public License.
00006 ! See LICENSE and gpl-3.0.txt for details.
00007 !
00008 !
00009 !  Description: How to create and use and index to access messages from a file.
00010 !               Demo also loading and writing an index from a file.
00011 !
00012 !  Author: Enrico Fucile 
00013 !
00014 !
00015 program index
00016   use grib_api
00017   implicit none
00018   
00019   integer              :: iret
00020   character(len = 256) :: error
00021   integer,dimension(:),allocatable :: step,level,number
00022   character(len=20),dimension(:),allocatable :: shortName
00023   integer                          :: ostep,olevel,onumber
00024   character(len=20)                :: oshortName
00025   integer                          :: shortNameSize,numberSize,levelSize,stepSize
00026   integer              :: i,j,k,l
00027   integer              :: idx,igrib,count
00028   logical              :: index_exists
00029   character(len=10)    :: index_file='index.idx'
00030 
00031   ! uncomment following line to load index from file
00032   !call grib_index_read(idx,index_file)
00033 
00034   ! create an index from a grib file using some keys
00035   call grib_index_create(idx,'../../data/index.grib','shortName,number,level,step')
00036 
00037   ! get the number of distinct values of shortName in the index
00038   call grib_index_get_size(idx,'shortName',shortNameSize)
00039   ! allocate the array to contain the list of distinct shortName
00040   allocate(shortName(shortNameSize))
00041   ! get the list of distinct shortName from the index
00042   call grib_index_get(idx,'shortName',shortName)
00043   write(*,'(a,i3)') 'shortNameSize=',shortNameSize
00044 
00045   ! get the number of distinct values of number in the index
00046   call grib_index_get_size(idx,'number',numberSize)
00047   ! allocate the array to contain the list of distinct numbers
00048   allocate(number(numberSize))
00049   ! get the list of distinct numbers from the index
00050   call grib_index_get(idx,'number',number)
00051   write(*,'(a,i3)') 'numberSize=',numberSize
00052 
00053   ! get the number of distinct values of level in the index
00054   call grib_index_get_size(idx,'level',levelSize)
00055   ! allocate the array to contain the list of distinct levels
00056   allocate(level(levelSize))
00057   ! get the list of distinct levels from the index
00058   call grib_index_get(idx,'level',level)
00059   write(*,'(a,i3)') 'levelSize=',levelSize
00060 
00061   ! get the number of distinct values of step in the index
00062   call grib_index_get_size(idx,'step',stepSize)
00063   ! allocate the array to contain the list of distinct steps
00064   allocate(step(stepSize))
00065   ! get the list of distinct steps from the index
00066   call grib_index_get(idx,'step',step)
00067   write(*,'(a,i3)') 'stepSize=',stepSize
00068 
00069   count=0
00070   do l=1,stepSize ! loop on step
00071     ! select step=step(l)
00072     call grib_index_select(idx,'step',step(l))
00073 
00074     do j=1,numberSize ! loop on number
00075       ! select number=number(j)
00076       call grib_index_select(idx,'number',number(j))
00077 
00078       do k=1,levelSize ! loop on level
00079         ! select level=level(k)
00080         call grib_index_select(idx,'level',level(k))
00081 
00082         do i=1,shortNameSize ! loop on shortName
00083           ! select shortName=shortName(i)
00084           call grib_index_select(idx,'shortName',shortName(i))
00085 
00086           call grib_new_from_index(idx,igrib, iret)
00087           do while (iret /= GRIB_END_OF_INDEX)
00088              count=count+1
00089              call grib_get(igrib,'shortName',oshortName)
00090              call grib_get(igrib,'number',onumber)
00091              call grib_get(igrib,'level',olevel)
00092              call grib_get(igrib,'step',ostep)
00093              write(*,'(A,A,A,i3,A,i4,A,i3)') 'shortName=',trim(oshortName),&
00094                      '   number=',onumber,&
00095                      '   level=' ,olevel, &
00096                      '   step='  ,ostep
00097   
00098              call grib_release(igrib)
00099              call grib_new_from_index(idx,igrib, iret)
00100           end do
00101           call grib_release(igrib)
00102 
00103         end do ! loop on step
00104       end do ! loop on level
00105     end do ! loop on number
00106   end do ! loop on shortName
00107   write(*,'(i4,a)') count,' messages selected'
00108 
00109   ! save the index to a file for later reuse
00110   call grib_index_write(idx,index_file)
00111 
00112   call grib_index_release(idx)
00113 
00114 end program index
  • No labels