Convert Greg's Table to DSEP Format Script

This is a Python script designed to convert surface boundary condition tables, such as those provided by Greg, into a format that is compatible with the Dartmouth Stellar Evolution Program (DSEP). It includes functionality to read the initial table file, remove comments, manipulate the data into the required DSEP format, and write the new table to a file. The script supports command-line arguments for specifying the input and output files, making it flexible for different users or datasets.

# Convert greg's surface boundary condition tables to a form of table
# DSEP can accept
import numpy as np
import argparse
import re
from io import StringIO
 
def remove_comments(string):
    commentPattern = r"(?:#)(.*)(?:\n)"
    comments = re.findall(commentPattern, string)
    cleanedString = re.sub(commentPattern, '\n', string)
    return cleanedString, comments
 
def load_new_style(path):
    with open(path) as f:
        contents = f.read()
 
    cleanedContents, comments = remove_comments(contents)
    buffer = StringIO(cleanedContents)
    parray = np.genfromtxt(buffer)
    return parray, comments
 
def convert_2_DSEP_style(arr):
    t = arr[:, 0]
    pgas = arr[:, 1::2]
    dsepForm = np.empty(shape=(t.shape[0], 1+pgas.shape[1]))
    dsepForm[:, 0] = t
    dsepForm[:, 1:] = pgas
    return dsepForm
 
def write_to_file(file, dsepForm, comments):
    headerComment = list(filter(lambda x : bool(re.match(r'\[Z/Z_SOLAR\]', str(x))), comments))
    formatSpec = "%1.5E"
    np.savetxt(file, dsepForm, fmt=formatSpec, header=headerComment[0], comments='#', newline='\n ')
 
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="convert greg table to DSEP table")
    parser.add_argument("ifile", type=str, help="input file")
    parser.add_argument("-o", "--output", type=str, help="output file", default="res.dat")
 
    args = parser.parse_args()
    arr, comments = load_new_style(args.ifile)
    dsepForm = convert_2_DSEP_style(arr)
    write_to_file(args.output, dsepForm, comments)