read_json_content Subroutine

private subroutine read_json_content(unit_in, mol_index, unit_out, filename)

Uses

  • proc~~read_json_content~~UsesGraph proc~read_json_content read_json_content module~mqc_io_helpers mqc_io_helpers proc~read_json_content->module~mqc_io_helpers

Read and write JSON content from an individual molecule file Properly handles nested structures from fragmented calculations

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: unit_in
integer, intent(in) :: mol_index
integer, intent(in) :: unit_out
character(len=*), intent(in) :: filename

Calls

proc~~read_json_content~~CallsGraph proc~read_json_content read_json_content error error proc~read_json_content->error proc~get_molecule_name get_molecule_name proc~read_json_content->proc~get_molecule_name

Called by

proc~~read_json_content~~CalledByGraph proc~read_json_content read_json_content proc~merge_multi_molecule_json merge_multi_molecule_json proc~merge_multi_molecule_json->proc~read_json_content proc~run_multi_molecule_calculations run_multi_molecule_calculations proc~run_multi_molecule_calculations->proc~merge_multi_molecule_json program~main main program~main->proc~run_multi_molecule_calculations

Variables

Type Visibility Attributes Name Initial
character(len=10000), private, allocatable :: all_lines(:)
integer, private :: i
integer, private :: io_stat
character(len=10000), private :: line
integer, private :: nlines

Source Code

   subroutine read_json_content(unit_in, mol_index, unit_out, filename)
      !! Read and write JSON content from an individual molecule file
      !! Properly handles nested structures from fragmented calculations
      use mqc_io_helpers, only: get_molecule_name

      integer, intent(in) :: unit_in, mol_index, unit_out
      character(len=*), intent(in) :: filename

      character(len=10000), allocatable :: all_lines(:)
      character(len=10000) :: line
      integer :: io_stat, nlines, i

      ! Read all lines into memory
      allocate (all_lines(1000))  ! Reasonable size for most JSON files
      nlines = 0

      do
         read (unit_in, '(a)', iostat=io_stat) line
         if (io_stat /= 0) exit
         nlines = nlines + 1
         if (nlines > size(all_lines)) then
            ! Reallocate if needed
            call logger%error("JSON file too large: "//trim(filename))
            return
         end if
         all_lines(nlines) = line
      end do

      ! Lines structure:
      ! 1: "{"
      ! 2: '  "molecule_name": {'
      ! 3..(n-2): content
      ! n-1: "  }"
      ! n: "}"

      if (nlines < 3) then
         call logger%error("Invalid JSON structure: "//trim(filename))
         return
      end if

      ! Write molecule key (extracted from filename)
      if (mol_index > 1) write (unit_out, '(a)') '    },'
      write (unit_out, '(a)') '    "'//trim(get_molecule_name(filename))//'" : {'

      ! Write all content lines (from line 3 to line n-2)
      do i = 3, nlines - 2
         write (unit_out, '(a)') '  '//trim(all_lines(i))  ! Add 2 spaces for proper indentation
      end do

      deallocate (all_lines)

   end subroutine read_json_content