#!/bin/bash

# Copyright (c) 2012-2018 by the GalSim developers team on GitHub
# https://github.com/GalSim-developers
#
# This file is part of GalSim: The modular galaxy image simulation toolkit.
# https://github.com/GalSim-developers/GalSim
#
# GalSim is free software: redistribution and use in source and binary forms,
# with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions, and the disclaimer given in the accompanying LICENSE
#    file.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions, and the disclaimer given in the documentation
#    and/or other materials provided with the distribution.
#

python='/usr/bin/env python'

/bin/rm -rf output
/bin/rm -rf output_yaml

time $python demo1.py || exit
time galsim -v2 demo1.yaml || exit

time $python demo2.py || exit
time galsim -v2 demo2.yaml || exit

time $python demo3.py || exit
time galsim -v2 demo3.yaml || exit

time $python demo4.py || exit
time galsim -v2 demo4.yaml || exit

time $python demo5.py || exit
time galsim -v2 demo5.yaml || exit

time $python demo6.py || exit
time galsim -v2 demo6.yaml || exit

time $python demo7.py || exit
time galsim -v2 demo7.yaml || exit

time $python demo8.py || exit
time galsim -v2 demo8.yaml || exit

time $python demo9.py || exit
time galsim -v1 -n 3 -j 1 demo9.yaml || exit
time galsim -v1 -n 3 -j 2 demo9.yaml || exit
time galsim -v1 -n 3 -j 3 demo9.yaml || exit

time $python demo10.py || exit
time galsim -v2 demo10.yaml || exit

time $python demo11.py || exit
time galsim -v2 demo11.yaml || exit

echo 'Checking diffs: (No output means success)'

# demo1:
# Don't check demo1, since it doesn't use a deterministic seed.
# If you add an initial seed in both places, then you can uncomment
# the next line and it should work.
#$python check_diff.py output/demo1.fits output_yaml/demo1.fits

# demo2:
$python check_diff.py output/demo2.fits output_yaml/demo2.fits

# demo3:
$python check_diff.py output/demo3.fits output_yaml/demo3.fits
$python check_diff.py output/demo3_epsf.fits output_yaml/demo3_epsf.fits

# demo4:
$python check_diff.py output/multi.fits output_yaml/multi.fits

# demo5:
$python check_diff.py output/g08_psf.fits output_yaml/g08_psf.fits
$python check_diff.py output/g08_gal.fits output_yaml/g08_gal.fits

# demo6:
$python check_diff.py output/psf_real.fits output_yaml/psf_real.fits
$python check_diff.py output/cube_real.fits output_yaml/cube_real.fits

# demo7:
# Note; the raw .gz files include the filename in them.
# Since the file names are different, they don't match.
# So unzip them first before running diff.
gunzip output/cube_phot.fits.gz
gunzip output_yaml/cube_phot.fits.gz
$python check_diff.py output/cube_phot.fits output_yaml/cube_phot.fits

# demo8:
$python check_diff.py output/bpd_single.fits output_yaml/bpd_single.fits
$python check_diff.py output/bpd_multi.fits output_yaml/bpd_multi.fits

# demo9:
for dir_num in {1..4}
do
    for file_num in {0..4}
    do
        file_name=nfw$dir_num/cluster000$file_num.fits
        truth_name=nfw$dir_num/truth000$file_num.dat
        $python check_diff.py output/$file_name output_yaml/$file_name
        $python check_diff.py output/$truth_name output_yaml/$truth_name
    done
done

# demo10:
$python check_diff.py output/power_spectrum.fits output_yaml/power_spectrum.fits

# demo11:
$python check_diff.py output/tabulated_power_spectrum.fits.fz output_yaml/tabulated_power_spectrum.fits.fz

