title: How to Decode Compressed Binary .RAW Files from LTspice with python
body: <p>I am trying to read from .raw LTSPice files. In the Control panel of LTSPice, there is a possibility to switch between ASCII and binary compression. ASCII is readable without a problem, but it has drawbacks in size and performance, and that's the reason why I want to use binary compression. There is a possibility to select 2nd order compression, and that's the target.
  LTSpice gives as output .raw files, and ASCII file has values of voltages and currents, but in a binary file, there are symbols that I couldn't decode.</p>
  
  <p>First I tried to open .raw files with notepad++. Notpad++ shows in the bottom left UTF-8 when I open ASCII .raw files and UCS-2 Little Endian when open binary decompressed .raw files.
  I tried with different encoding such as UTF-16le, ISO-8859-1...actually I print all codecs and tried with them:</p>
  
  <pre><code>import codecs
  print(dir(codecs))
  </code></pre>
  
  <p>Also, I tried without BOM. I found this and it gives no results for me:</p>
  
  <pre><code>import codecs
  encoded_text = open('Circuit_binary_Transient.raw', 'rb').read()    #should read in binary mode to get the BOM correctly
  bom= codecs.BOM_UTF16_LE                                      #print dir(codecs) for other encodings
  assert encoded_text.startswith(bom)                           
  encoded_text= encoded_text[len(bom):]                         #strip away 
  decoded_text= encoded_text.decode('utf-16le')
  print(decoded_text)
  </code></pre>
  
  <p>I found that Zlib is common compression and I tried to find out on which positions in my file I have compression, but whitout results.
  I used:</p>
  
  <pre><code>import zlib
  from glob import glob
  
  def zipstreams(filename):
      """Return all zip streams and their positions in file."""
      with open(filename, 'rb') as fh:
          data = fh.read()
      i = 0
      while i &lt; len(data):
          try:
              zo = zlib.decompressobj()
              yield i, zo.decompress(data[i:])
              i += len(data[i:]) - len(zo.unused_data)
          except zlib.error:
              i += 1
  
  for i, data in zipstreams("Circuit_binary_Transient.raw.raw"):
      print (i, len(data))
  </code></pre>
  
  <p>I always get 0 for len(data).</p>
  
  <p>I found this script, but without the implementation of binary .raw files decoding:
   <a href="http://www2.ee.unsw.edu.au/~tlehmann/ltspy.py" rel="nofollow noreferrer">http://www2.ee.unsw.edu.au/~tlehmann/ltspy.py</a><br>
  Also, I found the solution for Matlab, LTSpice2Matlab by Paul Wagner (ltspice reader for Matlab), but I need it for python, and from that particular script I can't find which encoding is used. I found that for binary decompressed files Paul Wagner used interpolation to get more points and then to convert to a readable format.</p>
  
  <p>Actually, I used this code to read .raw files:</p>
  
  <pre><code>data = fo.read() # Binary data read
  fo.close()
  line = str(data, encoding='utf-16le')
  print(line)
  </code></pre>
  
  <p>When I used ASCII .raw files and UTF8 then I have text at the end, but when I used binary compressed .raw files I cant find how to decode file in readable format.</p>
  
  <p>This is what I have currently in .raw file from LTSpice (After text "Binary:"):</p>
  
  <pre><code>栋㲚朎㲚鸞㕼鸞㕼鸞땼䕘퇠≄뻕襾㷨⁄㷨⡜㚾⡜㚾⡜뚾䕘퇠≄㻥䏨㹔ᙘ㹓㜬㜬뜬岓逇Ⳡ㻵◲㻇㯋㻃㞟㞟랟ጟ箽㼄籼㼯덌㼩Ӏ㠋Ӏ㠋Ӏ렋괄蝄㼎ڕ㽦럍㽢먵㠹먵㠹먵령귴脍侧㼑з㽴鍰㽲럜㡆럜㡆럜롆쵺튆㼒㽺땺㽹迀㡌迀㡌迀롌偵姏䰦㼔㽾枎㽽雈㡏雈㡏雈롏䰼ᬇⅤ㼖쵅㽿ᦊ㽾⢖㡐⢖㡐⢖롐暾좳죯㼗챭㽼錒㽺䕈㡍䕈㡍䕈롍얳✀礦㼚嘻㽰㽫㿃㡁㿃㡁㿃롁먬쬥㼞㽉뙵㽁낁㠞낁㠞낁렞袨�봜㼢睿㻔瘵㻄㞠㞠랠嘈䆬흞㼦㷧븥譻븠蒱뜃蒱뜃蒱㜃빝袏噥㼪뼝뼙㼆럼㼆럼㼆㟼玵嬆㼬ﵠ뽐】뽏멬렩멬렩멬㠩떋Ҕ덭㼭葔뽩뽨챢렾챢렾챢㠾넼ꧼ㼮쏐뽵뼃뽵傴롉傴롉傴㡉뮷惵㼯툡뽻뽻婇롎婇롎婇㡎�ﶻሣ㼰姢뽿ᚶ뽿롐롐㡐誔
  </code></pre>
  
  <p>I expect to decode and read this, and I suppose that I should get the same as in ASCII .raw files, such as:</p>
  
  <pre><code>0       0.000000000000000e+000  
      1.884843971540818e-002  
      1.884796850054908e-002  
      9.423984250366810e-007  
      9.423984250274539e-007  
      -9.423984250328532e-007  
  1       3.951925877448456e-008  
      1.959322256186405e-002  
      1.959271263253092e-002  
      9.796356316741367e-007  
      9.796356316265457e-007  
      -9.796356317259836e-007  
  2       1.185577763234539e-007  
      2.108275522427772e-002  
      2.108216492289399e-002  
      1.054108246213077e-006  
      1.054108246144700e-006  
      -1.054108246245633e-006
  </code></pre>
  
  <p>I will appreciate any help. The goal is to read a compressed binary .raw LTSpice file.</p>