Created
November 3, 2018 12:16
-
-
Save hovedguy/5d3afbca47b9319ece6010d7d8fefb6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import time | |
| start_time = time.time() | |
| full_path = r"C:\Temp\Example_3\Job-1.inp" # inp full file path | |
| with open(full_path, "r") as f: | |
| f_data = f.read() | |
| file_list_ = f_data.split("\n") # creates list of all lines | |
| # OP: ['*Heading', '** Job name: Job-1 Model name: Model-1', ....] | |
| def nodeParser(file_list): | |
| ''' creates pandas data-frame in the form of - | |
| part_id, node_id, node_x, node_y, node_z as | |
| int , int , float , float , float''' | |
| node_start = [] | |
| node_end = [] | |
| node_flag = False | |
| for x in range(len(file_list)): | |
| if "*Part," in file_list[x - 2] and "*Node" in file_list[x - 1]: | |
| node_start.append(x) | |
| node_flag = True | |
| if node_flag and "*" in file_list[x]: | |
| node_end.append(x - 1) | |
| node_flag = False | |
| node_band = list(zip(node_start, node_end)) # creates a list of tuples containing start and end of line nums containing node def | |
| # OP: [(9, 104), (192, 202)] | |
| node_band_dict = {} # creates a dict of part id vs tuple containing same info node_band | |
| # OP: {1: (9, 104), 2: (192, 202)} | |
| for x in range(len(node_band)): | |
| node_band_dict[x + 1] = node_band[x] | |
| d_cont = [] # node data content as list, all elements are stored as string | |
| # OP: [['*Heading'], ['** Job name: Job-1 Model name: Model-1'], ...] | |
| for i in range(len(file_list)): | |
| b = file_list[i].split(",") | |
| final = list(b[x].strip() for x in range(len(b))) | |
| d_cont.append(final) | |
| df = pd.DataFrame(columns = ["part_id", "node_id", "node_x", "node_y", "node_z"]) | |
| for key, val in node_band_dict.items(): | |
| df_local = pd.DataFrame(columns = ["node_id", "node_x", "node_y", "node_z"]) | |
| for line in range(len(d_cont[val[0]:val[1]])): | |
| df_local.loc[line] = d_cont[val[0]:val[1]][line] | |
| df_local["part_id"] = list(key for _ in range(len(df_local.node_id))) | |
| column_list = df_local.columns.tolist() | |
| df_local = df_local[ [column_list[-1]] + column_list[:-1] ] | |
| df = df.append(df_local, ignore_index=True) | |
| # to change data type | |
| coords = df.columns.drop("part_id", "node_id") | |
| df[coords] = df[coords].apply(pd.to_numeric, errors='coerce') # creates float | |
| df.node_id = df.node_id.astype(int) # creates integer | |
| df.part_id = df.part_id.astype(int) | |
| return df | |
| def main(): | |
| node_df = nodeParser(file_list_) | |
| print(node_df.head()) | |
| delta_time = time.time() - start_time | |
| print(f"JOB'S COMPLETED, TIME TAKEN: {delta_time} sec") | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment