module grids_eq_parameters
    implicit none
    integer::mpsi_sol, mpara_sol, mgrid_sol,igrid_eq, mgrid, numerfrc, mpara_core,&
        mpsi_core,mgrid_core, mpsi,  i
    real(kind=8), dimension(:),allocatable :: psimesh_sol, psimesh_core, kapanip, kapatip, kapanep, kapatep,&
    psiEq0, dpsidr, dpsidz, bmeshr_unit, bmeshz_unit, bmesh,&
    grad_b_scale, curl_b_scale, curl_Bmag_scale,&
    meshni, meshti, meshne, meshte,&
    kapanir, kapaniz, kapatir, kapatiz,&
    kapaner, kapanez, kapater, kapatez,&
    bmeshr_bsp, bmeshz_bsp, bmesh_bsp,&
    db0mag_dr_bsp, db0mag_dz_bsp,psimesh,&
    db0r_dr_bsp, db0r_dz_bsp, db0z_dr_bsp, db0z_dz_bsp
    CHARACTER(LEN=999) fname
!f2py intent(in) fname

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

subroutine deallocate
    if(numerfrc==3)then
    deallocate(kapanip, psimesh_sol, kapatip,&
    kapanep, kapatep,psiEq0,dpsidr,dpsidz,&
    bmeshr_unit, bmeshz_unit, bmesh,&
    grad_b_scale,curl_b_scale, curl_Bmag_scale,&
    kapanir,kapaniz,kapatir,kapatiz,&
    kapaner,kapanez,kapater,kapatez,&
    meshni,meshne,meshti,meshte)
    elseif(numerfrc==1)then
        deallocate(psimesh_core, psimesh_sol)
        deallocate(meshni,meshti,meshte,meshne)
        deallocate(psiEq0,bmeshr_unit,bmeshz_unit,bmesh,&
               dpsidr,dpsidz)
        deallocate(kapanip,kapanep,kapatip,kapatep)
        deallocate(kapanir,kapaniz,kapatir,kapatiz,&
            kapaner,kapanez,kapater,kapatez)
        deallocate(bmesh_bsp,bmeshr_bsp,bmeshz_bsp,&
            db0mag_dr_bsp,db0mag_dz_bsp,&
            db0r_dr_bsp,db0r_dz_bsp,&
            db0z_dr_bsp,db0z_dz_bsp)
        deallocate(grad_b_scale,curl_b_scale, curl_Bmag_scale)
    elseif(numerfrc==2)then
        deallocate(kapanip, psimesh_core, kapatip,&
        kapanep, kapatep,psiEq0,dpsidr,dpsidz,&
        bmeshr_unit, bmeshz_unit, bmesh,&
        grad_b_scale,curl_b_scale, curl_Bmag_scale,&
        kapanir,kapaniz,kapatir,kapatiz,&
        kapaner,kapanez,kapater,kapatez,&
        meshni,meshne,meshti,meshte)
    endif
    deallocate(psimesh)
end subroutine deallocate

subroutine read
    implicit none
    igrid_eq = 100
    open(igrid_eq, file=fname, status='old')
    if(numerfrc==3 .or. numerfrc==4)then
        read(igrid_eq,101)mpsi_sol,mpara_sol,mgrid_sol
        mpsi = mpsi_sol
        mgrid = mgrid_sol
        allocate(psimesh_sol(mpsi_sol), kapanip(mpsi_sol), kapatip(mpsi_sol),&
        kapanep(mpsi_sol), kapatep(mpsi_sol),psiEq0(mgrid),dpsidr(mgrid),dpsidz(mgrid),&
        bmeshr_unit(mgrid), bmeshz_unit(mgrid), bmesh(mgrid),&
        grad_b_scale(mgrid),curl_b_scale(mgrid), curl_Bmag_scale(mgrid),&
        kapanir(mgrid),kapaniz(mgrid),kapatir(mgrid),kapatiz(mgrid),&
        kapaner(mgrid),kapanez(mgrid),kapater(mgrid),kapatez(mgrid),&
        meshni(mgrid),meshne(mgrid),meshti(mgrid),meshte(mgrid))
        read(igrid_eq,102)psimesh_sol
        read(igrid_eq,102)kapanip,kapatip,kapanep,kapatep
        read(igrid_eq,102)psiEq0, dpsidr, dpsidz
        read(igrid_eq,102)bmeshr_unit, bmeshz_unit, bmesh
        read(igrid_eq,102)grad_b_scale, curl_b_scale, curl_Bmag_scale
        read(igrid_eq,102)meshni, meshti, meshne, meshte
        read(igrid_eq,102)kapanir, kapaniz, kapatir, kapatiz
        read(igrid_eq,102)kapaner, kapanez, kapater, kapatez

    elseif(numerfrc==1)then
        read(igrid_eq,101)mpsi_core,mpara_core,mgrid_core
        read(igrid_eq,101)mpsi_sol,mpara_sol,mgrid_sol
        mpsi = mpsi_core + mpsi_sol
        mgrid = mgrid_core + mgrid_sol
        allocate(psimesh_core(mpsi_core), psimesh_sol(mpsi_sol))
        allocate(meshni(mgrid),meshti(mgrid),meshte(mgrid),meshne(mgrid))
        allocate(psiEq0(mgrid),bmeshr_unit(mgrid),bmeshz_unit(mgrid),bmesh(mgrid),&
               dpsidr(mgrid),dpsidz(mgrid))
        allocate(kapanip(mpsi),kapanep(mpsi),kapatip(mpsi),kapatep(mpsi))
        allocate(kapanir(mgrid),kapaniz(mgrid),kapatir(mgrid),kapatiz(mgrid),&
            kapaner(mgrid),kapanez(mgrid),kapater(mgrid),kapatez(mgrid))
        allocate(bmesh_bsp(mgrid),bmeshr_bsp(mgrid),bmeshz_bsp(mgrid),&
            db0mag_dr_bsp(mgrid),db0mag_dz_bsp(mgrid),&
            db0r_dr_bsp(mgrid),db0r_dz_bsp(mgrid),&
            db0z_dr_bsp(mgrid),db0z_dz_bsp(mgrid))
        allocate(grad_b_scale(mgrid),curl_b_scale(mgrid), curl_Bmag_scale(mgrid))
        read(igrid_eq,102)psimesh_core,psimesh_sol
        read(igrid_eq,102)kapanip,kapatip,kapanep,kapatep

        read(igrid_eq,102)psiEq0, dpsidr, dpsidz
        read(igrid_eq,102)bmeshr_unit, bmeshz_unit, bmesh

        read(igrid_eq,102)bmeshr_bsp, bmeshz_bsp, bmesh_bsp
        read(igrid_eq,102)db0mag_dr_bsp, db0mag_dz_bsp
        read(igrid_eq,102)db0r_dr_bsp, db0r_dz_bsp, db0z_dr_bsp, db0z_dz_bsp

        read(igrid_eq,102)grad_b_scale, curl_b_scale, curl_Bmag_scale
        read(igrid_eq,102)meshni, meshti, meshne, meshte
        read(igrid_eq,102)kapanir, kapaniz, kapatir, kapatiz
        read(igrid_eq,102)kapaner, kapanez, kapater, kapatez
    elseif(numerfrc==2)then
        read(igrid_eq,101)mpsi_core,mpara_core,mgrid_core
        mpsi = mpsi_core
        mgrid = mgrid_core
        allocate(psimesh_core(mpsi), kapanip(mpsi), kapatip(mpsi),&
        kapanep(mpsi), kapatep(mpsi),psiEq0(mgrid),dpsidr(mgrid),dpsidz(mgrid),&
        bmeshr_unit(mgrid), bmeshz_unit(mgrid), bmesh(mgrid),&
        grad_b_scale(mgrid),curl_b_scale(mgrid), curl_Bmag_scale(mgrid),&
        kapanir(mgrid),kapaniz(mgrid),kapatir(mgrid),kapatiz(mgrid),&
        kapaner(mgrid),kapanez(mgrid),kapater(mgrid),kapatez(mgrid),&
        meshni(mgrid),meshne(mgrid),meshti(mgrid),meshte(mgrid))

        read(igrid_eq,102)psimesh_core
        read(igrid_eq,102)kapanip,kapatip,kapanep,kapatep

        read(igrid_eq,102)psiEq0, dpsidr, dpsidz
        read(igrid_eq,102)bmeshr_unit, bmeshz_unit, bmesh
        read(igrid_eq,102)grad_b_scale, curl_b_scale, curl_Bmag_scale
        read(igrid_eq,102)meshni, meshti, meshne, meshte
        read(igrid_eq,102)kapanir, kapaniz, kapatir, kapatiz
        read(igrid_eq,102)kapaner, kapanez, kapater, kapatez
    endif
    close(igrid_eq)
    allocate(psimesh(mpsi))

    do i=1,mpsi_core
        psimesh(i)=psimesh_core(i)
    enddo
    do i=1,mpsi_sol
        psimesh(i+mpsi_core) = psimesh_sol(i)
    enddo
    
    101 format(i6)
    102 format(e13.6)
end subroutine read
end module grids_eq_parameters
