// ✔
 private int FineOrCoarse(Keithley2280BatteryModelTypeEnum x)
 {
     if (x == Keithley2280BatteryModelTypeEnum.Keithley2280BatteryModelTypeCoarse)
     {
         return(11);
     }
     return(101);
 }
        } // format check

        //
        // Creates and saves a .csv file with the Battery Model data to the computer
        // NEEDS WORK!
        public void SaveBatteryModel(int x)
        {
            index = x;
            CheckRange(ref index, 1, 9);
            Keithley2280BatteryModelTypeEnum type = FineOrCoarse(index);
            int length = FineOrCoarse(type);

            double[] voc = new double[length];
            double[] res = new double[length];
            double   cap = 0;

            // COMMAND CAUSES ERROR
            driver.Battery.Model.QueryModel("1", type, index, ref voc, ref res, ref cap);

            string[] lines = new string[length + 3];
            lines[0] = "Battery Model saved at index " + index.ToString() + " of Keithley 2281S-20-6 on " + DateTime.Now.ToString();
            lines[1] = "Capacity=" + cap.ToString() + "AH";
            lines[2] = "SOC,Voc,Res";

            switch (type)
            {
            case Keithley2280BatteryModelTypeEnum.Keithley2280BatteryModelTypeCoarse:
                for (int i = 0; i < length; i++)
                {
                    lines[i + 3] = (i * 10).ToString() + "," + voc[i].ToString() + "," + res[i].ToString();
                }
                break;

            case Keithley2280BatteryModelTypeEnum.Keithley2280BatteryModelTypeFine:
                for (int i = 0; i < length; i++)
                {
                    lines[i + 3] = i.ToString() + "," + voc[i].ToString() + "," + res[i].ToString();
                }
                break;
            }

            List <string> exportFile = new List <string>();

            for (int i = 0; i < length + 3; i++)
            {
                exportFile.Add(lines[i]);
            }

            StoreDataToCSVFile(exportFile);
        }
        // ✔
        // Check if desired battery model already exists on device
        private bool CheckIfBatteryModelExists(double[] voc, double[] res, double cap, double[,] resOffset)
        {
            bool flag = true;
            int  j    = 0;

            double[] modelVoc = new double[voc.Length];
            double[] modelRes = new double[voc.Length];
            double   modelCap = 0;

            double[,] modelResOffset = new double[voc.Length, 2];

            string filePath  = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Keithley 2281\Battery Models Repository\ResistanceOffset";
            string extension = ".csv";

            Keithley2280BatteryModelTypeEnum type = FineOrCoarse(voc.Length);

            for (int i = 1; i < 10; i++) // without the built-in models
            {
                // find resOffset File from Repos
                if (File.Exists(filePath + i.ToString() + extension))
                {
                    List <string> fileLines = new List <string>();
                    StreamReader  str       = new StreamReader(filePath + i.ToString() + extension);

                    while (!str.EndOfStream)
                    {
                        fileLines.Add(str.ReadLine());
                    }

                    str.Close();

                    string[] lines = fileLines.ToArray();

                    for (j = 0; j < lines.Length; j++)
                    {
                        List <string> separatedValues = new List <string>(lines[j].Split(','));
                        modelResOffset[j, 0] = double.Parse(separatedValues[0].Replace('.', ','));
                        modelResOffset[j, 1] = double.Parse(separatedValues[1]);
                    }
                }

                // Load index model
                driver.Battery.Model.QueryModel("1", type, i, ref modelVoc, ref modelRes, ref modelCap);

                flag = true;

                // Check if same battery model already exists
                if (modelCap == Math.Round(cap, 3))
                {
                    j = 0;
                    while (flag && j < 101)
                    {
                        Console.WriteLine(j.ToString() + " , " + Math.Round(voc[j], 4).ToString() + "  ,  " + modelVoc[j].ToString());
                        Console.WriteLine(j.ToString() + " , " + Math.Round(res[j], 4).ToString() + "  ,  " + modelRes[j].ToString());
                        Console.WriteLine(j.ToString() + " , " + resOffset[j, 0].ToString() + "  ,  " + modelResOffset[j, 0].ToString());
                        if (modelVoc[j] != Math.Round(voc[j], 4)) // || modelRes[j] != Math.Round(res[j], 4) || modelResOffset[j, 0] != resOffset[j, 0])
                        {
                            if (modelRes[j] != Math.Round(res[j], 4))
                            {
                            }
                            if (modelResOffset[j, 0] != resOffset[j, 0])
                            {
                            }
                            flag = false;
                            break;
                        }
                        j++;
                    }

                    if (flag)
                    {
                        Console.WriteLine("Battery Model already exists on index " + i.ToString());
                        driver.Battery.Model.RecallByModelIndex("1", i);
                        Console.WriteLine("Battery Model Loaded");
                        return(true);
                    }
                }
            }

            return(false);
        }