aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/ciao_img_rotcrop.sh
blob: afc218e833d2e5442c5e8b75daa6f3e46c39f4c1 (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
#!/bin/sh
#
# Rotate the FITS image to be upright using 'dmregrid2',
# and crop the blank edges from the rotated image
# according to the CCDs sizes.
#
# NOTE:
# * rotation center is set to be the center of the input image
#   (in *image* coordinate and in pixel unit)
# * rotation angle is obtained from the "ROLL_PNT" keyword
# * cropped image size is set to '1010x1010' for ACIS-S, and
#   '2060x2060' for ACIS-I
#
#
# Aaron LI
# Created: 2015-08-23
# Updated: 2016-04-11
#

WIDTH_ACIS_S="1010"
HEIGHT_ACIS_S="1010"
WIDTH_ACIS_I="2060"
HEIGHT_ACIS_I="2060"


if [ $# -ne 2 ]; then
    printf "Usage:\n"
    printf "    `basename $0` <input_img> <output_img>\n"
    exit 1
fi

INIMG="$1"
OUTIMG="$2"

# Get the rotation angle from "ROLL_PNT" keyword
punlearn dmkeypar
ROTANGLE=`dmkeypar ${INIMG} ROLL_PNT echo=yes`
printf "## rotation angle (degree): ${ROTANGLE}\n"

# Determine the rotation center
ROTXCENTER=`dmlist ${INIMG} blocks | grep '^Block.*1:' | tr '(x)' ' ' | awk '{ print $(NF-1)/2 }'`
ROTYCENTER=`dmlist ${INIMG} blocks | grep '^Block.*1:' | tr '(x)' ' ' | awk '{ print $NF/2 }'`
printf "## rotation center (pixel): (${ROTXCENTER},${ROTYCENTER})\n"

# Rotate the image with "dmregrid2"
printf "# rotate image ...\n"
TMP_ROT_IMG="_rot_${INIMG}"
punlearn dmregrid2
dmregrid2 infile="${INIMG}" outfile="${TMP_ROT_IMG}" \
    theta=${ROTANGLE} rotxcenter=${ROTXCENTER} rotycenter=${ROTYCENTER} \
    clobber=yes

# Determine the central point in _physical_ coordinate of the rotated image
punlearn get_sky_limits
get_sky_limits ${TMP_ROT_IMG} verbose=0
XYGRID=`pget get_sky_limits xygrid`
XC=`echo "${XYGRID}" | awk -F'[:,]' '{ print 0.5*($1+$2) }'`
YC=`echo "${XYGRID}" | awk -F'[:,]' '{ print 0.5*($4+$5) }'`

# Determine the crop box size
## determine ACIS type {{{
punlearn dmkeypar
DETNAM=`dmkeypar ${TMP_ROT_IMG} DETNAM echo=yes`
if echo "${DETNAM}" | \grep -q 'ACIS-0123'; then
    printf "## \`DETNAM' (${DETNAM}) has chips 0123 => ACIS-I\n"
    WIDTH=${WIDTH_ACIS_I}
    HEIGHT=${HEIGHT_ACIS_I}
elif echo "${DETNAM}" | \grep -q 'ACIS-[0-6]*7'; then
    printf "## \`DETNAM' (${DETNAM}) has chip 7 => ACIS-S\n"
    WIDTH=${WIDTH_ACIS_S}
    HEIGHT=${HEIGHT_ACIS_S}
else
    printf "ERROR: unknown detector type: ${DETNAM}\n"
    exit 11
fi
printf "## set crop box size: ${WIDTH}x${HEIGHT}\n"
## ACIS type }}}
CROP_REG="rotbox(${XC},${YC},${WIDTH},${HEIGHT},0)"
printf "## crop region: ${CROP_REG}\n"

# Crop the rotated image to match CCD size
printf "# crop rotated image ...\n"
punlearn dmcopy
dmcopy "${TMP_ROT_IMG}[sky=${CROP_REG}]" ${OUTIMG} clobber=yes

# Clean temporary file
rm -f ${TMP_ROT_IMG}