# atmos_std.dat, atmos_10.dat
# darksky.dat
# detector.dat
# filter_[ugrizy].dat
# lens[123].dat
# m[123].dat
# total_[ugrizy].dat
# hardware_[ugrizy].dat

# Read the data needed from the syseng_throughput repo.
defaultDirs = bu.setDefaultDirs()
addLosses = True

detector = bu.buildDetector(defaultDirs['detector'], addLosses)
filters = bu.buildFilters(defaultDirs['filters'], addLosses)
lens1 = bu.buildLens(defaultDirs['lens1'], addLosses)
lens2 = bu.buildLens(defaultDirs['lens2'], addLosses)
lens3 = bu.buildLens(defaultDirs['lens3'], addLosses)
m1 = bu.buildMirror(defaultDirs['mirror1'], addLosses)
m2 = bu.buildMirror(defaultDirs['mirror2'], addLosses)
m3 = bu.buildMirror(defaultDirs['mirror3'], addLosses)
atmos_std = bu.readAtmosphere(defaultDirs['atmosphere'],
                              atmosFile='pachonModtranAtm_12_aerosol.dat')
atmos_10 = bu.readAtmosphere(defaultDirs['atmosphere'],
                             atmosFile='atmos_10_aerosol.dat')
darksky = Sed()
darksky.readSED_flambda(os.path.join(defaultDirs['atmosphere'], 'darksky.dat'))
hardware, system = bu.buildHardwareAndSystem(defaultDirs,
                                             addLosses=addLosses,
                                             atmosphereOverride=atmos_std)
    # Generic 'minimum' detector throughputs.
    qevendors['Min'] = bu.buildDetector(defaultDirs['detector'], addLosses)
    throughputs['detector'] = qevendors['Min']
    bu.plotBandpasses(qevendors, title='Combining Vendor Detector Responses')

    # Read the previously generated 'intermediate' file and plot the comparison.
    comparison = Bandpass()
    oldDetector = '../intermediateFiles/components/camera/detThroughput.dat'
    comparison.readThroughput(oldDetector)
    # Plot old and new detectors.
    plotDict = {'New Detector':throughputs['detector'], 'Old Detector':comparison}
    bu.plotBandpasses(plotDict, title='Compare Detector Throughputs')

    # Build and compare the lens throughput curves, for lens1/2/3.
    for lens in ('lens1', 'lens2', 'lens3'):
        throughputs[lens] = bu.buildLens(defaultDirs[lens], addLosses)
        # Read the old intermediate file version.
        comparison.readThroughput('../intermediateFiles/components/camera/%sThroughput.dat'
                                  %(lens))
        plotDict = {'New %s' %(lens): throughputs[lens], 'Old %s' %(lens): comparison}
        bu.plotBandpasses(plotDict, title='Compare %s' %(lens))

    # Build and compare the filter curves.
    filters = bu.buildFilters(defaultDirs['filters'], addLosses)
    oldfilters = {}
    for f in filters:
        oldfilters[f] = Bandpass()
        oldfilters[f].readThroughput('../intermediateFiles/components/camera/'+f+'BandThroughput.dat')
    bu.plotBandpasses(filters)
    bu.plotBandpasses(oldfilters, newfig=False, linestyle=':', title='Compare filters')
    # Put the individual filter bandpasses straight into 'throughputs', for easier use below.
    allPlots = False

    photParams = PhotometricParameters()
    lsstDefaults = LSSTdefaults()

    # Build the detectors.
    qevendors = {}
    qevendors[1] = bu.buildVendorDetector(os.path.join(defaultDirs['detector'], 'vendor1'), addLosses)
    qevendors[2] = bu.buildVendorDetector(os.path.join(defaultDirs['detector'], 'vendor2'), addLosses)
    qevendors['combo'] = bu.buildGenericDetector(defaultDirs['detector'], addLosses)
    if allPlots:
        bu.plotBandpasses(qevendors, title='Vendor Detector Responses')

    # Build the other components.
    lens1 = bu.buildLens(defaultDirs['lens1'], addLosses)
    lens2 = bu.buildLens(defaultDirs['lens2'], addLosses)
    lens3 = bu.buildLens(defaultDirs['lens3'], addLosses)
    filters = bu.buildFilters(defaultDirs['filters'], addLosses)
    mirror1 = bu.buildMirror(defaultDirs['mirror1'], addLosses)
    mirror2 = bu.buildMirror(defaultDirs['mirror2'], addLosses)
    mirror3 = bu.buildMirror(defaultDirs['mirror3'], addLosses)
    atmosphere = bu.buildAtmosphere(defaultDirs['atmosphere'])

    # Plot all components.
    if allPlots:
        plt.figure()
        plt.plot(qevendors['combo'].wavelen, qevendors['combo'].sb, 'k-', linewidth=2, label='Detector')
        plt.plot(lens1.wavelen, lens2.sb, 'g-', linewidth=2, label='L1')
        plt.plot(lens2.wavelen, lens2.sb, 'r-', linewidth=2, label='L2')
        plt.plot(lens3.wavelen, lens3.sb, 'b-', linewidth=2, label='L3')
    bu.plotBandpasses(qevendors, title='Combining Vendor Detector Responses')

    # Read the previously generated 'intermediate' file and plot the comparison.
    comparison = Bandpass()
    oldDetector = '../intermediateFiles/components/camera/detThroughput.dat'
    comparison.readThroughput(oldDetector)
    # Plot old and new detectors.
    plotDict = {
        'New Detector': throughputs['detector'],
        'Old Detector': comparison
    }
    bu.plotBandpasses(plotDict, title='Compare Detector Throughputs')

    # Build and compare the lens throughput curves, for lens1/2/3.
    for lens in ('lens1', 'lens2', 'lens3'):
        throughputs[lens] = bu.buildLens(defaultDirs[lens], addLosses)
        # Read the old intermediate file version.
        comparison.readThroughput(
            '../intermediateFiles/components/camera/%sThroughput.dat' % (lens))
        plotDict = {
            'New %s' % (lens): throughputs[lens],
            'Old %s' % (lens): comparison
        }
        bu.plotBandpasses(plotDict, title='Compare %s' % (lens))

    # Build and compare the filter curves.
    filters = bu.buildFilters(defaultDirs['filters'], addLosses)
    oldfilters = {}
    for f in filters:
        oldfilters[f] = Bandpass()
        oldfilters[f].readThroughput(