bool
avtProgrammableOperation::avtVisItForEachLocationR::func(ProgrammableOpArguments& args, vtkShapedDataArray &result)
{
    Variant windowArray = args.getArg(0);
    vtkShapedDataArray var = args.getArgAsShapedDataArray(1);
    Variant kernelName = args.getArg(2); //name of the function to call..
    Variant primaryVariable = args.getArg(3); // primary variable name to modify..

    std::vector<Variant> kernelArgs = args.getArgAsVariantVector(4);

    avtPythonFilterEnvironment* environ = args.GetPythonEnvironment();

    std::ostringstream rsetup;
    rsetup << "import rpy2, rpy2.robjects\n"
           << kernelName.AsString() <<  " = rpy2.robjects.r('" << kernelName.AsString() << "')\n";
    environ->Interpreter()->RunScript(rsetup.str());

    std::string arglist = "";

    for(int i = 0; i < kernelArgs.size(); ++i)
        arglist += kernelArgs[i].ConvertToString() + (i == kernelArgs.size()-1 ? "" : ",");

    std::ostringstream resultKernel;
    resultKernel << "res = " << kernelName.AsString() <<  "(numpy.asarray(__internal_array)";

    if(arglist.size() > 0)
        resultKernel << "," << arglist << ")\n";
    else
        resultKernel << ")\n";

    resultKernel << "res = numpy.asarray(res).tolist()\n";

    /// run the time loop filter..

    //std::cout << resultKernel << std::endl;
    avtTimeWindowLoopFilter *filt = new avtTimeWindowLoopFilter;
    filt->environment = args.GetPythonEnvironment();
    filt->inputDataSet = args.GetInputDataSet();
    filt->inputDomain = args.GetInputDomain();
    filt->script = resultKernel.str();

    filt->SetInput(args.GetInput());
    avtDataObject_p dob = filt->GetOutput();

    dob->Update(args.GetContract());

    result = filt->globalOutputDataArray;

//    result.shape = var.shape;
//    result.vtkarray = var.vtkarray->NewInstance();
//    result.vtkarray->DeepCopy(var.vtkarray);

    return true;
}
bool
avtProgrammableOperation::avtVisItForEachLocation::func(ProgrammableOpArguments& args, vtkShapedDataArray& result)
{
    Variant windowArray = args.getArg(0);
    vtkShapedDataArray var = args.getArgAsShapedDataArray(1);
    Variant kernelLanguage = args.getArg(2); //R or Python
    Variant kernel = args.getArg(3); //kernel itself
    Variant kernelName = args.getArg(4); //name of the function to call..
    Variant primaryVariable = args.getArg(5); // primary variable name to modify..

    std::vector<Variant> kernelArgs = args.getArgAsVariantVector(6);

    avtPythonFilterEnvironment* environ = args.GetPythonEnvironment();

    /// register the kernels..
    if(kernelLanguage == "Python")
    {
        environ->Interpreter()->RunScript(kernel.AsString());
    }
    else
    {
#ifdef HAVE_LIB_R
        std::ostringstream rsetup;

        rsetup << "import rpy2, rpy2.robjects\n"
               << kernelName.AsString() <<  " = rpy2.robjects.r(\"\"\"\n"
               << kernel.AsString() << "\n"
               << "\"\"\")\n";
        environ->Interpreter()->RunScript(rsetup.str());
#else
        std::cerr << "R is not supported at this time.." << std::endl;
        return false;
#endif
    }

    std::string arglist = "";

    for(int i = 0; i < kernelArgs.size(); ++i)
        arglist += kernelArgs[i].ConvertToString() + (i == kernelArgs.size()-1 ? "" : ",");

    std::ostringstream resultKernel;
    resultKernel << "res = " << kernelName.AsString() <<  "(numpy.asarray(__internal_array)";

    if(arglist.size() > 0)
        resultKernel << "," << arglist << ")\n";
    else
        resultKernel << ")\n";

    resultKernel << "res = numpy.asarray(res).tolist()\n";


    /// std::cout << resultKernel.str() << std::endl;
    /// run the time loop filter..

    //std::cout << "disabling timer" << std::endl;
    avtCallback::ResetTimeout(0);

    avtTimeWindowLoopFilter *filt = new avtTimeWindowLoopFilter;
    filt->environment = environ;
    filt->inputDataSet = args.GetInputDataSet();
    filt->inputDomain = args.GetInputDomain();
    filt->script = resultKernel.str();

    filt->SetInput(args.GetInput());
    avtDataObject_p dob = filt->GetOutput();

    dob->Update(args.GetContract());

    avtCallback::ResetTimeout(5*60);
    //std::cout << "enabling timer" << std::endl;

    std::cout << PAR_Rank() << " finishing... " << std::endl;
    std::cout << var.vtkarray->GetDataSize() << " "
              << filt->globalOutputDataArray.vtkarray->GetDataSize() << " "
              << filt->globalOutputDataArray.shape[0] << " "
              << filt->globalOutputDataArray.shape[1] << std::endl;

    result = filt->globalOutputDataArray;

    //result.shape.push_back(var->GetDataSize());
    //result.vtkarray = var->NewInstance();
    //result.vtkarray->DeepCopy(var);

    return true;
}