forked from bolidozor/js9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits2png
executable file
·219 lines (189 loc) · 4.44 KB
/
fits2png
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
#!/bin/bash
# set -x
# error routine
error () {
echo "ERROR: $*" >&2
exit 1
}
hash fkeyprint 1>/dev/null 2>&1
if [ $? = 0 ]; then
XEQ=ftools
else
hash funimage 1>/dev/null 2>&1
if [ $? = 0 ]; then
XEQ=funtools
else
error "requires either ftools (fkeyprint) or funtools (fumimage)"
fi
fi
ODIR=""
SWITCHES=""
REPLACE=true
# switch processing
while [ x"$1" != x ]; do
case $1 in
-abs) shift
ABSPATH=$1
shift
continue;;
-dim) shift
XDIM=$1
YDIM=$1
shift
continue;;
-dims) shift
XDIM=$1
YDIM=$2
shift
shift
continue;;
-js9) shift
XDIM=1024
YDIM=1024
VERBOSE=ofile
ODIR="@IDIR@"
REPLACE=false
continue;;
-odir) shift
ODIR=$1
shift
continue;;
-repl) shift
REPLACE=$1
shift
continue;;
-id) shift
ID="/""$1"
shift
continue;;
-gz) shift
FTYPE="gzip:"
continue;;
-ftype) shift
FTYPE="$1"
shift
continue;;
-v) shift
VERBOSE=$1
shift
continue;;
-d) shift
DEBUG=1
continue;;
*) break;;
esac
done
# required first arg: input FITS file
if [ x"$1" != x ]; then
F=$1
else
echo "usage: $0 ifits [opng]" >&2
exit 1
fi
# get input file without extension
IFILE=`echo $F | sed 's#\[.*##'`
# make sure input file exits
if [ ! -r $IFILE ]; then
error "can't locate file: $IFILE"
fi
# get extension
IEXT=`echo $F | sed 's#^[^[]*##'`
# get absolute path
if [ x$ABSPATH = xtrue ]; then
ADIR=$PWD
MYDIR=`dirname $IFILE`
MYFILE=`basename $IFILE`
cd $MYDIR 2>/dev/null || error "can't cd to $MYDIR (for absolute directory)"
IFILE=$PWD/$MYFILE
cd $ADIR || exit
fi
# optional second arg: output PNG file
if [ x"$2" != x ]; then
OFILE=$2
else
OFILE=`basename $IFILE .fits`
OFILE=${OFILE}.png
fi
# where to put the png file ...
# put png is same directory as fits?
if [ x"$ODIR" = 'x@IDIR@' ]; then
ODIR=`dirname $F`
fi
# look for %X string in odir, which will be replaced by a cookie value
KEY=`echo $ODIR | sed -n 's#.*%\([A-Za-z0-9_][A-Za-z0-9_]*\).*#\1#p'`
if [ x"$KEY" != x ]; then
VAL=""
OFS="$IFS"
if [ "$HTTP_COOKIE" != "" ] ; then
IFS=";"; set $HTTP_COOKIE; IFS="$OFS"; COOKIES=$*
for COOK in $COOKIES ; do
IFS="="; set $COOK; IFS="$OFS"
# check for a key of the same name, set value if present
case $1 in
$KEY) VAL="$2";;
esac
done
fi
# make replacement
if [ x"$VAL" != x ]; then
ODIR=`echo $ODIR | sed 's#%'$KEY'#'$VAL'#'`
else
error "no cookie found with key: $KEY"
fi
fi
# make sure output directory exists
if [ x$ODIR != x ]; then
mkdir -p $ODIR
# final form for output file
OFILE=${ODIR}/${OFILE}
fi
# if output file already exists, do we replace it?
if [ x$REPLACE = xtrue -o ! -r $OFILE ]; then
# for js9 processing, determine block factor
if [ x$XDIM != x -a x$YDIM != x ]; then
BL=8
case $XEQ in
ftools)
NAXIS1=`fkeyprint infile=${FTYPE}$IFILE'[events][bin '$BL']' keynam=NAXIS1 | egrep = | awk -F= '{print $2}' | awk -F/ '{print $1}' | sed 's/^ *//g;s/ *$//g'`
NAXIS2=`fkeyprint infile=${FTYPE}$IFILE'[events][bin '$BL']' keynam=NAXIS2 | egrep = | awk -F= '{print $2}' | awk -F/ '{print $1}' | sed 's/^ *//g;s/ *$//g'`
if [ $NAXIS1 -gt $NAXIS2 ]; then
BLOCK=`echo "($NAXIS1 * $BL) / $XDIM" | bc`
else
BLOCK=`echo "($NAXIS2 * $BL) / $XDIM" | bc`
fi
;;
funtools)
DIMS=`funimage ${FTYPE}$IFILE'[*,*,'$BL']' stdout bitpix=8 2>/dev/null | dd bs=80 count=10 2>/dev/null | sed -e 's/.\{80\}/&@/g' | tr '@' '\012' | egrep NAXIS'[1,2]' | sed 's# /.*##g;s# ##g'`
eval $DIMS
if [ $NAXIS1 -gt $NAXIS2 ]; then
BLOCK=`echo "($NAXIS1 * $BL + ($XDIM - 1)) / $XDIM" | bc`
else
BLOCK=`echo "($NAXIS2 * $BL + ($XDIM - 1)) / $XDIM" | bc`
fi
;;
esac
case $XEQ in
ftools)
IEXT="[events]"$IEXT
SWITCHES="-b $BLOCK"
;;
funtools)
IEXT="[*,*,$BLOCK]"$IEXT
;;
esac
fi
# don't execute
if [ x$DEBUG = x1 ]; then
echo "ifile: ${FTYPE}$IFILE""$IEXT"
echo "(xeq: $XEQ; naxes: $NAXIS1 $NAXIS2; dims: $XDIM $YDIM)"
exit
fi
# create png file (filter out the annoying warning about dimensions!)
{ tpos $SWITCHES "${FTYPE}$IFILE""$IEXT" "$OFILE" 2>&1 1>&3 | egrep -v WARNING 1>&2; } 3>&1
fi
# verbose output
case $VERBOSE in
ofile) echo "$OFILE";;
cmd) echo tpos "${FTYPE}$IFILE""$IEXT" "$OFILE";;
* ) ;;
esac