Skip to content

Commit

Permalink
made timestamp parsing from .osi optional and clarify difference betw…
Browse files Browse the repository at this point in the history
…een required and optional metadata

Signed-off-by: Timm Ruppert <[email protected]>
  • Loading branch information
TimmRuppert committed Dec 2, 2024
1 parent d009ea1 commit bd3a708
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
33 changes: 19 additions & 14 deletions examples/convert_osi2mcap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@
#include "osi_trafficcommandupdate.pb.h"
#include "osi_trafficupdate.pb.h"

std::string ExtractTimestampFromFileName(const std::filesystem::path& file_path) {
std::optional<std::string> ExtractTimestampFromFileName(const std::filesystem::path& file_path) {
// Get first 16 characters which should be the timestamp
auto possible_timestamp = file_path.filename().string().substr(0, 16);

// Parse the timestamp using std::get_time
tm tm_struct = {};
std::istringstream string_stream(possible_timestamp);
string_stream >> std::get_time(&tm_struct, "%Y%m%dT%H%M%SZ");
// Check if parsing was successful

// Return nullopt if parsing failed
if (string_stream.fail()) {
std::cerr << "ERROR: Failed to parse timestamp.\n Only files following the recommended OSI .osi naming convention can be converted. Expected format: YYYYMMDDTHHMMSSZ";
exit(1);
return std::nullopt;
}

std::cout << "Assuming timestamp for required MCAP metadata 'zero_time' and 'timestamp' to be : " << possible_timestamp << std::endl;
return possible_timestamp;
// Format the timestamp in the by OSI specified mcap metadata format for the zero_time field
std::ostringstream formatted_timestamp;
formatted_timestamp << std::put_time(&tm_struct, "%Y-%m-%dT%H:%M:%SZ");

std::cout << "Found timestamp for MCAP metadata 'zero_time' from tracefile name: " << formatted_timestamp.str() << std::endl;
return formatted_timestamp.str();
}

const std::unordered_map<osi3::ReaderTopLevelMessage, const google::protobuf::Descriptor*> kMessageTypeToDescriptor = {
Expand Down Expand Up @@ -169,14 +173,15 @@ int main(const int argc, const char** argv) {
return 1;
}

// according to the OSI specification mcap must contain a timestamp and zero_time metadata entry
// try to parse the file timestamp from the .osi file name (as it should follow the recommended OSI naming conventions)
const auto timestamp_from_osi_file = ExtractTimestampFromFileName(options->input_file_path);

auto required_metadata = osi3::MCAPTraceFileWriter::PrepareRequiredFileMetadata();
required_metadata.metadata["description"] = "Converted from " + options->output_file_path.string();
required_metadata.metadata["zero_time"] = timestamp_from_osi_file;
if (!trace_file_writer.AddFileMetadata(required_metadata)) {
// add required and optional metadata to the net.asam.osi.trace metadata record
auto net_asam_osi_trace_metadata = osi3::MCAPTraceFileWriter::PrepareRequiredFileMetadata();
// Add optional metadata to the net.asam.osi.trace metadata record, as recommended by the OSI specification.
net_asam_osi_trace_metadata.metadata["description"] = "Converted from " + options->output_file_path.string(); // optional field
net_asam_osi_trace_metadata.metadata["creation_time"] = osi3::MCAPTraceFileWriter::GetCurrentTimeAsString(); // optional field
if (const auto timestamp_from_osi_file = ExtractTimestampFromFileName(options->input_file_path)) {
net_asam_osi_trace_metadata.metadata["zero_time"] = timestamp_from_osi_file.value(); // optional field
}
if (!trace_file_writer.AddFileMetadata(net_asam_osi_trace_metadata)) {
std::cerr << "Failed to add required metadata to trace_file." << std::endl;
exit(1);
}
Expand Down
13 changes: 7 additions & 6 deletions examples/example_mcap_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ int main(int argc, const char** argv) {
std::cout << "Creating trace_file at " << trace_file_path << std::endl;
trace_file_writer.Open(trace_file_path);

// add OSI-specification mandatory metadata for the entire trace file
auto required_metadata = osi3::MCAPTraceFileWriter::PrepareRequiredFileMetadata();
required_metadata.metadata["description"] = "Example mcap trace file created with the ASAM OSI utilities library."; // optional
required_metadata.metadata["creation_time"] = osi3::MCAPTraceFileWriter::GetCurrentTimeAsString(); // optional
required_metadata.metadata["authors"] = "Jane Doe, John Doe"; // optional
if (!trace_file_writer.AddFileMetadata(required_metadata)) {
// add required and optional metadata to the net.asam.osi.trace metadata record
auto net_asam_osi_trace_metadata = osi3::MCAPTraceFileWriter::PrepareRequiredFileMetadata();
// Add optional metadata to the net.asam.osi.trace metadata record, as recommended by the OSI specification.
net_asam_osi_trace_metadata.metadata["description"] = "Example mcap trace file created with the ASAM OSI utilities library."; // optional field
net_asam_osi_trace_metadata.metadata["creation_time"] = osi3::MCAPTraceFileWriter::GetCurrentTimeAsString(); // optional field
net_asam_osi_trace_metadata.metadata["authors"] = "Jane Doe, John Doe"; // optional field
if (!trace_file_writer.AddFileMetadata(net_asam_osi_trace_metadata)) {
std::cerr << "Failed to add required metadata to trace_file." << std::endl;
exit(1);
}
Expand Down

0 comments on commit bd3a708

Please sign in to comment.