import logging, argparse, os, sys, re
import numpy as np
from collections import OrderedDict
from .utils import getWorkDirs, checkSymLink
from ..ccsgp.ccsgp import make_panel
from ..ccsgp.utils import getOpts
from ..ccsgp.config import default_colors

def gp_panel(version):
  """example for a panel plot using QM12 data (see gp_xfac)"""
  inDir, outDir = getWorkDirs()
  inDir = os.path.join(inDir, version)
  data = {}
  for file in os.listdir(inDir):
    energy = re.compile('\d+').search(file).group()
    data_type = re.sub('%s\.dat' % energy, '', file)
    file_url = os.path.join(inDir, file)
    data_import = np.loadtxt(open(file_url, 'rb'))
    if energy not in data: data[energy] = [ data_import ]
    else: data[energy].append(data_import)
  make_panel(
    dpt_dict = OrderedDict(
      (' '.join([k, 'GeV']), [
        data[k], [
          'with filledcurves lt 1 lw 4 pt 0 lc %s' % default_colors[8],
          'with lines lc 0 lw 5 lt 1',
          'lt 1 lw 4 ps 1.5 lc %s pt 18' % default_colors[0]
        ], [ '+medium', 'cocktail', 'data' ]
      ]) for k in sorted(data, key=int)
    ), # 'lc 0' works here because no error plotting necessary
    name = os.path.join(outDir, 'panel%s' % version),
    ylabel = 'dielectron pair production rate',
    xlabel = 'dielectron mass (GeV/c^{2})',
    ylog = True, xr = [0, 1.1], yr = [1e-4, 20],
    lmargin = 0.08, bmargin = 0.15,
    gpcalls = ['mxtics 2']
  )
  return 'done'

if __name__ == '__main__':
  checkSymLink()
  parser = argparse.ArgumentParser()
  parser.add_argument("version", help="version = subdir name of input dir")
  parser.add_argument("--log", help="show log output", action="store_true")
  args = parser.parse_args()
  loglevel = 'DEBUG' if args.log else 'WARNING'
  logging.basicConfig(
    format='%(message)s', level=getattr(logging, loglevel)
  )
  print gp_panel(args.version)

@@ -327,16 +327,184 @@
 gp_xfac)
+%0A%0A  .. image:: ../ccsgp_get_started_data/examples/gp_panel/panelQM12.png%0A     :width: 450 px%0A%0A  :param version: plot version / input subdir name%0A  :type version: str%0A  
 %22%22%22%0A  in

include panelQM12 plot