      SUBROUTINE IMGSCL (LAT,STDLAT,HEMIS,IMGSCA,ERROR)
C**********************************************************************
C
C   NAME- IMGSCL
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY    
C
C   VERSION- 1.0  DATE- 08/22/91   PROGRAMMER- JEFFREY R. JONES (SMSRC)
C   VERSION- 3.0  DATE- 05/21/96   PROGRAMMER- ALEX POZDNYAK (FDC)     
C     FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C     INTEGER, ALL R*4 TO REAL. 
C   VERSION- 3.1  DATE- 09/05/97   PROGRAMMER- LEAH WILSON, HSTX  
C     USE DEGRAD AS A CONSTANT INSTEAD OF AS A FUNCTION.
C
C   DESCRIPTION- THIS ROUTINE CALCULATES THE RATIO, AT A POINT, OF
C                DIFFERENTIAL DISTANCE ON AN IMAGE SURFACE OR IMAGE
C                PLANE TO DIFFERENTIAL DISTANCE ON THE SURFACE OF THE
C                EARTH, OTHERWISE KNOWN AS THE IMAGE SCALE (HOKE, 1981).
C                THE EQUATION USED IS AS FOLLOWS:
C
C                      SCALE = 1 + H SIN (SL) / 1 + H SIN (L)
C
C                WHERE H IS THE HEMISPHERE, SL IS THE STANDARD LATITUDE,
C                AND L IS THE LATITUDE OF THE POINT. TO PREVENT A
C                DIVISION BY ZERO ERROR, THE LATITUDE AND HEMISPHERE ARE
C                CHECKED TO ENSURE BOTH ARE IN THE SAME HEMISPHERE. IF
C                THEY ARE NOT, IMGSC$ IS CALLED TO WRITE A MESSAGE TO
C                THE KIDS MESSAGE LOG AND SET THE ERROR RETURN
C                CODE.
C
C                REFERENCE: HOKE, HAYES, AND RENNIGER, MARCH 1981,
C                           "MAP PROJECTIONS AND GRID SYSTEMS FOR
C                           METEOROLOGICAL APPLICATIONS", PG. 29
C
C   CALLING SEQUENCE PARAMETERS-
C
C       NAME   TYPE  I/O                  DESCRIPTION
C      ------  ----  ---  --------------------------------------------
C      LAT     REAL   I   LATITUDE OF POINT AT WHICH IMAGE SCALE IS TO
C                         BE COMPUTED. (DEGREES)
C
C      STDLAT  REAL   I   LATITUDE AT WHICH PROJECTION PLANE IS DRAWN.
C                         (STANDARD LATITUDE) (DEGREES)
C
C      HEMIS   INT    I   1 FOR NORTHERN HEMISPHERE, -1 FOR SOUTHERN
C                         HEMISPHERE.
C
C      IMGSCA  REAL   O   THE RESULTING IMAGE SCALE.
C
C      ERROR   INT   I/O  ERROR CODE.
C                            0 - SUCCESSFUL COMPLETION.
C                           99 - FATAL ERROR.
C
C      *              I   THE LABEL IN THE CALLING ROUTINE THAT HANDLES
C                         ERRORS.
C
C   LOCAL PARAMETERS-
C
C      NAME   TYPE  I/O                  DESCRIPTION
C     ------  ----  ---  --------------------------------------------
C     DIVISR  REAL  N/A  HOLDS THE DIVISOR OF THE EQUATION FOR ERROR
C                        CHECKING.
C     DEGRAD  REAL  N/A  FACTOR REQUIRED TO CONVERT FROM DEGREES
C                        INTO RADIANS
C
C   SUBPROGRAMS CALLED- IMGSC$
C
C   INTRINSIC FUNCTIONS- SIN
C
      INTRINSIC SIN
C
C   EXIT STATES-
C     99 - FATAL ERROR: THE INPUT LATITUDE IS NOT IN THE HEMISPHERE
C                       SPECIFIED BY THE VARIABLE HEMIS.
C
C   COMMENTS-
C      1.  LAT IS IN THE RANGE OF -90.0 AND +90.0 AND HAS BEEN CHECKED
C          IN A HIGHER ROUTINE.
C      2.  STDLAT IS IN THE RANGE OF -90.0 AND +90.0 AND HAS BEEN
C          CHECKED IN A HIGHER ROUTINE.
C      3.  HEMIS HAS THE VALUE OF EITHER -1 OR +1 AFTER BEING COMPUTED
C          IN A HIGHER ROUTINE.
C
C**********************************************************************
C
      REAL     LAT, STDLAT, IMGSCA, DEGRAD
      INTEGER  HEMIS, ERROR
C
      REAL     DIVISR
      DEGRAD = 0.0174532925
C
C     INITIALIZE OUTPUT VARIABLES
C
      IMGSCA = 0
C
C     INITIALIZE LOCAL VARIABLES
C
      DIVISR = 0
C
C     MAKE SURE LATITUDE AND HEMISPHERE AGREE.
C
      IF (((LAT .LT. 0.0) .AND. (HEMIS .EQ. 1)).OR.
     1   ((LAT .GT. 0.0) .AND. (HEMIS .EQ. -1))) THEN
C
C          HANDLE ERROR.
C
           CALL IMGSC$ (310, LAT, HEMIS, ERROR)
C
C     OTHERWISE CONTINUE WITH CALCULATION.
C
      ELSE
C
C          COMPUTE DIVISOR OF EQUATION
C
           DIVISR = 1 + (HEMIS * SIN( DEGRAD * LAT ))
C
C          COMPUTE IMAGE SCALE.
C
           IMGSCA = (1+(HEMIS*(SIN(DEGRAD*STDLAT)))) / DIVISR
C
      ENDIF
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE IMGSC$(MSGCOD,LAT,HEMIS,STAT)
C**********************************************************************
C   NAME- IMGSC$
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY    
C
C   VERSION- 1.0  DATE- 05/07/91   PROGRAMMER- JEFF R. JONES (SMSRC)
C   VERSION- 3.0  DATE- 10/07/97   PROGRAMMER- LEAH WILSON (HSTX)
C
C   FILES-
C     DSN                    I/O   UNIT   FUNCTIONAL DESCRIPTION
C     ---                    ---  ------  -----------------------
C     ...KIDS                 O    KUNIT   KRAY INTERACTIVE DOCUMENTATION
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE IMGSCL.
C   VERSION- 3.0 WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C
C   CALLING SEQUENCE PARAMETERS-
C
C      VARIABLE               TYPE  I/O  FUNCTION
C      --------               ----  ---  --------
C      MSGCOD                  INT   I   MESSAGE CODE USED TO
C                                        IDENTIFY THE ERROR.
C
C      LAT                     REAL  I   LATITUDE OF THE LOCATION.
C
C      HEMIS                   INT   I   HEMISPHERE OF THE LOCATION.
C
C      STAT                    INT   O   STATUS CODE IF AN ERROR
C                                        OCCURS DURING I/O.
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C      VARIABLE                TYPE    FUNCTION
C      --------                ----    --------
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C     C7TEMP                  CH*7    HOLDING BUFFER FOR WRITING TO KIDS
C
C   SUBROUTINES CALLED- KIDS
C
C**********************************************************************
C
      INTEGER   MSGCOD,STAT,HEMIS
      REAL      LAT
      CHARACTER*4   C4TEMP
      CHARACTER*7   C7TEMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
C  CHECK MESSAGE CODE AND WRITE ERROR MESSAGE.
C
      IF (MSGCOD .EQ. 310) THEN
        KMSG(1:50)=' * FATAL ERROR * The latitude (       ) does not a'
        KMSG(51:100)='gree with the hemisphere (    ).'
        WRITE(C7TEMP,'(F7.2)') LAT    
        KMSG(32:38) = C7TEMP
        WRITE(C4TEMP,'(I4)') HEMIS  
        KMSG(77:80) = C4TEMP
        STAT = 99
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END
