summaryrefslogtreecommitdiffstats
path: root/funtools/funindex.tmpl
blob: 0bdbe44bd7a4208e3e4eefca74104060974e4a92 (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
#!/bin/sh
# set -x

# initialize
doascii=false
doverbose=true
gz=cat

# process switches
while [ x"$1" != x ]; do
    case $1 in
	-a) doascii=true
	    shift
	    continue;;

	-c) gz="gzip -c"
	    shift
	    continue;;

	-f) doascii=false
	    shift
	    continue;;

	-l) doverbose=true
	    shift
	    continue;;

	-s) doverbose=false
	    shift
	    continue;;

	*)  break;;
    esac
done

# check for required arguments
if [ x"$2" = x ]; then
  echo "usage: $0 [switches] input_fits 'key1 ... keyn' [output]"
  echo "output file is set to root_key.idx for use with funtools processing"
  echo " "
  echo "experimental switches (don't use unless you know what you are doing):"
  echo "  -a        # ASCII output, ignore -c (default: FITS table)"
  echo "  -c        # compress output using gzip"
  echo "  -f        # FITS table output (default: FITS table)"
  echo "  -l        # long output, i.e. with key value(s) (default: long)"
  echo "  -s        # short output, i.e. no key value(s) (default: long)"
  echo " "
  echo "output file name can be specified but funtools will not find it"
  exit 1
fi

# required arguments
FILE=$1
KEYS=$2
if [ x"$3" != x ]; then
  IDX=$3
fi

# output index file name
if [ x"$IDX" = x ]; then
  IDX1=`basename $FILE | sed 's/\(.*\)\.[^.]*/\1/g'`
  if [ x"$IDX1" = x ]; then
    IDX1=$FILE
  fi
  IDX2=`echo $KEYS | sed 's/ /_/g'`
  IDX=${IDX1}_${IDX2}.idx
fi

# input and output rows
ROWS1="\$N $KEYS"
if [ $doverbose = true ]; then
  ROWS2="N $KEYS"
else
  ROWS2='N'
fi

# remove possible already existing columns named N
# sort on the key column and output an additional column N
# output only the key and N columns
if [ $doascii = true ]; then
  funtable $FILE stdout "-N" | \
  funtable -s "$KEYS" stdin stdout "$ROWS1" | \
  fundisp -T stdin'[1]' "$ROWS2" > $IDX
else
  funtable $FILE stdout "-N" | \
  funtable -s "$KEYS" stdin stdout "$ROWS1" | \
  funtable stdin'[1]' $IDX "$ROWS2"
fi
if [ x"$gz" != x"cat" ]; then
  gzip $IDX
  IDX=$IDX.gz
fi

if [ -s $IDX ]; then
  echo "Index file $IDX created for data file $FILE"
else
  echo "ERROR: Index file $IDX not created"
fi