      PROGRAM ANOFILE
C  THIS PROGRAMME TAKES THE OUTPUT FILE FROM STRUCFAC FOR ANOMALOUS
C  SCATTERING AND GIVES AN OUTPUT FILE WITH FOURIER AMPLITUDES
C  (|F(H)|-|F(-H)|).   THIS CAN BE USED AS INPUT TO FOUR2 TO 
C  CALCULATE A PATTERSON FOR LOCATING THE ANOMALOUS SCATTERERS.
      REAL F(1000),PHI(1000)
      INTEGER H(1000),K(1000)
      CHARACTER ANS*1, FNAME*10
C  GIVE THE NAME OF THE INPUT DATA FILE.
      FNAME='SF1.DAT'
    3 WRITE(6,'('' THE INPUT FILE IS ASSUMED TO BE SF1.DAT '')')
      WRITE(6,'('' DO YOU WANT TO CHANGE THIS?   Y/N   '')')
      READ(5,50)ANS
   50 FORMAT(A1)
      IF(ANS.EQ.'N'.OR.ANS.EQ.'n')GOTO 1
      IF(ANS.EQ.'Y'.OR.ANS.EQ.'y')GOTO 2
      GOTO 3
    2 WRITE(6,'('' READ IN THE FILE NAME <= 10 CHARACTERS] '')')
      READ(5,100)FNAME
  100 FORMAT(A10)
    1 OPEN(UNIT=10,FILE=FNAME)   
      FNAME='SANO.DAT'
   13 WRITE(6,'('' THE OUPUT FILE IS ASSUMED TO BE SANO.DAT '')')
      WRITE(6,'('' DO YOU WANT TO CHANGE THIS?   Y/N   '')')
      READ(5,50)ANS
      IF(ANS.EQ.'N'.OR.ANS.EQ.'n')GOTO 11
      IF(ANS.EQ.'Y'.OR.ANS.EQ.'y')GOTO 12
      GOTO 13
   12 WRITE(6,'('' READ IN THE FILE NAME <= 10 CHARACTERS] '')')
      READ(5,100)FNAME
   11 OPEN(UNIT=11,FILE=FNAME)
C  MODIFY ARRAY F TO CONTAIN ANOMALOUS DIFFERENCE.   SINCE THE 
C  REFLECTIONS ARE IN ADJACENT PAIRS WITH INDICES h AND -h THE 
C  OUTPUT FILE HAS ONLY ONE HALF THE NUMBER OF REFLECTIONS AND 
C  THE STRUCTURE AMPLITUDES ARE THE DIFFERENCES OF NEIGHBOURING 
C  F VALUES ON THE INPUT FILE.
      READ(10,*)NOREF,(H(I),K(I),F(I),PHI(I),I=1,NOREF)
      DO 10 I=1,NOREF-1,2
      F(I)=ABS(F(I)-F(I+1))
   10 CONTINUE
C IDENTIFY ABOUT 25% OF THE LARGEST ANOMALOUS DIFFERENCES AND
C PUT THE REMAINDER EQUAL TO ZERO IN OUTPUT FILE
      DIFMAX=0
      DO 20 I=1,NOREF-1,2
      IF(F(I).GT.DIFMAX)DIFMAX=F(I)
   20 CONTINUE
      DO 30 I=1,19
      XLIM=(1.0-0.05*I)*DIFMAX
      NUM=0
      DO 31 J=1,NOREF-1,2
      IF(F(J).GT.XLIM)NUM=NUM+1
   31 CONTINUE
      IF(FLOAT(NUM)/FLOAT(NOREF).GT.0.125)GOTO 35
   30 CONTINUE
   35 DO 36 I=1,NOREF-1,2
      IF(F(I).LT.XLIM)F(I)=0
   36 CONTINUE
C  READ IN TO FILE FOR INPUT TO FOUR2    
      NOREF2=NOREF/2
      WRITE(11,*)NOREF2,(H(I),K(I),F(I),PHI(I),I=1,NOREF-1,2)   
      STOP
      END


