#!/bin/csh
######################################################################
# Prepare ASTORB: verify files, remove errors, etc
# Must be in local directory...
# New file = astorb.dat.gz
# Argument "continue" ==> continue the processing
######################################################################

set B = /ftp/cats/B/astorb

if ($#argv>0) then
    if ("$1" =~ cont*) goto ProcessFiles
    echo "Usage: $0 [continue]"
    exit 1
endif

if (-r astorb.dat.gz) then
    echo "#`ls -l astorb.dat*`"
else
    echo "***astorb.dat.gz was not copied???"
    exit 1
endif

rm -f *.log *.Z
rm -f astorb.dat
mv astorb.txt astorb.ori ; a2a astorb.ori > astorb.txt
rm *.ori

zcat astorb.dat.gz | tr '\0' ' ' | trcol -t45 " " . | sed 's/NaN/   /'g \
  | sed 's/\*\*\*\*\*/     /g' \
  | sed 's/-    / 0.0 /g' | sed 's/-2\.1E+35 / 0.0E+00 /g' | gawk '\
    length<200{if(++n<=5) { print "#BAD#" NR, $0 > "/dev/stderr"; next}}\
    {print}' \
| acut -c1-191 -c192-198's/       /   0.0/' -c199-207's/        /   0.0 /' \
     -c208-217 -c218-224's/       /   0.0/' -c- \
| trim \
| sed 's/0.9999242858761.1198671 /0.99992428              /g' \
| acut -c1-41 -c42-47%.2 -c48- > astorb.dat
cp -p astorb.dat /tmp/astorb.dat.1
echo "step1: /tmp/astorb.dat.1(`pwd`)"

set ocount = `fcat $B/astorb.dat | wc -l`
set ncount = `wc -l < astorb.dat`
echo "#---astorb files (new/old): $ncount/$ocount"
if ($ncount <= $ocount) then
    echo "#***No change in ASTORB files???"
    echo "#   [if ok, execute]"
    echo "cd /home/cats/pftp/astorb && ./convert continue"
    exit 0
endif

ProcessFiles:

rm -f ReadMe .history .files .copies .Summary ana.log
cp $B/.[cfh]* .
(echo ""; echo `date +%d-%h-%Y`: Copied from ftp.lowell.edu) >> .history
cmp astorb.txt  $B/astorb.txt
cmp astorb.html $B/astorb.html

cp -p astorb.dat /tmp/astorb.dat.2
echo "step2: /tmp/astorb.dat.2(`pwd`)  `wc -l < astorb.dat`"
@ test_count = 0
Test_loop:
@ test_count += 1
set r = `wc -l < astorb.dat`
gawk -v d=`date +%Y-%h-%d` -v r=$r '\
    /, version/ { v++; if (v == 1) { sub(/ [^ ]*$/, ""); print $0,d; next}}\
    /^astorb.dat/ { if (r < $3)  print "*****Diminution???"; sub($3, r)}\
    {print}' $B/ReadMe > ReadMe
cp -p astorb.dat /tmp/astorb.dat.3
echo "step3: /tmp/astorb.dat.3(`pwd`)  `wc -l < astorb.dat`"
set planets = `cut -c1-6 astorb.dat | uniq | wc -l`
gawk -v planets=$planets -v d=`date +%Y-%h-%d` '\
    /, [Vv]ersion/ { v++;      # {{ \
       if (v == 1) { sub(/ [^ ]* *} *$/, ""); print $0,d "}"; next}}\
    /^\\cRecords/{printf("\\cRecords{%d planets}\n", planets); next}\
    {print}' $B/.Summary > .Summary
anafile -fs ReadMe | tee ana.log ; chmod 600 ana.log
set lana = `wc -l < ana.log`
cp -p astorb.dat /tmp/astorb.dat.4
echo "step4: /tmp/astorb.dat.4(`pwd`) `wc -l < astorb.dat`"
if ($lana > 2) then
    echo "****Problems with 'anafile' (test#$test_count) in file"
    ls -l $cwd/ana.log
    set l6 = `fgrep '++++Value <6> too large in col#013' $cwd/ana.log | wc -l`
    @ lana -= $l6 
    if (($l6 > 0) && ($test_count < 3)) then
        mv astorb.dat astorb.bad
        trcol -t90 6 0 astorb.bad > astorb.dat
        chmod 644 astorb.dat
        if (`wc -l < astorb.bad` == `wc -l < astorb.dat`) then
            rm astorb.bad ; rm -f ana.log
            goto Test_loop
        endif
    endif
    set l0 = `gawk '/NULL.*line #/{print substr($(NF-1),2)}' $cwd/ana.log|uniq`
    if (($#l0 == 1) && ($test_count < 3)) then
        mv astorb.dat astorb.bad
        set lineno=`echo $l0 | acut -d. -f1` 
        gawk "NR!=$lineno{print}" astorb.bad > astorb.dat
	cp -p astorb.bad /tmp/astorb.bad
	cp -p astorb.dat /tmp/astorb.dat.000
	echo "step......`wc -l astorb.bad astorb.dat` /tmp/astorb.dat.000 /tmp/astorb.bad"
        chmod 644 astorb.dat
        rm astorb.bad ; rm -f ana.log
        goto Test_loop
    endif
    exit 1
else
    echo "====astorb successfully copied. Compare with other versions:"
    $HOME/bin/astorb $cwd ; set st = $status;
    #ls -l /ftp/cats/more/astorb
    if ($st == 0) then
        mv -f astorb.dat astorb.old
        mv -f astorb.mod astorb.dat
	# Convert astorb.txt done in ./make
    endif
    echo "====REBUILD (on VizieR) with the command"
    echo "cd ~/pftp/astorb ; ./make"
    exit 2
endif
