      SUBROUTINE PRELCH(NPREL,*)

      include "prelch_62.comments"

      include "params.inc"
      include "ui1b.inc"
      include "uiinst.inc"
      include "fdbdir.inc"
      include "contrl.inc"
      include "opshin.inc"
      include "date_cmn.inc"
      include "day1cals.inc"

      INTEGER*4 REC1B(180),POSD1B
      INTEGER*4 RCAL(12,3)
      BYTE B_REC1B(720)
c
      real*4 rirr12(12),rirr23(12) !,acf(13)
C
      EQUIVALENCE (RCAL(1,1),REC1B(3))
      EQUIVALENCE (B_REC1B(1),REC1B(1))
C
C  NIMBUS-7 ACF/F2 UNIT AND INTER-RANGE RATIO INFO
C
      CHARACTER*2 N07,N09,N11,N14,N16,N17,N18,N19
      DATA N07/'07'/,N09/'09'/,N11/'11'/,N14/'14'/,N16/'16'/,N17/'17'/
     +    ,N18/'18'/,N19/'19'/
      DATA LUACF7/33/, R1BYR2/52.42/, R2BYR3/80.50/
C
C  ACF/F2 CODE NEEDED FOR NIMBUS-7 !!!!!
C
C  NIMBUS-7 PRELAUNCH CALIBRATION, DAY ONE SOLAR FLUX
C
      DIMENSION PCALN7(13), FVALN7(13)
      DATA PCALN7/5.102E-5,5.506E-5,5.978E-5,6.157E-5,6.274E-5,
     1            6.352E-5,6.366E-5,6.347E-5,6.268E-5,6.188E-5,
     2            5.203E-5,4.463E-5,1.853E-3/

C
C  CHECK FOR LEVEL-2 INPUT WITH NEW CALIBRATION
C
      IF(IOPTS(11)) THEN
        WRITE(6,7777)
 7777 FORMAT(1x,'OPTION NOT SUPPORTED')
        STOP
      ELSE
C
C  READ IN PRELAUNCH CALIBRATION
C
    5 continue
c-lkh  Revised for Big-Small endian byte swap
c-lkh      read (lufdb,iostat=ios,end=900,err=900) B_REC1B
      call rdswap(lufdb,b_rec1b,rec_id,*900)
      if(rec1b(1).ne.525) go to 5
        CALL FNMOVE (CAL(1,13),4,REC1B(2))
        DO 10 I = 1,3
        DO 10 J = 1,12
          CALL FNMOVE (CAL(I,J),4,RCAL(J,I))
   10   CONTINUE
C
C  READ IN DAY ONE SOLAR FLUX
C
      NREL = NREL + 1
      NREC = NPPREC(NREL,RNODIR(ID1B),MXNREC,IREFDB)
c-lkh      read (lufdb,iostat=ios,end=900,err=900) B_REC1B
      call rdswap(lufdb,b_rec1b,rec_id,*900)
      IF (REC1B(1).NE.565) GO TO 910
      CALL FNMOVE (FVALUE(1),52,REC1B(2))
C
C  READ IN INTER-RANGE RATIOS
C
      NREL = NREL + 1
      NREC = NPPREC(NREL,RNODIR(ID1B),MXNREC,IREFDB)
c-lkh      read (lufdb,iostat=ios,end=900,err=900) B_REC1B
      call rdswap(lufdb,b_rec1b,rec_id,*900)
      IF (REC1B(1).NE.575) GO TO 910
      CALL FNMOVE (rirr12(1),48,REC1B(3))
      CALL FNMOVE (rirr23(1),48,REC1B(16))
C
C  READ IN ALBEDO CORRECTION FACTORS
C
      NREL = NREL + 1
      NREC = NPPREC(NREL,RNODIR(ID1B),MXNREC,IREFDB)
c-lkh      read (lufdb,iostat=ios,end=900,err=900) B_REC1B
      call rdswap(lufdb,b_rec1b,rec_id,*900)
      IF (REC1B(1).NE.585) GO TO 910
      CALL FNMOVE (ACF(1),52,REC1B(2))

C
C  Use ACF Table and IRR Table to replace those in 1b data set.
C
      CALL get_acf(date_i4,acf)
C
C-H Add optional input of day-1 solar flux, radiance calcons,
c-H irr12, irr23 and offset from namelist with IOPTS(20)=.TRUE.
      if (.NOT. IOPTS(20)) then
      IF ( SCNO .EQ. N09 )  THEN
         call rcal_n09(date_i4,rcal_day1)
         call flux_n09(date_i4,flux_day1)
         call IRR_N09(date_i4,rirr12_value,rirr23_day1)
         call offset_n09(date_i4)
      ENDIF
      IF ( SCNO .EQ. N11 )  THEN
         call rcal_n11(date_i4,rcal_day1)
         call flux_n11(date_i4,flux_day1)
         call IRR_N11(date_i4,rirr12_value,rirr23_day1)
         call offset_n11(date_i4)
      ENDIF
      IF ( SCNO .EQ. N14 )  THEN
         call zigzag_acf(date_i4,acf)
         call rcal_n14(date_i4,rcal_day1)
         call flux_n14(date_i4,flux_day1)
         call IRR_N14(date_i4,rirr12_value,rirr23_day1)
         call offset_n14(date_i4)
      ENDIF
      IF ( SCNO .EQ. N16 )  THEN
         call rcal_n16(date_i4,rcal_day1)
         call flux_n16(date_i4,flux_day1)
         call IRR_N16(date_i4,rirr12_value,rirr23_day1)
         call offset_n16(date_i4)
      ENDIF
      IF ( SCNO .EQ. N17 )  THEN
         call rcal_n17(date_i4,rcal_day1)
         call flux_n17(date_i4,flux_day1)
         call IRR_N17(date_i4,rirr12_value,rirr23_day1)
         call offset_n17(date_i4)
      ENDIF
      IF ( SCNO .EQ. N18 )  THEN
         call rcal_n18(date_i4,rcal_day1)
         call flux_n18(date_i4,flux_day1)
         call IRR_N18(date_i4,rirr12_value,rirr23_day1)
         call offset_n18(date_i4)
      ENDIF
      IF ( SCNO .EQ. N19 )  THEN
         call rcal_n19(date_i4,rcal_day1)
         call flux_n19(date_i4,flux_day1)
         call IRR_N19(date_i4,rirr12_value,rirr23_day1)
         call offset_n19(date_i4)
      ENDIF
      endif
C-H   input magnitude of hysteresis
      IF ( SCNO .EQ. N09 ) call hyst_n09(date_i4)
      IF ( SCNO .EQ. N11 ) call hyst_n11(date_i4)
      IF ( SCNO .EQ. N14 ) call hyst_n14(date_i4)
      IF ( SCNO .EQ. N16 ) call hyst_n16(date_i4)
      IF ( SCNO .EQ. N17 ) call hyst_n17(date_i4)
      IF ( SCNO .EQ. N18 ) call hyst_n18(date_i4)
      IF ( SCNO .EQ. N19 ) call hyst_n19(date_i4)
C-H
      call irr_drift(date_i4,rirr12_value,rirr23_day1,rirr12,rirr23)
      do k=1,12
         cal(2,k)=rcal_day1(k)
         fvalue(k)=flux_day1(k)
      end do
      cal(1,13)=rcal_day1(13)
      fvalue(13)=flux_day1(13)

C  PRINT ICAC INFORMATION
C
      IF(LPRINT(10)) WRITE(6,3000) FVALUE,rirr12,rirr23,ACF,CAL
C
C  MAKE INTER-RANGE RATIO AND ALBEDO CORRN FACTOR ADJUSTMENT
C
      DO 200 I=1,12
        CAL(2,I) = CAL(2,I) * ACF(I)
        CAL(1,I) = CAL(2,I) / rirr12(I)
        CAL(3,I) = CAL(2,I) * rirr23(I)
  200 CONTINUE
      CAL(1,13) = CAL(1,13) * ACF(13)
C
C  PRINT ICAC INFORMATION
C
      IF(LPRINT(10)) WRITE(6,3000) FVALUE,rirr12,rirr23,ACF,CAL
 3000 FORMAT(/T30,'INPUT INFO FROM ICAC'//1X,'FVALUE -',
     1     13F9.3//1X,'IRR 2/1 -',12F10.3//1X,'IRR 3/2 -',
     2     12F10.3//1X,'ACF -',13F9.5//1X,'FINAL CALIBRATION VALUES -'//
     3     13(1X,3E15.4/)//)
      ENDIF
      RETURN
900   WRITE (6,999)
999   FORMAT(//,'  ***-ERROR-(PRELCH)-*** - ERROR IN READING ICAC'/)
      RETURN 1
C
910   WRITE (6,919)
919   FORMAT(//,'  ***-ERROR-(PRELCH)-*** - RECORD ID ERROR IN ICAC'/)
      RETURN 1
C
      END
