/// <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> /// Loads the data from an emme2 311 file into a ODC /// </summary> /// <param name="emme2File">The emm2 file to read from</param> /// <param name="highestZone">The highest numbered zone</param> /// <param name="times">The ammount of distinct times</param> /// <param name="types">The number of types of variables to store per time period</param> /// <param name="odc">Where to save this data</param> /// <param name="offsetTimes">The time offset to use</param> /// <param name="offsetType">The type offset to use</param> public void LoadEMME2(string emme2File, int offsetTimes, int offsetType) { string line; int pos; var ammountOfData = this.Types * this.Times; // do this because highest zone isn't high enough for array indexes using (StreamReader reader = new StreamReader(new FileStream(emme2File, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan))) { int injectIndex = this.Times * offsetType + offsetTimes; while ((line = reader.ReadLine()) != null) { if (line.Length > 0 && line[0] == 'a') { break; } } while ((line = reader.ReadLine()) != null) { int length = line.Length; // don't read blank lines if (length < 7) { continue; } int o = FastParse.ParseFixedInt(line, 0, 7); pos = 7; while (pos + 8 <= length) { int d = FastParse.ParseFixedInt(line, pos, 7); var data = Data[o, d]; if (data == null) { Data[o, d] = data = new float[ammountOfData]; } if (line[pos + 7] == ':') { data[injectIndex] = FastParse.ParseFixedFloat(line, pos + 8, 5); } else { data[injectIndex] = FastParse.ParseFixedFloat(line, pos + 8, (length > ((pos + 8) + 9) ? 9 : (length - (pos + 8)))); } pos += 13; } } } }
/// <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(); }
/// <summary> /// Loads the data from an emme2 311 file into a ODC /// </summary> /// <param name="emme2File">The emm2 file to read from</param> /// <param name="highestZone">The highest numbered zone</param> /// <param name="times">The ammount of distinct times</param> /// <param name="types">The number of types of variables to store per time period</param> /// <param name="odc">Where to save this data</param> /// <param name="offsetTimes">The time offset to use</param> /// <param name="offsetType">The type offset to use</param> public void LoadEMME2(string emme2File, int offsetTimes, int offsetType) { string line; int pos; // do this because highest zone isn't high enough for array indexes HighestZone += 1; using (StreamReader reader = new StreamReader(new FileStream(emme2File, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan))) { FilesLoaded.Add(new DimensionInfo(emme2File, offsetType, offsetTimes, true, false, false)); int injectIndex = this.Times * offsetType + offsetTimes; while ((line = reader.ReadLine()) != null) { if (line.Length > 0 && line[0] == 'a') { break; } } while ((line = reader.ReadLine()) != null) { int length = line.Length; // don't read blank lines if (length < 7) { continue; } int o = FastParse.ParseFixedInt(line, 0, 7); if (o > Data.Length) { continue; } if (Data[o] == null) { Data[o] = new float[HighestZone][]; for (int k = HighestZone - 1; k >= 0; k--) { Data[o][k] = new float[AmmountOfData]; } HasData[o] = new bool[HighestZone]; for (int i = 0; i < HighestZone; i++) { HasData[o][i] = false; } } pos = 7; while (pos + 13 <= length) { int d = FastParse.ParseFixedInt(line, pos, 7); if (d < Data.Length) { if (line[pos + 7] == ':') { Data[o][d][injectIndex] = FastParse.ParseFixedFloat(line, pos + 8, 5); } else { Data[o][d][injectIndex] = FastParse.ParseFixedFloat(line, pos + 8, 9); } HasData[o][d] = true; } pos += 13; } } } }