%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from astro.main.ReducedScienceFrame import ReducedScienceFrame
from astro.util import darma
chip_name = 'ESO_CCD_#65'
ob_name = 'KIDS_194.5_2.5_r'
red = ((ReducedScienceFrame.observing_block.name==ob_name)
& (ReducedScienceFrame.chip.name==chip_name)
).max('creation_date')
red.retrieve()
red.raw.retrieve()
red.flat.domeflat.retrieve()
dred = darma.image.image(red.filename).data[:3500,:]
draw = darma.image.image(red.raw.filename, memmap=False).data[:3500,48:-48]
dome = darma.image.image(red.flat.domeflat.filename).data[:3500,:]
ared = np.mean(dred, axis=0)
araw = np.mean(draw, axis=0)
aome = np.mean(dome, axis=0)
mred = np.median(dred, axis=0)
mraw = np.median(draw, axis=0)
mome = np.median(dome, axis=0)
ncols = 2000
sred = mred[:ncols]
sraw = mraw[:ncols]
some = mome[:ncols]
plt.figure(num='ReducedScienceFrame as function of column, CCD #65', figsize=(12,6))
plt.plot(sred, '.')
plt.figure('RawFrame as function of column, CCD #65', figsize=(12,6))
plt.plot(sraw,'.')
plt.figure('DomeFlatFrame as function of column, CCD #65', figsize=(12,6))
plt.plot(some,'.')
plt.figure('DomeFlatFrame vs. RawScienceFrame - columns 2-160, CCD #65', figsize=(12,6))
plt.scatter(some[2:160], sraw[2:160])
p = np.poly1d(np.polyfit(some[2:160], sraw[2:160], 1))
print(p)
plt.plot([0.6,1.1], p([0.6,1.1]),'r')
plt.figure('RawScienceFrame and SCALE*(DomeFlatFrame-OFFSET) as function of column, CCD #65', figsize=(12,6))
p = np.poly1d(np.polyfit(some[2:160], sraw[2:160], 1))
offset = p[0]/p[1]
plt.plot(p(some), 'r.',ms=3.0)
plt.plot(sraw,'b.',ms=3.0)
plt.figure('RawScienceFrame / (DomeFlatFrame - OFFSET) as function of column, CCD #65', figsize=(12,6))
p = np.poly1d(np.polyfit(some[2:160], sraw[2:160], 1))
offset = -p[0]/p[1]
print(offset, 'negative')
plt.plot(np.median(draw/(dome-offset),axis=0)[:ncols],'.')
plt.figure('(RawScienceFrame - OFFSET) / DomeFlatFrame as function of column, CCD #65', figsize=(12,6))
p = np.poly1d(np.polyfit(some[2:160], sraw[2:160], 1))
offset = p[0]
print(offset, 'positive')
plt.plot(np.median((draw-offset)/dome,axis=0)[:ncols],'.')
plt.figure('(RawScienceFrame - SCALE * DomeFlatFrame as function of column, CCD #65', figsize=(12,6))
p = np.poly1d(np.polyfit(some[2:160], sraw[2:160], 1))
scale = p[1]
print(scale)
plt.plot(np.median(draw-scale*dome,axis=0)[:ncols],'.')