public static void TestDataSetVariable(string tableName, LookupColumnTest lookupColumnTest, Variable v) { var columnTest = lookupColumnTest.Invoke(v.Name); if (columnTest.Include) { var count = columnTest.Expected.Count(); var actualTruncated = (double[])null; switch (v.TypeOfData.Name.ToString().ToLower()) { case "single": var actual = (float[])v.GetData(); actualTruncated = actual.Select(a => (double)a).Take(count).ToArray(); break; case "double": actualTruncated = ((double[])v.GetData()).Take(count).ToArray(); break; default: throw new Exception("unexpected column type"); } DoColumnTest(tableName, v.Name, columnTest, actualTruncated); } else { Console.WriteLine("Skipping : {0}", v.Name); } }
public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler) { int nstep = 0; var xx = _XVariable.GetData() as float[]; var yy = _YVariable.GetData() as float[]; var nc_array = _SelectedVariable.GetData() as float[, , ]; var time = _TimeVariable.GetData() as float[]; if (time != null) { nstep = time.Count(); } else { var tt = _TimeVariable.GetData() as double[]; if (tt != null) { nstep = tt.Length; } else { var t1 = _TimeVariable.GetData() as int[]; if (t1 != null) { nstep = t1.Length; } } } int progress = 0; var numColumns = xx.Length; var numRows = yy.Length; Extent extent = new Extent(0, -90, 360, 90); RasterBounds bound = new RasterBounds(numRows, numColumns, extent); if (nc_array != null) { for (int t = 0; t < nstep; t++) { var fn = Path.Combine(OutputFolder, (t + 1) + ".tif"); var raster = Raster.CreateRaster(fn, string.Empty, numColumns, numRows, 1, typeof(float), new[] { string.Empty }); raster.Bounds = bound; raster.Projection = Projection; for (int row = 0; row < numRows; row++) { for (int j = 0; j < numColumns; j++) { if (nc_array[t, row, j] > 0) { raster.Value[row, j] = nc_array[t, row, j]; } } } raster.Save(); progress = t * 100 / nstep; cancelProgressHandler.Progress("Package_Tool", progress, "Processing time step:" + t); } } return(true); }
private void FetchTest_Check(Variable v, int[] shape) { Assert.IsFalse(v.DataSet.HasChanges); Assert.IsTrue(v.DataSet.IsAutocommitEnabled); Assert.AreEqual(shape.Length, v.Rank); for (int i = 0; i < v.Rank; i++) { Assert.AreEqual(shape[i], v.GetShape()[i]); } var data = v.GetData(); int[] index = new int[v.Rank]; for (int i = 0; i < data.Length; i++) { Assert.IsTrue(((double)data.GetValue(index)) > 0); for (int j = v.Rank; --j >= 0;) { index[j]++; if (index[j] < shape[j]) { break; } index[j] = 0; } } Assert.IsNotNull(v.MissingValue); Assert.IsNotNull(v.Metadata["Units", SchemaVersion.Recent]); Assert.IsNotNull(v.Metadata["Name", SchemaVersion.Recent]); Assert.IsNotNull(v.Metadata["long_name", SchemaVersion.Recent]); }
public float GetElevationAtPoint(FileMetadata metadata, int x, int y) { var Y = metadata.Height - y; float height = (float)_elevationVariable.GetData(new int[] { Y, x }, new int[] { 1, 1 }).GetValue(0, 0); return(height); }
private Array GetData() { int rank = source.Rank; int[] start = new int[rank]; int[] stride = new int[rank]; int[] count = new int[rank]; for (int i = 0; i < rank; i++) { if (dims[i] != -1) { start[i] = dims[i]; stride[i] = 1; count[i] = 1; } else { start[i] = 0; stride[i] = 1; count[i] = source.Dimensions[i].Length; } } Array array = source.GetData(start, stride, count); Type type = TypeUtils.GetElementType(array); Array res = Array.CreateInstance(type, array.Length); int j = 0; foreach (var x in array) { res.SetValue(x, j); j++; } return(res); }
public static void TestDataSet3DVariable(string tableName, LookupColumn3dTest lookupColumn3dTest, LookupColumnTest lookupColumnTest, Variable v) { var columnTest = lookupColumnTest.Invoke(v.Name); if (columnTest.Include) { ColumnTest.TestDataSetVariable(tableName, lookupColumnTest, v); } else { var column3dTest = lookupColumn3dTest.Invoke(v.Name); if (column3dTest.Include) { var actualTruncated = (double[, , ])null; switch (v.TypeOfData.Name.ToString().ToLower()) { case "double": { var actual = (double[, , ])v.GetData(); var count1 = column3dTest.Expected.GetLength(0); var count2 = column3dTest.Expected.GetLength(1); var count3 = column3dTest.Expected.GetLength(2); actualTruncated = new double[count1, count2, count3]; for (var ii = 0; ii < count1; ii++) { for (var jj = 0; jj < count2; jj++) { for (var kk = 0; kk < count3; kk++) { actualTruncated[ii, jj, kk] = actual[ii, jj, kk]; } } } } break; default: throw new Exception("unexpected column type"); } DoColumn3dTest(column3dTest, actualTruncated); } else { Console.WriteLine("Skipping : {0}", v.Name); } } }
public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler) { IFeatureSet fs = null; if (!TypeConverterEx.IsNull(PointFeatureFileName) && File.Exists(PointFeatureFileName)) { fs = FeatureSet.Open(PointFeatureFileName); } if (fs != null) { var npt = fs.NumRows(); Coordinate[] coors = new Coordinate[npt]; int progress = 0; for (int i = 0; i < npt; i++) { var geo_pt = fs.GetFeature(i).Geometry; coors[i] = geo_pt.Coordinate; } var time = _TimeVariable.GetData() as float[]; var xx = _XVariable.GetData() as float[]; var yy = _YVariable.GetData() as float[]; var nc_array = _SelectedVariable.GetData() as float[, , ]; int nstep = time.Count(); var mat_out = new DataCube <float>(1, time.Length, npt); mat_out.Name = OutputMatrix; mat_out.Variables = new string[] { _SelectedVariableName }; mat_out.DateTimes = new DateTime[nstep]; var pt_index = GetIndex(xx, yy, coors); for (int t = 0; t < nstep; t++) { for (int i = 0; i < npt; i++) { mat_out[0, t, i] = nc_array[t, pt_index[i][1], pt_index[i][0]]; } progress = t * 100 / nstep; cancelProgressHandler.Progress("Package_Tool", progress, "Processing time step:" + t); mat_out.DateTimes[t] = DateTime.FromOADate(time[t]); } Workspace.Add(mat_out); return(true); } else { cancelProgressHandler.Progress("Package_Tool", 50, "Failed to run. The input parameters are incorrect."); return(false); } }
private static void CheckProvenance(Variable v, params string[] dims) { Assert.AreEqual(v.Rank, dims.Length); for (int i = 0; i < dims.Length; i++) { Assert.AreEqual(v.Dimensions[i].Name, dims[i]); } Array a = v.GetData(); foreach (string c in a) { Assert.IsTrue(!String.IsNullOrWhiteSpace(c)); } }
private static void CheckUncertainty(Variable v, params string[] dims) { Assert.AreEqual(v.Rank, dims.Length); for (int i = 0; i < dims.Length; i++) { Assert.AreEqual(v.Dimensions[i].Name, dims[i]); } Array a = v.GetData(); foreach (double c in a) { Assert.IsTrue(double.IsNaN(c) || c > 0); } }
private void OnDataSetCommitted(object sender, DataSetCommittedEventArgs e) { int[] start = new int[] { 0, 0, 0, u.Dimensions[3].Length - 1 }; double[, , ,] uData = (double[, , , ])u.GetData(start, null); double[, , ,] vData = (double[, , , ])v.GetData(start, null); double[, , ,] wData = (double[, , , ])w.GetData(start, null); SetData(new FuncData3D <Vector3D>((i, j, k) => { Vector3D result = new Vector3D(uData[i, j, k, 0], vData[i, j, k, 0], wData[i, j, k, 0]); return(result); })); RaiseChanged(); }
public static Point[][] SplitFirstDimension(Variable variable) { if (variable == null) { throw new ArgumentNullException("variable"); } if (variable.Rank != 2) { throw new ArgumentException("Only 2d variables are supported"); } int n = variable.GetShape()[0]; int m = variable.GetShape()[1]; Array d = variable.GetData(); Point[][] pts = new Point[n][]; for (int i = 0; i < n; i++) { List <Point> tmp = new List <Point>(m); if (variable.TypeOfData == typeof(float)) { float[,] dt = (float[, ])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) { if (!Double.IsNaN(dt[i, j])) { tmp.Add(new Point(j, dt[i, j])); } } } else { float mv; try { mv = Convert.ToSingle(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to Double: " + exc.Message); mv = float.NaN; } for (int j = 0; j < m; j++) { if (dt[i, j] != mv) { tmp.Add(new Point(j, dt[i, j])); } } } } else if (variable.TypeOfData == typeof(double)) { double[,] dt = (double[, ])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) { if (!Double.IsNaN(dt[i, j])) { tmp.Add(new Point(j, dt[i, j])); } } } else { double mv = Convert.ToDouble(mvObj); try { mv = Convert.ToDouble(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to double: " + exc.Message); mv = double.NaN; } for (int j = 0; j < m; j++) { if (dt[i, j] != mv) { tmp.Add(new Point(j, dt[i, j])); } } } } else { object mv = variable.GetMissingValue(); for (int j = 0; j < m; j++) { object obj = d.GetValue(i, j); if (!Object.Equals(obj, mv)) { tmp.Add(new Point(j, Convert.ToDouble(obj))); } } } pts[i] = tmp.ToArray(); } return(pts); }
public static Point[][] SplitSecondDimension(Variable variable) { if (variable == null) throw new ArgumentNullException("variable"); if (variable.Rank != 2) throw new ArgumentException("Only 2d variables are supported"); int n = variable.GetShape()[1]; int m = variable.GetShape()[0]; Array d = variable.GetData(); Point[][] pts = new Point[n][]; for (int i = 0; i < n; i++) { List<Point> tmp = new List<Point>(m); if (variable.TypeOfData == typeof(float)) { float[,] dt = (float[,])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) if (!Double.IsNaN(dt[j, i])) tmp.Add(new Point(j, dt[j, i])); } else { float mv; try { mv = Convert.ToSingle(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to float: " + exc.Message); mv = float.NaN; } for (int j = 0; j < m; j++) if (dt[j, i] != mv) tmp.Add(new Point(j, dt[j, i])); } } else if (variable.TypeOfData == typeof(double)) { double[,] dt = (double[,])d; object mvObj = variable.GetMissingValue(); if (mvObj == null) { for (int j = 0; j < m; j++) if (!Double.IsNaN(dt[j, i])) tmp.Add(new Point(j, dt[j, i])); } else { double mv; try { mv = Convert.ToDouble(mvObj); } catch (Exception exc) { Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to double: " + exc.Message); mv = double.NaN; } for (int j = 0; j < m; j++) if (dt[j, i] != mv) tmp.Add(new Point(j, dt[j, i])); } } else { object mv = variable.GetMissingValue(); for (int j = 0; j < m; j++) { object obj = d.GetValue(j, i); if (!Object.Equals(obj, mv)) tmp.Add(new Point(j, Convert.ToDouble(obj))); } } pts[i] = tmp.ToArray(); } return pts; }
static void PrintData(Variable v, string range, string format) { try { // Find out which data to take int rank = v.Rank; var shape = v.GetShape(); var origin = rank == 0 ? null : new int[rank]; var count = rank == 0 ? null : new int[rank]; var stride = rank == 0 ? null : new int[rank]; for (int i = 0; i < rank; i++) { origin[i] = 0; count[i] = shape[i]; stride[i] = 1; } if (range != null) { // parse range specification 'from:to' or 'from:step:to' var ranges = range.Split(','); for (int i = 0; i < rank && i < ranges.Length; i++) { var fromto = ranges[i].Split(':'); if (!int.TryParse(fromto[0], out origin[i])) origin[i] = 0; if (origin[i] < 0) origin[i] = 0; if (origin[i] >= shape[i]) origin[i] = shape[i] - 1; if (fromto.Length < 2) count[i] = 1; else { if (!int.TryParse(fromto[fromto.Length - 1], out count[i])) count[i] = 0; if (count[i] == 0 || count[i] >= shape[i]) count[i] = shape[i] - 1; if (count[i] < origin[i]) count[i] = origin[i]; if (fromto.Length > 2) { if (!int.TryParse(fromto[1], out stride[i])) stride[i] = 1; if (stride[i] <= 0) stride[i] = 1; } count[i] = 1 + (count[i] - origin[i]) / stride[i]; } } } // Now get the data Array data = v.GetData(origin, stride, count); if (data == null || (rank > 0 && data.Rank != rank)) Console.Error.WriteLine("No data"); else { int prefix = string.Join(",", Array.ConvertAll(shape, i => i.ToString())).Length + 2; // parse format int cell = 8; string frm = "{0,8}"; if (format != null) { int p = format.IndexOf(':'); if (p < 0) { if (!int.TryParse(format, out cell)) cell = 8; frm = "{0," + cell + "}"; } else { if (!int.TryParse(format.Substring(0, p), out cell)) cell = 8; frm = "{0," + cell + format.Substring(p) + "}"; } } // Now print the data if (rank == 0) // scalar variable { Console.WriteLine(); string item = string.Format(frm, data.GetValue(0)); // check that item fits a cell if (item.Length > cell) item = item.Substring(0, cell - 1) + "#"; Console.Write(" "); Console.Write(item); } else // has rank > 0 (array) { int[] indices = new int[data.Rank]; int[] varIndices = new int[rank]; int bufferWidth = Math.Max(80, Console.BufferWidth); // .. to allow for non-console output string lineSeparator = Environment.NewLine; while (indices[0] < data.GetLength(0)) { // print last dimension Console.Write(lineSeparator); lineSeparator = string.Empty; int lastIndex = data.Rank - 1; indices[lastIndex] = 0; while (indices[lastIndex] < data.GetLength(lastIndex)) { // print line of cells // print prefix of current indices for (int i = 0; i < rank; i++) varIndices[i] = origin[i] + indices[i] * stride[i]; string item = ("[" + string.Join(",", Array.ConvertAll(varIndices, i => i.ToString())) + "]").PadRight(prefix); Console.Write(item); int left = bufferWidth - item.Length; while (left > cell && indices[lastIndex] < data.GetLength(lastIndex)) { item = string.Format(frm, data.GetValue(indices)); // check that item fits a cell if (item.Length > cell) item = item.Substring(0, cell - 1) + "#"; Console.Write(" "); Console.Write(item); left -= item.Length + 1; indices[lastIndex]++; varIndices[lastIndex] += stride[lastIndex]; } Console.WriteLine(); if (indices[lastIndex] < data.GetLength(lastIndex)) lineSeparator = Environment.NewLine; } lastIndex -= 1; // increment recursively other dimensions while (lastIndex >= 0 && ++indices[lastIndex] >= data.GetLength(lastIndex)) { if (lastIndex > 0) indices[lastIndex] = 0; lastIndex -= 1; } } } } Console.WriteLine(); } catch (Exception e) { Console.Error.WriteLine("Error while printing data: " + e.Message); } }
public static void TestDataSet3DVariable(string tableName, LookupColumn3dTest lookupColumn3dTest, LookupColumnTest lookupColumnTest, Variable v) { var columnTest = lookupColumnTest.Invoke(v.Name); if (columnTest.Include) { ColumnTest.TestDataSetVariable(tableName, lookupColumnTest, v); } else { var column3dTest = lookupColumn3dTest.Invoke(v.Name); if (column3dTest.Include) { var actualTruncated = (double[, ,])null; switch (v.TypeOfData.Name.ToString().ToLower()) { case "double": { var actual = (double[, ,])v.GetData(); var count1 = column3dTest.Expected.GetLength(0); var count2 = column3dTest.Expected.GetLength(1); var count3 = column3dTest.Expected.GetLength(2); actualTruncated = new double[count1, count2, count3]; for (var ii = 0; ii < count1; ii++) { for (var jj = 0; jj < count2; jj++) { for (var kk = 0; kk < count3; kk++) { actualTruncated[ii, jj, kk] = actual[ii, jj, kk]; } } } } break; default: throw new Exception("unexpected column type"); } DoColumn3dTest(column3dTest, actualTruncated); } else { Console.WriteLine("Skipping : {0}", v.Name); } } }
public FileMetadata ParseMetaData(DEMFileDefinition fileFormat) { // Data origin is lower left corner try { int[] shape = new int[1]; shape[0] = 2; int ncols = _longVariable.Dimensions.First().Length; int nrows = _latVariable.Dimensions.First().Length; Array longValues = _longVariable.GetData(null, shape); Array latValues = _latVariable.GetData(null, shape); double xllcorner = (double)longValues.GetValue(0); double yllcorner = (double)latValues.GetValue(0); double cellsizex = (double)longValues.GetValue(1) - (double)longValues.GetValue(0); double cellsizey = (double)latValues.GetValue(1) - (double)latValues.GetValue(0); float NODATA_value = -9999f; FileMetadata metadata = new FileMetadata(_filename, fileFormat) { Height = nrows, Width = ncols, PixelScaleX = cellsizex, PixelScaleY = cellsizey }; metadata.pixelSizeX = metadata.PixelScaleX; metadata.pixelSizeY = metadata.PixelScaleY; if (fileFormat.Registration == DEMFileRegistrationMode.Grid) { metadata.DataStartLat = yllcorner; metadata.DataStartLon = xllcorner; metadata.DataEndLat = yllcorner + metadata.Height * metadata.pixelSizeY; metadata.DataEndLon = xllcorner + metadata.Width * metadata.pixelSizeX; metadata.PhysicalStartLat = yllcorner; metadata.PhysicalStartLon = xllcorner; metadata.PhysicalEndLat = metadata.DataEndLat; metadata.PhysicalEndLon = metadata.DataEndLon; } else { metadata.DataStartLat = yllcorner; metadata.DataStartLon = xllcorner; metadata.DataEndLat = yllcorner + (metadata.Height - 1) * metadata.pixelSizeY; metadata.DataEndLon = xllcorner + (metadata.Width - 1) * metadata.pixelSizeX; metadata.PhysicalStartLat = yllcorner - (metadata.PixelScaleY / 2.0); metadata.PhysicalStartLon = xllcorner - (metadata.PixelScaleX / 2.0); metadata.PhysicalEndLat = Math.Round(metadata.DataStartLat + metadata.Height * metadata.pixelSizeY - (metadata.PixelScaleY / 2.0), 10); metadata.PhysicalEndLon = Math.Round(metadata.DataStartLon + metadata.Width * metadata.pixelSizeX - (metadata.PixelScaleX / 2.0), 10); } metadata.SampleFormat = RasterSampleFormat.FLOATING_POINT; metadata.NoDataValue = NODATA_value.ToString(); return(metadata); } catch (Exception) { throw; } }
private IEnumerator <Point> GetPoints() { Array gridA = grid.GetData(); Array dataA = data.GetData(); object gmv = grid.GetMissingValue(); object dmv = data.GetMissingValue(); int len = Math.Min(gridA.Length, dataA.Length); Type gt = grid.TypeOfData; Type dt = data.TypeOfData; if (dt == typeof(double)) { if (gt == typeof(double)) // data: double, grid: double { double[] darr = (double[])dataA; double[] garr = (double[])gridA; for (int i = 0; i < len; i++) { double g = garr[i]; double d = darr[i]; if (g.Equals(gmv) || double.IsNaN(g)) { continue; } if (d.Equals(dmv) || double.IsNaN(d)) { continue; } yield return(new Point(gridValueConverter(g), d)); } } else if (gt == typeof(float)) // data: double, grid: float { double[] darr = (double[])dataA; float[] garr = (float[])gridA; for (int i = 0; i < len; i++) { float g = garr[i]; double d = darr[i]; if (g.Equals(gmv) || float.IsNaN(g)) { continue; } if (d.Equals(dmv) || double.IsNaN(d)) { continue; } yield return(new Point(gridValueConverter(g), d)); } } else // data: double, grid: other { double[] darr = (double[])dataA; for (int i = 0; i < len; i++) { object g = gridA.GetValue(i); if (g.Equals(gmv)) { continue; } double d = darr[i]; if (d.Equals(dmv) || double.IsNaN(d)) { continue; } yield return(new Point(gridValueConverter(g), d)); } } } else if (dt == typeof(float)) { if (gt == typeof(double)) // data: float, grid: double { float[] darr = (float[])dataA; double[] garr = (double[])gridA; for (int i = 0; i < len; i++) { double g = garr[i]; float d = darr[i]; if (g.Equals(gmv) || double.IsNaN(g)) { continue; } if (d.Equals(dmv) || float.IsNaN(d)) { continue; } yield return(new Point(gridValueConverter(g), (double)d)); } } else if (gt == typeof(float)) // data: float, grid: float { float[] darr = (float[])dataA; float[] garr = (float[])gridA; for (int i = 0; i < len; i++) { float g = garr[i]; float d = darr[i]; if (g.Equals(gmv) || float.IsNaN(g)) { continue; } if (d.Equals(dmv) || float.IsNaN(d)) { continue; } yield return(new Point(gridValueConverter(g), (double)d)); } } else // data: float, grid: other { float[] darr = (float[])dataA; for (int i = 0; i < len; i++) { object g = gridA.GetValue(i); if (g.Equals(gmv)) { continue; } float d = darr[i]; if (d.Equals(dmv) || float.IsNaN(d)) { continue; } yield return(new Point(gridValueConverter(g), (double)d)); } } } else { if (gt == typeof(double)) // data: other, grid: double { double[] garr = (double[])gridA; for (int i = 0; i < len; i++) { double g = garr[i]; if (g.Equals(gmv) || double.IsNaN(g)) { continue; } object d = dataA.GetValue(i); if (d.Equals(dmv)) { continue; } yield return(new Point(gridValueConverter(g), Convert.ToDouble(d))); } } else if (gt == typeof(float)) // data: other, grid: float { float[] garr = (float[])gridA; for (int i = 0; i < len; i++) { float g = garr[i]; if (g.Equals(gmv) || float.IsNaN(g)) { continue; } object d = dataA.GetValue(i); if (d.Equals(dmv)) { continue; } yield return(new Point(gridValueConverter(g), Convert.ToDouble(d))); } } else // data: other, grid: other { for (int i = 0; i < len; i++) { object g = gridA.GetValue(i); if (g.Equals(gmv)) { continue; } object d = dataA.GetValue(i); if (d.Equals(dmv)) { continue; } yield return(new Point(gridValueConverter(g), Convert.ToDouble(d))); } } } yield break; }
static void PrintData(Variable v, string range, string format) { try { // Find out which data to take int rank = v.Rank; var shape = v.GetShape(); var origin = rank == 0 ? null : new int[rank]; var count = rank == 0 ? null : new int[rank]; var stride = rank == 0 ? null : new int[rank]; for (int i = 0; i < rank; i++) { origin[i] = 0; count[i] = shape[i]; stride[i] = 1; } if (range != null) { // parse range specification 'from:to' or 'from:step:to' var ranges = range.Split(','); for (int i = 0; i < rank && i < ranges.Length; i++) { var fromto = ranges[i].Split(':'); if (!int.TryParse(fromto[0], out origin[i])) { origin[i] = 0; } if (origin[i] < 0) { origin[i] = 0; } if (origin[i] >= shape[i]) { origin[i] = shape[i] - 1; } if (fromto.Length < 2) { count[i] = 1; } else { if (!int.TryParse(fromto[fromto.Length - 1], out count[i])) { count[i] = 0; } if (count[i] == 0 || count[i] >= shape[i]) { count[i] = shape[i] - 1; } if (count[i] < origin[i]) { count[i] = origin[i]; } if (fromto.Length > 2) { if (!int.TryParse(fromto[1], out stride[i])) { stride[i] = 1; } if (stride[i] <= 0) { stride[i] = 1; } } count[i] = 1 + (count[i] - origin[i]) / stride[i]; } } } // Now get the data Array data = v.GetData(origin, stride, count); if (data == null || (rank > 0 && data.Rank != rank)) { Console.Error.WriteLine("No data"); } else { int prefix = string.Join(",", Array.ConvertAll(shape, i => i.ToString())).Length + 2; // parse format int cell = 8; string frm = "{0,8}"; if (format != null) { int p = format.IndexOf(':'); if (p < 0) { if (!int.TryParse(format, out cell)) { cell = 8; } frm = "{0," + cell + "}"; } else { if (!int.TryParse(format.Substring(0, p), out cell)) { cell = 8; } frm = "{0," + cell + format.Substring(p) + "}"; } } // Now print the data if (rank == 0) // scalar variable { Console.WriteLine(); string item = string.Format(frm, data.GetValue(0)); // check that item fits a cell if (item.Length > cell) { item = item.Substring(0, cell - 1) + "#"; } Console.Write(" "); Console.Write(item); } else // has rank > 0 (array) { int[] indices = new int[data.Rank]; int[] varIndices = new int[rank]; int bufferWidth = Math.Max(80, Console.BufferWidth); // .. to allow for non-console output string lineSeparator = Environment.NewLine; while (indices[0] < data.GetLength(0)) { // print last dimension Console.Write(lineSeparator); lineSeparator = string.Empty; int lastIndex = data.Rank - 1; indices[lastIndex] = 0; while (indices[lastIndex] < data.GetLength(lastIndex)) { // print line of cells // print prefix of current indices for (int i = 0; i < rank; i++) { varIndices[i] = origin[i] + indices[i] * stride[i]; } string item = ("[" + string.Join(",", Array.ConvertAll(varIndices, i => i.ToString())) + "]").PadRight(prefix); Console.Write(item); int left = bufferWidth - item.Length; while (left > cell && indices[lastIndex] < data.GetLength(lastIndex)) { item = string.Format(frm, data.GetValue(indices)); // check that item fits a cell if (item.Length > cell) { item = item.Substring(0, cell - 1) + "#"; } Console.Write(" "); Console.Write(item); left -= item.Length + 1; indices[lastIndex]++; varIndices[lastIndex] += stride[lastIndex]; } Console.WriteLine(); if (indices[lastIndex] < data.GetLength(lastIndex)) { lineSeparator = Environment.NewLine; } } lastIndex -= 1; // increment recursively other dimensions while (lastIndex >= 0 && ++indices[lastIndex] >= data.GetLength(lastIndex)) { if (lastIndex > 0) { indices[lastIndex] = 0; } lastIndex -= 1; } } } } Console.WriteLine(); } catch (Exception e) { Console.Error.WriteLine("Error while printing data: " + e.Message); } }