program RitaTX

  implicit none

      character(120)                :: rita05
      character(120), allocatable   :: HWMid(:), county(:), floodtype(:), sheetnum(:)
      integer                       :: Nsta, i, j
      integer                       :: yy, mo, dd, hh, mi
      double precision, allocatable :: lat(:), lon(:), HWMft(:), HWMm(:)
      
!!Get from the user the date of the simulation   
      write(6,*) "What is the date of the simulation start? (YYYY, MM, DD, HH, Min)"
      read(5,*) yy, mo, dd, hh, mi
      write(6,*)

!!Program will ask for the name of the text file that contains the
!hurricane data and store it as rita05.
      write(6,*) "Enter the name of the rita.txt file"
      read(5,*) rita05
      write(6,*)      

!!The program will now open the designated file and read how many stations there
!are within the text file. This number should be 364 for Hurricane Rita. This
!number will be stored as Nsta.
      open(unit=61,file=rita05,status='old',action='read')
      read(61,*) Nsta
      read(61,*)      
      read(61,*)

!!Allocate the variables in each column into there own arrays.      
      allocate(HWMid(1:Nsta), county(1:Nsta), floodtype(1:Nsta), HWMft(1:Nsta), lat(1:Nsta))
      allocate(lon(1:Nsta), sheetnum(1:Nsta), HWMm(1:Nsta))

!! Collect data from ADCIRC output file
      do i = 1,Nsta
         write(6,*) "Collecting data for each station", i, "."
         read(61,*) HWMid(i), county(i), floodtype(i), HWMft(i), lat(i), lon(i), sheetnum(i), HWMm(i)
      enddo
      close(61)
     
!! Reformat data as IMEDS generic output for each floodtype
open(unit=21,file="hwm_URS_TX_riverine.IMEDS",status='replace',action='write')
      write(21,*) "% IMEDS generic format - High Water Mark, Riverine-Hurricane floodtype"
      write(21,*) "% ", "year ","month ","day ","hour ","min ", "hwm (m)"
      write(21,*) "URS " , "UTC", " NAVD88"
open(unit=22,file="hwm_URS_TX_runup.IMEDS",status='replace',action='write')
      write(22,*) "% IMEDS generic format - High Water Mark, Coastal-Wave Run-up floodtype"
      write(22,*) "% ", "year ","month ","day ","hour ","min ", "hwm (m)"
      write(22,*) "URS " , "UTC", " NAVD88"
open(unit=23,file="hwm_URS_TX_surge.IMEDS",status='replace',action='write')
      write(23,*) "% IMEDS generic format - High Water Mark, Coastal-Surge Only floodtype"
      write(23,*) "% ", "year ","month ","day ","hour ","min ", "hwm (m)"
      write(23,*) "URS " , "UTC", " NAVD88"
open(unit=24,file="hwm_URS_TX_height.IMEDS",status='replace',action='write')
      write(24,*) "% IMEDS generic format - High Water Mark, Coastal-Wave Height floodtype"
      write(24,*) "% ", "year ","month ","day ","hour ","min ", "hwm (m)"
      write(24,*) "URS " , "UTC", " NAVD88"
open(unit=25,file="hwm_URS_TX_break.IMEDS",status='replace',action='write')
      write(25,*) "% IMEDS generic format - High Water Mark, Levee Break floodtype"
      write(25,*) "% ", "year ","month ","day ","hour ","min ", "hwm (m)"
      write(25,*) "URS " , "UTC", " NAVD88"
open(unit=26,file="hwm_URS_TX_issue.IMEDS",status='replace',action='write')
      write(26,*) "% IMEDS generic format - High Water Mark, Levee-Issue floodtype"
      write(26,*) "% ", "year ","month ","day ","hour ","min ", "hwm (m)"
      write(26,*) "URS " , "UTC", " NAVD88"
                  
do j = 1,Nsta
select case (floodtype(j))
case("Riverine-Hurricane")
      write(21,*) TRIM(HWMid(j)), lat(j), lon(j) 
      write(21,*) yy, mo, dd, hh, mi, HWMm(j)
case("Coastal-WaveRun-up")
      write(22,*) TRIM(HWMid(j)), lat(j), lon(j)
      write(22,*) yy, mo, dd, hh, mi, HWMm(j)
case("Coastal-SurgeOnly")
      write(23,*) TRIM(HWMid(j)), lat(j), lon(j)
      write(23,*) yy, mo, dd, hh, mi, HWMm(j)
case("Coastal-WaveHeight")
      write(24,*) TRIM(HWMid(j)), lat(j), lon(j)
      write(24,*) yy, mo, dd, hh, mi, HWMm(j)
case("Leveebreak")
      write(25,*) TRIM(HWMid(j)), lat(j), lon(j)
      write(25,*) yy, mo, dd, hh, mi, HWMm(j)
case("Levee-issue")
      write(26,*) TRIM(HWMid(j)), lat(j), lon(j)
      write(26,*) yy, mo, dd, hh, mi, HWMm(j)
      end select
enddo
close(21)
close(22)
close(23)
close(24)
close(25)
close(26)


end program
