      SUBROUTINE INGCM12(NUSP,SG,SH,SECPU,KFINAL,AVGPS,LEVOZ,
     1                   NILEV,NLEVS,NTRAC,ITRVAR,XSRCRAT,XSRCRM1,
     2                   XPP,XP0,XPM,TFICXM,XSRC0,XSRCM,XSCL0,XSCLM)

C     * JUN 28/2018 - S.KHARIN.  KEEP KSTART FROM IBUF(2) INSTEAD OF F(1)
C     *                          REPLACE INT(*) WITH NINT(*).
C     * MAY 24/2017 - M.LAZARE.  FOR CONVERSION TO XC40:
C     *                          - REPLACE "DTIME" BY "CPU_TIME".
C     *                          - XIT IF LEVOZ.NE.INT(F(12)) AND ADJUST
C     *                            FOLLOWING XIT NUMBERS ACCORDINGLY.
C     * SEP 28/2006 - F.MAJAESS. NEW VERSION FOR GCM15F, BASED ON INGCM11:
C     *                          - REPLACE DIRECT BINARY "DATA" RECORD
C     *                            READS BY "GETFLD2" CALLS.
C     * APR 05/2006 - F.MAJAESS. PREVIOUS VERSION INGCM11 FOR GCM15C/D/E:
C     *                          - MODIFIED FOR EXIT PROCESSING ON RIGEL.
C     * FEB 15/2005 - M.LAZARE.NEW VERSION FOR GCM15C:
C     *                        INCLUDE READING OF HIGH-FREQUENCY SAVE
C     *                        SWITCH.
C     * DEC 15/2003 - M.LAZARE.NEW VERSION FOR GCM15B:
C     *                        INCLUDE QSRC0,QSRCM,XSRC0,XSRCM IN
C     *                        VARIABLES READ IN FROM DATA RESTART FILE.
C     * DEC 10/2003 - M.LAZARE.PREVIOUS VERSION INGCM9X FOR GCM13B.
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 GCM13 OR LATER WHICH SUPPORT
C     * RUNS WITH SURFACE TRACER FLUX INPUT.

      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(*), XSRCRM1(*),    XPP(*)
      REAL     XP0(*),     XPM(*), TFICXM(*)
      REAL   XSRC0(*),   XSRCM(*),  XSCL0(*),   XSCLM(*)

      INTEGER NFDM(13)
      INTEGER*4 MYNODE

      COMMON /PROCES/ IDIV
      COMMON /INTVLS/ ISSP,ISGG,ISRAD,ISBEG,ISEN,ISTR,IEPR,ICSW,ICLW,
     1                JLATPR
      COMMON /IHFSAVE/ISHF,ISMON,ISST
      COMMON /IOPTION/IOCEAN,ICEMOD,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,MDAYT,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,QSRC0,QSRCM,
     1                QSCL0,QSCLM,QCTOFF
      COMMON /KEEPTIM/IYEAR,IMDH,MYRSSTI,ISAVDTS
      COMMON /MPINFO/ MYNODE

      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)
C     SECPU=DTIME(TARRAY)
      F=0.0
      SECPU=0.0
C
      IF(MYNODE.EQ.0)      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     * ISTR    = INTERVAL AT WHICH TRACERS ARE SAVED.
C     * ISHF    = INTERVAL AT WHICH HIGH-FREQUENCY SAVE FIELDS 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     * ISST    = INTERVAL AT WHICH SITE LEVEL DATA SHOULD BE 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     * 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     *           'SL3D','SLQB).
C     * IYEAR   = YEAR WHICH MODEL STARTS FROM (I.E. '1979').

C     * EXAMPLE...

C     *  GCM15      72     15264    0   18    1
C     *             36   36   72    1   18    9   18   72    0   72    0
C     *              0    0 QHYB
C     *    1200.0    0 ET15 QHYB 1979

      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,ISTR,ISHF,ISST
      READ(5,5015,END=903) IOCEAN,ICEMOD,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('INGCM12',-1)

C     * ENSURE THAT ISBEG=KSTEPS IF AN OCEAN MODEL IS BEING USED.

      IF (IOCEAN.NE.0 .AND. ISBEG.NE.KSTEPS) CALL XIT('INGCM12',-2)

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)

      IF (KSTART.GE.KFINAL ) THEN
       IF (MYNODE.EQ.0) 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'
        CLOSE (98)
        CALL                                      XIT('INGCM12',-3)
       ELSE
        CALL                                      XIT('INGCM12',0)
       ENDIF
      ENDIF

      REWIND NUSP
C
      KTOTAL=KSTART+KSTEPS
      IF (KTOTAL.GT.KFINAL) KTOTAL=KFINAL
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

          IF(2*NILEV+5.GT.MX)  CALL               XIT('INGCM12',-4)
C
C         * INPUT DATA RECORD #1.
C
          CALL GETFLD2(NUSP,F,-1,0,0,0,IBUF,MX,OK)
          IF (.NOT. OK)        CALL               XIT('INGCM12',-5)

C         ALABL=F(1)
          KSTART=NINT(F(2))
          ILEV=NINT(F(3))
          DO L=1,ILEV
           SG(L)=F(3+L)
           SH(L)=F(3+ILEV+L)
          ENDDO
          LAY=NINT(F(3+2*ILEV+1))
          PTOIT=F(3+2*ILEV+2)

C
C         * INPUT DATA RECORD #2.
C
          CALL GETFLD2(NUSP,F,-1,0,0,0,IBUF,MX,OK)
          IF (.NOT. OK)         CALL              XIT('INGCM12',-6)
C
C         LABL=NINT(F(1))
          LRLMT=NINT(F(2))
          ILG=NINT(F(3))
          ILAT=NINT(F(4))
          IDAY=NINT(F(5))
          GMT=F(6)

C
C         * INPUT DATA RECORD #3.
C
          CALL GETFLD2(NUSP,F,-1,0,0,0,IBUF,MX,OK)
          IF (.NOT. OK) CALL                      XIT('INGCM12',-7)

C         ALABL=F(1)
          AVGPS=F(2)
C
          IFDIFF=1
          NEWRUN=1
          LEVS  =LEVS1
          IF (LEVS.GT.ILEV) LEVS=ILEV
          LDAY  =IDAY
          MDAY  =IDAY
          MDAYT =IDAY
          NSECS =NINT(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('INGCM12',-8)
          IHH=NINT(GMT)
          IMDH = 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*NILEV+6.GT.MX)  CALL               XIT('INGCM12',-9)
C
C         * INPUT DATA RECORD #1.
C
          CALL GETFLD2(NUSP,F,-1,0,0,0,IBUF,MX,OK)
          IF (.NOT. OK) CALL                      XIT('INGCM12',-10)
C
C         * KEEP KSTART VALUE FROM IBUF(2)
C         KSTART =NINT(F(1))
          LRLMT  =NINT(F(2))
          XXXX   =F(3)
          ILEV   =NINT(F(4))
          LAY    =NINT(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(10*NTRAC+22.GT.MX) CALL              XIT('INGCM12',-11)
C
C         * INPUT DATA RECORD #2.
C
          CALL GETFLD2(NUSP,F,-1,0,0,0,IBUF,MX,OK)
          IF (.NOT. OK) CALL                      XIT('INGCM12',-12)
C
          ILG    =NINT(F(1))
          ILAT   =NINT(F(2))
          LEVS   =NINT(F(3))
          DT     =F(4)
          IDAY   =NINT(F(5))
          LDAY   =NINT(F(6))
          MDAY   =NINT(F(7))
          MDAYT  =NINT(F(8))
          NDAYS  =NINT(F(9))
          NSECS  =NINT(F(10))
          AVGPS  =F(11)
          IF(LEVOZ.NE.NINT(F(12))) CALL XIT('INGCM12',-13)
          QSRCRAT=F(13)
          QSRCRM1=F(14)
          QSRC0  =F(15)
          QSRCM  =F(16)
          QSCL0  =F(17)
          QSCLM  =F(18)
          WPP    =F(19)
          WP0    =F(20)
          WPM    =F(21)
          TFICQM =F(22)
          IF(NTRAC.GT.0)                                  THEN
              DO 200 N=1,NTRAC
                  XSRCRAT(N)=F(22+N)
                  XSRCRM1(N)=F(22+NTRAC+N)
                  XPP    (N)=F(22+2*NTRAC+N)
                  XP0    (N)=F(22+3*NTRAC+N)
                  XPM    (N)=F(22+4*NTRAC+N)
                  TFICXM (N)=F(22+5*NTRAC+N)
                  XSRC0  (N)=F(22+6*NTRAC+N)
                  XSRCM  (N)=F(22+7*NTRAC+N)
                  XSCL0  (N)=F(22+8*NTRAC+N)
                  XSCLM  (N)=F(22+9*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
             IF(MYNODE.EQ.0)  WRITE(6,6070)
             IYEAR=0
             IMDH=0
             REWIND NUSP
          ELSE
             IYEAR = NINT(F(1))
             IMDH  = NINT(F(2))
          ENDIF
      ENDIF

      IF(ILEV.NE.NILEV) CALL                      XIT('INGCM12',-14)
      IF(LEVS.NE.NLEVS) CALL                      XIT('INGCM12',-15)
      IF(JLATPR.GT.ILAT.OR.JLATPR.LT.0) CALL      XIT('INGCM12',-16)
C---------------------------------------------------------------------
      IF(MYNODE.EQ.0)                                          THEN
C
C     * PRINT OUT MODEL PARAMETERS.
C
      WRITE(6,6010) KSTART,KSTEPS,KTOTAL,KFINAL,IFDIFF,NEWRUN
      WRITE(6,6015) ISSP,ISGG,ISRAD,ISEN,IEPR,ICSW,ICLW,ISBEG,ISTR,ISHF
      WRITE(6,6018) IOCEAN,ICEMOD
      WRITE(6,6020) ILG,ILAT,ILEV,LEVS,LRLMT
      WRITE(6,6022) NDAYS,NSECS,IDAY,LDAY,MDAY,MDAYT,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,IMDH
      ENDIF

      RETURN

  901 CALL                                        XIT('INGCM12',-17)
  902 CALL                                        XIT('INGCM12',-18)
  903 CALL                                        XIT('INGCM12',-19)
  904 CALL                                        XIT('INGCM12',-20)
  905 CALL                                        XIT('INGCM12',-21)
  906 CALL                                        XIT('INGCM12',-22)
  907 CALL                                        XIT('INGCM12',-23)
  908 CALL                                        XIT('INGCM12',-24)
  909 CALL                                        XIT('INGCM12',-25)
  910 CALL                                        XIT('INGCM12',-26)
C-----------------------------------------------------------------------
 5005 FORMAT(10X,I5,I10,3I5)
 5010 FORMAT(10X,12I5)
 5015 FORMAT(10X,2I5,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,ISTR,ISHF',3X,
     1         10I5)
 6018 FORMAT('0IOCEAN,ICEMOD,',3X,2I5)
 6020 FORMAT('0LON,ILAT,ILEV,LEVS,LRLMT',29X,4I5,I10)
 6022 FORMAT('0NDAYS,NSECS,IDAY,LDAY,MDAY,MDAYT,INCD', 22X,2I10,5I5/)
 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,IMDH = ',I5,5X,I6/)
 6070 FORMAT('0RESTART FILE DOES NOT CONTAIN IYEAR/IMDH DATA,',
     1       ' SO THESE INITIALIZED TO ZERO')
 9000 FORMAT(A4)
      END
