# coding: utf-8

""" Given a config file, create a master inference.hdf5 file. """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os, sys
import logging
import glob

# Third-party
import acor
import h5py
import numpy as np
import astropy.units as u

# Project
from streams import usys
from streams.io import read_hdf5, read_config
from streams.inference import StreamModel
from streams.util import streamspath

def master_inference(filename):
    """ Create a master inference hdf5 file from output every 1000 steps """

    cache_path = os.path.join(streamspath, "plots", "infer_potential",
                              filename, "cache")
    print(cache_path)
    for filename in sorted(glob.glob(os.path.join(cache_path,"inference_*.hdf5"))):
        print(filename)
        with h5py.File(filename, "r") as f:
            try:
                chain = np.hstack((chain,f["chain"].value))
            except NameError:
                chain = f["chain"].value

            accfrac = f["acceptance_fraction"].value

    #taur = [acor.acor(chain[:,:,i])[0] for i in range(chain.shape[2])]
    tau,mm,xx = acor.acor(np.mean(chain[accfrac > 0.02],axis=0).T)
    acor_time = int(2*np.max(tau))
    print("Autocorrelation time: ", acor_time)

    fn = os.path.join(cache_path, "combined_inference.hdf5")

    with h5py.File(fn, "w") as f:
        f["chain"] = chain[:,::acor_time].copy()

if __name__ == '__main__':
    from argparse import ArgumentParser

    # Define parser object
    parser = ArgumentParser(description="")
    parser.add_argument("-f", "--file", dest="filename", required=True,
                        help="Path to the configuration file to run with.")

    args = parser.parse_args()

    master_inference(args.filename)

@@ -501,24 +501,20 @@
 ference(
-filename
+path
 ):%0A    %22
@@ -586,16 +586,44 @@
 ps %22%22%22%0A%0A
+    # first see if relative%0A
     cach
@@ -688,56 +688,13 @@
 al%22,
-%0A                              filename, %22cache%22
+ path
 )%0A  
@@ -1605,19 +1605,13 @@
 t(%22-
-f%22, %22--file
+-path
 %22, d
@@ -1615,24 +1615,20 @@
 , dest=%22
-filename
+path
 %22, requi
@@ -1683,38 +1683,101 @@
 the 
-configuration file to run with
+inference files relative to %22%0A                             %22STREAMSPATH/plots/infer_potential
 .%22)%0A
@@ -1808,17 +1808,16 @@
 _args()%0A
-%0A
     mast
@@ -1834,18 +1834,14 @@
 ce(args.
-filename
+path
 )%0A

take path not filename