#!/bin/bash
#
# Just reads the db file and re-creates mega table html files
#
export LOFARSOFT=/home/$USER/lofarsoft
export PYTHONPATH="${LOFARSOFT}/release/lib/python:${LOFARSOFT}/release/share/pulsar/bin:${LOFARSOFT}/release/share/pulsar/lib/python:${LOFARSOFT}/release/share/pulsar/lib/python/coast_guard:${LOFARSOFT}/release/share/pulsar/lib/python2.6/site-packages:${PYTHONPATH}:/home/kondratiev/python/lib/python2.6/site-packages"

username=$USER
prg="/usr/bin/env python /home/$username/bin/obsinfo-cep2.py"

lsenodes="1-100"
#webserver="$username@10.87.15.4"   # ag004 - former dop95
webserver="$username@ag004"   # ag004 - former dop95
webaddress="$webserver:/var/www"

dir="/home/$username/Lofar"

# Making the MegaTable (excluding IM-only)
usual=$dir/psr
prof=$dir/pulsars
megaprof=$dir/megapulsars
hbamegaprof=$dir/megapulsars-hba
lbamegaprof=$dir/megapulsars-lba
precycle0megaprof=$dir/psr-precycle0
cycle0megaprof=$dir/psr-cycle0
cycle1megaprof=$dir/psr-cycle1
cycle2megaprof=$dir/psr-cycle2
cycle3megaprof=$dir/psr-cycle3
cycle4megaprof=$dir/psr-cycle4
cycle5megaprof=$dir/psr-cycle5
psrcensus=$dir/psrcensus
psrcensusbrief=$dir/psrcensus-brief
htmltitle="CEPII Pulsar observations "
basehrefdir="lofarpwg"
$prg --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $megaprof --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v plots --lse $lsenodes --linkedhtml $prof --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v usual --lse $lsenodes --linkedhtml $usual --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
# make specific pre-Cycle0 page
##$prg -t 2012-11-30 --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $precycle0megaprof --htmltitle "pre-Cycle0 $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
# make specific Cycle0/Cycle1/Cycle2/Cycle3 and HBA/LBA pages
##$prg -f 2012-12-01 -t 2013-11-14 --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $cycle0megaprof --htmltitle "Cycle0 $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
##$prg -f 2013-11-15 -t 2014-05-14 --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $cycle1megaprof --htmltitle "Cycle1 $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
#$prg -f 2014-05-15 -t 2014-11-14 --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $cycle2megaprof --htmltitle "Cycle2 $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
#$prg -f 2014-11-15 -t 2015-05-14 --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $cycle3megaprof --htmltitle "Cycle3 $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg -f 2015-05-15 --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $cycle4megaprof --htmltitle "Cycle4 $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg -f 2015-11-15 -t 2016-05-14 --search "id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $cycle5megaprof --htmltitle "Cycle5 $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg --search "id.antenna == 'HBA' and id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $hbamegaprof --htmltitle "HBA $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg --search "id.antenna == 'LBA' and id.FE != '-' or id.IS != '-' or id.CS != '-' or id.CV != '-'" --sort time -v smega --lse $lsenodes --linkedhtml $lbamegaprof --htmltitle "LBA $htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
# make specific page for PSR Census (LC1_003)
##$prg --search "id.project == 'LC1_003'" -f 2014-01-30 --sort time -v smega --lse $lsenodes --linkedhtml $psrcensus --htmltitle "Pulsar Census (LC1_003)" --basehrefdir $basehrefdir 2>&1 1>/dev/null
##$prg --search "id.project == 'LC1_003'" -f 2014-01-30 --sort time -v plots --lse $lsenodes --linkedhtml $psrcensusbrief --htmltitle "Pulsar Census (LC1_003)" --basehrefdir $basehrefdir 2>&1 1>/dev/null
# rsync
rsync -avxP $megaprof-*.html $prof-*.html $usual-*.html $precycle0megaprof-*.html $cycle0megaprof-*.html $cycle1megaprof-*.html $cycle2megaprof-*.html $cycle3megaprof-*.html $cycle4megaprof-*.html $cycle5megaprof-*.html $psrcensus-*.html $psrcensusbrief-*.html $webaddress/lofarpwg

# make LOTAAS page
lotaasprof=$dir/lotaas
lotaasplainprof="$dir/lotaas-plain"
basehrefdir="lofarpwg/lotaas"
htmltitle="LOFAR Tied-Array All-sky Survey observations "
$prg --search "id.source[:6] == 'LOTAAS' and id.dur > 3590 and 'LOFAROBS' not in id.project" --sort time -v smega --lse $lsenodes --linkedhtml $lotaasprof --basehrefdir $basehrefdir --htmltitle "$htmltitle" 2>&1 1>/dev/null
$prg --search "id.source[:6] == 'LOTAAS' and id.dur > 3590 and 'LOFAROBS' not in id.project" --sort time -v usual --lse $lsenodes --linkedhtml $lotaasplainprof --basehrefdir $basehrefdir --htmltitle "$htmltitle" 2>&1 1>/dev/null
rsync -avxP $lotaasprof-*.html $lotaasplainprof-*.html $webaddress/lofarpwg/lotaas

# make a Complete list of ObsIDs on CEP2
totusual=$dir/total_usual
totbrief=$dir/total_brief
total=$dir/total
basehrefdir="lofarpwg"
htmltitle="All CEPII observations "
$prg --sort time -v smega --lse $lsenodes --linkedhtml $total --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg --sort time -v plots --lse $lsenodes --linkedhtml $totbrief --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
$prg --sort time -v usual --lse $lsenodes --linkedhtml $totusual --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
rsync -avxP $total-*.html $totbrief-*.html $totusual-*.html $webaddress/lofarpwg

# Making the summary page of only obs with IM (for imaging people)
#imgplain="$dir/img-noplots"
#imgbrief="$dir/img-brief"
#imgtot=$dir/img
#htmltitle="All CEPII Imaging observations "
#basehrefdir="lofarimg"
#$prg --search "id.IM == '+'" --sort time -v smega --lse $lsenodes --linkedhtml $imgtot --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
#$prg --search "id.IM == '+'" --sort time -v plots --lse $lsenodes --linkedhtml $imgbrief --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
#$prg --search "id.IM == '+'" --sort time -v usual --lse $lsenodes --linkedhtml $imgplain --htmltitle "$htmltitle" --basehrefdir $basehrefdir 2>&1 1>/dev/null
#rsync -avxP $imgtot-*.html $imgplain-*.html $imgbrief-*.html $webaddress/lofarimg
