#  Copyright (C) 2017 The authors of Py-ChemShell
#
#  This file is part of Py-ChemShell.
#
#  Py-ChemShell is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  Py-ChemShell is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with Py-ChemShell.  If not, see
#  <http://www.gnu.org/licenses/>.

import chemsh
from ...dl_py2f import utils

def parse(filename, _regex=None, receiver=None, prefix='', **kwargs):
    '''NWChem output for BQ gradients'''

    from numpy import append, zeros
    from os    import path

    # YL 13/08/2022: path prefix
    filename = path.join(prefix, filename)

    with open(filename) as fp:

        # array buffer
        abuff = zeros((0,))

        while abuff.size < receiver.size:
            abuff = append(abuff, utils.strutils.str2num(next(fp)))

        utils.nputils.setField(receiver, abuff, debug=chemsh.CHEMSH_DEBUG)

    return receiver
