Skip to content

Commit

Permalink
allow comment line to be overrriden so that plain xyz files can be read
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskermode committed Sep 20, 2023
1 parent 896ee96 commit 6d603a3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
13 changes: 10 additions & 3 deletions libextxyz/extxyz.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ char *read_line(char **line, unsigned long *line_len, FILE *fp) {
return *line;
}

int extxyz_read_ll(cleri_grammar_t *kv_grammar, FILE *fp, int *nat, DictEntry **info, DictEntry **arrays) {
char *line;
int extxyz_read_ll(cleri_grammar_t *kv_grammar, FILE *fp, int *nat, DictEntry **info, DictEntry **arrays, char *comment) {
char *line, *temp_line;
unsigned long line_len;
unsigned long line_len_init = 1024;

Expand Down Expand Up @@ -603,8 +603,15 @@ int extxyz_read_ll(cleri_grammar_t *kv_grammar, FILE *fp, int *nat, DictEntry **
free(line);
return 0;
}
// actually parse
// actually parse - optionally replace line read from file with `comment` argument
if (comment != NULL) {
temp_line = line;
line = comment;
}
cleri_parse_t * tree = cleri_parse(kv_grammar, line);
if (comment != NULL) {
line = temp_line;
}
if (! tree->is_valid) {
fprintf(stderr, "Failed to parse string at pos %zd\n", tree->pos);
cleri_parse_free(tree);
Expand Down
7 changes: 4 additions & 3 deletions libextxyz/extxyz.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* interface for the the low-level C wrapper around libcleri parsed extxyz comment lines.
int extxyz_read_ll(kv_grammar, fp, nat, info, arrays)
int extxyz_read_ll(kv_grammar, fp, nat, info, arrays, comment)
Parameters:
cleri_grammar_t *kv_grammar: grammar, passed in so it does not have to be compiled in every time.
calling routine should compile once and save indefinitely
FILE *fp: file pointer to object to be read from, positioned at start of natoms line
int *nat: storage for number of atoms
DictEntry **info: pointer to allocated storage for info dict, will return pointer to first entry in linked list
DictEntry **arrays: pointer to allocated storage for arrays dict, will return pointer to first entry in linked list
char *comment: NULL or pointer to replacement comment line. Useful if a previous call failed due to parse error.
Returns
int 0 for failure and 1 for success.
Expand Down Expand Up @@ -67,6 +68,6 @@ typedef struct dict_entry_struct {

void print_dict(DictEntry *dict);
void free_dict(DictEntry *dict);
int extxyz_read_ll(cleri_grammar_t *kv_grammar, FILE *fp, int *nat, DictEntry **info, DictEntry **arrays);
int extxyz_read_ll(cleri_grammar_t *kv_grammar, FILE *fp, int *nat, DictEntry **info, DictEntry **arrays, char *comment);
int extxyz_write_ll(FILE *fp, int nat, DictEntry *info, DictEntry *arrays);
void* extxyz_malloc(size_t nbytes);
void* extxyz_malloc(size_t nbytes);
17 changes: 11 additions & 6 deletions python/extxyz/cextxyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class Dict_entry_struct(ctypes.Structure):
extxyz.extxyz_read_ll.args = [ctypes.c_void_p, ctypes.c_void_p,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(Dict_entry_ptr),
ctypes.POINTER(Dict_entry_ptr)]
ctypes.POINTER(Dict_entry_ptr),
ctypes.c_void_p]

extxyz.extxyz_write_ll.args = [ctypes.c_void_p, ctypes.c_int, Dict_entry_ptr, Dict_entry_ptr]

Expand Down Expand Up @@ -209,12 +210,13 @@ def cfclose(fp):
fclose(fp)


def read_frame_dicts(fp, verbose=False):
def read_frame_dicts(fp, verbose=False, comment=None):
"""Read a single frame using extxyz_read_ll() C function
Args:
fp (FILE_ptr): open file pointer, as returned by `cfopen()`
verbose (bool, optional): Dump C dictionaries to stdout. Defaults to False.
comment (str, optional): Overrride comment line with specified string.
Returns:
nat, info, arrays: int, dict, dict
Expand All @@ -225,11 +227,14 @@ def read_frame_dicts(fp, verbose=False):
eof = False

try:
if comment is not None:
comment = comment.encode('utf-8')
if not extxyz.extxyz_read_ll(_kv_grammar,
fp,
ctypes.byref(nat),
ctypes.byref(info),
ctypes.byref(arrays)):
fp,
ctypes.byref(nat),
ctypes.byref(info),
ctypes.byref(arrays),
comment):
eof = True
raise EOFError()

Expand Down
4 changes: 3 additions & 1 deletion python/extxyz/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def main():
parser.add_argument('-R', '--round-trip', action='store_true')
parser.add_argument('-P', '--profile', action='store_true')
parser.add_argument('-C', '--cextxyz', action='store_true')
parser.add_argument('--comment', action='store', default=None)
args = parser.parse_args()
if args.round_trip:
args.write = True # -R implies -w too
Expand All @@ -37,7 +38,8 @@ def main():
use_regex=args.regex,
create_calc=args.create_calc,
calc_prefix=args.calc_prefix,
use_cextxyz=args.cextxyz)
use_cextxyz=args.cextxyz,
comment=args.comment)
tr = time.time() - t0
if args.verbose:
print("main output of read()")
Expand Down
8 changes: 5 additions & 3 deletions python/extxyz/extxyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,22 +584,24 @@ def read_frame_dicts(file, verbose=0, use_regex=True):


def read_frame(file, verbose=0, use_cextxyz=True,
use_regex=True, create_calc=False, calc_prefix=''):
use_regex=True, create_calc=False, calc_prefix='',
comment=None):
"""
Read a single frame in extxyz format from `file`.
"""

try:
if use_cextxyz:
natoms, info, arrays = cextxyz.read_frame_dicts(file, verbose=verbose)
natoms, info, arrays = cextxyz.read_frame_dicts(file, verbose=verbose,
comment=comment)
properties = info.pop('Properties', 'species:S:1:pos:R:3')
properties = Properties(property_string=properties)
data = np.zeros(natoms, properties.dtype_vector)
for name, value in arrays.items():
data[name] = value
else:
natoms, info, data, properties = read_frame_dicts(file, verbose=verbose,
use_regex=use_regex)
use_regex=use_regex)
except EOFError:
return None

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_ext_filename(self, ext_name):

setup(
name='extxyz',
version='0.1.3',
version='0.1.4',
author='various',
packages=['extxyz'],
package_dir={'': 'python'},
Expand Down

0 comments on commit 6d603a3

Please sign in to comment.