      SUBROUTINE WETHR(IBLOCK,NSEQW)
C
C SEGMENT OF PROGRAM TO READ AND PROCESS WEATHER DATA
C        IBLOCK=1...READ IN SOLAR DATA
C               2...READ IN OTHER DATA
C               3...MEAN DAY/AVERAGES/INTERPOLATE
C               4...START A NEW HOUR
C               5...STEP WITHIN AN HOUR
C
C        NSEQW =0...CARRIER DATA FROM TAPE20
C               1...BCL INITIAL DATA SEQUENTIAL (CITY DATA), TAPE40
C               2...BCL HOUSE WEATHER DATA (MEASURED) TAPE40
C               .LT.0...CARRIER DATA FROM INPUT
C
C         WHEN NSEQW=2, TAPES 41 AND 19 ARE WRITTEN IF THEY DON'T
C         EXIST. THEY CONTAIN
C         TAPE41...SEQUENTIAL WEATHER DATA
C         TAPE19...HOURLY SOLAR DATA 
C         TAPE45...AVERAGED ATTACHED SPACE TEMPERATURE
C
C WEATHER DATA CHANNEL ASSIGNMENTS
C  1)UNUSED           2)TC-REFERENCE         3)WIND SPEED
C  4)WIND DIRECTION   5)UNUSED               6)SOLAR
C  7)                 8)DRY BULB             9)PRESSURE
C 10)GARAGE TEMP     11)-14)UNUSED          15)OUT DOOR WET BULB
C
C =====================================================================
C
CMDK IWETHR
CMDK OWETHR
CMDK SIMA
CMDK TIMEB
CMDK TSTATC
      REAL WTIME(200),WDATA(15),WDOLD(15)
      REAL ODDB(200),ODHUM(200),WIND(200), TGAR(200)
      REAL NDYRT(12)
      INTEGER ITIME(6)
      LOGICAL FIRST45

C *** FUNCTION DEFINITIONS
C
      TOD()=FLOAT(ITIME(2))+FLOAT(ITIME(3))/60.+
     +           FLOAT(ITIME(4))/3600.+FLOAT(ITIME(5))/36000.
C
      XINTER(X1,Y1,X2,Y2,X)=   Y1 + (Y2-Y1)/(X2-X1) * (X-X1)
C
      DATA NDYRT/31,59,90,120,151,181,212,243,273,304,334,365/
      DATA FIRST45/.TRUE./
      DATA WDATA,WDOLD/30*0.0/
      DATA I45/0/,TX/0./
      DATA BAD/1.E10/, SMALL/1.E-10/
      DATA INPRT1/0/
C
      IDBG=0
      GO TO (1000,2000,3000,4000,5000)IBLOCK
C
C =====================================================================
C *** BLOCK 1: READ IN SOLAR DATA
C
 1000 CONTINUE
C
      IDAYDB=0
      IF(NSEQW.LT.0) THEN
          KM=-NSEQW
C         INSERT CARRIER READ CARDS CODE
          STOP 'WETHR ERROR -.0'
      ELSE IF(NSEQW.EQ.0) THEN
C        INSTALL CARRIER TAPE DATA CODE  FOR READING TAPE20
         STOP 'WETHR ERROR 1.0'
C
      ELSE IF(NSEQW.EQ.1) THEN
C        INSTALL TAPE40 SEQUENTIAL DATA CODE
         STOP 'WETHR ERROR 1.1'
C
      ELSE IF(NSEQW.EQ.2) THEN
C
C             READ/WRITE INITIAL RECORDS
          REWIND 40
          REWIND 41
 1210     CONTINUE
C
C  TAPE40 WAS WRITTEN BY BATTELLE'S BURR-BROWN DATA LOGGER
C  WTIME(2) -
C  ITIME(1-5) -
C  ICODE - NO. OF WDATA ITEMS TO READ( LESS 1 )
C  ITIME(6) -
C  WDATA(3) - WIND SPEED, MILES/HR
C  WDATA(6) - TOTAL SOLAR RADIATION ON HORIZONTAL SURFACE, BTU/HR-FT2
C  WDATA(8) - OUTDOOR DRY-BULB TEMP, F
C  WDATA(10) - GARAGE AIR TEMP, F
C  WDATA(15) - OUTDOOR SPECIFIC HUMIDITY OUTPUT, WET-BULB TEMP INPUT
C  WDATA(6) -
          READ(40,100,IOSTAT=IEOF40) WTIME(2),(ITIME(I),I=1,5),
     +           ICODE,ITIME(6),(WDATA(I),I=2,ICODE)
          IF(IEOF40.LT.0) THEN
                WRITE(60,*) 'TIME = ',WTIME(2),' IEOF40 = ',IEOF40
                STOP 'EOF ON T40 AT 1210'
          ENDIF
          NDYRQ=ITIME(1)
          DO 1211 KQ=1,12
          IF(NDYRQ.LE.NDYRT(KQ))GO TO 1212
 1211     CONTINUE
          STOP 'WETHR:DAY OF YR BAD ON TAPE40'
 1212     IMO=KQ
          DO 1213 I=1,ICODE
          IF(ABS(WDATA(I)).LT.SMALL. OR.
     +       ABS(WDATA(I)).GT.BAD     ) WDATA(I)=SMALL
 1213     CONTINUE
          IF(ISDAY.GT.ITIME(1)) GO TO 1210
          ISDAY=ITIME(1)
          IF(INPRT1.NE.0) WRITE(60,500) WTIME(2),(ITIME(I),I=1,6),
     +        ICODE,(I,I=1,ICODE)
          IDAY41=0
          TIME41=23.9
          WDATA(6)=0.0
          IF(WDATA(15).GT.WDATA(8)) WDATA(15)=WDATA(8)-.1
          IF(ICODE.GE.15) THEN
C                SET SPECIFIC HUMIDITY (USE FUNCS FROM PSY)
            PWSS = FPWS(WDATA(8))
            WSS = FW22(PWSS)
            W = FW35(WDATA(8),WDATA(15),WSS)
          ENDIF
          WDATA(15)=W
          WRITE(41,200) IDAY41,TIME41,(WDATA(I),I=1,ICODE)
          IF(INPRT1.NE.0)WRITE(60,510) IDAY41,TIME41,
     +                         (WDATA(J),J=1,ICODE)
          IDAY41=1
C
C             FILL IN FROM MIDNIGHT TO ITIME(2)
          IF(ITIME(2).NE.0) THEN
                 DO 1220 I=1,ITIME(2)
                    TIME41=FLOAT(I-1)+.00001
                    WRITE(41,200) IDAY41,TIME41,
     +                   (WDATA(J),J=1,ICODE)
                    IF(INPRT1.NE.0)WRITE(60,510) IDAY41,TIME41,
     +                   (WDATA(J),J=1,ICODE)
                    SOLARH(I)=0.0
                    ODDB(I) = WDATA(8)
 1220            CONTINUE
                 ITIM2 = ITIME(2)
                 TIME41=TOD()
                 WRITE(41,200) IDAY41,TIME41,
     +                   (WDATA(J),J=1,ICODE)
          ENDIF
C
C                READ, PROCESS AND WRITE DATA
          LASTDAY=ITIME(1)
          LASTHR=ITIME(2)
          SDB=0.
          SDTDB=0.
          SSF=0.
          SDTSF=0.
C
 1230     CONTINUE
          IF(IEDAY.LT.ITIME(1)) GO TO 1250
          WTIME(1)=WTIME(2)
          DO 1235 I=1,ICODE
           WDOLD(I)=WDATA(I)
 1235     CONTINUE
          READ(40,100,END=1250) WTIME(2),(ITIME(I),I=1,5),
     +        ICODE,ITIME(6),(WDATA(I),I=2,ICODE)
C  FIND MONTH OF YEAR FOR EACH NEW DAY OF YEAR ON WEATHER TAPE
          IF(NDYRQ.NE.ITIME(1))THEN
             NDYRQ=ITIME(1)
             DO 1236 KQ=1,12
             IF(NDYRQ.LE.NDYRT(KQ))GO TO 1237
 1236        CONTINUE
             STOP 'WETHR-1:DAY OF YR BAD ON TAPE40'
 1237        IMO=KQ
             ENDIF
          TIME41=TOD()
          IF(LASTDAY.NE.ITIME(1)) IDAY41=IDAY41+1
          IF(IDAY41.GT.7) THEN
               IDAY41=1
          ENDIF
          IF(WDATA(3).LT.SMALL) WDATA(3)=SMALL
          DO 1240 I=1,ICODE
          IF(ABS(WDATA(I)).GE.BAD) WDATA(I)=WDOLD(I)
          IF(ABS(WDATA(I)).LT.SMALL) WDATA(I)=SMALL
 1240     CONTINUE
          IF(WDATA(3).LT.0.0) WDATA(3)=SMALL
          IF(WDATA(6).LT.5.0) WDATA(6)=0.0
          IF(WDATA(15).GT.WDATA(8)) WDATA(15)=WDATA(8)-.1
          IF(ICODE.GE.15) THEN
C                        SET SPECIFIC HUMIDITY (USE FUNCS FROM PSY)
             PWSS = FPWS(WDATA(8))
             WSS = FW22(PWSS)
             W = FW35(WDATA(8),WDATA(15),WSS)
          ENDIF
          WDATA(15)=W
          WRITE(41,200) IDAY41,TIME41,(WDATA(J),J=1,ICODE)
             IF(INPRT1.NE.0)WRITE(60,510) IDAY41,TIME41,(WDATA(J),
     +        J=1,ICODE)
C
C             SUMS FOR HOURLY  SOLAR AND ODDB 
          DT=WTIME(2)-WTIME(1)
          SDTDB=SDTDB+DT
          SDTSF=SDTSF+DT
          SDB=SDB+(WDATA(8)+WDOLD(8))/2.0 * DT
          SSF=SSF+(WDATA(6)+WDOLD(6))/2.0 *DT
          IF(LASTHR.EQ.ITIME(2)) GO TO 1230
          LASTHR=ITIME(2)
          IT2=ITIME(2)
          ID=IDAY41
          IF(IT2.EQ.0) THEN
             IT2=24
             ID=ID-1
          ENDIF
C  ID IS SEQUENCE DAY OF MONTH(1 TO NDAYM)--NOT CALENDAR DAY OF MONTH!!!
C  IT2 IS HOUR OF DAY
C  IMO IS ACTUAL MONTH OF YEAR
          SOLARH(IT2)=SSF/SDTSF
          ODDB(IT2) = SDB/SDTDB
          IF(IDBG.NE.0)WRITE(60,501)ID,IT2,IMO,SOLARH(IT2)
  501     FORMAT(1X,'WETHR:ID,IT2,IMO,SOLARH= ',3I5,G13.5)
          SDTSF=0.0
          SSF=0.0
          SDB=0.
          SDTDB=0.
          IF(LASTDAY.EQ.ITIME(1)) GO TO 1230
          WRITE(19,533)(SOLARH(IHR),IHR=1,24)
          IF(IDAYDB.EQ.0)THEN
            IDAYDB=1
            SUM=0.
            ITM=0
            DO 1242 IHR = ITIM2,24
            ITM=ITM+1
            SUM=SUM+ODDB(IHR)
 1242       CONTINUE
            TODAVG = SUM / ITM
            WRITE(60,*)' TODAVG (OF ACTUAL DATA) = ',TODAVG
            WRITE(60,*)' 1ST DAY ODDB VALUES BELOW'
            WRITE(60,533)(ODDB(IHR),IHR=1,24)
          ENDIF
          LASTDAY=ITIME(1)
          GO TO 1230
C
C             FINISH OFF LAST DAY (ITIME(2) TO 24.1)
 1250     CONTINUE
          IEDAY=ITIME(1)-1
          IF(IDAY41.EQ.1) STOP 'WETHR: LESS THAN 1 DAY OF DATA'
          IF(TIME41.LT.1.0) GO TO 1290
          DO 1252 I=1,ICODE
          IF(ABS(WDATA(I)).LT.SMALL. OR.
     +       ABS(WDATA(I)).GT.BAD     ) WDATA(I)=SMALL
 1252     CONTINUE
          DO 1255 I=ITIME(2),24
C              USE VALUES FROM PREV DAY FOR SOLARH          
             IF(IDBG.NE.0)WRITE(60,502)IDAY41,I,IMO,SOLARH(I)
  502        FORMAT(1X,'WETHR:IDAY41,I,IMO,SOLARH= ',3I5,G13.5)
             TIME41=FLOAT(I)+1.0001
             ID=IDAY41
             IF(TIME41.GT.17.)WDATA(6)=0.
                 IF(TIME41.GT.24.) THEN
                    TIME41=TIME41-24.
                    ID=IDAY41+1
                 ENDIF
                 WRITE(41,200) ID,TIME41,
     +            (WDATA(J),J=1,ICODE)
          IF(INPRT1.NE.0) WRITE(60,510)ID,TIME41,(WDATA(J),J=1,ICODE)
 1255        CONTINUE
          WRITE(19,533)(SOLARH(IHR),IHR=1,24)
C
 1290     CONTINUE
C
C             SET UP FOR SUBSEQUENT READINGS
 1292     CONTINUE
          REWIND 19
          REWIND 41
          IPOS=2
          DO 1295 I=1,IPOS
           READ(41,200,IOSTAT=IEOF41) IDAY41,TIME41,
     +         (WDATA(J),J=1,ICODE)
           IF(IEOF41.LT.0) STOP 'EOF ON 41 IN WETHR NEAR 1295'
           WTIME(I)=TIME41
           ODDB(I)=WDATA(8)
           WIND(I)=WDATA(3)
           ODHUM(I)=WDATA(15)
           TGAR(I)=WDATA(10)
 1295     CONTINUE
          IF(WTIME(1).GT.WTIME(2)) WTIME(2)=WTIME(2)+24.
          LASTDAY=0
          RETURN
C
          ENDIF
         STOP 'BAD NSEQW IN WETHR BLOCK 1'
C
C =====================================================================
C *** BLOCK 2: READ IN OTHER DATA
C              TEMP,(HUM,WETB),WIND   (DO NOTHING BLOCK!  RDF)
C
 2000 CONTINUE
C
      IF(NSEQW.EQ.0) THEN
C        INSTALL CARRIER TAPE DATA CODE
         STOP 'WETHR ERROR 2.0'
C
      ELSE IF(NSEQW.EQ.1) THEN
C        INSTALL SEQUENTIAL TAPE40 CODE
         STOP 'SETHR ERROR 2.1'
C
      ELSE IF(NSEQW.EQ.2) THEN
         RETURN
C
      ELSE
         STOP 'BAD NSEQW IN WETHR BLOCK 2'
C
      ENDIF
C
C =====================================================================
C *** BLOCK 3: MEAN DAY/AVERAGES/INTERPOLATION (DO NOTHING BLOCK! RDF)
C
 3000 CONTINUE
C
      IF(NSEQW.EQ.0.OR.NSEQW.EQ.1) THEN
C        INSTALL MEAN DAY/AVE AND INTER CODE
         STOP 'WETHR ERROR 3.0 & 3.1'
C
      ELSE IF(NSEQW.EQ.2)THEN
C        TODAVG = ODDBAV(IDAY41,IMO)    NOW CALC ON 1ST CALL TO WETHR!
        RETURN
C        
      ELSE
         STOP 'BAD NSEQW IN WETHR BLOCK 3'
C
      ENDIF

C
C =====================================================================
C *** BLOCK 4: INITIALIZE DATA FOR A NEW HOUR
C
 4000 CONTINUE
C
      IF(NSEQW.EQ.0.OR.NSEQW.EQ.1) THEN
C        SUBTRACT DXXX(I) FROM XXX(I) TO PREPARE TO STEP
C        TO THE FIRST TIME STEP IN THE HOUR
         STOP 'WETHR ERROR 4.0 & 4.1'
C
      ELSE IF(NSEQW.EQ.2) THEN
C
          IF(IDAY41.EQ.LASTDAY) THEN
            IF(FLOAT(IT).LT.WTIME(IPOS)) RETURN
          ELSE
            IF(WTIME(IPOS).GT.25.) THEN
                     DO 4002 I=1,IPOS
                        WTIME(I)=WTIME(I)-24.
 4002                CONTINUE
                     LOCIT=1
                     LASTDAY=IDAY41
                     RETURN
            ENDIF
          ENDIF
C
         DO 4210 I=1,2
            J=IPOS-2+I
            WTIME(I)=WTIME(J)
            IF(IDAY41.NE.LASTDAY) WTIME(I)=WTIME(I)-24.
            ODDB(I)=ODDB(J)
            WIND(I)=WIND(J)
            ODHUM(I)=ODHUM(J)
            TGAR(I)=TGAR(J)
 4210    CONTINUE
         LASTDAY=IDAY41
C
         IPOS=3
         LOCIT=1
 4220    CONTINUE
         READ(41,200,IOSTAT=IEOF41) IDAY41,TIME41,
     +       (WDATA(I),I=1,ICODE)
          IF(IEOF41.LT.0) THEN
             WRITE(60,*) ' '
             WRITE(60,*) 'NO MORE WEATHER DATA',IDAY41,TIME41,IT,TIME
             RETURN
          ENDIF
          WTIME(IPOS)=TIME41
         ODDB(IPOS)=WDATA(8)
         WIND(IPOS)=WDATA(3)
         ODHUM(IPOS)=WDATA(15)
         TGAR(IPOS)=WDATA(10)
         IF(TIME41.GT.FLOAT(IT).OR.IDAY41.NE.LASTDAY) GO TO 4230
         IPOS=IPOS+1
         LASTDAY=IDAY41
         GO TO 4220
 4230    CONTINUE
         IF(IDAY41.NE.LASTDAY) WTIME(IPOS)=WTIME(IPOS)+24.
          RETURN
C
C
          ENDIF
         STOP 'BAD NSEQW IN WETHR BLOCK 4'
C
C =====================================================================
C *** BLOCK 5: STEP WEATHER DATA WITHIN A TIME STEP
C
 5000 CONTINUE
C
      IF(NSEQW.EQ.0.OR.NSEQW.EQ.1) THEN
C        STEP DATA AND CALC WEATHER PARAMETERS
         STOP 'WETHR:ERROR 5.0&5.1'
C
      ELSE IF(NSEQW.EQ.2) THEN
         DO 5205 I=LOCIT,(IPOS-1)
             IF(WTIME(I).LE.TIME.AND.TIME.LE.WTIME(I+1))
     +        GO TO 5210
 5205    CONTINUE
         I=IPOS-1
 5210    CONTINUE
         IF(IPCOOL.EQ.1)THEN
           IF(FIRST45) THEN
             OPEN(45,FILE='TAPE45',STATUS='NEW',IOSTAT=IO45)
             IF(IO45.NE.0)THEN
               WRITE(60,*)' WETHR: CANT OPEN TAPE45 W/ ATTCHD SPC TEMP'
               STOP ' WETHR: CANT OPEN TAPE45 W/ ATTCHD SPC TEMP'
             ENDIF
C  TX  - TIME OF DAY AT NEXT READING OF TPC, HR  (?? WEIRD! RDF)
C  TPC - ATTACHED UNCONDITIONED SPACE TEMP, F
             READ(45,*) TX,TPC
             FIRST45=.FALSE.
           ENDIF
           IF(TIME.GT.TX.AND.I45.EQ.0) THEN
             READ(45,*,END=5300) TX,TPC
           ENDIF
         ENDIF
 5250    TPCOOL=TPC
         LOCIT=I
         WODHUM=XINTER(WTIME(LOCIT),ODHUM(LOCIT),
     +              WTIME(LOCIT+1),ODHUM(LOCIT+1),TIME)
         WSPED=XINTER(WTIME(LOCIT),WIND(LOCIT),
     +              WTIME(LOCIT+1),WIND(LOCIT+1),TIME)
         TODDB=XINTER(WTIME(LOCIT),ODDB(LOCIT),
     +              WTIME(LOCIT+1),ODDB(LOCIT+1),TIME)
         IF(ICODE.GT.9) THEN
            TGARAG=XINTER(WTIME(LOCIT),TGAR(LOCIT),
     +              WTIME(LOCIT+1),TGAR(LOCIT+1),TIME)
         ELSE
            TGARAG=TODDB
         ENDIF
          RETURN
 5300    I45=1
         GO TO 5250

C
      ELSE
         STOP 'BAD NSEQW IN WETHR BLOCK 5'
C
      ENDIF
C ***
  100 FORMAT(F12.5,I4,5I3,I5/(8E10.4))
  110 FORMAT(7G10.4)
  200 FORMAT(I2,G14.7,10(E11.4))
  500 FORMAT('1MEASURED WEATHER DATA',F12.5,7I5/
     +    ' DAY       HOUR',10(' ---CH ',I2,'--') )
  510 FORMAT(I4,F11.5,(10G11.3))
C
  533 FORMAT(1X,6G13.5/1X,6G13.5/1X,6G13.5/1X,6G13.5)
      END
