C**********************************************************                  MASCOR.2     
C             SUBROUTINE MASCOR                                              MASCOR.3     
C**********************************************************                  MASCOR.4     
      SUBROUTINE MASCOR                                                      MASCOR.5     
C                                                                            MASCOR.6     
C     MASCOR - Correction of global average surface pressure.                MASCOR.7     
C     No longer requires KOUNTE=1 to work correctly.                         MASCOR.8     
C                                                                            MASCOR.9     
C     Mike Blackburn  15/05/2000.  Original version for IGCM2.               MASCOR.10    
C                                                                            MASCOR.11    
C     Purpose.                                                               MASCOR.12    
C     --------                                                               MASCOR.13    
C                                                                            MASCOR.14    
C        This subroutine modifies the m=0,n=0 coefficient of ln(ps)          MASCOR.15    
c     to correct the mean surface pressure (total mass) at that at the       MASCOR.16    
c     start of the run, preventing drift in extended integrations.           MASCOR.17    
C                                                                            MASCOR.18    
C     Interface.                                                             MASCOR.19    
C     ----------                                                             MASCOR.20    
C                                                                            MASCOR.21    
C     MASCOR is called from the main program MLTRI.                          MASCOR.22    
C     Timestep counters are stored in common BATS.                           MASCOR.23    
C     Mean surface pressures and switches are stored in common STATS.        MASCOR.24    
C     Spectral arrays SP,SPA are stored in common SPECTR.                    MASCOR.25    
C                                                                            MASCOR.26    
C     Method.                                                                MASCOR.27    
C     -------                                                                MASCOR.28    
C                                                                            MASCOR.29    
C        Rescale the global mean surface pressure to its initial value       MASCOR.30    
c     by adding the required constant to the m=0,n=0 ln(ps) coefficient.     MASCOR.31    
C     Use the time-lagged mean pressure to avoid numerical instability.      MASCOR.32    
C                                                                            MASCOR.33    
C        It is assumed there are no sources or sinks in the mass             MASCOR.34    
C     continuity equation (i.e. will not work if SPA changes in the diabat   MASCOR.35    
C     part of the time step).                                                MASCOR.36    
C     The correction should be applied to the                                MASCOR.37    
C     mass of dry air if precip/evap mass variations were included.          MASCOR.38    
C                                                                            MASCOR.39    
C                                                                            PARAM1.2     
C     Determines model resolution                                            PARAM1.3     
C                                                                            PARAM1.4     
      PARAMETER(NN=21,MM=21,NHEM=2,NL=5,MOCT=1,MG=64,JG=16,NWJ2=121          NETCDF.35    
     P         ,NCRAY=64,JGL=JG,NTRAC=1,NLEVRF=1)                            NETCDF.36    
                                                                             PARAM1.7     
C                                                                            PARAM1.8     
C                                                                            PARAM2.2     
C     Sets basic constants, especially those needed for array dimensions     PARAM2.3     
C                                                                            PARAM2.4     
      PARAMETER(MH=2,PI=3.14159265359,PI2=2.0*PI                             PARAM2.5     
     +,NNP=NN+1,MGPP=MG+2,JGP=JG+1,JGG=JG*NHEM,JGGP=JGG+1,MJP=NWJ2+NWJ2      PARAM2.6     
     +,NLM=NL-1,NLP=NL+1,NLPP=NL+2,NLA=NL+3,NLB=NL+4,NL2=NL*NL               PARAM2.7     
     +,IDA=(MG+MG+MG)/2+1,IDB=NWJ2*NL,IDC=IDB+IDB,IDD=MGPP*NL                PARAM2.8     
     +,IDE=NL2*NN,IDF=NCRAY*(MG+1),IDG=JG*NL,IDH=JG*MG                       PARAM2.9     
     +,IDI=NNP/2,IDJ=IDI*IDI,IDK=NL*IDI,IDL=MGPP/2,IDM=NNP/2,IDN=IDM*NL      PARAM2.10    
     +,NWW=1+(MM-1)/MOCT)                                                    PARAM2.11    
      PARAMETER(IGA=NWJ2*NHEM,IGB=IDB*NHEM,IGC=MGPP*NHEM,IGD=IDD*NHEM        PARAM2.12    
     +,IGG=IDG*NHEM,IGL=IDL*NHEM,IGM=IDM*NHEM,IGN=IDN*NHEM                   PARAM2.13    
     +,IGO=IGA+IGA,IGP=IGB+IGB,NFTWG=(5+NTRAC)*NL+3                          PARAM2.14    
     +,NFTGW=(6+3*NTRAC)*NL+2,NFTGD=(3+NTRAC)*NL,NLTR=NL*NTRAC)              PARAM2.15    
C     Number of 2D (surface) output fields. This value is                    PARAM2.16    
C     Doubled due to averaged and instantaneous fields.                      PARAM2.17    
      PARAMETER (N2DFLD=21,NGRPAD=N2DFLD*2*IGC)                              PARAM2.18    
C                                                                            PARAM2.19    
C                                                                            BATS.2     
C     Constant arrays and variables associated with time and vertical        BATS.3     
C     differencing. Also counters.                                           BATS.4     
C                                                                            BATS.5     
      COMMON/BATS/  BM1(IDE),AK(NNP),AQ(NL2),G(NL2),TAU(NL2)                 BATS.6     
     +              ,KOUNT,KITS,KSTART,KTOTAL,KRUN,BEGDAY,ITSPD              BATS.7     
     +              ,DELT,DELT2,CV,CG,CT,CQ,PNU,PNU2,PNU21                   BATS.8     
     +              ,NTRACO,CTRA(NTRAC),KOLOUR(NTRAC),RGG(NL2)               BATS.9     
     +              ,BEGDOY,DOY                                              BATS.10    
C                                                                            BATS.11    
C                                                                            SPECTR.2     
C     Array ordering in SPECTR must correspond to that in GRIDP.             SPECTR.3     
C                                                                            SPECTR.4     
      COMMON/SPECTR/Z(IGB),D(IGB),T(IGB),TRA(IGB,NTRAC),SP(IGA),GS(IGA)      SPECTR.5     
     :              ,SPA(IGA),VP(IGA),DTE(IGB),TT(IGB)                       SPECTR.6     
     :              ,TRAT(IGB,NTRAC),DT(IGB),ZT(IGB)                         SPECTR.7     
     :              ,ZMI(IGB),DMI(IGB),TMI(IGB)                              SPECTR.8     
     :              ,TRAMI(IGB,NTRAC),SPMI(IGA)                              SPECTR.9     
      COMPLEX Z,D,T,TRA,SP,GS,SPA,VP,DTE,TT,TRAT,DT,ZT                       SPECTR.10    
     :       ,ZMI,DMI,TMI,TRAMI,SPMI                                         SPECTR.11    
C                                                                            SPECTR.12    
      COMMON/STATS/GMSP0,GMSPMI,LMASCOR,LMASOLD,LMASPRT                      STATS.2     
      LOGICAL LMASCOR,LMASOLD,LMASPRT                                        STATS.3     
C                                                                            STATS.4     
C-----------------------------------------------------------------------     STATS.5     
C                                                                            MASCOR.45    
C     ------------------------------------------------------------------     MASCOR.46    
C                                                                            MASCOR.47    
C     Extract the global mass.                                               MASCOR.48    
C                                                                            MASCOR.49    
      GMSP=1.0+REAL(SPA(1))/SQRT(2.0)                                        MASCOR.50    
C                                                                            MASCOR.51    
C     ------------------------------------------------------------------     MASCOR.52    
C                                                                            MASCOR.53    
C     Preserve the mass at (re)start as required.                            MASCOR.54    
C     Note that the timestep counter has already been incremented.           MASCOR.55    
C Pressure at start of initial run. Otherwise read in from restart file.     MASCOR.56    
C                                                                            MASCOR.57    
      IF ((KOUNT.EQ.KSTART+1).AND.GMSP0.EQ.0.0) THEN                         MASCOR.58    
         GMSP0=GMSP                                                          MASCOR.59    
         GMSPMI=GMSP                                                         MASCOR.60    
         WRITE(2,6900) GMSP                                                  MASCOR.61    
      ENDIF                                                                  MASCOR.62    
C                                                                            MASCOR.63    
C     ------------------------------------------------------------------     MASCOR.64    
C                                                                            MASCOR.65    
C     Compute correction.                                                    MASCOR.66    
C                                                                            MASCOR.67    
      ZSPRAT=GMSPMI/GMSP0                                                    MASCOR.68    
      ZLNRAT=LOG(ZSPRAT)                                                     MASCOR.69    
      SP(1)=SP(1)-SQRT(2.)*CMPLX(ZLNRAT,0.)                                  MASCOR.70    
C                                                                            MASCOR.71    
C     ------------------------------------------------------------------     MASCOR.72    
C                                                                            MASCOR.73    
C     Print diagnostics, correcting for (KITS-1) offset.                     MASCOR.74    
C                                                                            MASCOR.75    
      IF (KITS.GT.0) THEN                                                    MASCOR.76    
         KTEMP=KOUNT+1-KITS                                                  MASCOR.77    
      ELSE                                                                   MASCOR.78    
         KTEMP=KOUNT                                                         MASCOR.79    
      ENDIF                                                                  MASCOR.80    
      IF (LMASPRT.AND.                                                       MASCOR.81    
     :    (KTEMP.LE.(KSTART+ITSPD).OR.MOD(KTEMP,ITSPD).EQ.0)) THEN           MASCOR.82    
         ZSPDIF=GMSPMI-GMSP0                                                 MASCOR.83    
         WRITE(2,6901) GMSPMI,GMSP0,ZSPDIF,ZLNRAT,REAL(SP(1))                MASCOR.84    
      ENDIF                                                                  MASCOR.85    
C                                                                            MASCOR.86    
C     ------------------------------------------------------------------     MASCOR.87    
C                                                                            MASCOR.88    
C     Update the time-lagged mass.                                           MASCOR.89    
C                                                                            MASCOR.90    
      GMSPMI=GMSP                                                            MASCOR.91    
C                                                                            MASCOR.92    
C     ------------------------------------------------------------------     MASCOR.93    
C                                                                            MASCOR.94    
C     Formats.                                                               MASCOR.95    
C                                                                            MASCOR.96    
 6900 FORMAT(' GLOBAL MASS RESET TO CURRENT RESTART VALUE =',1PE15.8)        MASCOR.97    
 6901 FORMAT('  PMEAN=',1PE15.8,' PM0=',1PE15.8                              MASCOR.98    
     :,' DIFF=',1PE15.8,' LN(PS) CORR=',1PE15.8,' LN(PS)=',1PE15.8)          MASCOR.99    
C                                                                            MASCOR.100   
C     ------------------------------------------------------------------     MASCOR.101   
C                                                                            MASCOR.102   
      RETURN                                                                 MASCOR.103   
      END                                                                    MASCOR.104   
