#!/usr/bin/env python
import re,os
from exceptions import IndexError
from optparse import OptionParser
import ConfigParser

import numpy as N
import numpy.fft as FT
from scipy.optimize import brentq,fmin,anneal

try:
    import tables
    HAS_TABLES=True
except:
    HAS_TABLES=False


def write_parameter_file(filename):
    config = ConfigParser.RawConfigParser()
    config.add_section('Spectrum')
    cmd_opt_dict = vars(options)
    for option in cmd_opt_dict:
        config.set('Spectrum', option, cmd_opt_dict[option])
    cfgfile = open(filename,'w')
    config.write(cfgfile)
    cfgfile.close()
    return



typ = re.compile("'.*'")

try:
    import matplotlib.pyplot as P
    pyl = True
except:
    pyl = None

parser = OptionParser(version="%prog 0.5")

parser.add_option("--area", dest="normalize_area",
                  help="Normalize area under the spectrum", 
                  metavar="AREA",
                  action="store_true",
                  default=False)


parser.add_option("-b", "--batch", dest="batch",
                  help="No plots, batch processing", 
                  metavar="BATCH",
                  action="store_true",
                  default=False)

parser.add_option("--baseline",
                  type="int", 
                  dest="baseline", 
                  default=0,
                  metavar="N",
                  help="Apply baseline correction to the last N points")

parser.add_option("-c", "--start",
                  type="int", 
                  dest="start", 
                  default=-1,
                  help="Start point of FFT")


parser.add_option("-e", "--end",
                  type="int", 
                  dest="end", 
                  default=-1,
                  metavar="END",
                  help="Skip points past END")

parser.add_option("-f", "--file", dest="infilename",
                  help="Read data from INFILE, can be HDF5 file", 
                  metavar="INFILE")

parser.add_option("--filter",
                  type="float", 
                  dest="filter", 
                  default=0,
                  help="Low pass filter in nyquist frequency")


parser.add_option("-l", "--lb",
                  type="float", 
                  dest="lb", 