immv.py 1.59 KB
Newer Older
1
2
#!/usr/bin/env python
#
3
# immv.py - Move image files.
4
5
6
7
#
# Author: Paul McCarthy <paulmc@fmrib.ox.ac.uk>
#

8
9
10
11
12
13
14
"""This module defines the ``immv`` application, for moving NIFTI image
files.

The :func:`main` function is essentially a wrapper around the
:func:`fsl.utils.imcp.immv` function - see its documentation for more details.
"""

15
16
17
18
19
20

from __future__ import print_function

import os.path        as op
import                   sys
import fsl.utils.path as fslpath
21
22
import fsl.utils.imcp as imcp
import fsl.data.image as fslimage
23
24
25
26
27
28
29
30
31
32
33
34


usage = """Usage:
  immv <file1> <file2>
  immv <file1> <directory>
  immv <file1> <file2> ... <fileN> <directory>

Moves images from <file1> to <file2>, or move all <file>s to <directory>

NB: filenames can be basenames or include an extension.

Recognised file extensions: {}
35
""".format(', '.join(fslimage.ALLOWED_EXTENSIONS))
36
37


38
def main(argv=None):
39
    """Parses CLI arguments (see the usage string), and calls the
40
41
42
43
44
    fsl.utils.imcp.immv function on each input.
    """

    if argv is None:
        argv = sys.argv[1:]
45

46
    if len(argv) < 2:
47
48
49
        print(usage)
        sys.exit(1)

50
51
    srcs = argv[:-1]
    dest = argv[ -1]
52
53
54
55
56

    if len(srcs) > 1 and not op.isdir(dest):
        print(usage)
        sys.exit(1)

57
    srcs = fslpath.removeDuplicates(srcs,
58
59
                                    allowedExts=fslimage.ALLOWED_EXTENSIONS,
                                    fileGroups=fslimage.FILE_GROUPS) 
60

61
    for src in srcs:
62
        imcp.immv(src, dest, useDefaultExt=True)
63
64
65


if __name__ == '__main__':
66
67
68
69
70
    try:
        main()
    except Exception as e:
        print(e)
        sys.exit(1)