       SUBROUTINE UNLINTOMM(X,Y)
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine UNLINTOMM 
C.LIBRARY:        FOTLIB
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - 25 Aug 97 - original version
C.PURPOSE:        convert (unlinearized) pixels to mm
C.METHOD:         apply linearization formulae
C.SYNTAX:         CALL  UNLINTOMM(X,Y)
C.PARAMETERS:     REAL*8 X,Y  are x,y  pixels (in) and mm (out)
C.RESTRICTIONS:   provisionally contains MECS specific code !!!!
C.NOTES:          calls instrument specific parameter loading
C.FILES:
C.REFERENCES
C
C----------------------------------------------------------------------
C
       DOUBLE PRECISION X,Y,CX,CY,A(10)
       CHARACTER BUFFER*80
       CHARACTER INSTR*4
       REAL EKEV,half,size
       INTEGER NTIMES
       DATA NTIMES /0/
       SAVE NTIMES,EKEV
       INCLUDE 'pixcommon.inc'
       CALL GET_GLOBAL_DEFAULT('INSTRUMENT','NONE',INSTR)
C
       IF(INSTR.EQ.'mecs')THEN
C-->   provisionally place here MECS code, to be moved to MECSLIB routine
C
C-->   need full set of A1..A10 coeffs
       CALL INIT_CORRECT_ME_FAST(A,HALF,SIZE)
C      and need a reference energy (via xasset)
       IF(NTIMES.EQ.0)THEN
         EKEV=3.5
         CALL GET_GLOBAL_DEFAULT('LINENERGY','3.5',BUFFER)
         READ(BUFFER,*)EKEV
         WRITE(*,*)' Linearising at ',EKEV,' keV'
         NTIMES=1
       ENDIF
C      this is the Cusumano correction
C      operating on true coordinates, double precision, no randomization
C      resulting values are in mm
       CX=X-A(1)
       CY=Y-A(2)
       X=A(3)*(1+A(7)/ekev)*CX+A(5)*CX*CX+A(9 )*CX*CX*CX
       Y=A(4)*(1+A(8)/ekev)*CY+A(6)*CY*CY+A(10)*CY*CY*CY
C
C-->   end of MECS code
       ELSE
        WRITE(*,*)' not yet supported for instrument ',INSTR
        CALL Z_EXIT(99)
       ENDIF
C
C      if no pixel size loaded from image define it here
       IF(PIXSIZE.EQ.0.0)THEN
          PIXSIZE=SIZE
          WRITE(*,*)' natural pixel size ',PIXSIZE,'mm'
       ENDIF
C-->   optionally can compute linearized pixels
*      write(*,*)size,half
       write(*,*)' lin pix ',(X+HALF)/PIXSIZE,(Y+HALF)/PIXSIZE
       RETURN
       END
