!$$$  sub  PROGRAM DOCUMENTATION BLOCK
!                .      .    .                                       .
! sub  PROGRAM:  gribst      UNPACK GLOBAL Grib SST
!   PRGMMR: GERALD           ORG: W/NMC21     DATE: 95-08-01
!
! ABSTRACT: DECODES GLOBAL AVN, FNL, OR MRF GRIB GRIDDED FIELDS.
!
! PROGRAM HISTORY LOG:
!   95-08-01  GERALD
!   96-07-23  PETERS - UPDATED FOR USE WITH ETA MODEL, GETS 
!                      REYNOLDS 1X1 OISST 
!mp
!mp	Changed 99-02-22 by Pyle to use a normal version of GETGB
!
!	CHANGED 01-02-16 by PYLE to use 0.5 X 0.5 analysis
!mp
!
!
! USAGE:
!   INPUT FILES:
!     DDNAME1  - GENERIC NAMES & USAGE
!     FTNNF001 - START IN COL 7
!              - BUT TAB CONTINUATIONS TO LINE UP LIKE THIS
!     FXN      - LINE UP DASHES WHEN DESCRIBING FILES
!     TAPENN   - CRAY TYPE DESIGNATION
!     UNITNN   - CRAY TYPE DESIGNATION
!     PARM     - IF PARM FIELD IS USED, DESCRIBE HERE
!
!   OUTPUT FILES:  (INCLUDING SCRATCH FILES)
!     FTMMF001 - NAMES & USAGE AS ABOVE IN THE INPUT SECTION
!     FT06F001 - INCLUDE IF ANY PRINTOUT
!
!   SUBPROGRAMS CALLED: (LIST ALL CALLED FROM ANYWHERE IN CODES)
!     UNIQUE:    - ROUTINES THAT ACCOMPANY SOURCE FOR COMPILE
!     LIBRARY:
!       COMMON   - LIST COMMON LIBRARY ROUTINES, E.G., CONSOL
!       W3LIB    -
!       W4LIB    - DELETE THE CORRESPONDING LINE OR LINES
!       GRAPHICS - IF LIBRARY IS UNNEEDED
!
!   EXIT STATES:
!     COND =   0 - SUCCESSFUL RUN
!          =NNNN - TROUBLE OR SPECIAL FLAG - SPECIFY NATURE
!
! REMARKS: LIST CAVEATS, OTHER HELPFUL HINTS OR INFORMATION
!
! ATTRIBUTES:
!   LANGUAGE: INDICATE EXTENSIONS, COMPILER OPTIONS
!   MACHINE:  CRAY
!
!$$$
!
      subroutine gribst(insst,indxst,gsst,ierr,nd)
!
      Parameter(jf=720*360)
      INTEGER PDS,GDS,GRID
      INTEGER JPDS(200),JGDS(200),IGRD(5,3)
      INTEGER KPDS(200),KGDS(200)
      integer::nd
      character(len=3)::cnd
      character(len=80)::fname1
      REAl FLD(jf),out(720,360)
      dimension gsst(721,360)
      logical lb(jf)
      equivalence(out(1,1),fld(1))
!
!
!                sst
      DATA IGRD/ 11, 34,  2, 52, 11,  &
                  1,105,102,105,105,  &
                  0, 10,  0,  2,  2/
!
!            INPUT UNITS FOR DECODING GRIB FILE
!
          LUGB=INSST
!mp          LUGI=INDXST
	LUGI=0
          ierr = 0
          j = 0
!
!........    DECODE THE FIELDS
!
        DO 30 GRID = 1,1
!
          DO 10 PDS=1,200
          JPDS(PDS) = -1
   10      CONTINUE
!
             DO 20 GDS = 1,200
           JGDS(GDS) = -1
   20     CONTINUE
!
!........   GET SELECT FIELDS
!
           jPDS(5) = IGRD(GRID,1)
           jPDS(6) = IGRD(GRID,2)
           jPDS(7) = IGRD(GRID,3)
!
!             print *,'call getgb with unit #s ', LUGB,LUGI
         if(nd.eq.0)then
	   CALL BAOPENR(LUGB,'data_in/init/hires_sst',IRETBA)
         else
	   write(cnd,'(i3.3)')nd
	   fname1='data_in/init/hires_sst'//cnd
	   call baopen(lugb,fname1,iretba)
         endif
 	write(6,*) 'back from baopen, IRET= ', IRETBA
           CALL GETGB(LUGB,LUGI,JF,j,JPDS,JGDS,  &
                                KF,K,KPDS,KGDS,LB,Fld,IRET)
!
          IF(IRET.NE.0) THEN
            ierr = 1
            WRITE(6,60)IRET
            GO TO 999
          ENDIF
   60  FORMAT(1X,' IRET =',I5)
!
!..   flip grid to PT(1,1) =(0e,-90.0)
!
	write(6,*) 'flipping the data'
              do  jj= 1,360
                do  kk= 1,720
                 gsst(kk,360-jj+1) = out(kk,jj)
                end do
              end do
!
!...   add greenich to right side of grid
!
             do jj = 1,360
               gsst(721,jj) = gsst(1,jj)
             end do
!
!mp            WRITE(6,61)KPDS,KF,KGDS
!mp   61  FORMAT(2(/,2X,'PDS=',13I7),2(/,2X,' GDS=',11I7 ))
	write(6,*) 'KPDS= ', (KPDS(I),I=1,12)
	write(6,*) 'KGDS= ', (KGDS(I),I=1,25)
!
   30        CONTINUE
!
  999           CONTINUE
	write(6,*) 'leaving GRIBST'
             return
             END
