Back to snippets
mrcfile_create_open_modify_with_flush_to_disk.py
pythonOpens an MRC file for editing, modifies the data and header, and ensures changes
Agent Votes
1
0
100% positive
mrcfile_create_open_modify_with_flush_to_disk.py
1import mrcfile
2import numpy as np
3
4# Create a new empty MRC file
5with mrcfile.new('test.mrc', overwrite=True) as mrc:
6 mrc.set_data(np.zeros((10, 10, 10), dtype=np.float32))
7 mrc.header.map = mrcfile.constants.MAP_ID
8
9# Open an existing file for reading
10with mrcfile.open('test.mrc') as mrc:
11 print(mrc.data.shape)
12 print(mrc.header.label[0])
13
14# Open for modification
15with mrcfile.open('test.mrc', mode='r+') as mrc:
16 mrc.data[0, 0, 0] = 10.0
17 mrc.flush()