      PROGRAM READVALUES


      
      INTEGER   IFILE 
      INTEGER   GRIBID 
      INTEGER   GRIBID2 
      INTEGER   VALUE 
      INTEGER   NARGS
      INTEGER   IRET
      INTEGER   IFILEN
      INTEGER   VALUESLEN
      INTEGER   J
      INTEGER   JCOUNT

      INTEGER  GRIB_OPEN_FILE
      EXTERNAL GRIB_OPEN_FILE
      INTEGER  GRIB_CLONE
      EXTERNAL  GRIB_CLONE
      EXTERNAL GRIB_PRINT
      INTEGER GRIB_PRINT
      INTEGER GRIB_CLOSE_FILE
      INTEGER GRIB_NEW_FROM_FILE
      INTEGER GRIB_NEW_FROM_TEMPLATE
      INTEGER GRIB_SET_REAL8_ARRAY
      INTEGER GRIB_SET_REAL4_ARRAY
      INTEGER GRIB_SET_INT_ARRAY
      INTEGER GRIB_GET_INT
      INTEGER GRIB_SET_INT
      INTEGER GRIB_GET_REAL8_ARRAY
      INTEGER GRIB_RELEASE

      EXTERNAL GRIB_CLOSE_FILE
      EXTERNAL GRIB_NEW_FROM_FILE
      EXTERNAL GRIB_NEW_FROM_TEMPLATE
      EXTERNAL GRIB_GET_INT
      EXTERNAL GRIB_SET_INT
      EXTERNAL GRIB_GET_REAL8_ARRAY
      EXTERNAL GRIB_SET_REAL8_ARRAY
      EXTERNAL GRIB_SET_REAL4_ARRAY
      EXTERNAL GRIB_SET_INT_ARRAY
      EXTERNAL GRIB_RELEASE
      INTEGER PVSIZE


      INTEGER GRIB_WRITE_TO
      EXTERNAL GRIB_WRITE_TO

      INTEGER  GRIB_GET_ERROR_STRING
      EXTERNAL GRIB_GET_ERROR_STRING


      INTEGER PLSIZE
      CHARACTER*2 INPUT
      CHARACTER*256 YDATAFILE
      CHARACTER*256 MPARAM
      CHARACTER*256 MERROR
      REAL*8     VALUES(2048)
      INTEGER    PL(32)
      REAL*8     PVSS(20)
      REAL*8     VMAX
      REAL*8     VMIN
      IFILE = 0
      IRET = 5
      GRIBID = 0;
      PVSIZE =20
      PLSIZE =32
      NARGS = IARGC()

      VALUESLEN = 2048

      CALL GETARG(1,INPUT)

      IF(INPUT.EQ.'-i') THEN
         CALL GETARG(2,YDATAFILE)
      ELSE
         print*,'Usage: readvalues -i inputfile' 
         STOP
      END IF

      JCOUNT = 0

      IFILEN = INDEX(YDATAFILE,' ') - 1

      IRET =  GRIB_OPEN_FILE(IFILE, YDATAFILE (1: IFILEN),"r")
      WRITE (*,*) "Opened returned ", IRET, IFILE

      JCOUNT = 0

   50 CONTINUE
      JCOUNT = JCOUNT + 1
      IRET = GRIB_NEW_FROM_TEMPLATE(GRIBID, "gg_ml")

C      IRET = GRIB_NEW_FROM_FILE(IFILE,GRIBID)
C      WRITE (*,*) "gribid ret ",IRET, IFILE

      IF ( IRET .LT. 0 ) THEN
        WRITE (*, *) 'total ', JCOUNT ,' ret = ', IRET
C        IRET = GRIB_CLOSE_FILE(IFILE)
        STOP 'no more gribs'
      ENDIF
      IF ( JCOUNT .GT. 1 ) THEN
        WRITE (*, *) 'total ', JCOUNT ,' ret = ', IRET
        IRET = GRIB_CLOSE_FILE(IFILE)
        STOP 'no more gribs'
      ENDIF
      IRET = GRIB_GET_INT(GRIBID,"ni",VALUE)
      WRITE (*,*) "NI ", VALUE , " ret ",IRET

      IRET = GRIB_GET_ERROR_STRING(IRET,MERROR)
      WRITE (*,*) "ERROR ", MERROR
      

      DO J = 1, PVSIZE 
        PVSS(J) = J
      ENDDO


      IRET = GRIB_SET_REAL8_ARRAY(GRIBID,"pv",PVSS, PVSIZE)
      IRET = GRIB_GET_ERROR_STRING(IRET,MERROR)

      DO J = 1, PLSIZE 
        PL(J) = 64 
      ENDDO

      IRET = GRIB_SET_INT_ARRAY(GRIBID,"pl",PL, PLSIZE)
      IRET = GRIB_GET_ERROR_STRING(IRET,MERROR)
      VALUE = 1
      IRET = GRIB_SET_INT(GRIBID,"marsClass",VALUE)
      WRITE (*,*) "Class ", VALUE , " ret ",IRET

      VMAX = VALUES(1)
      VMIN = VMAX
 
      DO J = 1, VALUESLEN 
       VALUES(J) = J + 0.5
      ENDDO

      IRET = GRIB_SET_REAL8_ARRAY(GRIBID,"values",VALUES, VALUESLEN)
      IRET = GRIB_GET_ERROR_STRING(IRET,MERROR)
      WRITE (*,*) "PACK ERROR here", MERROR

      IRET = GRIB_PRINT(GRIBID,"level")
      IRET = GRIB_PRINT(GRIBID,"topLevel")
      IRET = GRIB_PRINT(GRIBID,"bottomLevel")
      IRET = GRIB_PRINT(GRIBID,"GRIB")
      IRET = GRIB_WRITE_TO(GRIBID,"test.grib")
      IRET = GRIB_RELEASE(GRIBID)
      GO TO 50
   

      END
