aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xscripts/analyze_path.py86
-rwxr-xr-xscripts/analyze_path.sh61
2 files changed, 86 insertions, 61 deletions
diff --git a/scripts/analyze_path.py b/scripts/analyze_path.py
new file mode 100755
index 0000000..06cfcab
--- /dev/null
+++ b/scripts/analyze_path.py
@@ -0,0 +1,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()
diff --git a/scripts/analyze_path.sh b/scripts/analyze_path.sh
deleted file mode 100755
index 9a1a7b9..0000000
--- a/scripts/analyze_path.sh
+++ /dev/null
@@ -1,61 +0,0 @@
-#!/bin/sh
-
-analyze_path() {
- # extract `obs_id' and `source name' from path
- #
- # Weitian LI <liweitianux@gmail.com>
- # 2013/02/04
- #
- # input:
- # path that include `oi' and `source name'
- # e.g.:
- #
- # output:
- #
-
- echo "$@" | awk '
- # main part
- {
- if (NF==1) {
- ## oi & name
- input=($1 "/")
- if (input ~ /_oi/) {
- ## PATTERN: .../$name_oi$oi/...
- idx_oi = match(input, /oi[0-9]+/) + 2; # "2" skip the "oi"
- len_oi = RLENGTH - 2;
- oi = substr(input, idx_oi, len_oi);
- idx_name = match(input, /\/[a-zA-Z0-9.+-]+_oi/) + 1;
- len_name = RLENGTH - 4;
- name = substr(input, idx_name, len_name);
- }
- else {
- ## PATTERN: .../$name/$oi/...
- idx_oi = match(input, /\/[0-9]+\//) + 1;
- len_oi = RLENGTH - 2;
- oi = substr(input, idx_oi, len_oi);
- idx_name1 = match(input, /\/[a-zA-Z0-9_.+-]+\/[0-9]+\//);
- len_name1 = RLENGTH;
- name1 = substr(input, idx_name1, len_name1);
- idx_name = match(name1, /\/[a-zA-Z0-9_.+-]+\//) + 1;
- len_name = RLENGTH - 2;
- name = substr(name1, idx_name, len_name);
- }
- ## output
- printf("input: %s\n", input)
- printf("oi: %s\nname: %s\n", oi, name)
- }
- else {
- printf("*** WARNING: invalid input: %s\n", $0)
- }
- }
- # END { }
- '
-}
-
-analyze_path $1
-
-OI=`analyze_path $1 | grep '^oi:' | awk '{ print $2 }'`
-NAME=`analyze_path $1 | grep '^name:' | awk '{ print $2 }'`
-printf "\nANALYZE_PATH:\n"
-printf "OI: ${OI}\nNAME: ${NAME}\n"
-