# pull files necessary for the regression tests to test_data
# must run in the resdir

import os, shutil

from gavo import api
from gavo.utils import pyfits

def recompress(src, dest, scale):
	"""reads the HDAP file src and writes a compressed version to dest.
	"""
	if os.path.exists(dest):
		return
	hdus = pyfits.open(src)
	hdr, data = hdus[0].header, hdus[0].data.astype('float')
	del hdr["BZERO"]
	newhdus = pyfits.CompImageHDU(data, hdr,
		compression_type="HCOMPRESS_1", tile_size=[128, 128], hcomp_scale=scale)
	newhdus.writeto(dest, clobber=True)

os.makedirs("test_data/static/envelopes")
os.makedirs("test_data/static/photos")
os.makedirs("test_data/plates")
shutil.copy2("data/static/envelopes/ESO040_004362e.jpg",
	"test_data/static/envelopes")
shutil.copy2("data/static/photos/ESO040_004362.jpg",
	"test_data/static/photos")
shutil.copy2("data/static/envelopes/ESO040_007815e.jpg",
	"test_data/static/envelopes")
shutil.copy2("data/static/photos/ESO040_007815.jpg",
	"test_data/static/photos")

recompress("data/plates/ESO040_007815.fits",
	"test_data/plates/ESO040_007815.fits", 4)
recompress("data/plates/ESO040_004362.fits",
	"test_data/plates/ESO040_004362.fits", 2)
shutil.copytree("data/metadata", "test_data/metadata")

