C**********************************************************                  XSECT.2     
C             SUBROUTINE XSECT                                               XSECT.3     
C**********************************************************                  XSECT.4     
      SUBROUTINE XSECT(ISKIP)                                                XSECT.5     
C                                                                            XSECT.6     
C     This subroutine gives quick look sigma-latitude x-sections             XSECT.7     
C     of [U], [T] and [V*T*].                                                XSECT.8     
C                                                                            XSECT.9     
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                                                                            BLANK.2     
C     Basic planetary parameters for run plus information about              BLANK.3     
C     vertical grid structure                                                BLANK.4     
C                                                                            BLANK.5     
C     Note that RD and GASCON are identical and CPD is set from RD,AKAP.     BLANK.6     
      COMMON        SQ(NNP),RSQ(NNP),SIGMAH(NLM),SIGMA(NL)                   BLANK.7     
     +              ,T01S2(NLM),T0(NL),ALPHA(NL),DSIGMA(NL),RDSIG(NL)        BLANK.8     
     +              ,TKP(NL),C(NL2),SQH(NNP)                                 BLANK.9     
     +              ,MF,MFP,JZF,NF,NFP                                       BLANK.10    
     +              ,AKAP,GA,GASCON,RADEA,WW,P0,PFAC,EZ,AIOCT                BLANK.11    
     +              ,RD,RV,CPD,CLATNT                                        BLANK.12    
     +              ,LRSTRT,LSHORT,LTVEC,LSTRETCH                            BLANK.13    
     +              ,LFLUX                                                   BLANK.14    
     +              ,LBALAN,LRESTIJ                                          BLANK.15    
     +              ,LCLIM, LPERPET, L22L,LOROG ,LCSFCT                      BLANK.16    
     +              ,LNOISE                                                  BLANK.17    
      COMPLEX EZ,AIOCT                                                       BLANK.18    
      LOGICAL LRSTRT,LSHORT,LTVEC,LSTRETCH,LBALAN,LRESTIJ                    BLANK.19    
     +       ,LFLUX,LNOISE                                                   BLANK.20    
     +       ,LCLIM, LPERPET, L22L,LOROG,LCSFCT                              BLANK.21    
C                                                                            BLANK.22    
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                                                                            GRIDP.2     
C     Array ordering in GRIDP must correspond to that in SPECTR.             GRIDP.3     
C     Real arrays: multi-level arrays are 1-dimensional.                     GRIDP.4     
C                                                                            GRIDP.5     
      COMMON/GRIDP/ CHIG(IGD),SFG(IGD),UG(IGD),VG(IGD)                       GRIDP.6     
     *              ,ZG(IGD),DG(IGD),TG(IGD)                                 GRIDP.7     
     +              ,TRAG(IGD,NTRAC)                                         GRIDP.8     
     *              ,PLG(IGC),PJG(IGC),PMG(IGC)                              GRIDP.9     
     *              ,SPG(IGC),VPG(IGC),EG(IGD)                               GRIDP.10    
     +              ,TNLG(IGD),TRANLG(IGD,NTRAC),FUG(IGD),FVG(IGD)           GRIDP.11    
     +              ,UTG(IGD),UTRAG(IGD,NTRAC)                               GRIDP.12    
     +              ,VTG(IGD),VTRAG(IGD,NTRAC),FVGT(IGD),FUGT(IGD)           GRIDP.13    
     $              ,GRPAD(NGRPAD)                                           GRIDP.14    
C                                                                            GRIDP.15    
C                                                                            LEGAU.2     
C     Legendre polynomials and information about gaussian latitudes          LEGAU.3     
C                                                                            LEGAU.4     
      COMMON/LEGAU/ ALPJ(MJP),DALPJ(MJP)                                     LEGAU.5     
     +              ,ALP(NWJ2,2,JGL),DALP(NWJ2,2,JGL)                        LEGAU.6     
     +              ,RLP(NWJ2,2,JGL),RDLP(NWJ2,2,JGL)                        LEGAU.7     
     +              ,SI(JGG),CS(JGG),SISQ(JGG),CSSQ(JGG),SECSQ(JGG)          LEGAU.8     
     +              ,ALAT(JGG),GWT(JGG),AW(JGG),JH,JL,JINC                   LEGAU.9     
C                                                                            LEGAU.10    
C                                                                            OUTCON.2     
C     Switches counters and constants controlling type and frequency of      OUTCON.3     
C     model output                                                           OUTCON.4     
C                                                                            OUTCON.5     
      COMMON/OUTCON/RNTAPE,NCOEFF,NLAT,INLAT,INSPC                           OUTCON.6     
     +              ,RNTAPO                                                  OUTCON.7     
     +              ,KOUNTP,KOUNTE,KOUNTH,KOUNTR                             OUTCON.8     
     +              ,KOUTP,KOUTE,KOUTH,KOUTR,DAY                             OUTCON.9     
     +              ,SQR2,RSQR2,EAM1,EAM2,TOUT1,TOUT2,RMG                    OUTCON.10    
     +              ,LSPO(NL),LGPO(NL)                                       OUTCON.11    
     $              ,LSHIST,LMINIH                                           OUTCON.12    
      LOGICAL LSHIST,LMINIH                                                  OUTCON.13    
      LOGICAL LSPO,LGPO                                                      OUTCON.14    
C                                                                            OUTCON.15    
      COMMON/GRIDSS/ZG1(IGD,JG),DG1(IGD,JG),UG1(IGD,JG),VG1(IGD,JG),         GRIDSS.2     
     :              TG1(IGD,JG),SPG1(IGC,JG)                                 GRIDSS.3     
C                                                                            GRIDSS.4     
C                                                                            RESTOR.2     
C     Restoration fields and timescale                                       RESTOR.3     
C                                                                            RESTOR.4     
      COMMON/RESTOR/ZRES(IGN),DRES(IGN),TRES(IGN),SPRES(IGM),DAMP            RESTOR.5     
C                                                                            RESTOR.6     
      INTEGER LU(IGG),LT(IGG),LVT(IGG)                                       XSECT.19    
c                                                                            NETDATA.2     
      integer nmaxdims,nall                                                  NETDATA.3     
      parameter(nmaxdims=4,nall=10)                                          NETDATA.4     
      integer ndim,nvar,natts(nall),nattsvar(nall),                          NETDATA.5     
     :        vdims(nall),vadims(nmaxdims,nall),                             NETDATA.6     
     :        ndims(nall)                                                    NETDATA.7     
      character*200 dimname(nall),varname(nall),                             NETDATA.8     
     :              attdimname(2,nmaxdims,nall),                             NETDATA.9     
     :              attvarname(2,nmaxdims,nall)                              NETDATA.10    
      integer nc1,iddim1(nall),idvar1(nall)                                  NETDATA.11    
      common/netcdfvars/nc1,iddim1,idvar1                                    NETDATA.12    
c                                                                            NETDATA.13    
      REAL LUG(MG,JGG,NL),LTG(MG,JGG,NL)                                     NETCDF.2     
      LOGICAL LFIRST                                                         NETCDF.3     
      INTEGER INETCOUNT                                                      NETCDF.4     
      SAVE LFIRST,INETCOUNT                                                  NETCDF.5     
      DATA LFIRST/ .TRUE. /                                                  NETCDF.6     
      DATA INETCOUNT/0/                                                      NETCDF.7     
C                                                                            XSECT.20    
C     Set ISLT, The default being to have 16 I5 integers per level.          XSECT.21    
C                                                                            XSECT.22    
      IF (ISKIP.EQ.0) THEN                                                   XSECT.23    
         RETURN                                                              XSECT.24    
      ELSE IF (ISKIP.GT.0) THEN                                              XSECT.25    
         ISLT = ISKIP                                                        XSECT.26    
      ELSE                                                                   XSECT.27    
         ISLT = (NHEM*JG-1)/18 + 1                                           XSECT.28    
      ENDIF                                                                  XSECT.29    
      IF (LFIRST) THEN                                                       NETCDF.8     
         call ini_netcdf                                                     NETCDF.9     
         LFIRST=.FALSE.                                                      NETCDF.10    
      END IF                                                                 NETCDF.11    
      INETCOUNT=INETCOUNT+1                                                  NETCDF.12    
C                                                                            XSECT.30    
C     Output is wanted. Read grid point fields from stream 24                XSECT.31    
C                                                                            XSECT.32    
C      REWIND 24                                                             XSECT.33    
      RMG=1./REAL(MG)                                                        XSECT.34    
      WRITE (2,104) DAY,DOY                                                  XSECT.35    
  104 FORMAT(/' CROSS SECTIONS FOR DAY, DOY',2F6.1)                          XSECT.36    
      DO 10 J=1,JG                                                           XSECT.37    
C                                                                            XSECT.38    
C        Loop over latitudes to read gridpoint data, dimensionalise          XSECT.39    
C        and calculate zonal means                                           XSECT.40    
C                                                                            XSECT.41    
         SEC=ALAT(J)/57.29578                                                XSECT.42    
         SEC=10.*CV/COS(SEC)                                                 XSECT.43    
C         READ(24) ZG,DG,UG,VG,TG,SPG                                        XSECT.44    
         DO I=1,IGD                                                          XSECT.45    
            ZG(I)=ZG1(I,J)                                                   XSECT.46    
            DG(I)=DG1(I,J)                                                   XSECT.47    
            UG(I)=UG1(I,J)                                                   XSECT.48    
            VG(I)=VG1(I,J)                                                   XSECT.49    
            TG(I)=TG1(I,J)                                                   XSECT.50    
         END DO                                                              XSECT.51    
         DO I=1,IGC                                                          XSECT.52    
            SPG(I)=SPG1(I,J)                                                 XSECT.53    
         END DO                                                              XSECT.54    
C                                                                            XSECT.55    
C        Loop for [U] and [T] sections                                       XSECT.56    
C                                                                            XSECT.57    
         DO L=1,NL                                                           NETCDF.13    
         DO IHEM=1,NHEM                                                      NETCDF.14    
               IF (IHEM.EQ.1) THEN                                           NETCDF.15    
                  JJ=J                                                       NETCDF.16    
               ELSE                                                          NETCDF.17    
                  JJ=JGGP-J                                                  NETCDF.18    
               ENDIF                                                         NETCDF.19    
            IOF=MGPP*(IHEM-1)+(L-1)*IGC                                      NETCDF.20    
            DO I=1,MG                                                        NETCDF.21    
               LUG(I,JJ,L)=0.1*SEC*UG(I+IOF)                                 NETCDF.22    
            END DO                                                           NETCDF.23    
            DO I=1,MG                                                        NETCDF.24    
               LTG(I,JJ,L)=CT*(TG(I+IOF)+T0(L))                              NETCDF.25    
            END DO                                                           NETCDF.26    
        end do                                                               NETCDF.27    
        end do                                                               NETCDF.28    
         DO 11 L=1,NL                                                        XSECT.58    
            DO 12 IHEM=1,NHEM                                                XSECT.59    
               IP=(L-1)*IGC+(IHEM-1)*MGPP                                    XSECT.60    
               UB=0.                                                         XSECT.61    
               DO 30 I=1,MG                                                  XSECT.62    
                  UB=UB+UG(IP+I)                                             XSECT.63    
   30          CONTINUE                                                      XSECT.64    
               UB=UB*RMG                                                     XSECT.65    
               TB=0.                                                         XSECT.66    
               DO 40 I=1,MG                                                  XSECT.67    
                  TB=TB+TG(IP+I)                                             XSECT.68    
   40          CONTINUE                                                      XSECT.69    
               TB=TB*RMG+T0(L)                                               XSECT.70    
C                                                                            XSECT.71    
C              Check whether hemisphere or globe                             XSECT.72    
C                                                                            XSECT.73    
               IF (IHEM.EQ.1) THEN                                           XSECT.74    
                  JJ=J                                                       XSECT.75    
               ELSE                                                          XSECT.76    
                  JJ=JGGP-J                                                  XSECT.77    
               ENDIF                                                         XSECT.78    
               IX=(L-1)*JGG+JJ                                               XSECT.79    
               LU(IX)=NINT(UB*SEC)                                           XSECT.80    
               LT(IX)=NINT(TB*CT)                                            XSECT.81    
   12       CONTINUE                                                         XSECT.82    
   11    CONTINUE                                                            XSECT.83    
C                                                                            XSECT.84    
C        Loop for temperature flux section                                   XSECT.85    
C                                                                            XSECT.86    
         DO 13 L=1,NL                                                        XSECT.87    
            DO 14 IHEM=1,NHEM                                                XSECT.88    
               IP=(L-1)*IGC+(IHEM-1)*MGPP                                    XSECT.89    
               VT=0.                                                         XSECT.90    
               DO 15 I=1,MG                                                  XSECT.91    
                  VT=VT+TG(IP+I)*VG(IP+I)                                    XSECT.92    
   15          CONTINUE                                                      XSECT.93    
               VT=RMG*VT                                                     XSECT.94    
               TB=0.                                                         XSECT.95    
               DO 50 I=1,MG                                                  XSECT.96    
                  TB=TB+TG(IP+I)                                             XSECT.97    
   50          CONTINUE                                                      XSECT.98    
               TB=TB*RMG                                                     XSECT.99    
               VB=0.                                                         XSECT.100   
               DO 60 I=1,MG                                                  XSECT.101   
                  VB=VB+VG(IP+I)                                             XSECT.102   
   60          CONTINUE                                                      XSECT.103   
               VB=VB*RMG                                                     XSECT.104   
C                                                                            XSECT.105   
C              Check whether hemisphere or globe                             XSECT.106   
C                                                                            XSECT.107   
               IF (IHEM.EQ.1) THEN                                           XSECT.108   
                  JJ=J                                                       XSECT.109   
               ELSE                                                          XSECT.110   
                  JJ=JGGP-J                                                  XSECT.111   
               ENDIF                                                         XSECT.112   
               IX=(L-1)*JGG+JJ                                               XSECT.113   
               LVT(IX)=NINT((VT-VB*TB)*SEC*CT)                               XSECT.114   
   14       CONTINUE                                                         XSECT.115   
   13    CONTINUE                                                            XSECT.116   
   10 CONTINUE                                                               XSECT.117   
C                                                                            XSECT.118   
      call writevar2(nc1,idvar1(1),lug,                                      NETCDF.29    
     :                     1,mg,1,jgg,1,nl,inetcount,inetcount)              NETCDF.30    
      call writevar2(nc1,idvar1(2),ltg,                                      NETCDF.31    
     :                     1,mg,1,jgg,1,nl,inetcount,inetcount)              NETCDF.32    
C     Printing sections - separate loop for each field.                      XSECT.119   
C     First the zonal wind                                                   XSECT.120   
C                                                                            XSECT.121   
      WRITE (2,100)                                                          XSECT.122   
  100 FORMAT(' ZONAL WIND IN 0.1 m/s')                                       XSECT.123   
      DO 20 L=1,NL                                                           XSECT.124   
         J0=(L-1)*JGG                                                        XSECT.125   
         WRITE (2,101) (LU(J0+J),J=1,JGG,ISLT)                               XSECT.126   
   20 CONTINUE                                                               XSECT.127   
101   FORMAT(18I4)                                                           XSECT.128   
C                                                                            XSECT.129   
C     Secondly the temperature                                               XSECT.130   
C                                                                            XSECT.131   
      WRITE(2,102)                                                           XSECT.132   
  102 FORMAT(' TEMPERATURE IN K')                                            XSECT.133   
      DO 21 L=1,NL                                                           XSECT.134   
         J0=(L-1)*JGG                                                        XSECT.135   
         WRITE(2,101) (LT(J0+J),J=1,JGG,ISLT)                                XSECT.136   
   21 CONTINUE                                                               XSECT.137   
C                                                                            XSECT.138   
C     Thirdly temperature flux                                               XSECT.139   
C                                                                            XSECT.140   
      WRITE(2,103)                                                           XSECT.141   
  103 FORMAT(' POLEWARD TEMPERATURE FLUX IN 0.1Km/s')                        XSECT.142   
      DO 22 L=1,NL                                                           XSECT.143   
         J0=(L-1)*JGG                                                        XSECT.144   
         WRITE(2,101) (LVT(J0+J),J=1,JGG,ISLT)                               XSECT.145   
   22 CONTINUE                                                               XSECT.146   
C                                                                            XSECT.147   
C      REWIND 24                                                             XSECT.148   
      RETURN                                                                 XSECT.149   
      END                                                                    XSECT.150   
                                                                             XSECT.151   
