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 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();
        }
        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();
        }
示例#5
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            //nc_out = new NetCDFDataSet("e:\\test.nc", Microsoft.Research.Science.Data.ResourceOpenMode.Create);
            //float[] xx = new float[10];
            //float[] yy = new float[10];
            //float[] dates = new float[2];
            //var var_name = "test";
            //var mat1 = new float[2, 10, 10];

            //nc_out.AddVariable(typeof(float), "longitude", xx, new string[] { "longitude", });
            //nc_out.AddVariable(typeof(float), "latitude", yy, new string[] { "latitude" });
            //var dt = nc_out.AddVariable(typeof(float), "time", dates, new string[] { "time" });
            //var test = nc_out.AddVariable(typeof(float), var_name, mat1, new string[] { "time", "latitude", "longitude" });
            //nc_out.Commit();
            //var newmat = new float[1, 10, 10];

            //dt.Append(new float[1]);
            //test.Append(newmat);
            //nc_out.Commit();
            //    return true;

            var var_index = 0;
            var mat       = Get3DMat(Source, ref var_index);
            int progress  = 0;
            int nsteps    = mat.Size[1];

            var grid   = ProjectService.Project.Model.Grid as RegularGrid;
            var lonlat = grid.GetLonLatAxis();

            var times = new float[nsteps];

            if (mat.DateTimes != null)
            {
                for (int t = 0; t < nsteps; t++)
                {
                    times[t] = mat.DateTimes[t].ToFileTime();
                }
            }
            else
            {
                for (int t = 0; t < nsteps; t++)
                {
                    times[t] = DateTime.Now.AddDays(t).ToFileTime();
                }
            }
            var mat_step = grid.To3DMatrix <float>(mat[var_index, "0", ":"], 0);

            nc_out = new NetCDFDataSet("e:\\test.nc");
            //   nc_out = new NetCDFDataSet(OutputFileName);
            nc_out.AddVariable(typeof(float), "longitude", lonlat[0], new string[] { "longitude", });
            nc_out.AddVariable(typeof(float), "latitude", lonlat[1], new string[] { "latitude" });
            var nc_dt  = nc_out.AddVariable(typeof(float), "time", new float[] { times[0] }, new string[] { "time" });
            var nc_var = nc_out.AddVariable(typeof(float), VariableName, mat_step, new string[] { "time", "latitude", "longitude" });

            nc_out.Commit();
            for (int t = 1; t < nsteps; t++)
            {
                mat_step = grid.To3DMatrix <float>(mat[var_index, "0", ":"], t);
                nc_var.Append(mat_step);
                nc_dt.Append(new float[] { times[t] });
                nc_out.Commit();

                progress = t * 100 / nsteps;
                cancelProgressHandler.Progress("Package_Tool", progress, "Processing step:" + t);
            }
            return(true);
        }