      SUBROUTINE POTM
C     ****
C     ****     CALCULATE HEELIS POTENTIAL IN GEOMAGNETIC COORDINATES
C     ****
      include "params.h"
      PARAMETER(IMAXG=ZIMX, JMAXG=ZJMX)
      PARAMETER(IMAXGP=IMAXG+1, JMAXGP=JMAXG+1, JMAXGP2=JMAXG+2)
      include "cons.h"
      include "consts.h"
      include "dynphi.h"
      include "amie.h"
C     ****
C     ****     WORK SPACE
C     ****
      COMMON/VSCR/IFLAG(IMAXM), DLAT(IMAXM), DLON(IMAXM), RATIO(IMAXM),
     1  PSI(8), PHI(IMAXM,8), IFN(IMAXM), PHIFUN(IMAXM), COLAT(IMAXM),
     2  ALON(IMAXM), SINLAT(IMAXM),
     3  COSLAT(IMAXM), SINLON(IMAXM), COSLON(IMAXM), WK1(IMAXM),
     4  WK2(IMAXM),WK3(IMAXM)
C     ****
C     ****     /TRGM/ CONTAINS MAGNETIC INFO
C     ****
      COMMON/TRGM/RLATM(ZIMXP,ZJMX),RLONM(ZIMXP,ZJMX),
     1  DIPMAG(ZIMXP,ZJMX),DECMAG(ZIMXP,ZJMX),SNDEC(ZIMXP,ZJMX),
     2  CSDEC(ZIMXP,ZJMX),SN2DEC(ZIMXP,ZJMX),SNCSDC(ZIMXP,ZJMX),
     3  RLATMP(ZJMX),RLONMP(ZJMX),DLONS(ZJMX)

      IF (IAMIE .EQ. 1) THEN
	DO 20 IH=1,2
	 SECP = AMAX1(UTSP(1,IH),SECTGCM)
	 SECP = AMIN1(UTSP(2,IH),SECP)
	 DENOMP = UTSP(2,IH) - UTSP(1,IH)
	 P2 = (SECP - UTSP(1,IH))/DENOMP
	 P1 = (UTSP(2,IH) - SECP)/DENOMP
	 J1 = 1
	 J2 = JMAXM/2
	 IF (IH .EQ. 2) THEN
	  J1 = JMAXM/2 + 1
	  J2 = JMAXM
	 ENDIF
	 DO 10 J=J1,J2
	 DO 10 I=1,IMAXMP
   10    PHIHM(I,J) = P1*TIEPOT1(I,J) + P2*TIEPOT2(I,J)
   20   CONTINUE
	GO TO 30
      ENDIF

C     ****
C     ****     LOOP OVER MAGNETIC LATITUDE
C     ****
      DO 1 J = 1,JMAXM
C     ****
C     ****     FILL ARRAYS IFLAG, DLAT, DLON, RATIO
C     ****
        DO 2 I = 1,IMAXM
          IFLAG(I) = 1
          DLAT(I) = YLATM(J)
          DLON(I) = YLONM(I)-DLONS(1)
          RATIO(I) = 1.
    2   CONTINUE
C       ****
C       ****     CALCULATE HEELIS POTENTIAL IN PHIHM(IMAXMP,JMAXM)
C       ****
        IF(ABS(YLATM(J)).GT.C(110)/6.)THEN
          CALL FLWV32(PHIHM(1,J))
        ELSE
          DO 3 I = 1,IMAXM
            PHIHM(I,J) = 0.
    3     CONTINUE
        ENDIF
    1 CONTINUE
C     ****
C     ****    PERIODIC POINTS
C     ****
      DO 4 J = 1,JMAXM
        PHIHM(IMAXMP,J) = PHIHM(1,J)
    4 CONTINUE
   30 CONTINUE
C  Plot of hi-lat potential
C     CALL EZCNTR (PHIHM,IMX0,JMAXM)
      RETURN
      END
