summaryrefslogtreecommitdiffstats
path: root/ciao_img_rotcrop.sh
blob: ba009d79b8f13788e20c4105911bc70cab5eb716 (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
#!/bin/sh
#
# Copyright (c) 2016 Aaron LI
# MIT license
#
# 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
#
# XXX/CAVEAT:
# The `dmregrid2' will take account for the *excluded* source regions
# even after the FITS image creation.  Therefore, even if we fill the
# excluded source regions using `dmfilth', then rotate the FITS image
# using `dmregrid2', the filled regions are *EXCLUDED* in the rotated
# image!
# This problem is due to that `dmregrid2' considers the DSTYP1/DSVAL1, ...
# keywords/attributes in the FITS header.
#
#
# Created: 2015-08-23
# Updated: 2016-04-11
#

# Image crop sizes
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
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"
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}

echo "============================ WARNING =============================="
echo "The 'dmregrid2' will take account for the *excluded* source regions"
echo "even after the FITS image creation.  Therefore, even if we fill the"
echo "excluded source regions using 'dmfilth', then rotate the FITS image"
echo "using 'dmregrid2', the filled regions are *EXCLUDED* in the rotated"
echo "image!"
echo "*** CHECK THE RESULTS BEFORE PROCEDDING ***"
echo "==================================================================="