# Path: photometry/prepare.py
# def quality_from_tpf(tpffile, time_start, time_end):
# def _iterate_hdf_group(dset, start=0, stop=None):
# def prepare_photometry(input_folder=None, sectors=None, cameras=None, ccds=None,
# 		calc_movement_kernel=False, backgrounds_pixels_threshold=0.5, output_file=None):


import pytest
import tempfile
import h5py
import os.path
import numpy as np
from conftest import capture_cli
from photometry import prepare
			dset = f'{k:04d}'
			assert 'images/' + dset in hdf and isinstance(hdf['images/' + dset], h5py.Dataset), "IMAGES dset=" + dset + " does not exist"
			assert 'images_err/' + dset in hdf and isinstance(hdf['images_err/' + dset], h5py.Dataset), "IMAGES_ERR dset=" + dset + " does not exist"
			assert 'backgrounds/' + dset in hdf and isinstance(hdf['backgrounds/' + dset], h5py.Dataset), "BACKGROUNDS dset=" + dset + " does not exist"
			assert 'pixel_flags/' + dset in hdf and isinstance(hdf['pixel_flags/' + dset], h5py.Dataset), "PIXEL_FLAGS dset=" + dset + " does not exist"
			assert 'wcs/' + dset in hdf and isinstance(hdf['wcs/' + dset], h5py.Dataset), "WCS dset=" + dset + " does not exist"

			# Check sizes of datasets in depth:
			assert hdf['images/' + dset].shape == img_size, "IMAGES dset=" + dset + " does not have the correct size"
			assert hdf['images_err/' + dset].shape == img_size, "IMAGES_ERR dset=" + dset + " does not have the correct size"
			assert hdf['backgrounds/' + dset].shape == img_size, "BACKGROUNDS dset=" + dset + " does not have the correct size"
			assert hdf['pixel_flags/' + dset].shape == img_size, "PIXEL_FLAGS dset=" + dset + " does not have the correct size"

		# Check headers:
		timestamps = np.asarray(hdf['time']) - np.asarray(hdf['timecorr'])

		if sector is not None:
			assert hdf['images'].attrs['SECTOR'] == sector
		if camera is not None:
			assert hdf['images'].attrs['CAMERA'] == camera
		if ccd is not None:
			assert hdf['images'].attrs['CCD'] == ccd

		assert int(hdf['images'].attrs['CADENCE']) == np.round(86400*np.median(np.diff(timestamps)))

#--------------------------------------------------------------------------------------------------
def test_prepare_photometry(SHARED_INPUT_DIR):

	with tempfile.NamedTemporaryFile() as tmpfile:
		# Run prepare_photometry and save output to temp-file:

# Path: photometry/prepare.py
# def quality_from_tpf(tpffile, time_start, time_end):
# def _iterate_hdf_group(dset, start=0, stop=None):
# def prepare_photometry(input_folder=None, sectors=None, cameras=None, ccds=None,
# 		calc_movement_kernel=False, backgrounds_pixels_threshold=0.5, output_file=None):


# Path: tests/test_prepare.py
import pytest
import tempfile
import h5py
import os.path
import numpy as np
from conftest import capture_cli
from photometry import prepare

			dset = f'{k:04d}'
			assert 'images/' + dset in hdf and isinstance(hdf['images/' + dset], h5py.Dataset), "IMAGES dset=" + dset + " does not exist"
			assert 'images_err/' + dset in hdf and isinstance(hdf['images_err/' + dset], h5py.Dataset), "IMAGES_ERR dset=" + dset + " does not exist"
			assert 'backgrounds/' + dset in hdf and isinstance(hdf['backgrounds/' + dset], h5py.Dataset), "BACKGROUNDS dset=" + dset + " does not exist"
			assert 'pixel_flags/' + dset in hdf and isinstance(hdf['pixel_flags/' + dset], h5py.Dataset), "PIXEL_FLAGS dset=" + dset + " does not exist"
			assert 'wcs/' + dset in hdf and isinstance(hdf['wcs/' + dset], h5py.Dataset), "WCS dset=" + dset + " does not exist"

			# Check sizes of datasets in depth:
			assert hdf['images/' + dset].shape == img_size, "IMAGES dset=" + dset + " does not have the correct size"
			assert hdf['images_err/' + dset].shape == img_size, "IMAGES_ERR dset=" + dset + " does not have the correct size"
			assert hdf['backgrounds/' + dset].shape == img_size, "BACKGROUNDS dset=" + dset + " does not have the correct size"
			assert hdf['pixel_flags/' + dset].shape == img_size, "PIXEL_FLAGS dset=" + dset + " does not have the correct size"

		# Check headers:
		timestamps = np.asarray(hdf['time']) - np.asarray(hdf['timecorr'])

		if sector is not None:
			assert hdf['images'].attrs['SECTOR'] == sector
		if camera is not None:
			assert hdf['images'].attrs['CAMERA'] == camera
		if ccd is not None:
			assert hdf['images'].attrs['CCD'] == ccd

		assert int(hdf['images'].attrs['CADENCE']) == np.round(86400*np.median(np.diff(timestamps)))

#--------------------------------------------------------------------------------------------------
def test_prepare_photometry(SHARED_INPUT_DIR):

	with tempfile.NamedTemporaryFile() as tmpfile:
		# Run prepare_photometry and save output to temp-file:

		prepare.prepare_photometry(SHARED_INPUT_DIR,