#  Copyright (C) 2023 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

__author__ = 'You Lu <you.lu@stfc.ac.uk>'

# see: https://www.ks.uiuc.edu/Research/namd/2.10/ug/node11.html
def parse(filename, _regex=None, receiver=None, nrecords_only=False, prefix='', **kwargs):
    '''Parse an NAMD coordinates (.coor) file'''

    from numpy import fromfile, int32, float64
    from os    import path

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

    with open(filename, 'rb') as fp:

        # natoms
        natoms = fromfile(fp, dtype=int32, count=1)[0]
        receiver.update({'natoms':natoms})
        if chemsh.CHEMSH_DEBUG > 4:
            print("\n ChemShell NAMD binary .coor parser: natoms =", natoms)

        coords = fromfile(fp, dtype=float64, count=natoms*3).reshape((natoms,3))
        receiver.update({'coords':coords})
    
    return 0


