C Copyright(c) 1997, Space Science and Engineering Center, UW-Madison
C Refer to "McIDAS Software Acquisition and Distribution Policies"
C in the file  mcidas/data/license.txt

C *** $Id: suok.f,v 1.24 1998/10/21 14:49:56 daves Rel $ ***

C--- SUOK CHECKS FOR THE EXISTANCE OF THE SPECIFIED STRETCH TABLE
C--- AND CHECKS THAT THE BAND, UNIT AND TYPE MATCH BETWEEN THE TABLE
C--- AND THE DISPLAYED IMAGE.
C--- FUNCTION SUOK (BAND, TYPE, CNAME)
C       SUOK=  0 : OK TO CONTINUE WITH STRETCH
C       SUOK=  -1: INCONSISTENCY FOUND, DISCONTINUE PROCESSING
C
C       BAND - BAND NUMBER OF IMAGE TO BE DISPLAYED
C       TYPE - CALIBRATION TYPE
C       CNAME - NAME OF STRETCH TABLE TO CHECK

        INTEGER FUNCTION SUOK(BAND,TYPE,CNAME,SATID)
        INTEGER*4 BAND,LIT,SATID
        INTEGER CTIND, CUIND, BIND
        CHARACTER*4 TYPE,CLIT
        CHARACTER*(*) CNAME
        CHARACTER*12 CTEMP

        INTEGER STABLE(400),IIN(3),IOUT(100)

        DATA CTIND/1/, CUIND/2/, NBIND/4/,BIND/5/

        CTEMP=CNAME
C--- DEFAULT RETURN VALUE IS 0 IF EVERYTHING IS OK
        SUOK = 0
C--- ADD THE '.ST' EXTENSION TO THE NAME
        IND=INDEX(CTEMP,' ')
        IF (INDEX(CTEMP,'.').EQ.0) CTEMP(IND:IND+2)='.ST'

C--- CHECK FOR FILE EXISTANCE RETURN BAD STAT IF NOT FOUND
        IF (LWFILE(CTEMP) .EQ. 0) THEN
           SUOK = -1
           CALL SDEST('Specified stretch table does not exist ',0)
        ELSE
          STAT= LWI(CTEMP, 1, 400, STABLE)

C--- CHECK FOR BREAKPOINTS IN TABLE RETURN BAD STAT IF NONE FOUND
           IF (IFTOK(CLIT(STABLE(NBIND))).LT.1) THEN
              CALL SDEST('Specified stretch table contains'//
     &         ' no breakpoints',0)
              SUOK = -1
              RETURN
           ENDIF

C--- CHECK THAT DISPLAYED BAND MATCHES BAND SPECIFIED IN TABLE
           IF (IFTOK(CLIT(STABLE(BIND))).NE.-1) THEN
              IF (IFTOK(CLIT(STABLE(BIND))).NE.BAND) THEN
                 SUOK = 0
                 CALL SDEST('NOTE : Displayed band not equal to '//
     &                   'band specified in stretch table',0)
              ENDIF
           ENDIF

C--- CHECK THAT CALIBRATION TYPE MATCHES TYPE STORED IN TABLE
           IF (CLIT(STABLE(CTIND)).NE.TYPE) THEN
              SUOK = 0
              CALL SDEST('NOTE : Displayed image is not of the same ',0)
              CALL SDEST('       calibration type as specified in '//
     &         'stetch table',0)
           ENDIF

C--- CHECK THAT UNIT STORED IN TABLE IS AVAILABLE UNDER THIS CALIBRATION
           KBSTAT=KBPREP(3,LIT(TYPE))
           IF (KBSTAT.NE.0) THEN
              CALL EDEST('Unsupported calibration type ',0)
              SUOK = 0
           ENDIF
           IIN(1) = BAND
           IIN(2) = SATID
           IIN(3) = LIT(TYPE)
           KBVAL = KB3OPT('INFO',IIN,IOUT)
           DO 100 I=1,IOUT(1)
              IF (CLIT(IOUT(I+1)).EQ.CLIT(STABLE(CUIND))) FOUND= 1
              IF (CLIT(IOUT(I+1)).EQ.'PROD') FOUND=1
100        CONTINUE
           IF (FOUND.NE.1) THEN
              SUOK = -1
              CALL EDEST('Stretch table is in units which are',0)
              CALL EDEST('not available for this calibration',0)
           ENDIF
        ENDIF
        RETURN
        END
