* Last processed by NICE on 12-Jun-2000 15:53:00
* Customized for :  IEEE, LINUX, UNIX, MOTIF, F77
      SUBROUTINE SOLVE(LINE,COMM,ERROR)
      CHARACTER*(*) LINE,COMM
      LOGICAL ERROR
*
      INCLUDE 'setup.inc'
      INCLUDE 'size.inc'
      INCLUDE 'data.inc'
*
      INTEGER NITER,NUM,L,NAZ,NEL,I
      CHARACTER*2 ARG
      REAL*8 EPS, FUNPNL, SIGM
      LOGICAL BAVARD,SIC_PRESENT
      REAL*8 PI,FAC,SFAC,RTS
      PARAMETER (PI=3.14159265358979323846D0)
*
      SFAC = PI/180D0/3600D0
      FAC  = 180D0/PI
      RTS  = 1.D0/SFAC
*
      BAVARD = SIC_PRESENT(0,1)
      EPS = 1D-2
      NITER=15
*
      CALL GET_GOOD (NPT,KP)
      ARG = ' '
      CALL SIC_CH (LINE,0,1,ARG,I,.FALSE.,ERROR)
      IF (ERROR) RETURN
      IF (ARG.EQ.'EL') THEN
         DO I=1,NDATA
            IF (GOOD(I)) THEN
               GOOD(I) = .FALSE.
               KP = KP-1
            ENDIF
         ENDDO
      ELSEIF (ARG.EQ.'AZ') THEN
         DO I=NDATA,NPT
            IF (GOOD(I)) THEN
               GOOD(I) = .FALSE.
               KP = KP-1
            ENDIF
         ENDDO
      ENDIF
      WRITE(6,'(A,I4,A,I4)') ' I-POINTING, ',KP,
     $' good points among ',NPT
      IF (KP.LE.NPF) THEN
         WRITE(6,*) 'E-POINTING,  Not enough data points'
         ERROR = .TRUE.
         RETURN
      ENDIF
*
      SIGCO(3) = SIGCO(3)*COS(COEF(3))
      COEF(3)  = SIN(COEF(3))
      VALPRE(3)= SIN(VALPRE(3))
      IF (NPF.NE.0) THEN
         WRITE(6,*) 'I-POINTING,  Calling FITPNL'
         CALL FITPNL(X,FOBS,NPT,NPF,IPAR,COEF,NITER,NUM,FFIT,
     $   SIGM,SIGCO,EPS,BAVARD,GOOD)
         SIGMA = SIGM
      ELSE
         WRITE(6,*) 'I-POINTING,  No variable parameter'
      ENDIF
*
      DO L=1,NPT
         FFIT(L)=FUNPNL(X(1,L),COEF)
         RESD(L)=FOBS(L)-FFIT(L)
      ENDDO
*
      SIGMA_AZ = 0
      SIGMA_EL = 0
      NAZ = 0
      NEL = 0
      DO L=1,NDATA
         FITAZ(L)=FFIT(L)*RTS
         RESAZ(L)=RESD(L)*RTS
         FITEL(L)=FFIT(NDATA+L)*RTS
         RESEL(L)=RESD(NDATA+L)*RTS
         IF (GOOD(L)) THEN
            NAZ = NAZ+1
            SIGMA_AZ = SIGMA_AZ+RESAZ(L)**2
         ENDIF
         IF (GOOD(NDATA+L)) THEN
            SIGMA_EL = SIGMA_EL+RESEL(L)**2
            NEL = NEL+1
         ENDIF
      ENDDO
      SIGMA = SIGMA_AZ+SIGMA_EL
      SIGMA = SQRT(2.0*SIGMA/MAX(1,(NAZ+NEL-NPF)))
      SIGMA_AZ = SQRT(SIGMA_AZ/MAX(1,(NAZ-NPF)))
      SIGMA_EL = SQRT(SIGMA_EL/MAX(1,(NEL-NPF)))
      WRITE(6,'(1X,A,F8.2,A,F8.2,A,F8.2,A)')
     $'I-POINTING,  Sigma ',SIGMA,'  ( Azimuth',SIGMA_AZ
     $,'  Elevation',SIGMA_EL,' ) '
      FITTED = .TRUE.
      COEF(3)  = ASIN(COEF(3))
      SIGCO(3) = SIGCO(3)/COS(COEF(3))
      VALPRE(3)= ASIN(VALPRE(3))
      END
*<FF>
      SUBROUTINE PRINT (LINE,COMM,ERROR)
      CHARACTER*(*) LINE,COMM
      LOGICAL ERROR
*
      INCLUDE 'setup.inc'
      INCLUDE 'size.inc'
      INCLUDE 'data.inc'
      INTEGER LUN,L,KK,I
      CHARACTER*80 OFILE,NAME
      LOGICAL SIC_PRESENT
      INTEGER LENC
      REAL*8 PI,FAC,SFAC,RTS
      PARAMETER (PI=3.14159265358979323846D0)
      CHARACTER*36 PAR30M(MVAR)
      INTEGER SIGNE(MVAR)
*
      DATA PAR30M/
     $'P1      Zero azimuth (-IAZ or NULA)',
     $'P7      Zero elevation (-IEL-COV or NULE)',
     $'P2      Azimuth collimation (-COH or COL*)',
     $'P4      East Inclination (-MVE)',
     $'P5      North Inclination (MVN)',
     $'P3      Non perpendicularity (NPE)',
     $'REF1    1st order refraction',
     $'REF2    2nd order refraction',
     $'ELES    grav.+exc. coder ele. (sin)',
     $'ELEC    grav.+exc. coder ele. (cos)',
     $'AZES    excen. coder az. (sin)',
     $'AZEC    excen. coder az. (cos)',
     $'incl. coder az. (sin)',
     $'incl. coder az. (cos)',
     $'incl. coder ele. (sin)',
     $'incl. coder ele. (cos)'/
      DATA SIGNE/-1,-1,-1,-1,1,1,4*1,1,1,4*1/
*
      IF (.NOT.FITTED) THEN
         WRITE(6,*) 'E-PRINT,  No fit done'
         ERROR = .TRUE.
         RETURN
      ENDIF
      SFAC = PI/180D0/3600D0
      FAC  = 180D0/PI
      RTS  = 1.D0/SFAC
*
      LUN = 6
      IF (SIC_PRESENT(1,0)) THEN
         CALL SIC_CH (LINE,1,1,OFILE,I,.TRUE.,ERROR)
         IF (ERROR) RETURN
         LUN = 1
         NAME = OFILE
         CALL SIC_PARSEF(NAME,OFILE,' ','.RESULT')
         OPEN (UNIT=LUN,FILE=OFILE,
C+VMS
c     $   CARRIAGECONTROL='LIST',
C-VMS
     $   STATUS='UNKNOWN')
      ENDIF
*
      IF (SIC_PRESENT(2,0)) THEN
         WRITE(LUN,100)
         DO L=1,KVAR
            IF (FREE(L)) THEN
               WRITE(LUN,101) L,VALPRE(L)/SFAC,NOMPAR(L)
            ELSE
               WRITE(LUN,102) L,VALPRE(L)/SFAC,COEF(L)/SFAC,NOMPAR(L)
            ENDIF
         ENDDO
         WRITE(LUN,'(/)')
      ENDIF
      WRITE(LUN,'(1X,A,A)') 'POINTING results for file ',
     $CFILE(1:LENC(CFILE))
      WRITE(LUN,'(1X,A,A)') '   ',STAT_ANT
      WRITE(LUN,'(1X,A,I4,A,I4,A)') '  Number of points used ',KP,
     $', among ',NPT,' data points'
      WRITE(LUN,103)
     $'  Sigma ',SIGMA,'  ( Azimuth',SIGMA_AZ
     $,'  Elevation',SIGMA_EL,' ) '
*
* If no 30-m Format
      IF (.NOT.SIC_PRESENT(3,0)) THEN
         DO L=1,NPF
            KK=IPAR(L)
            WRITE(LUN,104) KK,NOMPAR(KK),COEF(KK)/SFAC,SIGCO(L)/SFAC
         ENDDO
      ELSE
         DO L=1,NPF
            KK=IPAR(L)
            WRITE(LUN,104) KK,PAR30M(KK),SIGNE(KK)*COEF(KK)/SFAC,
     $      SIGCO(L)/SFAC
         ENDDO
      ENDIF
*
      IF (LUN.NE.6) THEN
         IF (NAME.NE.'PRINTER') THEN
            CLOSE(UNIT=LUN)
         ELSE
C+VMS
c            CLOSE(UNIT=LUN,DISP='PRINT/DELETE')
C-VMS
C+UNIX
            CLOSE(UNIT=LUN)
            CALL SYSTEM('lp printer.result; rm printer.result '
     $      //CHAR(0))
C-UNIX
         ENDIF
      ENDIF
      RETURN
*
100   FORMAT(/,' Par.   Previous',23X,'Name')
101   FORMAT(1X,I3,2X,F10.2,'   ** Variable **   ',3X,A,A)
102   FORMAT(1X,I3,2X,F10.2,' Fixed at ',F10.2,3X,A,A)
103   FORMAT(1X,A,F8.2,A,F8.2,A,F8.2,A,/)
104   FORMAT(1X,I4,1X,A,1X,F12.2,' +/- ',F6.2)
      END
