! histroy read
module histroy_parameters
    implicit none
    INTEGER frcdiag,ndstep,nfield,modes,istep,realstep
    REAL tstep_diag
    REAL(kind=8), DIMENSION(:), ALLOCATABLE:: phi_hist, phi_real_hist,phi_imag_hist,&
    densitye, sdeltne, flowe, suepara, phi_rms, phi0_rms
    real(kind=8), DIMENSION(:,:), ALLOCATABLE:: fieldmode

    CHARACTER(LEN=999) fname
!f2py intent(in) fname


contains
subroutine filename(name)
    CHARACTER(*) name
    fname=trim(name)
end subroutine filename

subroutine dealloc
    DEALLOCATE(phi_hist, phi_real_hist, phi_imag_hist, densitye, sdeltne, fieldmode, flowe, suepara, phi_rms, phi0_rms)
end subroutine dealloc

subroutine history_read
    nfield=1
    modes=1
    frcdiag=654
    realstep=0
    open(frcdiag,file=fname,status='old')
    read(frcdiag,101)ndstep
    read(frcdiag,102)tstep_diag
    ALLOCATE(phi_hist(ndstep), phi_real_hist(ndstep), phi_imag_hist(ndstep), densitye(ndstep),&
    sdeltne(ndstep),fieldmode(2,ndstep),flowe(ndstep),suepara(ndstep), phi_rms(ndstep), phi0_rms(ndstep))

    do istep=1,ndstep
        read(frcdiag, 102, end=111) phi_hist(istep)
        read(frcdiag, 102, end=111) phi_real_hist(istep)
        read(frcdiag, 102, end=111) phi_imag_hist(istep)
        read(frcdiag, 102, end=111) fieldmode(1,istep), fieldmode(2,istep)
        read(frcdiag, 102, end=111) densitye(istep)
        read(frcdiag, 102, end=111) sdeltne(istep)
        read(frcdiag, 102, end=111) flowe(istep)
        read(frcdiag, 102, end=111) suepara(istep)
        read(frcdiag, 102, end=111) phi_rms(istep)
        read(frcdiag, 102, end=111) phi0_rms(istep)
        realstep=realstep+1
    enddo
    111 close(frcdiag)
    101 format(i6)
    102 format(e13.6)
end subroutine history_read
end module histroy_parameters
