import os
import numpy as np
import argparse

def Gene_Grid(rad, div):
    L_max = int(np.round(rad / div))
    L_min = -1 * L_max
    grid = []
    crd = []

    for i in range(L_min, L_max + 1):
        x = i * div
        for j in range(L_min, L_max + 1):
            y = j * div
            for k in range(L_min, L_max + 1):
                z = k * div
                r = np.sqrt(x * x + y * y + z * z)
                if r <= rad:
                    grid.append(i)
                    grid.append(j)
                    grid.append(k)
                    crd.append(x)
                    crd.append(y)
                    crd.append(z)

    gridn = np.array(grid).reshape(-1, 3)
    crdn = np.array(crd).reshape(-1, 3)
    return gridn, crdn

def Write_Grid(grid, crd, out_file):
    with open(out_file, "w") as fw:
        ln = grid.shape[0]
        for i in range(ln):
            fw.write(
                "%3d %3d %3d %7.3f %7.3f %7.3f\n"
                % (
                    grid[i][0], grid[i][1], grid[i][2],
                    crd[i][0], crd[i][1], crd[i][2]
                )
            )

def main():
    parser = argparse.ArgumentParser(description="Generate spherical grid points")
    parser.add_argument("--rad", type=float, default=7.0, help="Radius (default: 7.0)")
    parser.add_argument("--div", type=float, default=0.375, help="Grid spacing (default: 0.375)")
    parser.add_argument("--out", default="Grid", help="Output file name (default: Grid)")
    args = parser.parse_args()

    grid, crd = Gene_Grid(args.rad, args.div)
    Write_Grid(grid, crd, args.out)

if __name__ == "__main__":
    main()

