      SUBROUTINE TYPE15(TIME,XIN,OUT,T,DTDT,PAR,INFO,ICNTRL,*)

C-----------------------------------------------------------------------------------------------------------------------
C     THIS TYPE READS AND INTERPOLATES WEATHER DATA FROM TMY, TMY2, IWEC, CWEC AND ENERGY PLUS FORMATTED
C     DATA FILES. THE DATA READING MODES ARE AS FOLLOWS:
C    
C     MODE 1: TMY FILE FORMAT  
C     MODE 2: TMY2 FORMAT
C     MODE 3: ENERGYPLUS FORMAT
C     MODE 4: IWEC (INTERNATIONAL WEATHER FOR ENERGY CALCULATIONS)
C     MODE 5: CWEC (CANADIAN WEATHER FOR ENERGY CALCULATIONS)
C     MODE 6: METEONORM (TMY2 FILE FORMAT)
C     MODE 7: TMY3 FORMAT
C
C     LAST MODIFIED:
C       11/04 -- JWT - RE-CODING FROM TYPES 89 AND 16
C       12/04 -- JWT - FIXED THE OCHECK DECLARATION
C       03/04 -- JWT - SKY COVER MUTLIPLIERS WERE WRONG FOR CWEC AND ENERGY+ FORMATS
C       02/06 -- JWT - Fixed a bug in tracking modes 2 and 3 where the axis slope was not being reset.
C       02/06 -- JWT - Added the capability to take the slope and azimuth data as inputs to the model.
C       05/06 -- DAA - Fixed bug when high latitudes were used. The angles for the integration  
C                      of the solar radiation were not calculated wright when the sun does not
C                      set during the day.
C       09/06 -- DAA - The previuos fix conflicted with some files at very small time steps. 
C                      It was modified to prevent conflict between high latitudes and short
C                      timesteps.
C       10/06 -- DEB/DAA - Opening a file with status 'NEW' created a conflict when the file already
C                      exists. Modified the opening status to 'UNKNOWN'.
C       09/07 -- JWT - Fixed the multipliers for illuminance and optical depth for the TMY-2 mode
C       09/07 -- JWT - Fixed the multipliers for wind velocity in the Energy+ mode
C       09/07 -- JWT - Fixed the high incidence angle check from 79.9 to 89.9 degrees.
C       03/08 -- DEB - Many Meteonorm files have a 999 in the snow depth field to indicate that there is no data. This 
C                       was causing the "snow covered" ground reflectance to be set all year.
C       04/08 -- JWT - Added the capability to read TMY3 files
C
C     REFERENCES:
C      1) Martin M.,Berdahl P., Characteristics of infrared sky radiation in the U.S.A.,Solar Energy, vol.33, No 3/4,p.321,1984
C      2) Walton G.,TARP Reference Manual,NBSIR 83-2655,1983
C-----------------------------------------------------------------------------------------------------------------------

!export this subroutine for its use in external DLLs.
!DEC$ATTRIBUTES DLLEXPORT :: TYPE15

C-----------------------------------------------------------------------------------------------------------------------
C    ACCESS TRNSYS FUNCTIONS
      USE TrnsysConstants
      USE TrnsysFunctions
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    TRNSYS DECLARATIONS
      IMPLICIT NONE                       
      DOUBLE PRECISION XIN,OUT,TIME,PAR,T,DTDT,TIME0,TFINAL,DELT
      INTEGER*4 INFO(15),NP,NI,ND,IUNIT,ITYPE,ICNTRL,NOUT,
     1   NITEMS_MAX,NPAR,NDER,NS_MAX,NIN
      CHARACTER*3 OCHECK,YCHECK
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    USER DECLARATIONS
      PARAMETER (NS_MAX=25,NP=7+3*NS_MAX,NI=2*NS_MAX,NITEMS_MAX=36,
     1   NOUT=52+8*NS_MAX,ND=0)                  
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    REQUIRED TRNSYS DIMENSIONS
      DIMENSION XIN(NI),OUT(NOUT),PAR(NP),YCHECK(NI),OCHECK(NOUT)
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES
      INTEGER I,J,K,J_MONTH,J_SPOT,J_MIN,J_MAX,ITEMP1,ITEMP2     !A TEMPORARY VARIABLE
      INTEGER LU                          !A VARIABLE FOR OPENING A TEMPORARY FILE
      INTEGER LUW                         !THE LOGICAL UNIT FOR THE LIST FILE
      INTEGER COMMACNTR                   
      INTEGER IE                          !AN INDICATOR THAT THERE WAS AN ERROR IN PSYCH
      INTEGER IE_READ                     !AN INDICATOR THAT THERE WAS A DATA READING ERROR
      INTEGER N_ITEMS                     !NUMBER OF COLUMNS TO BE READ FROM THE DATA FILE
      INTEGER HEADERSKIP                  !NUMBER OF HEADER LINES AT TOP OF DATA FILE
      INTEGER ISKIP                       !NUMBER OF EXTRA LINES TO SKIP IF SKIP IS ON
      INTEGER INTERP(NITEMS_MAX)          !INDICATOR FOR INTERPOLATION FOR EACH VALUE
      INTEGER LU_DATA                     !THE LOGICAL UNIT FOR THE DATA FILE
      INTEGER SITE
      INTEGER I_SOLARTIME                 !1=DATA IS IN LOCAL TIME, -1=DATA IN SOLAR TIME
      INTEGER IERROR_RAD                  !AN INDICATOR IF THE RADIATION PRCESSOR FOUND PROBLEMS
      INTEGER MODE                        !INDICATES WHICH TYPE OF DATA FILE IS TO BE READ
                                          !   1=TMY
                                          !   2=TMY2
                                          !   3=ENERGY+
                                          !   4=IWEC
                                          !   5=CWEC
                                          !   6=METEONORM
	                                    !   7=TMY3
      INTEGER TIME_STAMP(NITEMS_MAX)      !THE TIME BASE FOR THE VARIABLE
                                          !   0=AVERAGE VALUE REPORTED AT END OF RANGE
                                          !   1=INSTANTANEOUS VALUE AT END OF RANGE
      INTEGER WMOREGION,TMZNHR,TMZNMN
      INTEGER NS                          !NUMBER OF SURFACES FOR RADIATION PROCESSING
      INTEGER RAD_MODE                    !DIFFUSE RADIATION MODEL
                                          !   1=ISOTROPIC SKY
                                          !   2=HAY AND DAVIES MODEL
                                          !   3=REINDL MODEL
                                          !   4=PEREZ MODEL
      INTEGER TRACK_MODE(NS_MAX)          !TRACKING MODE FOR THE SURFACE
                                          !   1=FIXED SURFACE
                                          !   2=ONE-AXIS TRACKING: FIXED SLOPE AND VARIABLE AZIMUTH
                                          !   3=ONE-AXIS TRACKING: ROTATION ABOUT A FIXED AXIS
                                          !   4=TWO-AXIS TRACKING

      
      CHARACTER(LEN=MaxMessageLength) MESSAGE1,MESSAGE2,MESSAGE3,
     1 MESSAGE4,MESSAGE5,MESSAGE6,MESSAGE7,MESSAGE8    !ERROR AND WARNING MESSAGES FROM THIS TYPE
      CHARACTER*1 TMZNDIR,LATDIR,ELEVDIR
      CHARACTER*22 CITY                   !THE NAME OF THE CITY
      CHARACTER*2 STATE                   !THE NAME OF THE STATE
      CHARACTER*1 LONGDIR                 !INDICATES THE DIRECTION OF THE LONGITUDE
      CHARACTER*1 EPLUSHEADER(161)
      CHARACTER*8 KEYWORD
      CHARACTER*22 SOURCE
      CHARACTER*22 COUNTRY
      CHARACTER*80 ALPHANUM
      
      DOUBLE PRECISION PSYDAT(9)          !ARRAY HOLDING PSYCH PROPERTIES
      DOUBLE PRECISION XMULT(NITEMS_MAX)  !MULTIPLIER FOR READ VALUE
      DOUBLE PRECISION XADD(NITEMS_MAX)   !ADDITION FACTOR FOR READ VALUE
      DOUBLE PRECISION VALUE_NOW(NITEMS_MAX)      !THE CURRENT VALUE OF THE READ VARIABLE
      DOUBLE PRECISION VALUE_NEXT(NITEMS_MAX)     !THE NEXT VALUE OF THE READ VARIABLE
      DOUBLE PRECISION VALUE_WAS(NITEMS_MAX)      !THE PAST VALUE OF THE READ VARIABLE
      DOUBLE PRECISION TIME_NEXT          !THE NEXT TIME AT WHICH DATA SHOULD BE READ
      DOUBLE PRECISION RATIO              !AN INTERPOLATION MULTIPLIER
      DOUBLE PRECISION XNOW               !INSTANTANEOUS VALUE OF FUNCTION AT END OF CURRENT TIMESTEP
      DOUBLE PRECISION XWAS               !INSTANTANEOUS VALUE OF FUNCTION AT END OF PREVIOUS TIMESTEP
      DOUBLE PRECISION XNEXT              !INSTANTANEOUS VALUE OF FUNCTION AT END OF NEXT TIMESTEP
      DOUBLE PRECISION BEGIN(NITEMS_MAX)  !THE INSTANTANEOUS VALUE OF THE FUNCTION AT THE BEGINNING OF THE TIMESTEP
      DOUBLE PRECISION ENDING(NITEMS_MAX) !THE INSTANTANEOUS VALUE OF THE FUNCTION AT THE END OF THE TIMESTEP
      DOUBLE PRECISION PREVIOUS(NITEMS_MAX)       !THE INSTANTANEOUS VALUE OF THE FUNCTION AT THE END OF THE PREVIOUS TIMESTEP
      DOUBLE PRECISION TMZONEHR           !TIME ZONE FOR DATA CITY (in Hours)
      DOUBLE PRECISION TMZONE             !TIME ZONE FOR DATA CITY
      DOUBLE PRECISION LATDEG             !LATITUDE DEGREES FOR DATA CITY
      DOUBLE PRECISION LATMIN             !LATITUDE MINUTES FOR DATA CITY
      DOUBLE PRECISION LONGDEG            !LONGITUDE DEGREES FOR DATA CITY
      DOUBLE PRECISION LONGMIN            !LONGITUDE MINUTES FOR DATA CITY
      DOUBLE PRECISION LAT                !THE LATITUDE OF DATA CITY
      DOUBLE PRECISION LONG               !THE LONGITUDE OF DATA CITY
      DOUBLE PRECISION ELEV               !ELEVATION OF DATA CITY
      DOUBLE PRECISION SHIFT              !THE SHIFT IN HOUR ANGLE REQUIRED TO CONVERT FROM LST TO SOLAR TIME
      DOUBLE PRECISION RHO                !GROUND REFLECTANCE
      DOUBLE PRECISION RHOG               !GROUND REFLECTANCE FOR NON-SNOW COVERED PERIODS
      DOUBLE PRECISION RHOG_SNOW          !GROUND REFLECTANCE FOR SNOW COVERED PERIODS
      DOUBLE PRECISION MONTH_AVE_T(0:13)  !THE MONTHLY AVERAGE TEMPERATURE
      DOUBLE PRECISION MONTH_MIN_T(0:13)  !THE MINIMUM MONTHLY TEMPERATURE
      DOUBLE PRECISION MONTH_MAX_T(0:13)  !THE MAXIMUM MONTHLY TEMPERATURE
      DOUBLE PRECISION ANNUAL_AVE_T       !THE ANNUAL AVERAGE TEMPERATURE
      DOUBLE PRECISION ANNUAL_MIN_T       !THE MINIMUM ANNUAL TEMPERATURE
      DOUBLE PRECISION ANNUAL_MAX_T       !THE MAXIMUM ANNUAL TEMPERATURE
      DOUBLE PRECISION LENGTH_MONTH       !THE LENGTH OF EACH MONTH IN HOURS
      DOUBLE PRECISION WDT_MIN            !CRITICAL MINIMUM ANNUAL TEMPERATURE FOR HEATING SEASON
      DOUBLE PRECISION T_AVE_HEAT         !AVERAGE MONTHLY TEMPERATURE BELOW WHICH HEATING IS ENABLED FOR THE MONTH
      DOUBLE PRECISION T_AVE_COOL         !AVERAGE MONTHLY TEMPERATURE ABOVE WHICH COOLING IS ENABLED FOR THE MONTH
      DOUBLE PRECISION X_HEAT(0:13)       !HEATING SEASON MONTHLY INDICATOR
      DOUBLE PRECISION X_COOL(0:13)       !COOLING SEASON MONTHLY INDICATOR
      DOUBLE PRECISION X_SWING(0:13)      !SWING SEASON TEMPORARY ARRAY
      DOUBLE PRECISION OFFSET,MAINS_RATIO,XDAY,LAG            !USED IN THE MAINS WATER CALCULATION
      DOUBLE PRECISION DT_MAX             !MAXIMUM MONTHLY AVERAGE TEMPERATURE SWING
      DOUBLE PRECISION T_MAINS            !THE MAINS WATER TEMPERATURE
      DOUBLE PRECISION TD1                !THE TIME OF THE LAST DATA READ
      DOUBLE PRECISION TD2                !THE TIME OF THE NEXT DATA READ
      DOUBLE PRECISION SLOPE(NS_MAX)      !SLOPE OF THE SURFACE
      DOUBLE PRECISION AXIS_SLOPE(NS_MAX) !SLOPE OF THE AXIS
      DOUBLE PRECISION AZIMUTH(NS_MAX)    !AZIMUTH OF THE SURFACE
      DOUBLE PRECISION AXIS_AZIMUTH(NS_MAX)    !AZIMUTH OF THE AXIS
      DOUBLE PRECISION IHOR               !GLOBAL HORIZONTAL SOLAR RADIATION
      DOUBLE PRECISION IDN                !DIRECT NORMAL SOLAR RADIATION
      DOUBLE PRECISION SOLAR(13)          !AN ARRAY HOLDING THE SOLAR RADIATION VALUES
      DOUBLE PRECISION ECLD               !EMISIVITY OF THE CLOUDS
      DOUBLE PRECISION EOSKY,ESKY,TSKY    !SKY TEMPERATURE VARIABLES
      DOUBLE PRECISION XTEMP1,XTEMP2      !TEMPORARY VARIABLES
      DOUBLE PRECISION OUT_TEMP(NOUT)     !A TEMPORARY OUTPUT ARRAY REQUIRED BY THE ORDERING OF THE OUTPUTS
      DOUBLE PRECISION STORE(74+9*NITEMS_MAX)      !STORAGE ARRAY
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    DATA STATEMENTS 
      DATA MESSAGE1 /'Unable to correctly read from the supplied data fi
     1le - please check the file and re-run the simulation.'/
      DATA MESSAGE2 /'End of file encountered'/
      DATA MESSAGE3 /'Total sky cover values for this time step were mis 
     1sing from the data file. The last read values were retained.'/
      DATA MESSAGE4 /'Opaque sky cover values for this time step were mi
     1ssing from the data file. The last read values were retained.'/
	DATA MESSAGE5 /'Illegal value supplied to the utility subroutine G
	1etIncidentRadiation; radiation calculations aborted.'/
      DATA MESSAGE6/'The calculated solar hour angle for the end of the 
	1data timestep has been modified from its original value.  This fix
     1 was added to account for sundown periods near midnight at high-la
     1titude locations.  If this warning is noted for locations that do 
     1not meet this criteria, please contact your TRNSYS distributor.'/
	DATA MESSAGE7/'The calculated value of the beam radiation exceeded
	1 the maximum possible beam radiation for this timestep.  The beam 
     1radiation has been set to the extraterrestrial radiation for this 
     1timestep.'/
      DATA MESSAGE8/'Time inconsistency.'/
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    GET GLOBAL TRNSYS SIMULATION VARIABLES
      TIME0=getSimulationStartTime()
      TFINAL=getSimulationStopTime()
      DELT=getSimulationTimeStep()
      LUW=getListingFileLogicalUnit()
C-----------------------------------------------------------------------------------------------------------------------
 
C-----------------------------------------------------------------------------------------------------------------------
C    SET THE VERSION INFORMATION FOR TRNSYS
      IF(INFO(7).EQ.-2) THEN
         INFO(12)=16
         RETURN 1
      ENDIF
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    DO ALL THE VERY LAST CALL OF THE SIMULATION MANIPULATIONS HERE
      IF (INFO(8).EQ.-1) THEN
         RETURN 1
      ENDIF
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    DO ALL THE VERY FIRST CALL OF THE SIMULATION MANIPULATIONS HERE
      IF (INFO(7).EQ.-1) THEN

C       RETRIEVE THE UNIT NUMBER AND TYPE NUMBER FOR THIS COMPONENT FROM THE INFO ARRAY
         IUNIT=INFO(1)
         ITYPE=INFO(2)

C       SET THE MODE OF THE DATA READER
         MODE=JFIX(PAR(1)+0.5)
         IF((MODE.LT.1).OR.(MODE.GT.7)) CALL TYPECK(-4,INFO,0,1,0)

C       SET THE NUMBER OF COLUMNS TO BE READ FROM THE DATA FILE
         IF (MODE.EQ.1) THEN !TMY
            N_ITEMS = 36
         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN !TMY2
            N_ITEMS = 27
         ELSE IF(MODE.EQ.3) THEN !ENERGY+
            N_ITEMS = 31
         ELSE IF(MODE.EQ.4) THEN !IWEC
            N_ITEMS = 28
         ELSE IF(MODE.EQ.5) THEN !CWEC
            N_ITEMS = 31
         ELSE IF(MODE.EQ.7) THEN !TMY3
            N_ITEMS = 28
         ENDIF

C       SEE HOW MANY SURFACES ARE TO BE EVALUATED
         NS=JFIX(PAR(6)+0.5)
         IF((NS.LT.0).OR.(NS.GT.NS_MAX)) CALL TYPECK(-4,INFO,0,6,0)
         
C       SET SOME INFO ARRAY VARIABLES TO TELL THE TRNSYS ENGINE HOW THIS TYPE IS TO WORK
         INFO(6)=52+8*NS
         INFO(9)=1
         INFO(10)=0

C       SET THE REQUIRED NUMBER OF INPUTS, PARAMETERS AND DERIVATIVES THAT THE USER SHOULD SUPPLY IN THE INPUT FILE
         IF(MODE.EQ.1) THEN
            NPAR=7+3*NS
         ELSE
            NPAR=6+3*NS
         ENDIF
         NDER=0

	   IF(INFO(3).GT.0) THEN
	      NIN=2*NS
	   ELSE
	      NIN=0
	   ENDIF

C       CALL THE TYPE CHECK SUBROUTINE TO COMPARE WHAT THIS COMPONENT REQUIRES TO WHAT IS SUPPLIED IN THE INPUT FILE
         CALL TYPECK(1,INFO,NIN,NPAR,NDER)

C       SET THE YCHECK AND OCHECK ARRAYS TO CONTAIN THE CORRECT VARIABLE TYPES FOR THE INPUTS AND OUTPUTS
         DATA OCHECK(1:23)/'TE1','TE1','TE1','TE1','TE1','DM1','PC1',
	1   'VE1','DG1','PR4','DM1','DM1','IR1','IR1','IR1','DG1','DG1',
     1   'IR1','IR1','IR1','IR1','IR1','DG1'/
         
	   DO J=1,2*NS
            YCHECK(J)='DG1'
	   ENDDO

         DO J=1,NS
	      OCHECK(23+J)='IR1'
	      OCHECK(23+NS+J)='IR1'
	      OCHECK(23+2*NS+J)='IR1'
	      OCHECK(23+3*NS+J)='IR1'
	      OCHECK(23+4*NS+J)='IR1'
	      OCHECK(23+5*NS+J)='DG1'
	      OCHECK(23+6*NS+J)='DG1'
	      OCHECK(23+7*NS+J)='DG1'
	   ENDDO
         OCHECK(24+8*NS)='DG1'
	   OCHECK(25+8*NS)='DG1'
	   OCHECK(26+8*NS)='DG1'
	   OCHECK(27+8*NS)='LE1'
	   OCHECK(28+8*NS)='DM1'
	   OCHECK(29+8*NS)='DM1'
	   OCHECK(30+8*NS)='TE1'
	   OCHECK(31+8*NS)='TE1'
	   OCHECK(32+8*NS)='TE1'
	   OCHECK(33+8*NS)='TE1'
	   OCHECK(34+8*NS)='TE1'
	   OCHECK(35+8*NS)='TE1'
	   OCHECK(36+8*NS)='NAV'
	   OCHECK(37+8*NS)='NAV'
	   OCHECK(38+8*NS)='NAV'
	   OCHECK(39+8*NS)='NAV'
	   OCHECK(40+8*NS)='LE3'
	   OCHECK(41+8*NS)='LE1'
	   OCHECK(42+8*NS)='NAV'
	   OCHECK(43+8*NS)='NAV'
	   OCHECK(44+8*NS)='LE2'
	   OCHECK(45+8*NS)='DY1'
	   OCHECK(46+8*NS)='DM1'
	   OCHECK(47+8*NS)='TD1'
	   OCHECK(48+8*NS)='TD1'
	   OCHECK(49+8*NS)='DM1'
	   OCHECK(50+8*NS)='DM1'
	   OCHECK(51+8*NS)='DM1'

C       CALL THE RCHECK SUBROUTINE TO SET THE CORRECT INPUT AND OUTPUT TYPES FOR THIS COMPONENT
         CALL RCHECK(INFO,YCHECK,OCHECK)

C       SET THE NUMBER OF STORAGE VARIABLES
         CALL SetStorageSize(74+9*N_ITEMS,INFO)

C       RETURN TO THE CALLING PROGRAM
         RETURN 1

      ENDIF
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    DO ALL OF THE INITIAL TIMESTEP MANIPULATIONS HERE - THERE ARE NO ITERATIONS AT THE INTIAL TIME
      IF (TIME.LT.(TIME0+DELT/2.)) THEN

C       SET THE UNIT NUMBER FOR FUTURE CALLS
         IUNIT=INFO(1)
         ITYPE=INFO(2)

C       GET THE TYPE OF DATA TO BE READ IN
         MODE=JFIX(PAR(1)+0.5)
         LU_DATA=JFIX(PAR(2)+0.1)
         RAD_MODE=JFIX(PAR(3)+0.1)
         RHOG=PAR(4)
         RHOG_SNOW=PAR(5)

C       CHECK THE PARAMETERS
         IF((RAD_MODE.LT.1).OR.(RAD_MODE.GT.4)) 
     1      CALL TYPECK(-4,INFO,0,3,0)
         IF((RHOG.LT.0.).OR.(RHOG.GT.1.)) CALL TYPECK(-4,INFO,0,4,0)
         IF((RHOG_SNOW.LT.0.).OR.(RHOG_SNOW.GT.1.)) 
     1      CALL TYPECK(-4,INFO,0,5,0)

         NS=JFIX(PAR(6)+0.5)
      
         DO J=1,NS
            TRACK_MODE(J)=JFIX(PAR(6+3*(J-1)+1)+0.5)
            IF((TRACK_MODE(J).LT.1).OR.(TRACK_MODE(J).GT.4)) 
     1         CALL TYPECK(-4,INFO,0,6+J,0)
	      SLOPE(J)=PAR(6+3*(J-1)+2)
            AZIMUTH(J)=PAR(6+3*(J-1)+3)

	      AXIS_SLOPE(J)=PAR(6+3*(J-1)+2)
            AXIS_AZIMUTH(J)=PAR(6+3*(J-1)+3)
         ENDDO

         IF(MODE.EQ.1) THEN
            LAT=PAR(7+3*NS)
            LONG=-99
            IF((LAT.LT.-90.).OR.(LAT.GT.90.)) 
     1         CALL TYPECK(-4,INFO,0,7+3*NS,0)
         ENDIF

C       GET THE NUMBER OF ITEMS TO BE READ AND SET THE INTERPOLATION AND CONVERSION FACTORS
         IF(MODE.EQ.1) THEN !STANDARD TMY
            N_ITEMS=36
            HEADERSKIP=0

C          YEAR
            INTERP(1)=-1 ; TIME_STAMP(1)=0
            XMULT(1)=1.D0 ; XADD(1)=0.D0

C          MONTH
            INTERP(2)=-2 ; TIME_STAMP(2)=0
            XMULT(2)=1.D0 ; XADD(2)=0.D0

C          DAY
            INTERP(3)=-3 ; TIME_STAMP(3)=0
            XMULT(3)=1.D0 ; XADD(3)=0.D0

C          HOUR
            INTERP(4)=-4 ; TIME_STAMP(4)=1
            XMULT(4)=0.01D0 ; XADD(4)=0.D0

C          LOCAL STANDARD TIME
            INTERP(5)=-5 ; TIME_STAMP(5)=1
            XMULT(5)=0.01D0 ; XADD(5)=0.D0

C          EXTRATERRESTRIAL SOLAR RADIATION
            INTERP(6)=-6 ; TIME_STAMP(6)=0
            XMULT(6)=1.D0 ; XADD(6)=0.D0

C          DIRECT NORMAL SOLAR RADIATION
            INTERP(7)=-7 ; TIME_STAMP(7)=0
            XMULT(7)=1.D0 ; XADD(7)=0.D0

C          DIFFUSE HORIZONTAL SOLAR RADIATION
            INTERP(8)=-8 ; TIME_STAMP(8)=0
            XMULT(8)=1.D0 ; XADD(8)=0.D0

C          NET RADIATION
            INTERP(9)=-9 ; TIME_STAMP(9)=0
            XMULT(9)=1.D0 ; XADD(9)=-5000.D0

C          GLOBAL RADIATION ON TILT
            INTERP(10)=-10 ; TIME_STAMP(10)=0
            XMULT(10)=1.D0 ; XADD(10)=0.D0

C          OBSERVED DATA
            INTERP(11)=-11 ; TIME_STAMP(11)=0
            XMULT(11)=1.D0 ; XADD(11)=0.D0

C          ENGINEERING CORRECTED DATA
            INTERP(12)=-12 ; TIME_STAMP(12)=0
            XMULT(12)=1.D0 ; XADD(12)=0.D0

C          STANDARD YEAR CORRECTED DATA
            INTERP(13)=-13 ; TIME_STAMP(13)=0
            XMULT(13)=1.D0 ; XADD(13)=0.D0

C          ADDITIONAL RADIATION MEASUREMENTS
            INTERP(14)=-14 ; TIME_STAMP(14)=0
            XMULT(14)=1.D0 ; XADD(14)=0.D0

C          MINUTES OF SUNSHINE
            INTERP(15)=-15 ; TIME_STAMP(15)=0
            XMULT(15)=1.D0 ; XADD(15)=0.D0

C          TIME OF TD 1440 MEASUREMENTS
            INTERP(16)=-16 ; TIME_STAMP(16)=0
            XMULT(16)=1.D0 ; XADD(16)=0.D0

C          CEILING HEIGHT
            INTERP(17)=17 ; TIME_STAMP(17)=1
            XMULT(17)=10.D0 ; XADD(17)=0.D0

C          SKY CONDITION INDICATOR
            INTERP(18)=-18 ; TIME_STAMP(18)=1
            XMULT(18)=1.D0 ; XADD(18)=0.D0

C          VISIBILITY
            INTERP(19)=19 ; TIME_STAMP(19)=1
            XMULT(19)=0.1D0 ; XADD(19)=0.D0

C          OCCURRENCE OF THUNDERSTORM, TORNADO OR SQUALL
            INTERP(20)=-20 ; TIME_STAMP(20)=1
            XMULT(20)=1.D0 ; XADD(20)=0.D0

C          OCCURRENCE OF RAIN, RAIN SHOWERS, OR FREEZING RAIN
            INTERP(21)=-21 ; TIME_STAMP(21)=1
            XMULT(21)=1.D0 ; XADD(21)=0.D0

C          OCCURRENCE OF DRIZZLE OR FREEZING DRIZZLE
            INTERP(22)=-22 ; TIME_STAMP(22)=1
            XMULT(22)=1.D0 ; XADD(22)=0.D0

C          OCCURRENCE OF SNOW, SNOW PELLETS OR ICE CRYSTALS
            INTERP(23)=-23 ; TIME_STAMP(23)=1
            XMULT(23)=1.D0 ; XADD(23)=0.D0

C          OCCURRENCE OF SNOW SHOWERS OR SNOW GRAINS
            INTERP(24)=-24 ; TIME_STAMP(24)=1
            XMULT(24)=1.D0 ; XADD(24)=0.D0

C          OCCURRENCE OF SLEET SLEET SHOWERS OR HAIL 
            INTERP(25)=-25 ; TIME_STAMP(25)=1
            XMULT(25)=1.D0 ; XADD(25)=0.D0

C          OCCURRENCE OF FOG, BLOWING DUST OR BLOWING SAND
            INTERP(26)=-26 ; TIME_STAMP(26)=1
            XMULT(26)=1.D0 ; XADD(26)=0.D0

C          OCCURRENCE OF SMOKE HAZE DUST BLOWING SNOW OR BLOWING SPRAY
            INTERP(27)=-27 ; TIME_STAMP(27)=1
            XMULT(27)=1.D0 ; XADD(27)=0.D0

C          SEA LEVEL PRESSURE 
            INTERP(28)=28 ; TIME_STAMP(28)=1
            XMULT(28)=0.01D0/101.325D0 ; XADD(28)=0.D0

C          STATION PRESSURE
            INTERP(29)=29 ; TIME_STAMP(29)=1
            XMULT(29)=0.01D0/101.325D0 ; XADD(29)=0.D0

C          DRY BULB TEMPERATURE
            INTERP(30)=30 ; TIME_STAMP(30)=1
            XMULT(30)=0.1D0 ; XADD(30)=0.D0

C          DEW POINT TEMPERATURE
            INTERP(31)=31 ; TIME_STAMP(31)=1
            XMULT(31)=0.1D0 ; XADD(31)=0.D0

C          WIND DIRECTION
            INTERP(32)=32 ; TIME_STAMP(32)=1
            XMULT(32)=1.D0 ; XADD(32)=0.D0

C          WIND SPEED
            INTERP(33)=33 ; TIME_STAMP(33)=1
            XMULT(33)=0.1D0 ; XADD(33)=0.D0

C          TOTAL SKY COVER
            INTERP(34)=34 ; TIME_STAMP(34)=1
            XMULT(34)=10.D0 ; XADD(34)=0.D0

C          OPAQUE SKY COVER
            INTERP(35)=35 ; TIME_STAMP(35)=1
            XMULT(35)=10.D0 ; XADD(35)=0.D0

C          SNOW COVER INDICATOR
            INTERP(36)=-36 ; TIME_STAMP(36)=1
            XMULT(36)=1.D0 ; XADD(36)=0.D0

         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN !TMY2
            N_ITEMS=27
            HEADERSKIP=1

C          YEAR
            INTERP(1)=-1 ; TIME_STAMP(1)=0
            XMULT(1)=1.D0 ; XADD(1)=0.D0

C          MONTH
            INTERP(2)=-2 ; TIME_STAMP(2)=0
            XMULT(2)=1.D0 ; XADD(2)=0.D0

C          DAY
            INTERP(3)=-3 ; TIME_STAMP(3)=0
            XMULT(3)=1.D0 ; XADD(3)=0.D0

C          HOUR
            INTERP(4)=-4 ; TIME_STAMP(4)=1
            XMULT(4)=1.D0 ; XADD(4)=0.D0

C          EXTRATERRESTRIAL SOLAR RADIATION
            INTERP(5)=-5
            TIME_STAMP(5)=0
            XMULT(5)=3.6D0
            XADD(5)=0.D0

C          EXTRATERRESTRIAL DIRECT NORMAL SOLAR RADIATION
            INTERP(6)=-6
            TIME_STAMP(6)=0
            XMULT(6)=3.6D0
            XADD(6)=0.D0

C          GLOBAL HORIZONTAL SOLAR RADIATION
            INTERP(7)=-7
            TIME_STAMP(7)=0
            XMULT(7)=3.6D0
            XADD(7)=0.D0

C          DIRECT NORMAL SOLAR RADIATION
            INTERP(8)=-8
            TIME_STAMP(8)=0
            XMULT(8)=3.6D0
            XADD(8)=0.D0

C          DIFFUSE HORIZONTAL SOLAR RADIATION
            INTERP(9)=-9
            TIME_STAMP(9)=0
            XMULT(9)=3.6D0
            XADD(9)=0.D0

C          GLOBAL HORIZONTAL ILLUMINANCE
            INTERP(10)=-10
            TIME_STAMP(10)=0
            XMULT(10)=100.D0
            XADD(10)=0.D0

C          DIRECT NORMAL ILLUMINANCE
            INTERP(11)=-11
            TIME_STAMP(11)=0
            XMULT(11)=100.D0
            XADD(11)=0.D0

C          DIFFUSE HORIZONTAL ILLUMINANCE
            INTERP(12)=-12
            TIME_STAMP(12)=0
            XMULT(12)=100.D0
            XADD(12)=0.D0

C          ZENITH LUMINANCE
            INTERP(13)=-13
            TIME_STAMP(13)=0
            XMULT(13)=10.D0
            XADD(13)=0.D0

C          TOTAL SKY COVER
            INTERP(14)=14
            TIME_STAMP(14)=1
            XMULT(14)=10.D0
            XADD(14)=0.D0

C          OPAQUE SKY COVER
            INTERP(15)=15
            TIME_STAMP(15)=1
            XMULT(15)=10.D0
            XADD(15)=0.D0

C          DRY BULB TEMPERATURE
            INTERP(16)=16
            TIME_STAMP(16)=1
            XMULT(16)=0.1D0
            XADD(16)=0.D0

C          DEW POINT TEMPERATURE
            INTERP(17)=17
            TIME_STAMP(17)=1
            XMULT(17)=0.1D0
            XADD(17)=0.D0

C          RELATIVE HUMIDITY
            INTERP(18)=18
            TIME_STAMP(18)=1
            XMULT(18)=1.D0
            XADD(18)=0.D0

C          ATMOSPHERIC PRESSURE
            INTERP(19)=19
            TIME_STAMP(19)=1
            XMULT(19)=0.001D0/1.01325D0
            XADD(19)=0.D0

C          WIND DIRECTION
            INTERP(20)=20
            TIME_STAMP(20)=1
            XMULT(20)=1.D0
            XADD(20)=0.D0

C          WIND SPEED
            INTERP(21)=21
            TIME_STAMP(21)=1
            XMULT(21)=0.1D0
            XADD(21)=0.D0

C          VISIBILITY
            INTERP(22)=-22
            TIME_STAMP(22)=1
            XMULT(22)=0.1D0
            XADD(22)=0.D0

C          CEILING HEIGHT
            INTERP(23)=-23
            TIME_STAMP(23)=1
            XMULT(23)=1.D0
            XADD(23)=0.D0

C          PRECIPITABLE WATER
            INTERP(24)=24
            TIME_STAMP(24)=1
            XMULT(24)=1.D0
            XADD(24)=0.D0

C          AEROSOL DEPTH
            INTERP(25)=-25
            TIME_STAMP(25)=0
            XMULT(25)=1.D-3
            XADD(25)=0.D0

C          SNOW DEPTH
            INTERP(26)=-26
            TIME_STAMP(26)=1
            XMULT(26)=1.D0
            XADD(26)=0.D0

C          DAYS SINCE LAST SNOWFALL
            INTERP(27)=-27
            TIME_STAMP(27)=0
            XMULT(27)=1.D0
            XADD(27)=0.D0

         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN !ENERGY+, CWEC
            N_ITEMS=31
            HEADERSKIP=8

C          YEAR
            INTERP(1)=-1
            TIME_STAMP(1)=0
            XMULT(1)=1.D0
            XADD(1)=0.D0

C          MONTH
            INTERP(2)=-2
            TIME_STAMP(2)=0
            XMULT(2)=1.D0
            XADD(2)=0.D0

C          DAY
            INTERP(3)=-3
            TIME_STAMP(3)=0
            XMULT(3)=1.D0
            XADD(3)=0.D0

C          HOUR
            INTERP(4)=-4
            TIME_STAMP(4)=1
            XMULT(4)=1.D0
            XADD(4)=0.D0

C          MINUTES
            INTERP(5)=-5
            TIME_STAMP(5)=1
            XMULT(5)=1.D0
            XADD(5)=0.D0

C          DRY BULB TEMPERATURE
            INTERP(6)=6
            TIME_STAMP(6)=1
            XMULT(6)=1.D0
            XADD(6)=0.D0

C          DEW POINT TEMPERATURE
            INTERP(7)=7
            TIME_STAMP(7)=1
            XMULT(7)=1.D0
            XADD(7)=0.D0

C          RELATIVE HUMIDITY
            INTERP(8)=8
            TIME_STAMP(8)=1
            XMULT(8)=1.D0
            XADD(8)=0.D0

C          ATMOSPHERIC PRESSURE
            INTERP(9)=9
            TIME_STAMP(9)=1
            XMULT(9)=0.000009869D0
            XADD(9)=0.D0

C          EXTRATERRESTRIAL HORIZONTAL SOLAR RADIATION
            INTERP(10)=-10
            TIME_STAMP(10)=0
            XMULT(10)=3.6D0
            XADD(10)=0.D0

C          EXTRATERRESTRIAL DIRECT NORMAL SOLAR RADIATION
            INTERP(11)=-11
            TIME_STAMP(11)=0
            XMULT(11)=3.6D0
            XADD(11)=0.D0

C          HORIZONTAL INFRARED RADIATION
            INTERP(12)=-12
            TIME_STAMP(12)=0
            XMULT(12)=3.6D0
            XADD(12)=0.D0

C          GLOBAL HORIZONTAL SOLAR RADIATION
            INTERP(13)=-13
            TIME_STAMP(13)=0
            XMULT(13)=3.6D0
            XADD(13)=0.D0

C          DIRECT NORMAL SOLAR RADIATION
            INTERP(14)=-14
            TIME_STAMP(14)=0
            XMULT(14)=3.6D0
            XADD(14)=0.D0

C          DIFFUSE HORIZONTAL SOLAR RADIATION
            INTERP(15)=-15
            TIME_STAMP(15)=0
            XMULT(15)=3.6D0
            XADD(15)=0.D0

C          GLOBAL HORIZONTAL ILLUMINANCE
            INTERP(16)=-16
            TIME_STAMP(16)=0
            XMULT(16)=1.D0
            XADD(16)=0.D0

C          DIRECT NORMAL ILLUMINANCE
            INTERP(17)=-17
            TIME_STAMP(17)=0
            XMULT(17)=1.D0
            XADD(17)=0.D0

C          DIFFUSE HORIZONTAL ILLUMINANCE
            INTERP(18)=-18
            TIME_STAMP(18)=0
            XMULT(18)=1.D0
            XADD(18)=0.D0

C          ZENITH LUMINANCE
            INTERP(19)=-19
            TIME_STAMP(19)=0
            XMULT(19)=1.D0
            XADD(19)=0.D0

C          WIND DIRECTION
            INTERP(20)=20
            TIME_STAMP(20)=1
            XMULT(20)=1.D0
            XADD(20)=0.D0

C          WIND SPEED
            INTERP(21)=21
            TIME_STAMP(21)=1
            XMULT(21)=1.D0
            XADD(21)=0.D0

C          TOTAL SKY COVER
            INTERP(22)=22
            TIME_STAMP(22)=1
            XMULT(22)=10.D0
            XADD(22)=0.D0

C          OPAQUE SKY COVER
            INTERP(23)=23
            TIME_STAMP(23)=1
            XMULT(23)=10.D0
            XADD(23)=0.D0

C          VISIBILITY
            INTERP(24)=-24
            TIME_STAMP(24)=1
            XMULT(24)=1.D0
            XADD(24)=0.D0

C          CEILING HEIGHT
            INTERP(25)=-25
            TIME_STAMP(25)=1
            XMULT(25)=1.D0
            XADD(25)=0.D0

C          UNUSED
            INTERP(26)=-26
            TIME_STAMP(26)=1
            XMULT(26)=1.D0
            XADD(26)=0.D0

C          UNUSED
            INTERP(27)=-27
            TIME_STAMP(27)=0
            XMULT(27)=1.D0
            XADD(27)=0.D0

C          PRECIPITABLE WATER
            INTERP(28)=28
            TIME_STAMP(28)=1
            XMULT(28)=1.D0
            XADD(28)=0.D0

C          AEROSOL DEPTH
            INTERP(29)=-29
            TIME_STAMP(29)=0
            XMULT(29)=1.D0
            XADD(29)=0.D0

C          SNOW DEPTH
            INTERP(30)=-30
            TIME_STAMP(30)=1
            XMULT(30)=1.D0
            XADD(30)=0.D0

C          DAYS SINCE LAST SNOWFALL
            INTERP(31)=-31
            TIME_STAMP(31)=0
            XMULT(27)=1.D0
            XADD(31)=0.D0

         ELSE IF(MODE.EQ.4) THEN !INTERNATIONAL WEATHER FOR ENERGY CALCULATIONS
            N_ITEMS=28
            HEADERSKIP=1

C          YEAR
            INTERP(1)=-1 ; TIME_STAMP(1)=0
            XMULT(1)=1.d0 ; XADD(1)=0.d0

C          MONTH
            INTERP(2)=-2 ; TIME_STAMP(2)=0
            XMULT(2)=1.d0 ; XADD(2)=0.d0

C          DAY OF THE MONTH
            INTERP(3)=-3 ; TIME_STAMP(3)=0
            XMULT(3)=1.d0 ; XADD(3)=0.d0

C          HOUR
            INTERP(4)=-4 ; TIME_STAMP(4)=1
            XMULT(4)=1.d0 ; XADD(4)=0.d0

C          GLOBAL EXTRATERRESTRIAL RADIATION (KJ/HR-M2) 
            INTERP(5)=-5 ; TIME_STAMP(5)=0
            XMULT(5)=3.6d0 ; XADD(5)=0.d0
           
C          EXTRATERRESTRIAL DIRECT NORMAL RADIATION (KJ/HR-M2) 
            INTERP(6)=-6 ; TIME_STAMP(6)=0
            XMULT(6)=3.6d0 ; XADD(6)=0.d0
           
C          GLOBAL HORIZONTAL SOLAR RADIATION (KJ/M2)  
            INTERP(7)=-7 ; TIME_STAMP(7)=0
            XMULT(7)=3.6d0 ; XADD(7)=0.d0
           
C          DIRECT NORMAL SOLAR RADIATION (KJ/M2)  
            INTERP(8)=-8 ; TIME_STAMP(8)=0
            XMULT(8)=3.6d0 ; XADD(8)=0.d0 
           
C          DIFFUSE HORIZONTAL SOLAR RADIATION (KJ/M2)  
            INTERP(9)=-9 ; TIME_STAMP(9)=0
            XMULT(9)=3.6d0 ; XADD(9)=0.d0 
           
C          GLOBAL HORIZONTAL ILLUMINANCE (LUX)  
            INTERP(10)=-10 ; TIME_STAMP(10)=0
            XMULT(10)=100.d0 ; XADD(10)=0.d0 
           
C          DIRECT NORMAL ILLUMINANCE (LUX)
            INTERP(11)=-11 ; TIME_STAMP(11)=0
            XMULT(11)=100.d0 ; XADD(11)=0.d0 
           
C          DIFFUSE HORIZONTAL ILLUMINANCE (LUX) 
            INTERP(12)=-12 ; TIME_STAMP(12)=0
            XMULT(12)=100.d0 ; XADD(12)=0.d0 
           
C          ZENITH LUMINANCE (CD/M2) 
            INTERP(13)=-13 ; TIME_STAMP(13)=0
            XMULT(13)=10.d0 ; XADD(13)=0.d0 
           
C          TOTAL SKY COVER (%) 
            INTERP(14)=14 ; TIME_STAMP(14)=1
            XMULT(14)=10.d0  ; XADD(14)=0.d0
           
C          OPAQUE SKY COVER (%) 
            INTERP(15)=15 ; TIME_STAMP(15)=1
            XMULT(15)=10.d0 ; XADD(15)=0.d0 
           
C          DRY BULB TEMP (C)
            INTERP(16)=16 ; TIME_STAMP(16)=0
            XMULT(16)=0.1d0 ; XADD(16)=0.d0 
          
C          DEW POINT TEMP (C)
            INTERP(17)=17 ; TIME_STAMP(17)=0
            XMULT(17)=0.1d0 ; XADD(17)=0.d0 
          
C          RELATIVE HUMIDITY
            INTERP(18)=18 ; TIME_STAMP(18)=0
            XMULT(18)=1.d0 ; XADD(18)=0.d0

C          PRESSURE (ATM)
            INTERP(19)=19 ; TIME_STAMP(19)=0
            XMULT(19)=0.0009869 ; XADD(19)=0.d0 
          
C          WIND DIRECTION (DEGREES)
            INTERP(20)=-20 ; TIME_STAMP(20)=1
            XMULT(20)=1.d0 ; XADD(20)=0.d0

C          WINDSPEED (M/S)
            INTERP(21)=21 ; TIME_STAMP(21)=1
            XMULT(21)=0.1d0 ; XADD(21)=0.d0 
          
C          VISIBILITY (KM)
            INTERP(22)=-22 ; TIME_STAMP(22)=1
            XMULT(22)=0.1 ; XADD(22)=0.d0 
          
C          CEILING HEIGHT (M)
            INTERP(23)=-23 ; TIME_STAMP(23)=1
            XMULT(23)=1.d0 ; XADD(23)=0.d0

C          UNUSED
            INTERP(24)=-24 ; TIME_STAMP(24)=0
            XMULT(24)=1.d0 ; XADD(24)=0.d0

C          PRECIPITABLE WEATHER (MM H2O)
            INTERP(25)=25 ; TIME_STAMP(25)=1
            XMULT(25)=1.d0 ; XADD(25)=0.d0

C          AEROSOL OPTICAL DEPTH (THOUSANDTHS)
            INTERP(26)=-26 ; TIME_STAMP(26)=0
            XMULT(26)=0.001 ; XADD(26)=0.d0 

C          SNOW DEPTH (CM)
            INTERP(27)=-27 ; TIME_STAMP(27)=1
            XMULT(27)=1.d0 ; XADD(27)=0.d0

C          DAYS SINCE LAST SNOWFALL (DAYS)
            INTERP(28)=-28 ; TIME_STAMP(28)=0
            XMULT(28)=1.d0 ; XADD(28)=0.d0

         ELSE IF(MODE.EQ.7) THEN !TMY3
            N_ITEMS=28
            HEADERSKIP=2

C          YEAR
            INTERP(1)=-1 ; TIME_STAMP(1)=0
            XMULT(1)=1.D0 ; XADD(1)=0.D0

C          MONTH
            INTERP(2)=-2 ; TIME_STAMP(2)=0
            XMULT(2)=1.D0 ; XADD(2)=0.D0

C          DAY
            INTERP(3)=-3 ; TIME_STAMP(3)=0
            XMULT(3)=1.D0 ; XADD(3)=0.D0

C          HOUR
            INTERP(4)=-4 ; TIME_STAMP(4)=1
            XMULT(4)=1.D0 ; XADD(4)=0.D0

C          EXTRATERRESTRIAL SOLAR RADIATION
            INTERP(5)=-5
            TIME_STAMP(5)=0
            XMULT(5)=3.6D0
            XADD(5)=0.D0

C          EXTRATERRESTRIAL DIRECT NORMAL SOLAR RADIATION
            INTERP(6)=-6
            TIME_STAMP(6)=0
            XMULT(6)=3.6D0
            XADD(6)=0.D0

C          GLOBAL HORIZONTAL SOLAR RADIATION
            INTERP(7)=-7
            TIME_STAMP(7)=0
            XMULT(7)=3.6D0
            XADD(7)=0.D0

C          DIRECT NORMAL SOLAR RADIATION
            INTERP(8)=-8
            TIME_STAMP(8)=0
            XMULT(8)=3.6D0
            XADD(8)=0.D0

C          DIFFUSE HORIZONTAL SOLAR RADIATION
            INTERP(9)=-9
            TIME_STAMP(9)=0
            XMULT(9)=3.6D0
            XADD(9)=0.D0

C          GLOBAL HORIZONTAL ILLUMINANCE
            INTERP(10)=-10
            TIME_STAMP(10)=0
            XMULT(10)=1.D0
            XADD(10)=0.D0

C          DIRECT NORMAL ILLUMINANCE
            INTERP(11)=-11
            TIME_STAMP(11)=0
            XMULT(11)=1.D0
            XADD(11)=0.D0

C          DIFFUSE HORIZONTAL ILLUMINANCE
            INTERP(12)=-12
            TIME_STAMP(12)=0
            XMULT(12)=1.D0
            XADD(12)=0.D0

C          ZENITH LUMINANCE
            INTERP(13)=-13
            TIME_STAMP(13)=0
            XMULT(13)=1.D0
            XADD(13)=0.D0

C          TOTAL SKY COVER
            INTERP(14)=14
            TIME_STAMP(14)=1
            XMULT(14)=10.D0
            XADD(14)=0.D0

C          OPAQUE SKY COVER
            INTERP(15)=15
            TIME_STAMP(15)=1
            XMULT(15)=10.D0
            XADD(15)=0.D0

C          DRY BULB TEMPERATURE
            INTERP(16)=16
            TIME_STAMP(16)=1
            XMULT(16)=1.0D0
            XADD(16)=0.D0

C          DEW POINT TEMPERATURE
            INTERP(17)=17
            TIME_STAMP(17)=1
            XMULT(17)=1.0D0
            XADD(17)=0.D0

C          RELATIVE HUMIDITY
            INTERP(18)=18
            TIME_STAMP(18)=1
            XMULT(18)=1.D0
            XADD(18)=0.D0

C          ATMOSPHERIC PRESSURE
            INTERP(19)=19
            TIME_STAMP(19)=1
            XMULT(19)=0.001D0/1.01325D0
            XADD(19)=0.D0

C          WIND DIRECTION
            INTERP(20)=20
            TIME_STAMP(20)=1
            XMULT(20)=1.D0
            XADD(20)=0.D0

C          WIND SPEED
            INTERP(21)=21
            TIME_STAMP(21)=1
            XMULT(21)=1.D0
            XADD(21)=0.D0

C          VISIBILITY
            INTERP(22)=-22
            TIME_STAMP(22)=1
            XMULT(22)=0.001D0
            XADD(22)=0.D0

C          CEILING HEIGHT
            INTERP(23)=-23
            TIME_STAMP(23)=1
            XMULT(23)=1.D0
            XADD(23)=0.D0

C          PRECIPITABLE WATER
            INTERP(24)=24
            TIME_STAMP(24)=1
            XMULT(24)=10.D0
            XADD(24)=0.D0

C          AEROSOL DEPTH
            INTERP(25)=-25
            TIME_STAMP(25)=0
            XMULT(25)=1.D0
            XADD(25)=0.D0

C          ALBEDO
            INTERP(26)=-26
            TIME_STAMP(26)=1
            XMULT(26)=1.D0
            XADD(26)=0.D0

C          LIQUID PRECIPITATION DEPTH
            INTERP(27)=-27
            TIME_STAMP(27)=0
            XMULT(27)=1.D0
            XADD(27)=0.D0

C          LIQUID PRECIPITATION TIME
            INTERP(28)=-28
            TIME_STAMP(28)=1
            XMULT(28)=1.D0
            XADD(28)=0.D0

         ENDIF

C       OPEN THE DATA FILE
         OPEN(LU_DATA,ERR=2000)
            
C       READ AND PROCESS THE HEADER INFORMATION AT THE TOP OF THE DATA FILE
         IF(MODE.EQ.1) THEN !TMY
            SHIFT=0.                 !tmy files are in solar time
         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN !TMY2
            READ (LU_DATA,5000,ERR=2000,END=2002) CITY,STATE,TMZONEHR,
     1         LATDIR,LATDEG,LATMIN,LONGDIR,LONGDEG,LONGMIN,ELEV
            LAT=LATDEG+LATMIN/60.
	      IF(LATDIR.EQ.'S') LAT=-LAT
            LONG=LONGDEG+LONGMIN/60.
            TMZONE = TMZONEHR
            IF (TMZONE.GT.0) TMZONE=TMZONE-24.
            TMZONE=-TMZONE 
            IF (LONGDIR.EQ.'E') LONG=360.-LONG
            SHIFT=(TMZONE*15.)-LONG
            
            !Modification to correct for time zones overlapping the prime meridian
            !Mike Wagner, 5/18/09
            IF((TMZONEHR.ge.0.).and.(longdir.eq.'W')) SHIFT=SHIFT-360.
            IF((TMZONEHR.lt.0.).and.(longdir.eq.'E')) SHIFT=(360.+SHIFT)
            !********

         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN !ENERGY+
            READ (LU_DATA,5001,ERR=2000,END=2000) (EPLUSHEADER(I),
     &         I=1,160)
            COMMACNTR=0
            DO I=1,160
               IF(EPLUSHEADER(I).EQ.',') COMMACNTR=COMMACNTR+1
               IF((EPLUSHEADER(I).EQ.' ').AND.(COMMACNTR.LT.9))THEN
                  EPLUSHEADER(I)='_'
               ELSEIF((EPLUSHEADER(I).EQ.' ').AND.
     &         (COMMACNTR.GE.9))THEN
                  EPLUSHEADER(I)=','
               ENDIF
            END DO

            LU=getNextAvailableLogicalUnit()

C          WRITE HEADER INFO TO A TEMP FILE
            OPEN (UNIT=LU,FILE='T15.TMP',STATUS='UNKNOWN')
            WRITE (LU,5001) EPLUSHEADER
            CLOSE (LU,STATUS='KEEP')

C          NOW READ THE INFO INTO THE PROPER VARIABLES
            OPEN (UNIT=LU,FILE='T15.TMP',STATUS='OLD')
            READ (LU,*,ERR=2000,END=2000) KEYWORD,CITY,STATE,COUNTRY,
     &         SOURCE,SITE,LAT,LONG,TMZONEHR,ELEV
            CLOSE (LU,STATUS='DELETE')

C          WRITE OUT USEFUL INFO TO THE LIST FILE
            TMZONE = TMZONEHR
            TMZONE = -TMZONE
            LONG = -LONG
            SHIFT = (TMZONE*15.)-LONG

C          NOW READ THE NEXT SEVEN HEADER LINES AND IGNORE THEM
            DO J=1,7
               READ (LU_DATA,*)
            ENDDO
         ELSE IF(MODE.EQ.4) THEN !IWEC
            READ (LU_DATA,5003,ERR=2000,END=2000) CITY,STATE,COUNTRY,
     &       WMOREGION,TMZNDIR,TMZNHR,TMZNMN,LATDIR,LATDEG,LATMIN,
     &       LONGDIR,LONGDEG,LONGMIN,ELEVDIR,ELEV
            LAT = LATDEG + LATMIN/60.d0
            LONG = LONGDEG +LONGMIN/60.d0
            TMZONE = TMZNHR + TMZNMN/60.d0
            IF (TMZNDIR.EQ.'+') THEN 
              TMZONE = -TMZONE
            ENDIF
            IF (LATDIR.EQ.'S') LAT=-LAT
            IF (LONGDIR.EQ.'E') LONG = -LONG
            SHIFT = TMZONE*15.d0 - LONG
         ELSE IF(MODE.EQ.7) THEN !TMY3
            READ (LU_DATA,*,ERR=2000,END=2002) J,ALPHANUM,STATE,TMZONEHR
	1         ,LAT,LONG,ELEV
            READ (LU_DATA,*)
	      LONG=-LONG
	      TMZONE=-TMZONEHR
            SHIFT=(TMZONE*15.)-LONG
         ENDIF

C       INTIALIZE THE MONTHLY TEMPERATURE ARRAYS
         MONTH_AVE_T=0.
         MONTH_MIN_T=1.D+20
         MONTH_MAX_T=-1.D+20

         ANNUAL_AVE_T=0.
         ANNUAL_MIN_T=1.D+20
         ANNUAL_MAX_T=-1.D+20

C       READ THE INITIAL VALUES FOR THE VARIABLES - 
         DO J=1,8760
            IF(J.LE.744) THEN
               LENGTH_MONTH=744.
               J_MONTH=1
            ELSE IF((J.GT.744).AND.(J.LE.1416)) THEN
               LENGTH_MONTH=672.
               J_MONTH=2
            ELSE IF((J.GT.1416).AND.(J.LE.2160)) THEN
               LENGTH_MONTH=744.
               J_MONTH=3
            ELSE IF((J.GT.2160).AND.(J.LE.2880)) THEN
               LENGTH_MONTH=720.
               J_MONTH=4
            ELSE IF((J.GT.2880).AND.(J.LE.3624)) THEN
               LENGTH_MONTH=744.
               J_MONTH=5
            ELSE IF((J.GT.3624).AND.(J.LE.4344)) THEN
               LENGTH_MONTH=720.
               J_MONTH=6
            ELSE IF((J.GT.4344).AND.(J.LE.5088)) THEN
               LENGTH_MONTH=744.
               J_MONTH=7
            ELSE IF((J.GT.5088).AND.(J.LE.5832)) THEN
               LENGTH_MONTH=744.
               J_MONTH=8
            ELSE IF((J.GT.5832).AND.(J.LE.6552)) THEN
               LENGTH_MONTH=720.
               J_MONTH=9
            ELSE IF((J.GT.6552).AND.(J.LE.7296)) THEN
               LENGTH_MONTH=744.
               J_MONTH=10
            ELSE IF((J.GT.7296).AND.(J.LE.8016)) THEN
               LENGTH_MONTH=720.
               J_MONTH=11
            ELSE
               LENGTH_MONTH=744.
               J_MONTH=12
            ENDIF

            IF(MODE.EQ.1) THEN  !STANDARD TMY
               READ(LU_DATA,6000,ERR=2000,END=2000) (BEGIN(I),
     1            I=1,N_ITEMS)
               J_SPOT=30
            ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN       !TMY2
               READ(LU_DATA,4000,ERR=2000,END=2000) (BEGIN(I),
     1            I=1,N_ITEMS)
               J_SPOT=16
            ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN  !ENERGY+
               READ(LU_DATA,*,END=2000,ERR=2000) BEGIN(1),BEGIN(2),
     &            BEGIN(3),BEGIN(4),BEGIN(5),ALPHANUM,BEGIN(6),BEGIN(7),
     &            BEGIN(8),BEGIN(9),BEGIN(10),BEGIN(11),BEGIN(12),
     &            BEGIN(13),BEGIN(14),BEGIN(15),BEGIN(16),BEGIN(17),
     &            BEGIN(18),BEGIN(19),BEGIN(20),BEGIN(21),BEGIN(22),
     &            BEGIN(23),BEGIN(24),BEGIN(25),BEGIN(26),BEGIN(27),
     &            BEGIN(28),BEGIN(29),BEGIN(30),BEGIN(31)
               J_SPOT=6
            ELSE IF(MODE.EQ.4) THEN  !IWEC
               READ(LU_DATA,6001,ERR=2000,END=2000) (BEGIN(I),
     1            I=1,N_ITEMS)
               J_SPOT=16
            ELSE IF(MODE.EQ.7) THEN       !TMY3
               READ(LU_DATA,5004,ERR=2000,END=2000,ADVANCE='NO') 
	1            BEGIN(1),BEGIN(2),BEGIN(3),BEGIN(4)
               J_SPOT=16
               READ(LU_DATA,*,ERR=2000,END=2000) BEGIN(5),BEGIN(6),
	1            BEGIN(7),ALPHANUM,ALPHANUM,BEGIN(8),ALPHANUM,ALPHANUM,
     1            BEGIN(9),ALPHANUM,ALPHANUM,BEGIN(10),ALPHANUM,ALPHANUM
     1           ,BEGIN(11),ALPHANUM,ALPHANUM,BEGIN(12),ALPHANUM,
     1            ALPHANUM,BEGIN(13),ALPHANUM,ALPHANUM,BEGIN(14),
     1            ALPHANUM,ALPHANUM,BEGIN(15),ALPHANUM,ALPHANUM,
     1            BEGIN(16),ALPHANUM,ALPHANUM,BEGIN(17),ALPHANUM,
     1            ALPHANUM,BEGIN(18),ALPHANUM,ALPHANUM,BEGIN(19),
     1            ALPHANUM,ALPHANUM,BEGIN(20),ALPHANUM,ALPHANUM,
     1            BEGIN(21),ALPHANUM,ALPHANUM,BEGIN(22),ALPHANUM,
     1            ALPHANUM,BEGIN(23),ALPHANUM,ALPHANUM,BEGIN(24),
     1            ALPHANUM,ALPHANUM,BEGIN(25),ALPHANUM,ALPHANUM,
     1            BEGIN(26),ALPHANUM,ALPHANUM,BEGIN(27),BEGIN(28)
            ENDIF

C          UPDATE THE ANNUAL AVERAGE TEMPERATURE    
            ANNUAL_AVE_T=ANNUAL_AVE_T+BEGIN(J_SPOT)/8760.

C          CALCULATE THE MONTHLY AVERAGE, MINIMUM AND MAXIMUM TEMPERATURES
            IF(BEGIN(J_SPOT).LT.MONTH_MIN_T(J_MONTH)) THEN
               MONTH_MIN_T(J_MONTH)=BEGIN(J_SPOT)
            ENDIF

            IF(BEGIN(J_SPOT).GT.MONTH_MAX_T(J_MONTH)) THEN
               MONTH_MAX_T(J_MONTH)=BEGIN(J_SPOT)
            ENDIF
 
            IF(BEGIN(J_SPOT).LT.ANNUAL_MIN_T) ANNUAL_MIN_T=BEGIN(J_SPOT)
            IF(BEGIN(J_SPOT).GT.ANNUAL_MAX_T) ANNUAL_MAX_T=BEGIN(J_SPOT)

            MONTH_AVE_T(J_MONTH)=MONTH_AVE_T(J_MONTH)+BEGIN(J_SPOT)/
     1         LENGTH_MONTH

         ENDDO

C       REPLACE SOME 'MISSING DATA' CONDITIONS IN STANDARD TMY FILES APPROPRIATE VALUES
         IF(MODE.EQ.1) THEN         !STANDARD TMY
           DO I=1,N_ITEMS
             !replace 'missing radiation' values (9999) with zeros.
             IF((BEGIN(I).GT.9998.).AND.(BEGIN(I).LT.10000.)) THEN
               BEGIN(I) = 0.D0
             ENDIF
             !replace 'missing sky cover' values (99) with the last read value (0 in this case).
             IF(((I.EQ.34).OR.(I.EQ.35)).AND.(BEGIN(I).GT.
     &        98)) THEN
               BEGIN(I) = 0.D0
             ENDIF
           ENDDO
C       CORRECT THE PRECIPTABLE WATER
         ELSE IF(MODE.EQ.7) THEN
	      IF(BEGIN(27).LT.0.) BEGIN(27)=0.
	      IF(BEGIN(28).LT.0.) BEGIN(28)=0.
	   ENDIF
CNB      REPLACE SOME 'MISSING DATA' CONDITIONS IN TMY2 FILES WITH APPROPRIATE VALUES 10-8-07
           IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
               !replace 'missing snow cover' values (999) with the last read value.
             DO I=1,N_ITEMS
               IF((I.EQ.26).AND.(BEGIN(I).GT.998)) THEN
                  BEGIN(I) = 0.
               ENDIF
             ENDDO
           ENDIF


C       USE THE INTERPOLATION FACTORS TO CORRECT THE AVERAGES
	   MONTH_AVE_T=MONTH_AVE_T*XMULT(J_SPOT)+XADD(J_SPOT)
	   MONTH_MIN_T=MONTH_MIN_T*XMULT(J_SPOT)+XADD(J_SPOT)
	   MONTH_MAX_T=MONTH_MAX_T*XMULT(J_SPOT)+XADD(J_SPOT)

	   ANNUAL_AVE_T=ANNUAL_AVE_T*XMULT(J_SPOT)+XADD(J_SPOT)
	   ANNUAL_MIN_T=ANNUAL_MIN_T*XMULT(J_SPOT)+XADD(J_SPOT)
	   ANNUAL_MAX_T=ANNUAL_MAX_T*XMULT(J_SPOT)+XADD(J_SPOT)

C       SET THE ROLLOVER MONTH VALUES
	   MONTH_AVE_T(0)=MONTH_AVE_T(12)
	   MONTH_MIN_T(0)=MONTH_MIN_T(12)
	   MONTH_MAX_T(0)=MONTH_MAX_T(12)

	   MONTH_AVE_T(13)=MONTH_AVE_T(1)
	   MONTH_MIN_T(13)=MONTH_MIN_T(1)
	   MONTH_MAX_T(13)=MONTH_MAX_T(1)

C       SET SOME PARAMETERS ACCORDING TO THE ENERGYGAUGE METHOD OF CALCULATING THE SEASONS
         WDT_MIN=(59.-32.)/1.8
	   T_AVE_HEAT=(71.5-32.)/1.8
	   T_AVE_COOL=(66.-32.)/1.8

C       CALCULATE THE HEATING AND COOLING SEASON INDICATORS
         DO J=1,12
            IF((J.EQ.1).AND.(ANNUAL_MIN_T.LT.WDT_MIN)) THEN
	         X_HEAT(J)=1.
            ELSE IF((J.EQ.12).AND.(ANNUAL_MIN_T.LT.WDT_MIN)) THEN
	         X_HEAT(J)=1.
	      ELSE IF(MONTH_AVE_T(J).LE.T_AVE_HEAT) THEN
	         X_HEAT(J)=1.
	      ELSE
	         X_HEAT(J)=0.
	      ENDIF

            IF((J.EQ.7).OR.(J.EQ.8)) THEN
	         X_COOL(J)=1.
	      ELSE IF(MONTH_AVE_T(J).GE.T_AVE_COOL) THEN
	         X_COOL(J)=1.
	      ELSE
	         X_COOL(J)=0.
	      ENDIF
	   ENDDO

	   X_HEAT(0)=X_HEAT(12)
	   X_HEAT(13)=X_HEAT(1)

	   X_COOL(0)=X_COOL(12)
	   X_COOL(13)=X_COOL(1)

C       PERFORM A CHECK ON THE SWING SEASON MONTHS
         X_SWING=0.
	   DO J=0,12
	      IF((X_HEAT(J).GE.0.5).AND.(X_COOL(J+1).GE.0.5)) THEN
	         X_SWING(J)=1.
	         X_SWING(J+1)=1.
	      ENDIF
           
	      IF((X_COOL(J).GE.0.5).AND.(X_HEAT(J+1).GE.0.5)) THEN
	         X_SWING(J)=1.
	         X_SWING(J+1)=1.
	      ENDIF
	   ENDDO

C       SET THE HEATING AND COOLING SEASON INDICATORS
	   DO J=0,12
	      X_HEAT(J)=DMAX1(X_HEAT(J),X_SWING(J))
	      X_COOL(J)=DMAX1(X_COOL(J),X_SWING(J))
	   ENDDO

C      SET THE VALUES AT THE CURRENT TIME
        DO J=1,N_ITEMS
           VALUE_NOW(J)=BEGIN(J)
        ENDDO

C       REWIND THE FILE AND READ THE HEADER LINE
         REWIND(LU_DATA)
         DO J=1,HEADERSKIP
            READ(LU_DATA,*,ERR=2000,END=2000)
         ENDDO

C       FIGURE OUT HOW MANY LINES TO SKIP TO FIND THE RIGHT POINT IN THE DATA FILE TO START READING
         ISKIP=MAX(0,JFIX(TIME+0.5/3600.))

C       SKIP THE RIGHT NUMBER OF LINES
         DO J=1,ISKIP
           IF(MODE.EQ.1) THEN   !STANDARD TMY
             READ(LU_DATA,6000,ERR=2000,END=150) 
     &        (VALUE_NOW(I),I=1,N_ITEMS)
             IF(VALUE_NOW(I).EQ.9999) VALUE_NOW(I)=0.d0
           ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
             READ(LU_DATA,4000,ERR=2000,END=150) 
     &        (VALUE_NOW(I),I=1,N_ITEMS)
           ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN   !ENERGY+
             READ(LU_DATA,*,END=150,ERR=2000) VALUE_NOW(1),VALUE_NOW(2),
     &        VALUE_NOW(3),VALUE_NOW(4),VALUE_NOW(5),ALPHANUM,
     &        VALUE_NOW(6),VALUE_NOW(7),VALUE_NOW(8),VALUE_NOW(9),
     &        VALUE_NOW(10),VALUE_NOW(11),VALUE_NOW(12),VALUE_NOW(13),
     &        VALUE_NOW(14),VALUE_NOW(15),VALUE_NOW(16),VALUE_NOW(17),
     &        VALUE_NOW(18),VALUE_NOW(19),VALUE_NOW(20),VALUE_NOW(21),
     &        VALUE_NOW(22),VALUE_NOW(23),VALUE_NOW(24),VALUE_NOW(25),
     &        VALUE_NOW(26),VALUE_NOW(27),VALUE_NOW(28),VALUE_NOW(29),
     &        VALUE_NOW(30),VALUE_NOW(31)
           ELSE IF(MODE.EQ.4) THEN   !IWEC
             READ(LU_DATA,6001,ERR=2000,END=150) 
     &        (VALUE_NOW(I),I=1,N_ITEMS)
           ELSE IF(MODE.EQ.7) THEN   !TMY3
             READ(LU_DATA,5004,ERR=2000,END=150,ADVANCE='NO') 
	1          VALUE_NOW(1),VALUE_NOW(2),VALUE_NOW(3),VALUE_NOW(4)
             READ(LU_DATA,*,ERR=2000,END=150) VALUE_NOW(5),VALUE_NOW(6)
     1         ,VALUE_NOW(7),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(8),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(9),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(10),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(11),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(12),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(13),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(14),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(15),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(16),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(17),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(18),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(19),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(20),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(21),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(22),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(23),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(24),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(25),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(26),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(27),VALUE_NOW(28)
           ENDIF
           GOTO 160

150        CALL REWIND(TIME,LU_DATA,HEADERSKIP,IUNIT,ITYPE,IE_READ)
           IF(MODE.EQ.1) THEN   !STANDARD TMY
             READ(LU_DATA,6000,ERR=2000,END=150) 
     &        (VALUE_NOW(I),I=1,N_ITEMS)
           ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
             READ(LU_DATA,4000,ERR=2000,END=2000) 
     &        (VALUE_NOW(I),I=1,N_ITEMS)
           ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN   !ENERGY+
             READ(LU_DATA,*,END=2000,ERR=2000) VALUE_NOW(1),VALUE_NOW(2)
     &        ,VALUE_NOW(3),VALUE_NOW(4),VALUE_NOW(5),ALPHANUM,
     &        VALUE_NOW(6),VALUE_NOW(7),VALUE_NOW(8),VALUE_NOW(9),
     &        VALUE_NOW(10),VALUE_NOW(11),VALUE_NOW(12),VALUE_NOW(13),
     &        VALUE_NOW(14),VALUE_NOW(15),VALUE_NOW(16),VALUE_NOW(17),
     &        VALUE_NOW(18),VALUE_NOW(19),VALUE_NOW(20),VALUE_NOW(21),
     &        VALUE_NOW(22),VALUE_NOW(23),VALUE_NOW(24),VALUE_NOW(25),
     &        VALUE_NOW(26),VALUE_NOW(27),VALUE_NOW(28),VALUE_NOW(29),
     &        VALUE_NOW(30),VALUE_NOW(31)
           ELSE IF(MODE.EQ.4) THEN   !IWEC
             READ(LU_DATA,6001,ERR=2000,END=150) 
     &        (VALUE_NOW(I),I=1,N_ITEMS)
           ELSE IF(MODE.EQ.7) THEN   !TMY3
             READ(LU_DATA,5004,ERR=2000,END=2000,ADVANCE='NO') 
	1          VALUE_NOW(1),VALUE_NOW(2),VALUE_NOW(3),VALUE_NOW(4)
             READ(LU_DATA,*,ERR=2000,END=2000) VALUE_NOW(5),VALUE_NOW(6)
     1         ,VALUE_NOW(7),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(8),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(9),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(10),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(11),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(12),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(13),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(14),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(15),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(16),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(17),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(18),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(19),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(20),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(21),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(22),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(23),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(24),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(25),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(26),ALPHANUM,ALPHANUM
     1         ,VALUE_NOW(27),VALUE_NOW(28)
           ENDIF

C       REPLACE SOME 'MISSING DATA' CONDITIONS IN STANDARD TMY FILES APPROPRIATE VALUES
           IF(MODE.EQ.1) THEN         !STANDARD TMY
             DO I=1,N_ITEMS
               !replace 'missing radiation' values (9999) with zeros.
               IF((VALUE_NOW(I).GT.9998.).AND.(VALUE_NOW(I).LT.
     &          10000.)) THEN
                  VALUE_NOW(I) = 0.D0
               ENDIF
               !replace 'missing sky cover' values (99) with the last read value.
               IF(((I.EQ.34).OR.(I.EQ.35)).AND.(VALUE_NOW(I).GT.
     &          98)) THEN
                  VALUE_NOW(I) = VALUE_WAS(I)
               ENDIF
             ENDDO
           ELSE IF(MODE.EQ.7) THEN
	       IF(VALUE_NOW(27).LT.0.) VALUE_NOW(27)=0.
	       IF(VALUE_NOW(28).LT.0.) VALUE_NOW(28)=0.
           ENDIF

CNB      REPLACE SOME 'MISSING DATA' CONDITIONS IN TMY2 FILES WITH APPROPRIATE VALUES 10-8-07
           IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
               !replace 'missing snow cover' values (999) with the last read value.
             DO I=1,N_ITEMS
               IF((I.EQ.26).AND.(VALUE_NOW(I).GT.998)) THEN
                  VALUE_NOW(I) = VALUE_WAS(I)
               ENDIF
             ENDDO
           ENDIF

160        IF(IE_READ.GT.0) GOTO 2000

C          IF INTERPOLATING AVERAGE VALUES, CALCULATE THE VALUE AT THE END OF THE TIMESTEP
           DO K=1,N_ITEMS
             IF((INTERP(K).GT.0).AND.(TIME_STAMP(K).EQ.0)) THEN
                BEGIN(K)=2.D0*VALUE_NOW(K)-BEGIN(K)
             ELSE
                BEGIN(K)=VALUE_NOW(K)
             ENDIF
           ENDDO
         ENDDO

C       DETERMINE THE NEXT RESET TIME FOR ABSOLUTE INTEGRATIONS
         I=JFIX((TIME0+DELT/2.D0))
         TIME_NEXT=DBLE(I)+1.D0

C       READ THE VALUES AT THE CURRENT TIMESTEP
         IF(MODE.EQ.1) THEN   !STANDARD TMY
           READ(LU_DATA,6000,ERR=2000,END=190) 
     1          (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
           READ(LU_DATA,4000,ERR=2000,END=190) 
     1          (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN   !ENERGYPLUS / CWEC
          READ(LU_DATA,*,END=190,ERR=2000) VALUE_NEXT(1),VALUE_NEXT(2)
     &      ,VALUE_NEXT(3),VALUE_NEXT(4),VALUE_NEXT(5),ALPHANUM,
     &       VALUE_NEXT(6),VALUE_NEXT(7),VALUE_NEXT(8),VALUE_NEXT(9),
     &       VALUE_NEXT(10),VALUE_NEXT(11),VALUE_NEXT(12),VALUE_NEXT(13)
     &      ,VALUE_NEXT(14),VALUE_NEXT(15),VALUE_NEXT(16),VALUE_NEXT(17)
     &      ,VALUE_NEXT(18),VALUE_NEXT(19),VALUE_NEXT(20),VALUE_NEXT(21)
     &      ,VALUE_NEXT(22),VALUE_NEXT(23),VALUE_NEXT(24),VALUE_NEXT(25)
     &      ,VALUE_NEXT(26),VALUE_NEXT(27),VALUE_NEXT(28),VALUE_NEXT(29)
     &      ,VALUE_NEXT(30),VALUE_NEXT(31)
         ELSE IF(MODE.EQ.4) THEN   !IWEC
           READ(LU_DATA,6001,ERR=2000,END=190) 
     1          (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF(MODE.EQ.7) THEN   !TMY3
           READ(LU_DATA,5004,ERR=2000,END=190,ADVANCE='NO') 
	1        VALUE_NEXT(1),VALUE_NEXT(2),VALUE_NEXT(3),VALUE_NEXT(4)
           READ(LU_DATA,*,ERR=2000,END=190) 
	1        VALUE_NEXT(5),VALUE_NEXT(6)
     1       ,VALUE_NEXT(7),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(8),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(9),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(10),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(11),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(12),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(13),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(14),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(15),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(16),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(17),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(18),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(19),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(20),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(21),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(22),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(23),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(24),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(25),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(26),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(27),VALUE_NEXT(28)
         ENDIF

C       REPLACE SOME 'MISSING DATA' CONDITIONS IN STANDARD TMY FILES APPROPRIATE VALUES
         IF(MODE.EQ.1) THEN         !STANDARD TMY
            DO I=1,N_ITEMS
               !replace 'missing radiation' values (9999) with zeros.
               IF((VALUE_NEXT(I).GT.9998.).AND.(VALUE_NEXT(I).LT.
     &          10000.)) THEN
                  VALUE_NEXT(I) = 0.D0
               ENDIF
               !replace 'missing sky cover' values (99) with the last read value.
               IF(((I.EQ.34).OR.(I.EQ.35)).AND.(VALUE_NEXT(I).GT.
     &          98)) THEN
                  VALUE_NEXT(I) = VALUE_NOW(I)
               ENDIF
            ENDDO
         ELSE IF(MODE.EQ.7) THEN
	     IF(VALUE_NEXT(27).LT.0.) VALUE_NEXT(27)=VALUE_NOW(27)
	     IF(VALUE_NEXT(28).LT.0.) VALUE_NEXT(28)=VALUE_NOW(28)
         ENDIF
CNB      REPLACE SOME 'MISSING DATA' CONDITIONS IN TMY2 FILES WITH APPROPRIATE VALUES 10-8-07
           IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
               !replace 'missing snow cover' values (999) with the last read value.
             DO I=1,N_ITEMS
               IF((I.EQ.26).AND.(VALUE_NOW(I).GT.998)) THEN
                  VALUE_NOW(I) = VALUE_WAS(I)
               ENDIF
             ENDDO
           ENDIF

         GOTO 200

190      CALL REWIND(TIME,LU_DATA,HEADERSKIP,IUNIT,ITYPE,IE_READ)
         IF(MODE.EQ.1) THEN   !STANDARD TMY
           READ(LU_DATA,6000,ERR=2000,END=2000) 
     1          (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
           READ(LU_DATA,4000,ERR=2000,END=2000) 
     1          (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN   !ENERGYPLUS / CWEC
          READ(LU_DATA,*,END=2000,ERR=2000) VALUE_NEXT(1),VALUE_NEXT(2)
     &     ,VALUE_NEXT(3),VALUE_NEXT(4),VALUE_NEXT(5),ALPHANUM,
     &      VALUE_NEXT(6),VALUE_NEXT(7),VALUE_NEXT(8),VALUE_NEXT(9),
     &      VALUE_NEXT(10),VALUE_NEXT(11),VALUE_NEXT(12),VALUE_NEXT(13),
     &      VALUE_NEXT(14),VALUE_NEXT(15),VALUE_NEXT(16),VALUE_NEXT(17),
     &      VALUE_NEXT(18),VALUE_NEXT(19),VALUE_NEXT(20),VALUE_NEXT(21),
     &      VALUE_NEXT(22),VALUE_NEXT(23),VALUE_NEXT(24),VALUE_NEXT(25),
     &      VALUE_NEXT(26),VALUE_NEXT(27),VALUE_NEXT(28),VALUE_NEXT(29),
     &      VALUE_NEXT(30),VALUE_NEXT(31)
         ELSE IF(MODE.EQ.4) THEN   !IWEC
           READ(LU_DATA,6001,ERR=2000,END=2000) 
     1          (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF(MODE.EQ.7) THEN   !TMY3
           READ(LU_DATA,5004,ERR=2000,END=2000,ADVANCE='NO') 
	1        VALUE_NEXT(1),VALUE_NEXT(2),VALUE_NEXT(3),VALUE_NEXT(4)
           READ(LU_DATA,*,ERR=2000,END=2000) 
	1        VALUE_NEXT(5),VALUE_NEXT(6)
     1       ,VALUE_NEXT(7),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(8),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(9),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(10),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(11),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(12),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(13),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(14),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(15),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(16),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(17),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(18),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(19),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(20),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(21),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(22),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(23),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(24),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(25),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(26),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(27),VALUE_NEXT(28)
         ENDIF

C       REPLACE SOME 'MISSING DATA' CONDITIONS IN STANDARD TMY FILES APPROPRIATE VALUES
         IF(MODE.EQ.1) THEN         !STANDARD TMY
            DO I=1,N_ITEMS
               !replace 'missing radiation' values (9999) with zeros.
               IF((VALUE_NEXT(I).GT.9998.).AND.(VALUE_NEXT(I).LT.
     &          10000.)) THEN
                  VALUE_NEXT(I) = 0.D0
               ENDIF
               !replace 'missing sky cover' values (99) with the last read value.
               IF(((I.EQ.34).OR.(I.EQ.35)).AND.(VALUE_NEXT(I).GT.
     &          98)) THEN
                  VALUE_NEXT(I) = VALUE_NOW(I)
               ENDIF
            ENDDO
         ELSE IF(MODE.EQ.7) THEN
	     IF(VALUE_NEXT(27).LT.0.) VALUE_NEXT(27)=VALUE_NOW(27)
	     IF(VALUE_NEXT(28).LT.0.) VALUE_NEXT(28)=VALUE_NOW(28)
         ENDIF

CNB      REPLACE SOME 'MISSING DATA' CONDITIONS IN TMY2 FILES WITH APPROPRIATE VALUES 10-8-07
           IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
               !replace 'missing snow cover' values (999) with the last read value.
             DO I=1,N_ITEMS
               IF((I.EQ.26).AND.(VALUE_NOW(I).GT.998)) THEN
                  VALUE_NOW(I) = VALUE_WAS(I)
               ENDIF
             ENDDO
           ENDIF


200      IF(IE_READ.GT.0) GOTO 2000

C       SET THE PREVIOUS VALUES AS WELL
         DO J=1,N_ITEMS
            VALUE_WAS(J)=VALUE_NOW(J)
         ENDDO
                  
C       SET THE INITIAL STORAGE VARIABLES HERE
         STORE(1)=TIME_NEXT
         DO J=1,N_ITEMS
            STORE(1+J)=VALUE_WAS(J)
            STORE(1+N_ITEMS+J)=VALUE_NOW(J)
            STORE(1+2*N_ITEMS+J)=VALUE_NEXT(J)
            STORE(1+3*N_ITEMS+J)=BEGIN(J)
            STORE(1+4*N_ITEMS+J)=BEGIN(J)

            STORE(1+5*N_ITEMS+J)=DBLE(INTERP(J))
            STORE(1+6*N_ITEMS+J)=DBLE(TIME_STAMP(J))
            STORE(1+7*N_ITEMS+J)=XMULT(J)
            STORE(1+8*N_ITEMS+J)=XADD(J)
         ENDDO

	   STORE(1+9*N_ITEMS+1)=ANNUAL_AVE_T
	   STORE(1+9*N_ITEMS+2)=ANNUAL_MIN_T
	   STORE(1+9*N_ITEMS+3)=ANNUAL_MAX_T

	   DO J=1,14
	      STORE(1+9*N_ITEMS+3+J)=MONTH_AVE_T(J-1)
	      STORE(1+9*N_ITEMS+3+14+J)=MONTH_MIN_T(J-1)
	      STORE(1+9*N_ITEMS+3+28+J)=MONTH_MAX_T(J-1)
	      STORE(1+9*N_ITEMS+3+42+J)=X_HEAT(J-1)
	      STORE(1+9*N_ITEMS+3+56+J)=X_COOL(J-1)
	   ENDDO

	   CALL SetStorageVars(STORE,74+9*N_ITEMS,INFO)

C       SET THE VALUE OF THE INITIAL OUTPUTS
         DO J=1,N_ITEMS

C          INTERPOLATING OF AVERAGE VALUES          
            IF((INTERP(J).GT.0).AND.(TIME_STAMP(J).EQ.0)) THEN

C             CALCULATE THE INSTANTANEOUS VALUE AT THE END OF THE DATA TIMESTEP
               ENDING(J)=2.*VALUE_NEXT(J)-BEGIN(J)

C             CALCULATE THE INTERPOLATION RATIO
               RATIO=(TIME_NEXT-TIME)

C             CALCULATE THE INSTANTANEOUS VALUE AT THE BEGINNING OF THE SIMULATION
               XNOW=BEGIN(J)+(1.D0-RATIO)*(ENDING(J)-BEGIN(J))

C             SET THE INSTANTANEOUS VALUE AT THE END OF THE NEXT TIMESTEP
               RATIO=(TIME_NEXT-TIME-DELT)
               XNEXT=BEGIN(J)+(1.D0-RATIO)*(ENDING(J)-BEGIN(J))

               OUT_TEMP(J)=XNOW*XMULT(J)+XADD(J)

C          INERPOLATING INSTANTANEOUS VALUES
            ELSE IF(INTERP(J).GT.0) THEN

C             CALCULATE THE INSTANTANEOUS VALUE AT THE END OF THE DATA TIMESTEP
               ENDING(J)=VALUE_NEXT(J)

C             CALCULATE THE INTERPOLATION RATIO
               RATIO=(TIME_NEXT-TIME)

C             CALCULATE THE INSTANTANEOUS VALUE AT THE BEGINNING OF THE SIMULATION
               XNOW=BEGIN(J)+(1.D0-RATIO)*(ENDING(J)-BEGIN(J))

C             SET THE INSTANTANEOUS VALUE AT THE END OF THE NEXT TIMESTEP
               RATIO=(TIME_NEXT-TIME-DELT)
               XNEXT=BEGIN(J)+(1.D0-RATIO)*(ENDING(J)-BEGIN(J))

               OUT_TEMP(J)=XNOW*XMULT(J)+XADD(J)

C          NO INTERPOLATION OF VALUES
            ELSE
               IF(TIME.GT.(TIME_NEXT-1.D0+DELT/2.)) THEN
                  OUT_TEMP(J)=VALUE_NEXT(J)*XMULT(J)+XADD(J)
               ELSE
                  OUT_TEMP(J)=VALUE_NOW(J)*XMULT(J)+XADD(J)
               ENDIF

            ENDIF
         ENDDO
         
         TD1=JFIX(TIME-1./3600.D0/2.D0)
         TD2=JFIX(TIME-1./3600.D0/2.D0)+1.D0

C       CALCULATE THE CURRENT MONTH
         IF((DMOD(TIME,8760.D0)-DELT/2.).LE.744) THEN
	      J_MONTH=1
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.1416.) THEN
	      J_MONTH=2
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.2160.) THEN
	      J_MONTH=3
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.2880.) THEN
	      J_MONTH=4
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.3624.) THEN
	      J_MONTH=5
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.4344.) THEN
	      J_MONTH=6
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.5088.) THEN
	      J_MONTH=7
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.5832.) THEN
	      J_MONTH=8
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.6552.) THEN
	      J_MONTH=9
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.7296.) THEN
	      J_MONTH=10
         ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.8016.) THEN
	      J_MONTH=11
         ELSE
	      J_MONTH=12
	   ENDIF

C       NOW SET THE OUTPUTS BASED ON THE MODE
         IF(MODE.EQ.1) THEN
        
C          MONTH
            OUT(46+8*NS)=OUT_TEMP(2)

C          HOUR OF THE MONTH
            OUT(47+8*NS)=OUT_TEMP(4)

C          IDN
            OUT(15)=OUT_TEMP(7)

C          I
            OUT(14)=OUT_TEMP(13)

C          TDB
            OUT(1)=OUT_TEMP(30) 

C          WIND SPEED
            OUT(8)=OUT_TEMP(33) 

C          WIND DIRECTION
            OUT(9)=OUT_TEMP(32) 

C          DEW POINT
            OUT(2)=OUT_TEMP(31) 

C          ILL
            OUT(36+8*NS)=-99.d0 

C          ILL_DN
            OUT(37+8*NS)=-99.d0 0

C          ILL_DIFF
            OUT(38+8*NS)=-99.d0 0

C          ILL_ZEN
            OUT(39+8*NS)=-99.d0 

C          SKY COVER
            OUT(11)=OUT_TEMP(34) 

C          OPAQUE COVER
            OUT(12)=OUT_TEMP(35) 

C          PRESSURE
            OUT(10)=OUT_TEMP(29) 

C          HORIZ VISIBILITY
            OUT(40+8*NS)=OUT_TEMP(19) 

C          CEILING HEIGHT
            OUT(41+8*NS)=OUT_TEMP(17)

C          PRECIP WATER
            OUT(42+8*NS)=-99.d0 

C          OPTICAL DEPTH
            OUT(43+8*NS)=-99.d0 

C          SNOW DEPTH
            OUT(44+8*NS)=-99.d0 

C          DAYS SINCE LAST SNOW
            OUT(45+8*NS)=-99.d0 

C          W, TWB, AND %RH - CALCULATED FROM TDB, PRESSURE AND TDP
            PSYDAT(1)=OUT(10)
            PSYDAT(2)=OUT(1)
            PSYDAT(5)=OUT(2)
            CALL PSYCHROMETRICS(TIME,INFO,1,3,1,PSYDAT,0,IE,*207)
207         OUT(6)=PSYDAT(6)           !HUMIDITY RATIO W
            OUT(7)=PSYDAT(4)*100.D0    !RELATIVE HUMIDITY RH
            OUT(3)=PSYDAT(3)

         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN  !TMY2
        
C          MONTH
            OUT(46+8*NS)=OUT_TEMP(2)

C          HOUR OF THE MONTH
            OUT(47+8*NS)=(OUT_TEMP(3)-1.D0)*24.D0+OUT_TEMP(4)

C          IDN
            OUT(15)=OUT_TEMP(8)

C          I
            OUT(14)=OUT_TEMP(7)

C          TDB
            OUT(1)=OUT_TEMP(16)

C          WIND SPEED
            OUT(8)=OUT_TEMP(21)

C          WIND DIRECTION
            OUT(9)=OUT_TEMP(20)

C          DEW POINT
            OUT(2)=OUT_TEMP(17)

C          RELATIVE HUMIDITY
            OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(18)))))

C          ILL
            OUT(36+8*NS)=OUT_TEMP(10)

C          ILL_DN
            OUT(37+8*NS)=OUT_TEMP(11)

C          ILL_DIFF
            OUT(38+8*NS)=OUT_TEMP(12)

C          ILL_ZEN
            OUT(39+8*NS)=OUT_TEMP(13)

C          SKY COVER
            OUT(11)=OUT_TEMP(14)

C          OPAQUE COVER
            OUT(12)=OUT_TEMP(15)

C          PRESSURE
            OUT(10)=OUT_TEMP(19)

C          HORIZ VISIBILITY
            OUT(40+8*NS)=OUT_TEMP(22)

C          CEILING HEIGHT
            OUT(41+8*NS)=OUT_TEMP(23)

C          PRECIP WATER
            OUT(42+8*NS)=OUT_TEMP(24)

C          OPTICAL DEPTH
            OUT(43+8*NS)=OUT_TEMP(25)

C          SNOW DEPTH
            OUT(44+8*NS)=OUT_TEMP(26)

C          DAYS SINCE LAST SNOW
            OUT(45+8*NS)=OUT_TEMP(27)

C          W AND TWB - CALCULATED FROM TDB, PRESSURE AND %RH
            PSYDAT(1)=OUT(10)
            PSYDAT(2)=OUT(1)
            PSYDAT(4)=OUT(7)/100.
            CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*201)
201         OUT(6)=PSYDAT(6)
            OUT(3)=PSYDAT(3)

         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN
        
C          MONTH
            OUT(46+8*NS)=OUT_TEMP(2)

C          HOUR OF THE MONTH
            OUT(47+8*NS)=(OUT_TEMP(3)-1.D0)*24.D0+OUT_TEMP(4)

C          IDN
            OUT(15)=OUT_TEMP(14)

C          I
            OUT(14)=OUT_TEMP(13)

C          TDB
            OUT(1)=OUT_TEMP(6)

C          WIND SPEED
            OUT(8)=OUT_TEMP(21)

C          WIND DIRECTION
            OUT(9)=OUT_TEMP(20)

C          DEW POINT
            OUT(2)=OUT_TEMP(7)

C          RELATIVE HUMIDITY
            OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(8)))))

C          ILL
            OUT(36+8*NS)=OUT_TEMP(16)

C          ILL_DN
            OUT(37+8*NS)=OUT_TEMP(17)

C          ILL_DIFF
            OUT(38+8*NS)=OUT_TEMP(18)

C          ILL_ZEN
            OUT(39+8*NS)=OUT_TEMP(19)

C          SKY COVER
            OUT(11)=OUT_TEMP(22)

C          OPAQUE COVER
            OUT(12)=OUT_TEMP(23)

C          PRESSURE
            OUT(10)=OUT_TEMP(9)

C          HORIZ VISIBILITY
            OUT(40+8*NS)=OUT_TEMP(24)

C          CEILING HEIGHT
            OUT(41+8*NS)=OUT_TEMP(25)

C          PRECIP WATER
            OUT(42+8*NS)=OUT_TEMP(28)

C          OPTICAL DEPTH
            OUT(43+8*NS)=OUT_TEMP(29)

C          SNOW DEPTH
            OUT(44+8*NS)=OUT_TEMP(30)

C          DAYS SINCE LAST SNOW
            OUT(45+8*NS)=OUT_TEMP(31)

C          W AND TWB - CALCULATED FROM TDB, PRESSURE AND %RH
            PSYDAT(1)=OUT(10)
            PSYDAT(2)=OUT(1)
            PSYDAT(4)=OUT(7)/100.
            CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*203)
203         OUT(6)=PSYDAT(6)
            OUT(3)=PSYDAT(3)

         ELSE IF(MODE.EQ.4) THEN !IWEC

C          MONTH
            OUT(46+8*NS)=OUT_TEMP(2) 

C          HOUR OF THE MONTH
            OUT(47+8*NS)=(OUT_TEMP(3)-1.d0)*24.d0+OUT_TEMP(4)

C          DIRECT NORMAL SOLAR RADIATION (KJ/M2)  
            OUT(15)=OUT_TEMP(8) 

C          GLOBAL HORIZONTAL SOLAR RADIATION (KJ/M2)  
            OUT(14)=OUT_TEMP(7) 
           
C          DRY BULB TEMP (C)
            OUT(1)=OUT_TEMP(16) 
          
C          WINDSPEED (M/S)
            OUT(8)=OUT_TEMP(21) 

C          WIND DIRECTION (DEGREES)
            OUT(9)=OUT_TEMP(20) 

C          DEW POINT TEMP (C)
            OUT(2)=OUT_TEMP(17) 
          
C          RELATIVE HUMIDITY
            OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(18)))))

C          GLOBAL HORIZONTAL ILLUMINANCE (LUX)  
            OUT(36+8*NS)=OUT_TEMP(10) 
           
C          DIRECT NORMAL ILLUMINANCE (LUX)
            OUT(37+8*NS)=OUT_TEMP(11) 
           
C          DIFFUSE HORIZONTAL ILLUMINANCE (LUX) 
            OUT(38+8*NS)=OUT_TEMP(12) 
           
C          ZENITH LUMINANCE (CD/M2) 
            OUT(39+8*NS)=OUT_TEMP(13) 
           
C          TOTAL SKY COVER (%) 
            OUT(11)=OUT_TEMP(14) 
           
C          OPAQUE SKY COVER (%) 
            OUT(12)=OUT_TEMP(15) 
           
C          PRESSURE (ATM)
            OUT(10)=OUT_TEMP(19) 
          
C          VISIBILITY (KM)
            OUT(40+8*NS)=OUT_TEMP(22) 
          
C          CEILING HEIGHT (M)
            OUT(41+8*NS)=OUT_TEMP(23) 

C          PRECIPITABLE WEATHER (MM H2O)
            OUT(42+8*NS)=OUT_TEMP(25) 

C          AEROSOL OPTICAL DEPTH (THOUSANDTHS)
            OUT(43+8*NS)=OUT_TEMP(26) 

C          SNOW DEPTH (CM)
            OUT(44+8*NS)=OUT_TEMP(27) 

C          DAYS SINCE LAST SNOWFALL (DAYS)
            OUT(45+8*NS)=OUT_TEMP(28) 
          
C          W - CALCULATED FROM TDB, PRESSURE AND %RH
            PSYDAT(1)=OUT(10)
            PSYDAT(2)=OUT(1)
            PSYDAT(4)=OUT(7)/100.
            CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*205)
205         OUT(6)=PSYDAT(6)
            OUT(3)=PSYDAT(3)

         ELSE IF(MODE.EQ.7) THEN  !TMY3
        
C          MONTH
            OUT(46+8*NS)=OUT_TEMP(1)

C          HOUR OF THE MONTH
            OUT(47+8*NS)=(OUT_TEMP(2)-1.D0)*24.D0+OUT_TEMP(4)

C          IDN
            OUT(15)=OUT_TEMP(8)

C          I
            OUT(14)=OUT_TEMP(7)

C          TDB
            OUT(1)=OUT_TEMP(16)

C          WIND SPEED
            OUT(8)=OUT_TEMP(21)

C          WIND DIRECTION
            OUT(9)=OUT_TEMP(20)

C          DEW POINT
            OUT(2)=OUT_TEMP(17)

C          RELATIVE HUMIDITY
            OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(18)))))

C          ILL
            OUT(36+8*NS)=OUT_TEMP(10)

C          ILL_DN
            OUT(37+8*NS)=OUT_TEMP(11)

C          ILL_DIFF
            OUT(38+8*NS)=OUT_TEMP(12)

C          ILL_ZEN
            OUT(39+8*NS)=OUT_TEMP(13)

C          SKY COVER
            OUT(11)=OUT_TEMP(14)

C          OPAQUE COVER
            OUT(12)=OUT_TEMP(15)

C          PRESSURE
            OUT(10)=OUT_TEMP(19)

C          HORIZ VISIBILITY
            OUT(40+8*NS)=OUT_TEMP(22)

C          CEILING HEIGHT
            OUT(41+8*NS)=OUT_TEMP(23)

C          PRECIP WATER
            OUT(42+8*NS)=OUT_TEMP(24)

C          OPTICAL DEPTH
            OUT(43+8*NS)=OUT_TEMP(25)

C          LIQUID PRECIPITATION DEPTH
            OUT(44+8*NS)=OUT_TEMP(27)

C          LIQUID PRECIPITATION QUANTITY
            OUT(45+8*NS)=OUT_TEMP(28)

C          W AND TWB - CALCULATED FROM TDB, PRESSURE AND %RH
            PSYDAT(1)=OUT(10)
            PSYDAT(2)=OUT(1)
            PSYDAT(4)=OUT(7)/100.
            CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*206)
206         OUT(6)=PSYDAT(6)
            OUT(3)=PSYDAT(3)

         ENDIF

C       CALCULATE THE MAX AND MIN MONTHLY AVERAGES
	   J_MIN=1
	   J_MAX=1
	   DO J=2,12
	      IF(MONTH_AVE_T(J).GT.MONTH_AVE_T(J-1)) THEN
	         J_MAX=J
	      ENDIF

	      IF(MONTH_AVE_T(J).LT.MONTH_AVE_T(J-1)) THEN
	         J_MIN=J
	      ENDIF
	   ENDDO

C       CALCULATE THE MAINS WATER TEMPERATURE BASED ON AN ALGORITHM BY CRAIG CHRISTENSEN AND JAY BURCH OF NREL
	   OFFSET=6.
	   MAINS_RATIO=0.4+0.01*((ANNUAL_AVE_T*1.8+32.)-44.)
         DT_MAX=(MONTH_AVE_T(J_MAX)-MONTH_AVE_T(J_MIN))*1.8
	   XDAY=DMOD(TIME,8760.D0)/24.
	   LAG=35.-((ANNUAL_AVE_T*1.8+32.)-44.)
         T_MAINS=((ANNUAL_AVE_T*1.8+32.)+OFFSET)+MAINS_RATIO*(DT_MAX/2.)
	1      *DSIN((0.986*(XDAY-15-LAG)-90)*0.017453292)
	   T_MAINS=(T_MAINS-32.)/1.8

C       SET THE LATITUDE, LONGITUDE, ELEVATION AND SHIFT
         OUT(24+8*NS)=LAT
         OUT(25+8*NS)=LONG
         OUT(26+8*NS)=SHIFT
         OUT(27+8*NS)=ELEV

C       SET THE MONTHLY AVERAGE AIR TEMPERATURES
	   OUT(30+8*NS)=MONTH_AVE_T(J_MONTH)
	   OUT(31+8*NS)=MONTH_MIN_T(J_MONTH)
	   OUT(32+8*NS)=MONTH_MAX_T(J_MONTH)

C       SET THE ANNUAL AIR TEMPERATURES
	   OUT(33+8*NS)=ANNUAL_AVE_T
	   OUT(34+8*NS)=ANNUAL_MIN_T
	   OUT(35+8*NS)=ANNUAL_MAX_T

C       SET THE HEATING AND COOLING SEASON OUTPUTS
         OUT(28+8*NS)=X_HEAT(J_MONTH)
         OUT(29+8*NS)=X_COOL(J_MONTH)

C       SET THE MAINS WATER TEMPERATURE
         OUT(5)=T_MAINS

C       CALCULATE THE GROUND REFLECTANCE
         IF(MODE.EQ.7) THEN
	      IF(OUT_TEMP(26).LT.0.) THEN
	         OUT(51+8*NS)=RHOG
	         RHO=RHOG
	      ELSE
	         OUT(51+8*NS)=OUT_TEMP(26)
	         RHO=OUT_TEMP(26)
	      ENDIF
	   ELSE
         IF ((OUT(44+8*NS).GT.0.).AND.(OUT(44+8*NS).NE.999.)) THEN  ! 999 indicates no data.
	         RHO=RHOG_SNOW
	      ELSE
	         RHO=RHOG
            ENDIF
	      OUT(51+8*NS)=RHO
	   ENDIF

C       SET THE SOLAR TIME INDICATOR
         IF(MODE.EQ.1) THEN
	      I_SOLARTIME=-1
	   ELSE
	      I_SOLARTIME=1
	   ENDIF

         IHOR=OUT(14)
	   IDN=OUT(15)

C       PROCESS THE INCIDENT SOLAR RADIATION
         ITEMP1=1
	   ITEMP2=3
         XTEMP1=0.
	   XTEMP2=0.
	   CALL GetIncidentRadiation(TIME,INFO,RHO,XTEMP1,XTEMP2,ITEMP1,
	1      ITEMP2,LAT,SHIFT,I_SOLARTIME,TD1,TD2,IHOR,IDN,SOLAR,
     1      IERROR_RAD)
	   
	   IF(IERROR_RAD.EQ.1) THEN
	      CALL MESSAGES(-1,MESSAGE5,'FATAL',INFO(1),INFO(2))
	      RETURN 1
	   ENDIF

	   OUT(13)=SOLAR(1)
	   OUT(16)=SOLAR(2)
	   OUT(17)=SOLAR(3)
	   OUT(18)=SOLAR(10)
	   OUT(19)=SOLAR(11)
	   OUT(20)=SOLAR(12)
	   OUT(21)=SOLAR(13)
	   OUT(22)=SOLAR(12)+SOLAR(13)
	   OUT(23)=SOLAR(6)
	   
	   DO J=1,NS

	      IF((TRACK_MODE(J).EQ.2).OR.(TRACK_MODE(J).EQ.3)) THEN
	         SLOPE(J)=AXIS_SLOPE(J)
	         AZIMUTH(J)=AXIS_AZIMUTH(J)
	      ENDIF

	      CALL GetIncidentRadiation(TIME,INFO,RHO,SLOPE(J),AZIMUTH(J),
	1         TRACK_MODE(J),RAD_MODE,LAT,SHIFT,I_SOLARTIME,TD1,TD2,
     1         IHOR,IDN,SOLAR,IERROR_RAD)

	      IF(IERROR_RAD.EQ.1) THEN
	         CALL MESSAGES(-1,MESSAGE5,'FATAL',INFO(1),INFO(2))
	         RETURN 1
	      ENDIF

            OUT(23+J)=SOLAR(10)
            OUT(23+NS+J)=SOLAR(11)
            OUT(23+2*NS+J)=SOLAR(12)
            OUT(23+3*NS+J)=SOLAR(13)
            OUT(23+4*NS+J)=SOLAR(12)+SOLAR(13)
            OUT(23+5*NS+J)=SOLAR(6)
            OUT(23+6*NS+J)=SOLAR(4)
            OUT(23+7*NS+J)=SOLAR(5)
	   ENDDO

C       CALCULATE THE SKY TEMPERATURE
         ECLD=0.9
         EOSKY=0.711D0+0.56D0*(OUT(2)/100.D0)+0.73D0*(OUT(2)/100.D0)**2
         EOSKY=EOSKY+.013D0*DCOS(2.D0*3.141592654D0*(DMOD(TIME,24.D0)/
	1      24.D0))
         EOSKY = EOSKY +.00012D0*((OUT(10)*1013.25D0)-1013.25D0)
         ESKY=EOSKY+(1.D0-EOSKY)*(OUT(12)/100.D0)*ECLD
         TSKY=ESKY**0.25D0*(OUT(1)+273.15D0)-273.15D0
         OUT(4)=TSKY

C       CALCULATE SOME OF THE TIME VARIABLES
         OUT(48+8*NS)=DMOD(TIME,24.D0)
         OUT(49+8*NS)=DBLE(JFIX((TIME-DELT/2.)/24.))+1.
         OUT(50+8*NS)=DBLE(JFIX(OUT(47+8*NS)/24.))+1.

C       PRINT ANY WARNING MESSAGES FROM THE RADIATION PROCESSOR
	   IF(IERROR_RAD.EQ.2) THEN
	      CALL MESSAGES(-1,MESSAGE6,'WARNING',INFO(1),INFO(2))
	   ELSE IF(IERROR_RAD.EQ.3) THEN
	      CALL MESSAGES(-1,MESSAGE7,'WARNING',INFO(1),INFO(2))
	   ELSE IF(IERROR_RAD.EQ.4) THEN
	      CALL MESSAGES(-1,MESSAGE8,'WARNING',INFO(1),INFO(2))
	   ENDIF

C       RETURN TO THE CALLING PROGRAM
         RETURN 1

      ENDIF
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    RETURN FROM THIS MODEL AS NO "AFTER-ITERATION" MANIPULATIONS ARE REQUIRED
      IF(INFO(13).GT.0) RETURN 1
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    *** ITS AN ITERATIVE CALL TO THIS COMPONENT ***
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    RE-READ THE PARAMETERS IF ANOTHER UNIT OF THIS TYPE HAS BEEN CALLED
      IF(INFO(1).NE.IUNIT) THEN

C       RESET THE UNIT NUMBER
         IUNIT=INFO(1)
         ITYPE=INFO(2)

C       GET THE TYPE OF DATA TO BE READ IN
         MODE=JFIX(PAR(1)+0.5)
         LU_DATA=JFIX(PAR(2)+0.1)
         RAD_MODE=JFIX(PAR(3)+0.1)
         RHOG=PAR(4)
         RHOG_SNOW=PAR(5)
         NS=JFIX(PAR(6)+0.5)
         
	   DO J=1,NS
            TRACK_MODE(J)=JFIX(PAR(6+3*(J-1)+1)+0.5)
	      SLOPE(J)=PAR(6+3*(J-1)+2)
            AZIMUTH(J)=PAR(6+3*(J-1)+3)

	      AXIS_SLOPE(J)=PAR(6+3*(J-1)+2)
            AXIS_AZIMUTH(J)=PAR(6+3*(J-1)+3)
         ENDDO

         IF(MODE.EQ.1) THEN
            LAT=PAR(7+3*NS)
            LONG=-99.
         ENDIF
      
C       GET THE NUMBER OF ITEMS TO BE READ AND SET THE INTERPOLATION AND CONVERSION FACTORS
         IF(MODE.EQ.1) THEN
            N_ITEMS=36
	      HEADERSKIP=0
         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN
            N_ITEMS=27
	      HEADERSKIP=1
         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN
            N_ITEMS=31
	      HEADERSKIP=8
         ELSE IF(MODE.EQ.4) THEN
            N_ITEMS=28 
	      HEADERSKIP=1
         ELSE IF(MODE.EQ.7) THEN
            N_ITEMS=28 
	      HEADERSKIP=2
         ENDIF

      ENDIF
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    CHECK TO SEE IF THIS COMPONENT IS BEING CALLED AGAIN DUE TO AN UNSTABLE CONTROLLER STATE - IF SO, BACK-UP ONE LINE
      IF((CheckStability().LT.1).AND.(OUT(52+8*NS).GT.0.)) THEN
         BACKSPACE(LU_DATA)
      ENDIF
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    GET THE INPUTS AT THE CURRENT TIME
      IF(INFO(3).GT.0) THEN
	   DO J=1,NS
	      SLOPE(J)=XIN(1+2*(J-1))
            AZIMUTH(J)=XIN(2+2*(J-1))

	      AXIS_SLOPE(J)=XIN(1+2*(J-1))
            AXIS_AZIMUTH(J)=XIN(2+2*(J-1))
	   ENDDO
	ENDIF
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    RETRIEVE THE VALUES IN THE STORAGE ARRAY FOR THIS ITERATION

C    RESET A FEW OF THE LOCATION PARAMETERS
      LAT=OUT(24+8*NS)
      LONG=OUT(25+8*NS)
      SHIFT=OUT(26+8*NS)
      ELEV=OUT(27+8*NS)
      
C    RETRIEVE ANY VALUES NEEDED FROM THE STORAGE ARRAY - AN EXAMPLE IS PROVIDED
	CALL GetStorageVars(STORE,74+9*N_ITEMS,INFO)

      TIME_NEXT=STORE(1)
      DO J=1,N_ITEMS
         VALUE_WAS(J)=STORE(1+J)
         VALUE_NOW(J)=STORE(1+N_ITEMS+J)
         VALUE_NEXT(J)=STORE(1+2*N_ITEMS+J)
         BEGIN(J)=STORE(1+3*N_ITEMS+J)
         PREVIOUS(J)=STORE(1+4*N_ITEMS+J)
         INTERP(J)=JFIX(STORE(1+5*N_ITEMS+J))
         TIME_STAMP(J)=JFIX(STORE(1+6*N_ITEMS+J))
         XMULT(J)=STORE(1+7*N_ITEMS+J)
         XADD(J)=STORE(1+8*N_ITEMS+J)
      ENDDO

	ANNUAL_AVE_T=STORE(1+9*N_ITEMS+1)
	ANNUAL_MIN_T=STORE(1+9*N_ITEMS+2)
	ANNUAL_MAX_T=STORE(1+9*N_ITEMS+3)
	
	DO J=1,14
	   MONTH_AVE_T(J-1)=STORE(1+9*N_ITEMS+3+J)
	   MONTH_MIN_T(J-1)=STORE(1+9*N_ITEMS+3+14+J)
	   MONTH_MAX_T(J-1)=STORE(1+9*N_ITEMS+3+28+J)
	   X_HEAT(J-1)=STORE(1+9*N_ITEMS+3+42+J)
	   X_COOL(J-1)=STORE(1+9*N_ITEMS+3+56+J)
	ENDDO
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    MAIN CALCULATION ALGORITHM

C    INITIALIZE THE OUTPUT TO NO-DATA-READ
      OUT(52+8*NS)=0.

C    CHECK TO SEE IF ITS TIME TO READ SOME DATA
      IF((TIME+DELT/2.).GT.TIME_NEXT) THEN

C       SET THE NEXT READ TIME
         TIME_NEXT=TIME_NEXT+1.D0
         OUT(52+8*NS)=1.

C       MOVE THE NEXT VALUES TO THE CURRENT VALUES
         DO J=1,N_ITEMS
            VALUE_WAS(J)=VALUE_NOW(J)
            VALUE_NOW(J)=VALUE_NEXT(J)
         ENDDO

C       TIME TO READ THE DATA FOR THE NEXT TIMESTEP
         IF(MODE.EQ.1) THEN !STANDARD TMY
           READ(LU_DATA,6000,ERR=2000,END=210) 
     1        (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN
           READ(LU_DATA,4000,ERR=2000,END=210) 
     1        (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN
          READ(LU_DATA,*,END=210,ERR=2000) VALUE_NEXT(1),VALUE_NEXT(2)
     &     ,VALUE_NEXT(3),VALUE_NEXT(4),VALUE_NEXT(5),ALPHANUM,
     &      VALUE_NEXT(6),VALUE_NEXT(7),VALUE_NEXT(8),VALUE_NEXT(9),
     &      VALUE_NEXT(10),VALUE_NEXT(11),VALUE_NEXT(12),VALUE_NEXT(13),
     &      VALUE_NEXT(14),VALUE_NEXT(15),VALUE_NEXT(16),VALUE_NEXT(17),
     &      VALUE_NEXT(18),VALUE_NEXT(19),VALUE_NEXT(20),VALUE_NEXT(21),
     &      VALUE_NEXT(22),VALUE_NEXT(23),VALUE_NEXT(24),VALUE_NEXT(25),
     &      VALUE_NEXT(26),VALUE_NEXT(27),VALUE_NEXT(28),VALUE_NEXT(29),
     &      VALUE_NEXT(30),VALUE_NEXT(31)
         ELSE IF(MODE.EQ.4) THEN !IWEC
           READ(LU_DATA,6001,ERR=2000,END=210) 
     1        (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF(MODE.EQ.7) THEN   !TMY3
           READ(LU_DATA,5004,ERR=2000,END=210,ADVANCE='NO') 
	1        VALUE_NEXT(1),VALUE_NEXT(2),VALUE_NEXT(3),VALUE_NEXT(4)
           READ(LU_DATA,*,ERR=2000,END=210) 
	1        VALUE_NEXT(5),VALUE_NEXT(6)
     1       ,VALUE_NEXT(7),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(8),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(9),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(10),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(11),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(12),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(13),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(14),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(15),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(16),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(17),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(18),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(19),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(20),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(21),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(22),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(23),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(24),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(25),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(26),ALPHANUM,ALPHANUM
     1       ,VALUE_NEXT(27),VALUE_NEXT(28)
         ENDIF

C       REPLACE SOME 'MISSING DATA' CONDITIONS IN STANDARD TMY FILES APPROPRIATE VALUES
         IF(MODE.EQ.1) THEN         !STANDARD TMY
            DO I=1,N_ITEMS
               !replace 'missing radiation' values (9999) with zeros.
               IF((VALUE_NEXT(I).GT.9998.).AND.(VALUE_NEXT(I).LT.
     &          10000.)) THEN
                  VALUE_NEXT(I) = 0.D0
               ENDIF
               !replace 'missing sky cover' values (99) with the last read value.
               IF(((I.EQ.34).OR.(I.EQ.35)).AND.(VALUE_NEXT(I).GT.
     &          98)) THEN
                  VALUE_NEXT(I) = VALUE_NOW(I)
                  IF (I.EQ.34) THEN
                    CALL MESSAGES(-1,MESSAGE3,'notice',IUNIT,ITYPE)
                  ELSEIF (I.EQ.35) THEN
                    CALL MESSAGES(-1,MESSAGE4,'notice',IUNIT,ITYPE)
                  ENDIF
               ENDIF
            ENDDO
         ELSE IF(MODE.EQ.7) THEN
	     IF(VALUE_NEXT(27).LT.0.) VALUE_NEXT(27)=VALUE_NOW(27)
	     IF(VALUE_NEXT(28).LT.0.) VALUE_NEXT(28)=VALUE_NOW(28)
         ENDIF
CNB      REPLACE SOME 'MISSING DATA' CONDITIONS IN TMY2 FILES WITH APPROPRIATE VALUES 10-8-07
           IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
               !replace 'missing snow cover' values (999) with the last read value.
             DO I=1,N_ITEMS
               IF((I.EQ.26).AND.(VALUE_NOW(I).GT.998)) THEN
                  VALUE_NOW(I) = VALUE_WAS(I)
               ENDIF
             ENDDO
           ENDIF


         GOTO 230
210      CALL REWIND(TIME,LU_DATA,HEADERSKIP,IUNIT,ITYPE,IE_READ)
         IF(MODE.EQ.1) THEN
           READ(LU_DATA,6000,ERR=2000,END=2000) 
     1        (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN
           READ(LU_DATA,4000,ERR=2000,END=2000) 
     1        (VALUE_NEXT(I),I=1,N_ITEMS)
         ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN
          READ(LU_DATA,*,END=2000,ERR=2000) VALUE_NEXT(1),VALUE_NEXT(2)
     &     ,VALUE_NEXT(3),VALUE_NEXT(4),VALUE_NEXT(5),ALPHANUM,
     &      VALUE_NEXT(6),VALUE_NEXT(7),VALUE_NEXT(8),VALUE_NEXT(9),
     &      VALUE_NEXT(10),VALUE_NEXT(11),VALUE_NEXT(12),VALUE_NEXT(13),
     &      VALUE_NEXT(14),VALUE_NEXT(15),VALUE_NEXT(16),VALUE_NEXT(17),
     &      VALUE_NEXT(18),VALUE_NEXT(19),VALUE_NEXT(20),VALUE_NEXT(21),
     &      VALUE_NEXT(22),VALUE_NEXT(23),VALUE_NEXT(24),VALUE_NEXT(25),
     &      VALUE_NEXT(26),VALUE_NEXT(27),VALUE_NEXT(28),VALUE_NEXT(29),
     &      VALUE_NEXT(30),VALUE_NEXT(31)
         ELSE IF(MODE.EQ.4) THEN
           READ(LU_DATA,6001,ERR=2000,END=2000) 
     1        (VALUE_NEXT(I),I=1,N_ITEMS)
         ENDIF

C       REPLACE SOME 'MISSING DATA' CONDITIONS IN STANDARD TMY FILES APPROPRIATE VALUES
         IF(MODE.EQ.1) THEN         !STANDARD TMY
            DO I=1,N_ITEMS
               !replace 'missing radiation' values (9999) with zeros.
               IF((VALUE_NEXT(I).GT.9998.).AND.(VALUE_NEXT(I).LT.
     &          10000.)) THEN
                  VALUE_NEXT(I) = 0.D0
               ENDIF
               !replace 'missing sky cover' values (99) with the last read value.
               IF(((I.EQ.34).OR.(I.EQ.35)).AND.(VALUE_NEXT(I).GT.
     &          98)) THEN
                  VALUE_NEXT(I) = VALUE_NOW(I)
               ENDIF
            ENDDO
         ELSE IF(MODE.EQ.7) THEN
	     IF(VALUE_NEXT(27).LT.0.) VALUE_NEXT(27)=VALUE_NOW(27)
	     IF(VALUE_NEXT(28).LT.0.) VALUE_NEXT(28)=VALUE_NOW(28)
         ENDIF
CNB      REPLACE SOME 'MISSING DATA' CONDITIONS IN TMY2 FILES WITH APPROPRIATE VALUES 10-8-07
           IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN        !TMY2
               !replace 'missing snow cover' values (999) with the last read value.
             DO I=1,N_ITEMS
               IF((I.EQ.26).AND.(VALUE_NOW(I).GT.998)) THEN
                  VALUE_NOW(I) = VALUE_WAS(I)
               ENDIF
             ENDDO
           ENDIF


230      IF(IE_READ.GT.0) GOTO 2000

         DO J=1,N_ITEMS
            PREVIOUS(J)=BEGIN(J)
            IF((INTERP(J).GT.0).AND.(TIME_STAMP(J).EQ.0)) THEN
               BEGIN(J)=2.*VALUE_NOW(J)-BEGIN(J)
            ELSE
               BEGIN(J)=VALUE_NOW(J)
            ENDIF
         ENDDO

      ENDIF

C    STEP THROUGH EACH VARIABLE
      DO J=1,N_ITEMS

C       INTERPOLATION IS OFF OR THE VALUES ARE AVERAGE VALUES AND THEREFORE CANNOT BE INTERPOLATED
         IF(INTERP(J).LT.0) THEN

C          SET THE OUTPUTS
            IF((TIME-DELT/2.).GT.(TIME_NEXT-1.D0)) THEN
               OUT_TEMP(J)=VALUE_NEXT(J)*XMULT(J)+XADD(J)
            ELSE
               OUT_TEMP(J)=VALUE_NOW(J)*XMULT(J)+XADD(J)
            ENDIF

C       INTERPOLATION IS ON AND VALUES IN THE FILE ARE AVERAGE VALUES OVER THE TIMESTEP
         ELSE IF(TIME_STAMP(J).EQ.0) THEN

C          CALCULATE THE INSTANTANEOUS VALUE AT THE END OF THE DATA TIMESTEP
            ENDING(J)=2.*VALUE_NEXT(J)-BEGIN(J)

C          CALCULATE THE INTERPOLATION RATIO
            RATIO=(TIME_NEXT-TIME)

C          CALCULATE THE INSTANTANEOUS VALUE AT THE END OF THE SIMULATION TIMESTEP
            XNOW=BEGIN(J)+(1.D0-RATIO)*(ENDING(J)-BEGIN(J))

C          SET THE INSTANTANEOUS VALUE AT THE END OF THE NEXT TIMESTEP
            RATIO=(TIME_NEXT-TIME-DELT)
            XNEXT=BEGIN(J)+(1.D0-RATIO)*
     1                (ENDING(J)-BEGIN(J))

C          CALCULATE THE INSTANTANEOUS VALUE OF THE FUNCTION AT THE END OF THE PREVIOUS TIMESTEP
            RATIO=(TIME_NEXT-TIME+DELT)
            XWAS=BEGIN(J)+DMAX1(0.,((1.D0-RATIO)))*(ENDING(J)-
     1           BEGIN(J))+DMAX1(0.,((RATIO-1.D0)))*(PREVIOUS(J)-
     1           BEGIN(J))


C          SET THE AVERAGES OVER THE TIMESTEP AND CALCULATE THE OUTPUTS
            OUT_TEMP(J)=(XWAS+XNOW)/2.*XMULT(J)+XADD(J)

C       INTERPOLATION IS ON AND VALUES IN THE FILE ARE INSTANTANEOUS
         ELSE

C          INSTANTANEOUS VALUES - STEP THROUGH EACH ONE - REMEMBER THAT TRNSYS PUTS OUT THE AVERAGE VALUE
C          OVER THE TIMESTEP AND NOT THE INSTANTANEOUS VALUE!
            IF(TIME_STAMP(J).EQ.1) THEN

C             CALCULATE THE INSTANTANEOUS VALUE OF THE FUNCTION AT THE END OF THE TIMESTEP
               RATIO=(TIME_NEXT-TIME)
               XNOW=VALUE_NOW(J)+(1.D0-RATIO)*(VALUE_NEXT(J)-
     1            VALUE_NOW(J))

C             CALCULATE THE INSTANTANEOUS VALUE OF THE FUNCTION AT THE END OF THE PREVIOUS TIMESTEP
               RATIO=(TIME_NEXT-TIME+DELT)
               XWAS=VALUE_NOW(J)+DMAX1(0.,((1.D0-RATIO)))*(VALUE_NEXT(J)
	1             -VALUE_NOW(J))+DMAX1(0.,((RATIO-1.D0)))*(VALUE_WAS(J)
     1             -VALUE_NOW(J))

C             SET THE INSTANTANEOUS VALUE AT THE END OF THE NEXT TIMESTEP
               RATIO=(TIME_NEXT-TIME-DELT)
               XNEXT=VALUE_NOW(J)+(1.D0-RATIO)*
     1             (VALUE_NEXT(J)-VALUE_NOW(J))

C             SET THE AVERAGES OVER THE TIMESTEP AND CALCULATE THE OUTPUTS
               OUT_TEMP(J)=(XNOW+XWAS)/2.*XMULT(J)+XADD(J)

            ENDIF
         ENDIF
      ENDDO

C    CALCULATE THE CURRENT MONTH
      IF((DMOD(TIME,8760.D0)-DELT/2.).LE.744) THEN
	   J_MONTH=1
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.1416.) THEN
	   J_MONTH=2
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.2160.) THEN
	   J_MONTH=3
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.2880.) THEN
	   J_MONTH=4
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.3624.) THEN
	   J_MONTH=5
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.4344.) THEN
	   J_MONTH=6
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.5088.) THEN
	   J_MONTH=7
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.5832.) THEN
	   J_MONTH=8
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.6552.) THEN
	   J_MONTH=9
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.7296.) THEN
	   J_MONTH=10
      ELSE IF((DMOD(TIME,8760.D0)-DELT/2.).LE.8016.) THEN
	   J_MONTH=11
      ELSE
	   J_MONTH=12
	ENDIF

C    NOW SET THE OUTPUTS BASED ON THE MODE
      IF(MODE.EQ.1) THEN
        
C       MONTH
         OUT(46+8*NS)=OUT_TEMP(2) 

C       HOUR OF THE MONTH
         OUT(47+8*NS)=OUT_TEMP(4) 

C       IDN
         OUT(15)=OUT_TEMP(7) 

C       I
         OUT(14)=OUT_TEMP(13) 

C       TDB
         OUT(1)=OUT_TEMP(30) 

C       WIND SPEED
         OUT(8)=OUT_TEMP(33) 

C       WIND DIRECTION
         OUT(9)=OUT_TEMP(32) 

C       DEW POINT
         OUT(2)=OUT_TEMP(31) 

C       ILL
         OUT(36+8*NS)=-99.d0 

C       ILL_DN
         OUT(37+8*NS)=-99.d0 

C       ILL_DIFF
         OUT(38+8*NS)=-99.d0 

C       ILL_ZEN
         OUT(39+8*NS)=-99.d0 

C       SKY COVER
         OUT(11)=OUT_TEMP(34) 

C       OPAQUE COVER
         OUT(12)=OUT_TEMP(35) 

C       PRESSURE
         OUT(10)=OUT_TEMP(29) 

C       HORIZ VISIBILITY
         OUT(40+8*NS)=OUT_TEMP(19) 

C       CEILING HEIGHT
         OUT(41+8*NS)=OUT_TEMP(17) 

C       PRECIP WATER
         OUT(42+8*NS)=-99.d0 

C       OPTICAL DEPTH
         OUT(43+8*NS)=-99.d0 

C       SNOW DEPTH
         OUT(44+8*NS)=-99.d0 

C       DAYS SINCE LAST SNOW
         OUT(45+8*NS)=-99.d0 

C       W AND %RH - CALCULATED FROM TDB, PRESSURE AND TDP
         PSYDAT(1)=OUT(10)
         PSYDAT(2)=OUT(1)
         PSYDAT(5)=OUT(2)
         CALL PSYCHROMETRICS(TIME,INFO,1,3,1,PSYDAT,0,IE,*221)
221      OUT(6)=PSYDAT(6)           !HUMIDITY RATIO W
         OUT(7)=PSYDAT(4)*100.D0    !RELATIVE HUMIDITY RH
         OUT(3)=PSYDAT(3)

      ELSE IF((MODE.EQ.2).OR.(MODE.EQ.6)) THEN !TMY2
        
C       MONTH
         OUT(46+8*NS)=OUT_TEMP(2)

C       HOUR OF THE MONTH
         OUT(47+8*NS)=(OUT_TEMP(3)-1.D0)*24.D0+OUT_TEMP(4)

C       IDN
         OUT(15)=OUT_TEMP(8)

C       I
         OUT(14)=OUT_TEMP(7)

C       TDB
         OUT(1)=OUT_TEMP(16)

C       WIND SPEED
         OUT(8)=OUT_TEMP(21)

C       WIND DIRECTION
         OUT(9)=OUT_TEMP(20)

C       DEW POINT
         OUT(2)=OUT_TEMP(17)

C       RELATIVE HUMIDITY
         OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(18)))))

C       ILL
         OUT(36+8*NS)=OUT_TEMP(10)

C       ILL_DN
         OUT(37+8*NS)=OUT_TEMP(11)

C       ILL_DIFF
         OUT(38+8*NS)=OUT_TEMP(12)

C       ILL_ZEN
         OUT(39+8*NS)=OUT_TEMP(13)

C       SKY COVER
         OUT(11)=OUT_TEMP(14)

C       OPAQUE COVER
         OUT(12)=OUT_TEMP(15)

C       PRESSURE
         OUT(10)=OUT_TEMP(19)

C       HORIZ VISIBILITY
         OUT(40+8*NS)=OUT_TEMP(22)

C       CEILING HEIGHT
         OUT(41+8*NS)=OUT_TEMP(23)

C       PRECIP WATER
         OUT(42+8*NS)=OUT_TEMP(24)

C       OPTICAL DEPTH
         OUT(43+8*NS)=OUT_TEMP(25)

C       SNOW DEPTH
         OUT(44+8*NS)=OUT_TEMP(26)

C       DAYS SINCE LAST SNOW
         OUT(45+8*NS)=OUT_TEMP(27)

C       W AND TWB - CALCULATED FROM TDB, PRESSURE AND %RH
         PSYDAT(1)=OUT(10)
         PSYDAT(2)=OUT(1)
         PSYDAT(4)=OUT(7)/100.
         CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*215)
215      OUT(6)=PSYDAT(6)
         OUT(3)=PSYDAT(3)

      ELSE IF((MODE.EQ.3).OR.(MODE.EQ.5)) THEN
        
C       MONTH
         OUT(46+8*NS)=OUT_TEMP(2)

C       HOUR OF THE MONTH
         OUT(47+8*NS)=(OUT_TEMP(3)-1.D0)*24.D0+OUT_TEMP(4)

C       IDN
         OUT(15)=OUT_TEMP(14)

C       I
         OUT(14)=OUT_TEMP(13)

C       TDB
         OUT(1)=OUT_TEMP(6)

C       WIND SPEED
         OUT(8)=OUT_TEMP(21)

C       WIND DIRECTION
         OUT(9)=OUT_TEMP(20)

C       DEW POINT
         OUT(2)=OUT_TEMP(7)

C       RELATIVE HUMIDITY
         OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(8)))))

C       ILL
         OUT(36+8*NS)=OUT_TEMP(16)

C       ILL_DN
         OUT(37+8*NS)=OUT_TEMP(17)

C       ILL_DIFF
         OUT(38+8*NS)=OUT_TEMP(18)

C       ILL_ZEN
         OUT(39+8*NS)=OUT_TEMP(19)

C       SKY COVER
         OUT(11)=OUT_TEMP(22)

C       OPAQUE COVER
         OUT(12)=OUT_TEMP(23)

C       PRESSURE
         OUT(10)=OUT_TEMP(9)

C       HORIZ VISIBILITY
         OUT(40+8*NS)=OUT_TEMP(24)

C       CEILING HEIGHT
         OUT(41+8*NS)=OUT_TEMP(25)

C       PRECIP WATER
         OUT(42+8*NS)=OUT_TEMP(28)

C       OPTICAL DEPTH
         OUT(43+8*NS)=OUT_TEMP(29)

C       SNOW DEPTH
         OUT(44+8*NS)=OUT_TEMP(30)

C       DAYS SINCE LAST SNOW
         OUT(45+8*NS)=OUT_TEMP(31)

C       W AND TWB - CALCULATED FROM TDB, PRESSURE AND %RH
         PSYDAT(1)=OUT(10)
         PSYDAT(2)=OUT(1)
         PSYDAT(4)=OUT(7)/100.
         CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*217)
217      OUT(6)=PSYDAT(6)
         OUT(3)=PSYDAT(3)

      ELSE IF(MODE.EQ.4) THEN !IWEC

C       MONTH
         OUT(46+8*NS)=OUT_TEMP(2) 

C       HOUR OF THE MONTH
         OUT(47+8*NS)=(OUT_TEMP(3)-1.d0)*24.d0+OUT_TEMP(4)

C       DIRECT NORMAL SOLAR RADIATION (KJ/M2)  
         OUT(15)=OUT_TEMP(8) 

C       GLOBAL HORIZONTAL SOLAR RADIATION (KJ/M2)  
         OUT(14)=OUT_TEMP(7) 
           
C       DRY BULB TEMP (C)
         OUT(1)=OUT_TEMP(16) 
          
C       WINDSPEED (M/S)
         OUT(8)=OUT_TEMP(21) 

C       WIND DIRECTION (DEGREES)
         OUT(9)=OUT_TEMP(20) 

C       DEW POINT TEMP (C)
         OUT(2)=OUT_TEMP(17) 
          
C       RELATIVE HUMIDITY
         OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(18)))))

C       GLOBAL HORIZONTAL ILLUMINANCE (LUX)  
         OUT(36+8*NS)=OUT_TEMP(10) 
           
C       DIRECT NORMAL ILLUMINANCE (LUX)
         OUT(37+8*NS)=OUT_TEMP(11) 
           
C       DIFFUSE HORIZONTAL ILLUMINANCE (LUX) 
         OUT(38+8*NS)=OUT_TEMP(12) 
           
C       ZENITH LUMINANCE (CD/M2) 
         OUT(39+8*NS)=OUT_TEMP(13) 
           
C       TOTAL SKY COVER (%) 
         OUT(11)=OUT_TEMP(14) 
           
C       OPAQUE SKY COVER (%) 
         OUT(12)=OUT_TEMP(15) 
           
C       PRESSURE (ATM)
         OUT(10)=OUT_TEMP(19) 
          
C       VISIBILITY (KM)
         OUT(40+8*NS)=OUT_TEMP(22) 
          
C       CEILING HEIGHT (M)
         OUT(41+8*NS)=OUT_TEMP(23) 

C       PRECIPITABLE WEATHER (MM H2O)
         OUT(42+8*NS)=OUT_TEMP(25) 

C       AEROSOL OPTICAL DEPTH (THOUSANDTHS)
         OUT(43+8*NS)=OUT_TEMP(26) 

C       SNOW DEPTH (CM)
         OUT(44+8*NS)=OUT_TEMP(27) 

C       DAYS SINCE LAST SNOWFALL (DAYS)
         OUT(45+8*NS)=OUT_TEMP(28) 
          
C       W - CALCULATED FROM TDB, PRESSURE AND %RH
         PSYDAT(1)=OUT(10)
         PSYDAT(2)=OUT(1)
         PSYDAT(4)=OUT(7)/100.
         CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*219)
219      OUT(6)=PSYDAT(6)
         OUT(3)=PSYDAT(3)

      ELSE IF(MODE.EQ.7) THEN !TMY2
        
C       MONTH
         OUT(46+8*NS)=OUT_TEMP(1)

C       HOUR OF THE MONTH
         OUT(47+8*NS)=(OUT_TEMP(2)-1.D0)*24.D0+OUT_TEMP(4)

C       IDN
         OUT(15)=OUT_TEMP(8)

C       I
         OUT(14)=OUT_TEMP(7)

C       TDB
         OUT(1)=OUT_TEMP(16)

C       WIND SPEED
         OUT(8)=OUT_TEMP(21)

C       WIND DIRECTION
         OUT(9)=OUT_TEMP(20)

C       DEW POINT
         OUT(2)=OUT_TEMP(17)

C       RELATIVE HUMIDITY
         OUT(7)=DMIN1(100.,(DMAX1(0.,(OUT_TEMP(18)))))

C       ILL
         OUT(36+8*NS)=OUT_TEMP(10)

C       ILL_DN
         OUT(37+8*NS)=OUT_TEMP(11)

C       ILL_DIFF
         OUT(38+8*NS)=OUT_TEMP(12)

C       ILL_ZEN
         OUT(39+8*NS)=OUT_TEMP(13)

C       SKY COVER
         OUT(11)=OUT_TEMP(14)

C       OPAQUE COVER
         OUT(12)=OUT_TEMP(15)

C       PRESSURE
         OUT(10)=OUT_TEMP(19)

C       HORIZ VISIBILITY
         OUT(40+8*NS)=OUT_TEMP(22)

C       CEILING HEIGHT
         OUT(41+8*NS)=OUT_TEMP(23)

C       PRECIP WATER
         OUT(42+8*NS)=OUT_TEMP(24)

C       OPTICAL DEPTH
         OUT(43+8*NS)=OUT_TEMP(25)

C       LIQUID PRECIPITATION DEPTH
         OUT(44+8*NS)=OUT_TEMP(27)

C       LIQUID PRECIPITATION QUANTITY
         OUT(45+8*NS)=OUT_TEMP(28)

C       W AND TWB - CALCULATED FROM TDB, PRESSURE AND %RH
         PSYDAT(1)=OUT(10)
         PSYDAT(2)=OUT(1)
         PSYDAT(4)=OUT(7)/100.
         CALL PSYCHROMETRICS(TIME,INFO,1,2,1,PSYDAT,0,IE,*220)
220      OUT(6)=PSYDAT(6)
         OUT(3)=PSYDAT(3)

      ENDIF

      TD1=JFIX(TIME-1./3600.D0/2.D0)
      TD2=JFIX(TIME-1./3600.D0/2.D0)+1.D0

C    CALCULATE THE MAX AND MIN MONTHLY AVERAGES
	J_MIN=1
	J_MAX=1
	DO J=2,12
	   IF(MONTH_AVE_T(J).GT.MONTH_AVE_T(J-1)) THEN
	      J_MAX=J
	   ENDIF

	   IF(MONTH_AVE_T(J).LT.MONTH_AVE_T(J-1)) THEN
	      J_MIN=J
	   ENDIF
	ENDDO

C    CALCULATE THE MAINS WATER TEMPERATURE BASED ON AN ALGORITHM BY CRAIG CHRISTENSEN AND JAY BURCH OF NREL
	OFFSET=6.
	MAINS_RATIO=0.4+0.01*((ANNUAL_AVE_T*1.8+32.)-44.)
      DT_MAX=(MONTH_AVE_T(J_MAX)-MONTH_AVE_T(J_MIN))*1.8
	XDAY=DMOD(TIME,8760.D0)/24.
	LAG=35.-((ANNUAL_AVE_T*1.8+32.)-44.)
      T_MAINS=((ANNUAL_AVE_T*1.8+32.)+OFFSET)+MAINS_RATIO*(DT_MAX/2.)
	1   *DSIN((0.986*(XDAY-15-LAG)-90)*0.017453292)
	T_MAINS=(T_MAINS-32.)/1.8

C    SET THE LATITUDE, LONGITUDE, ELEVATION AND SHIFT
      OUT(24+8*NS)=LAT
      OUT(25+8*NS)=LONG
      OUT(26+8*NS)=SHIFT
      OUT(27+8*NS)=ELEV

C    SET THE MONTHLY AVERAGE AIR TEMPERATURES
      OUT(30+8*NS)=MONTH_AVE_T(J_MONTH)
	OUT(31+8*NS)=MONTH_MIN_T(J_MONTH)
	OUT(32+8*NS)=MONTH_MAX_T(J_MONTH)

C    SET THE ANNUAL AIR TEMPERATURES
	OUT(33+8*NS)=ANNUAL_AVE_T
	OUT(34+8*NS)=ANNUAL_MIN_T
	OUT(35+8*NS)=ANNUAL_MAX_T

C    SET THE HEATING AND COOLING SEASON OUTPUTS
      OUT(28+8*NS)=X_HEAT(J_MONTH)
      OUT(29+8*NS)=X_COOL(J_MONTH)

C    SET THE MAINS WATER TEMPERATURE
      OUT(5)=T_MAINS

C    CALCULATE THE GROUND REFLECTANCE
      IF(MODE.EQ.7) THEN
	   IF(OUT_TEMP(26).LT.0.) THEN
	      OUT(51+8*NS)=RHOG
	      RHO=RHOG
	   ELSE
	      OUT(51+8*NS)=OUT_TEMP(26)
	      RHO=OUT_TEMP(26)
	   ENDIF
	ELSE
      IF ((OUT(44+8*NS).GT.0.).AND.(OUT(44+8*NS).NE.999.)) THEN  ! 999 indicates no data.
	      RHO=RHOG_SNOW
	   ELSE
	      RHO=RHOG
         ENDIF
	   OUT(51+8*NS)=RHO
      ENDIF

C    SET THE SOLAR TIME INDICATOR
      IF(MODE.EQ.1) THEN
	   I_SOLARTIME=-1
	ELSE
	   I_SOLARTIME=1
	ENDIF

      IHOR=OUT(14)
	IDN=OUT(15)

C    PROCESS THE INCIDENT SOLAR RADIATION
      ITEMP1=1
	ITEMP2=3
      XTEMP1=0.
	XTEMP2=0.
	CALL GetIncidentRadiation(TIME,INFO,RHO,XTEMP1,XTEMP2,ITEMP1,
	1   ITEMP2,LAT,SHIFT,I_SOLARTIME,TD1,TD2,IHOR,IDN,SOLAR,IERROR_RAD)
	   
	IF(IERROR_RAD.EQ.1) THEN
	   CALL MESSAGES(-1,MESSAGE5,'FATAL',INFO(1),INFO(2))
	   RETURN 1
	ENDIF

	OUT(13)=SOLAR(1)
	OUT(16)=SOLAR(2)
	OUT(17)=SOLAR(3)
	OUT(18)=SOLAR(10)
	OUT(19)=SOLAR(11)
	OUT(20)=SOLAR(12)
	OUT(21)=SOLAR(13)
	OUT(22)=SOLAR(12)+SOLAR(13)
	OUT(23)=SOLAR(6)
	   
	DO J=1,NS
	
	   IF((TRACK_MODE(J).EQ.2).OR.(TRACK_MODE(J).EQ.3)) THEN
	      SLOPE(J)=AXIS_SLOPE(J)
	      AZIMUTH(J)=AXIS_AZIMUTH(J)
	   ENDIF

	   CALL GetIncidentRadiation(TIME,INFO,RHO,SLOPE(J),AZIMUTH(J),
	1      TRACK_MODE(J),RAD_MODE,LAT,SHIFT,I_SOLARTIME,TD1,TD2,IHOR,
     1      IDN,SOLAR,IERROR_RAD)

	   IF(IERROR_RAD.EQ.1) THEN
	      CALL MESSAGES(-1,MESSAGE5,'FATAL',INFO(1),INFO(2))
	      RETURN 1
	   ENDIF

         OUT(23+J)=SOLAR(10)
         OUT(23+NS+J)=SOLAR(11)
         OUT(23+2*NS+J)=SOLAR(12)
         OUT(23+3*NS+J)=SOLAR(13)
         OUT(23+4*NS+J)=SOLAR(12)+SOLAR(13)
         OUT(23+5*NS+J)=SOLAR(6)
         OUT(23+6*NS+J)=SOLAR(4)
         OUT(23+7*NS+J)=SOLAR(5)
	ENDDO

C    CALCULATE THE SKY TEMPERATURE
      ECLD=0.9
      EOSKY=0.711D0+0.56D0*(OUT(2)/100.D0)+0.73D0*(OUT(2)/100.D0)**2
      EOSKY=EOSKY+.013D0*DCOS(2.D0*3.141592654D0*(DMOD(TIME,24.D0)/
	1   24.D0))
      EOSKY = EOSKY +.00012D0*((OUT(10)*1013.25D0)-1000.D0)
      ESKY=EOSKY+(1.D0-EOSKY)*(OUT(12)/100.D0)*ECLD
      TSKY=ESKY**0.25D0*(OUT(1)+273.15D0)-273.15D0
      OUT(4)=TSKY

C    CALCULATE SOME OF THE TIME VARIABLES
      OUT(48+8*NS)=DMOD(TIME,24.D0)
      OUT(49+8*NS)=DBLE(JFIX((TIME-DELT/2.)/24.))+1.
      OUT(50+8*NS)=DBLE(JFIX(OUT(47+8*NS)/24.))+1.

C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    STORE THE VALUES AND GET OUT
      STORE(1)=TIME_NEXT
      DO J=1,N_ITEMS
         STORE(1+J)=VALUE_WAS(J)
         STORE(1+N_ITEMS+J)=VALUE_NOW(J)
         STORE(1+2*N_ITEMS+J)=VALUE_NEXT(J)
         STORE(1+3*N_ITEMS+J)=BEGIN(J)
         STORE(1+4*N_ITEMS+J)=PREVIOUS(J)
      ENDDO
      CALL setStorageVars(STORE,1+5*N_ITEMS,INFO)

C    PRINT ANY WARNING MESSAGES FROM THE RADIATION PROCESSOR
	IF(IERROR_RAD.EQ.2) THEN
	   CALL MESSAGES(-1,MESSAGE6,'WARNING',INFO(1),INFO(2))
	ELSE IF(IERROR_RAD.EQ.3) THEN
	   CALL MESSAGES(-1,MESSAGE7,'WARNING',INFO(1),INFO(2))
	ELSE IF(IERROR_RAD.EQ.4) THEN
	   CALL MESSAGES(-1,MESSAGE8,'WARNING',INFO(1),INFO(2))
	ENDIF
     
      RETURN 1
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    THE MODEL WAS UNABLE TO READ FROM THE DATA FILE - SET AN ERROR AND GET OUT
2000  CALL MESSAGES(-1,MESSAGE1,'FATAL',IUNIT,ITYPE)
      RETURN 1
2002  CALL MESSAGES(-1,MESSAGE2,'FATAL',IUNIT,ITYPE)
      RETURN 1
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    EVERYTHING IS DONE - RETURN FROM THIS SUBROUTINE AND MOVE ON
      RETURN 1
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------

C    FORMAT FOR READING A TMY2 DATA FILE
4000  FORMAT (1X,F2.0,F2.0,F2.0,F2.0,F4.0,F4.0,F4.0,2X,F4.0,2X,F4.0,
     &       2X,F4.0,2X,F4.0,2X,F4.0,2X,F4.0,2X,F2.0,2X,F2.0,
     &       2X,F4.0,2X,F4.0,2X,F3.0,2X,F4.0,2X,F3.0,2X,F3.0,
     &       2X,F4.0,2X,F5.0,12X,F3.0,2X,F3.0,2X,F3.0,2X,F2.0)

C    FORMAT FOR READING THE FIRST LINE (HEADER) OF A TMY2 DATA FILE
5000  FORMAT (7X,A22,1X,A2,1X,F3.0,1X,A1,1X,F2.0,1X,F2.0,1X,A1,1X,F3.0,
     1        1X,F2.0,2X,F4.0)
c5000  FORMAT (7X,A22,1X,A2,1X,F3.0,3X,F2.0,1X,F2.0,1X,A1,1X,F3.0,1X,F2.0
c     1        ,2X,F4.0)

C    FORMATS FOR READING THE HEADER OF A E-PLUS DATA FILE
5001  FORMAT(160A1)

C    FORMAT FOR READING THE HEADER OF AN IWEC WEATHER FILE
5003  FORMAT (13X,A22,1X,A2,1X,A3,1X,I1,1X,A1,1X,I2,1X,I2,1X,A1,1X,F2.0,
     &        1X,F2.0,1X,A1,1X,F3.0,1X,F2.0,1X,A1,1X,F4.0)
C    FORMAT FOR READING THE BEGINNING SECTION OF EACH LINE OF A TMY3 FILE
5004  FORMAT(F2.0,1X,F2.0,1X,F4.0,1X,F2.0,4X)

C    FORMAT FOR READING A DATA LINE IN A STANDARD TMY DATA FILE
6000  FORMAT (5X,F2.0,F2.0,F2.0,F4.0,F4.0,F4.0,1X,F4.0,1X,F4.0,
     &       1X,F4.0,1X,F4.0,1X,F4.0,1X,F4.0,1X,F4.0,F10.0,
     &       F2.0,F2.0,F4.0,F5.0,F4.0,F1.0,F1.0,F1.0,F1.0,F1.0,
     &       F1.0,F1.0,F1.0,F5.0,F5.0,F4.0,F4.0,F3.0,F4.0,F2.0,F2.0,
     &       F1.0,F9.0)

C    FORMAT FOR READING A DATA LINE OF AN IWEC DATA FILE
6001  FORMAT (1X,F4.0,F2.0,F2.0,F2.0,F4.0,F4.0,F4.0,2X,F4.0,2X,
     &       F4.0,2X,F4.0,2X,F4.0,2X,F4.0,2X,F4.0,2X,F2.0,2X,
     &       F2.0,2X,F4.0,2X,F4.0,2X,F3.0,2X,F4.0,2X,F3.0,2X,
     &       F3.0,2X,F4.0,2X,F5.0,2X,F4.0,2X,10X,F3.0,2X,F3.0,2X,
     &       F3.0,2X,F2.0,2X)


C-----------------------------------------------------------------------------------------------------------------------

      END





      SUBROUTINE GetIncidentRadiation(TIME,INFO,RHOG,SLOPE,AZIMUTH,
	1   MODE_TRACK,MODE_TILT,LATITUDE,SHIFT,I_SOLARTIME,TD1,TD2,I,IDN,
     1   SOLAR,IERROR_RAD)

C**********************************************************************************************
C THIS ROUTINE FINDS THE POSITION OF THE SUN IN THE SKY AND ESTIMATES THE RADIATION INCIDENT 
C ON SURFACES OF ANY ORIENTATION.
C*******************************************************************************

      !export this subroutine for its use in external DLLs.
C      !DEC$ATTRIBUTES DLLEXPORT :: GETINCIDENTRADIATION

C-----------------------------------------------------------------------------------------------------------------------
C    ACCESS TRNSYS FUNCTIONS
	USE TrnsysFunctions
	USE TrnsysConstants
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    USER DECLARATIONS
      IMPLICIT NONE
	DOUBLE PRECISION TIME,TIME0,TFINAL,DELT,SOLAR(13),SC,TD1,TD2,I,IDN
	DOUBLE PRECISION EOT,XX,ANGLE,TT,ET,SHIFT,RDCONV,PI,PI2365,DGCONV
	DOUBLE PRECISION P11(8),P12(8),P13(8),P21(8),P22(8),P23(8),ALAT
	DOUBLE PRECISION LATITUDE,SINLAT,COSLAT,TANLAT,DAY,DECL
	DOUBLE PRECISION SINDEC,COSDEC,TANDEC,TANSUM,WS,SD,SU,CC,SS
	DOUBLE PRECISION ECC,SMALL,W1,W2,W,COSHR,SINHR,DDELT
	DOUBLE PRECISION WD1,WD2,HX1,HX2,HEXTRA,HRATIO
	DOUBLE PRECISION SAZM,COSZEN,ZENITH,SINZEN,TANZEN,SINAZM,CWEW,WEW
	DOUBLE PRECISION HHOR,HD,HB,HDN,DIFFRAT,BEAMRAT
	DOUBLE PRECISION RHOG,AXSLP,AXAZM,SINASL,COSASL,TANASL,ALF,COSTTP
	DOUBLE PRECISION SLOPE,AZM,AUX,TANSLP,TANGAM,GAM,COSSLP,SINSLP
	DOUBLE PRECISION COSTT,THETA,RB,HBEAM,HGRF,HDIFF,AI,F,SCUBE,EPS
	DOUBLE PRECISION EPSILN,SKYB,P1,P2,A1,B1,HTOTAL,AZIMUTH
	INTEGER MODE_TRACK,MODE_TILT,I_SOLARTIME,NBIN,IERROR_RAD,INFO(15)
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    CONSTANTS REQUIRED BY THE MODEL
      DATA RDCONV/0.017453292D0/,DGCONV/57.2957795D0/
	DATA PI/3.141592654D0/
      DATA PI2365/0.0172028D0/
	DATA SMALL/0.000001/

C    DATA FOR PEREZ MODEL REPORTED IN SANDIA REPORT, 1988.
      DATA P11 /-0.196,0.236,0.454,0.866,1.026,0.978,0.748,0.318/
      DATA P12 /1.084,0.519,0.321,-0.381,-0.711,-0.986,-0.913,-0.757/
      DATA P13 /-0.006,-0.18,-0.255,-0.375,-0.426,-0.35,-0.236,0.103/
      DATA P21 /-0.114,-0.011,0.072,0.203,0.273,0.28,0.173,0.062/
      DATA P22 /0.18,0.02,-0.098,-0.403,-0.602,-0.915,-1.045,-1.698/
      DATA P23 /-0.019,-0.038,-0.046,-0.049,-0.061,-0.024,0.065,0.236/
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    STATEMENT FUNCTIONS TO CALCULATE SOLAR TIME AND HOUR ANGLES. THE EQUATION OF TIME IS FROM D.L. SIEBERS, TECH.
C    REPORT  ME-HTL-75-2, PURDUE UNIVERSITY, LAFAYETTE, INDIANA
      EOT(XX) = -(0.1236D0*DSIN(XX) - 0.004289D0*DCOS(XX) 
	1        + 0.1539D0*DSIN(2.D0*XX)+ 0.06078D0*DCOS(2.D0*XX))
      ANGLE(TT) = ((DMOD(TT+ET,24.D0)-12.D0)*15.D0 + SHIFT)*RDCONV
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
C    GET GLOBAL TRNSYS SIMULATION VARIABLES
      TIME0=getSimulationStartTime()
      TFINAL=getSimulationStopTime()
      DELT=getSimulationTimeStep()
C-----------------------------------------------------------------------------------------------------------------------

C-----------------------------------------------------------------------------------------------------------------------
      ALAT=LATITUDE*RDCONV
	SC=4871.
      IF(I_SOLARTIME.LT.0) SHIFT = 0.
	IERROR_RAD=0
	SOLAR=0.

      IF(MODE_TRACK .LT. 1 .OR. MODE_TRACK .GT. 4) THEN
	   IERROR_RAD=1
	   RETURN
	ENDIF

      IF(MODE_TILT .LT. 1 .OR. MODE_TILT .GT. 4) THEN
	   IERROR_RAD=1
	   RETURN
      ENDIF

      IF((ALAT.LT.-90.D0*RDCONV).OR.(ALAT.GT.90.D0*RDCONV)) THEN
	   IERROR_RAD=1
	   RETURN
      ENDIF

      IF((SHIFT.LT.-360.D0).OR.(SHIFT.GT.360.D0)) THEN
	   IERROR_RAD=1
	   RETURN
      ENDIF

C    DECLINATION AND OTHER DAY DEPENDENT QUANTITIES.
	ALAT = SIGN(DMAX1(DABS(ALAT),1.D-06),ALAT)
      SINLAT =DSIN(ALAT)
      COSLAT = DCOS(ALAT)
      TANLAT = SINLAT/COSLAT
      DAY = DBLE(JFIX(TIME/24.))
      DECL = 0.40928D0*DSIN((284.D0+DAY)*PI2365)
      SINDEC = DSIN(DECL)
      COSDEC = DCOS(DECL)
      TANDEC = SINDEC/COSDEC
      TANSUM=-TANDEC*TANLAT
      IF(TANSUM.GT.1.D0) TANSUM=1.D0
      IF(TANSUM.LT.-1.D0) TANSUM=-1.D0
      WS=DACOS(TANSUM)

C    CALCULATE SUNUP AND SUNDOWN
      ECC = 1.D0 + 0.033D0*DCOS(DAY*PI2365)
      IF (I_SOLARTIME.GE.0) THEN
	   ET = EOT((DAY-1.D0)*PI2365)
	ELSE
         ET = 0.D0
      ENDIF

      SD = ((WS/RDCONV) - SHIFT)/15.D0 + 12.D0 - ET
      SU = ((-WS/RDCONV) - SHIFT)/15.D0 + 12.D0 - ET
      CC = COSLAT*COSDEC
      SS = SINLAT*SINDEC

C    FIND HOUR ANGLES FOR START AND END OF TIMESTEP. THE PORTION OF THE TIMESTEP DURING WHICH THE SUN IS DOWN IS IGNORED.
      W1 = ANGLE(TIME-DELT)
      W2 = ANGLE(TIME)

C     NO POINT IN CONTINUING IF THERE IS NO SUN
      IF (W2 .GT. -WS .AND. W1 .LT. WS) THEN
         W1 = DMAX1(W1,-WS)
         W2 = DMIN1(W2,WS)
c This was commented out by Nate 10-4-07 based on information from Thornton that the fix was more harmful than good for several locations.
C        MAKE SURE W2 IS GREATER THAN W1 (IT CAN BE SET OPPOSITE WITH A COMBINATION OF HIGH LATITUDES AND
C        HIGH SHIFT VALUES NEAR MIDNIGHT)
C         IF(W2.LT.W1) THEN
C            W2=W2+2.*PI
C		  W2 = DMIN1(W2,WS)
C         ENDIF

         IF (DABS(W2-W1) .GT. 0.D0) THEN
            W  = (W1+W2)*0.5D0
            COSHR = DCOS(W)
            SINHR = DSIN(W)

            DDELT = TD2-TD1

C          FIND HOUR ANGLES FOR START AND END OF DATA INTERVAL
            WD1 = ANGLE(TD1)
            WD2 = ANGLE(TD2)

C          MAKE SURE WD2 IS GREATER THAN WD1 (IT CAN BE SET OPPOSITE WITH A COMBINATION OF HIGH LATITUDES AND
C          HIGH SHIFT VALUES NEAR MIDNIGHT)
            IF(WD2.LT.WD1) THEN
	         WD2=WD2+2.*PI
               IERROR_RAD=2
	      ENDIF
 
            WD1 = DMAX1(WD1,-WS)
            WD2 = DMIN1(WD2,WS)

		  IF((W1.LT.(WD1-SMALL)).OR.(W1.GT.(WD2+SMALL))) THEN
               IERROR_RAD=4
            ELSE IF((W2.LT.(WD1-SMALL)).OR.(W2.GT.(WD2+SMALL))) THEN
               IERROR_RAD=4
            ENDIF
            
            HX1 = CC*(DSIN(W2)-DSIN(W1)) + SS*(W2-W1)
            HX2 = CC*(DSIN(WD2)-DSIN(WD1)) + SS*(WD2-WD1)
            HEXTRA = SC*ECC*HX1/(W2-W1)
            
C          FIND POSITION OF THE SUN
            SAZM = 0.D0
            COSZEN = CC*COSHR + SS
            COSZEN = DSIGN(DMAX1(DABS(COSZEN),SMALL),COSZEN)

            ZENITH = DACOS(COSZEN)

CJT        ADDED A CHECK HERE ON ZENITH ANGLE AS THINGS CAN GO BLAMMO (TECHNICAL TERM) WHEN TILTED SURFACE CALCULATIONS
CJT        ARE PERFORMED WITH ZENITH ANGLES APPROACHING 90 DEGREES AS 1/COS GOES TO INFINITY
            IF(ZENITH.GT.(87.5*RDCONV)) THEN
	         ZENITH=87.5*RDCONV
	         COSZEN=DCOS(ZENITH)
               SINZEN = DSIN(ZENITH)
               TANZEN = SINZEN/COSZEN
	      ELSE
               SINZEN = DSIN(ZENITH)
               TANZEN = SINZEN/COSZEN
	      ENDIF

            IF (DABS(SINZEN) .GE. SMALL) THEN
              SINAZM = COSDEC*SINHR/SINZEN
              SINAZM=DSIGN(DMIN1(DABS(SINAZM),1.D0),SINAZM)
              SAZM = DASIN(SINAZM)

C            DETERMINE IF THE ABSOLUTE VALUE OF THE SOLAR AZIMUTH IS GREATER THAN 90 DEGREES BY COMPARING THE HOUR
C            ANGLE WITH THE HOUR ANGLE AT WHICH THE SOLAR AZIMUTH IS +/- 90 DEGREES
              CWEW = TANDEC/TANLAT
              CWEW = DSIGN(DMIN1(DABS(CWEW),1.D0),CWEW)
              WEW = PI
              IF(ALAT*(DECL-ALAT) .LE. 0.D0) WEW = DACOS(CWEW)
              IF((DABS(W)-DABS(WEW))*ALAT*(DECL-ALAT) .LE. 0.D0)
     .          SAZM = DSIGN(PI,SAZM) - SAZM

C             DON'T ALLOW THE ABSOLUTE VALUE OF THE SOLAR AZIMUTH TO BE GREATER THAN 180 DEGREES.
              IF(DABS(SAZM) .GT. PI) SAZM = SAZM - DSIGN((2.D0*PI),SAZM)
            ENDIF

C          CALCULATE EXTRATERRESTRIAL RADIATION FOR TIMESTEP AND THE RATIO OF EXTRATERRESTRIAL FOR TIMESTEP TO 
C          EXTRATERRESTRIAL OVER PERIOD OF DATA. RADIATION ON THE HORIZONTAL IS INTERPOLATED FROM DATA USING
C          THE CURVE FOR EXTRATERRESTRIAL RADIATION.
            IF (HEXTRA .LT. SMALL .OR. I.LE.0.D0) GOTO 260
            HRATIO = HX1/HX2
            HRATIO = HRATIO*DDELT/DELT

C          INPUTS ARE TOTAL (HORIZONTAL) AND DIRECT NORMAL
            HHOR = DMIN1(SC,(I*HRATIO))
            HDN = DMIN1(SC,(IDN*HRATIO))
            HB = DMIN1(HHOR,(HDN*COSZEN))
            HB = DMAX1(HB,0.D0)
            HD = DMAX1(HHOR-HB,0.D0)

C         CHECK FOR BEAM RADIATION GREATER THAN EXTRATERRESTRIAL RADIATION
            IF(HB .GT. HEXTRA) THEN
               DIFFRAT=DMIN1(1.,DMAX1(HD/HHOR,SMALL))
               BEAMRAT=DMIN1(1.,DMAX1(HB/HHOR,SMALL))
               HB = HEXTRA
               HHOR=HB/BEAMRAT
               HD=HHOR*DIFFRAT
               IERROR_RAD=3
            ENDIF

C          SET FIRST SET OF OUTPUTS
            SOLAR(1) = HEXTRA
            SOLAR(2) = ZENITH*DGCONV
            SOLAR(3) = SAZM*DGCONV
            SOLAR(7) = HHOR
            SOLAR(8) = HB
            SOLAR(9) = HD

C          CALCULATE BEAM RADIATION, TOTAL RADIATION AND INCIDENCE ANGLE FOR EACH SLOPE.
            AXSLP = SLOPE*RDCONV
            AXAZM = AZIMUTH*RDCONV
            SINASL = DSIN(AXSLP)
            COSASL = DCOS(AXSLP)
            COSASL = DSIGN(DMAX1(DABS(COSASL),SMALL),COSASL)
            TANASL = SINASL/COSASL
            ALF = SAZM - AXAZM
            ALF = DSIGN(DMAX1(DABS(ALF),SMALL),ALF)
            
C           KEEP THE DIFFERENCE OF SOLAR AZIMUTH AND AXIS AZIMUTH BETWEEN 180 AND -180 DEGREES
             IF(DABS(ALF).GT.PI) ALF = ALF - DSIGN((2.D0*PI),ALF)
 
             COSTTP = COSASL*COSZEN + SINASL*SINZEN*DCOS(ALF)
             COSTTP = DSIGN(DMAX1(DABS(COSTTP),SMALL),COSTTP)

C           CASE ON TRACKING MODE
             GO TO (221,223,222,225) ,MODE_TRACK

C           FIXED SURFACE
221          SLOPE = AXSLP
             AZM = AXAZM
             GO TO 227

C           SINGLE-AXIS TRACKER
222          AUX=DABS(DMOD(AXSLP*DGCONV,180.D0))
             IF(AUX.GT.0.1D0.AND.AUX.LE.179.9D0) GO TO 223

C           HORIZONTAL AXIS
             IF(ALF.LT.0.D0) AZM = AXAZM - PI/2.D0
             IF(ALF.GT.0.D0) AZM = AXAZM + PI/2.D0
             TANSLP = TANZEN*DCOS(SAZM - AZM)
             SLOPE = DATAN(TANSLP)
             IF(SLOPE.LT.0.D0) SLOPE = PI + SLOPE
             GO TO 227

223          AUX=DABS(DMOD(AXSLP*DGCONV,90.D0))
             IF(AUX.GT.0.1D0.AND.AUX.LE. 89.9D0.AND.MODE_TRACK.NE.2) 
	1          GO TO 224

C           VERTICAL AXIS
             SLOPE = AXSLP
             AZM = SAZM
             GO TO 227

C           SLOPED AXIS
224          TANGAM = SINZEN*DSIN(ALF)/SINASL/COSTTP
             GAM = DATAN(TANGAM)
             IF(GAM.LT.0.D0 .AND. ALF.GT.0.D0) GAM = PI + GAM
             IF(GAM.GT.0.D0 .AND. ALF.LT.0.D0) GAM = GAM - PI
             TANSLP = TANASL/DCOS(GAM)
             SLOPE = DATAN(TANSLP)
             IF(SLOPE.LT.0.D0) SLOPE = PI + SLOPE
             AZM = GAM + AXAZM
             IF(DABS(AZM).GT.PI) AZM = AZM - DSIGN((2.D0*PI),AZM)
             GO TO 227

C           TWO-AXIS TRACKER
225          AZM = SAZM
             SLOPE = ZENITH
C
C           END OF CASE ON MODE_TRACK

227          COSSLP  =  DCOS(SLOPE)
             SINSLP  =  DSIN(SLOPE)
             COSTT = COSSLP*COSZEN + SINSLP*SINZEN*DCOS(SAZM - AZM)
             IF (COSTT.GT.1.D0) COSTT=1.D0

             THETA = DACOS(COSTT)*DGCONV
             SLOPE = SLOPE*DGCONV
             AZIMUTH = AZM*DGCONV

C           BEAM AND GROUND REFLECTED RADIATION INDEPENDENT OF TILTED SURFACE MODEL
             RB = DMAX1(COSTT,0.D0)/DMAX1(COSZEN,0.01745D0)
             HBEAM = HB*RB
             HGRF  = HHOR*RHOG*0.5D0*(1.D0 - COSSLP)

C           CASE ON TILTED SURFACE CORRELATION MODE
             GO TO (228,229,230,231), MODE_TILT

C           ISOTROPIC SKY MODEL FOR TILTED SURFACE DIFFUSE 
228          HDIFF = HD*0.5D0*(1.D0 + COSSLP)
             GO TO 235

C           HAY MODEL FOR TILTED SURFACE DIFFUSE
229          AI = HB/HEXTRA
             HDIFF = HD * (0.5D0*(1.D0-AI)*(1.D0+COSSLP) + AI*RB)
             GO TO 235

C           REINDL TILTED SURFACE MODEL
230          AI = HB/HEXTRA
             IF (DABS(HHOR).LT.SMALL) HHOR = SMALL
             F  = DSQRT(HB/HHOR)
             SCUBE = (DSIN(SLOPE*0.5D0*RDCONV))**3.D0
             HDIFF = HD*(0.5D0*(1.D0-AI)*(1.D0+COSSLP)*(1.D0+F*SCUBE) + 
	1               AI*RB)
             GO TO 235

C           PEREZ POINT SOURCE MODEL (SANDIA REPORT OCT, 1988)
231          HDN = HB/COSZEN
             IF ( HD .LT. SMALL ) THEN
                EPS = 99999.D0
             ELSE
                EPSILN = ( HD + HDN ) / HD
                EPS = ( EPSILN+1.041D0*ZENITH**3.D0 )/
	1                ( 1.D0+1.041D0*ZENITH**3.D0 )
             ENDIF
             SKYB = HD/HEXTRA
             IF (EPS .GT. 0.D0 .AND. EPS .LE. 1.065D0) THEN
               NBIN = 1
             ELSEIF (EPS .GT. 1.065D0 .AND. EPS .LE. 1.230D0) THEN
               NBIN = 2
             ELSEIF (EPS .GT. 1.23D0 .AND. EPS .LE. 1.5D0) THEN
               NBIN = 3
             ELSEIF (EPS .GT. 1.5D0 .AND. EPS .LE. 1.95D0) THEN
               NBIN = 4
             ELSEIF (EPS .GT. 1.95D0 .AND. EPS .LE. 2.8D0) THEN
               NBIN = 5
             ELSEIF (EPS .GT. 2.8D0 .AND. EPS .LE. 4.5D0) THEN
               NBIN = 6
             ELSEIF (EPS .GT. 4.5D0 .AND. EPS .LE. 6.2D0) THEN
               NBIN = 7
             ELSE
               NBIN = 8
             ENDIF

             P1 = P11(NBIN) + P12(NBIN)*SKYB + P13(NBIN)*ZENITH
             IF ( P1 .LT. 0.D0 ) P1 = 0.D0
             P2 = P21(NBIN) + P22(NBIN)*SKYB + P23(NBIN)*ZENITH
             A1 = DMAX1(COSTT,0.0)
             B1 = DMAX1(DCOS(85.D0*RDCONV),COSZEN)
              
             IF (HB.GT.0.D0) THEN
                HDIFF=HD*(0.5D0*(1.D0-P1)*(1.D0+COSSLP)+
	1                P1*A1/B1+P2*SINSLP)
             ELSE !DIFFUSE ASSUMED ISOTROPIC IF BEAM NOT POSITIVE
                HDIFF = HD*0.5D0*(1.D0 + COSSLP)
             ENDIF
             IF ( HDIFF .LT. 0.D0 )  HDIFF = 0.D0

C           END OF CASE
235         CONTINUE

C           OUTPUT TOTAL (FLAT SURFACE) RADIATION, BEAM RADIATION,DIFFUSE RADIATION, INCIDENCE ANGLE, AND SLOPE.
            HTOTAL=HBEAM + HDIFF + HGRF
	      IF(HTOTAL.GT.SC) THEN
	         HBEAM=HBEAM*SC/HTOTAL
	         HDIFF=HDIFF*SC/HTOTAL
	         HGRF=HGRF*SC/HTOTAL
               IERROR_RAD=3
	      ENDIF

C          FILL THE ARRAY
	      SOLAR(4)=SLOPE
	      SOLAR(5)=AZIMUTH
	      SOLAR(6)=THETA
		  SOLAR(10) = HBEAM + HDIFF + HGRF
            SOLAR(11) = HBEAM
            SOLAR(12) = HDIFF
            SOLAR(13) = HGRF
        ENDIF
      
	ELSE
         SOLAR(1) = 0.
         SOLAR(2) = 90.
         SOLAR(3) = -90.
	   SOLAR(4) = SLOPE
	   SOLAR(5) = AZIMUTH
	   SOLAR(6) = 90.
         SOLAR(7) = 0.
	   SOLAR(8) = 0.
         SOLAR(9) = 0.
	   SOLAR(10) = 0.
         SOLAR(11) = 0.
         SOLAR(12) = 0.
         SOLAR(13) = 0.

	ENDIF
	
	RETURN

C    NO SOLAR

260   SOLAR(1) = HEXTRA
      SOLAR(2) = ZENITH*DGCONV
      SOLAR(3) = SAZM*DGCONV
	SOLAR(4) = SLOPE
	SOLAR(5) = AZIMUTH

	RETURN
      END
C-----------------------------------------------------------------------------------------------------------------------



