aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/make_infojson.sh
blob: 85706c098252b37943e479fffff4ffc0e15b0f7c (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/sh
#
# This script is used to make a bare '_INFO.json' file
# placed under directory 'repro/',
# thus scripts 'ciao_calc_ct.sh' and 'ciao_calc_csb.sh'
# can get required information from this file.
#
# Run this script in directory 'repro/'.
#
# Weitian LI <liweitianux@gmail.com>
# 2014/06/24
#
#
VERSION="v2.0"
UPDATED="2015/06/03"
#
# ChangeLog:
# v2.0, 2015/06/03, Aaron LI
#   * Copy needed pfiles to tmp directory,
#     set environment variable $PFILES to use these first.
#     and remove them after usage.
# v1.1, 2014/12/05, Weitian LI
#   regex '\s+' not supported by old grep version, change to use '[[:space:]]+'
#

## usage, help {{{
case "$1" in
    -[hH]*|--[hH]*)
        printf "usage:\n"
        printf "    `basename $0` evtdir=<evt_dir> spcdir=<spc_dir> imgdir=<img_dir> massdir=<mass_dir> json=<info_json>\n"
        printf "\nNOTE:\n"
        printf "    run this script in dir 'repro/'\n"
        printf "\nversion:\n"
        printf "    ${VERSION}, ${UPDATED}\n"
        exit 1
        ;;
esac
## usage, help }}}

## default parameters {{{
# basedir: . (run this script under basedir)
BASEDIR=`pwd -P`
#
## directory structure of wjy's data
# default evtdir relative to 'basedir'
DFT_EVTDIR="../evt2/evt"
# default spcdir relative to 'basedir'
DFT_SPCDIR="../evt2/spc/profile"
# default imgdir relative to 'basedir'
DFT_IMGDIR="../evt2/img"
# default massdir relative to 'basedir'
DFT_MASSDIR="../evt2/mass"
## }}}

## functions {{{
# process commandline arguments
# cmdline arg format: `KEY=VALUE'
getopt_keyval() {
    until [ -z "$1" ]
    do
        key=${1%%=*}                    # extract key
        val=${1#*=}                     # extract value
        keyval="${key}=\"${val}\""
        echo "## getopt: eval '${keyval}'"
        eval ${keyval}
        shift                           # shift, process next one
    done
}
## functions }}}

## parameters {{{
# process cmdline args using `getopt_keyval'
getopt_keyval "$@"

# evt dir
if [ ! -z "${evtdir}" ] && [ -d "${BASEDIR}/${evtdir}" ]; then
    EVT_DIR=`( cd ${BASEDIR}/${evtdir} && pwd -P )`
elif [ -d "${BASEDIR}/${DFT_EVTDIR}" ]; then
    EVT_DIR=`( cd ${BASEDIR}/${DFT_EVTDIR} && pwd -P )`
else
    read -p "> evt dir (relative to basedir): " EVT_DIR
    if [ ! -d "${BASEDIR}/${EVT_DIR}" ]; then
        printf "ERROR: given \`${EVT_DIR}' invalid\n"
        exit 20
    else
        EVT_DIR="${BASEDIR}/${EVT_DIR}"
    fi
fi
printf "## use evtdir: \`${EVT_DIR}'\n"

# spc dir
if [ ! -z "${spcdir}" ] && [ -d "${BASEDIR}/${spcdir}" ]; then
    SPC_DIR=`( cd ${BASEDIR}/${spcdir} && pwd -P )`
elif [ -d "${BASEDIR}/${DFT_SPCDIR}" ]; then
    SPC_DIR=`( cd ${BASEDIR}/${DFT_SPCDIR} && pwd -P )`
else
    read -p "> spc dir (relative to basedir): " SPC_DIR
    if [ ! -d "${BASEDIR}/${SPC_DIR}" ]; then
        printf "ERROR: given \`${SPC_DIR}' invalid\n"
        exit 21
    else
        SPC_DIR="${BASEDIR}/${SPC_DIR}"
    fi
fi
printf "## use spcdir: \`${SPC_DIR}'\n"

# img dir
if [ ! -z "${imgdir}" ] && [ -d "${BASEDIR}/${imgdir}" ]; then
    IMG_DIR=`( cd ${BASEDIR}/${imgdir} && pwd -P )`
elif [ -d "${BASEDIR}/${DFT_IMGDIR}" ]; then
    IMG_DIR=`( cd ${BASEDIR}/${DFT_IMGDIR} && pwd -P )`
else
    read -p "> img dir (relative to basedir): " IMG_DIR
    if [ ! -d "${BASEDIR}/${IMG_DIR}" ]; then
        printf "ERROR: given \`${IMG_DIR}' invalid\n"
        exit 22
    else
        IMG_DIR="${BASEDIR}/${IMG_DIR}"
    fi
fi
printf "## use imgdir: \`${IMG_DIR}'\n"

# mass dir
if [ ! -z "${massdir}" ] && [ -d "${BASEDIR}/${massdir}" ]; then
    MASS_DIR=`( cd ${BASEDIR}/${massdir} && pwd -P )`
elif [ -d "${BASEDIR}/${DFT_MASSDIR}" ]; then
    MASS_DIR=`( cd ${BASEDIR}/${DFT_MASSDIR} && pwd -P )`
else
    read -p "> mass dir (relative to basedir): " MASS_DIR
    if [ ! -d "${BASEDIR}/${MASS_DIR}" ]; then
        printf "ERROR: given \`${MASS_DIR}' invalid\n"
        exit 22
    else
        MASS_DIR="${BASEDIR}/${MASS_DIR}"
    fi
fi
printf "## use massdir: \`${MASS_DIR}'\n"
## }}}

## prepare parameter files (pfiles) {{{
CIAO_TOOLS="dmkeypar"

PFILES_TMPDIR="/tmp/pfiles-$$"
[ -d "${PFILES_TMPDIR}" ] && rm -rf ${PFILES_TMPDIR} || mkdir ${PFILES_TMPDIR}

# Copy necessary pfiles for localized usage
for tool in ${CIAO_TOOLS}; do
    pfile=`paccess ${tool}`
    punlearn ${tool} && cp -Lvf ${pfile} ${PFILES_TMPDIR}/
done

# Modify environment variable 'PFILES' to use local pfiles first
export PFILES="${PFILES_TMPDIR}:${PFILES}"
## pfiles }}}

# set files
EVT2_CLEAN=`readlink -f ${EVT_DIR}/evt2*_clean*.fits | head -n 1`
MASS_SBP_CFG=`readlink -f ${MASS_DIR}/source.cfg`
MASS_RES=`readlink -f ${MASS_DIR}/final_result*.txt`
SPC_CFG=`readlink -f ${IMG_DIR}/spc_fit.cfg`

# get needed data
punlearn dmkeypar
OBSID=`dmkeypar "${EVT2_CLEAN}" OBS_ID echo=yes`
NAME=`dmkeypar "${EVT2_CLEAN}" OBJECT echo=yes`
Z=`grep -E '^z[[:space:]]+' ${MASS_SBP_CFG} | awk '{ print $2 }'`
NH=`grep -E '^nh[[:space:]]+' ${SPC_CFG} | awk '{ print $2 }'`
R500=`grep -E '^r500=' ${MASS_RES} | awk '{ print $2 }'`

# generate info json file
if [ ! -z "${json}" ]; then
    JSON_FILE="${json}"
else
    JSON_FILE="oi${OBSID}_INFO.json"
fi
printf "## info json_filename: \`${JSON_FILE}'\n"

cat > ${JSON_FILE} << _EOF_
{
    "Obs. ID": ${OBSID},
    "Source Name": "${NAME}",
    "nH (10^22 cm^-2)": ${NH},
    "redshift": ${Z},
    "R500 (kpc)": ${R500},
    "Comment": "Bare info json file created by `basename $0`"
},
_EOF_

# clean pfiles
rm -rf ${PFILES_TMPDIR}