#!/bin/sh

#Column Statistics (decreasing frequency in each column):
#-------------------------------------------------------------------------------
# 2(  4-4  ): 1 2 3 4 5 6 7 8 9 0				(1014-590)
# 3(  7-7  ): . 3 2 4 1 5 6 7 8 0 9				(2938-1486)
# 4(  7-7  ): . 4 1 2 5 3 6 7 9 8 0				(2938-1415)
# 5(  7-7  ): 1 . 8 9 7 6 5 2 4 3 0				(4073-1191)
# 6(  7-7  ): 1 . 9 8 0 2 7 5 6 3 4				(3670-1145)
# 7(  7-7  ): 1 . 8 9 7 5 6 4 3 2 0				(4109-1167)
# 8(  7-7  ): 1 . 9 8 0 2 7 6 3 4 5				(3699-1158)
# 9(  6-6  ): 0 . 5 4 6 7 1 8 9 3 2				(6275-633)
#10(  6-6  ): 0 . 6 4 5 7 1 8 3 9 2				(6299-734)
#11(  7-7  ): 1 . 0 2 9 8 3 4 5 6 7				(3264-1073)
#12(  7-7  ): 2 . 1 0 9 7 8 6 4 3 5				(3821-1147)
#13(  7-7  ): 3 . 1 2 0 9 8 5 6 7 4				(3800-882)
#14(  7-7  ): 2 . 4 1 5 0 9 8 7 6 3				(3580-861)
#15(  7-7  ): . 1 0 5 2 3 4 9 7 8 6				(2938-845)
#16(  7-7  ): 0 . 5 - 1 2 3 4 7 8 6 9				(8090-329)
#17(  1-1  ): 1 2 4 3						(949-484)
#-------------------------------------------------------------------------------

# Preparation of the #recs in each file
#-------------------------------------------------------------------------------
anafile -fsr ReadMe | grep ^db | gawk '{n=substr($1, 4); # Remove db/ \
   sub(/.dat[.gz:]*/, "", n); sub(/:/, "", $5); \
   printf "set Np = %s where CMD=\"%s\";\n", $5, n}'
#-------------------------------------------------------------------------------
exit 0	#

#Proposed format for the description of the data:
#0X I4     ---   Col2   [1,1038] Explain column #2
#1X F7.3   ---   Col3   [36.946,798.632] Explain column #3
#1X F7.3   ---   Col4   [30.706,797.969] Explain column #4
#1X F7.4   ---   Col5   [12.2256,23.9143] Explain column #5
#1X F7.4   ---   Col6   [12.7075,21.0108] Explain column #6
#1X F7.4   ---   Col7   [12.2469,22.8736] Explain column #7
#1X F7.4   ---   Col8   [12.7265,21.0894] Explain column #8
#1X F6.4   ---   Col9   [0.0263,1.3339] Explain column #9
#1X F6.4   ---   Col10  [0.0197,1.2527] Explain column #10
#1X F7.4   ---   Col11  [13.5101,25.047] Explain column #11
#1X F7.4   ---   Col12  [14.363,22.6235] Explain column #12
#1X F7.4   ---   Col13  [13.5373,24.4703] Explain column #13
#1X F7.4   ---   Col14  [14.4244,22.7694] Explain column #14
#1X F7.4   ---   Col15  [0.7615,25.959] Explain column #15
#1X F7.4   ---   Col16  [-1.1725,0.741] Explain column #16
#1X I1     ---   Col17  [1,4] Explain column #17

#Proposed acut arguments to align the data:
for f in cal/*.cal; do 
   name=`basename $f`
   radix=`echo $name|acut -d. -f1`
   echo "----Convert $f --> db/$radix.dat"
   rm -f db/$radix.dat
   fcat $f |  acut -d' '  -C'#' \
          -f2%6j    -i'|'  -f3%7.3j  -i'|'  -f4%7.3j  -i'|'  -f5%7.4j \
   -i'|'  -f6%7.4j  -i'|'  -f7%7.4j  -i'|'  -f8%7.4j  -i'|'  -f9%6.4j \
   -i'|' -f10%6.4j  -i'|' -f11%7.4j  -i'|' -f12%7.4j  -i'|' -f13%7.4j \
   -i'|' -f14%7.4j  -i'|' -f15%7.4j  -i'|' -f16%8.4j  -i'|' -f17%1j   \
   | gawk -F\| 'BEGIN{bv[1]=4; bv[2]=5; bv[3]=6; bv[4]=7; bv[5]=10; bv[6]=11;\
     for (i in bv) col[bv[i]] = i }\
   /^#/{print; next}\
   { nb=0; for(i=1; i<=6; i++) { k=bv[i]; m=$k+0; if((m>=32.)||(m<=-10)) nb++ }\
     if(nb == 0) { print; next} \
     printf "%s", $1; \
     for(i=2; i<=NF; i++) { if (i in col) { \
        m=$i+0; if((m>-10.)&&(m<32.)) printf "|%s", $i; \
	else printf "|%7s", ""; \
        } else printf "|%s", $i; } print "" \
   }' > db/$radix.dat
done

