/// <summary> /// Converts a csv file into odc. /// Multiple entries are stored as different types. /// </summary> /// <param name="csv">The CSV file to read</param> /// <param name="highestZone">The highest numbered zone</param> /// <param name="times">How many time periods to account for</param> /// <param name="types">How many different types of data to store per OD</param> /// <param name="odc">The file to write to</param> /// <param name="header">Does the CSV have a header?</param> /// <param name="offsetTimes">The offset into the times</param> /// <param name="offsetType">Should we offset the CSV's information in the types?</param> public void LoadCSVTimes(string csv, bool header, int offsetTimes, int offsetType) { // Gain access to the files StreamReader reader = new StreamReader(new FileStream(csv, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan)); FilesLoaded.Add(new DimensionInfo(csv, offsetType, offsetTimes, false, header, true)); string line; int injectIndex = this.Times * offsetType + offsetTimes; if (header) { reader.ReadLine(); } // Read the line from the CSV while ((line = reader.ReadLine()) != null) { // Calculate where to store the data int pos, next; int o = FastParse.ParseInt(line, 0, (pos = line.IndexOf(','))); int d = FastParse.ParseInt(line, pos + 1, (next = line.IndexOf(',', pos + 1))); pos = next + 1; int length = line.Length; // if we haven't stored anything for this O before, load some memory for it now if (Data[o] == null) { Data[o] = new float[HighestZone][]; HasData[o] = new bool[HighestZone]; for (int k = HighestZone - 1; k >= 0; k--) { Data[o][k] = new float[AmmountOfData]; } for (int k = 0; k < HighestZone; k++) { HasData[o][k] = false; } } int entry = 0; for (int i = pos; i < length; i++) { if (line[i] == ',') { float num = FastParse.ParseFixedFloat(line, pos, i - pos); this.Data[o][d][injectIndex + entry] = num; this.HasData[o][d] = true; entry++; pos = i + 1; } } if (pos < length) { this.Data[o][d][injectIndex + entry] = FastParse.ParseFixedFloat(line, pos, length - pos); } } // Close our access to the file streams reader.Close(); }
/// <summary> /// Converts a csv file into odc. /// Multiple entries are stored as different types. /// </summary> /// <param name="csv">The CSV file to read</param> /// <param name="highestZone">The highest numbered zone</param> /// <param name="times">How many time periods to account for</param> /// <param name="types">How many different types of data to store per OD</param> /// <param name="odc">The file to write to</param> /// <param name="header">Does the CSV have a header?</param> /// <param name="offsetTimes">The offset into the times</param> /// <param name="offsetType">Should we offset the CSV's information in the types?</param> public void LoadCSVTimes(string csv, bool header, int offsetTimes, int offsetType) { // Gain access to the files using (StreamReader reader = new StreamReader(new FileStream(csv, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan))) { string line; var ammountOfData = this.Types * this.Times; int injectIndex = this.Times * offsetType + offsetTimes; if (header) { reader.ReadLine(); } // Read the line from the CSV while ((line = reader.ReadLine()) != null) { // Calculate where to store the data int pos, next; int o = FastParse.ParseInt(line, 0, (pos = line.IndexOf(','))); int d = FastParse.ParseInt(line, pos + 1, (next = line.IndexOf(',', pos + 1))); pos = next + 1; int length = line.Length; int entry = 0; var position = Data[o, d]; if (position == null) { Data[o, d] = position = new float[ammountOfData]; } for (int i = pos; i < length; i++) { if (line[i] == ',') { position[injectIndex + entry] = FastParse.ParseFixedFloat(line, pos, i - pos); entry++; pos = i + 1; } } if (pos < length) { position[injectIndex + entry] = FastParse.ParseFixedFloat(line, pos, length - pos); } } // Close our access to the file streams } }
/// <summary> /// Converts a csv file into odc. /// </summary> /// <param name="csv">The CSV file to parse</param> /// <param name="highestZone">The highest numbered zone</param> /// <param name="types">The number of types of data per recored</param> /// <param name="zfc">The ZFC we are to produce</param> /// <param name="header">Does this csv file contain a header?</param> /// <param name="offset">How much other data comes before our new entries?</param> public static void CsvToZfc(string csv, int highestZone, int types, string zfc, bool header, int offset) { StreamReader reader = new StreamReader(new FileStream(csv, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan)); BinaryWriter writer = new BinaryWriter(new FileStream(zfc, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 0x1000, FileOptions.RandomAccess), Encoding.Default); string line; writer.Write(highestZone); writer.Write(0); writer.Write(types); byte[] data = new byte[types * 4]; if (header) { reader.ReadLine(); } while ((line = reader.ReadLine()) != null) { int position; int origin = FastParse.ParseInt(line, 0, (position = line.IndexOf(','))); position++; writer.BaseStream.Seek((sizeof(int) * 3) + ((origin * types) + offset) * sizeof(float), SeekOrigin.Begin); // It is faster to store the length before itterating over it int length = line.Length; int ammount = 0; for (int i = position; i < length; i++) { if (line[i] == ',') { LoadData(data, FastParse.ParseFloat(line, position, i), ref ammount); position = i + 1; } } LoadData(data, FastParse.ParseFloat(line, position, line.Length), ref ammount); writer.Write(data, 0, ammount); } reader.Close(); writer.Close(); }