      SUBROUTINE SOLRLD(STM,NDYRI,ICC,KM,IDAY,DST,NSEQW,IDBG)
C     PROGRAM TO CALCULATE MONTHLY SOLAR LOADING ON RES. STRUCTURES 
C  STM      TIME ZONE (SEE ETIME SUBPROGRAM)
C  NDYRI    DAY OF YEAR 
C            =>0, DAY OF YR AT MID POINT IN RUN(USED FOR VALIDATION)
C            =-1, DAY OF YR WILL BE 21ST OF CURRENT MONTH DEFINED IN 
C             MTHS ARRAY. 
C            =-2, DAY OF YR WILL BE CALC FROM MONTH AND DAY ON TAPE19
C  ICC    =1  CLOUD COVER DATA USED TO COMPUTE SOLAR
C         =0  TOTAL SOLAR RADIATION SUPPLIED IN  CC  ARRAY
C  KM       MONTH OF SOLAR CALCULATION
C  IDAY    DAY OF THIS SIMULATION CASE (1....)
C  DST    =0 FOR STANDARD TIME
C         =1 FOR DAYLIGHT SAVINGS TIME
C 
C  INPUTS IN COMMON BLOCKS ENCBLK,WNDBLK,ROOF ARE DEFINED IN READIN 
C 
C  INPUTS IN COMMON BLOCKS: 
C  /SURFAR/  VALUES CALC IN CONSLD SUBPROGRAM:  
C     AWALLA  SURFACE AREA, SQ FT 
C     AWAWA   WINDOW AREA, SQ FT
C     RAREA   TOTAL ROOF AREA OVER ATTICS 1 AND 2, SQ FT
C 
C  /SHDF/  SHDFCT  SHADING FACTOR(0.-1.). INDICES ARE 
C                  (COMPASS DIR, OUTSIDE WALL,   ZONE NO.,HR OF DAY)
C                              , WINDOWS IN WALL, 
C                              , ROOF,
C           COMPASS DIR: 1-E,2-S,3-S!!
C         SHDFCT IS ONLY IMPLEMENTED FOR ONE ZONE AS YET.  MAKE
C         CHANGES IN 3TH DIMENSION LATER IN 4 PLCS WHEN IMPLEMENTED
C         FOR MORE THAN ONE ZONE.
C 
C  /NCR/   IFXDS   COMPASS DIRECTIONS ALWAYS SHADED (1-4) 
C 
C  /SIMA/  REFLG   GROUND REFLECTANCE FOR EACH MONTH (0.-1.)
C
C  /CITINF/ DNLAT  DEG. N. LATTITUDE
C           DLONG  DEG. LONGITUDE 
C           CLEAR  CLEARNESS NO.  (USED IF ICC=1) 
C 
C -- OUTPUTS - - - - - - - - - - - -
C  SOLARH - 24 VALUES OF TOTAL HORIZONTAL SOLAR RADIATION FOR THE DAY
C  DATOUT (I,IHOUR) 
C    I=1  SOLAR LOADING ON ROOF OVER ATTIC NO. 1
C      2  SOLAR LOADING ON ROOF OVER ATTIC NO. 2
C      3 TO (NROOMS+2) TOTAL SOLAR LOAD TRANSMITTED THROUGH WINDOWS 
C                      INTO ZONES NRMA(J) WHERE J=I-2 
C      (NROOMS+3) TO (NROOMS+3+4)SOLAR LOADING ON OUTER SURFACE OF
C                      OUTSIDE WALLS FACING EACH ID COMPASS DIRECTION
C 
C 
C - - - - CONSTANTS 
C   IF IWALS = 0  NIWL = 2 * NROOMS(FOR SOLAR THRU WINDOWS AND ON WALLS)
C                        + 2(FOR 2 ATTIC ROOFS) ... = 8
C   IF IWALS = 1  NIWL = NROOMS(FOR SOLAR THRU WINDOWS) + 
C                        2(FOR 2 ATTIC ROOFS) +
C                        SUM OF NWALLA(I) FOR ALL ZONES I                  
CMDK NZN
CMDK NIWL
CMDK NKONST
CMDK NRF
CMDK NWL
CMDK NWN
CMDK NWZN
CMDK NZW
CMDK NCR
CMDK CITINF
CMDK CNSTRK
CMDK ENCBK1
CMDK ENCBLK
CMDK IWLS
CMDK IZZQ
CMDK LDPOUT
CMDK NIJB
CMDK ROOF
CMDK SHDF
CMDK SIMA
CMDK SURFAR
CMDK WNDBLK
C 
      REAL ITH,IDHC,IDC,IDFC,ITC
      CHARACTER *4 STM
      DIMENSION IDC(4),IDFC(4),DIRECT(4),DIFFUS(4),SFST(4),SFTT(4)
     +,CLSLR(NZN),WSLR(NZW),TRSL(2),SLWL(4),WALS(4),NDYL(12)
C 
C  NDYL IS DAY OF YR AT END OF EACH MONTH 
      DATA NDYL/31,59,90,120,151,181,212,243,273,304,334,365/ 
C 
C  IWALS  OUTPUT OPTION FOR WALL SOLAR LOADING
C  IWALS IS SET IN NAMELIST CONTROL READ IN STRRUN
C     =0  ONLY ONE VALUE FOR EACH OF FOUR COMPASS DIRECTIONS
C     =1  VALUES FOR EACH CONSOLIDATED OUTSIDE-WALL ENCLOSURE ELEMENT 
C            (=1 OPTION WILL REQUIRE TOO MUCH MEMORY FOR A 640K PC)
      CALL CURS(19)
      WRITE(*,513)
      CALL CURS(19)
      WRITE(*,*)'START CALC SOLAR DATA FOR DAY SEQUENCE NO.',
     +          IDAY,' OCCURING IN MONTH ',KM
      WRITE(60,*)'START CALC SOLAR DATA FOR DAY SEQUENCE NO.',
     +          IDAY,' OCCURING IN MONTH ',KM
      IDBGSV=IDBG 
      IDBG=0
C 
      DO 10 I=1,NIWL
      DO 10 J=1,24
   10 DATOUT(I,J)=0.
      DO 12 I=1,NZW 
   12 NIJA(I)=0 
      IF(IWALS.EQ.0)THEN
C  SUM NET WALL AREA IN EACH COMPASS DIRECTION
          DO 18 I=1,4 
          WALS(I)=1.E-13
   18     CONTINUE
          DO 20 J=1,NROOMS
          NRM=NRMA(J) 
          N2=NWALLA(NRM)
          DO 20 K2=1,N2 
          IZW=NENC(NRM,K2)
          ID=IDEXP(IZW) 
          IF(ID.LE.4)WALS(ID)=WALS(ID)+AWALLA(IZW)
   20     CONTINUE
          ENDIF 
C 
      ITIME=-60 
C  READ 24 HRS OF SOLAR OR CLOUD COVER DATA
      NDYRIS=NDYRI
      IF(NSEQW.EQ.3)THEN
C
C NOTE: TAPE 19 WITH SOLARH DATA IS USED TO MINIMIZE MEMORY REQMTS
C       DURING EXECUTION.  IT IS WRITTEN IN WETHR.FOR OR WWEATH.FOR      
        READ(19,*,END=990)KMN,IDYMN
        WRITE(60,*)' SOLRLD FR TAPE19: KMN,IDYMN= ',KMN,IDYMN
        IF(KMN.NE.KM)THEN
          WRITE(*,*)' SOLRLD: KMN FROM TAPE19,KM,IDYMN= ',
     +              KMN,KM,IDYMN
          WRITE(60,*)' SOLRLD: KMN FROM TAPE19,KM,IDYMN= ',
     +               KMN,KM,IDYMN
          STOP ' SOLRLD: KMN FROM TAPE19 NE KM!'
        ENDIF
        IF(NDYRI.EQ. -2)THEN
C  COMPUTE NDYRI FROM DATA ON TAPE19 WHEN NDYRI=-2!        
          IF(KMN.EQ.1)THEN
            NDYRI=IDYMN
          ELSE
            NDYRI=NDYL(KMN-1)+IDYMN
          ENDIF
        ENDIF
      ENDIF
      READ(19,533,END=991)(SOLARH(IHR),IHR=1,24)
C      
C  SET OUTPUT ARRAY SUBSCRIPT FOR HOUR TO ZERO
      IHOUR=0 
C 
   42 ITIME=ITIME+60
      IHOUR=IHOUR+1 
      TIME=FLOAT(ITIME)/60. 
      ISUN=0
      IF(TIME.GT.23.98)GO TO 9996 
C 
C  GET SOLAR DATA FOR THIS HOUR AND DAY 
      CALL SOLR(TIME,NDYRI,IDAY,KM,STM,DNLAT,DLONG, 
     +          DST,ICC,CLEAR,
     +          ISUN,ITH,IDHC,SAZM,SALT,
     +          IDC,IDFC,DIRECT,DIFFUS,SFST,SFTT) 
      SOLARH(IHOUR)=ITH
C 
      DO 60 I=1,NZN 
  60  CLSLR(I)=0. 
      DO 61 IJ=1,4
      SLWL(IJ)=0. 
   61 CONTINUE
      IWAL=0
      DO 62 K=1,NZW 
      WSLR(K)=0.
   62 CONTINUE
C 
C  LOOP FOR EXPOSED WALLS IN EACH ZONE
  
      DO 295 J=1,NROOMS 
      NRM=NRMA(J) 
      N2=NWALLA(NRM)
      DO 295 K2=1,N2
      IZW=NENC(NRM,K2)
      ID=IDEXP(IZW) 
C  SUN CAN ONLY SHINE ON OUTSIDE WALLS
C   NOTE:  ABOVE GROUND BASEMENT WALLS ARE ASSUMED TO BE SHADED 
      IF(ID.GT.4)GO TO 295
      M=ID
      XLEN=XLENA(IZW) 
      HGT=HGTA(IZW) 
      AWALC=XLEN*HGT
      OHANG=OHANGA(IZW) 
      OHNG=OHANG*12.
      HHANG=HHANGA(IZW) 
      NWND=NWNDA(IZW) 
      EPSLM=EPSLR(IZW)
      KON=KONSTA(IZW) 
      WNDSA=0.
      WNDUA=0.
      IF(ISUN.GT.0)GO TO 251
C **************     3RD DIMENSION ON SHDFCT SHOULD BE NRM LATER
C                    WHEN SHDFCT APPLIES TO > 1 ZONE      
      SHDFC=1.
      IF(ID.GT.1)SHDFC=SHDFCT(ID-1,1,1,IHOUR)
      ITC=IDC(ID)*SHDFC+IDFC(ID) 
      IF(NWND.EQ.0)GO TO 241
C 
      DO 240 K4=1,NWND
      NI=NIWND(IZW,K4)
      WHGT=WHGTA(NI)
      WWDTH=WWDTHA(NI)
      REVEAL=REVELA(NI) 
      OHNGH=OHNGHA(NI)
      SFSD=SFSDA(NI)
      WAREA=AWAWA(NI) 
C  IF ANY WALL IS ALWAYS SHADED, GO TO 210
      DO 206 IJ=1,4 
      IF(M.EQ.IFXDS(IJ))GO TO 210 
  206 CONTINUE
      IF (SALT.EQ.0.0 .AND.SAZM.EQ. 0.0) GO TO 210
      WP=REVEAL *SFST(ID)/12. 
      IF (WP.GT.WWDTH) WP=WWDTH 
      IF(WP.LT.0.0)WP=0.0 
      HP=((REVEAL+OHNG)*SFTT(ID)-OHNGH)/12. 
      IF (HP.GT.WHGT) HP=WHGT 
      IF(SFTT(ID).EQ.0.0)HP=0.0 
      IF(HP.LT.0.0)HP=0.0 
      SA=WWDTH*HP+WHGT*WP-WP*HP 
      GO TO 215 
210   SA=WAREA
      WNDSA=WNDSA+WAREA 
      GO TO 216 
C 
C---- TOTAL SHADED AREA FROM OVERHANG USED FOR FINDING SHADED WALL AREA 
215   HPP=(OHNG*SFTT(ID)-OHNGH)/12. 
      IF(HPP.LT.0.0)HPP=0.0 
      WNDSA=WNDSA+WWDTH*HPP 
C 
C---- TOTAL UNSHADED WINDOW AREA USED FOR FINDING UNSHADED WALL AREA
      WNDUA=WNDUA+(WAREA-WWDTH*HPP) 
C 
C     CALC INSTANTANEOUS HEAT TRANSMISSION THRU WINDOWS 
C 
  216 SLA=(WAREA-SA)/WAREA
      DIRCT=SLA*DIRECT(M) 
      DIFFS=DIFFUS(M) 
      IF(SFSD.LE.0.)SFSD=1. 
C 
C  TOTAL SOLAR RADIATION TRANSMITTED THRU WINDOW
C  3RD DIMENSION ON SHDFCT SHOULD BE NRM LATER WHEN SHDFCT > 1 ZONE
      SHDFC=1.
      IF(M.GT.1)SHDFC=SHDFCT(M-1,2,1,IHOUR)
      COOL=SFSD*WAREA*(DIRCT*SHDFC+DIFFS) 
      IF(IDBG.NE.0)WRITE(60,503)M,IHOUR,COOL,SFSD,WAREA,SLA
      CLSLR(NRM)=CLSLR(NRM)+COOL
  240 CONTINUE
C 
C  END LOOP FOR WINDOWS IN THIS WALL
C 
C  SOLAR LOADING ON WALL
  241 CONTINUE
C 
      DO 242 I=1,4
C  IF ANY WALL ALWAYS SHADED, GO TO 245 
      ISWA=1
      IF(M.EQ.IFXDS(I))GO TO 245
C  CHECK FOR ALWAYS SHADED CONSTRUCT
      IFX=IFXL(I,KON) 
      ISWA=2
      IF(M.EQ.IFX)GO TO 245 
  242 CONTINUE
C 
C  GET SHADING FROM OVERHANG (ASSUMES SHDFCT'S WERE CALC W/O OVERHANG 
      IF(OHANG.LE.0.0)GO TO 243 
      ISWA=3
      IF (SALT.EQ.0.0 .AND.SAZM.EQ. 0.0) GO TO 245
      HP=(OHNG*SFTT(ID)-HHANG)/12.
      IF(HP.GT.HGT)HP=HGT 
      IF(SFTT(ID).EQ.0.0)HP=0.
      IF(HP.LT.0.0)HP=0.
      SWA=XLEN*HP 
      IF(SWA.LT.0.0)SWA=0.0 
      ISWA=4
      IF(SWA.GE.AWALC) GO TO 245
C 
C---- NET SHADED WALL AREA=TOTAL SHADED WALL AREA - WINDOW AREA 
C---- SHADED FROM OVERHANG
C 
      SWANET=SWA-WNDSA
      IF(SWANET.LT.0.0)GO TO 243
C 
C---- NET UNSHADED WALL AREA=TOTAL WALL AREA-TOTAL SHADED WALL AREA-
C---- TOTAL WINDOW AREA NOT SHADED BY OVERHANG
C 
      UWANET=AWALC-SWA-WNDUA
      GO TO 250 
C 
C---- NO OVERHANG OR SHADED WALL AREA 
243   SWANET=0.0
      UWANET=AWALC-WNDUA-WNDSA
      GO TO 250 
C 
C---- ENTIRE WALL IS SHADED 
C 
245   UWANET=0.0
      SWANET=AWALC-WNDUA-WNDSA
      IF(IDBG.NE.0)WRITE(60,506)ISWA,OHANG,SFTT(ID),XLEN,HGT,SWANET
C 
C CALC INSTANTANEOUS SOLAR RAD INTENSITIES FOR SHADED & 
C     UNSHADED AREAS
250   HIS=IDFC(ID)
      HIUS=ITC
      GO TO 252 
C 
C---- SOLAR LOAD ON WALLS (BTUH)
C 
  251 SLWALL=0. 
      GO TO 253 
  252 CONTINUE
      SLWALL=(HIUS*UWANET+HIS*SWANET)*EPSLM 
      IF(IDBG.NE.0)THEN 
      WRITE(60,504)KM,IZW,M,IHOUR,SLWALL,HIUS,HIS,UWANET,SWANET,EPSLM
          ENDIF 
  253 WSLR(IZW)=SLWALL/AWALLA(IZW)
      SLWL(ID)=SLWL(ID)+SLWALL
      IF(IWALS.EQ.1)THEN
        IWAL=IWAL+1 
        IF(IWAL.GT.(NIWL-2-NROOMS))THEN 
          WRITE(60,502)IWAL,NIWL,NROOMS
          STOP 'SOLRLD:NIWL MUST BE INCR' 
          ENDIF 
        ENDIF
  295 CONTINUE
C 
C ** ** ** END LOOP FOR WALLS IN ZONE AND LOOP FOR ZONES
C 
C  SOLAR LOADING ON ROOF SECTIONS FOR ROOFS ABOVE TWO ATTICS
      TRSL(1)=0.
      TRSL(2)=0.
      IF(ISUN.GT.0)GO TO 364
C 
      DO 360 I=1,NSECTA 
      M=IDEXPR(I) 
      NAT=NATTC(I)
C  3RD DIMENSION ON SHDFCT BELOW SHOULD BE NAT LATER WHEN SHDFCT > 1 
C      ZONE      
      SHDFC=1.
      IF(M.GT.1)SHDFC=SHDFCT(M-1,3,1,IHOUR)
      SLRS=(IDR(I)*SHDFC+IDIFR(I))*SECTAA(I)*EPRSLR(I)
      IF(IDBG.NE.0)THEN 
      WRITE(60,505)I,IDR(I),SHDFC,IDIFR(I),SECTAA(I),EPRSLR(I),SLRS
          ENDIF 
      IF(NAT.EQ.1)TRSL(1)=TRSL(1)+SLRS
      IF(NAT.EQ.2)TRSL(2)=TRSL(2)+SLRS
  360 CONTINUE
C 
  364 TRSL(1)=TRSL(1)/RAREA(1)
      IF(RAREA(2).NE.0)THEN 
          TRSL(2)=TRSL(2)/RAREA(2)
          ENDIF 
C 
      IF(TRSL(1).GT.0.)THEN 
          IF(IDBG.NE.0)WRITE(60,509)1,IHOUR,IDAY,KM,TRSL(1)
  509 FORMAT(1X,'SOLRLD:NIJ,IHR,IDY,KM,TRSL= ',4I5,G13.5) 
          DATOUT(1,IHOUR)=TRSL(1)
          ENDIF 
      IF(TRSL(2).GT.0.)THEN 
          IF(IDBG.NE.0)WRITE(60,510)2,IHOUR,IDAY,KM,TRSL(2)
  510 FORMAT(1X,'SOLRLD:NIJ,IHR,IDY,KM,TRSL= ',4I5,G13.5) 
          DATOUT(2,IHOUR)=TRSL(2)
          ENDIF 
      NIJ=2 
      DO 365 J=1,NROOMS 
      NRM=NRMA(J) 
      NIJ=NIJ+1 
      IF(IDBG.NE.0)WRITE(60,511)NRM,NIJ,IHOUR,IDAY,KM,CLSLR(NRM) 
  511 FORMAT(1X,'SOLRLD:NRM,NIJ,IHR,IDY,KM,CSLR= ',5I5,G13.5) 
      DATOUT(NIJ,IHOUR)=CLSLR(NRM) 
  365 CONTINUE
      IF(IWALS.EQ.1)THEN
          DO 366 J=1,NROOMS 
          NRM=NRMA(J) 
          N2=NWALLA(NRM)
C 
          DO 366 K2=1,N2
          IZW=NENC(NRM,K2)
          ID=IDEXP(IZW) 
C  SUN CAN ONLY SHINE ON OUTSIDE WALLS
          IF(ID.GT.4)GO TO 366
          NIJ=NIJ+1 
          IF(NIJ.GT.NIWL)THEN 
              WRITE(60,501)NIJ,NIWL
              STOP 'SOLRLD:NIWL AT LDPOUT COMMON BLK MUST BE INCR'
              ENDIF 
          NIJA(IZW)=NIJ 
          IF(IDBG.NE.0)WRITE(60,512)NRM,NIJ,IHOUR,IDAY,KM,WSLR(IZW)
  512     FORMAT(1X,'SOLRLD:NRM,NIJ,IHR,IDY,KM,WSLR= ',5I5,G13.5) 
C  SOLAR ON EACH CONSOLIDATED OUTSIDE-WALL ENCLOSURE ELEMENT
          DATOUT(NIJ,IHOUR)=WSLR(IZW)
  366     CONTINUE
      ELSE
C       FIND AVERAGE SOLAR LOADING ON WALLS IN EACH DIRECTION 
          DO 368 ID=1,4 
          SWSLR=SLWL(ID)/WALS(ID) 
          NIJ=NIJ+1 
          IF(NIJ.GT.NIWL)THEN 
              WRITE(60,501)NIJ,NIWL
          STOP 'SOLRLD:NIWL AT LDPOUT COMMON BLK MUST BE INCR'
              ENDIF 
          NIJA(ID)=NIJ
          DATOUT(NIJ,IHOUR)=SWSLR
  368     CONTINUE
          ENDIF 
      GO TO 42
C 
 9996 CONTINUE
      NDYRI=NDYRIS
C  END OF HOURLY LOOP * * * * * * * * * 
      IDBG=IDBGSV 
      CALL CURS(19)
      WRITE(*,513)
      CALL CURS(19)
      WRITE(*,*)'  END CALC SOLAR DATA FOR DAY SEQUENCE NO.',
     +          IDAY,' OCCURING IN MONTH ',KM
      RETURN
  990 WRITE(60,*)' SOLRLD: PROBLEM READING MONTH AND DAY FROM TAPE19'
      STOP ' SOLRLD: PROBLEM READING MONTH AND DAY FROM TAPE19'
  991 WRITE(60,*)' SOLRLD: PROBLEM READING HORIZ SOLAR DATA FROM TAPE19'
      STOP ' SOLRLD: PROBLEM READING HORIZ SOLAR DATA FROM TAPE19'
  500 FORMAT(1X)
  501 FORMAT(1H0,'SOLRLD:NIWL AT LDPOUT COMMON BLK MUST BE INCR', 
     +/'  NIJ,NIWL= ',2I5)
  502 FORMAT(1H0,'OOPS!!SOLRLD:NIWL MUST BE INCREASED. IWAL,NIWL',
     +'NROOMS= ',3I5) 
  503 FORMAT(1X,'ID= ',I3,'  IHOUR= ',I3,'  COOL,SFSD,WAREA,SLA= ', 
     +4G13.5) 
  504 FORMAT(1X,'SOLRLD:KM= ',I3,' IZW= ',I3,' ID= ',I3,'  IHOUR= ',I3/ 
     +' SLWALL,HIUS,HIS,UWANET,SWANET,EPSLM= ',6G13.5)
  505 FORMAT(1X,'I,IDR,SHDFC,IDIFR,SECTA,EPRSLR,SLRS= ',I4,6G13.5)
  506 FORMAT(1X,'ISWA,SFT,XLEN,HGT,SWANET= ',I3,5G13.5) 
  507 FORMAT(1H0,'NOTE!!!! NMONTH MAX IS 5 IN SOLRLD')
  513 FORMAT(80(1H ))
  533 FORMAT(1X,6G13.5/1X,6G13.5/1X,6G13.5/1X,6G13.5)
      END 
