<commit_before>!--------------------------------------------------------------------------------------------------!
! ANNEAL_POST
!
! Post-process a simulated annealing search output file from FLTINV. The precise output format
! depends on the options used in the annealing search. In general, the format is:
!
!     # Header Line 1
!     # Header Line 2
!     :
!     > Iteration i Header
!     value(s)
!     value(s)
!     :
!
!--------------------------------------------------------------------------------------------------!

module anneal_post

character(len=512) :: anneal_log_file
integer :: lu_log_file
character(len=512) :: best_output_file
character(len=512) :: plocked_output_file
character(len=512) :: mean_slip_output_file
integer :: burnin_iterations

integer :: nmarg1d
integer :: marg1d_flt(1000)
character(len=64) :: slip_marg_output_file(1000)

character(len=512) :: mean_pole_output_file
integer :: nmarg_pole
integer :: marg_pole(20)
character(len=64) :: pole_marg_output_file(20)

integer :: nflocked
character(len=64) :: flocked_subset_file(10)
character(len=64) :: flocked_output_file(10)

character(len=512) :: obj_file
character(len=512) :: uncert_file

logical :: doResample
integer :: nresamp
integer, allocatable :: samp(:)

end module


!==================================================================================================!


program main

use io, only: stdout
use earth, only: pole_geo2xyz, pole_xyz2geo
use annealing, only: resample
use random, only: iseed, timeseed
use sort, only: heapsort

use anneal_post, only: anneal_log_file, &
                       lu_log_file, &
                       burnin_iterations, &
                       best_output_file, &
                       plocked_output_file, &
                       mean_slip_output_file, &
                       nmarg1d, &
                       marg1d_flt, &
                       slip_marg_output_file, &
                       mean_pole_output_file, &
                       nmarg_pole, &
                       marg_pole, &
                       pole_marg_output_file, &
                       nflocked, &
                       flocked_subset_file, &
                       flocked_output_file, &
                       obj_file, &
                       uncert_file, &
                       doResample, &
                       nresamp, &
                       samp

implicit none

! Local variables
character(len=512) :: input_line
character(len=32) :: log_format
logical :: fileExists
logical, allocatable :: isAccepted(:)
logical, allocatable :: isFltInSubset(:)
integer :: it, nit
integer :: i, ios, iflt, nflt, ipole, npoles, nsubset
integer, allocatable :: samp_tmp(:)
integer, allocatable :: locked(:,:), plocked(:)
double precision, allocatable :: temp(:)
double precision, allocatable :: obj(:), obj_resamp(:)
double precision :: max_obj
double precision, allocatable :: uncert(:)
double precision :: min_obj, fraction_locked
double precision, allocatable :: slip(:,:,:), mean_ss(:), mean_ds(:)
double precision :: px, py, pz, plon, plat, pmag
double precision, allocatable :: poles(:,:,:), mean_px(:), mean_py(:), mean_pz(:)
double precision :: ddum
integer :: idum
logical :: ldum

ios = 0

! Parse command line
call gcmdln()


!----
! Open annealing log file
!----
! Check that file exists
inquire(file=anneal_log_file,exist=fileExists)
if (.not.fileExists) then
    call usage('anneal_post: no anneal log file found named '//trim(anneal_log_file))
endif

! Open the log file
lu_log_file = 21
open(unit=lu_log_file,file=anneal_log_file,status='old')


!----
! Read annealing log file
!----
! Parse log file header
call read_header(nit,nflt,npoles,log_format)


! Set up arrays
allocate(obj(nit))                ! DP: Objective function values
allocate(locked(nit,nflt))        ! Int: Locked (1) and unlocked (0) faults
allocate(slip(nit,nflt,2))        ! DP: Strike- and dip-slip of each fault
allocate(poles(nit,npoles,3))     ! DP: Pole coordinates and rotation rate
allocate(temp(nit))               ! DP: Temperature
allocate(uncert(nit))             ! DP: Model prediction error
allocate(isAccepted(nit))         ! Logical: Was the proposed model accepted?
write(*,*) 'anneal_post: finished allocating array memory'


! Resample ensemble if requested
if (doResample) then
    if (nresamp.eq.0) then
        nresamp = nit
    endif

    write(*,*) 'anneal_post: reading model objective functions for resampling'

    ! Read all objectives (log-posterior-probabilities)
    i = 0
    do
        read(lu_log_file,'(A)',iostat=ios) input_line
        if (ios.ne.0) then
            exit
        endif
        if (index(input_line,'>').eq.1) then
            if (i.ge.1) then
                call parse_iteration_header(input_line,it,ddum,obj(i),ddum,ldum,ios)
            endif
            i = i + 1
        endif
    enddo

    ! Set nit to number read in previous block
    if (i-1.ne.nit) then<commit_msg>Stop anneal_post if nresamp is greater than nit<commit_after>!--------------------------------------------------------------------------------------------------!
! ANNEAL_POST
!
! Post-process a simulated annealing search output file from FLTINV. The precise output format
! depends on the options used in the annealing search. In general, the format is:
!
!     # Header Line 1
!     # Header Line 2
!     :
!     > Iteration i Header
!     value(s)
!     value(s)
!     :
!
!--------------------------------------------------------------------------------------------------!

module anneal_post

character(len=512) :: anneal_log_file
integer :: lu_log_file
character(len=512) :: best_output_file
character(len=512) :: plocked_output_file
character(len=512) :: mean_slip_output_file
integer :: burnin_iterations

integer :: nmarg1d
integer :: marg1d_flt(1000)
character(len=64) :: slip_marg_output_file(1000)

character(len=512) :: mean_pole_output_file
integer :: nmarg_pole
integer :: marg_pole(20)
character(len=64) :: pole_marg_output_file(20)

integer :: nflocked
character(len=64) :: flocked_subset_file(10)
character(len=64) :: flocked_output_file(10)

character(len=512) :: obj_file
character(len=512) :: uncert_file

logical :: doResample
integer :: nresamp
integer, allocatable :: samp(:)

end module


!==================================================================================================!


program main

use io, only: stdout
use earth, only: pole_geo2xyz, pole_xyz2geo
use annealing, only: resample
use random, only: iseed, timeseed
use sort, only: heapsort

use anneal_post, only: anneal_log_file, &
                       lu_log_file, &
                       burnin_iterations, &
                       best_output_file, &
                       plocked_output_file, &
                       mean_slip_output_file, &
                       nmarg1d, &
                       marg1d_flt, &
                       slip_marg_output_file, &
                       mean_pole_output_file, &
                       nmarg_pole, &
                       marg_pole, &
                       pole_marg_output_file, &
                       nflocked, &
                       flocked_subset_file, &
                       flocked_output_file, &
                       obj_file, &
                       uncert_file, &
                       doResample, &
                       nresamp, &
                       samp

implicit none

! Local variables
character(len=512) :: input_line
character(len=32) :: log_format
logical :: fileExists
logical, allocatable :: isAccepted(:)
logical, allocatable :: isFltInSubset(:)
integer :: it, nit
integer :: i, ios, iflt, nflt, ipole, npoles, nsubset
integer, allocatable :: samp_tmp(:)
integer, allocatable :: locked(:,:), plocked(:)
double precision, allocatable :: temp(:)
double precision, allocatable :: obj(:), obj_resamp(:)
double precision :: max_obj
double precision, allocatable :: uncert(:)
double precision :: min_obj, fraction_locked
double precision, allocatable :: slip(:,:,:), mean_ss(:), mean_ds(:)
double precision :: px, py, pz, plon, plat, pmag
double precision, allocatable :: poles(:,:,:), mean_px(:), mean_py(:), mean_pz(:)
double precision :: ddum
integer :: idum
logical :: ldum

ios = 0

! Parse command line
call gcmdln()


!----
! Open annealing log file
!----
! Check that file exists
inquire(file=anneal_log_file,exist=fileExists)
if (.not.fileExists) then
    call usage('anneal_post: no anneal log file found named '//trim(anneal_log_file))
endif

! Open the log file
lu_log_file = 21
open(unit=lu_log_file,file=anneal_log_file,status='old')


!----
! Read annealing log file
!----
! Parse log file header
call read_header(nit,nflt,npoles,log_format)


! Set up arrays
allocate(obj(nit))                ! DP: Objective function values
allocate(locked(nit,nflt))        ! Int: Locked (1) and unlocked (0) faults
allocate(slip(nit,nflt,2))        ! DP: Strike- and dip-slip of each fault
allocate(poles(nit,npoles,3))     ! DP: Pole coordinates and rotation rate
allocate(temp(nit))               ! DP: Temperature
allocate(uncert(nit))             ! DP: Model prediction error
allocate(isAccepted(nit))         ! Logical: Was the proposed model accepted?
write(*,*) 'anneal_post: finished allocating array memory'


! Resample ensemble if requested
if (doResample) then
    if (nresamp.eq.0) then
        nresamp = nit
    endif
    if (nresamp.gt.nit) then
        call usage('anneal_post: nresamp cannot be greater than nit')
    endif

    write(*,*) 'anneal_post: reading model objective functions for resampling'

    ! Read all objectives (log-posterior-probabilities)
    i = 0
    do
        read(lu_log_file,'(A)',iostat=ios) input_line
        if (ios.ne.0) then
            exit
        endif
        if (index(input_line,'>').eq.1) then
            if (i.ge.1) then
                call parse_iteration_header(input_line,it,ddum,obj(i),ddum,ldum,ios)
            endif
            i = i + 1
        endif
    enddo

    ! Set nit to number read in previous block
    if (i-1.ne.nit) then
{'fraction_non_alphanumeric': 0.16063416093329344, 'fraction_numerical': 0.014358360753813939, 'mean_word_length': 2.4046164290563476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7088012', 'n_tokens_mistral': 3220, 'n_tokens_neox': 2967, 'n_words': 769}