Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | funit | |||
logical, | intent(in) | :: | header |
function input_count_rows(funit, header) result(nlines)
! Author: stefan.liersch@pik-potsdam.de
! Date: 2009-09-20
! PURPOSE: counts the number of rows in input file with given file unit
! The file must already be open!!!
! file unit
integer, intent(in) :: funit
! true if input file contains a header
logical, intent(in) :: header
! counter
integer :: nlines
! file status
integer :: status
! dummy for reading
character(len=read_line_buffer_size) :: a
character(len=path_max_length) fname
status = 0
nlines = 0
! rewind file in order to be sure to read the file from the first line
rewind(funit)
if (header) nlines = nlines - 1
! count number of rows in file: fname
do
read(funit, *, IOSTAT=status) a
if (status > 0) then ! read error (unlikely since we are only skipping lines)
inquire(unit=funit, name=fname)
call log_error("input_count_rows", 'Unable to count lines of file '//trim(fname))
else if (status == -1 .or. len_trim(a) == 0) then ! end of file or empty line
EXIT
end if
nlines = nlines + 1
end do
rewind(funit)
end function input_count_rows