Unidata /
python-awips
| 1 | # |
||
| 2 | # Partially compatible AWIPS-II Thrift Binary Protocol |
||
| 3 | # |
||
| 4 | # |
||
| 5 | # SOFTWARE HISTORY |
||
| 6 | # |
||
| 7 | # Date Ticket# Engineer Description |
||
| 8 | # ------------ ---------- ----------- -------------------------- |
||
| 9 | # 11/11/09 chammack Initial Creation. |
||
| 10 | # 06/09/10 njensen Added float, list methods |
||
| 11 | # Apr 24, 2015 4425 nabowle Add F64List support. |
||
| 12 | # |
||
| 13 | # |
||
| 14 | |||
| 15 | import struct |
||
| 16 | import numpy |
||
| 17 | from thrift.protocol.TProtocol import * |
||
| 18 | from thrift.protocol.TBinaryProtocol import * |
||
| 19 | |||
| 20 | FLOAT = 64 |
||
| 21 | |||
| 22 | intList = numpy.dtype(numpy.int32).newbyteorder('>') |
||
| 23 | floatList = numpy.dtype(numpy.float32).newbyteorder('>') |
||
| 24 | longList = numpy.dtype(numpy.int64).newbyteorder('>') |
||
| 25 | shortList = numpy.dtype(numpy.int16).newbyteorder('>') |
||
| 26 | byteList = numpy.dtype(numpy.int8).newbyteorder('>') |
||
| 27 | doubleList = numpy.dtype(numpy.float64).newbyteorder('>') |
||
| 28 | |||
| 29 | |||
| 30 | View Code Duplication | class SelfDescribingBinaryProtocol(TBinaryProtocol): |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
|
|||
| 31 | |||
| 32 | def readFieldBegin(self): |
||
| 33 | ftype = self.readByte() |
||
| 34 | if ftype == TType.STOP: |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 35 | return None, ftype, 0 |
||
| 36 | name = self.readString() |
||
| 37 | fid = self.readI16() |
||
| 38 | return name, ftype, fid |
||
| 39 | |||
| 40 | def readStructBegin(self): |
||
| 41 | return self.readString() |
||
| 42 | |||
| 43 | def writeStructBegin(self, name): |
||
| 44 | self.writeString(name) |
||
| 45 | |||
| 46 | def writeFieldBegin(self, name, ftype, fid): |
||
| 47 | self.writeByte(ftype) |
||
| 48 | self.writeString(name) |
||
| 49 | self.writeI16(fid) |
||
| 50 | |||
| 51 | def readFloat(self): |
||
| 52 | d = self.readI32() |
||
| 53 | dAsBytes = struct.pack('i', d) |
||
| 54 | f = struct.unpack('f', dAsBytes) |
||
| 55 | return f[0] |
||
| 56 | |||
| 57 | def writeFloat(self, f): |
||
| 58 | dAsBytes = struct.pack('f', f) |
||
| 59 | i = struct.unpack('i', dAsBytes) |
||
| 60 | self.writeI32(i[0]) |
||
| 61 | |||
| 62 | def readI32List(self, sz): |
||
| 63 | buff = self.trans.readAll(4*sz) |
||
| 64 | val = numpy.frombuffer(buff, dtype=intList, count=sz) |
||
| 65 | return val |
||
| 66 | |||
| 67 | def readF32List(self, sz): |
||
| 68 | buff = self.trans.readAll(4*sz) |
||
| 69 | val = numpy.frombuffer(buff, dtype=floatList, count=sz) |
||
| 70 | return val |
||
| 71 | |||
| 72 | def readF64List(self, sz): |
||
| 73 | buff = self.trans.readAll(8*sz) |
||
| 74 | val = numpy.frombuffer(buff, dtype=doubleList, count=sz) |
||
| 75 | return val |
||
| 76 | |||
| 77 | def readI64List(self, sz): |
||
| 78 | buff = self.trans.readAll(8*sz) |
||
| 79 | val = numpy.frombuffer(buff, dtype=longList, count=sz) |
||
| 80 | return val |
||
| 81 | |||
| 82 | def readI16List(self, sz): |
||
| 83 | buff = self.trans.readAll(2*sz) |
||
| 84 | val = numpy.frombuffer(buff, dtype=shortList, count=sz) |
||
| 85 | return val |
||
| 86 | |||
| 87 | def readI8List(self, sz): |
||
| 88 | buff = self.trans.readAll(sz) |
||
| 89 | val = numpy.frombuffer(buff, dtype=byteList, count=sz) |
||
| 90 | return val |
||
| 91 | |||
| 92 | def writeI32List(self, buff): |
||
| 93 | b = numpy.asarray(buff, intList) |
||
| 94 | self.trans.write(numpy.getbuffer(b)) |
||
| 95 | |||
| 96 | def writeF32List(self, buff): |
||
| 97 | b = numpy.asarray(buff, floatList) |
||
| 98 | self.trans.write(numpy.getbuffer(b)) |
||
| 99 | |||
| 100 | def writeF64List(self, buff): |
||
| 101 | b = numpy.asarray(buff, doubleList) |
||
| 102 | self.trans.write(numpy.getbuffer(b)) |
||
| 103 | |||
| 104 | def writeI64List(self, buff): |
||
| 105 | b = numpy.asarray(buff, longList) |
||
| 106 | self.trans.write(numpy.getbuffer(b)) |
||
| 107 | |||
| 108 | def writeI16List(self, buff): |
||
| 109 | b = numpy.asarray(buff, shortList) |
||
| 110 | self.trans.write(numpy.getbuffer(b)) |
||
| 111 | |||
| 112 | def writeI8List(self, buff): |
||
| 113 | b = numpy.asarray(buff, byteList) |
||
| 114 | self.trans.write(numpy.getbuffer(b)) |
||
| 115 |