def main(): from optparse import OptionParser parser = OptionParser(usage="%prog [options]") parser.add_option('--unit-test', dest='unittest', default=False, action='store_true', help='run the unit tests') parser.add_option('-v', '--verbose', dest='verbose', default=False, action='store_true', help='Make the test output verbose') # FIX: remove nmea from binary messages. No way to build the whole packet? # FIX: or build the surrounding msg 8 for a broadcast? typeChoices = ('binary', 'nmeapayload', 'nmea' ) # FIX: what about a USCG type message? parser.add_option('-t', '--type', choices=typeChoices, type='choice', dest='ioType', default='nmeapayload', help='What kind of string to write for encoding (' + ', '.join(typeChoices) + ') [default: %default]') outputChoices = ('std', 'html', 'csv', 'sql') parser.add_option('-T', '--output-type', choices=outputChoices, type='choice', dest='outputType', default='std', help='What kind of string to output (' + ', '.join(outputChoices) + ') ' '[default: %default]') parser.add_option( '-o', '--output', dest='outputFileName', default=None, help='Name of the python file to write [default: stdout]') parser.add_option( '-f', '--fields', dest='fieldList', default=None, action='append', choices=fieldList, help='Which fields to include in the output. Currently only for csv ' 'output [default: all]') parser.add_option('-p', '--print-csv-field-list', dest='printCsvfieldList', default=False, action='store_true', help='Print the field name for csv') parser.add_option('-c', '--sql-create', dest='sqlCreate', default=False, action='store_true', help='Print out an sql create command for the table.') parser.add_option('--latex-table', dest='latexDefinitionTable', default=False, action='store_true', help='Print a LaTeX table of the type') parser.add_option( '--text-table', dest='textDefinitionTable', default=False, action='store_true', help='Print delimited table of the type (for Word table importing)') parser.add_option('--delimt-text-table', dest='delimTextDefinitionTable', default=' ', help='Delimiter for text table [default: \'%default\'] ' '(for Word table importing)') dbChoices = ('sqlite', 'postgres') parser.add_option('-D', '--db-type', dest='dbType', default='postgres', choices=dbChoices, type='choice', help='What kind of database (' + ', '.join(dbChoices) + ') ' '[default: %default]') addMsgOptions(parser) options, args = parser.parse_args() if options.unittest: sys.argv = [sys.argv[0]] if options.verbose: sys.argv.append('-v') unittest.main() outfile = sys.stdout if None != options.outputFileName: outfile = file(options.outputFileName, 'w') if options.doEncode: # Make sure all non required options are specified. if None == options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField") if None == options.UserIDField: parser.error("missing value for UserIDField") if None == options.reasonField: parser.error("missing value for reasonField") if None == options.fromField: parser.error("missing value for fromField") if None == options.toField: parser.error("missing value for toField") if None == options.radiusField: parser.error("missing value for radiusField") if None == options.unitField: parser.error("missing value for unitField") if None == options.closingdayField: parser.error("missing value for closingdayField") if None == options.closingmonthField: parser.error("missing value for closingmonthField") if None == options.fromhourField: parser.error("missing value for fromhourField") if None == options.fromminField: parser.error("missing value for fromminField") if None == options.todayField: parser.error("missing value for todayField") if None == options.tomonthField: parser.error("missing value for tomonthField") if None == options.tohourField: parser.error("missing value for tohourField") if None == options.tominField: parser.error("missing value for tominField") msgDict = { 'MessageID': '8', 'RepeatIndicator': options.RepeatIndicatorField, 'UserID': options.UserIDField, 'Spare': '0', 'dac': '1', 'fid': '11', 'reason': options.reasonField, 'from': options.fromField, 'to': options.toField, 'radius': options.radiusField, 'unit': options.unitField, 'closingday': options.closingdayField, 'closingmonth': options.closingmonthField, 'fromhour': options.fromhourField, 'frommin': options.fromminField, 'today': options.todayField, 'tomonth': options.tomonthField, 'tohour': options.tohourField, 'tomin': options.tominField, 'spare2': '0', } bits = encode(msgDict) if 'binary' == options.ioType: print str(bits) elif 'nmeapayload' == options.ioType: # FIX: figure out if this might be necessary at compile time bitLen = len(bits) if bitLen % 6 != 0: bits = bits + BitVector( size=(6 - (bitLen % 6))) # Pad out to multiple of 6 print binary.bitvectoais6(bits)[0] # FIX: Do not emit this option for the binary message payloads. Does not make sense. elif 'nmea' == options.ioType: nmea = uscg.create_nmea(bits) print nmea else: sys.exit('ERROR: unknown ioType. Help!') if options.sqlCreate: sqlCreateStr(outfile, options.fieldList, dbType=options.dbType) if options.latexDefinitionTable: latexDefinitionTable(outfile) # For conversion to word tables if options.textDefinitionTable: textDefinitionTable(outfile, options.delimTextDefinitionTable) if options.printCsvfieldList: # Make a csv separated list of fields that will be displayed for csv if None == options.fieldList: options.fieldList = fieldList import StringIO buf = StringIO.StringIO() for field in options.fieldList: buf.write(field + ',') result = buf.getvalue() if result[-1] == ',': print result[:-1] else: print result if options.doDecode: if len(args) == 0: args = sys.stdin for msg in args: bv = None if msg[0] in ('$', '!') and msg[3:6] in ('VDM', 'VDO'): # Found nmea # FIX: do checksum bv = binary.ais6tobitvec(msg.split(',')[5]) else: # either binary or nmeapayload... expect mostly nmeapayloads # assumes that an all 0 and 1 string can not be a nmeapayload binaryMsg = True for c in msg: if c not in ('0', '1'): binaryMsg = False break if binaryMsg: bv = BitVector(bitstring=msg) else: # nmeapayload bv = binary.ais6tobitvec(msg) printFields(decode(bv), out=outfile, format=options.outputType, fieldList=options.fieldList, dbType=options.dbType)
def main(): from optparse import OptionParser parser = OptionParser(usage="%prog [options]") parser.add_option('--unit-test',dest='unittest',default=False,action='store_true', help='run the unit tests') parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', help='Make the test output verbose') # FIX: remove nmea from binary messages. No way to build the whole packet? # FIX: or build the surrounding msg 8 for a broadcast? typeChoices = ('binary','nmeapayload','nmea') # FIX: what about a USCG type message? parser.add_option('-t', '--type', choices=typeChoices, type='choice', dest='ioType', default='nmeapayload', help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]') outputChoices = ('std','html','csv','sql' ) parser.add_option('-T', '--output-type', choices=outputChoices, type='choice', dest='outputType', default='std', help='What kind of string to output ('+', '.join(outputChoices)+') ' '[default: %default]') parser.add_option('-o','--output',dest='outputFileName',default=None, help='Name of the python file to write [default: stdout]') parser.add_option('-f', '--fields', dest='fieldList', default=None, action='append', choices=fieldList, help='Which fields to include in the output. Currently only for csv ' 'output [default: all]') parser.add_option('-p', '--print-csv-field-list', dest='printCsvfieldList', default=False,action='store_true', help='Print the field name for csv') parser.add_option('-c', '--sql-create', dest='sqlCreate', default=False, action='store_true', help='Print out an sql create command for the table.') parser.add_option('--latex-table', dest='latexDefinitionTable', default=False,action='store_true', help='Print a LaTeX table of the type') parser.add_option('--text-table', dest='textDefinitionTable', default=False, action='store_true', help='Print delimited table of the type (for Word table importing)') parser.add_option('--delimt-text-table', dest='delimTextDefinitionTable', default=' ', help='Delimiter for text table [default: \'%default\'] ' '(for Word table importing)') dbChoices = ('sqlite','postgres') parser.add_option('-D', '--db-type', dest='dbType', default='postgres', choices=dbChoices,type='choice', help='What kind of database ('+', '.join(dbChoices)+') ' '[default: %default]') addMsgOptions(parser) options, args = parser.parse_args() if options.unittest: sys.argv = [sys.argv[0]] if options.verbose: sys.argv.append('-v') unittest.main() outfile = sys.stdout if None!=options.outputFileName: outfile = file(options.outputFileName,'w') if options.doEncode: # Make sure all non required options are specified. if None==options.unavail_uintField: parser.error("missing value for unavail_uintField") if None==options.anUIntField: parser.error("missing value for anUIntField") if None==options.anIntField: parser.error("missing value for anIntField") if None==options.aBoolField: parser.error("missing value for aBoolField") if None==options.aStrField: parser.error("missing value for aStrField") if None==options.anUDecimalField: parser.error("missing value for anUDecimalField") if None==options.aDecimalField: parser.error("missing value for aDecimalField") if None==options.aFloatField: parser.error("missing value for aFloatField") msgDict = { 'dac': '366', 'reqDecimal': '122', 'unavail_uint': options.unavail_uintField, 'anUInt': options.anUIntField, 'anInt': options.anIntField, 'aBool': options.aBoolField, 'aStr': options.aStrField, 'anUDecimal': options.anUDecimalField, 'aDecimal': options.aDecimalField, 'aFloat': options.aFloatField, } bits = encode(msgDict) if 'binary' == options.ioType: print str(bits) elif 'nmeapayload'==options.ioType: # FIX: figure out if this might be necessary at compile time bitLen=len(bits) if bitLen % 6 != 0: bits = bits + BitVector(size=(6 - (bitLen%6))) # Pad out to multiple of 6 print binary.bitvectoais6(bits)[0] # FIX: Do not emit this option for the binary message payloads. Does not make sense. elif 'nmea' == options.ioType: nmea = uscg.create_nmea(bits) print nmea else: sys.exit('ERROR: unknown ioType. Help!') if options.sqlCreate: sqlCreateStr(outfile,options.fieldList,dbType=options.dbType) if options.latexDefinitionTable: latexDefinitionTable(outfile) # For conversion to word tables if options.textDefinitionTable: textDefinitionTable(outfile,options.delimTextDefinitionTable) if options.printCsvfieldList: # Make a csv separated list of fields that will be displayed for csv if None == options.fieldList: options.fieldList = fieldList import StringIO buf = StringIO.StringIO() for field in options.fieldList: buf.write(field+',') result = buf.getvalue() if result[-1] == ',': print result[:-1] else: print result if options.doDecode: if len(args)==0: args = sys.stdin for msg in args: bv = None if msg[0] in ('$','!') and msg[3:6] in ('VDM','VDO'): # Found nmea # FIX: do checksum bv = binary.ais6tobitvec(msg.split(',')[5]) else: # either binary or nmeapayload... expect mostly nmeapayloads # assumes that an all 0 and 1 string can not be a nmeapayload binaryMsg=True for c in msg: if c not in ('0','1'): binaryMsg=False break if binaryMsg: bv = BitVector(bitstring=msg) else: # nmeapayload bv = binary.ais6tobitvec(msg) printFields(decode(bv) ,out=outfile ,format=options.outputType ,fieldList=options.fieldList ,dbType=options.dbType )
def main(): from optparse import OptionParser parser = OptionParser(usage="%prog [options]", version="%prog " + __version__) parser.add_option('--unit-test', dest='unittest', default=False, action='store_true', help='run the unit tests') parser.add_option('-v', '--verbose', dest='verbose', default=False, action='store_true', help='Make the test output verbose') # FIX: remove nmea from binary messages. No way to build the whole packet? # FIX: or build the surrounding msg 8 for a broadcast? typeChoices = ('binary', 'nmeapayload', 'nmea' ) # FIX: what about a USCG type message? parser.add_option('-t', '--type', choices=typeChoices, type='choice', dest='ioType', default='nmeapayload', help='What kind of string to write for encoding (' + ', '.join(typeChoices) + ') [default: %default]') outputChoices = ('std', 'html', 'csv', 'sql') parser.add_option('-T', '--output-type', choices=outputChoices, type='choice', dest='outputType', default='std', help='What kind of string to output (' + ', '.join(outputChoices) + ') [default: %default]') parser.add_option( '-o', '--output', dest='outputFileName', default=None, help='Name of the python file to write [default: stdout]') parser.add_option( '-f', '--fields', dest='fieldList', default=None, action='append', choices=fieldList, help= 'Which fields to include in the output. Currently only for csv output [default: all]' ) parser.add_option('-p', '--print-csv-field-list', dest='printCsvfieldList', default=False, action='store_true', help='Print the field name for csv') parser.add_option('-c', '--sql-create', dest='sqlCreate', default=False, action='store_true', help='Print out an sql create command for the table.') parser.add_option('--latex-table', dest='latexDefinitionTable', default=False, action='store_true', help='Print a LaTeX table of the type') parser.add_option( '--text-table', dest='textDefinitionTable', default=False, action='store_true', help='Print delimited table of the type (for Word table importing)') parser.add_option( '--delimt-text-table', dest='delimTextDefinitionTable', default='\t', help= 'Delimiter for text table [default: \'%default\'](for Word table importing)' ) dbChoices = ('sqlite', 'postgres') parser.add_option('-D', '--db-type', dest='dbType', default='postgres', choices=dbChoices, type='choice', help='What kind of database (' + ', '.join(dbChoices) + ') [default: %default]') addMsgOptions(parser) options, args = parser.parse_args() if options.unittest: sys.argv = [sys.argv[0]] if options.verbose: sys.argv.append('-v') unittest.main() outfile = sys.stdout if None != options.outputFileName: outfile = file(options.outputFileName, 'w') if options.doEncode: # First make sure all non required options are specified if None == options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField") if None == options.UserIDField: parser.error("missing value for UserIDField") if None == options.DestID1Field: parser.error("missing value for DestID1Field") if None == options.SeqID1Field: parser.error("missing value for SeqID1Field") if None == options.DestID2Field: parser.error("missing value for DestID2Field") if None == options.SeqID2Field: parser.error("missing value for SeqID2Field") if None == options.DestID3Field: parser.error("missing value for DestID3Field") if None == options.SeqID3Field: parser.error("missing value for SeqID3Field") if None == options.DestID4Field: parser.error("missing value for DestID4Field") if None == options.SeqID4Field: parser.error("missing value for SeqID4Field") msgDict = { 'MessageID': '7', 'RepeatIndicator': options.RepeatIndicatorField, 'UserID': options.UserIDField, 'Spare': '0', 'DestID1': options.DestID1Field, 'SeqID1': options.SeqID1Field, 'DestID2': options.DestID2Field, 'SeqID2': options.SeqID2Field, 'DestID3': options.DestID3Field, 'SeqID3': options.SeqID3Field, 'DestID4': options.DestID4Field, 'SeqID4': options.SeqID4Field, } bits = encode(msgDict) if 'binary' == options.ioType: print str(bits) elif 'nmeapayload' == options.ioType: # FIX: figure out if this might be necessary at compile time #print "bitLen",len(bits) bitLen = len(bits) if bitLen % 6 != 0: bits = bits + BitVector( size=(6 - (bitLen % 6))) # Pad out to multiple of 6 #print "result:",binary.bitvectoais6(bits)[0] print binary.bitvectoais6(bits)[0] # FIX: Do not emit this option for the binary message payloads. Does not make sense. elif 'nmea' == options.ioType: nmea = uscg.create_nmea(bits) print nmea else: sys.exit('ERROR: unknown ioType. Help!')
def main(): parser = OptionParser(usage="%prog [options]") parser.add_option('--unit-test', dest='unittest', default=False, action='store_true', help='run the unit tests') parser.add_option('-v', '--verbose', dest='verbose', default=False, action='store_true', help='Make the test output verbose') # FIX: remove nmea from binary messages. No way to build the whole packet? # FIX: or build the surrounding msg 8 for a broadcast? typeChoices = ('binary', 'nmeapayload', 'nmea' ) # FIX: what about a USCG type message? parser.add_option('-t', '--type', choices=typeChoices, type='choice', dest='ioType', default='nmeapayload', help='What kind of string to write for encoding (' + ', '.join(typeChoices) + ') [default: %default]') outputChoices = ('std', 'html', 'csv', 'sql', 'kml', 'kml-full') parser.add_option('-T', '--output-type', choices=outputChoices, type='choice', dest='outputType', default='std', help='What kind of string to output (' + ', '.join(outputChoices) + ') [default: %default]') parser.add_option( '-o', '--output', dest='outputFileName', default=None, help='Name of the python file to write [default: stdout]') parser.add_option( '-f', '--fields', dest='fieldList', default=None, action='append', choices=fieldList, help= 'Which fields to include in the output. Currently only for csv output [default: all]' ) parser.add_option('-p', '--print-csv-field-list', dest='printCsvfieldList', default=False, action='store_true', help='Print the field name for csv') parser.add_option('-c', '--sql-create', dest='sqlCreate', default=False, action='store_true', help='Print out an sql create command for the table.') parser.add_option('--latex-table', dest='latexDefinitionTable', default=False, action='store_true', help='Print a LaTeX table of the type') parser.add_option( '--text-table', dest='textDefinitionTable', default=False, action='store_true', help='Print delimited table of the type (for Word table importing)') parser.add_option( '--delimt-text-table', dest='delimTextDefinitionTable', default='\t', help= 'Delimiter for text table [default: \'%default\'](for Word table importing)' ) dbChoices = ('sqlite', 'postgres') parser.add_option('-D', '--db-type', dest='dbType', default='postgres', choices=dbChoices, type='choice', help='What kind of database (' + ', '.join(dbChoices) + ') [default: %default]') addMsgOptions(parser) options, args = parser.parse_args() success = True outfile = sys.stdout if None != options.outputFileName: outfile = file(options.outputFileName, 'w') if options.doEncode: # First make sure all non required options are specified if None == options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField") if None == options.UserIDField: parser.error("missing value for UserIDField") if None == options.Time_yearField: parser.error("missing value for Time_yearField") if None == options.Time_monthField: parser.error("missing value for Time_monthField") if None == options.Time_dayField: parser.error("missing value for Time_dayField") if None == options.Time_hourField: parser.error("missing value for Time_hourField") if None == options.Time_minField: parser.error("missing value for Time_minField") if None == options.Time_secField: parser.error("missing value for Time_secField") if None == options.PositionAccuracyField: parser.error("missing value for PositionAccuracyField") if None == options.Position_longitudeField: parser.error("missing value for Position_longitudeField") if None == options.Position_latitudeField: parser.error("missing value for Position_latitudeField") if None == options.fixtypeField: parser.error("missing value for fixtypeField") if None == options.RAIMField: parser.error("missing value for RAIMField") if None == options.state_syncstateField: parser.error("missing value for state_syncstateField") if None == options.state_slottimeoutField: parser.error("missing value for state_slottimeoutField") if None == options.state_slotoffsetField: parser.error("missing value for state_slotoffsetField") msgDict = { 'MessageID': '4', 'RepeatIndicator': options.RepeatIndicatorField, 'UserID': options.UserIDField, 'Time_year': options.Time_yearField, 'Time_month': options.Time_monthField, 'Time_day': options.Time_dayField, 'Time_hour': options.Time_hourField, 'Time_min': options.Time_minField, 'Time_sec': options.Time_secField, 'PositionAccuracy': options.PositionAccuracyField, 'Position_longitude': options.Position_longitudeField, 'Position_latitude': options.Position_latitudeField, 'fixtype': options.fixtypeField, 'Spare': '0', 'RAIM': options.RAIMField, 'state_syncstate': options.state_syncstateField, 'state_slottimeout': options.state_slottimeoutField, 'state_slotoffset': options.state_slotoffsetField, } bits = encode(msgDict) if 'binary' == options.ioType: print str(bits) elif 'nmeapayload' == options.ioType: bitLen = len(bits) if bitLen % 6 != 0: bits = bits + BitVector( size=(6 - (bitLen % 6))) # Pad out to multiple of 6 print binary.bitvectoais6(bits)[0] # FIX: Do not emit this option for the binary message payloads. Does not make sense. elif 'nmea' == options.ioType: nmea = uscg.create_nmea(bits) print nmea else: sys.exit('ERROR: unknown ioType. Help!')
def main(): from optparse import OptionParser parser = OptionParser(usage="%prog [options]", version="%prog "+__version__) parser.add_option('--unit-test',dest='unittest',default=False,action='store_true', help='run the unit tests') parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', help='Make the test output verbose') # FIX: remove nmea from binary messages. No way to build the whole packet? # FIX: or build the surrounding msg 8 for a broadcast? typeChoices = ('binary','nmeapayload','nmea') # FIX: what about a USCG type message? parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType' ,default='nmeapayload' ,help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]') outputChoices = ('std','html','csv','sql' ) parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType' ,default='std' ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]') parser.add_option('-o','--output',dest='outputFileName',default=None, help='Name of the python file to write [default: stdout]') parser.add_option('-f','--fields',dest='fieldList',default=None, action='append', choices=fieldList, help='Which fields to include in the output. Currently only for csv output [default: all]') parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true', help='Print the field name for csv') parser.add_option('-c','--sql-create',dest='sqlCreate',default=False,action='store_true', help='Print out an sql create command for the table.') parser.add_option('--latex-table',dest='latexDefinitionTable',default=False,action='store_true', help='Print a LaTeX table of the type') parser.add_option('--text-table',dest='textDefinitionTable',default=False,action='store_true', help='Print delimited table of the type (for Word table importing)') parser.add_option('--delimt-text-table',dest='delimTextDefinitionTable',default='\t' ,help='Delimiter for text table [default: \'%default\'](for Word table importing)') dbChoices = ('sqlite','postgres') parser.add_option('-D','--db-type',dest='dbType',default='postgres' ,choices=dbChoices,type='choice' ,help='What kind of database ('+', '.join(dbChoices)+') [default: %default]') addMsgOptions(parser) options, args = parser.parse_args() if options.unittest: sys.argv = [sys.argv[0]] if options.verbose: sys.argv.append('-v') unittest.main() outfile = sys.stdout if None!=options.outputFileName: outfile = file(options.outputFileName,'w') if options.doEncode: # First make sure all non required options are specified if None==options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField") if None==options.UserIDField: parser.error("missing value for UserIDField") if None==options.DestID1Field: parser.error("missing value for DestID1Field") if None==options.SeqID1Field: parser.error("missing value for SeqID1Field") if None==options.DestID2Field: parser.error("missing value for DestID2Field") if None==options.SeqID2Field: parser.error("missing value for SeqID2Field") if None==options.DestID3Field: parser.error("missing value for DestID3Field") if None==options.SeqID3Field: parser.error("missing value for SeqID3Field") if None==options.DestID4Field: parser.error("missing value for DestID4Field") if None==options.SeqID4Field: parser.error("missing value for SeqID4Field") msgDict={ 'MessageID': '7', 'RepeatIndicator': options.RepeatIndicatorField, 'UserID': options.UserIDField, 'Spare': '0', 'DestID1': options.DestID1Field, 'SeqID1': options.SeqID1Field, 'DestID2': options.DestID2Field, 'SeqID2': options.SeqID2Field, 'DestID3': options.DestID3Field, 'SeqID3': options.SeqID3Field, 'DestID4': options.DestID4Field, 'SeqID4': options.SeqID4Field, } bits = encode(msgDict) if 'binary'==options.ioType: print str(bits) elif 'nmeapayload'==options.ioType: # FIX: figure out if this might be necessary at compile time #print "bitLen",len(bits) bitLen=len(bits) if bitLen%6!=0: bits = bits + BitVector(size=(6 - (bitLen%6))) # Pad out to multiple of 6 #print "result:",binary.bitvectoais6(bits)[0] print binary.bitvectoais6(bits)[0] # FIX: Do not emit this option for the binary message payloads. Does not make sense. elif 'nmea'==options.ioType: nmea = uscg.create_nmea(bits) print nmea else: sys.exit('ERROR: unknown ioType. Help!')
def main(): parser = OptionParser(usage="%prog [options]") parser.add_option('--unit-test',dest='unittest',default=False,action='store_true', help='run the unit tests') parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', help='Make the test output verbose') # FIX: remove nmea from binary messages. No way to build the whole packet? # FIX: or build the surrounding msg 8 for a broadcast? typeChoices = ('binary','nmeapayload','nmea') # FIX: what about a USCG type message? parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType' ,default='nmeapayload' ,help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]') outputChoices = ('std','html','csv','sql' , 'kml','kml-full') parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType' ,default='std' ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]') parser.add_option('-o','--output',dest='outputFileName',default=None, help='Name of the python file to write [default: stdout]') parser.add_option('-f','--fields',dest='fieldList',default=None, action='append', choices=fieldList, help='Which fields to include in the output. Currently only for csv output [default: all]') parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true', help='Print the field name for csv') parser.add_option('-c','--sql-create',dest='sqlCreate',default=False,action='store_true', help='Print out an sql create command for the table.') parser.add_option('--latex-table',dest='latexDefinitionTable',default=False,action='store_true', help='Print a LaTeX table of the type') parser.add_option('--text-table',dest='textDefinitionTable',default=False,action='store_true', help='Print delimited table of the type (for Word table importing)') parser.add_option('--delimt-text-table',dest='delimTextDefinitionTable',default='\t' ,help='Delimiter for text table [default: \'%default\'](for Word table importing)') dbChoices = ('sqlite','postgres') parser.add_option('-D','--db-type',dest='dbType',default='postgres' ,choices=dbChoices,type='choice' ,help='What kind of database ('+', '.join(dbChoices)+') [default: %default]') addMsgOptions(parser) options, args = parser.parse_args() success = True outfile = sys.stdout if None!=options.outputFileName: outfile = file(options.outputFileName,'w') if options.doEncode: # First make sure all non required options are specified if None==options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField") if None==options.UserIDField: parser.error("missing value for UserIDField") if None==options.Time_yearField: parser.error("missing value for Time_yearField") if None==options.Time_monthField: parser.error("missing value for Time_monthField") if None==options.Time_dayField: parser.error("missing value for Time_dayField") if None==options.Time_hourField: parser.error("missing value for Time_hourField") if None==options.Time_minField: parser.error("missing value for Time_minField") if None==options.Time_secField: parser.error("missing value for Time_secField") if None==options.PositionAccuracyField: parser.error("missing value for PositionAccuracyField") if None==options.Position_longitudeField: parser.error("missing value for Position_longitudeField") if None==options.Position_latitudeField: parser.error("missing value for Position_latitudeField") if None==options.fixtypeField: parser.error("missing value for fixtypeField") if None==options.RAIMField: parser.error("missing value for RAIMField") if None==options.state_syncstateField: parser.error("missing value for state_syncstateField") if None==options.state_slottimeoutField: parser.error("missing value for state_slottimeoutField") if None==options.state_slotoffsetField: parser.error("missing value for state_slotoffsetField") msgDict={ 'MessageID': '4', 'RepeatIndicator': options.RepeatIndicatorField, 'UserID': options.UserIDField, 'Time_year': options.Time_yearField, 'Time_month': options.Time_monthField, 'Time_day': options.Time_dayField, 'Time_hour': options.Time_hourField, 'Time_min': options.Time_minField, 'Time_sec': options.Time_secField, 'PositionAccuracy': options.PositionAccuracyField, 'Position_longitude': options.Position_longitudeField, 'Position_latitude': options.Position_latitudeField, 'fixtype': options.fixtypeField, 'Spare': '0', 'RAIM': options.RAIMField, 'state_syncstate': options.state_syncstateField, 'state_slottimeout': options.state_slottimeoutField, 'state_slotoffset': options.state_slotoffsetField, } bits = encode(msgDict) if 'binary'==options.ioType: print str(bits) elif 'nmeapayload'==options.ioType: bitLen=len(bits) if bitLen%6!=0: bits = bits + BitVector(size=(6 - (bitLen%6))) # Pad out to multiple of 6 print binary.bitvectoais6(bits)[0] # FIX: Do not emit this option for the binary message payloads. Does not make sense. elif 'nmea'==options.ioType: nmea = uscg.create_nmea(bits) print nmea else: sys.exit('ERROR: unknown ioType. Help!')
def main(): from optparse import OptionParser parser = OptionParser(usage="%prog [options]") parser.add_option("--unit-test", dest="unittest", default=False, action="store_true", help="run the unit tests") parser.add_option( "-v", "--verbose", dest="verbose", default=False, action="store_true", help="Make the test output verbose" ) # FIX: remove nmea from binary messages. No way to build the whole packet? # FIX: or build the surrounding msg 8 for a broadcast? typeChoices = ("binary", "nmeapayload", "nmea") # FIX: what about a USCG type message? parser.add_option( "-t", "--type", choices=typeChoices, type="choice", dest="ioType", default="nmeapayload", help="What kind of string to write for encoding (" + ", ".join(typeChoices) + ") [default: %default]", ) outputChoices = ("std", "html", "csv", "sql") parser.add_option( "-T", "--output-type", choices=outputChoices, type="choice", dest="outputType", default="std", help="What kind of string to output (" + ", ".join(outputChoices) + ") " "[default: %default]", ) parser.add_option( "-o", "--output", dest="outputFileName", default=None, help="Name of the python file to write [default: stdout]" ) parser.add_option( "-f", "--fields", dest="fieldList", default=None, action="append", choices=fieldList, help="Which fields to include in the output. Currently only for csv " "output [default: all]", ) parser.add_option( "-p", "--print-csv-field-list", dest="printCsvfieldList", default=False, action="store_true", help="Print the field name for csv", ) parser.add_option( "-c", "--sql-create", dest="sqlCreate", default=False, action="store_true", help="Print out an sql create command for the table.", ) parser.add_option( "--latex-table", dest="latexDefinitionTable", default=False, action="store_true", help="Print a LaTeX table of the type", ) parser.add_option( "--text-table", dest="textDefinitionTable", default=False, action="store_true", help="Print delimited table of the type (for Word table importing)", ) parser.add_option( "--delimt-text-table", dest="delimTextDefinitionTable", default=" ", help="Delimiter for text table [default: '%default'] " "(for Word table importing)", ) dbChoices = ("sqlite", "postgres") parser.add_option( "-D", "--db-type", dest="dbType", default="postgres", choices=dbChoices, type="choice", help="What kind of database (" + ", ".join(dbChoices) + ") " "[default: %default]", ) addMsgOptions(parser) options, args = parser.parse_args() if options.unittest: sys.argv = [sys.argv[0]] if options.verbose: sys.argv.append("-v") unittest.main() outfile = sys.stdout if None != options.outputFileName: outfile = file(options.outputFileName, "w") if options.doEncode: # Make sure all non required options are specified. if None == options.RepeatIndicatorField: parser.error("missing value for RepeatIndicatorField") if None == options.UserIDField: parser.error("missing value for UserIDField") if None == options.monthField: parser.error("missing value for monthField") if None == options.dayField: parser.error("missing value for dayField") if None == options.hourField: parser.error("missing value for hourField") if None == options.minField: parser.error("missing value for minField") if None == options.stationidField: parser.error("missing value for stationidField") if None == options.waterlevelField: parser.error("missing value for waterlevelField") if None == options.datumField: parser.error("missing value for datumField") if None == options.sigmaField: parser.error("missing value for sigmaField") if None == options.sourceField: parser.error("missing value for sourceField") msgDict = { "MessageID": "8", "RepeatIndicator": options.RepeatIndicatorField, "UserID": options.UserIDField, "Spare": "0", "dac": "366", "fid": "63", "month": options.monthField, "day": options.dayField, "hour": options.hourField, "min": options.minField, "stationid": options.stationidField, "waterlevel": options.waterlevelField, "datum": options.datumField, "sigma": options.sigmaField, "source": options.sourceField, } bits = encode(msgDict) if "binary" == options.ioType: print str(bits) elif "nmeapayload" == options.ioType: # FIX: figure out if this might be necessary at compile time bitLen = len(bits) if bitLen % 6 != 0: bits = bits + BitVector(size=(6 - (bitLen % 6))) # Pad out to multiple of 6 print binary.bitvectoais6(bits)[0] # FIX: Do not emit this option for the binary message payloads. Does not make sense. elif "nmea" == options.ioType: nmea = uscg.create_nmea(bits) print nmea else: sys.exit("ERROR: unknown ioType. Help!") if options.sqlCreate: sqlCreateStr(outfile, options.fieldList, dbType=options.dbType) if options.latexDefinitionTable: latexDefinitionTable(outfile) # For conversion to word tables if options.textDefinitionTable: textDefinitionTable(outfile, options.delimTextDefinitionTable) if options.printCsvfieldList: # Make a csv separated list of fields that will be displayed for csv if None == options.fieldList: options.fieldList = fieldList import StringIO buf = StringIO.StringIO() for field in options.fieldList: buf.write(field + ",") result = buf.getvalue() if result[-1] == ",": print result[:-1] else: print result if options.doDecode: if len(args) == 0: args = sys.stdin for msg in args: bv = None if msg[0] in ("$", "!") and msg[3:6] in ("VDM", "VDO"): # Found nmea # FIX: do checksum bv = binary.ais6tobitvec(msg.split(",")[5]) else: # either binary or nmeapayload... expect mostly nmeapayloads # assumes that an all 0 and 1 string can not be a nmeapayload binaryMsg = True for c in msg: if c not in ("0", "1"): binaryMsg = False break if binaryMsg: bv = BitVector(bitstring=msg) else: # nmeapayload bv = binary.ais6tobitvec(msg) printFields( decode(bv), out=outfile, format=options.outputType, fieldList=options.fieldList, dbType=options.dbType, )