Rewriting KVN files into XML

Dear (Python) Orekit users,

I would like to rewrite CCSDS files from KVN onto XML format.
In TdmParserTest (Java code), I saw that once I have a Tdm object (named “original”), I could simply do that:

final CharArrayWriter caw = new CharArrayWriter();
final Generator generator = new KvnGenerator(caw, TdmWriter.KVN_PADDING_WIDTH, "dummy", 60);
new WriterBuilder().withRangeUnitsConverter(null).buildTdmWriter().writeMessage(generator, original);

However CharArrayWriter is a Java object, not an Orekit one. Is there a way to create it from Python anyway?


Hi Romain,

Maybe this answer from Aerow610 can help you: KvnGenerator requires unavailable Java classes - #4 by aerow610

Best regards,

Hi Bryan,

thanks for the link. This seems to do the trick for the pure Java object indeed. However I’m now getting an error saying that a TdmWriter does not have an attribute called writeMessage, but it’s a method so I don’t understand. Here is the code:

data_source = DataSource(file_name_and_path)
parser = ParserBuilder().buildTdmParser()
tdm_kvn: Tdm = Tdm.cast_(parser.parseMessage(data_source))
indent = 4
write_units = False
generator = XmlGenerator(StringBuilder(), indent, file_name_and_path, write_units)
tdm_writer: TdmWriter = TdmWriter.cast_(WriterBuilder().withRangeUnitsConverter(None).buildTdmWriter())
tdm_writer.writeMessage(generator, tdm_kvn)

Hi Romain,

It seems to be an issue related to Python. Indeed, the writeMessage(...) method is in the MessageWriter interface, implemented by the TdmWriter. However, as the method is by default in the interface and not overridden by the TdmWriter, maybe the problem is due to the cast. Did you tried this:

writer = WriterBuilder().withRangeUnitsConverter(None).buildTdmWriter()
writer.writeMessage(generator, tdm_kvn)


Hi Bryan,

I tried both options (w/ and w/o the cast), none works.


I’ll have to open my Python IDE and test it
It’s been a while since I last used Orekit Python… :slight_smile:

I will keep you informed!


Hi @bcazabonne,

I changed the cast to TdmWriter with MessageWriter and I don’t get any errors any more.
However, I have no clue where the file has been written…
It is not clear to me at all where the MessageWriter operates.


Hi @Serrof,

In practice, the message is appended to some Appendable passed as the first argument when building the Generator used.

With your code

generator = XmlGenerator(StringBuilder(), indent, file_name_and_path, write_units)

the TdM has disappeared into limbo with the StringBuilder.

You should use something like a FileWriter instead of a StringBuilder if you want to write the TdM to a file.


Thank you @pascal.parraud for the clarifications!
By rereading the other discussion cited by Bryan, I realized I was actually missing the last part in Python post-processing the StringBuilder!

Everything works fine now!