/// <summary>
        /// This function takes a raster object and returns the formatted name of
        /// the object for display in the UI.
        /// </summary>
        /// <param name="inputRaster">Object whose name is to be found</param>
        /// <returns>Name of the object</returns>
        private string GetInputRasterName(object inputRaster)
        {
            if ((inputRaster is IRasterDataset))
            {
                IRasterDataset rasterDataset = (IRasterDataset)inputRaster;
                return(rasterDataset.CompleteName);
            }

            if ((inputRaster is IRaster))
            {
                IRaster myRaster = (IRaster)inputRaster;
                return(((IRaster2)myRaster).RasterDataset.CompleteName);
            }

            if (inputRaster is IDataset)
            {
                IDataset dataset = (IDataset)inputRaster;
                return(dataset.Name);
            }

            if (inputRaster is IName)
            {
                if (inputRaster is IDatasetName)
                {
                    IDatasetName inputDSName = (IDatasetName)inputRaster;
                    return(inputDSName.Name);
                }

                if (inputRaster is IFunctionRasterDatasetName)
                {
                    IFunctionRasterDatasetName inputFRDName = (IFunctionRasterDatasetName)inputRaster;
                    return(inputFRDName.BrowseName);
                }

                if (inputRaster is IMosaicDatasetName)
                {
                    IMosaicDatasetName inputMDName = (IMosaicDatasetName)inputRaster;
                    return("MD");
                }

                IName inputName = (IName)inputRaster;
                return(inputName.NameString);
            }

            if (inputRaster is IRasterFunctionTemplate)
            {
                IRasterFunctionTemplate rasterFunctionTemplate =
                    (IRasterFunctionTemplate)inputRaster;
                return(rasterFunctionTemplate.Function.Name);
            }

            if (inputRaster is IRasterFunctionVariable)
            {
                IRasterFunctionVariable rasterFunctionVariable =
                    (IRasterFunctionVariable)inputRaster;
                return(rasterFunctionVariable.Name);
            }

            return("");
        }
        public static void Main(string[] args)
        {
            #region Initialize License
            ESRI.ArcGIS.esriSystem.AoInitialize aoInit;
            try
            {
                Console.WriteLine("Obtaining license");
                ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
                aoInit = new AoInitializeClass();
                // To make changes to a Mosaic Dataset, a Standard or Advanced license is required.
                esriLicenseStatus licStatus = aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced);
                Console.WriteLine("Ready with license.");
            }
            catch (Exception exc)
            {
                // If it fails at this point, shutdown the test and ignore any subsequent errors.
                Console.WriteLine(exc.Message);
                return;
            }
            #endregion

            try
            {
                // Flags to specify the operation to perform
                bool addToRD            = true;  // Create NDVI Custom Function Raster Dataset
                bool addToMD            = false; // Add NDVI Custom Function to MD
                bool writeTemplateToXml = false; // Serialize a template form of the NDVI Custom Funtion to Xml.
                bool getfromXml         = false; // Get a template object back from its serialized xml.

                #region Specify inputs.
                // Raster Dataset parameters
                string workspaceFolder   = @"c:\temp";
                string rasterDatasetName = "Dubai_ov.tif";
                // Output parameters for Function Raster Dataset
                string outputFolder = @"c:\temp";
                string outputName   = "NDVICustomFunctionSample.afr";

                // Mosaic dataset parameters
                // GDB containing the Mosaic Dataset
                string mdWorkspaceFolder = @"c:\temp\testGdb.gdb";
                // Name of the mosaic dataset
                string mdName = "testMD";

                // NDVI Custom Function Parameters
                string bandIndices = @"4 3";

                // Xml file path to save to or read from xml.
                string xmlFilePath = @"e:\Dev\Samples CSharp\CustomRasterFunction\Xml\NDVICustomAFR.xml";
                #endregion

                if (addToRD)
                {
                    // Open the Raster Dataset
                    Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
                    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
                    IRasterWorkspace  rasterWorkspace  = (IRasterWorkspace)workspaceFactory.OpenFromFile(workspaceFolder, 0);
                    IRasterDataset    rasterDataset    = rasterWorkspace.OpenRasterDataset(rasterDatasetName);
                    AddNDVICustomToRD(rasterDataset, outputFolder, outputName, bandIndices);
                    // Cleanup
                    workspaceFactory = null;
                    rasterWorkspace  = null;
                    rasterDataset    = null;
                }

                if (addToMD)
                {
                    AddNDVICustomDataToMD(mdWorkspaceFolder, mdName, bandIndices, true);
                }

                if (writeTemplateToXml && xmlFilePath != "")
                {
                    // Create a template with the NDVI Custom Function.
                    IRasterFunctionTemplate ndviCustomFunctionTemplate = CreateNDVICustomTemplate(bandIndices);
                    // Serialize the template to an xml file.
                    bool status = WriteToXml(ndviCustomFunctionTemplate, xmlFilePath);
                }


                if (getfromXml && xmlFilePath != "")
                {
                    // Create a RasterFunctionTemplate object from the serialized xml.
                    object serializedObj = ReadFromXml(xmlFilePath);
                    if (serializedObj is IRasterFunctionTemplate)
                    {
                        Console.WriteLine("Success.");
                    }
                    else
                    {
                        Console.WriteLine("Failed.");
                    }
                }

                Console.WriteLine("Press any key...");
                Console.ReadKey();
                aoInit.Shutdown();
            }
            catch (Exception exc)
            {
                Console.WriteLine("Exception Caught in Main: " + exc.Message);
                Console.WriteLine("Failed.");
                Console.WriteLine("Press any key...");
                Console.ReadKey();
                aoInit.Shutdown();
            }
        }