#!/usr/bin/env python
import lcatr.schema
import siteUtils
import metUtils
from MetrologyData import md_factory

producer = 'SR-MET-05'
testtype = 'FLATNESS'
results = metUtils.aggregate_filerefs(producer, testtype)

sensorData = md_factory.load('flatness.pickle')
peak_valley_95 = sensorData.quantiles['0.975'] - sensorData.quantiles['0.025']
results.append(lcatr.schema.valid(lcatr.schema.get('sensor_flatness'),
                                  peak_valley_95=peak_valley_95))

results.append(siteUtils.packageVersions())

lcatr.schema.write_file(results)
lcatr.schema.validate_file()
sensor_id = siteUtils.getUnitId()
ccd_vendor = siteUtils.getCcdVendor()

png_files = glob.glob('%(sensor_id)s_abs_height*.png'% locals())
results = [lcatr.schema.fileref.make(x) for x in png_files]

txt_files = glob.glob('%(sensor_id)s_abs_height*.txt'% locals())
results.extend([lcatr.schema.fileref.make(x) for x in txt_files])

#
# Extract numerical results from pickled MetrologyData object, if it exists.
#
pickle_file = 'abs_height.pickle'
if os.path.isfile(pickle_file):
    ZNOM = dict(ITL=12998., e2v=13000.)
    sensorData = md_factory.load(pickle_file)
    dzdx, dzdy, z0 = sensorData.plane_functor.pars
    zmean = np.mean(sensorData.sensor.z)
    znom = ZNOM[ccd_vendor]
    znom_residual_025 = sensorData.quantiles['0.025']
    znom_residual_975 = sensorData.quantiles['0.975']
    results.append(lcatr.schema.valid(lcatr.schema.get('absolute_height'),
                                      dzdx=dzdx, dzdy=dzdy, z0=z0,
                                      zmean=zmean, znom=znom, 
                                      znom_residual_025=znom_residual_025,
                                      znom_residual_975=znom_residual_975))

results.append(siteUtils.packageVersions())

lcatr.schema.write_file(results)
lcatr.schema.validate_file()
# Add the QA plot to the results
raft_id = siteUtils.getUnitId()
qafile = glob.glob('*_qa_plot.png')[0]
#print('qafile:  %s' % qafile)
#print('raft_id:  %s' % raft_id)

md = siteUtils.DataCatalogMetadata(CCD_MANU=siteUtils.getCcdVendor(),
                                   LSST_NUM=siteUtils.getUnitId(),
                                   PRODUCER=producer,
                                   ORIGIN=siteUtils.getSiteName(),
                                   TESTTYPE=testtype,
                                   TEST_CATEGORY='MET')

results.extend([lcatr.schema.fileref.make(qafile, metadata=md(DATA_PRODUCT='QA_PLOT'))])

raftData = md_factory.load('flatness_ts5_delta.pickle')
peak_valley_95 = raftData.quantiles['0.975'] - raftData.quantiles['0.025']
peak_valley_100 = raftData.quantiles['1.000'] - raftData.quantiles['0.000']

# Make strings out of the quantile information
quantiles = raftData.quantiles
quantile_levels = quantiles.keys()
quantile_str = '' 
z_str = ''
for key in sorted(quantile_levels):
    quantile_str += key + ', '
    z_str += "%.2f" % quantiles[key] + ', '
quantile_str = quantile_str[0:len(quantile_str)-2]
z_str = z_str[0:len(z_str)-2]

results.append(lcatr.schema.valid(lcatr.schema.get('ts5_flatness_delta'),