Ben
Ben

Reputation: 342

python loop through files

I have a python code that is pasted below. It works fine for what I need to do. You can notice that I load in a single dump file. How can I loop through all dump files that have the same ending pattern of *.dump and have each new file just add a new column of data to the output file? Essentially I want to add a loop so I do not have to manually re-write the code for each dump file.

from ovito.io import *
from ovito.data import *
from ovito.modifiers import *
import numpy as np

node = import_file("../200eV.dump",multiple_frames = True)

# Perform Wigner-Seitz analysis:
ws = WignerSeitzAnalysisModifier(
    per_type_occupancies = True, 
    eliminate_cell_deformation = True)
ws.reference.load("../../../WS_Ref/ws.dump")
node.modifiers.append(ws)

# Define a modifier function that selects sites of type A=1 which
# are occupied by exactly one atom of type B=2.
def modify(frame, input, output):

    # Retrieve the two-dimensional Numpy array with the site occupancy numbers.
    occupancies = input.particle_properties['Occupancy'].array

    # Get the site types as additional input:
    site_type = input.particle_properties.particle_type.array

    # Calculate total occupancy of every site:
    total_occupancy = np.sum(occupancies, axis=1)

    # Set up a particle selection by creating the Selection property:

    selection1 = (site_type == 1) & (occupancies[:,0] == 0) & (occupancies[:,1] == 0)

    output.attributes['Ca_Vac'] = np.count_nonzero(selection1)


# Insert Python modifier into the data pipeline.
node.modifiers.append(PythonScriptModifier(function = modify))

# Let OVITO do the computation and export the number of identified 
# antisites as a function of simulation time to a text file:
export_file(node, "defects_200.txt", "txt", 
    columns = ['Timestep', 'Ca_Vac'],
    multiple_frames = True)

Upvotes: 0

Views: 311

Answers (2)

Işık Kaplan
Işık Kaplan

Reputation: 3002

import numpy as np
from ovito.data import *
from ovito.io import *
from ovito.modifiers import *

ws = WignerSeitzAnalysisModifier(
    per_type_occupancies=True,
    eliminate_cell_deformation=True)
ws.reference.load("../../../WS_Ref/ws.dump")


def modify(frame, input, output):
    occupancies = input.particle_properties['Occupancy'].array

    site_type = input.particle_properties.particle_type.array

    total_occupancy = np.sum(occupancies, axis=1)  # you are also not using, also not using the frame parameter

    selection1 = (site_type == 1) & (occupancies[:, 0] == 0) & (occupancies[:, 1] == 0)

    output.attributes['Ca_Vac'] = np.count_nonzero(selection1)


import glob

for file in glob.glob('../*.glob'):
    node = import_file(file, multiple_frames=True)
    node.modifiers.append(ws)
    node.modifiers.append(PythonScriptModifier(function=modify))
    export_file(
        node, "defects_200.txt", "txt",
        columns=['Timestep', 'Ca_Vac'],
        multiple_frames=True
    )

Without knowing more this is the best I could come up with, I hope it works!

Adding this part as per the OP's request.

for index, file in enumerate(glob.glob('../*.glob')):
    node = import_file(file, multiple_frames=True)
    node.modifiers.append(ws)
    node.modifiers.append(PythonScriptModifier(function=modify))
    export_file(
        node, "defects_{}.txt".format(index), "txt",
        columns=['Timestep', 'Ca_Vac'],
        multiple_frames=True
    )

And again, this is a guess on how the library works and would work only if the original code produces defetcs_200.txt as the result.

Upvotes: 1

Dharmishtha Ghoghra
Dharmishtha Ghoghra

Reputation: 49

Try python glob package.

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']

Upvotes: 0

Related Questions