module grids_parameters
    logical :: allocated = .false.
    integer :: numerfrc, mpsi_core, mpara_core1, mgrid_core, mpsi_sol,&
        mpara_sol1, mgrid_sol, mpara_sep_sol, mpara_share,i, mpsi, mgrid

    real(kind=8), DIMENSION(:), ALLOCATABLE :: Rcore, Zcore, Score, Thetacore, &
        Rsol, Zsol, Ssol, Rsol_sep_irreg, Zsol_sep_irreg, Ssol_sep_irreg, &
        Rsol_share, Zsol_share, Ssol_share, Rcore_share, Zcore_share, Score_share, &
        dcore_orthogonal, dsol_orthogonal, Rsol_sep_reg, Zsol_sep_reg, Ssol_sep_reg

    real(kind=8), DIMENSION(:), ALLOCATABLE :: rg1d_core, zg1d_core, psimesh_core, &
        rg1d_sol, zg1d_sol, psimesh_sol, totals_core, deltas_core1d, totals_sol, &
        deltas_sol, r_psi_theta, z_psi_theta, r_psi_s, z_psi_s, rg1d

    CHARACTER(LEN=999) fname
!f2py intent(in) fname
contains
subroutine filename(name)
    CHARACTER(*) name
    fname=trim(name)
end subroutine filename

subroutine deallocate
    implicit none
    allocated = .false.
    if (numerfrc==1) then
    deallocate(Rcore, Zcore, Score, Thetacore, Rsol, Zsol, Ssol, Rsol_sep_irreg, Zsol_sep_irreg,&
    Ssol_sep_irreg, Rsol_share, Zsol_share, Ssol_share, Rcore_share, Zcore_share, Score_share, dcore_orthogonal, dsol_orthogonal,&
    rg1d_core, zg1d_core, psimesh_core, rg1d_sol, zg1d_sol, psimesh_sol,totals_core, deltas_core1d, totals_sol, deltas_sol,&
    r_psi_s,z_psi_s, r_psi_theta,z_psi_theta, Rsol_sep_reg, Zsol_sep_reg, Ssol_sep_reg)
    elseif(numerfrc==3)then
        deallocate(Rsol, Zsol, Ssol,&
        rg1d_sol,zg1d_sol,psimesh_sol,&
        totals_sol,deltas_sol)
    endif
    deallocate(rg1d)
end subroutine deallocate

subroutine grids_read
    implicit none
    integer igrid
    igrid=100
    allocated = .true.
    open(igrid,file=fname,status='old')
    if(numerfrc==1)then
        mpsi = mpsi_core + mpsi_sol
        read(igrid,101)mpsi_core,mpara_core1,mgrid_core, &
                        mpsi_sol,mpara_sol1,mgrid_sol, &
                        mpara_sep_sol,mpara_share


        allocate(Rcore(mgrid_core), Zcore(mgrid_core), Score(mgrid_core),&
            Thetacore(mgrid_core), Rsol(mgrid_sol), Zsol(mgrid_sol), Ssol(mgrid_sol),&
            Rsol_sep_irreg(mpara_sep_sol), Zsol_sep_irreg(mpara_sep_sol), &
            Rsol_sep_reg(mpara_sep_sol), Zsol_sep_reg(mpara_sep_sol), Ssol_sep_reg(mpara_sep_sol),&
            Ssol_sep_irreg(mpara_sep_sol),Rsol_share(mpara_share), Zsol_share(mpara_share),&
            Ssol_share(mpara_share), Rcore_share(mpara_share), Zcore_share(mpara_share),&
            Score_share(mpara_share), dcore_orthogonal(mpara_share), dsol_orthogonal(mpara_share))

        allocate(rg1d_core(mpsi_core), zg1d_core(mpsi_core), psimesh_core(mpsi_core),&
            rg1d_sol(mpsi_sol), zg1d_sol(mpsi_sol), psimesh_sol(mpsi_sol))

        allocate(totals_core(mpsi_core), deltas_core1d(mpsi_core), totals_sol(mpsi_sol),&
            deltas_sol(mgrid_sol))

        allocate(r_psi_theta(mgrid_core), z_psi_theta(mgrid_core), r_psi_s(mgrid_core),&
            z_psi_s(mgrid_core))
        read(igrid,102)Rcore, Zcore, Score, Thetacore, &
                        Rsol, Zsol, Ssol, &
                        Rsol_sep_irreg, Zsol_sep_irreg, Ssol_sep_irreg, &
                        Rsol_sep_reg, Zsol_sep_reg, Ssol_sep_reg, &
                        Rsol_share, Zsol_share, Ssol_share, &
                        Rcore_share, Zcore_share, Score_share, &
                        dcore_orthogonal, dsol_orthogonal

        read(igrid,102)rg1d_core, zg1d_core, psimesh_core, &
                        rg1d_sol, zg1d_sol, psimesh_sol
        read(igrid,102)totals_core,deltas_core1d, &
                        totals_sol,deltas_sol

        read(igrid,102)r_psi_theta,z_psi_theta,r_psi_s,z_psi_s
    elseif(numerfrc==3)then
        mpsi_core=0
        read(igrid,101)mpsi_sol,mpara_sol1,mgrid_sol
        mgrid=mgrid_sol
        mpsi = mpsi_sol
        allocate(Rsol(mgrid_sol), Zsol(mgrid_sol), Ssol(mgrid_sol),&
        rg1d_sol(mpsi_sol),zg1d_sol(mpsi_sol),psimesh_sol(mpsi_sol),&
        totals_sol(mpsi_sol),deltas_sol(mpsi_sol))
        read(igrid,102)Rsol, Zsol, Ssol
        read(igrid,102)rg1d_sol, zg1d_sol, psimesh_sol
        read(igrid,102)totals_sol,deltas_sol
    else
        write(*,*) 'Warning: in grids_read, numerfrc/=1 not supported, exiting...'
    endif
    allocate(rg1d(mpsi))
    do i=1,mpsi_core
        rg1d(i) = rg1d_core(i)
    enddo
    do i=1,mpsi_sol
        rg1d(i+mpsi_core) = rg1d_sol(i)
    enddo
    close(igrid)

101 format(i6)
102 format(e13.6)
end subroutine grids_read
end module grids_parameters
