      SUBROUTINE INVERT(DLAT,DLONG,DATE,HT,LQ,*)
C     SUBROUTINE INVERT.  COMPUTE FX AT SATELLITE IF REQUIRED (IFXS=1).
C     INVERT H'(F) DATA TO OBTAIN N(H) PROFILE USING THE LAMINATION
C     METHOD CONTAINED IN SUBROUTINE XLAM. ALLOW THE SATELLITE POSITION
C     TO VARY IF REQUIRED (LOCSAT=1).
      IMPLICIT REAL*8(A-H,O-Z)
      REAL*4 DLAT,DLONG,HT,DATE,HGT,BN,BE,BV,BT
      REAL*8 HT2I(5),RAD/57.29577951D0/
      COMMON /CALC/ AA(50),BB(50),F(50),HP(50),FH(50),TH(70),DEN(70)
      COMMON /MISC/ DP,FH1,ASUM,BSUM,FA,CPH,X1,X2,GMT1
      COMMON /SWJTCH/ ISAT,LOCSAT,IFXS,NCASES,ICODE(4),N,METHOD,IORD,IP
      COMMON /IO/ IYR,IDY,ITI,LMT,TITLE(40),DIP,FHS
      SAVE
      DLT=DLAT
      DLG=DLONG
      DEN(1)=(F(1)*(F(1)-FH(1)))*12400.00
      F1=F(1)
      KK=2
      IF(IFXS.EQ.0) GO TO 1600
C     IF FXSAT IS CALLED, KK=4 AND THE CALCULATION OF THE FIRST TWO
C     LAMINATIONS IS NOT REPEATED.
C     DEN1 = INITIAL DENSITY AT ORIGINAL SATELLITE POSITION. HT = HEIGHT
      KK=4
      CALL FXSAT(DLAT,DLONG,DATE,LQ,*100)
      AA2=1.0/AA(2)
      FACT=DEN(1)*DEXP(-HT*AA2)
 1600 DEN1=DEN(1)
      FS=F(1)
      NL = N
      M=2
      IFLAG=0
      DO 300 I=KK,N
      DP=0.0
      IF (I .GT. NL) GO TO 300
      IF (F(I) .GE. (F(I-1)+0.001)) GO TO 500
C     TERMINATE IF F(I) IS NOT MONOTONICALLY INCREASING.
      WRITE(6,2) F(I)
      GO TO 100
  500 FA= F(I)
      IF (I .LT. 3 .OR. M .EQ. 3 .OR. LOCSAT .EQ. 0) GO TO 800
C     VARY SATELLITE POSITION AS FOLLOWS: COMPUTE GMT IN SEC AT WHICH
C     F(I) OCCURRED FROM NOMINAL SWEEP RATE OF THE SATELLITE (SUBROUTINE
C     FRTIME). COMPUTE CORRESPONDING SATELLITE POSITION BY INTERPOLATING
C     PREVIOUSLY INPUT WORLD MAP DATA (SUBROUTINE ORBINT). CALCULATE FH
C     AT EACH INPUT H' USING NEW LAT AND LONG IN THE CALL TO FIELD G.
C     M=3 IF FIRST LAMINATION COLLAPSES.
  900 CALL FRTIME(FS,TIME)
      CALL ORBINT(1,TIME,DLAT,DLONG,HGT)
      TH(1)=HGT
      IF(TH(1).GE.TH(2)) GO TO 1000
      WRITE(6,6) TH(1),FA
      WRITE(6,7) DLAT,DLONG,TH(2)
      TH(1)=TH(2)
      M=3
C     FIXED SATELLITE ASSUMED FOR REMAINDER OF ANALYSIS.
      GO TO 800
 1000 IFH=I-1
      DO 1100 L=1,IFH
      CALL FIELDG(DLAT,DLONG,HGT,DATE,10,LQ,BN,BE,BV,BT)
      FH(L)=BT*2.8D-5
      IF (I.EQ.NL.AND. L.EQ.1) DIPS=ATAN2(BV,SQRT(BT**2-BV**2))*RAD
 1100 HGT=TH(L+1)
 1700 DEN(1)=FACT*DEXP(TH(1)*AA2)
C      INITIAL FH(I) BASED UPON 0.1 PER CENT INCREASE IN DENSITY
  800 FH(I)=FA-DEN(I-1)*8.07258870D-5/FA
      IF(TH(I-1).GT.1.D3) GO TO 600
      AAA=1.15*FH(I-1)
      IF ((AAA-FH(I)) .LT. 0.0) FH(I)=AAA
  600 CALL XLAM(M,I,DLAT,DLONG,DATE,LQ,1,*100)
      IF (I .NE. 2 .OR. LOCSAT .EQ. 0) GO TO 300
C     FOR VARIABLE SATELLITE POSITION AND KNOWN FXS, ITERATE ON FIRST
C     LAMINATION TH(2) UNTIL CHANGE IS LESS THAN OR EQUAL TO 0.01 KM.
C     IF CALCULATION DOES NOT CONVERGE AFTER FIVE ITERATIONS, PRINT
C     MESSAGE AND ASSUME FIXED SATELLITE POSITION FOR ANALYSIS.
      AA2=1.0/AA(2)
      FACT=DEN1*DEXP(-HT*AA2)
      IFLAG=IFLAG+1
      DP=0.0
      HT2I(IFLAG)=TH(2)
      IF(IFLAG.GT.1) GO TO 1300
      HT2=TH(2)
      GO TO 900
 1300 HT1=HT2
      HT2=TH(2)
      IF ((HT2-HT1)**2 .LE. 1.D-4) GO TO 300
      IF(IFLAG.NE.5) GO TO 1700
      DLAT=DLT
      DLONG=DLG
      WRITE(6,3)
      WRITE (6,7) DLAT,DLONG,HT
      LOCSAT=0
  300 CONTINUE
      IF (LOCSAT.EQ.0) GO TO 1500
      I=IFLAG-1
      IF(IFXS.EQ.0) WRITE(6,4)I,(HT2I(L),L=1,IFLAG)
      WRITE(6,5) F(NL),TH(1),DLAT,DLONG,DIPS,FH(1)
C     RESET VALUES AT ORIGINAL SATELLITE POSITION.
 1500 FH(1)=FH1
      F(1)=F1
      TH(1)=HT
      DEN(1)=DEN1
      N = NL
      DLAT=DLT
      DLONG=DLG
      RETURN
C     USE RETURN 1 IF PROBLEMS ENCOUNTERED IN  XLAM OR FXSAT, OR IF
C     INPUT DATA IS NOT MONOTONICALLY INCREASING.
  100 RETURN 1
    2 FORMAT(' DATA FOR F=',F6.3,' NOT MONOTONICALLY INCREASING.  PROCES
     FSING TERMINATED.')
    3 FORMAT(' CALCULATION OF FIRST LAMINATION WITH VARIABLE POSITION DI
     FD NOT CONVERGE.')
    4 FORMAT(1x,I2,' ITERATIONS REQUIRED TO COMPUTE INITIAL SLOPE.'/
     F       1H ,'SUCCESSIVE HEIGHTS OF SECOND POINT:',4F8.2)
    5 FORMAT(1x,'SATELLITE POSITION AT FX = ',F6.3,' MHZ.'/'  HT=',F6.1
     F       ,'  LAT=',F7.2,'  LONG=',F7.2,'  DIP=',F7.2,'  FHS=',F5.3)
    6 FORMAT(' SATELLITE (',F6.1,' KM) BELOW FIRST LAMINATION AT',F7.3,
     F  ' MHZ ON X-TRACE.')
    7 FORMAT(' FIXED POSITION ASSUMED:  LAT =',F8.2,'  LONG =',F8.2,
     F  '  HT =',F7.1)
      END
