kipp_colnames Subroutine

private subroutine kipp_colnames(hdrfile, ncols, names, colunits)

Read every column name and optional unit of a .kipp sidecar header, in column order.

Second read of header, after parse_kipp_header; unit is third word of a column line ("Header file", docs/kippfiles.md), columns w/o a name line are blank.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: hdrfile

Path of sidecar header.

integer(kind=int32), intent(out) :: ncols

Number of columns, from ncols line, or 512 if missing.

character(len=32), intent(out), allocatable :: names(:)

Name of each column, blank if unnamed.

character(len=32), intent(out), allocatable :: colunits(:)

Unit of each column, blank if none stated.


Source Code

   subroutine kipp_colnames(hdrfile, ncols, names, colunits)
      !! Read every column name and optional unit of a `.kipp` sidecar header, in column order.
      !!
      !! Second read of header, after `parse_kipp_header`; unit is third word of a column line
      !! ("Header file", docs/kippfiles.md), columns w/o a name line are blank.
      ! Arguments
      character(len=*), intent(in) :: hdrfile
         !! Path of sidecar header.
      integer(int32), intent(out) :: ncols
         !! Number of columns, from `ncols` line, or 512 if missing.
      character(len=32), allocatable, intent(out) :: names(:)
         !! Name of each column, blank if unnamed.
      character(len=32), allocatable, intent(out) :: colunits(:)
         !! Unit of each column, blank if none stated.
      integer(int32), parameter :: MAXC = 512
      integer(int32) :: iunit, iostat, idx
      character(len=256) :: line, key, nm
      character(len=32) :: tmp(MAXC), utmp(MAXC)

      tmp = ''; utmp = ''
      ncols = 0
      open (NEWUNIT=iunit, FILE=trim(hdrfile), STATUS='OLD', ACTION='READ', &
            IOSTAT=iostat)
      if (iostat /= 0) &
         error stop '[kipp_colnames] cannot open '//trim(hdrfile)
      do
         read (iunit, '(a)', IOSTAT=iostat) line
         if (iostat /= 0) exit
         line = adjustl(line)
         if (len_trim(line) == 0) cycle
         read (line, *, IOSTAT=iostat) key
         if (iostat /= 0) cycle
         if (key == 'ncols') then
            read (line, *, IOSTAT=iostat) key, ncols
         else if (key == 'dtype' .or. key == 'columns' .or. key == 'columns:') then
            cycle
         else
            read (line, *, IOSTAT=iostat) idx, nm
            if (iostat /= 0) cycle
            if (idx >= 1 .and. idx <= MAXC) then
               tmp(idx) = nm
               utmp(idx) = third_word(line)
            end if
         end if
      end do
      close (iunit)
      if (ncols < 1) ncols = MAXC
      allocate (names(ncols), colunits(ncols))
      names = tmp(1:ncols)
      colunits = utmp(1:ncols)
   end subroutine kipp_colnames