## python script to crossfilter out and run Pade approximation on a database 
## inputs : Pade script nls.R, 
##          and one of database source csv or path to crossfiltered named
##          files            
## the necessary details to fit a Pade approximation for
## variety of functions.

import pandas as pd
import numpy as np
import os
from glob import glob
import sys

def crossfilters(database):
    """
    crossfilter out completely a collection
    """
    database = database #pd.read_csv('MainCollection.csv')

    # crossfilter down to VASP's fcc Nb Bulk modulus

    names = []

    codes = np.unique(database['code'])    

    for c in codes:
        code = database[database['code']==c]
        structures = np.unique(code['structure'])
        for struct in structures:
            struct_code = code[code['structure']==struct]
            exchanges = np.unique(struct_code['exchange'])
            for ex in exchanges:
                ex_struct_code = struct_code[struct_code['exchange']==ex]
                elements = np.unique(ex_struct_code['element'])
                for el in elements:
                     el_ex_struct_code = ex_struct_code[ex_struct_code['element']==el]
                     properties = el_ex_struct_code['property']
                     for pr in properties:
                         pr_el_ex_struct_code = el_ex_struct_code[el_ex_struct_code['property']==pr]

                         prop = list(pr_el_ex_struct_code['value'])
                         kpts = list(pr_el_ex_struct_code['k-point'])

                         k_atom = [ k**3 for k in kpts ]

                         Pade_df = pd.DataFrame({'Kpts_atom': k_atom, 'P': prop})

                         TAG =   {'element':el,
                                  'structure':struct,
                                  'exchange':ex,
                                  'code':c,
                                  'property':pr}

                         NAME = '_'.join([pr, el, ex, struct, c])+'.csv'
                         names.append( (NAME,TAG) )
                         print ("Writing {} ..".format(NAME))
                         Pade_df.to_csv('Crossfilts/'+NAME, index=False)

    return names


def read_crossfilts_from_file(filename):
    """
    reads the crossfiltered file and also decomposes the filename 
    into the tags and sends the crossfilt and the tags 
    """

    if len(filename[11:-4].split('_')) == 6:
        pr, el, ex, _,  struct, c = filename[11:-4].split('_')
        ex = '_'.join([ex,_])
    else:
        pr, el, ex, struct, c = filename[11:-4].split('_')
   
    tags = {'element': el,
            'property': pr,
            'exchange': ex, 
            'code': c,