aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/ciao_genregs.sh
blob: 7ab3f413a3c14fdeb9523dc87014596b78bda271 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/sh
##
## Generate regions for 'radial spectra analysis' (rspec.reg),
## and regions for 'surface brightness profile' extraction.
##
## Author: Weitian LI
## Created: 2013/10/12
##
VERSION="v2.1"
UPDATED="2017-02-06"
##
## Changelogs:
## v2.1, 2017-02-06, Weitian LI
##   * Specify regions format and system for ds9
## v2.0, 2015/06/03, Aaron LI
##   * Updated script description
##   * Replaced 'grep' with '\grep', 'ls' with '\ls'
##   * ds9 colormap changed from 'sls' to 'he'
##   * Copy needed pfiles to current working directory, and
##     set environment variable $PFILES to use these first.
## v1.0, 2013/10/12, Weitian LI
##   split from 'ciao_expcorr_sbp_v3.sh'
##

unalias -a
export LC_COLLATE=C

SCRIPT_PATH=`readlink -f $0`
SCRIPT_DIR=`dirname ${SCRIPT_PATH}`
XCENTROID_SCRIPT="chandra_xcentroid.sh"
GEN_SPCREG_SCRIPT="chandra_genspcreg.sh"
GEN_SBPREG_SCRIPT="chandra_gensbpreg.sh"

## err code {{{
ERR_USG=1
ERR_DIR=11
ERR_EVT=12
ERR_BKG=13
ERR_REG=14
ERR_ASOL=21
ERR_BPIX=22
ERR_PBK=23
ERR_MSK=24
ERR_BKGTY=31
ERR_SPEC=32
ERR_DET=41
ERR_ENG=42
ERR_CIAO=100
## error code }}}

## usage {{{
case "$1" in
    -[hH]*|--[hH]*)
        printf "usage:\n"
        printf "    `basename $0` evt=<evt2_clean> reg_in=<reg_in> bkgd=<bkgd_spec> ds9=<Yes|no>\n"
        printf "\nversion:\n"
        printf "    ${VERSION}, ${UPDATED}\n"
        exit ${ERR_USG}
        ;;
esac
## usage }}}

## link needed files {{{
BKGD_FILE=`\ls ../bkg/bkgcorr_bl*.pi 2> /dev/null | head -n 1`
if [ -r "${BKGD_FILE}" ]; then
    ln -svf ${BKGD_FILE} .
fi
ASOL_FILE=`\ls ../pcad*_asol?.fits 2> /dev/null`
if [ -r "${ASOL_FILE}" ]; then
    ln -svf ${ASOL_FILE} .
fi
CELL_REG_FILE=`\ls ../evt/celld*.reg 2> /dev/null | \grep -v 'orig'`
if [ -r "${CELL_REG_FILE}" ]; then
    ln -svf ${CELL_REG_FILE} .
fi
# }}}

## default parameters {{{
## clean evt2 file
DFT_EVT=`\ls evt2*_clean.fits 2> /dev/null`
## the repro dir
DFT_BASEDIR=".."
# default `asol file'
ASOL="`\ls pcadf*_asol1.fits 2> /dev/null | head -n 1`"

## energy range
# format: `E_START:E_END:E_WIDTH'
DFT_ENERGY=700:7000:100
E_START=`echo ${DFT_ENERGY} | awk -F':' '{ print $1 }'`
E_END=`echo ${DFT_ENERGY} | awk -F':' '{ print $2 }'`

## log file
DFT_LOGFILE="genreg_`date '+%Y%m%d'`.log"

## background spectra
DFT_BKGD=`\ls bkgcorr_bl*.pi | head -n 1`
## default parameters }}}

## functions {{{
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 }}}

## check ciao init and set path to solve conflit with heasoft {{{
if [ -z "${ASCDS_INSTALL}" ]; then
    printf "ERROR: CIAO NOT initialized\n"
    exit ${ERR_CIAO}
fi

## XXX: heasoft's `pget' etc. tools conflict with some CIAO tools
printf "set \$PATH to avoid conflicts between HEAsoft and CIAO\n"
export PATH="${ASCDS_BIN}:${ASCDS_CONTRIB}:${PATH}"
printf "## PATH: ${PATH}\n"
## check ciao&heasoft}}}

## parameters {{{
getopt_keyval "$@"

##log file
LOGFILE="not_exist"

##check evt file

if [ -r "${evt}" ]; then
    EVT=${evt}
elif [ -r "${DFT_EVT}" ]; then
    EVT=${DFT_EVT}
else
    read -p "clean evt2 file: " EVT
    if [ ! -r "${EVT}" ]; then
        printf "ERROR: cannot access given \`${EVT}' evt file\n"
        exit ${ERR_EVT}
    fi
fi

##check ori sbp region
if [ -r "${reg_in}" ]; then
    REG_IN="${reg_in}"
else
    read -p "input region file: " REG_IN
    if [ ! -r "${REG_IN}" ]; then
        printf "ERROR: cannot access given \`${REG_IN}' evt file\n"
        exit ${ERR_REG}
    fi
fi

# check give basedir
if [ -d "${basedir}" ]; then
    BASEDIR=${basedir}
elif [ -d "${DFT_BASEDIR}" ]; then
    BASEDIR=${DFT_BASEDIR}
else
    read -p "> basedir (contains asol files): " BASEDIR
    if [ ! -d "${BASEDIR}" ]; then
        printf "ERROR: given \`${BASEDIR}' NOT a directory\n"
        exit ${ERR_DIR}
    fi
fi
# remove the trailing '/'
BASEDIR=`echo ${BASEDIR} | sed 's/\/*$//'`

#background spectrum
if [ -r "${bkgd}" ] ;then
    BKGD=${bkgd}
elif [ -r "${DFT_BKGD}" ] ; then
    BKGD="${DFT_BKGD}"
    #ln -svf ${DFT_BKGD} .
else
    read -p ">background spectrum file: " BKGD
    if [ ! -d ${BKGD} ] ; then
        printf "ERROR on background spectrum file"
        exit ${ERR_BKG}
    fi
fi

## ds9: flag to determine whether to use ds9 check centroid and regions
if [ ! -z "${ds9}" ]; then
    case "${ds9}" in
        [nN][oO]|[fF]*)
            F_DS9="NO"
            ;;
        *)
            F_DS9="YES"
            ;;
    esac
else
    F_DS9="YES"
fi
## parameters }}}

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

# Copy necessary pfiles for localized usage
for tool in ${CIAO_TOOLS}; do
    pfile=`paccess ${tool}`
    [ -n "${pfile}" ] && punlearn ${tool} && cp -Lvf ${pfile} .
done

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

## determine ACIS type {{{
# consistent with `ciao_procevt'
punlearn dmkeypar
DETNAM=`dmkeypar ${EVT} DETNAM echo=yes`
if echo ${DETNAM} | \grep -q 'ACIS-0123'; then
    printf "## \`DETNAM' (${DETNAM}) has chips 0123\n"
    printf "## ACIS-I\n"
    ACIS_TYPE="ACIS-I"
    CCD="0:3"
    NEW_DETNAM="ACIS-0123"
    ROOTNAME="c0-3_e${E_START}-${E_END}"
elif echo ${DETNAM} | \grep -q 'ACIS-[0-6]*7'; then
    printf "## \`DETNAM' (${DETNAM}) has chip 7\n"
    printf "## ACIS-S\n"
    ACIS_TYPE="ACIS-S"
    CCD="7"
    NEW_DETNAM="ACIS-7"
    ROOTNAME="c7_e${E_START}-${E_END}"
else
    printf "ERROR: unknown detector type: ${DETNAM}\n"
    exit ${ERR_DET}
fi
## ACIS type }}}

## filter by energy band
printf "filter out events in energy band: \`${E_START}:${E_END}' ...\n"
EVT_E="evt2_${ROOTNAME}.fits"
if [ ! -r "${EVT_E}" ]; then
    punlearn dmcopy
    dmcopy infile="${EVT}[energy=${E_START}:${E_END}]" outfile="${EVT_E}" clobber=yes
fi

printf "======== X-RAY CENTROID =======\n"
CMD="${SCRIPT_DIR}/${XCENTROID_SCRIPT} evt=${EVT} reg=${REG_IN} conv=yes 2>&1 | tee xcentroid.dat"
printf "CMD: $CMD\n"
${SCRIPT_DIR}/${XCENTROID_SCRIPT} evt=${EVT} reg=${REG_IN} conv=yes 2>&1 | tee xcentroid.dat
X=`\grep '(X,Y)' xcentroid.dat | tr -d ' XY():' | awk -F',' '{ print $2 }'`
Y=`\grep '(X,Y)' xcentroid.dat | tr -d ' XY():' | awk -F',' '{ print $3 }'`
CNTRD_WCS_REG="centroid_wcs.reg"
CNTRD_PHY_REG="centroid_phy.reg"
printf "## X centroid: ($X,$Y)\n"
if [ "${F_DS9}" = "YES" ]; then
    printf "check the X centroid ...\n"
    ds9 ${EVT_E} -regions format ciao \
        -regions system physical \
        -regions ${CNTRD_PHY_REG} \
        -cmap he -bin factor 4
fi
X0=$X
Y0=$Y
X=`\grep -i 'point' ${CNTRD_PHY_REG} | head -n 1 | tr -d 'a-zA-Z() ' | awk -F',' '{ print $1 }'`
Y=`\grep -i 'point' ${CNTRD_PHY_REG} | head -n 1 | tr -d 'a-zA-Z() ' | awk -F',' '{ print $2 }'`
if [ "x${X}" != "x${X0}" ] || [ "x${Y}" != "x${Y0}" ]; then
    printf "## X CENTROID CHANGED -> ($X,$Y)\n"
    # update ${CNTRD_WCS_REG}
    printf "update ${CNTRD_WCS_REG} ...\n"
    rm -f ${CNTRD_WCS_REG}
    punlearn dmcoords
    dmcoords infile="${EVT}" asolfile="${ASOL}" option=sky x=${X} y=${Y}
    RA=`pget dmcoords ra`
    DEC=`pget dmcoords dec`
    echo "point(${RA},${DEC})" > ${CNTRD_WCS_REG}
fi
printf "======== X-RAY CENTROID FINISHED =======\n\n"

SPC_REG=rspec.reg
SBP_REG=sbprofile.reg

printf "======== GENERATE SBPROFILE REGIONS =======\n"
CMD="${SCRIPT_DIR}/${GEN_SBPREG_SCRIPT} ${EVT} ${EVT_E} ${X} ${Y} ${BKGD} ${SBP_REG}"
printf "CMD: ${CMD}\n"
${SCRIPT_DIR}/${GEN_SBPREG_SCRIPT} ${EVT} ${EVT_E} ${X} ${Y} ${BKGD} ${SBP_REG}
if [ "${F_DS9}" = "YES" ]; then
    printf "check SBP regions ...\n"
    ds9 ${EVT_E} -regions format ciao \
        -regions system physical \
        -regions ${SBP_REG} \
        -cmap he -bin factor 4
fi
printf "======== GENERATE SBPROFILE REGIONS FINISHED =======\n\n"

printf "======== GENERATE SPECTRUM REGIONS =======\n"
CMD="${SCRIPT_DIR}/${GEN_SPCREG_SCRIPT} ${EVT} ${EVT_E} ${BKGD} ${X} ${Y} ${SPC_REG}"
printf "CMD: ${CMD}\n"
${SCRIPT_DIR}/${GEN_SPCREG_SCRIPT} ${EVT} ${EVT_E} ${BKGD} ${X} ${Y} ${SPC_REG}
if [ "${F_DS9}" = "YES" ]; then
    printf "check SPC regions ...\n"
    ds9 ${EVT_E} -regions format ciao \
        -regions system physical \
        -regions ${SPC_REG} \
        -cmap he -bin factor 4
fi
printf "======== GENERATE SPECTRUM REGIONS FINISHED =======\n\n"

exit 0