
      SUBROUTINE routq(NT,NE,NAX,NDX,NTX,NEX,NHW,NHE,NVN,NVS)
C
C     THIS SUBROUTINE OPENS CHECKPOINT FILES AND READS
C     FILE NAMES FOR PLANT SPECIES AND MANAGEMENT
C
      include "parameters.h"
      include "filec.h"
      include "files.h"
      include "blkc.h"
      include "blk9c.h"
      DIMENSION NPP(JY,JX)
      CHARACTER*16 DATA(30),DATAC(30,250,250),DATAP(JP,JY,JX)
     2,DATAM(JP,JY,JX),DATAX(JP),DATAY(JP),DATAZ(JP,JY,JX)
     3,OUTS(10),OUTP(10),OUTFILS(10,JY,JX),OUTFILP(10,JP,JY,JX)
      CHARACTER*3 CHOICE(102,20)
      CHARACTER*8 CDATE
      CHARACTER*16 DATAA(JP,JY,JX),DATAB(JP,JY,JX)
      CHARACTER*16 OUTX,OUTC,OUTM,OUTR,OUTQ
      CHARACTER*4 CHARY
      CHARACTER*80 PREFIX
      CHARACTER*2 CLIMATE
C
C     OPEN CHECKPOINT FILES FOR PLANT VARIABLES
C
      IF(IGO.EQ.0)THEN
      DO 9999 NX=NHW,NHE
      DO 9999 NY=NVN,NVS
      DO 9999 NZ=1,5
      IDAY0(NZ,NY,NX)=-1E+06
      IYR0(NZ,NY,NX)=-1E+06
      IDAYH(NZ,NY,NX)=1E+06
      IYRH(NZ,NY,NX)=1E+06
9999  CONTINUE
      IF(DATA(20).EQ.'YES')THEN
      IDATE=IDATA(9)
      ELSE
      IDATE=IDATA(3)
      ENDIF
      WRITE(CHARY,'(I4)')IDATE
      OUTX='P'//DATA(1)(1:2)//CHARY(1:4)
      OUTC='C'//DATA(1)(1:2)//CHARY(1:4)
      OUTM='M'//DATA(1)(1:2)//CHARY(1:4)
      OUTR='R'//DATA(1)(1:2)//CHARY(1:4)
      OUTQ='Q'//DATA(1)(1:2)//CHARY(1:4)
      OPEN(26,FILE=OUTX,STATUS='UNKNOWN')
      OPEN(27,FILE=OUTC,STATUS='UNKNOWN')
      OPEN(28,FILE=OUTM,STATUS='UNKNOWN')
      OPEN(29,FILE=OUTR,STATUS='UNKNOWN')
      OPEN(30,FILE=OUTQ,STATUS='UNKNOWN')
      ENDIF
C
C     READ PLANT MANAGEMENT FILE NAMES FOR EACH GRID CELL
C
      IF(DATAC(10,NE,NEX).NE.'NO')THEN
      NN=0
      OPEN(14,FILE=TRIM(PREFIX)//DATAC(10,NE,NEX),STATUS='OLD')
50    READ(14,*,END=1000)NH1,NV1,NH2,NV2,NS
C     NN=NN+1
      NN=1
      DO 4995 NX=NH1,NH2
      DO 4990 NY=NV1,NV2
      NP0(NY,NX)=NS
      DO 4985 NZ=1,NS
      LSG(NZ,NY,NX)=NN
4985  CONTINUE
4990  CONTINUE
4995  CONTINUE
      IF(NS.GT.0)THEN
      READ(14,*)(DATAX(NZ),DATAY(NZ),NZ=1,NS)
      DO 4975 NX=NH1,NH2
      DO 4970 NY=NV1,NV2
      DO 4965 NZ=1,NS
      IF(IETYP(NY,NX).GT.0)THEN
      WRITE(CLIMATE,'(I2)')IETYP(NY,NX)
      DATAX(NZ)=DATAX(NZ)(1:4)//CLIMATE 
      ENDIF 
4965  CONTINUE
4970  CONTINUE
4975  CONTINUE
      ENDIF
      IF(DATA(20).EQ.'NO')THEN
      DO 8995 NX=NH1,NH2
      DO 8990 NY=NV1,NV2
      NP(NY,NX)=NS
      NPP(NY,NX)=0
      DO 100 NZ=1,NP(NY,NX)
      DATAP(NZ,NY,NX)=DATAX(NZ)
      DATAM(NZ,NY,NX)=DATAY(NZ)
100   CONTINUE
      DO 101 NZ=NP(NY,NX)+1,5
      DATAP(NZ,NY,NX)='NO'
      DATAM(NZ,NY,NX)='NO'
101   CONTINUE
8990  CONTINUE
8995  CONTINUE
      ELSE
C      write(*,*)'READ PLANT SPECIES NAMES'
C
C     READ PLANT SPECIES NAMES FROM EARLIER RUN IF NEEDED
C
      REWIND(30)
8000  CONTINUE
      DO 9995 NX=NHW,NHE
      DO 9990 NY=NVN,NVS
      READ(30,90,END=1001)IDATE,IYR,NPP(NY,NX)
     2,(DATAZ(NZ,NY,NX),IFLGC(NZ,NY,NX),NZ=1,NPP(NY,NX))
90    FORMAT(2I4,1I3,5(A16,I4))
9990  CONTINUE
9995  CONTINUE
      IF(IDATE.LT.IDAYR.OR.IYR.LT.IYRR)THEN
      GO TO 8000
      ELSEIF(IDATE.GE.IDAYR.AND.IYR.EQ.IYRR)THEN
C
C     MATCH PREVIOUS AND CURRENT PLANT SPECIES
C
      DO 7975 NX=NHW,NHE
      DO 7970 NY=NVN,NVS
      DO 7965 NZ=1,NS
      DATAA(NZ,NY,NX)=DATAX(NZ)
      DATAB(NZ,NY,NX)=DATAY(NZ)
7965  CONTINUE
7970  CONTINUE
7975  CONTINUE
      DO 7995 NX=NH1,NH2
      DO 7990 NY=NV1,NV2
      NP(NY,NX)=MAX(NS,NPP(NY,NX))
      DO 195 NN=1,NP(NY,NX)
      DATAP(NN,NY,NX)='NO'
      DATAM(NN,NY,NX)='NO'
195   CONTINUE
      IF(NPP(NY,NX).GT.0)THEN
      DO 200 NN=1,NPP(NY,NX)
      DO 205 NZ=1,NS
      IF(DATAZ(NN,NY,NX).EQ.DATAX(NZ).AND.IFLGC(NN,NY,NX).EQ.1)THEN
      DATAP(NN,NY,NX)=DATAX(NZ)
      DATAM(NN,NY,NX)=DATAY(NZ)
      DATAA(NZ,NY,NX)='NO'
      DATAB(NZ,NY,NX)='NO'
      ENDIF
205   CONTINUE
200   CONTINUE
C
C     ADD NEW PLANT SPECIES
C
      DO 250 NN=1,NP(NY,NX)
C     WRITE(*,2223)'250',NX,NY,NZ,NN,NP(NY,NX),NS
C    2,(DATAB(NZ,NY,NX),NZ=1,NS),DATAP(NN,NY,NX),DATAM(NN,NY,NX)
      IF(DATAP(NN,NY,NX).EQ.'NO')THEN
      DO 255 NZ=1,NS
C     WRITE(*,2223)'255',NX,NY,NZ,NN,NP(NY,NX),NS
C    2,DATAM(NN,NY,NX),DATAB(NZ,NY,NX)
2223  FORMAT(A8,6I4,10A16)
      IF(DATAA(NZ,NY,NX).NE.'NO')THEN
      DATAP(NN,NY,NX)=DATAA(NZ,NY,NX)
      DATAM(NN,NY,NX)=DATAB(NZ,NY,NX)
      DATAA(NZ,NY,NX)='NO'
      DATAB(NZ,NY,NX)='NO'
      GO TO 250
      ENDIF
255   CONTINUE
      ENDIF
250   CONTINUE
      DO 201 NZ=NP(NY,NX)+1,5
      DATAP(NZ,NY,NX)='NO'
      DATAM(NZ,NY,NX)='NO'
201   CONTINUE
      ELSE
      DO 265 NZ=1,NS
      DATAP(NZ,NY,NX)=DATAX(NZ)
      DATAM(NZ,NY,NX)=DATAY(NZ)
265   CONTINUE
      DO 270 NZ=NS+1,5
      DATAP(NZ,NY,NX)='NO'
      DATAM(NZ,NY,NX)='NO'
270   CONTINUE
      ENDIF
C
C     SET NUMBER OF PLANT SPECIES
C
      NN=5
      DO 202 NZ=5,1,-1
      IF(DATAP(NZ,NY,NX).EQ.'NO')THEN
      NN=NN-1
      ELSE
      GO TO 203
      ENDIF
202   CONTINUE
203   CONTINUE
      NP(NY,NX)=NN
      NP0(NY,NX)=NN
7990  CONTINUE
7995  CONTINUE
      ENDIF
      ENDIF
      GO TO 50
1001  CONTINUE
      DO 6995 NX=NHW,NHE
      DO 6990 NY=NVN,NVS
      NP(NY,NX)=NS
      DO 300 NZ=1,NP(NY,NX)
      DATAP(NZ,NY,NX)=DATAX(NZ)
      DATAM(NZ,NY,NX)=DATAY(NZ)
300   CONTINUE
      DO 301 NZ=NP(NY,NX)+1,5
      DATAP(NZ,NY,NX)='NO'
      DATAM(NZ,NY,NX)='NO'
301   CONTINUE
6990  CONTINUE
6995  CONTINUE
      GO TO 50
1000  CLOSE(14)
      ELSE
      DO 5995 NX=NHW,NHE
      DO 5995 NY=NVN,NVS
      NP(NY,NX)=0
      DO 5995 NZ=1,NP0(NY,NX)
      DATAP(NZ,NY,NX)='NO'
      DATAM(NZ,NY,NX)='NO'
5995  CONTINUE
      ENDIF
      RETURN
      END
