* Last processed by NICE on 12-Jun-2000 15:53:00
* Customized for :  IEEE, LINUX, UNIX, MOTIF, F77
      SUBROUTINE RDATA_OP(AZIS,ELES,ERAZ,EREL,TIEM,NP,VALPRE,
     $JANT,STAT,REFRAC)
C----------------------------------------------------------------------
C POINTING
C	Read optical data files.
C	List directed format can be used, except for sign...
C----------------------------------------------------------------------
      INTEGER NP,JANT
      REAL*8 AZIS(*),ELES(*),ERAZ(*),EREL(*),TIEM(*),VALPRE(*)
      CHARACTER*(*) STAT
      LOGICAL REFRAC
*
      REAL*8 AS(3),DS(3),AR(3),DR(3),LST(3),X(3)
      REAL*8 FAC,RTS,PI,TSD,COR1,COR2,FUNPNL
      REAL*8 ALP,ALPR,DEL,DELR,TIEMM,ERA,ERE
      REAL*8 REF(3),TANEL,TANE2,RFRA
*
      REAL IAZ, IEL, COH, COV, MVE, MVN
      REAL NPE, AZEC, AZES, ELEC, ELSI, REFR
      INTEGER IC,L,IANT, I
      EXTERNAL FUNPNL
      CHARACTER*132 CHAIN
      CHARACTER*3 STATION(4)
*
      PI = ACOS(-1.0D0)
      FAC = PI/180.D0
      RTS = 3600.0D0/FAC
      REFRAC = .FALSE.
*
      READ(2,'(A)') CHAIN                ! Antenna and Station
      CALL SIC_UPPER(CHAIN)
*
* Single-dish format
      WRITE(6,*) 'I-POINTING,  Reading optical data file'
      IF (INDEX(CHAIN,'ANT').NE.0) THEN
         READ(2,*)                       ! Plateau de Bure coordinates
         READ(2,*)                       ! IAZ IEL
         READ(2,'(1X,6F9.0)') IAZ, IEL, COH, COV, MVE, MVN
         READ(2,*)                       ! etc...
         READ(2,'(1X,6F9.0)') NPE, AZEC, AZES, ELEC, ELSI, REFR
         READ(2,*)                       ! etc...
         IC=0
         DO L=1,1000
            READ (2,'(A)',ERR=100,END=10) CHAIN
            READ (CHAIN,*,ERR=100) AS,DS,AR,DR,LST
            ALP=DABS(AS(1))+DABS(AS(2)/60.)+DABS(AS(3)/3600.)
            ALP=ALP*FAC
            IF (INDEX(CHAIN(1:14),'-').NE.0) ALP = -ALP
            DEL=DABS(DS(1))+DABS(DS(2)/60.)+DABS(DS(3)/3600.)
            DEL=DEL*FAC
            IF (INDEX(CHAIN(16:28),'-').NE.0) DEL = -DEL
            ALPR=DABS(AR(1))+DABS(AR(2)/60.)+DABS(AR(3)/3600.)
            ALPR=ALPR*FAC
            IF (INDEX(CHAIN(30:42),'-').NE.0) ALPR = -ALPR
            DELR=DABS(DR(1))+DABS(DR(2)/60.)+DABS(DR(3)/3600.)
            DELR=DELR*FAC
            IF (INDEX(CHAIN(44:56),'-').NE.0) DELR = -DELR
            TSD = LST(1)+LST(2)/60.+LST(3)/3600.
            IF (TSD.GT.24) THEN
               TSD=TSD-24
            ELSEIF (TSD.LT.0) THEN
               TSD=TSD+24
            ENDIF
            TIEMM=TSD
*
            IC=IC+1
            AZIS(IC)=ALP
            ELES(IC)=DEL
            TIEM(IC)=TIEMM
            ERA=-(ALP-ALPR)*DCOS(DEL)
            ERE=-(DEL-DELR)
            X(1)=AZIS(IC)
            X(2)=ELES(IC)
            X(3)=0
            COR1=FUNPNL(X,VALPRE)
            X(3)=1
            COR2=FUNPNL(X,VALPRE)
            ERAZ(IC)= (ERA+COR1)*RTS
            EREL(IC)= (ERE+COR2)*RTS
            AZIS(IC) = AZIS(IC)/FAC
            ELES(IC) = ELES(IC)/FAC
         ENDDO
         VALPRE(1) = IAZ/RTS
         VALPRE(2) = (IEL+COV)/RTS
         VALPRE(3) = COH/RTS
         VALPRE(4) = MVE/RTS
         VALPRE(5) = MVN/RTS
         VALPRE(6) = NPE/RTS
*	    VALPRE(7) = REFR
*	    VALPRE(8) = REF2
         VALPRE(9) = ELSI/RTS
         VALPRE(10) = ELEC/RTS
         VALPRE(11) = AZES/RTS
         VALPRE(12) = AZEC/RTS
      ELSE
         READ(2,*)                       ! Plateau de Bure coordinates
         DO I=1,4
            READ(2,'(A)') CHAIN
            STATION(I) = CHAIN(12:14)
            READ(2,*)                    ! IAZ IEL
            READ(2,*)                    ! etc...
            READ(2,*)                    ! etc...
            READ(2,*)                    ! etc...
         ENDDO
         IANT = 0
         DO I=1,4
            IF (STATION(I).NE.'0  ') THEN
               IANT = IANT+1
            ENDIF
         ENDDO
         IF (IANT.EQ.0) THEN
            WRITE(6,*) 'E-POINTING,  No antenna on station'
            RETURN
         ELSEIF (IANT.EQ.1) THEN
            DO I=1,4
               IF (STATION(I).NE.'0  '.AND.STATION(I).NE.'$$$') THEN
                  IF (JANT.NE.0) THEN
                     IF (JANT.NE.I) THEN
                        WRITE(6,'(1X,A,I1,A)')
     $                  'W-POINTING,  Antenna ',JANT,
     $                  'not on station'
                     ENDIF
                  ENDIF
                  JANT = I
               ENDIF
            ENDDO
            WRITE(6,'(1X,A,I1,A,A)') 'I-POINTING,  Selected Antenna ',
     $      JANT,' on station ',STATION(JANT)
         ELSE
            DO I=1,4
               IF (I.EQ.JANT) THEN
                  IF (STATION(I).NE.'0  ') THEN
                     WRITE(6,'(1X,A,I1,A,A)')
     $               'I-POINTING,  Antenna ',
     $               I,' on station ',STATION(I)
                  ELSE
                     WRITE(6,'(1X,A,I1,A,A)')
     $               'E-POINTING,  Antenna ',
     $               I,' not on station '
                     RETURN
                  ENDIF
               ENDIF
            ENDDO
         ENDIF
         STAT = STATION(JANT)
*
         IC=0
         DO L=1,1000
            READ (2,'(A)',ERR=100,END=10) CHAIN
            READ (CHAIN,*,ERR=100) IANT,AS,DS,AR,DR,LST
            IF (L.EQ.1 .AND. CHAIN(74:).NE.' ') REFRAC = .TRUE.
* Select only the right antenna
            IF (IANT.EQ.JANT) THEN
               ALP=DABS(AS(1))+DABS(AS(2)/60.)+DABS(AS(3)/3600.)
               ALP=ALP*FAC
               IF (INDEX(CHAIN(3:16),'-').NE.0) ALP = -ALP
               DEL=DABS(DS(1))+DABS(DS(2)/60.)+DABS(DS(3)/3600.)
               DEL=DEL*FAC
               IF (INDEX(CHAIN(18:30),'-').NE.0) DEL = -DEL
               ALPR=DABS(AR(1))+DABS(AR(2)/60.)+DABS(AR(3)/3600.)
               ALPR=ALPR*FAC
               IF (INDEX(CHAIN(32:44),'-').NE.0) ALPR = -ALPR
               DELR=DABS(DR(1))+DABS(DR(2)/60.)+DABS(DR(3)/3600.)
               DELR=DELR*FAC
               IF (INDEX(CHAIN(46:58),'-').NE.0) DELR = -DELR
               TSD = LST(1)+LST(2)/60.+LST(3)/3600.
               IF (TSD.GT.24) THEN
                  TSD=TSD-24
               ELSEIF (TSD.LT.0) THEN
                  TSD=TSD+24
               ENDIF
               TIEMM=TSD
*
* Refraction
               IF (REFRAC) THEN
                  READ(CHAIN(74:),*,END=101) REF
                  TANEL = 1.0/TAN(DEL)
                  TANE2 = TANEL**2
                  RFRA = - TANEL*
     $            (((REF(3)*TANE2)+REF(2))*TANE2+REF(1))
                  DELR = DELR+RFRA
               ENDIF
*
               IC=IC+1
               ELES(IC)=DEL
               TIEM(IC)=TIEMM
               ERA=-(ALP-ALPR)
* Beware of Azimut determination
               DO WHILE (ERA.LT.-PI)
                  ERA = ERA+2D0*PI
               ENDDO
               DO WHILE (ERA.GT.PI)
                  ERA = ERA-2D0*PI
               ENDDO
               ERA= ERA*COS(DEL)
               ERE=-(DEL-DELR)
               IF (ALP.LE.-PI) THEN
                  ALP = ALP+2D0*PI
               ELSEIF (ALP.GT.PI) THEN
                  ALP = ALP-2D0*PI
               ENDIF
               AZIS(IC)=ALP
               X(1)=AZIS(IC)
               X(2)=ELES(IC)
               X(3)=0
               COR1=FUNPNL(X,VALPRE)
               X(3)=1
               COR2=FUNPNL(X,VALPRE)
               ERAZ(IC)= (ERA+COR1)*RTS
               EREL(IC)= (ERE+COR2)*RTS
               AZIS(IC) = AZIS(IC)/FAC
               ELES(IC) = ELES(IC)/FAC
            ENDIF
         ENDDO
      ENDIF
*
10    NP = IC
      RETURN
*
101   WRITE(6,*) 'E-POINTING,  Inconsistent refraction log'
      RETURN
100   WRITE(6,*) 'E-POINTING,  Error at line ',L,' of data file'
      RETURN
      END
