import scheduler
from stardice import configfiles
from glob import glob
import astropy.io.fits as pyfits
import logging
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
import numpy as np
from notification import notify
import os

outdir = '../data/spectro_bench/check_leds'
if not os.path.exists(outdir):
    os.mkdir(outdir)


dkd_pos = 145709
zwo_pos = 91900
sbig_pos = 31500

config = configfiles.Config('spectro_bench.cfg')

sched = scheduler.Scheduler(config)


d = sched.get_default()

d['phd.range'] = 2e-4
d['phd.nsamples'] = 10
d['dkd.range'] = 2e-9
d['dkd.nsamples'] = 10
d['zwo'] = False
d['xy.x'] = int(dkd_pos)

wlpeaks = np.array([ 525.,  522., 1067.,  879.,  494.,  466.,  472.,  470.,  455.,
                     441.,  424.,  402.,  403.,  371.,  368.,  450.,  964., 1041.,
                     348.,  903.,  882.,  871.,  850.,  830.,  808.,  773.,  764.,
                     737.,  737.,  744.,  700.,  681.,  692.,  659.,  674.,  648.,
                     649.,  674.,  651.,  623.,  644.,  598.,  953.,  952.,  594.,
                     591.,  536.,  889.])

try:
    notify(f'Starting led check')
    for led in range(48):
        #notify(f'Starting led {led}')
        d['ar.led'] = led
        d['ar.iled'] = 1000
        d['mono.grating'] = 2#  if led['led'] > 5 else 2
        d['ar.shutter'] = 'open'
        d['mono.wavelength'] = int(wlpeaks[led])
        sched.shoot(d, outdir)
        sched.shoot(d, outdir)
        d['ar.shutter'] = 'close'
    for i in range(10):
        sched.shoot(d, outdir)
finally:
    notify('Wavelength scan stopped')

import pickle
import matplotlib.pyplot as plt
import numpy as np
