aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/ciao_fill_sources.sh
blob: cf9417d6588465ad592a650b521a889500a67483 (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
#!/bin/sh
#
# Fill the detected source regions by sampling from their surrounding
# regions, using CIAO `roi' and `dmfilth'.
#
# NOTE:
# * The `dmfilth' only operates on the FITS image, NOT event table.
# * Make `dmfilth' operates on the ONLY DEFLARED image (WITHOUT point
#   sources excluded/removed) (see the CAVEAT of script `ciao_img_rotcrop.sh')
#
# References:
# [1] An Image of Diffuse Emission - CIAO
#     http://cxc.harvard.edu/ciao/threads/diffuse_emission/
#
#
# Aaron LI
# Created: 2016-04-11
UPDATED="2016-04-12"


usage() {
    echo "usage:"
    echo "    `basename $0` evt=<evt.fits> outfile=<filled_img> erange=<elow:ehigh> reg=<sources.reg> repro=<repro_dir_relapath>"
    echo ""
    echo "version: ${UPDATED}"
}

# process commandline arguments of 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
    unset _key _val _keyval
}

# set CIAO pfiles
# syntax: set_pfiles tool1 tool2 ...
set_pfiles() {
    while [ ! -z "$1" ]; do
        _tool="$1"
        shift
        _pfile=`paccess ${_tool}`
        [ -n "${_pfile}" ] && punlearn ${_tool} && cp -Lvf ${_pfile} .
    done
    # Modify environment variable 'PFILES' to use local pfiles first
    export PFILES="./:${PFILES}"
    unset _tool _pfile
}

# set arguments/variables
# syntax: set_variable VAR_NAME default_value cmdline_value
set_variable() {
    _var="$1"
    _default="$2"
    _cmd="$3"
    if [ ! -z "${_cmd}" ]; then
        _value="${_cmd}"
    elif [ ! -z "${_default}" ]; then
        _value="${_default}"
    else
        echo "ERROR: variable '${_var}' get none value"
        exit 11
    fi
    eval "${_var}=${_value}"
    unset _var _default _cmd _value
}

# syntax: set_variable VAR_NAME default_ls_pattern cmdline_value
set_variable_pattern() {
    _default_pat="$2"
    if [ `\ls ${_default_pat} 2>/dev/null | wc -l` -eq 0 ]; then
        echo "ERROR: ls pattern '${_default_pat}' not found"
        exit 21
    elif [ `\ls ${_default_pat} 2>/dev/null | wc -l` -eq 1 ]; then
        _default="`\ls ${_default_pat} 2>/dev/null`"
        set_variable "$1" "${_default}" "$3"
    else
        echo "ERROR: ls pattern '${_default_pat}' found more than 1 values"
        exit 22
    fi
    unset _default_pat _default
}

# determine the ACIS type: whether "S" or "I"
get_acis_type() {
    punlearn dmkeypar
    _detnam=`dmkeypar "$1" DETNAM echo=yes`
    if echo "${_detnam}" | \grep -q 'ACIS-0123'; then
        echo "I"
    elif echo "${_detnam}" | \grep -q 'ACIS-[0-6]*7'; then
        echo "S"
    else
        printf "ERROR: unknown detector type: ${DETNAM}\n"
        exit 31
    fi
    unset _detnam
}


case "$1" in
    -[hH]*|--[hH]*)
        usage
        exit 1
        ;;
esac

# arguments
getopt_keyval "$@"
set_variable_pattern EVT "evt2_c7_deflare.fits evt2_c0-3_deflare.fits" ${evt}
set_variable ERANGE "700:7000" ${erange}
ROOTNAME="${EVT%_deflare.fits}_e`echo ${ERANGE} | tr ':' '-'`"
ROOTNAME="${ROOTNAME#evt2_}"
set_variable OUTFILE "img_${ROOTNAME}_fill.fits" ${outfile}
set_variable_pattern REG "celld_evt2_c7.reg celld_evt2_c0-3.reg" ${reg}
set_variable REPRO ".." ${repro}

set_pfiles skyfov dmcopy dmkeypar dmmakereg roi dmfilth

# Aspect/asol
ASOL_LIS=`\ls ${REPRO}/acisf*_asol1.lis`

# Generate FoV file, since some obs. do NOT provide the `fov1.fits' file
# NOTE: the parameter `aspec' should be provided for the correct FoV
echo "Make skyfov file ..."
FOV="skyfov.fits"
punlearn skyfov
skyfov infile=${EVT} outfile=${FOV} aspect="@${ASOL_LIS}" clobber=yes

# Filter energy
EVT_E="evt2_${ROOTNAME}_deflare.fits"
if [ ! -e "${EVT_E}" ]; then
    echo "Filter by energy range ..."
    punlearn dmcopy
    dmcopy infile="${EVT}[energy=${ERANGE}]" outfile=${EVT_E}
fi

# Make image
IMG="img_${ROOTNAME}_deflare.fits"
if [ ! -e "${IMG}" ]; then
    echo "Make FITS image ..."
    ACIS_TYPE=`get_acis_type ${EVT_E}`
    if [ "${ACIS_TYPE}" = "I" ]; then
        CCD=0:3
    else
        CCD=7
    fi
    punlearn dmcopy
    dmcopy infile="${EVT_E}[sky=region(${FOV}[ccd_id=${CCD}])][bin sky=1]" \
        outfile=${IMG}
fi

# Convert ASCII region to FITS format
echo "Convert region to FITS format ..."
REG_FITS="${REG%.reg}.fits"
punlearn dmmakereg
dmmakereg region="region(${REG})" outfile="${REG_FITS}" clobber=yes

# Determine the background regions for each source
echo "Determine the background regions for each source ..."
ROI_TMP_DIR="_tmp_roi"
[ -d "${ROI_TMP_DIR}" ] && rm -rf ${ROI_TMP_DIR}
mkdir ${ROI_TMP_DIR}
punlearn roi
roi infile="${REG_FITS}" outsrcfile="${ROI_TMP_DIR}/src_%d.fits" \
    fovregion="region(${FOV})" streakregion="" bkgfactor=0.5 \
    radiusmode=mul bkgradius=3 clobber=yes

# Combine all the background regions
echo "Combine all regions ..."
REG_FILL_ROOT="fill"
REG_FILL_SRC="${REG_FILL_ROOT}.src.reg"
REG_FILL_BKG="${REG_FILL_ROOT}.bg.reg"
REG_FILL_BKG2="${REG_FILL_ROOT}.bg.fits"
splitroi "${ROI_TMP_DIR}/src_*.fits" ${REG_FILL_ROOT}
punlearn dmmakereg
dmmakereg region="region(${REG_FILL_BKG})" \
    outfile=${REG_FILL_BKG2} clobber=yes

# Fill the source regions using `dmfilth'
echo "Fill the source regions ..."
punlearn dmfilth
dmfilth infile=${IMG} outfile=${OUTFILE} method=DIST \
    srclist="@${REG_FILL_SRC}" bkglist="@${REG_FILL_BKG}" clobber=yes

rm -rf ${ROI_TMP_DIR}