extract_element Subroutine

public subroutine extract_element(basis_file, element, element_content, error)

Extract the basis set data for a specific element from the basis file

Arguments

Type IntentOptional Attributes Name
type(basis_file_t), intent(in) :: basis_file
character(len=*), intent(in) :: element
character(len=:), intent(out), allocatable :: element_content
type(error_t), intent(out) :: error

Calls

proc~~extract_element~~CallsGraph proc~extract_element extract_element proc~error_set error_t%error_set proc~extract_element->proc~error_set proc~is_letter is_letter proc~extract_element->proc~is_letter proc~is_uppercase_letter is_uppercase_letter proc~extract_element->proc~is_uppercase_letter proc~uppercase uppercase proc~extract_element->proc~uppercase

Variables

Type Visibility Attributes Name Initial
logical, private :: at_line_start
integer, private :: end_pos
integer, private :: i
character(len=:), private, allocatable :: search_element
integer, private :: start_pos

Source Code

   subroutine extract_element(basis_file, element, element_content, error)
      !! Extract the basis set data for a specific element from the basis file
      type(basis_file_t), intent(in) :: basis_file
      character(len=*), intent(in) :: element
      character(len=:), allocatable, intent(out) :: element_content
      type(error_t), intent(out) :: error

      integer :: start_pos, end_pos, i
      character(len=:), allocatable :: search_element
      logical :: at_line_start

      ! Convert element to uppercase for searching
      search_element = uppercase(trim(element))

      ! Find the element name (it appears on its own line)
      start_pos = index(basis_file%data_section, new_line('a')//trim(search_element)//new_line('a'))

      if (start_pos == 0) then
         ! Try without leading newline (might be first element after $DATA)
         if (index(basis_file%data_section, trim(search_element)//new_line('a')) == 1) then
            start_pos = 1
         else
            call error%set(ERROR_VALIDATION, "Element not found in basis set file: "//element)
            return
         end if
      else
         start_pos = start_pos + 1  ! Skip the leading newline
      end if

      ! Find the next element by looking for a line that:
      ! - Starts with an uppercase letter
      ! - Has a second character that is also a letter (not a space or number)
      ! This distinguishes "CARBON" from "S   3"

      end_pos = len(basis_file%data_section)
      at_line_start = .false.

      i = start_pos + len(search_element) + 1
      do while (i < len(basis_file%data_section))
         if (basis_file%data_section(i:i) == new_line('a')) then
            at_line_start = .true.
            i = i + 1
            cycle
         end if

         if (at_line_start) then
            ! We're at the start of a new line
            if (is_uppercase_letter(basis_file%data_section(i:i))) then
               ! Check if next character is also a letter
               if (i + 1 <= len(basis_file%data_section)) then
                  if (is_letter(basis_file%data_section(i + 1:i + 1))) then
                     ! Found next element!
                     end_pos = i - 1
                     exit
                  end if
               end if
            end if
            at_line_start = .false.
         end if

         i = i + 1
      end do

      ! Extract the section
      element_content = basis_file%data_section(start_pos:end_pos)

   end subroutine extract_element