MODULE INIT_VARS
    use READ_DATA
    IMPLICIT NONE
    REAL*8 :: energy_exp
    INTEGER*2 :: rnd_spin,rnd_layer
    REAL :: rnd_accept,time_start
    INTEGER :: index_MC,index_MC_step
    INTEGER*1 :: index_AB,index_PT
    !WINDOWS
    INTEGER :: n_windows,index_window
    INTEGER*8 :: window_measures_q
    REAL*8, DIMENSION(:,:), ALLOCATABLE :: measure_windows_e,measure_windows_q,measure_windows_q2,measure_windows_q4,measure_windows_sp
    INTEGER, DIMENSION(:), ALLOCATABLE :: windows_limits
    REAL*8 :: q_aux

    CONTAINS
    SUBROUTINE DIMENSION_VARS()
        IMPLICIT NONE
        INTEGER :: i,j,num
        ALLOCATE(spin_array(n_spins,n_pt,2))
        ALLOCATE(g(n_pt))
        ALLOCATE(e_intra(n_pt,2),number_neighbours(n_spins))
        ALLOCATE(q_overlap(n_pt),q_l_overlap(n_pt))
        ALLOCATE(pntr(n_pt,2))
        ALLOCATE(histo_q(-n_spins:n_spins,n_pt))
        !Initialize pointer, gamma array and K1 K2
        DO i=0,n_pt-1
            g(i+1)=low_gamma+ dfloat(i)*((grt_gamma-low_gamma)/dfloat(n_pt-1))
            pntr(i+1,:)=i+1
        ENDDO
        !Conigurin the windows
        IF (MC_factor==1)THEN
            n_windows=3+2*(MC_steps_expoent-2)
        elseif(MC_factor==3)THEN
            n_windows=3+2*(MC_steps_expoent-2)+1
        else
            print*,'bad input for MC factor, 1 or 3'
            stop
        endif
        ALLOCATE(measure_windows_q(n_pt,n_windows), measure_windows_e(n_pt,n_windows), windows_limits(n_windows))
        ALLOCATE(measure_windows_q2(n_pt,n_windows),measure_windows_q4(n_pt,n_windows), measure_windows_sp(n_pt,n_windows))
        j=1
        num=10
        DO i=1,n_windows
            IF(j.le.n_windows)THEN
                windows_limits(j)=num
                j=j+1
                num=num*3
                IF(j.le.n_windows)THEN
                    windows_limits(j)=num
                    j=j+1
                    num=int(real(10*num)/real(3))
                END IF
            END IF
        END DO
        DO i=n_windows,2,-1
            windows_limits(i)=windows_limits(i)-windows_limits(i-1)
        END DO
        window_measures_q=int(0,8)
        measure_windows_e=0d0
        measure_windows_q=0d0
        measure_windows_q2=0d0
        measure_windows_q4=0d0
        measure_windows_sp=0d0


    END SUBROUTINE DIMENSION_VARS
    SUBROUTINE RANDOM_SPIN_ARRAY() !UPDATED
        IMPLICIT NONE
        INTEGER i,j,z
        REAL aux_rand_number,r1279
            DO i=1,n_pt
                DO j=1,n_spins
                        DO z=1,2
                            aux_rand_number=r1279()
                            IF(aux_rand_number.ge.0.5) THEN
                                spin_array(j,i,z)=1
                            ELSE
                                spin_array(j,i,z)=-1
                            END IF
                        END DO
                END DO
            END DO
    END SUBROUTINE
END MODULE INIT_VARS
