-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_crc.py
48 lines (41 loc) · 1.22 KB
/
fix_crc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/bin/env python
#Simo Siiria (simo.siiria at fmi.fi)
#
# Usage: fix_crc <filename>
#
#-adds/updates CRC check number after each line of the given file.
#-assumes lines are of format "xxxx(yyy)" and/or "xxxx(yyy) [crcnum]"
#-if old CRC num in [] exists, removes it and updates it.
#-if old CRC num in [] doesn't exist after line, adds it.
#
#-usable for apex floats mission files.
#
#Requires:
#CRCCITT.py by Cristian NAVALICI cristian.navalici at gmail dot com
import numpy
import time
#import pylab
import getopt
import sys
import os
import re
from CRCCCITT import CRCCCITT
def add_crc(string):
crc = CRCCCITT('1D0F') #This is the crc check format used by argo float.
tmp=string+' ['+hex(crc.calculate(string)).upper()+']\n'
tmp=re.sub('\[0X','[0x',tmp) #just a small tuning to get the format stay similar to original.
return tmp
def main(argv):
if (len(argv)!=1):
print( "usage 'fix_crc.py <filename>'")
exit(1)
lines=open(argv[0]).readlines()
new_lines=[]
for l in lines:
tmp=re.sub('\[.*\].*$','',l) #get rid of possible old crc
tmp=tmp.rstrip() #and extra spaces and linefeed
tmp=add_crc(tmp) #then add new crc
new_lines.append(tmp)
outp=open(argv[0],'w')
outp.writelines(new_lines)
main(sys.argv[1:])