#include <Core/Log/LOG.h>
#include <Core/Parsing/PARSE_ARGS.h>
#include <Core/Particles/PARTICLES.h>
#include <Core/Read_Write/FILE_UTILITIES.h>
#include <cstring>
#include <fstream>
#include <set>
using namespace PhysBAM;

std::set<std::string> keep;
std::string in,out;

template<class TV>
void Run()
{
    PARTICLES<TV> particles;
    Read_From_File(in,particles);
    if(keep.size()==0)
    {
        for(ATTRIBUTE_INDEX i(particles.arrays.m-1);i>=ATTRIBUTE_INDEX();i--)
            LOG::printf("%P\n",particles.arrays(i)->name);
        return;
    }
    for(ATTRIBUTE_INDEX i(particles.arrays.m-1);i>=ATTRIBUTE_INDEX();i--)
    {
        if(keep.find(particles.arrays(i)->name)==keep.end())
            particles.Remove_Array_Using_Index(i);
    }
    Write_To_File<typename TV::SCALAR>(out,particles);
}

//#################################################################
// Function main
//#################################################################
int main(int argc,char *argv[])
{
    bool use_double=true;
    bool use_3d=true;
    ARRAY<std::string> keep_list;
    PARSE_ARGS parse_args(argc,argv);
    parse_args.Add_Not("-float",&use_double,"use floats");
    parse_args.Add_Not("-2d",&use_3d,"use 2D");
    parse_args.Add("-i",&in,"file","input particles file");
    parse_args.Add("-o",&out,"file","output particles file");
    parse_args.Add("-k",&keep_list,"attribute","keep this attribute (can be repeated)");
    parse_args.Parse();

    for(const auto&s:keep_list) keep.insert(s);
    
    if(use_double)
    {
        if(use_3d) Run<VECTOR<double,3> >();
        else Run<VECTOR<double,2> >();
    }
    else
    {
        if(use_3d) Run<VECTOR<float,3> >();
        else Run<VECTOR<float,2> >();
    }

    return 0;
}
//#################################################################
