        SUBROUTINE ZOFIT1(N,CARPOS,ORDER,SAMPLE,DELTAW,COEF,FIT,ISTAT)
*
*  Module number: 13.9.3.1
*
*  Module name: ZOFIT1
*
*  Keyphrase:
*  ----------
*       Fit HRS wavelength offsets
*  Description:
*  ------------
*       This routine does a least squares fit to wavelengths offsets
*       giving coefficients c0,c1,c2,c3 such that:
*               order*deltaw = c0 + c1*carpos + c2*order + c3*sample
*
*  FORTRAN name: zofit1
*
*  Keywords of accessed files and tables:
*  --------------------------------------
*       none
*
*  Subroutines Called:
*  -------------------
*  CDBS:
*       minmax, dmnmax, lfit3
*  SDAS:
*       umsput
*  Others:
*
*
*  History:
*  --------
*  Version      Date        Author          Description
*    1          Oct. 87   D. Lindler    Designed and coded
*-------------------------------------------------------------------------------
C
C INPUT PARAMETERS
C
C       N - number of values to fit
C       CARPOS - VECTOR OF CARROUSEL POSITIONS (INTEGER)
C       ORDER - VECTOR OF ORDER NUMBERS (INTEGER)
C       SAMPLE - VECTOR OF SAMPLE POSITIONS (REAL*8)
C       DELTAW - VECTOR OF WAVELENGTH OFFSETS (REAL*8)
C OUTPUT PARAMETERS
C
C       COEF - VECTOR OF 4 COEFFICIENTS (REAL*8)
C       FIT - VECTOR OF FITTED DELTAW'S (REAL*8)
C       ISTAT - ERROR STATUS
C
C-------------------------------------------------------------------------------
C     INCLUDE FILE FOR THE IRAF77 FORTRAN INTERFACE TO THE IRAF VOS
C
C
C     FILE I/O ACCESS MODES
C
      INTEGER   RDONLY
      PARAMETER (RDONLY = 1)
      INTEGER   RDWRIT
      PARAMETER (RDWRIT = 2)
      INTEGER   WRONLY
      PARAMETER (WRONLY = 3)
      INTEGER   APPEND
      PARAMETER (APPEND = 4)
C
C     CODES FOR DATA TYPES
C
      INTEGER   TYBOOL
      PARAMETER (TYBOOL = 1)
      INTEGER   TYCHAR
      PARAMETER (TYCHAR = 2)
      INTEGER   TYINT
      PARAMETER (TYINT = 4)
      INTEGER   TYREAL
      PARAMETER (TYREAL = 6)
      INTEGER   TYDOUB
      PARAMETER (TYDOUB = 7)
C
C     UMSPUT DESTINATIONS -- CB, DAO, 4-SEP-87
C
      INTEGER STDOUT
      PARAMETER (STDOUT = 1)
      INTEGER STDERR
      PARAMETER (STDERR = 2)
C
C     UHDAS HEADER PARM TYPES -- CB, DAO, 5-SEP-87
C
      INTEGER GENHDR
      PARAMETER (GENHDR = 0)
      INTEGER IMSPEC
      PARAMETER (IMSPEC = 1)
C
C     THIS SECTION IS FOR PARAMETERS RELEVANT TO TABLE I/O.
C
C     THESE MAY BE SET BY UTPPTI AND/OR READ BY UTPGTI:
C
C                                       LENGTH OF ROW (UNIT = SIZE OF REAL)
      INTEGER   TBRLEN
      PARAMETER (TBRLEN = 1)
C                                       INCREASE ROW LENGTH
      INTEGER   TBIRLN
      PARAMETER (TBIRLN = 2)
C                                       NUMBER OF ROWS TO ALLOCATE
      INTEGER   TBALLR
      PARAMETER (TBALLR = 3)
C                                       INCREASE ALLOC NUM OF ROWS
      INTEGER   TBIALR
      PARAMETER (TBIALR = 4)
C                                       WHICH TYPE OF TABLE? (ROW OR COLUMN)
      INTEGER   TBWTYP
      PARAMETER (TBWTYP = 5)
C                                       MAXIMUM NUMBER OF USER PARAMETERS
      INTEGER   TBMXPR
      PARAMETER (TBMXPR = 6)
C                                       MAXIMUM NUMBER OF COLUMNS
      INTEGER   TBMXCL
      PARAMETER (TBMXCL = 7)
C                                       TYPE = ROW-ORDERED TABLE
      INTEGER   TBTYPR
      PARAMETER (TBTYPR = 11)
C                                       TYPE = COLUMN-ORDERED TABLE
      INTEGER   TBTYPC
      PARAMETER (TBTYPC = 12)
C
C     THESE MAY BE READ BY UTPGTI BUT MAY NOT BE SET:
C
C                                       NUMBER OF ROWS WRITTEN TO
      INTEGER   TBNROW
      PARAMETER (TBNROW = 21)
C
C     END IRAF77.INC
        INTEGER N
        DOUBLE PRECISION SAMPLE(N),DELTAW(N),COEF(4),FIT(N)
        INTEGER CARPOS(N),ORDER(N),ISTAT
C
C LOCAL VARIABLES
C
        DOUBLE PRECISION DCARPS(2000),DORDER(2000),MW(2000),SIG(2000)
        DOUBLE PRECISION COVAR(4,4),CHISQ
        INTEGER LISTA(4),I,MFIT
        INTEGER CMIN,CMAX,MINPOS,MAXPOS,M1,M2
        DOUBLE PRECISION SMIN,SMAX
C
C  DATA DECLARATIONS
C
        EXTERNAL ZOFUNC
        DATA SIG/2000*1.0/
C-------------------------------------------------------------------------------
C
C INITIALIZATION
C
        DO 10 I=1,4
10      COEF(I)=0.0
        MFIT=1
        LISTA(1)=1
C
C CAN WE FIT ON CARROUSEL POSITION
C
        CALL MINMAX(CARPOS,N,CMIN,MINPOS,CMAX,MAXPOS)
        IF(CMIN.NE.CMAX)THEN
                MFIT=MFIT+1
                LISTA(MFIT)=2
        ENDIF
C
C CAN WE FIT ON SPECTRAL ORDER
C
        CALL MINMAX(ORDER,N,M1,MINPOS,M2,MAXPOS)
        IF(M1.NE.M2)THEN
                MFIT=MFIT+1
                LISTA(MFIT)=3
        ENDIF
C
C CAN WE FIT ON SAMPLE POSITION
C
        CALL DMNMAX(SAMPLE,N,SMIN,MINPOS,SMAX,MAXPOS)
        IF(SMIN.NE.SMAX)THEN
                MFIT=MFIT+1
                LISTA(MFIT)=4
        ENDIF
C
C CONVERT INPUTS TO REAL*8 AND COMPUTE ORDER*DELTAW
C
        DO 100 I=1,N
                DCARPS(I)=CARPOS(I)
                DORDER(I)=ORDER(I)
                MW(I)=ORDER(I)*DELTAW(I)
100     CONTINUE
C
C DO WE HAVE ENOUGH DATA
C
        IF(N.LE.MFIT)THEN
                CALL UMSPUT('Too few data points to fit',
     *                  STDOUT+STDERR,0,ISTAT)
                ISTAT=1
                GO TO 999
        ENDIF
C
C FIT DATA
C
        CALL LFIT3(DCARPS,DORDER,SAMPLE,MW,SIG,N,COEF,4,LISTA,MFIT,
     *          COVAR,4,CHISQ,ZOFUNC,ISTAT)
        IF(ISTAT.NE.0)THEN
                GO TO 999
        ENDIF
C
C COMPUTE FITTED VALUES
C
        DO 200 I=1,N
                FIT(I)=COEF(1) + COEF(2)*DCARPS(I) + COEF(3)*DORDER(I) +
     *                          COEF(4)*SAMPLE(I)
                FIT(I)=FIT(I)/DORDER(I)
200     CONTINUE
        ISTAT=0
999     RETURN
        END
