#!/usr/bin/env python

from __future__ import print_function

import sys
import seabreeze

try:
    lib = sys.argv[1]
    if lib not in ['cseabreeze', 'pyseabreeze']:
        raise ValueError
except (IndexError, ValueError):
    print("Please call like this:")
    print("> seabreeze-compare cseabreeze")
    print("> seabreeze-compare pyseabreeze")
    exit(1)

seabreeze.use(lib)
import seabreeze.spectrometers as sbs
import traceback
import inspect
import types
import numpy as np



def decorator(func):
    def decorated(*args, **kwargs):
        try:
            print(">>> Calling %s with" % func.__name__, args[1:], kwargs)
            print("")
            a = func(*args, **kwargs)
            if isinstance(a, np.ndarray):
                print("Got Array, shape", a.shape, "dtype", a.dtype)
                print(a)
            else:
                print("Got", type(a), 'value', a)
            print("")
            return a
        except Exception as e:
            traceback.print_exc()
    return decorated

for name, fn in inspect.getmembers(sbs.Spectrometer):
    if isinstance(fn, types.UnboundMethodType):
        setattr(sbs.Spectrometer, name, decorator(fn))

print("Debugging output for '%s'" % lib)
try:
    dev, = sbs.list_devices()
except (sbs.SeaBreezeError, ValueError):
    print("Please connect a spectroscope and run again.")
    exit(1)

spec = sbs.Spectrometer(dev)


print("")

spec.serial_number
spec.model
spec.pixels
spec.minimum_integration_time_micros
spec.trigger_mode(0)
spec.integration_time_micros(100000)
spec.wavelengths()
spec.intensities()
spec.spectrum()
spec.intensities()
spec.intensities(correct_dark_counts=True)
spec.intensities(correct_dark_counts=True, correct_nonlinearity=True)
spec.light_sources
spec.eeprom_read_slot(10)
spec.tec_set_enable(1)
spec.tec_set_temperature_C(10.0)
spec.tec_get_temperature_C()
spec.lamp_set_enable(1)
spec.shutter_set_open(1)
spec.stray_light_coeffs()
spec.irrad_calibration()
spec.irrad_calibration_collection_area()
spec.continuous_strobe_set_enable(1)
spec.continuous_strobe_set_period_micros(10000)
spec.close()

print("")
print("done")

