merge_multi_molecule_json Subroutine

public subroutine merge_multi_molecule_json(individual_files, nmol)

Uses

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

Merge individual molecule JSON files into a single combined file

Arguments

Type IntentOptional Attributes Name
character(len=256), intent(in) :: individual_files(:)
integer, intent(in) :: nmol

Calls

proc~~merge_multi_molecule_json~~CallsGraph proc~merge_multi_molecule_json merge_multi_molecule_json error error proc~merge_multi_molecule_json->error info info proc~merge_multi_molecule_json->info proc~read_json_content read_json_content proc~merge_multi_molecule_json->proc~read_json_content to_char to_char proc~merge_multi_molecule_json->to_char proc~read_json_content->error proc~get_molecule_name get_molecule_name proc~read_json_content->proc~get_molecule_name

Called by

proc~~merge_multi_molecule_json~~CalledByGraph proc~merge_multi_molecule_json merge_multi_molecule_json 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=256), private :: basename
integer, private :: dot_pos
logical, private :: file_exists
integer, private :: imol
integer, private :: io_stat
character(len=10000), private :: line
character(len=256), private :: output_file
integer, private :: slash_pos
integer, private :: unit_in
integer, private :: unit_out

Source Code

   subroutine merge_multi_molecule_json(individual_files, nmol)
      !! Merge individual molecule JSON files into a single combined file
      use mqc_io_helpers, only: get_molecule_name

      character(len=256), intent(in) :: individual_files(:)
      integer, intent(in) :: nmol

      integer :: imol, unit_in, unit_out, io_stat, slash_pos, dot_pos
      character(len=10000) :: line
      character(len=256) :: output_file, basename
      logical :: file_exists

      ! Determine combined output filename from first individual file
      ! Example: "output_multi_structure_molecule_1.json" -> "output_multi_structure.json"
      basename = individual_files(1)
      slash_pos = index(basename, '/', back=.true.)
      if (slash_pos > 0) then
         basename = basename(slash_pos + 1:)
      end if

      ! Remove "_molecule_1" or similar suffix
      dot_pos = index(basename, '_molecule_')
      if (dot_pos > 0) then
         output_file = basename(1:dot_pos - 1)//".json"
      else
         output_file = "output_combined.json"
      end if

      ! Open combined output file
      open (newunit=unit_out, file=trim(output_file), status='replace', action='write', iostat=io_stat)
      if (io_stat /= 0) then
         call logger%error("Failed to open "//trim(output_file)//" for writing")
         return
      end if

      call logger%info("Merging "//to_char(nmol)//" molecule JSON files into "//trim(output_file))

      ! Write opening brace and top-level key (basename without "output_" and ".json")
      dot_pos = index(output_file, '.json')
      if (dot_pos > 0) then
         basename = output_file(8:dot_pos - 1)  ! Skip "output_"
      else
         basename = "combined"
      end if

      write (unit_out, '(a)') "{"
      write (unit_out, '(a)') '  "'//trim(basename)//'": {'

      ! Process each individual JSON file
      do imol = 1, nmol
         inquire (file=trim(individual_files(imol)), exist=file_exists)
         if (.not. file_exists) cycle

         open (newunit=unit_in, file=trim(individual_files(imol)), status='old', action='read', iostat=io_stat)
         if (io_stat /= 0) cycle

         ! Read all lines from the individual JSON file
         call read_json_content(unit_in, imol, unit_out, individual_files(imol))

         close (unit_in)

         ! Delete individual file
         open (newunit=unit_in, file=trim(individual_files(imol)), status='old', action='readwrite')
         close (unit_in, status='delete')
      end do

      ! Close last molecule
      write (unit_out, '(a)') '    }'

      ! Close top-level key and file
      write (unit_out, '(a)') '  }'
      write (unit_out, '(a)') '}'

      close (unit_out)
      call logger%info("Combined JSON written to "//trim(output_file))

   end subroutine merge_multi_molecule_json