      PROGRAM READPROF

c  *****************************************************************************
c      Programmer  :  Jeffrey T. Walters
c      Date        :  March 1, 1994
c      Purpose     :
c          This program will read the data file called 'NORMPROF.XXX'
c      and output the header information and the temperature profiles
c      for each day of the year.  Or it can output a range of months
c      which must be specified at the command line.  The output will 
c      be given in 12 day blocks with depths from 0 to '?' meters.  
c      The command line syntax is :
c                                    
c          readprof XXX MMM MMM
c
c      'XXX' = 3-char lake name; MMM = (1, 2, ...12) -or- (JAN, FEB, ...DEC)
c      The output file name is 'ASC_PROF.XXX'. 
c  *****************************************************************************

      IMPLICIT  NONE
      INTEGER   I, J, Cntr, Depth, EndDay, MonthStrt, MonthEnd
      INTEGER*1 FDay, FMonth, LDay, LMonth, NumCharTitle, NumCharSub,
     &          NumCharLeg, Day, Month
      INTEGER*2 RecLen, HeadRec, DataTyp, DataPts, Lines, DepthIntrvl,
     &          Fyear, LYear, Count, N, Year, Time, PrfTmp(500)
      REAL*4    Minn, Maxx, Factor, Summand, Block(1:500,1:31)
      CHARACTER Title*40, SubT*20, Legend*20  
      CHARACTER*127 Command_Line
      CHARACTER*3 FileExtension, Option
      CHARACTER*12 FileName, FileOut 
      CHARACTER*60 T1, T2, T1a, T1b, T2a, T2b
c
c  Get info from command line
c
      Call GETCL( Command_Line )
      FileExtension=Command_Line(2:4)
      IF (FileExtension .EQ. '   ') THEN
	WRITE(*, 1205)
	READ(*, 1305) FileExtension
      ENDIF
      FileName='NORMPROF.'
      FileName(10:12)=FileExtension
      FileOut='ASC_PROF.'
      FileOut(10:12)=FileExtension
      Option='???'
      Option=Command_Line(6:8)
      CALL CAPS(Option, 3)
      IF ( ( Option .EQ. '   ' ) .OR. ( Option .EQ. '???' ) ) THEN
	  MonthStrt = 0
      ELSE 
	  CALL OPTION_CHECK(Option, MonthStrt)
      ENDIF    
      Option='???'
      Option=Command_Line(10:12)
      CALL CAPS(Option, 3)
      MonthEnd = 0
      IF ( ( Option .EQ. '   ' ) .OR. ( Option .EQ. '???' ) ) THEN
	  MonthEnd = 0
      ELSE 
	  CALL OPTION_CHECK(Option, MonthEnd)
      ENDIF    

      IF (MonthStrt .EQ. 0) THEN
	 MonthStrt = 1
	 MonthEnd = 12
      ELSE 
	 IF (MonthEnd .LT. MonthStrt) MonthEnd = MonthStrt
      ENDIF
c
c  Open the input data file
c

      OPEN(UNIT=1, FILE=FileName, STATUS='OLD', RECL=256,
     &     ACCESS='DIRECT', FORM='UNFORMATTED')
      READ(1, REC=1) RecLen
      CLOSE(1)
      OPEN(UNIT=1, FILE=FileName, STATUS='OLD', RECL=RecLen,
     &     ACCESS='DIRECT', FORM='UNFORMATTED')

c
c  Open the output data file
c

      OPEN(UNIT=2, FILE=FileOut, STATUS='OLD', ERR=10)
      CLOSE(2, STATUS='DELETE')
   10 OPEN(UNIT=2, FILE=FileOut, STATUS='NEW')


c
c  Read header information
c

      T1a='an entire year.  '
      T1b='specified months.'
      T2a='The data starts on January 1st and ends on December 31st.'
      T2b='The data covers all the days of the months' 
      IF ( MonthStrt .EQ. 1  .AND.  MonthEnd .EQ. 12) THEN
	 T1=T1a
	 T2=T2a
      ELSE
	 T1=T1b
	 T2=T2b
      ENDIF

      READ(1, REC=1) RecLen, HeadRec, DataTyp, DataPts, Lines,
     &               DepthIntrvl, FDay, FMonth, FYear, LDay, LMonth,
     &               LYear, Minn, Maxx, NumCharTitle, Title,
     &               NumCharSub, SubT, NumCharLeg, Legend
      WRITE(2, 1201) Title, SubT, T1, T2, (DataPts-1)
c
c  Initialize variables and read the rest of the records
c

      N=DataPts              ! # of temperatures
      Depth=DataPts-1
      Count=Lines+1          ! Total # of records + 1
      Cntr=0
      CALL INITIALIT(Block)

      DO 100, I=2, Count                                   ! Loop for records
	Cntr=Cntr+1
	READ(1,REC=I) Day,Month,Year,Time,
     &                Factor,Summand,(PrfTmp(J),J=1,N)
c
c  Determine the last day of the month
c
	SELECT CASE (Month)
	  CASE (1,3,5,7,8,10,12)
	    EndDay = 31
	  CASE (4,6,9,11)
	    EndDay = 30
	  CASE (2)
	    EndDay = 28
	END SELECT
c
c  Convert to units and store in 2-D Block
c
	DO J=1, N
	  Block(J, Cntr) = (PrfTmp(J)-Summand)/Factor
	ENDDO
c
c  Check for Option and write to output file
c
	IF (Cntr .GE. EndDay) THEN
	  IF ((Month .GE. MonthStrt) .AND. (Month .LE. MonthEnd)) THEN
	    Cntr=0
	    CALL WRITE_IT(Block, Depth, Month, EndDay)
	  ENDIF
	  CALL INITIALIT(Block)
	  Cntr=0
	ENDIF

  100 CONTINUE                                      ! End of loop for records

c
c  Format statements
c

 1201 FORMAT(10X, A40, /, 24X, A20, /, /, 
     & 'The following temperature data is for ', A60, /,
     & A60,/,
     & 'The temperature is in degrees Celsius and the depth range is',
     & ' 0 - ',I3,' meters.',/,
     & 'The depth interval is 1 meter.')

 1205 FORMAT(' Please enter the lake name ',
     &       '(SUP,MIC,NHU,GEO,ERI,ONT): ', $)
 1305 FORMAT(A3)

c
c  Close the files
c
      CLOSE(1)
      CLOSE(2)

c
c  The following statements do nothing but clear up the warnings when compiling 
c
      Day = Day
      Year = Year
      Time = Time
      Headrec=headrec
      DataTyp=DataTyp
      FDay=FDay
      FMonth=FMonth
      FYear=FYear
      LDay=LDay
      LMonth=LMonth
      LYear=LYear
      NumCharTitle=NumCharTitle
      NumCharSub=NumCharSub
      NumCharLeg=NumCharLeg     
      Legend=Legend
      Minn=Minn
      Maxx=Maxx
      DepthIntrvl=DepthIntrvl

      STOP
      END

c   **********************************************************************
c       This subroutine checks for an extra Option from the command line
c     and interprets it.
c
      SUBROUTINE OPTION_CHECK(Option, MonthNum)  
      IMPLICIT NONE
      INTEGER MonthNum
      CHARACTER*3 Option

      SELECT CASE (Option)
	  CASE ('1  ', 'JAN')
	    MonthNum=1
	  CASE ('2  ', 'FEB')
	    MonthNum=2
	  CASE ('3  ', 'MAR')
	    MonthNum=3
	  CASE ('4  ', 'APR')
	    MonthNum=4
	  CASE ('5  ', 'MAY')
	    MonthNum=5
	  CASE ('6  ', 'JUN')
	    MonthNum=6
	  CASE ('7  ', 'JUL')
	    MonthNum=7
	  CASE ('8  ', 'AUG')
	    MonthNum=8
	  CASE ('9  ', 'SEP')
	    MonthNum=9
	  CASE ('10 ', 'OCT')
	    MonthNum=10
	  CASE ('11 ', 'NOV')
	    MonthNum=11
	  CASE ('12 ', 'DEC')
	    MonthNum=12
	  CASE DEFAULT
	    MonthNum=0
	    print*,'Error in option chosen :'
	    print*,'There can only be one space separating lake ',
     &             'name and option.'
	    print*,'It must be in correct form. Option is NOT ',
     &             'case sensitive.'
	    print*,'(eg: JAN, feb, MaR, ..., deC -or- 1, 2, ..., 12)'
       END SELECT
       RETURN

      END

c   **********************************************************************
c           This subroutine takes the 2-D array called Block and
c         initializes it to all zeros

      SUBROUTINE INITIALIT(Block)
      IMPLICIT  NONE
      INTEGER Row, Col
      REAL*4 Block(1:500,1:31)

      DO 11 Row=1, 500
       DO 10 Col=1, 12
	 Block(Row, Col)=0.0
   10 CONTINUE
   11 CONTINUE
      RETURN
      END

c   **********************************************************************
c  This subroutine makes a string all capital letters
c

      SUBROUTINE CAPS(String, Chars)
      IMPLICIT  NONE
      INTEGER*4 Chars, I
      CHARACTER*(*) String

      DO 10, I=1, Chars
	IF ((String(I:I) .GE. 'a') .AND. (String(I:I) .LE. 'z')) THEN
	    String(I:I) = CHAR(ICHAR(String(I:I))-32)
	ENDIF
   10 CONTINUE
      RETURN
      END


c   **********************************************************************
c       This subroutine creates the header and writes it to
c     the output file.

      SUBROUTINE HEADER(Month, StartDay, EndDay)
      IMPLICIT   NONE
      INTEGER    Hrzntl(31), Start, Endd, J, StartDay, EndDay
      CHARACTER*3 Mnth
      INTEGER*1 Month

      SELECT CASE (Month)
	  CASE (1)
	    Mnth = 'JAN'
	  CASE (2)
	    Mnth = 'FEB'
	  CASE (3)
	    Mnth = 'MAR'
	  CASE (4)
	    Mnth = 'APR'
	  CASE (5)
	    Mnth = 'MAY'
	  CASE (6)
	    Mnth = 'JUN'
	  CASE (7)
	    Mnth = 'JUL'
	  CASE (8)
	    Mnth = 'AUG'
	  CASE (9)
	    Mnth = 'SEP'
	  CASE (10)
	    Mnth = 'OCT'
	  CASE (11)
	    Mnth = 'NOV'
	  CASE (12)
	    Mnth = 'DEC'
      END SELECT

      Start = StartDay
      EndD = EndDay

      DO J=1,31
	Hrzntl(J)=J
      ENDDO

c
c  Write the header to the output file
c
      WRITE(2, 1204) Mnth
      WRITE(2, 1205)
      WRITE(2, 1206) (Hrzntl(J), J=Start, Endd)
      WRITE(2, 1207)

 1204 FORMAT(/,/,20X, 'Normal Lake Temperature Profiles for ',A3)
 1205 FORMAT(/,35X, 'Day of Month', /)
 1206 FORMAT(5X,12I6)
 1207 FORMAT(4X,'----------------------------------------------------',
     &       '-----------------------')

      RETURN
      END


c   **********************************************************************
c       This subroutine takes a month of temperatures and writes them to
c     the output file 
c
      SUBROUTINE WRITE_IT(Block, Depth, Month, EndDay)
      IMPLICIT NONE
      INTEGER Depth, Row, Col, EndDay, SDay, EDay
      REAL*4  Block(1:500, 1:31)
      INTEGER*1 Month

      SDay = 1
      EDay = 12

      DO WHILE (SDay .LE. EndDay)
	IF (EDay .GT. EndDay) EDay = EndDay
	CALL HEADER(Month, SDay, EDay)
	DO Row=1, Depth+1
	   WRITE(2, 1203) Row-1, (Block(Row, Col), Col=SDay, EDay)
	ENDDO
	SDay = SDay + 12
	EDay = EDay + 12
      ENDDO

      WRITE(2, 1299)
      WRITE(2, 1300)
      WRITE(2, 1300)

 1203 FORMAT(I3, ' | ', 12F6.2)
 1299 FORMAT(/)
 1300 FORMAT('********************************************************',
     &       '************************')
      RETURN
      END

     
