      SUBROUTINE CALSF(STDLAT,HEMIS,SCLFAC,ERROR)
C**********************************************************************
C
C   NAME- CALSF
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/14/96   PROGRAMMER- ALEX POZDNYAK, FDC
C      FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C   VERSION- 3.1    DATE- 09/05/97   PROGRAMMER- LEAH WILSON, HSTX 
C      REMOVE DEGRAD AS A FUNCTION CALL, AND USE IT FROM /RMPARM/.
C
C   DESCRIPTION- CALSF IS USED TO CALCULATE THE SCALING FACTOR (S)
C                OF THE MAP BASED ON THE STANDARD LATITUDE (SL) AND
C                HEMISPHERE (H) USING THE FOLLOWING EQUATION
C                (HOKE, 1981):
C
C                     S = 1 + H SIN (SL)/ 1 + SIN 60
C
C                THE 60 (DEGREES) IS SET UP IN THE INCLUDE FILE MAPCST
C                AS A CONSTANT. A CHECK IS MADE TO INSURE THAT IT HAS
C                NOT BEEN ACCIDENTALLY SET TO A VALUE LESS THAN OR EQUAL
C                TO -90.0 OR GREATER THAN +90.0. IF IT HAS BEEN SET
C                ERRONEOUSLY, CALSF$ IS CALLED TO WRITE A MESSAGE TO
C                THE KIDS MESSAGE LOG AND SET THE ERROR RETURN
C                CODE.  THE STANDARD LATITUDE IS ALSO CHECKED TO INSURE
C                ITS VALUE IS GREATER THAN OR EQUAL TO - 90.0 AND LESS
C                THAN OR EQUAL TO +90.0. CALSF$ IS CALLED IF THESE
C                CONDITIONS ARE NOT MET.
C
C                REFERENCE: HOKE, HAYES, AND RENNIGER, MARCH 1981,
C                           "MAP PROJECTIONS AND GRID SYSTEMS FOR
C                           METEOROLOGICAL APPLICATIONS", PG. 28
C
C   CALLING SEQUENCE PARAMETERS-
C
C       NAME   TYPE  I/O              DESCRIPTION
C      ------  ----  ---  -------------------------------------------
C      STDLAT  REAL   I   THE LATITUDE AT WHICH THE PROJECTION PLANE
C                         IS DRAWN. (STANDARD LATITUDE) (DEGREES)
C
C      HEMIS   INT    I   THE HEMISPHERE VALUE. +1 FOR THE NORTHERN
C                      HEMISPHERE, -1 FOR THE SOUTHERN HEMISPHERE.
C
C      SCLFAC  REAL   O   THE COMPUTED SCALING FACTOR OF THE MAP.
C
C      ERROR   INT   I/O  THE ERROR RETURN CODE.
C                              0 - SUCCESSFUL COMPLETION.
C                             99 - FATAL ERROR.
C
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'MAPCST'
      INCLUDE 'RMPARM'
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
C
C   SUBPROGRAMS CALLED- CALSF$
C
C   INTRINSIC FUNCTIONS-
C
      INTRINSIC SIN
C
C   EXIT STATES-
C     99 - FATAL ERROR: THE SCALING FACTOR CONSTANT MAPCST:SFCONS WAS
C                       SET TO -90.0 CAUSING A DIVISION BY ZERO ERROR.
C
C   COMMENTS-
C      1. HEMIS HAS THE VALUE -1 OR 1 (COMPUTED IN A HIGHER ROUTINE).
C      2. HEMIS AND STDLAT HAVE THE SAME SIGNS.
C      3. STDLAT IS BETWEEN -90.0 AND 90.0
C
C**********************************************************************
C
      REAL     STDLAT, SCLFAC
      INTEGER  HEMIS, ERROR
C
      REAL DIVISR
C
C     INITIALIZE OUTPUT VARIABLES
C
      SCLFAC = 0.0
      ERROR = 0
C
C     INITIALIZE LOCAL VARIABLES
C
      DIVISR = 0.0
C
C     PERFORM ERROR CHECKING FOR DIVISION BY ZERO.
C
      IF ((SFCONA .LE. -90.0) .OR. (SFCONA .GT. 90.0)) THEN
C
C         HANDLE ERROR.
C
          CALL CALSF$ (110, SFCONA, ERROR)
          GOTO 20
C
      ELSE IF ((STDLAT .LT. -90.0) .OR. (STDLAT .GT. 90.0)) THEN
C
C         HANDLE ERROR.
C
          CALL CALSF$ (120, STDLAT, ERROR)
          GOTO 20
C
      ELSE
C
C         CALCULATE DIVISOR
C
          DIVISR = 1 + (SIN (DEGRAD*SFCONA))
C
C         CALCULATE SCALING FACTOR
C
          SCLFAC = (1 + (HEMIS * SIN( DEGRAD * STDLAT))) / DIVISR
C
C         GO TO GOOD RETURN SECTION
C
          GOTO 20
C
      END IF
C
   20 RETURN
      END
C**********************************************************************
      SUBROUTINE CALSF$ (MSGCOD,ARG,STAT)
C**********************************************************************
C   NAME- CALSF$
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY    
C
C   VERSION- 1.0  DATE- 05/06/91   PROGRAMMER- JEFFREY R. JONES (SMSRC)
C   VERSION- 3.0  DATE- 10/09/97   PROGRAMMER- LEAH WILSON (HSTX)
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE CALSF.
C   VERSION- 3.0  CONVERT FROM MMLF TO KIDS.
C
C   FILES-
C     DSN                    I/O   UNIT   FUNCTIONAL DESCRIPTION
C     ---                    ---  ------  -----------------------
C     ...KIDS                 O    KUNIT  KIDS MESSAGE FILE.  
C
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE               TYPE  I/O  FUNCTION
C     --------               ----  ---  --------
C     MSGCOD                  INT   I   MESSAGE CODE USED TO
C                                       IDENTITIFY THE ERROR.
C
C     ARG                     REAL  I   THE ERRONEOUS VALUE.
C
C     STAT                    INT   O   STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O
C
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C  SET ERINFO:SUBNAM
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   SUBPROGRAMS CALLED- KIDS 
C
C**********************************************************************
C
      INTEGER   MSGCOD,STAT
      REAL      ARG
      CHARACTER*4   C4TEMP
      CHARACTER*7   C7TEMP
      CHARACTER*100 KMSG
C
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
C
C  CHECK MESSAGE CODE AND WRITE ERROR MESSAGE
C
      IF (MSGCOD .EQ. 110) THEN
        KMSG=' * FATAL ERROR * Invalid scaling factor constant: xxxxxxx'
        WRITE(C7TEMP,'(F7.2)') ARG
        KMSG(51:57) = C7TEMP
        STAT = 99
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE IF (MSGCOD .EQ. 120) THEN
        KMSG=' * FATAL ERROR * Invalid standard latitude of xxxxxxx'
        WRITE(C7TEMP,'(F7.2)') ARG
        KMSG(47:53) = C7TEMP
        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
