C     PROGRAM mat_unmix
C
C
C   This program converts raw isotopic data from the WHOI Finnigan MAT252 into 
C   enrichments with resepct to WHOI Reference Gas. Ostermann & Curry, Dec. 1999.
C
C         The program accommodates a-b line differences and crossover
C         mixing within the source using the patterns observed during the
C         series 2 and 3 data.  (Spec nos. 1493 to 4800)
C
C 
C
C
      CHARACTER*40 inFile, outFile
      CHARACTER*1 exp, cline
      CHARACTER*3 hole
      CHARACTER*56 cid
      CHARACTER*24 cdate
      CHARACTER*8 p
C
C
C
C  open input file...
C
         TYPE *, 'Enter name of input file :'
         ACCEPT 1010, inFile
 1010 FORMAT(A40)
C
          OPEN(UNIT=10, FILE=inFile, FORM='FORMATTED',
     +     STATUS='OLD')
C
C  open output file........
C
      TYPE 1000
 1000 format(1x)
      TYPE 1000
      TYPE *,'Enter output filename'
      ACCEPT 1010, outFile
C
      OPEN(UNIT=11,FILE=outFile, STATUS='NEW')
C
C
C       Read input data
    1 READ(10,1001,end=20)cdate,ispec,cid,c13,cpm,xo18,xopm,sa,sv,
     +t,p,exp,cline,hole
 1001 FORMAT(A24,I7,A56,F7.3,3F10.3,2F6.2,F5.1,A8,A1,1x,A1,A3)
C
      IF(ispec .GT. 8231) GOTO 77
C
C    Correct for a-b line differences in spec numbers LE 8231
C    We no longer use this correction as the difference went 
C    Away when we changed capillaries
C
C
      IF(cline .EQ. 'A' .AND. exp .EQ. 'N') THEN
C
C   Constants for the transformation
C
C  O18
         A1 = .0145
         B1 = -.1458
         C1 = .4078
C  C13
         A2 = .0011
         B2 = -.0190
         C2 = .0786
C
C
C
         c13  = c13  +  (A2*(sa)**2+B2*(sa)+C2)
         xo18 = xo18 +  (A1*(sa)**2+B1*(sa)+C1)
      END IF
C
C
C   Correct for crossover mixing
C
C  Old equations used for spsec numbers LE 8231
C         c13  =  c13/((.0089*sv)-.056+1.0)
C         xo18 =  xo18/((.0089*sv)-.056+1.0)
C
C  New numbers used for spec numbers GT 8231
C       We originally thought there to be no mixing at >5 volts
C       This is incorrect. There is actually 3.94% mixing therefor
C       the need to add "dmix" to the equation 
C       All four standards are used for the slope/intercept correction
C       DRO November 1997
C
  77  IF(ispec.GE. 1 .AND. ispec.LE.29454 )THEN
            dmix = -.0394
      END IF
C
      IF(ispec.GE. 29455 .AND. ispec.LE.36649 .AND.cline.EQ.'A')THEN
            dmix = -.0073
      END IF
C
      IF(ispec.GE. 29455 .AND. ispec.LE.36649 .AND.cline.EQ.'B')THEN
            dmix = -.0223
      END IF
C
      IF(ispec.GE. 36650 .AND. ispec.LE.45605 )THEN
            dmix = -.0394
      END IF
C
      IF(ispec.GE. 45606 .AND. ispec.LE.49433 )THEN
            dmix = -.028
      END IF
C
      IF(ispec.GE.49434 .AND. ispec.LE.51708 )THEN
            dmix = -.008
      END IF
C
      IF(ispec.GE.51709 .AND. ispec.LE.55738 )THEN
            dmix = -.015
      END IF
C
      IF(ispec.GE. 55739 )THEN
            dmix = -.020
      END IF


C
         c13  =  c13/((.008*sv)-.041+dmix+1.0)
         xo18 =  xo18/((.008*sv)-.041+dmix+1.0)
C
C
C
      WRITE(11,1001)cdate,ispec,cid,c13,cpm,xo18,xopm,sa,sv,t,p,exp,
     +cline,hole
      GO TO 1
C
   20 close(10)
      close(11)
C
C
      STOP
      END

