C___________________________________________________________
	
*  THIS ROUTINE READS OR WRITES TREE-RING
*  INDICES OR MEASUREMENTS IN STANDARD FORMATS USED
*  IN THE INTERNATIONAL TREE-RING DATA BANK (ITRDB).
*
*     CALL TRRW(ID,IYR,N,Y,NC,IFL,IU,IUO,Q)
*               ==       = ==            =        (= See below)
*
* ARGUMENTS (9):
*
* ID:    SERIES IDENTIFICATION
*           6 CHARACTERS FOR INDICES, 8 FOR MEASUREMENTS; STRING MAY BE
*           LONGER.  WHEN Q = 'IR', IF THE FIRST SIX CHARACTERS OF
*           ID = '******' THEN NC NEED NOT BE AN ARRAY.
* IYR:   STARTING YEAR OF SERIES
* N:     NUMBER OF DATA VALUES IN SERIES
*           RETURNED AS -999 IF END-OF-FILE
* Y:     ARRAY OF INDEX OR MEASUREMENT VALUES
*           WHEN Q = 'IW', IF MAXIMUM VALUE OF SERIES IS OVER
*           9.989, ENTIRE SERIES IS DIVIDED BY A FACTOR OF TEN
*           IN ORDER TO LOWER THE MAXIMUM TO 9.989 OR LOWER.
* NC:    ARRAY OF NUMBER OF SERIES INCLUDED IN INDEX VALUE FOR EACH YEAR
*           WHEN Q = 'IR', SEE EXCEPTION UNDER 'ID' ABOVE.
*           WHEN Q = 'IW', IF NC(1) IS ZERO OR NEGATIVE,
*           NC NEED NOT BE AN ARRAY)
* IFL:   COUNT OF NUMBER OF SERIES READ; RETURNED AS -999 IF END-OF-FILE
* IU:    UNIT FOR READING OR WRITING DATA
* IUO:   UNIT FOR PRINTED OUTPUT
* Q:     CHARACTER STRING *2 TO INDICATE ACTION DESIRED:
*
*      INDICES,      READ FROM FILE:   Q = 'IR'
*      MEASUREMENTS, READ FROM FILE:   Q = 'MR'
*      INDICES,      WRITE ON FILE:    Q = 'IW'
*      MEASUREMENTS, WRITE ON FILE:    Q = 'MW'
*
************************************************************
*
      SUBROUTINE TRRW(maxdat,ID,IYR,N,Y,NC,IFL,IU,IUO,Q)
      CHARACTER ID*(*),IDA*8,LINE*80,Q*2
      INTEGER NC(maxdat),IY(10),INC(10),FLAG
      REAL Y(maxdat)
      FLAG=-9999
      IF(Q .EQ. 'IR')THEN
      K=1
      IF(ID(1:6) .EQ. '******')K=-1
      ELSEIF(Q .EQ. 'MW' .OR. Q .EQ. 'IW')THEN
      GOTO 5
      ENDIF
    2 N=0
      ISW=0
      IER=0
      IFL=MAX0(IFL,0)
      IFL=IFL+1
      GOTO 1
   91 IER=IER+1
      IF(IER .GT. 25)STOP'Sbr TRRW:  Over 25 text lines'
      IF(IUO .GT. 0)WRITE(IUO,'(/T10,''Title of data: ''
     +,A/)')LINE
      WRITE(*,'(/''Title of data:''/A)')LINE
    1 READ(IU,'(A80)',ERR=99,END=99)LINE
      IF(N .EQ. 0 .AND. LINE(80:80) .EQ. '#')THEN
      BACKSPACE IU
      CALL DATAR(LINE,N,Y,IYR,IU)
      ID=LINE(1:8)
      IF(IUO .GT. 0)WRITE(IUO,'(/T10,''Title of data:  ''
     +,A/)')LINE
      RETURN
      ENDIF
      IF(Q .EQ. 'IR')THEN
      READ(LINE,'(A,T7,I4,10(I4,I3))',ERR=91,END=99)
     +IDA,IYR1,(IY(I),INC(I),I=1,10)
      IDA(7:8)='  '
      ELSEIF(Q .EQ. 'MR')THEN
      READ(LINE,'(A,T9,I4,10I6)',ERR=91,END=99)IDA,
     +IYR1,IY
      ENDIF
      ISW=ISW+1
      L=10
      IF(ISW .EQ. 1)THEN
      ID=IDA
      IYR=IYR1
      IF(IYR .GE. 0)THEN
      L=10-MOD(IYR,10)
      ELSE
      L=ABS(MOD(IYR,10))
      IF(L .EQ. 0)L=10
      ENDIF
      ENDIF
      IF(Q .EQ. 'MR')GOTO 3
      L=11-L
      DO 10 I=L,10
      IF(IY(I) .LT. 9990)THEN
      N=N+1
      Y(N)=FLOAT(IY(I))*.001
      IF(K .GE. 0)NC(N)=INC(I)
      ELSE
      RETURN
      ENDIF
   10 CONTINUE
      GOTO 1
   99 N=-999
      IFL=-999
      RETURN
    3 DO 20 I=1,L
      IF(IY(I) .EQ. -9999)THEN
        FLAG=-9999
        DO 6 J=1,N
          Y(J)=Y(J)*.1
    6   CONTINUE
        RETURN
      ENDIF
      IF(IY(I) .EQ. 999)THEN
        FLAG=-9999
        RETURN
      ENDIF
      N=N+1
      Y(N)=FLOAT(IY(I))*.01
   20 CONTINUE
      GOTO 1
    5 IF(N .LE. 0)RETURN
      IER=0
      J=0
      KYR=IYR
      IF(Q .EQ. 'IW')THEN
*
* ADJUST SERIES IF MAX VALUE IS OVER 9.989
      F=1.
      YX=Y(1)
      DO 60 I=2,N
   60 YX=MAX(Y(I),YX)
   61 IF((YX*F) .GT. 9.98949)THEN
      F=F*.1
      GOTO 61
      ENDIF
      IF(F .NE. 1.)THEN
      WRITE(*,'(''>> Sbr TRRW: Series '',A8,
     +'' max value'',F12.3,
     +'' multiplied by'',F10.6)')ID,YX,F
      ENDIF
      IF(IYR .GE. 0)THEN
      L=MOD(IYR,10)+1
      ELSE
      L=11-ABS(MOD(IYR,10))
      IF(L .EQ. 11)L=1
      ENDIF
   32 DO 30 I=1,10
      IY(I)=9990
   30 INC(I)=0
      DO 40 I=L,10
      J=J+1
      IF(J .GT. N)THEN
      J=-999
      GOTO 41
      ENDIF
      IF(NC(1) .GT. 0)THEN
      INC(I)=NC(J)
      ELSE
      INC(I)=1
      ENDIF
   40 IY(I)=NINT(Y(J)*1000.*F)
   41 WRITE(IU,'(A6,I4,10(I4,I3))')
     +ID(1:6),KYR,(IY(K),INC(K),K=1,10)
      IF(J .LT. 0)RETURN
      KYR=KYR-L+11
      L=1
      GOTO 32
      ELSEIF(Q .EQ. 'MW')THEN
      GOTO 4
      ENDIF
    4 IF(IYR .GE. 0)THEN
      L=10-MOD(IYR,10)
      ELSE
      L=ABS(MOD(IYR,10))
      IF(L .EQ. 0)L=10
      ENDIF
   42 DO 50 I=1,L
      J=J+1
      IF(J .GT. N)THEN
CCC      IY(I)=999
        IY(I)=FLAG
        L=I
        J=-999
        GOTO 51
      ELSE
        IF(FLAG.EQ.999)THEN
          IY(I)=NINT(Y(J)*100.)
          IF(IY(I) .EQ. 999)IY(I)=1000
        ELSEIF(FLAG.EQ.-9999)THEN
          IY(I)=NINT(Y(J)*1000.)
        ENDIF      
      ENDIF
   50 CONTINUE
   51 WRITE(IU,'(A8,I4,10I6)')ID,KYR,(IY(K),K=1,L)
      IF(J .LT. 0)RETURN
      KYR=KYR+L
      L=10
      GOTO 42
      END
*SBR DATAR: READ DATA SERIES PRESERVING SIGNIFICANT DIGITS
*
*  PROGRAMMED MAY 1986 BY
*
*     RICHARD  L  HOLMES
*     TREE-RING LABORATORY
*     UNIVERSITY OF ARIZONA
*     TUCSON, ARIZONA  85721  USA
*
*  MODIFIED BY R L HOLMES 19 SEP 1988
*
*  CALL DATAW (ID,N,X,IYR,IU) WRITE DATA SERIES
*  CALL DATAR (ID,N,X,IYR,IU) READ DATA SERIES
*
* PARAMETERS:
*
* ID:  CHARACTER STRING OF FLEXIBLE LENGTH TO IDENTIFY SERIES
* N:   NUMBER OF DATA VALUES
*        (NOTE: IN CALLING DATAR, IF 'N' IS RETURNED AS A NEGATIVE
*         VALUE, AN END-OF-FILE HAS BEEN REACHED.)
* X:   ARRAY OF DATA VALUES
* IYR: YEAR OF FIRST DATA VALUE
* IU:  UNIT TO WRITE OR READ SERIES
*
      SUBROUTINE DATAR(ID,N,X,IYR,IU)
      CHARACTER LINE*80,FRM*8,EMT*8,S*2,ID*(*)
      REAL X(1)
      S='RD'
      LZ=MIN(LEN(ID),80)
    2 READ(IU,'(A)',ERR=99,END=99)LINE
      IF(LINE(1:3) .NE.'N  ')THEN
      ID(1:LZ)=LINE(1:LZ)
      ENDIF
      READ(LINE,'(2(2X,I8),T23,A)',ERR=2,END=99)N,IYR,FRM
      IF(INDEX(FRM,'(') .EQ. 0 .OR. INDEX(FRM,')')
     + .EQ. 0)GOTO 2
      READ(IU,FRM,ERR=97,END=98)(X(I),I=1,N)
      RETURN
   97 WRITE(*,'(''>> Sbr DATAR:  Error reading data'')')
      RETURN
   98 WRITE(*,'(''>> Sbr DATAR:  
     +Unexpected end reading data'')')
      RETURN
   99 N=-999
      END
