program IkeCSI

  implicit none

      integer                       :: nsta, i, j, yy, mo, dd, hh
      integer                       :: dir, mi=0, ss=0, X1, X2
      double precision              :: hs, tm, tp, watlev
      double precision, allocatable :: lat(:), lon(:), conv(:)
      character(120), allocatable   :: filename(:), datum(:), stnid(:)
      character(1000)               :: tempC, templine, filestation

      write(6,*) "Enter the name of the CSI station list"
      !read(5,*) filestation
      filestation = 'CSI_Stations_v1.txt'

      ! Read Station Data
      open(unit=20,file=trim(filestation),status='old',action='read')
      read(20,*) nsta
      read(20,*)
      allocate(lat(1:nsta), lon(1:nsta),filename(1:nsta), conv(1:nsta))
      allocate(stnid(1:nsta),datum(1:nsta))
      do i=1,nsta
        read(20,*) stnid(i), filename(i), lon(i), lat(i), datum(i), conv(i)
      enddo
      close(20)

      open(unit=21,file="hs_CSI_v1.IMEDS",status='replace',action='write')
      write(21,*) "% IMEDS generic format - Wave Height"
      write(21,*) "% ", "year ","month ","day ","hour ","min ", "sec ", "hs (m)"
      write(21,*) "CSI " , "UTC", " NAVD"

      open(unit=22,file="dir_CSI_v1.IMEDS",status='replace',action='write')
      write(22,*) "% IMEDS generic format - Wave Direction"
      write(22,*) "% ", "year ","month ","day ","hour ","min ","sec ", "dir (degrees)"
      write(22,*) "CSI " , "UTC", " NAVD"
      
      open(unit=23,file="tm_CSI_v1.IMEDS",status='replace',action='write')
      write(23,*) "% IMEDS generic format - Average Wave Period"
      write(23,*) "% ", "year ","month ","day ","hour ","min ","sec ", "tp (s)"
      write(23,*) "CSI " , "UTC", " NAVD"

      open(unit=24,file="tp_CSI_v1.IMEDS",status='replace',action='write')
      write(24,*) "% IMEDS generic format - Peak Wave Period"
      write(24,*) "% ", "year ","month ","day ","hour ","min ","sec ", "tp (s)"
      write(24,*) "CSI " , "UTC", " NAVD"

      open(unit=25,file="watlev_CSI_v1.IMEDS",status='replace',action='write')
      write(25,*) "% IMEDS generic format - Water Level"
      write(25,*) "% ", "year ","month ","day ","hour ","min ","sec ", "watlev (m)"
      write(25,*) "CSI " , "UTC", " NAVD"

      do i=1,nsta
        write(6,*) "Processing station Id: ", i, trim(stnid(i))      
        open(unit=11,file=trim(filename(i)),status='old',action='read')
          read(11,*)
          do j = 21,25
            write(j,*) trim(stnid(i)), lat(i), lon(i)
          enddo       
          do
            read(11,'(a)',END=999) tempC
            if (tempC(1:1).eq.' ') goto 999
            X1 = index(tempC,"/")
            X2 = index(tempC(X1+1:len_trim(tempC)),"/")+X1
            read(tempC(1:X1-1),'(i10)') mo
            read(tempC(X1+1:X2-1),'(i10)') dd
            read(tempC(X2+1:X2+4),'(i10)') yy
            read(tempC(X2+5:len_trim(tempC)),'(a)',ERR=998,END=998) templine
            read(templine,*,ERR=998,END=998) hh, hs, tm, dir, tp, watlev
            goto 997
            watlev = -999
998         read(templine,*) hh, hs, tm, dir,tp
997         continue
            if (hs.le.-1) hs = -999
            if (dir.le.-1) dir = -999
            if (tm.le.-1) tm = -999
            if (tp.le.-1) tp = -999
            if (watlev.le.-1) watlev = -999
            if (hh.ge.100) hh = hh/100
            if (conv(i).ne.-9999) watlev = watlev + conv(i)
            write(21,220) yy, mo, dd, hh, mi, ss, hs
            write(22,221) yy, mo, dd, hh, mi, ss, dir
            write(23,220) yy, mo, dd, hh, mi, ss, tm
            write(24,220) yy, mo, dd, hh, mi, ss, tp
            write(25,220) yy, mo, dd, hh, mi, ss, watlev
          enddo        
999     close(10)
      enddo
      close(21)
      close(22)
      close(23)
      close(24)
      close(25)
220   Format(6i7,f14.4)
221   Format(6i7,i14)
end program
