C Copyright(c) 1998, Space Science and Engineering Center, UW-Madison
C Refer to "McIDAS Software Acquisition and Distribution Policies"
C in the file  mcidas/data/license.txt

C *** $Id: nvxps.dlm,v 1.12 1998/10/13 20:25:54 dglo Exp $ ***

      FUNCTION NVXINI(IFUNC,IPARMS)
      DIMENSION IPARMS(*)
      REAL*8 DRAD,DECC
      CHARACTER*4 CLIT
      COMMON/PSCOM/XROW,XCOL,XLAT1,XSPACE,XQLON,XBLAT,ITYPE,XPOLE,FAC
      COMMON/PSCOM/IHEM,IWEST
      DATA RAD/.01745329/
      IF (IFUNC.EQ.1) THEN
         IF (IPARMS(1).NE.LIT('PS  ')) THEN
            NVXINI=-1
            RETURN
         ENDIF
         ITYPE=1
         XROW=IPARMS(2)
         XCOL=IPARMS(3)
         IPOLE=IPARMS(11)
         IF(IPOLE.EQ.0) IPOLE=900000
         IHEM=1
         IF(IPOLE.LT.0) IHEM=-1
         XPOLE=FLALO(IPOLE)
         XLAT1=FLALO(IPOLE-IPARMS(4))*RAD
         XSPACE=IPARMS(5)/1000.
         XQLON=FLALO(IPARMS(6))
         DRAD=IPARMS(7)/1000.D0
         R=DRAD
         DECC=IPARMS(8)/1.D6
         IWEST=IPARMS(10)
         IF(IWEST.GE.0) IWEST=1
         CALL LLOPT(DRAD,DECC,IWEST,IPARMS(9))
         XBLAT=R*SIN(XLAT1)/(XSPACE*TAN(XLAT1*.5))
         SCLAT1=(90.-IHEM*FLALO(IPARMS(4)))*RAD
C        FAC=SIN(SCLAT1)/TAN(.5*SCLAT1)
         FAC=1
      ELSE IF (IFUNC.EQ.2) THEN
         IF(INDEX(CLIT(IPARMS(1)),'XY').NE.0) ITYPE=1
         IF(INDEX(CLIT(IPARMS(1)),'LL').NE.0) ITYPE=2
      ENDIF
      NVXINI=0
      RETURN
      END
      FUNCTION NVXSAE(XLIN,XELE,XDUM,XLAT,XLON,Z)
      COMMON/PSCOM/XROW,XCOL,XLAT1,XSPACE,XQLON,XBLAT,ITYPE,XPOLE,FAC
      COMMON/PSCOM/IHEM,IWEST
      DATA RAD/.01745329/
      XLDIF=IHEM*(XLIN-XROW)/XBLAT
      XEDIF=(XCOL-XELE)/XBLAT
      XRLON=0
      IF(.NOT.(XLDIF.EQ.0.AND.XEDIF.EQ.0)) XRLON=ATAN2(XEDIF,XLDIF)
      XLON=IWEST*XRLON/RAD+XQLON
      IF(XLON.GT.180. ) XLON=XLON-360.
      IF(XLON.LT.-180.) XLON=XLON+360.
C***
      RADIUS=SQRT(XLDIF*XLDIF+XEDIF*XEDIF)
      IF (ABS(RADIUS).LT.1.E-10) THEN
         XLAT=IHEM*90
      ELSE
         XLAT=IHEM*(90.-2*ATAN(EXP(ALOG(RADIUS/FAC)))/RAD)
      ENDIF
C***
C     IF(XPOLE*XLAT.LT.0.0) GO TO 900
      IF(ITYPE.EQ.1) THEN
         YLAT=XLAT
         YLON=XLON
         CALL LLCART(YLAT,YLON,XLAT,XLON,Z)
      ENDIF
      NVXSAE=0
      RETURN
C 900 CONTINUE
C     NVXSAE=-1
C     RETURN
      END
      FUNCTION NVXEAS(ZLAT,ZLON,Z,XLIN,XELE,XDUM)
      COMMON/PSCOM/XROW,XCOL,XLAT1,XSPACE,XQLON,XBLAT,ITYPE,XPOLE,FAC
      COMMON/PSCOM/IHEM,IWEST
      DATA RAD/.01745329/
      XLAT=ZLAT
      XLON=ZLON
      IF(ITYPE.EQ.1) THEN
         X=XLAT
         Y=XLON
         CALL CARTLL(X,Y,Z,XLAT,XLON)
      ENDIF
C     IF(XLAT*XPOLE.LT.0) GO TO 900
      XRLON=IHEM*(XLON-XQLON)
      IF(XRLON.GT.180. ) XRLON=XRLON-360.
      IF(XRLON.LT.-180.) XRLON=XRLON+360.
      XRLON=IWEST*XRLON*RAD
      XCLAT=(XPOLE-XLAT)*RAD*.5
      XRLAT=XBLAT*TAN(XCLAT)
      XLIN=XRLAT*COS(XRLON)+XROW
      XELE=-XRLAT*SIN(XRLON)+XCOL
      NVXEAS=0
      RETURN
C 900 CONTINUE
C     NVXEAS=-1
C     RETURN
      END
      FUNCTION NVXOPT(IFUNC,XIN,XOUT)
      COMMON/PSCOM/XROW,XCOL,XLAT1,XSPACE,XQLON,XBLAT,ITYPE,XPOLE,FAC
      COMMON/PSCOM/IHEM,IWEST
      REAL*4 XIN(*),XOUT(*)
      CHARACTER*4 CLIT,CFUNC
      DATA RAD/.01745329/
C
C IFUNC= 'SPOS'    SUBSATELLITE LAT/LON
C
C        XIN - NOT USED
C        XOUT - 1. STANDARD LATITUDE
C             - 2. NORMAL LONGITUDE
C
C IFUNC= 'ORAD'  OBLATE RADIUS
C
C        XIN - LATITUDE
C        XOUT - RADIUS IN KM
C
      CFUNC=CLIT(IFUNC)
      NVXOPT=0
      IF(CFUNC.EQ.'SPOS') THEN
         XOUT(1)=XPOLE-XLAT1/RAD
         XOUT(2)=XQLON
      ELSE IF(CFUNC.EQ.'ORAD') THEN
         CALL LLOBL(XIN,XOUT)
      ELSE
         NVXOPT=1
      ENDIF
      RETURN
      END
