Пример #1
0
def listVars(formula):
    """Extract variables from a formula and  sets indices"""
    tsPattern = re.compile("ts\.(\w*)")
    tsMatches = tsPattern.findall(formula)
    tsMatches=utilities.uniqify(tsMatches)

    #Making sure the first variable is speed and the second is status
    if "V" in tsMatches:
        tsMatches.remove("V")
        tsMatches.insert(0,"V")
    if "status" in tsMatches:
        tsMatches.remove("status")
        tsMatches.insert(1,"status")
    
    varPattern = re.compile("v\.(\w*)")
    varMatches = varPattern.findall(formula)
    varMatches = utilities.uniqify(varMatches)

    constPattern = re.compile("c\.(\w*)")
    constMatches = constPattern.findall(formula)
    constMatches = utilities.uniqify(constMatches)
    
    varList = []
    index = 1
    for var in tsMatches:
        varList.append((index, var, "TS"))
        index+=1
    for var in varMatches:
        varList.append((index, var, "VAR"))
        index+=1
    for var in constMatches:
        varList.append((index, var, "CONST"))
        index+=1


    for varIndex,varName,varType in varList:
        if len(varName)>15:
            raise ValueError("Variable name %s longer than max length of 13 characters" %varName)        
    return varList
Пример #2
0
def main():
    #-----------Setting up and using the option parser-----------------------
    parser=OptionParser(usage= usage, version=version)
    
    parser.add_option("-v", "--verbose",
                      action="store_true",dest="verbose",
                      help="Verbose output")

    parser.add_option("-b", "--begin",
                      action="store",dest="begin",default=None,
                      help="Time to start extracting ship tracks")

    parser.add_option("-e", "--end",
                      action="store",dest="end",default=None,
                      help="Time to end extraction of ship tracks")

    parser.add_option("-m", "--mmsi",
                      action="store",dest="mmsi",default=None,
                      help="Specify a mmsi")

    parser.add_option("-o", "--output",
                      action="store",dest="outputFile",default=None,
                      help="To write output in ascii format")

    
    (options, args) = parser.parse_args()

    if options.verbose:
        loglevel = logging.DEBUG
    else:
        loglevel = logging.INFO
    logging.basicConfig(level=loglevel)
    log = logging.getLogger(__name__)      
        

    if options.end is None or options.begin is None:
        parser.error("Need to specify begin and end time")

    if options.outputFile is None:
        parser.error("Need to specify output filename")

    #Get mmsi for ships with data in the time-inteval using keyscan
    cmd="keyscan -r f -b %s -e %s" %(options.begin,options.end)                
    returnCode,errMsg,outMsg=utilities.execute(cmd)
    keys=outMsg.split("\n")            
    keys=[int(key[:3]+key[5:11]) for key in keys[:-1]]
    keys=utilities.uniqify(keys) #remove doubles

    ais_stat=AisStat(domain=os.environ.get("AVDBNAME"))
    ais_stat.read()

    if options.mmsi is not None:
        mmsi=int(options.mmsi)
        if mmsi not in ais_stat.mmsi:
            log.error("MMSI %s not found in ais_stat.txt" %options.mmsi)
            sys.exit(1)
        if mmsi not in keys:
            log.info("No time-series data for ship in specified time interval")
            sys.exit(0)      
        mmsiList=[mmsi]
    else:
        mmsiList = [mmsi for mmsi in  keys if mmsi in ais_stat.mmsi]
        log.info("%i ships out of %i in time-series db found in ais_stat.txt" %(len(mmsiList),
                                                                                   len(keys)))
    firstShip=True
    for mmsi in mmsiList:
        cmd="shiptsget -r f -b %s -e %s %i -S" %(options.begin,options.end,mmsi)
        returnCode,errMsg,outMsg=utilities.execute(cmd)

        rows=outMsg.split("\n")
        geomType=rows.pop(0)
        attributeNames=rows.pop(0)
        coords=rows[1::4]
        data=rows[3::4]
        if firstShip:
            dataRows=data
            coordRows=coords
            firstShip=False
        else:
            dataRows+=data
            coordRows+=coords
    
    output=geomType+"\n"
    output+=attributeNames+"\n"
    for i in range(len(coordRows)):
        output+="#COORD %i 0\n" %(i+1)
        output+=coordRows[i]+"\n"
        output+="#DATA %i\n" %(i+1)
        output+=dataRows[i]+"\n"

    
    try:
        outputFile=open(path.abspath(options.outputFile),"w")
    except:
        log.error("Could not open file: %s" %path.abspath(options.outputFile))
        sys.exit(1)
    outputFile.write(output)
    outputFile.close()