subroutine dealloc_rdm_estimates_t(est)
! Initialise an rdm_estimates_t object. Allocate arrays to be large
! enough to hold estimates for nrdms RDMs.
! Also, if open_output_file is true, and if this is the processor with
! label 0, then open an RDMEstimates file, and write the file's header.
use Parallel_neci, only: iProcIndex
use rdm_data, only: rdm_estimates_t
use util_mod, only: get_free_unit
type(rdm_estimates_t), intent(inout) :: est
integer :: ierr
if (allocated(est%trace)) deallocate(est%trace, stat=ierr)
if (allocated(est%norm)) deallocate(est%norm, stat=ierr)
if (allocated(est%energy_1_num)) deallocate(est%energy_1_num, stat=ierr)
if (allocated(est%energy_2_num)) deallocate(est%energy_2_num, stat=ierr)
if (allocated(est%energy_num)) deallocate(est%energy_num, stat=ierr)
if (allocated(est%spin_num)) deallocate(est%spin_num, stat=ierr)
if (allocated(est%property)) deallocate(est%property, stat=ierr)
if (allocated(est%trace_inst)) deallocate(est%trace_inst, stat=ierr)
if (allocated(est%norm_inst)) deallocate(est%norm_inst, stat=ierr)
if (allocated(est%energy_1_num_inst)) deallocate(est%energy_1_num_inst, stat=ierr)
if (allocated(est%energy_2_num_inst)) deallocate(est%energy_2_num_inst, stat=ierr)
if (allocated(est%energy_num_inst)) deallocate(est%energy_num_inst, stat=ierr)
if (allocated(est%spin_num_inst)) deallocate(est%spin_num_inst, stat=ierr)
if (allocated(est%property_inst)) deallocate(est%property_inst, stat=ierr)
if (allocated(est%max_error_herm)) deallocate(est%max_error_herm, stat=ierr)
if (allocated(est%sum_error_herm)) deallocate(est%sum_error_herm, stat=ierr)
if (allocated(est%energy_pert)) deallocate(est%energy_pert, stat=ierr)
if (allocated(est%energy_pert_inst)) deallocate(est%energy_pert_inst, stat=ierr)
! Close the RDMEstimates unit, if it was opened on this processor.
! The following was what it was set to if it was not opened in
! init_rdm_estimates_t, so don't attempt a close in that case.
if (est%write_unit /= huge(est%write_unit)) close(est%write_unit)
end subroutine dealloc_rdm_estimates_t