##########################################################################
# For py.test unit tests and general development
##########################################################################
skadev
export KADI=/proj/sot/ska/data/kadi
export ENG_ARCHIVE=/proj/sot/ska/data/eng_archive

python setup.py install
cd doc  # make sure it's picking up the installed version
ipython
>>> import Ska.engarchive
>>> Ska.engarchive.test(args='-k -test_fetch_regr')  # normally skip this

##########################################################################
# Build and install docs
##########################################################################
cd doc
make html
cd ..
su -l aca
make install_doc

#############################################################################
# Make baseline (seed) test data using flight Ska.
#
# NORMALLY THIS IS NOT DONE, just use available flight regression data.
#
############
ska
cd /proj/sot/ska/share/eng_archive
export ENG_ARCHIVE=$PWD/test/eng_archive
mkdir -p test/eng_archive
rm -rf test/eng_archive/*

rm -f test/make_eng_archive.log
touch test/make_eng_archive.log

echo "Making regr data..."
./make_regr_data.py --start 2012:290 --stop 2012:300 --data-root test/eng_archive > test/make_eng_archive.log 2>&1

echo "Tarring..."
pushd test
tar zcf eng_archive.tar.gz eng_archive
cp eng_archive.tar.gz /proj/sot/ska/data/eng_archive/regr/flight_eng_archive.tar.gz
popd


#################################################################################
######## Regression test for primary (HEAD) update_archive.py and fetch.py
#################################################################################

# First make test data.  This sets ENG_ARCHIVE in that window.

[[ source make_test_eng_archive.csh ]]
. make_test_eng_archive.sh   # THIS actually works now

# Basic sanity test with some plots

[[ setenv ENG_ARCHIVE $PWD/test/eng_archive ]]
export ENG_ARCHIVE=$PWD/test/eng_archive

test/make_plots.py

# Regression test.  Get_regr_vals.py uses the local ../Ska/engarchive/fetch.py
# if --test is given, otherwise uses the installed Ska version.  It sets
# ENG_ARCHIVE internally prior to importing fetch.

[[ unsetenv ENG_ARCHIVE ]]
unset ENG_ARCHIVE

cd test
  ./get_regr_vals.py --start 2012:302 --stop 2012:310
  ./get_regr_vals.py --start 2012:302 --stop 2012:310 --test
  ./compare_regr_vals.py
cd ..
[[ setenv ENG_ARCHIVE $PWD/test/eng_archive ]]
export ENG_ARCHIVE=$PWD/test/eng_archive

## 2011-Feb-18: All tests pass for rev 56:5b12e819e1df
## 2012-Jan-21: All tests pass for 0.16dev e359acd5 !!
## 2012-Apr-29: All tests pass for 0.18dev efddf4d
## 2012-Nov-27: All tests pass for 0.21

############################################################################################
######## Regression test for new skare (updated Python, NumPy, tables, etc) in $ska/test/
############################################################################################

skatest

. make_test_eng_archive.sh   # THIS actually works now

# Basic sanity test with some plots
test/make_plots.py

pushd test
  rm regr_vals.test
  ./get_regr_vals.py --start 2012:302 --stop 2012:310 --test
popd


############
# Regression test.  Get_regr_vals.py uses the local ../Ska/engarchive/fetch.py
# if --test is given, otherwise uses the installed Ska version.  It sets
# ENG_ARCHIVE internally prior to importing fetch.
ska
unset ENG_ARCHIVE
pushd test
  rm regr_vals.flight
  ./get_regr_vals.py --start 2012:302 --stop 2012:310
  ./compare_regr_vals.py
popd

## 2011-Jun-20 All tests pass for devska (skare 0.10)
## 2012-Jun-12 All tests pass in skatest (skare 0.13) + new eng archive ddc63d2
## 2012-Jun-20 All tests pass in skatest skare 0.13-r241-427bb9c + eng_arch ddc63d2
## 2013-Apr-16 All tests pass in skatest skare 0.15-r293--e754375 + eng_arch f5db57c

########################################################################################################
######## Regression test for new skare in /proj/sot/ska
########################################################################################################

# Make baseline (seed) test data using flight Ska. 

ska

setenv ENG_ARCHIVE $PWD/test/eng_archive
mkdir -p test/eng_archive
rm -rf test/eng_archive/*

rm -f test/make_eng_archive.log
touch test/make_eng_archive.log

## If no previous tarfile is available:
echo "Making regr data..."
./make_regr_data.py --start 2010:260 --stop 2010:270 --data-root test/eng_archive >>& test/make_eng_archive.log

echo "Tarring..."
pushd test
tar zcf eng_archive.tar.gz eng_archive
popd

## If a previous tarfile is available (recommended)
echo "Restoring tar..."
pushd test
tar xf eng_archive.<previous>.tar.gz
popd

setenv ENG_ARCHIVE $PWD/test/eng_archive
set EA=/proj/sot/ska/share/eng_archive
$EA/update_archive.py --date-now 2010:271 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:272 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:273 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:274 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:275 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:276 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:277 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:278 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:279 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:280 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:281 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:282 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:283 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:284 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:285 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:286 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:287 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:288 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:289 --data-root test/eng_archive >>& test/make_eng_archive.log
$EA/update_archive.py --date-now 2010:290 --data-root test/eng_archive >>& test/make_eng_archive.log

# Basic sanity test with some plots
test/make_plots.py

############
# Regression test.  Get_regr_vals.py uses the local ../Ska/engarchive/fetch.py
# if --test is given, otherwise uses the installed Ska version.  It sets
# ENG_ARCHIVE internally prior to importing fetch.

unsetenv ENG_ARCHIVE
pushd test
./get_regr_vals.py --start 2010:265 --stop 2010:285 --test 
./get_regr_vals.py --start 2010:265 --stop 2010:285
./compare_regr_vals.py
popd



########################################################################################################
######## OCC FTP functionality testing
########################################################################################################

export ENG_ARCHIVE=$PWD/test/eng_archive
mkdir -p test/eng_archive
rm -rf test/eng_archive/*

rm -f test/make_eng_archive.head.log
touch test/make_eng_archive.head.log
rm -f test/make_eng_archive.occ.log
touch test/make_eng_archive.occ.log

rm -rf test/eng_archive
rm -rf test/eng_archive.occ
rm -rf test/eng_archive.head

cd test
tar xf /proj/sot/ska/data/eng_archive/regr/flight_eng_archive.tar.gz
mv eng_archive{,.head}
cp -rp eng_archive{.head,.occ}
cd ..

CONTENTS="--content=acis2eng --content=acis3eng --content=acisdeahk --content=ccdm4eng --content=dp_acispow128 --content=orbitephem0 --content=simcoor --content=thm1eng"

for doy in 300 302 304; do
  export ENG_ARCHIVE=$PWD/test/eng_archive.head
  ./update_archive.py $CONTENTS --date-now "2012:$doy" --data-root $ENG_ARCHIVE >> test/make_eng_archive.head.log 2>&1
  ./transfer_stage.py --data-root $ENG_ARCHIVE --ftp-dir=eng_archive_test >> test/make_eng_archive.head.log 2>&1

  export ENG_ARCHIVE=$PWD/test/eng_archive.occ
  ./transfer_stage.py --data-root $ENG_ARCHIVE --occ --ftp-dir=eng_archive_test >> test/make_eng_archive.occ.log 2>&1
  ./update_archive.py $CONTENTS --date-now "2012:$doy" --data-root $ENG_ARCHIVE --occ >> test/make_eng_archive.occ.log 2>&1
done

# Regression tests:
# diff logs (after cutting time columns and replacing _occ with ""): No diffs seen
# diff "ls -l" from respective MSID and 5min directories.  Only diffs in times seen.
# diff binary MSID files.  Some diffs seens...

cd test
  unset ENG_ARCHIVE
  ./get_regr_vals.py --start 2012:295 --stop 2012:303
  export ENG_ARCHIVE=$PWD/eng_archive.occ
  ./get_regr_vals.py --test --start 2012:295 --stop 2012:303
  ./compare_regr_vals.py
cd ..

## 2011-Feb-18: All tests pass for rev 56:5b12e819e1df
## 2011-Feb-20: All tests pass for rev 60
