| 1 |
import glob, sys, os, os.path as path |
| 2 |
import numpy as np |
| 3 |
import mrcz |
| 4 |
from distutils.version import StrictVersion |
| 5 |
import shutil |
| 6 |
|
| 7 |
args = sys.argv |
| 8 |
directory = '.' |
| 9 |
""" |
| 10 |
This script is used for updating a directory of older MRCZ files from versions |
| 11 |
<= 0.4.1 to the 0.5.0 standard, so that the volume sampling is set to the |
| 12 |
default (Mx, My, Mz) = [0, 0, 1]. |
| 13 |
""" |
| 14 |
|
| 15 |
if len(sys.argv) > 1: |
| 16 |
directory = sys.argv[1] |
| 17 |
|
| 18 |
mrcz_files = glob.glob(path.join(directory, '*.mrc'), recursive=False) |
| 19 |
mrcz_files.extend(glob.glob(path.join(directory, '*.mrcz'), recursive=False)) |
| 20 |
|
| 21 |
backup_dir = path.join(directory, 'backup') |
| 22 |
os.makedirs(backup_dir) |
| 23 |
for filename in mrcz_files: |
| 24 |
shutil.copy(filename, path.join(backup_dir, path.basename(filename))) |
| 25 |
|
| 26 |
data, meta = mrcz.readMRC(filename) |
| 27 |
|
| 28 |
compressor = meta['compressor'] if 'compressor' in meta else None |
| 29 |
clevel = meta['clevel'] if 'clevel' in meta else 1 |
| 30 |
voltage = meta['voltage'] if 'voltage' in meta else 0.0 |
| 31 |
C3 = meta['C3'] if 'C3' in meta else 0.0 |
| 32 |
gain = meta['gain'] if 'gain' in meta else 1.0 |
| 33 |
pixelsize = meta['pixelsize'] if 'pixelsize' in meta else [0.1,0.1,0.1] |
| 34 |
pixelunits = meta['pixelunits'] if 'pixelunits' in meta else u'\\AA' |
| 35 |
|
| 36 |
if 'packedBytes' in meta: |
| 37 |
meta.pop('packedBytes') |
| 38 |
if 'minImage' in meta: |
| 39 |
meta.pop('minImage') |
| 40 |
if 'maxImage' in meta: |
| 41 |
meta.pop('maxImage') |
| 42 |
if 'meanImage' in meta: |
| 43 |
meta.pop('meanImage') |
| 44 |
if 'extendedBytes' in meta: |
| 45 |
meta.pop('extendedBytes') |
| 46 |
|
| 47 |
mrcz.writeMRC(data, filename, meta=meta, |
| 48 |
pixelsize=pixelsize, pixelunits=pixelunits, |
| 49 |
voltage=voltage, C3=C3, gain=gain, |
| 50 |
compressor=compressor, clevel=clevel) |
| 51 |
|
| 52 |
# Try and read every file back in. |
| 53 |
try: |
| 54 |
mrcz.readMRC(filename) |
| 55 |
except: |
| 56 |
raise ValueError('{} could not be re-read'.format(filename)) |
| 57 |
|
| 58 |
print('__All__ originals were backed up to %s, remember to delete them' % path.join(directory, 'backup')) |
| 59 |
|
| 60 |
|