public static int Main(string[] args) { if (args.Length < 8) { System.Console.WriteLine("Plese run program:\n program <preprocessed_file> <M> <minX> <maxX> <minY> <maxY> <bin_size> <selection>"); return(1); } else { long NumberOfDataReads = 0; // read user parameters string fileName = args[0]; int M = int.Parse(args[1]); // 38 461 points => 999 986 B => close to 1 M int maxSizeOfMemoryUsage = 999986 * M; double minX = Double.Parse(args[2]); double maxX = Double.Parse(args[3]); double minY = Double.Parse(args[4]); double maxY = Double.Parse(args[5]); int binSize = int.Parse(args[6]); char selection = char.Parse(args[7].ToLower()); SelectionType selectionType = SelectionType.I; if (selection == 'i') { selectionType = SelectionType.I; } else if (selection == 'z') { selectionType = SelectionType.Z; } // create histogram with bin size Histogram histogram = new Histogram(binSize); // create empty summary StatisticalSummary statisticalSummary = new StatisticalSummary(); try { using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { byte[] chunk = reader.ReadBytes(maxSizeOfMemoryUsage); NumberOfDataReads++; int numOfPoints = 38461 / 2; bool foundX = false; bool foundlastX = false; int indexOfFirstX = 0; while (chunk.Length > 0) { int index = numOfPoints * 26; if (foundX) { index = 0; } while (index < chunk.Length && index >= 0 && foundX == false) { double lastX = BitConverter.ToDouble(chunk, maxSizeOfMemoryUsage - 26); if (lastX < minX) { break; } double x = BitConverter.ToDouble(chunk, index); // 8 B index += 26; if (x >= minX && x <= maxX) { foundX = true; indexOfFirstX = index; } else if (x < minX) { numOfPoints = numOfPoints / 2; index += numOfPoints * 26; } else if (x > maxX) { numOfPoints = numOfPoints / 2; index -= numOfPoints * 26; } } if (foundX) { index -= 26; while (index >= 0) { double x = BitConverter.ToDouble(chunk, index); // 8 B if (x >= minX && x <= maxX) { indexOfFirstX = index; } else { break; } index -= 26; } index += 26; } if (foundX) { while (index < chunk.Length && foundlastX == false) { double x = BitConverter.ToDouble(chunk, index); // 8 B index += 8; double y = BitConverter.ToDouble(chunk, index); // 8 B index += 8; double z = BitConverter.ToDouble(chunk, index); // 8 B index += 8; short i = BitConverter.ToInt16(chunk, index); // 2 B index += 2; //------ // 26 B if (x >= minX && x <= maxX) { if (y >= minY && y <= maxY) { if (selectionType == SelectionType.I) { histogram.InsertValue(i); } else if (selectionType == SelectionType.Z) { histogram.InsertValue(z); } } } else { foundlastX = true; } } } if (foundlastX) { break; } chunk = reader.ReadBytes(maxSizeOfMemoryUsage); NumberOfDataReads++; } } } catch (IOException e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } // statistics histogram.CreateIntervals(); histogram.MakeSummary(statisticalSummary); // output statisticalSummary.PrintReport(histogram.PointsCount, histogram.K, NumberOfDataReads); return(0); } }
public static void GetDataFromBinaryFile(string fileName, double minX, double maxX, double minY, double maxY, int size, bool isI, int M) { double X; byte[] chunk; using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { // ------------- BINARY SEARCH ------------- // ------------- FIND RIGHT CHUNK ----------- long wholeSize = reader.BaseStream.Length; double XFirst, XLast; int positionChunk = (int)(wholeSize / 2); // middle of the middle chunk int changeChunk = positionChunk; bool isFirst = true; do { changeChunk = changeChunk / 2; changeChunk = changeChunk - changeChunk % size; //changeChunk = changeChunk - changeChunk % 26; if (changeChunk < size) { changeChunk = size; } positionChunk -= size / 2; // get to the begening of the middle chunk // if M is even, in first move we placed in the middle of node (move to half left needed) if (M % 2 == 0 && isFirst == true) { isFirst = false; // move half to the left positionChunk -= 13; } reader.BaseStream.Position = positionChunk; chunk = reader.ReadBytes(size); XFirst = BitConverter.ToDouble(chunk, 0); // get first element in chunk XLast = BitConverter.ToDouble(chunk, size - 26); // get last element in chunk if (XFirst <= minX && minX < XLast) // right place { break; } else if (XFirst >= minX) // we are too far { positionChunk += size / 2; positionChunk -= changeChunk; } else { positionChunk += size / 2; positionChunk += changeChunk; } } while (true); // ------------- BINARY SEARCH ------------- // ------------- FIND RIGHT CHUNK ----------- // END // we are in right chunk //lets find where is our wanted boundaries (binary tree again) int change = size / 2; int position = size / 2; // middle position = position + position % 26; do { change = change / 2; change = change - change % 26; if (change < 26) { change = 26; } // try to find boundaries X = BitConverter.ToDouble(chunk, position); if (minX <= X && X < maxX) { // We are in the right place now, go up do { position -= 26; X = BitConverter.ToDouble(chunk, position); if (X < minX) { break; } } while (true); //We are on MINIMUM (one below minimum) do { if (X > maxX) { position -= 26; return; } if (position >= size - 26) { chunk = reader.ReadBytes(size); position = -26; } // ADDING POINTS position += 26; X = BitConverter.ToDouble(chunk, position); double value = 0; double Y = BitConverter.ToDouble(chunk, position + 8); if (minY <= Y && Y <= maxY) { // ADD Z OR I if (isI) { value = BitConverter.ToInt16(chunk, position + 24); // I } else { value = BitConverter.ToDouble(chunk, position + 16); // Z } histogram.InsertValue(value); } } while (true); } else if (maxX <= X) // we are too far { position -= change; } else { position += change; } } while (true); } }