packetassemble.py
· 3.3 KiB · Python
Исходник
#!/usr/bin/env python
import sys, struct, os
HEADERTYPE_MAP = {
0: "Primary Header",
1: "Image Structure",
2: "Image Navigation",
3: "Image Data Function",
4: "Annotation",
5: "Timestamp",
6: "Acililary Text",
7: "Key Header",
128: "Segment Identification",
129: "NOAA Specific",
130: "Header Structured Record"
}
for i in range(8,128):
HEADERTYPE_MAP[i] = "Reserved"
for i in range(131,256):
HEADERTYPE_MAP[i] = "Reserved"
filename = "channels/53/1702_0_1311_1.lrit"
f = open(filename, "r")
fsize = os.path.getsize(filename)
readbytes = 0
t = 0
def readHeader(f):
global t
type = ord(f.read(1))
size = f.read(2)
size = struct.unpack(">H", size)[0]
data = f.read(size-3)
t += size
if type == 0:
filetypecode, headerlength, datalength = struct.unpack(">BIQ", data)
print "Header type: %s (%s) File Type Code: %s Header Length %s Data Field Length: %s" %(type, HEADERTYPE_MAP[type], filetypecode, headerlength, datalength)
elif type == 1:
bitsperpixel, columns, lines, compression = struct.unpack(">BHHB", data)
print "Image Structure Header: "
print " Bits Per Pixel: %s" %bitsperpixel
print " Columns: %s" %columns
print " Lines: %s" %lines
print " Compression: %s" %compression
elif type == 2:
projname, cfac, lfac, coff, loff = struct.unpack(">32sIIII", data)
print "Image Navigation Record"
print " Projection Name: %s" %projname
print " Column Scaling Factor: %s" %cfac
print " Line Scaling Factor: %s" %lfac
print " Column Offset: %s" %coff
print " Line Offset: %s" %loff
elif type == 3:
print "Image Data Function Record"
print " Data: {HIDDEN}"
#print " Data: \n%s" %data
elif type == 4:
print "Annotation Record"
print " Data: %s" %data
elif type == 5:
print "Timestamp Record"
days, ms = struct.unpack(">HI", data[1:])
print " Delta from 1 January 1958"
print " Days: %s" %days
print " Miliseconds: %s" %ms
elif type == 6:
print "Ancillary Text"
print " Data: %s" %data
elif type == 7:
print "Key Header"
print " Data: %s" %data
elif type == 128:
print size
imageid, sequence, startcol, startline, maxseg, maxcol, maxrow = struct.unpack(">7H", data)
print "Segment Identification Header"
print " Image Id: %s" %imageid
print " Sequence: %s" %sequence
print " Start Column: %s" %startcol
print " Start Line: %s" %startline
print " Number of Segments: %s" %maxseg
print " Width: %s" %maxcol
print " Height: %s" %maxrow
elif type == 129:
print "NOAA Specific Header"
signature, productId, productSubId, parameter, compression = struct.unpack(">4sHHHB", data)
print " Signature: %s" %signature
print " Product ID: %s" %productId
print " Product SubId: %s" %productSubId
print " Parameter: %s" %parameter
print " Compression: %s" %compression
elif type == 130:
print "Header Structured Record"
print " Data: %s" % data
else:
print "Type not mapped: %s" % type
print ""
data = f.read(10)
filecounter, Transportlength = struct.unpack(">HQ", data)
print "File Counter: %s Transport Size: %s" %(filecounter, Transportlength)
readHeader(f)
readHeader(f)
readHeader(f)
readHeader(f)
readHeader(f)
readHeader(f)
readHeader(f)
readHeader(f)
readHeader(f)
readHeader(f)
print t
f.close()
| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys, struct, os |
| 4 | |
| 5 | HEADERTYPE_MAP = { |
| 6 | 0: "Primary Header", |
| 7 | 1: "Image Structure", |
| 8 | 2: "Image Navigation", |
| 9 | 3: "Image Data Function", |
| 10 | 4: "Annotation", |
| 11 | 5: "Timestamp", |
| 12 | 6: "Acililary Text", |
| 13 | 7: "Key Header", |
| 14 | 128: "Segment Identification", |
| 15 | 129: "NOAA Specific", |
| 16 | 130: "Header Structured Record" |
| 17 | } |
| 18 | |
| 19 | for i in range(8,128): |
| 20 | HEADERTYPE_MAP[i] = "Reserved" |
| 21 | |
| 22 | for i in range(131,256): |
| 23 | HEADERTYPE_MAP[i] = "Reserved" |
| 24 | |
| 25 | filename = "channels/53/1702_0_1311_1.lrit" |
| 26 | |
| 27 | f = open(filename, "r") |
| 28 | fsize = os.path.getsize(filename) |
| 29 | readbytes = 0 |
| 30 | t = 0 |
| 31 | |
| 32 | def readHeader(f): |
| 33 | global t |
| 34 | type = ord(f.read(1)) |
| 35 | size = f.read(2) |
| 36 | size = struct.unpack(">H", size)[0] |
| 37 | data = f.read(size-3) |
| 38 | t += size |
| 39 | if type == 0: |
| 40 | filetypecode, headerlength, datalength = struct.unpack(">BIQ", data) |
| 41 | print "Header type: %s (%s) File Type Code: %s Header Length %s Data Field Length: %s" %(type, HEADERTYPE_MAP[type], filetypecode, headerlength, datalength) |
| 42 | elif type == 1: |
| 43 | bitsperpixel, columns, lines, compression = struct.unpack(">BHHB", data) |
| 44 | print "Image Structure Header: " |
| 45 | print " Bits Per Pixel: %s" %bitsperpixel |
| 46 | print " Columns: %s" %columns |
| 47 | print " Lines: %s" %lines |
| 48 | print " Compression: %s" %compression |
| 49 | |
| 50 | elif type == 2: |
| 51 | projname, cfac, lfac, coff, loff = struct.unpack(">32sIIII", data) |
| 52 | print "Image Navigation Record" |
| 53 | print " Projection Name: %s" %projname |
| 54 | print " Column Scaling Factor: %s" %cfac |
| 55 | print " Line Scaling Factor: %s" %lfac |
| 56 | print " Column Offset: %s" %coff |
| 57 | print " Line Offset: %s" %loff |
| 58 | |
| 59 | elif type == 3: |
| 60 | print "Image Data Function Record" |
| 61 | print " Data: {HIDDEN}" |
| 62 | #print " Data: \n%s" %data |
| 63 | |
| 64 | elif type == 4: |
| 65 | print "Annotation Record" |
| 66 | print " Data: %s" %data |
| 67 | |
| 68 | elif type == 5: |
| 69 | print "Timestamp Record" |
| 70 | days, ms = struct.unpack(">HI", data[1:]) |
| 71 | print " Delta from 1 January 1958" |
| 72 | print " Days: %s" %days |
| 73 | print " Miliseconds: %s" %ms |
| 74 | |
| 75 | elif type == 6: |
| 76 | print "Ancillary Text" |
| 77 | print " Data: %s" %data |
| 78 | |
| 79 | elif type == 7: |
| 80 | print "Key Header" |
| 81 | print " Data: %s" %data |
| 82 | |
| 83 | elif type == 128: |
| 84 | print size |
| 85 | imageid, sequence, startcol, startline, maxseg, maxcol, maxrow = struct.unpack(">7H", data) |
| 86 | print "Segment Identification Header" |
| 87 | print " Image Id: %s" %imageid |
| 88 | print " Sequence: %s" %sequence |
| 89 | print " Start Column: %s" %startcol |
| 90 | print " Start Line: %s" %startline |
| 91 | print " Number of Segments: %s" %maxseg |
| 92 | print " Width: %s" %maxcol |
| 93 | print " Height: %s" %maxrow |
| 94 | |
| 95 | elif type == 129: |
| 96 | print "NOAA Specific Header" |
| 97 | signature, productId, productSubId, parameter, compression = struct.unpack(">4sHHHB", data) |
| 98 | print " Signature: %s" %signature |
| 99 | print " Product ID: %s" %productId |
| 100 | print " Product SubId: %s" %productSubId |
| 101 | print " Parameter: %s" %parameter |
| 102 | print " Compression: %s" %compression |
| 103 | |
| 104 | elif type == 130: |
| 105 | print "Header Structured Record" |
| 106 | print " Data: %s" % data |
| 107 | |
| 108 | else: |
| 109 | print "Type not mapped: %s" % type |
| 110 | print "" |
| 111 | |
| 112 | data = f.read(10) |
| 113 | |
| 114 | filecounter, Transportlength = struct.unpack(">HQ", data) |
| 115 | |
| 116 | print "File Counter: %s Transport Size: %s" %(filecounter, Transportlength) |
| 117 | |
| 118 | readHeader(f) |
| 119 | readHeader(f) |
| 120 | readHeader(f) |
| 121 | readHeader(f) |
| 122 | readHeader(f) |
| 123 | readHeader(f) |
| 124 | readHeader(f) |
| 125 | readHeader(f) |
| 126 | readHeader(f) |
| 127 | readHeader(f) |
| 128 | |
| 129 | print t |
| 130 | |
| 131 | f.close() |