      SUBROUTINE INGCM8 (NUSP,SG,SH,SECPU,KFINAL,AVGPS,LEVOZ, 
     1                   NILEV,NLEVS,NTRAC,ITRVAR,XSRCRAT,XSRCRM1,
     2                   XPP,XP0,XPM,TFICXM) 

C     * NOV 23/95 - M.LAZARE.  - FIX ERROR: "QSRCRM1" NOW DEFINED INSTEAD
C     *                          OF TYPO "QSRCM1". THIS MEANT THAT 
C     *                          PREVIOUSLY, "QSRCRM1" WAS BEING DEFINED
C     *                          AS ZERO AT FIRST STEP FOLLOWING A 
C     *                          RESTART, CAUSING PROBLEMS FOR MOISTURE
C     *                          CONSERVATION WITH MOIST=4HQHYB (NOT
C     *                          MOIST=4H   Q). WE HAVE NOT CHANGED
C     *                          INGCM7 SINCE IS FROZEN VERSION GCM10/MAM.     
C     * MAY 23/95 - M.LAZARE.  - INCLUDE OPTION "MYRSSTI" VIA COMMON
C     *                          BLOCK "KEEPTYM", TO DO AMIP-LIKE RUNS.
C     *                          ADD IN NEW 'IYEAR' PARAMETER READ FROM
C     *                          CARDS.
C     *                          SIMILARILY OPTION "ISAVDTS" TO SAVE
C     *                          GCM OUTPUT IN "yyyymmddhh" FORMAT.
C     * APR 26/95 - F.MAJAESS  - PREVIOUS VERSION INGCM7.
C
C     * READS CONTROLS FOR HYBRID GCM FROM CARDS AND THE START FILE NUSP. 
C     * PRINTS MODEL CONTROLS AND PARAMETERS. 
C     * FOR USE WITH MODEL VERSIONS GCM6U, GCM11 OR LATER WHICH SUPPORT
C     * AMIP-LIKE RUNS.

      IMPLICIT REAL (A-H,O-Z),
     +INTEGER (I-N)

      PARAMETER (IMX=2000)
  
      LOGICAL IDIV, OK
  
      REAL*4 TARRAY(4)
      REAL F(IMX)
  
      REAL SG(NILEV),SH(NILEV)

      REAL XSRCRAT(1), XSRCRM1(1),    XPP(1)
      REAL     XP0(1),     XPM(1), TFICXM(1)

      INTEGER NFDM(13)

      COMMON /PROCES/ IDIV
      COMMON /INTVLS/ ISSP,ISGG,ISRAD,ISBEG,ISEN,IEPR,ICSW,ICLW,JLATPR
      COMMON /IOPTION/IOCEAN,ICEMOD,IFIXCLD,IHYD,ICON,IRAD,ITRAC
      COMMON /SIZES/  ILG,ILAT,ILEV,LEVS,LRLMT,ICOORD,LAY,PTOIT,MOIST 
      COMMON /TIMES/  DELT,IFDIFF,KSTART,KTOTAL,NEWRUN, 
     1                NDAYS,NSECS,IDAY,LDAY,MDAY,INCD 
      COMMON /PARAM2/ FP,FC,FT,FS,FX,FPS
      COMMON /PARAHD/ DISP,DISC,DIST,DISES,DISX, SCRIT
      COMMON /CONSQ/  QSRCRAT,QSRCRM1,WPP,WP0,WPM,TFICQM
      COMMON /KEEPTIM/IYEAR,IYMDH,MYRSSTI,ISAVDTS

      COMMON /ICOM/ IBUF(8), IDAT(IMX)
C
      DATA MX/IMX/
      DATA NFDM/1,32,60,91,121,152,182,213,244,274,305,335,366/ 
C---------------------------------------------------------------------
C     CALL CLOCK (SECPU) 
      SECPU=DTIME(TARRAY)
      WRITE(6,6005) 
  
C     * READ MODEL PARAMETERS FROM CONTROL CARDS. 
  
C     * KSTEPS  = NUMBER OF TIMESTEPS IN ONE JOB
C     * KFINAL  = FINAL TIMESTEP TO END A RUN 
C     * NEWRUN  = 1 AT THE START OF A NEW RUN 
C     *         = 0 FOR RESTART INSIDE A RUN
C     * LEVS1   = NUMBER OF MOISTURE LEVELS IN THE MODEL
C     *           (CANNOT BE CHANGED AFTER THE INITIAL START-UP AT STEP 0)
C     * INCD    = 0 TO KEEP IDAY FIXED, 1 TO INCREMENT IDAY NORMALLY. 
  
C     * ISSP    = INTERVAL AT WHICH SPECTRAL FORECAST IS SAVED. 
C     * ISGG    = INTERVAL AT WHICH GAUSSIAN GRIDS ARE SAVED. 
C     * ISRAD   = INTERVAL AT WHICH RADIATIVE HEATING RATES AND/OR PHYSICS
C     *           TENDENCIES ARE SAVED. 
C     * ISEN    = INTERVAL AT WHICH ENERGIES ARE SAVED. 
C     * IEPR    = INTERVAL AT WHICH ENERGIES ARE PRINTED. 
C     * ICSW    = INTERVAL AT WHICH SHORTWAVE RADIATION IS RECALCULATED.
C     * ICLW    = INTERVAL AT WHICH LONGWAVE RADIATION IS RECALCULATED. 
C     * ISBEG   = INTERVAL AT WHICH BEG,UFS,VFS ARE SAVED.
C     * JLATPR  = LATITUDE ROW NUMBER TO PRINT OUT SELECTED ARRAYS IN PHYSICS 
C     *           AND DYNAMICS (IF JLATPR=0 NO PRINTOUT IS DONE). 
  
C     * IOCEAN  = 0 WHEN NOT RUNNING WITH INTERACTIVE OCEAN.
C     * ICEMOD  = 0 WHEN NOT RUNNING WITH INTERACTIVE SEA-ICE MODEL.
C     * IFIXCLD = 0 WHEN RUNNING WITH INTERACTIVE CLOUDS. 
C     *         = 1 FIXED-CLOUDS ***** DISABLED!!!. 
C     * IHYD    = 1 THE GCM6 HYDROLOGY FORMULATION IS USED. 
C     *         = 2 THE COMPLEX HYDROLOGY FORMULATION INVOLVING A 
C     *             CANOPY AND 2 SOIL LAYERS IS USED. 
C     * ICON    = 0 WHEN USING THE MASS FLUX DEEP CONVECTIVE SCHEME.
C     *         = 1 WHEN USING THE BETTS-MILLER CONVECTIVE SCHEME.
C     * IRAD    *********DISABLED !!!!!! ***************. 
C     * ITRAC   = 0 WHEN NOT RUNNING WITH A PASSIVE TRACTER.
C     * ITRVAR  = FUNCTIONAL FORM OF TRACER (I.E. '   Q' FOR MIXING RATIO).
  
C     * DELT    = MODEL TIMESTEP IN SECONDS. 
C     * IFDIFF  = 1 FOR FORWARD STEP, 0 FOR LEAPFROG STEP. 
C     * ICOORD  = HYBRID VERTICAL COORDINATE (' ET15',' ETA',' SIG).
C     * MOIST   = FUNCTIONAL FORM OF MOISTURE VARIABLE ('QHYB','   Q',
C     *           'T-TD','RLNQ','SQRT','  TD'). 
C     * IYEAR   = YEAR WHICH MODEL STARTS FROM (I.E. '1979').  

C     * EXAMPLE...
  
C     *  GCM7       72     15264    0   18    1 
C     *             36   36   72    1   18    9   18   36    0 
C     *              0    0    0    1    1    1    0    Q 1979 
C     *    1200.0    0 ET15 QHYB
  
      READ(5,5005,END=901) KSTEPS,KFINAL,NEWRUN,LEVS1,INCD
      READ(5,5010,END=902) ISSP,ISGG,ISRAD,ISEN,IEPR,ICSW,ICLW,ISBEG, 
     1                     JLATPR 
      READ(5,5015,END=903) IOCEAN,ICEMOD,IFIXCLD,IHYD,ICON,IRAD,ITRAC,
     1                     ITRVAR 
      READ(5,5020,END=904) DELT,IFDIFF,ICOORD,MOIST,IYEAR
  
C     * ENSURE THAT AN OCEAN MODEL IS BEING USED IF AN ICE MODEL
C     * IS TO BE USED.
  
      IF (ICEMOD.NE.0 .AND. IOCEAN.EQ.0) CALL XIT('INGCM8',-1)
  
C     * ABORT CONDITIONS FOR DISABLED OPTIONS:  
  
      IF (IFIXCLD.NE.0)                  CALL XIT('INGCM8',-2) 
      IF (IHYD.EQ.0)                     CALL XIT('INGCM8',-3) 
      IF (IRAD.NE.1)                     CALL XIT('INGCM8',-4) 
  
C     * ENSURE THAT ISBEG=KSTEPS IF AN OCEAN MODEL IS BEING USED. 
  
      IF (IOCEAN.NE.0 .AND. ISBEG.NE.KSTEPS) CALL XIT('INGCM8',-5)
  
C     * READ TIMESTEP NUMBER FROM FILE NUSP.
C     * MODEL RUNS KSTEPS TIMESTEPS FROM KSTART TO KTOTAL.
C     * RUN CUTOFF OCCURS AT TIMESTEP KFINAL. 
  
      REWIND NUSP 
      CALL FBUFFIN(NUSP,IBUF,-8,K,LEN)
      KSTART=IBUF(2)
      BACKSPACE NUSP
 
      IF (KSTART.GE.KFINAL) THEN
 
C       * SET THE SWITCH 'G0' IN THE FILE 'Switches' USED TO INDICATE
C       * SUCCESSFUL COMPLETION OF MODEL RUN (USED AT THE SCRIPT LEVEL).
 
        WRITE(6,6008)
        CALL OFAPPND(98,'Switches','FORMATTED',IOS)
        WRITE(98,9000) 'g0 0' 
        CALL XIT('INGCM8',-6)

      ENDIF 
C
      KTOTAL=KSTART+KSTEPS
      IF (KTOTAL.GT.KFINAL) KTOTAL=KFINAL 
C
      IF(ITRAC.GT.0) THEN
        NTRAC=ITRAC
        ITRAC=1
      ELSE
        NTRAC=0
        ITRAC=0
      ENDIF
C-------------------------------------------------------------------- 
C     * FOR AN INITIAL STARTUP, NUSP IS THE OUTPUT FILE FROM
C     * THE SPECTRAL INITIALIZATION PROGRAM WITH KSTART=0.
C     * MOISTURE LEVELS (LEVS) IS SET AT INITIAL START-UP ONLY. 
C     * THE FIRST STEP MUST BE A FORWARD TIMESTEP.
  
      IF (KSTART.LE.0)                             THEN 
          READ(NUSP, END=906) ALABL, AKSTART, AILEV,
     1                        (SG(L),L=1,INT(AILEV)),
     2                        (SH(L),L=1,INT(AILEV)),
     3                        ALAY, PTOIT
C
          READ(NUSP)
          READ(NUSP, END=907) ALABL, ALRLMT, AILG, AILAT, AIDAY, GMT
          READ(NUSP)
          READ(NUSP, END=908) ALABL, AVGPS
C
C         LABL=INT(ALABL)
          KSTART=INT(AKSTART)
          ILEV=INT(AILEV)
          LAY=INT(ALAY)
          LRLMT=INT(ALRLMT)
          ILG=INT(AILG)
          ILAT=INT(AILAT)
          IDAY=INT(AIDAY)
  
          IFDIFF=1
          NEWRUN=1
          LEVS  =LEVS1
          IF (LEVS.GT.ILEV) LEVS=ILEV 
          LDAY  =IDAY 
          MDAY  =IDAY 
          NSECS =INT(GMT*3600.) 
          NDAYS =0
C
C         * INITIALIZE YEAR-MONTH (USED FOR AMIP-LIKE RUNS). 
C
          MM=0 
          DO 50 N=1,12
            IF(IDAY.GE.NFDM(N).AND.IDAY.LT.NFDM(N+1) .AND. MM.EQ.0) THEN
              MM=N 
              IDD=IDAY-NFDM(N)+1
            ENDIF
   50     CONTINUE
          IF(MM.EQ.0)          CALL XIT('INGCM8',-7)
          IHH=INT(GMT)
          IYMDH=1000000*IYEAR + 10000*MM + 100*IDD + IHH
C-------------------------------------------------------------------- 
      ELSE
C         * FOR A RESTART, NUSP IS A FILE PRODUCED BY A PREVIOUS RUN
C         * OF THE MODEL, AND KSTART IS GREATER THAN ZERO. 

          IF(2*ILEV+6.GT.MX)  CALL XIT ('INGCM8',-8)  
          REWIND NUSP
C
C         * INPUT DATA RECORD #1.
C
          CALL GETFLD2(NUSP,F,-1,0,0,0,IBUF,MX,OK)
          IF (.NOT. OK)        CALL XIT (' INGCM8',-9)
C
          KSTART =INT(F(1))
          LRLMT  =INT(F(2))
          XXXX   =F(3)
          ILEV   =INT(F(4))
          LAY    =INT(F(4+2*ILEV+1))
          PTOIT  =F(4+2*ILEV+2)
C
          DO 100 I=1,ILEV
              SG(I)   =F(4+I)
              SH(I)   =F(4+I+ILEV)
 100      CONTINUE
C
          IF(6*NTRAC+17.GT.MX)  CALL XIT ('INGCM8',-10)
C
C         * INPUT DATA RECORD #2.
C
          CALL GETFLD2(NUSP,F,-1,0,0,0,IBUF,MX,OK)
          IF (.NOT. OK)         CALL XIT ('INGCM8',-11)
C
          ILG    =INT(F(1))
          ILAT   =INT(F(2))
          LEVS   =INT(F(3))
          DT     =F(4)
          IDAY   =INT(F(5))
          LDAY   =INT(F(6))
          MDAY   =INT(F(7))
          NDAYS  =INT(F(8))
          NSECS  =INT(F(9))
          AVGPS  =F(10)
          LEVOZ  =INT(F(11))
          QSRCRAT=F(12)
          QSRCRM1=F(13)
          WPP    =F(14)
          WP0    =F(15)
          WPM    =F(16)
          TFICQM =F(17)
C
          IF(NTRAC.GT.0)                                  THEN
              DO 200 N=1,NTRAC
                  XSRCRAT(N)=F(17+N)
                  XSRCRM1(N)=F(17+NTRAC+N)
                  XPP    (N)=F(17+2*NTRAC+N)
                  XP0    (N)=F(17+3*NTRAC+N)
                  XPM    (N)=F(17+4*NTRAC+N)
                  TFICXM (N)=F(17+5*NTRAC+N)
  200         CONTINUE    
          ENDIF
C
C         * READ-IN ADDITIONAL DATA RECORD FOR MULTI-YEAR SST/ICE RUNS.
C         * INPUT DATA RECORD #3.
C         * THIS WILL ONLY NOT BE FOUND IF RUNNING FROM AN OLD RESTART
C         * FILE WHICH WAS GENERATED BEFORE THE "MYRSSTI" OPTION WAS
C         * ADDED. IN THAT CASE, REWIND THE FILE SO THAT THE SPECTRAL
C         * DATA CAN BE SUCCESSFULLY FOUND IN THE RESTART SUBROUTINE.
C
          CALL GETFLD2(NUSP,F,NC4TO8("DATA"),-1,-1,-1,IBUF,MX,OK)
          IF (.NOT. OK)                              THEN
             WRITE(6,6070)
             IYEAR=0
             IYMDH=0
             REWIND NUSP
          ELSE
             IYEAR = INT(F(1))
             IYMDH = INT(F(2))
          ENDIF
      ENDIF 
  
      IF(ILEV.NE.NILEV)                    CALL XIT('INGCM8',-12) 
      IF(LEVS.NE.NLEVS)                    CALL XIT('INGCM8',-13) 
      IF(IHYD.LT.0.OR.IHYD.GT.2)           CALL XIT('INGCM8',-14) 
      IF(JLATPR.GT.ILAT.OR.JLATPR.LT.0)    CALL XIT('INGCM8',-15) 
      IF(ICON.LT.0.OR.ICON.GT.1)           CALL XIT('INGCM8',-16) 
      IF(IRAD.LT.0.OR.IRAD.GT.1)           CALL XIT('INGCM8',-17) 
C---------------------------------------------------------------------
C     * PRINT OUT MODEL PARAMETERS. 
  
      WRITE(6,6010) KSTART,KSTEPS,KTOTAL,KFINAL,IFDIFF,NEWRUN 
      WRITE(6,6015) ISSP,ISGG,ISRAD,ISEN,IEPR,ICSW,ICLW,ISBEG,JLATPR
      WRITE(6,6016) ITRAC 
      WRITE(6,6018) IOCEAN,ICEMOD,IFIXCLD,IHYD,ICON,IRAD
      WRITE(6,6020) ILG,ILAT,ILEV,LEVS,LRLMT
      WRITE(6,6022) NDAYS,NSECS,IDAY,LDAY,MDAY,INCD 
      CALL WRITLEV  (SG,ILEV,' SG ')
      CALL WRITLEV  (SH,ILEV,' SH ')
      WRITE(6,6025) LAY,ICOORD,PTOIT,MOIST,ITRVAR
      WRITE(6,6026) DELT
      WRITE(6,6030) IDIV
      WRITE(6,6050) AVGPS 
      WRITE(6,6060) IYEAR,IYMDH
      RETURN
  
  901 CALL XIT('INGCM8',-18) 
  902 CALL XIT('INGCM8',-19) 
  903 CALL XIT('INGCM8',-20) 
  904 CALL XIT('INGCM8',-21) 
  905 CALL XIT('INGCM8',-22) 
  906 CALL XIT('INGCM8',-23) 
  907 CALL XIT('INGCM8',-24) 
  908 CALL XIT('INGCM8',-25)
  909 CALL XIT('INGCM8',-26)
  910 CALL XIT('INGCM8',-27)
C-----------------------------------------------------------------------
 5005 FORMAT(10X,I5,I10,3I5)
 5010 FORMAT(10X,9I5) 
 5015 FORMAT(10X,7I5,A4) 
 5020 FORMAT(F10.0,I5,1X,A4,1X,A4,I5)
 6005 FORMAT('1C.C.R.N. GENERAL CIRCULATION MODEL') 
 6008 FORMAT('0*** GCM RUN HAS COMPLETED SUCCESSFULLY ***') 
 6010 FORMAT('0KSTART,KSTEPS,KTOTAL,KFINAL,IFDIFF,NEWRUN',8X,4I10,2I5)
 6015 FORMAT('0ISSP,ISGG,ISRAD,ISEN,IEPR,ICSW,ICLW,ISBEG,JLATPR',3X,9I5)
 6016 FORMAT('0TRACER SWITCH = ',I5)
 6018 FORMAT('0IOCEAN,ICEMOD,IFIXCLD,IHYD,ICON,IRAD',3X,6I5)
 6020 FORMAT('0LON,ILAT,ILEV,LEVS,LRLMT',29X,4I5,I10) 
 6022 FORMAT('0NDAYS,NSECS,IDAY,LDAY,MDAY,INCD', 22X,2I10,4I5/) 
 6025 FORMAT('0LAY=',I5,', ICOORD=',A4,', P.LID (PA)=',F10.3, 
     1       ', MOIST=',A4,', ITRVAR=',A4) 
 6026 FORMAT('0TIMESTEP (SEC) =',F8.1)
 6030 FORMAT('0IDIV = ',L6) 
 6050 FORMAT('0AVGPS = ',F10.3/)
 6060 FORMAT('0IYEAR,IYMDH = ',I5,5X,I10/)
 6070 FORMAT('0RESTART FILE DOES NOT CONTAIN IYEAR/IYMDH DATA, SO THESE
     1INITIALIZED TO ZERO')
 9000 FORMAT(A4)
      END 
