* find-date.f
*
* Run this program interactively to find the year from a
* corresponding FOAM history file postfix


      character 
     |  charmon*3, ayr*4,
     |  amon*2

      print *
      print *
      print *, 'Program Find-Datepost'
      print *
      print *, '0) Quit' 
      print *, '1) Calculate month/year from FOAM history ',
     |  'file postfix, or'
      print *, '2) Calculate FOAM history file postfix from',
     |  ' month/year (mmyyyy)'
      read(*,*) icalc
      if( icalc .eq. 0 ) stop
      if( icalc .eq. 2 ) goto 200

*     Calculate month/year
*     --------------------
100   continue
      print *
      print *, 'Enter postfix (zero to quit):'
      read(*,*) ipost
      if( ipost .eq. 0 ) stop
      call calcdat( ipost, ayr,amon, charmon )
      goto 100

*     Calculate postfix
*     -----------------
200   continue
      print *
      print *, 'Enter month/year (mmyyyy) (zero to quit):'
      read(*,*) imoyr
      if( imoyr .eq. 0 ) stop
      call calcpost( ipost, imoyr )
      goto 200

      end

*     ----------------------------------------------------------------
*     ----------------------------------------------------------------

      subroutine calcdat( ipost, ayr,amon, charmon )

      character
     |  amon*2, atmp*4, ayr*4

      character*3  
     |  chmon(12), charmon

      data chmon / 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',
     |             'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC' /

      charmon = ' '

      ryr = float(ipost) / 360.
      remain = ryr - int(ryr)
c     print *, 'Calcdat: ryr = ',ryr, '  Remain = ',remain

      iyr = int(ryr) + 1
      if( remain .eq. 0 ) iyr = iyr - 1

      imon = remain * 12. + 0.1
c     rmon = remain * 12.
      if( imon .eq. 0 ) imon = 12
c     print *, 'Calcdat: imon = ',imon,'  Rmon = ',rmon

      charmon = chmon(imon)

      write ( atmp, '(i4)' ) iyr
      if( iyr.lt. 10 ) then
        ayr = '000'//atmp(4:4)
      else  if( iyr.lt.100 .and. iyr.gt.9 ) then
        ayr = '00'//atmp(3:4)
      else  if( iyr.lt.1000 .and. iyr.gt.99 ) then
        ayr = '0'//atmp(2:4)
      else  if( iyr.gt.999 ) then
        ayr = atmp(1:4)

      endif

      write ( atmp, '(i4)' ) imon
      if( imon.lt. 10 ) then
        amon = '0'//atmp(4:4)
      else  if( imon.lt.100 .and. imon.gt.9 ) then
        amon = atmp(3:4)
      endif

      print *, 'Calcdat:  postfix',ipost,'     month ',
     |  charmon,'   year',iyr
c     print *, 'Calcdat: postfix',ipost,'   month',
c    |  imon,' ',amon,' ',charmon
c     print *, 'Calcdat: year',iyr,' ',ayr

      return
      end

*     ----------------------------------------------------------------
*     ----------------------------------------------------------------

      subroutine calcpost( ipost, imoyr )

      iyr = imoyr - imoyr/10000*10000
      imo = (imoyr-iyr)/10000
      ipost = (iyr-1)*360 + imo*30
      print *, 'Calcpost:  month',imo,'   year',
     |  iyr,'     postfix',ipost

      return
      end
