c    @(#)xwrite.f	1.2
      SUBROUTINE xwrite(FIELD,NUM,TIM,ITYP,K,K1,K2,IPLT)
      include'qg_par.inc'
C HEXADECIMAL OUTPUT ROUTINE FOR PLOT FILES
C ALLOWS EASY FILE TRANSFER FROM CRAY XMP TO FRONT-END VAX TO uVAX
C THIS VERSION WRITTEN AND INSTALLED FEB '86   RFM,LJW
      REAL FIELD(mn0+1),TIM
      REAL SF,FMIN,FMAX,RANGE
      INTEGER NUM,IPLT,K,ITYP
      real k1,k2
      INTEGER HFIELD(MN0+1),NHEX
      DATA NHEX/65535/
C LOCAL VARIABLES AND ARRAYS:
C *** HFIELD WILL HOLD SCALED HEX CONVERSIONS OF FIELD AT LEVEL K
C *** NUM IS THE NUMBER OF ELEMENTS IN FIELD AT LEVEL K
C *** NHEX IS THE LARGEST HEX NUMBER WE WILL STORE (=FFFF)
C *** FMIN AND FMAX ARE THE MAX AND MIN OF THIS FIELD AT LEVEL K
C *** SF IS A SCALE FACTOR, A FUNCTION OF FMAX,FMIN,NHEX
      FMIN=10000000.
      FMAX=-FMIN
         DO 100 I=1,NUM
         FMIN=MIN(FMIN,FIELD(I))
         FMAX=MAX(FMAX,FIELD(I))
100      CONTINUE
      RANGE=FMAX-FMIN
      IF (RANGE.NE.0.) THEN
         SF=NHEX/RANGE
      ELSE
         SF=1.
      ENDIF
         DO 200 I=1,NUM
         HFIELD(I)=INT((SF*(FIELD(I)-FMIN))+0.5)
200      CONTINUE
C
C WRITE OUT HEADER
      WRITE(IPLT,'(I5,2I2,4E15.6)') NUM,ITYP,K,TIM,FMIN,SF,k1
C WRITE OUT HFIELD
      WRITE(IPLT,'(20Z4)') (HFIELD(J),J=1,NUM)
C
C
C
      RETURN
      END
