aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/analyze_path.py
blob: 06cfcab155a5a6b9613bcdc34247af7d5637efb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
#
# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
# MIT license
#
# Weitian LI
# 2017-02-06

"""
Extract the object name and observation ID from the directory path.

The root directory of the object data has the format:
    <name>_oi<obsid>
"""

import os
import argparse
import re


RE_DATA_DIR = re.compile(r"^.*/(?P<name>[^/_]+)_oi(?P<obsid>\d+).*$")


def get_name(path):
    """
    Extract the object name from the directory path.

    Parameters
    ----------
    path : str
        Path to the data directory

    Returns
    -------
    objname : str
        The name part of the data directory
    """
    return RE_DATA_DIR.match(path).group("name")


def get_obsid(path):
    """
    Extract the observation ID from the directory path.

    Parameters
    ----------
    path : str
        Path to the data directory

    Returns
    -------
    obsid : int
        The observation ID of the data
    """
    return int(RE_DATA_DIR.match(path).group("obsid"))


def main():
    parser = argparse.ArgumentParser(
        description="Extract object name and ObsID from data directory")
    parser.add_argument("-b", "--brief", dest="brief",
                        action="store_true", help="Be brief")
    parser.add_argument("-n", "--name", dest="name",
                        action="store_true", help="Only get object name")
    parser.add_argument("-i", "--obsid", dest="obsid",
                        action="store_true", help="Only get observation ID")
    parser.add_argument("path", nargs="?", default=os.getcwd(),
                        help="Path to the data directory " +
                        "(default: current working directory)")
    args = parser.parse_args()

    b_get_name = False if args.obsid else True
    b_get_obsid = False if args.name else True

    if b_get_name:
        if not args.brief:
            print("Name:", end=" ")
        print(get_name(args.path))
    if b_get_obsid:
        if not args.brief:
            print("ObsID:", end=" ")
        print(get_obsid(args.path))


if __name__ == "__main__":
    main()