public static string DumpMatrixToNCFile(DenseMatrix dmData)
        {
            string baseDir          = Directory.GetCurrentDirectory() + "\\";
            string fileName         = DateTime.UtcNow.ToString("o").Replace(":", "-") + ".nc";
            string connectionString = "msds:nc?file=";

            connectionString += baseDir + fileName;

            NetCDFDataSet ds = null;

            try
            {
                ds = new NetCDFDataSet(connectionString, ResourceOpenMode.Create);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            Variable <double> thDataVar = ds.AddVariable <double>("DataMatrix", dmData.ToArray(), "y", "x");

            try
            {
                ds.TryCommit();
            }
            catch (Exception exc)
            {
                return("failed-to-dump");
            }

            ds.Dispose();
            return(fileName);
        }
        public static void AddDataMatrixToFile(DenseMatrix dmData, string fileName, TextBox tbLog,
                                               bool absolutePath = false, string varName = "dataMatrix")
        {
            string baseDir          = "G:\\_gulevlab\\SkyIndexAnalyzerSolo_appData\\_dataDirectory\\";
            string connectionString = "msds:nc?file=";

            if (absolutePath)
            {
                if (!ServiceTools.CheckIfDirectoryExists(fileName))
                {
                    return;
                }
                connectionString += fileName;
            }
            else
            {
                if (!ServiceTools.CheckIfDirectoryExists(baseDir + fileName))
                {
                    return;
                }
                connectionString += baseDir + fileName;
            }

            NetCDFDataSet ds = null;

            try
            {
                ds = new NetCDFDataSet(connectionString, ResourceOpenMode.OpenOrCreate);
            }
            catch (Exception ex)
            {
                if (tbLog != null)
                {
                    ThreadSafeOperations.SetTextTB(tbLog, "Не получилось :( " + Environment.NewLine + ex.Message, true);
                }
                else
                {
                    throw ex;
                }
            }


            Variable <double> thDataVar;

            if (!ds.Variables.Contains(varName))
            {
                thDataVar = ds.AddVariable <double>(varName, dmData.ToArray(), "y", "x");
            }
            else
            {
                thDataVar = (Variable <double>)ds.Variables[varName];
                thDataVar.Append(dmData.ToArray());
            }


            try
            {
                ds.TryCommit();
            }
            catch (Exception ex)
            {
                if (tbLog != null)
                {
                    ThreadSafeOperations.SetTextTB(tbLog, "Не получилось :( " + Environment.NewLine + ex.Message, true);
                }
                else
                {
                    throw ex;
                }
            }

            ds.Dispose();
        }
        public static void AddVariousDataToFile(Dictionary <string, object> dataToWrite, string fileName)
        {
            if (!ServiceTools.CheckIfDirectoryExists(fileName))
            {
                return;
            }

            string connectionString = "msds:nc?file=";

            connectionString += fileName;
            NetCDFDataSet ds = null;

            try
            {
                ds = new NetCDFDataSet(connectionString, ResourceOpenMode.OpenOrCreate);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            foreach (KeyValuePair <string, object> keyValuePair in dataToWrite)
            {
                if (keyValuePair.Value.GetType() == typeof(DenseMatrix))
                {
                    if (ds.Variables.Contains(keyValuePair.Key))
                    {
                        Variable <double> theDataVar = (Variable <double>)ds.Variables[keyValuePair.Key];
                        theDataVar.Append(((DenseMatrix)keyValuePair.Value).ToArray());
                    }
                    else
                    {
                        Variable <double> theDataVar = ds.AddVariable <double>(keyValuePair.Key,
                                                                               ((DenseMatrix)(keyValuePair.Value)).ToArray(), "y", "x");
                    }
                }

                if (keyValuePair.Value.GetType() == typeof(DenseVector))
                {
                    if (ds.Variables.Contains(keyValuePair.Key))
                    {
                        Variable <double> theDataVar = (Variable <double>)ds.Variables[keyValuePair.Key];
                        theDataVar.Append(((DenseVector)keyValuePair.Value).ToArray());
                    }
                    else
                    {
                        Variable <double> theDataVar = ds.AddVariable <double>(keyValuePair.Key,
                                                                               ((DenseVector)(keyValuePair.Value)).ToArray(), keyValuePair.Key);
                    }
                }

                if (keyValuePair.Value.GetType() == typeof(long[]))
                {
                    if (ds.Variables.Contains(keyValuePair.Key))
                    {
                        Variable <long> theDataVar = (Variable <long>)ds.Variables[keyValuePair.Key];
                        theDataVar.Append((long[])(keyValuePair.Value));
                    }
                    else
                    {
                        Variable <long> theDataVar = ds.AddVariable <long>(keyValuePair.Key, (long[])(keyValuePair.Value),
                                                                           keyValuePair.Key);
                    }
                }

                if (keyValuePair.Value.GetType() == typeof(int[]))
                {
                    if (ds.Variables.Contains(keyValuePair.Key))
                    {
                        Variable <int> theDataVar = (Variable <int>)ds.Variables[keyValuePair.Key];
                        theDataVar.Append((int[])(keyValuePair.Value));
                    }
                    else
                    {
                        Variable <int> theDataVar = ds.AddVariable <int>(keyValuePair.Key, (int[])(keyValuePair.Value),
                                                                         keyValuePair.Key);
                    }
                }

                if (keyValuePair.Value.GetType() == typeof(string[]))
                {
                    if (ds.Variables.Contains(keyValuePair.Key))
                    {
                        Variable <string> theDataVar = (Variable <string>)ds.Variables[keyValuePair.Key];
                        theDataVar.Append((string[])(keyValuePair.Value));
                    }
                    else
                    {
                        Variable <string> theDataVar = ds.AddVariable <string>(keyValuePair.Key, (string[])(keyValuePair.Value),
                                                                               keyValuePair.Key);
                    }
                }
            }

            try
            {
                ds.TryCommit();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            ds.Dispose();
        }
        public static void SaveVariousDataToFile(Dictionary <string, object> dataToWrite, string fileName)
        {
            if (!ServiceTools.CheckIfDirectoryExists(fileName))
            {
                return;
            }

            string connectionString = "msds:nc?file=";

            connectionString += fileName;
            // connectionString += "&deflate=best";
            NetCDFDataSet ds = null;

            try
            {
                ds = new NetCDFDataSet(connectionString, ResourceOpenMode.Create);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            foreach (KeyValuePair <string, object> keyValuePair in dataToWrite)
            {
                if (keyValuePair.Value.GetType() == typeof(DenseMatrix))
                {
                    try
                    {
                        Variable <double> theDataVar = ds.AddVariable <double>(keyValuePair.Key, ((DenseMatrix)(keyValuePair.Value)).ToArray(), "y", "x");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                if (keyValuePair.Value.GetType() == typeof(DenseVector))
                {
                    try
                    {
                        Variable <double> theDataVar = ds.AddVariable <double>(keyValuePair.Key, ((DenseVector)(keyValuePair.Value)).ToArray(), keyValuePair.Key);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                if (keyValuePair.Value.GetType() == typeof(long[]))
                {
                    try
                    {
                        Variable <long> theDataVar = ds.AddVariable <long>(keyValuePair.Key, (long[])(keyValuePair.Value), keyValuePair.Key);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }


                if (keyValuePair.Value.GetType() == typeof(long[, ]))
                {
                    try
                    {
                        Variable <long> theDataVar = ds.AddVariable <long>(keyValuePair.Key, (long[, ])(keyValuePair.Value), "y", "x");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }


                if (keyValuePair.Value.GetType() == typeof(int[]))
                {
                    try
                    {
                        Variable <int> theDataVar = ds.AddVariable <int>(keyValuePair.Key, (int[])(keyValuePair.Value), keyValuePair.Key);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }


                if (keyValuePair.Value.GetType() == typeof(int[, ]))
                {
                    try
                    {
                        Variable <int> theDataVar = ds.AddVariable <int>(keyValuePair.Key, (int[, ])(keyValuePair.Value), "y", "x");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }


                if (keyValuePair.Value.GetType() == typeof(short[]))
                {
                    try
                    {
                        Variable <short> theDataVar = ds.AddVariable <short>(keyValuePair.Key, (short[])(keyValuePair.Value), keyValuePair.Key);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }


                if (keyValuePair.Value.GetType() == typeof(short[, ]))
                {
                    try
                    {
                        Variable <short> theDataVar = ds.AddVariable <short>(keyValuePair.Key, (short[, ])(keyValuePair.Value), "y", "x");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }



                if (keyValuePair.Value.GetType() == typeof(byte[]))
                {
                    try
                    {
                        Variable <byte> theDataVar = ds.AddVariable <byte>(keyValuePair.Key, (byte[])(keyValuePair.Value), keyValuePair.Key);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }


                if (keyValuePair.Value.GetType() == typeof(byte[, ]))
                {
                    try
                    {
                        Variable <byte> theDataVar = ds.AddVariable <byte>(keyValuePair.Key, (byte[, ])(keyValuePair.Value), "y", "x");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }



                if (keyValuePair.Value.GetType() == typeof(byte[, , ]))
                {
                    try
                    {
                        Variable <byte> theDataVar = ds.AddVariable <byte>(keyValuePair.Key, (byte[, , ])(keyValuePair.Value), "y", "x", "z");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }



                if (keyValuePair.Value.GetType() == typeof(string[]))
                {
                    Variable <string> theDataVar = ds.AddVariable <string>(keyValuePair.Key, (string[])(keyValuePair.Value), keyValuePair.Key);
                }
            }



            try
            {
                ds.TryCommit();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            ds.Dispose();
        }