public static bool AddNDVICustomDataToMD(string MDWorkspaceFolder, string MDName,
                                                 string bandIndices, bool clearFunctions)
        {
            try
            {
                // Open MD
                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
                IWorkspaceFactory  mdWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
                IWorkspace         mdWorkspace        = mdWorkspaceFactory.OpenFromFile(MDWorkspaceFolder, 0);
                IRasterWorkspaceEx workspaceEx        = (IRasterWorkspaceEx)(mdWorkspace);
                IMosaicDataset     mosaicDataset      = (IMosaicDataset)workspaceEx.OpenRasterDataset(
                    MDName);

                if (clearFunctions) // Clear functions already added to MD.
                {
                    mosaicDataset.ClearFunction();
                }

                // Create NDVI Custom Function
                IRasterFunction rasterFunction = new CustomFunction.NDVICustomFunction();
                // Create the NDVI Custom Function Arguments object
                INDVICustomFunctionArguments rasterFunctionArguments = new NDVICustomFunctionArguments();
                // Set the Band Indices
                rasterFunctionArguments.BandIndices = bandIndices;

                // Add function to MD.
                // This function takes the name of the property corresponding to the Raster
                // property of the Arguments object (in this case is it called Raster itself:
                // rasterFunctionArguments.Raster) as its third argument.
                mosaicDataset.ApplyFunction(rasterFunction, rasterFunctionArguments, "Raster");

                Console.WriteLine("Added NDVI Custom Function to MD: " + MDName + ".");
                Console.WriteLine("Success.");
                return(true);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Exception Caught while adding NDVI Custom Function to MD: " + exc.Message);
                Console.WriteLine("Failed.");
                return(false);
            }
        }
        public static bool AddNDVICustomToRD(IRasterDataset RasterDataset, string OutputFolder, string OutputName,
                                             string bandIndices)
        {
            try
            {
                // Create NDVI Custom Function
                IRasterFunction rasterFunction = new CustomFunction.NDVICustomFunction();
                // Create the NDVI Custom Function Arguments object
                INDVICustomFunctionArguments rasterFunctionArguments = new NDVICustomFunctionArguments();
                // Set the Band Indices
                rasterFunctionArguments.BandIndices = bandIndices;

                // Set the RasterDataset as the input raster
                rasterFunctionArguments.Raster = RasterDataset;

                // Create Function Dataset
                IFunctionRasterDataset functionRasterDataset = new FunctionRasterDataset();
                // Create a Function Raster Dataset Name object
                IFunctionRasterDatasetName functionRasterDatasetName =
                    (IFunctionRasterDatasetName) new FunctionRasterDatasetName();
                // Set the path for the output Function Raster Dataset
                functionRasterDatasetName.FullName = System.IO.Path.Combine(OutputFolder, OutputName);
                functionRasterDataset.FullName     = (IName)functionRasterDatasetName;
                // Initialize the Function Raster Dataset with the function and
                // its arguments object
                functionRasterDataset.Init(rasterFunction, rasterFunctionArguments);

                // Save as Function Raster Dataset as an .afr file
                ITemporaryDataset myTempDset = (ITemporaryDataset)functionRasterDataset;
                myTempDset.MakePermanent();

                Console.WriteLine("Generated " + OutputName + ".");
                Console.WriteLine("Success.");
                return(true);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Exception Caught while adding NDVI Custom Function to Raster Dataset: " + exc.Message);
                Console.WriteLine("Failed.");
                return(false);
            }
        }