Program Exercise
 Implicit none
 
 integer :: istat, rstat
 
 character(len=66) :: line
 character(len=16) :: date
 character(len=16), allocatable :: date_a(:)
 real :: hs, tmm10, mdir, ws, wd
 real, allocatable :: hs_a(:), tmm10_a(:), mdir_a(:), ws_a(:), wd_a(:) 
 
 integer :: i, nline, ic
 
 open(10, file='TS_1979-2022_106-2.dat',status='old')
 open(20, file='Exercise_result.dat'   ,status='unknown')
 
 nline = 0 ! 파일에서 총 라인
 ic    = 0 ! 순 정상 데이터
 
 do 
! 문자열로 일단 다 읽어들임. 파일의 라인 자체를 세봄.
   read (10, fmt='(A)', iostat=istat) line
   if (istat /= 0) then 
       exit
   else 
       nline = nline + 1
   end if

   read (line, fmt='(A16, 5F10.2)', iostat=rstat) date, hs, tmm10, mdir, ws, wd
   if (rstat == 0) then ! 정상 데이터
       ic = ic + 1
! 확인 출력시 주석 해제
!       write (*, '(I3, A3, A16, 5F10.2)') ic, ' - ', date, hs, tmm10, mdir, ws, wd
   else
! Format 불일치. Error 이거나, Header이거나. 굳이 처리 안함.

   end if
 end do

! 파일 offset의 위치를 처음으로 돌림.
 rewind (10)

! 정상 데이터의 갯수로 배열 크기 지정, ic 다시 초기화
 allocate(date_a(ic), hs_a(ic), tmm10_a(ic), mdir_a(ic), ws_a(ic), wd_a(ic))
 ic = 0

! 파일 다시 읽어들임.
do i = 1, nline
   read (10, fmt='(A)', iostat=istat) line
   if (istat /= 0) then 
       exit
   end if

   read (line, fmt='(A16, 5F10.2)', iostat=rstat) date, hs, tmm10, mdir, ws, wd
   if (rstat == 0) then ! 정상 데이터
       ic = ic + 1
       date_a(ic) = date 
       hs_a(ic) = hs 
       tmm10_a(ic) = tmm10 
       mdir_a(ic) = mdir 
       ws_a(ic) = ws
       wd_a(ic) = wd
   else

   end if
 end do

 ! 읽어들인 데이터를 출력 파일로 출력하여 확인
 do i = 1, ic
   write(20, fmt='(I3, A3, A16, 5F10.2)') i, ' - ', date_a(i), hs_a(i), tmm10_a(i), mdir_a(i), ws_a(i), wd_a(i)
 end do


 
  
end program
