# Path: photometry/plots.py
# def plots_interactive(backend=('Qt5Agg', 'MacOSX', 'Qt4Agg', 'Qt5Cairo', 'TkAgg')):
# def plots_noninteractive():
# def plot_image(image, ax=None, scale='log', cmap=None, origin='lower', xlabel=None,
# 	ylabel=None, cbar=None, clabel='Flux ($e^{-}s^{-1}$)', cbar_ticks=None,
# 	cbar_ticklabels=None, cbar_pad=None, cbar_size='5%', title=None,
# 	percentile=95.0, vmin=None, vmax=None, offset_axes=None, color_bad='k', **kwargs):
# def plot_image_fit_residuals(fig, image, fit, residuals=None, percentile=95.0):
# def plot_outline(img, ax=None, threshold=0.5, **kwargs):
# def save_figure(path, fig=None, fmt='png', **kwargs):
# 
# Path: photometry/quality.py
# class PixelQualityFlags(QualityFlagsBase):
# 	"""
# 	This class encodes the meaning of the various TESS QUALITY bitmask flags.
# 	"""
# 	NotUsedForBackground = 1
# 	ManualExclude = 2
# 	BackgroundShenanigans = 4
# 
# 	# Default bitmask
# 	DEFAULT_BITMASK = (ManualExclude)
# 
# 	# Pretty string descriptions for each flag
# 	STRINGS = {
# 		NotUsedForBackground: "Pixel was not used in background calculation",
# 		ManualExclude: "Manual exclude",
# 		BackgroundShenanigans: "Background Shenanigans detected in pixel",
# 	}


import numpy as np
import h5py
import sys
import os.path
from photometry.plots import plt, plot_image, matplotlib
from photometry.quality import PixelQualityFlags
from matplotlib.colors import ListedColormap
from rasterize_and_save import rasterize_and_save
		img = np.asarray(hdf['pixel_flags/' + dset_name])
		#img = np.zeros_like(flux0, dtype='int32')
		#img[512:1024,512:1024] = 128

		flags = np.zeros_like(img, dtype='uint8')
		flags[img & PixelQualityFlags.NotUsedForBackground != 0] = 1
		flags[img & PixelQualityFlags.ManualExclude != 0] = 2
		flags[img & PixelQualityFlags.BackgroundShenanigans != 0] = 3

		vmin = hdf['backgrounds'].attrs.get('movie_vmin')
		vmax = hdf['backgrounds'].attrs.get('movie_vmax')
		vmin2 = hdf['images'].attrs.get('movie_vmin')
		vmax2 = hdf['images'].attrs.get('movie_vmax')

	print(vmin, vmax)
	print(vmin2, vmax2)

	# Colormap for images:
	cmap = plt.cm.viridis

	# Colormap for Flags:
	viridis = plt.get_cmap('Dark2')
	newcolors = viridis(np.linspace(0, 1, 4))
	newcolors[:1, :] = np.array([1, 1, 1, 1])
	cmap_flags = ListedColormap(newcolors)

	# Create figures:
	fig, ax = plt.subplots(1, 4, figsize=(20, 6.2))
	img1 = plot_image(flux0+bkg, ax=ax[0], scale='sqrt', vmin=vmin, vmax=vmax, title='Original Image', xlabel=None, ylabel=None, cmap=cmap, make_cbar=True)
	img2 = plot_image(bkg, ax=ax[1], scale='sqrt', vmin=vmin, vmax=vmax, title='Background', xlabel=None, ylabel=None, cmap=cmap, make_cbar=True)

# Path: photometry/plots.py
# def plots_interactive(backend=('Qt5Agg', 'MacOSX', 'Qt4Agg', 'Qt5Cairo', 'TkAgg')):
# def plots_noninteractive():
# def plot_image(image, ax=None, scale='log', cmap=None, origin='lower', xlabel=None,
# 	ylabel=None, cbar=None, clabel='Flux ($e^{-}s^{-1}$)', cbar_ticks=None,
# 	cbar_ticklabels=None, cbar_pad=None, cbar_size='5%', title=None,
# 	percentile=95.0, vmin=None, vmax=None, offset_axes=None, color_bad='k', **kwargs):
# def plot_image_fit_residuals(fig, image, fit, residuals=None, percentile=95.0):
# def plot_outline(img, ax=None, threshold=0.5, **kwargs):
# def save_figure(path, fig=None, fmt='png', **kwargs):
# 
# Path: photometry/quality.py
# class PixelQualityFlags(QualityFlagsBase):
# 	"""
# 	This class encodes the meaning of the various TESS QUALITY bitmask flags.
# 	"""
# 	NotUsedForBackground = 1
# 	ManualExclude = 2
# 	BackgroundShenanigans = 4
# 
# 	# Default bitmask
# 	DEFAULT_BITMASK = (ManualExclude)
# 
# 	# Pretty string descriptions for each flag
# 	STRINGS = {
# 		NotUsedForBackground: "Pixel was not used in background calculation",
# 		ManualExclude: "Manual exclude",
# 		BackgroundShenanigans: "Background Shenanigans detected in pixel",
# 	}


# Path: notes/paper_plot_backgrounds.py
import numpy as np
import h5py
import sys
import os.path
from photometry.plots import plt, plot_image, matplotlib
from photometry.quality import PixelQualityFlags
from matplotlib.colors import ListedColormap
from rasterize_and_save import rasterize_and_save

		img = np.asarray(hdf['pixel_flags/' + dset_name])
		#img = np.zeros_like(flux0, dtype='int32')
		#img[512:1024,512:1024] = 128

		flags = np.zeros_like(img, dtype='uint8')
		flags[img & PixelQualityFlags.NotUsedForBackground != 0] = 1
		flags[img & PixelQualityFlags.ManualExclude != 0] = 2
		flags[img & PixelQualityFlags.BackgroundShenanigans != 0] = 3

		vmin = hdf['backgrounds'].attrs.get('movie_vmin')
		vmax = hdf['backgrounds'].attrs.get('movie_vmax')
		vmin2 = hdf['images'].attrs.get('movie_vmin')
		vmax2 = hdf['images'].attrs.get('movie_vmax')

	print(vmin, vmax)
	print(vmin2, vmax2)

	# Colormap for images:
	cmap = plt.cm.viridis

	# Colormap for Flags:
	viridis = plt.get_cmap('Dark2')
	newcolors = viridis(np.linspace(0, 1, 4))
	newcolors[:1, :] = np.array([1, 1, 1, 1])
	cmap_flags = ListedColormap(newcolors)

	# Create figures:
	fig, ax = plt.subplots(1, 4, figsize=(20, 6.2))
	img1 = plot_image(flux0+bkg, ax=ax[0], scale='sqrt', vmin=vmin, vmax=vmax, title='Original Image', xlabel=None, ylabel=None, cmap=cmap, make_cbar=True)
	img2 = plot_image(bkg, ax=ax[1], scale='sqrt', vmin=vmin, vmax=vmax, title='Background', xlabel=None, ylabel=None, cmap=cmap, make_cbar=True)

	img3 = plot_image(flux0, ax=ax[2], scale='sqrt', vmin=vmin2, vmax=vmax2, title='Background subtracted', xlabel=None, ylabel=None, cmap=cmap, make_cbar=True)