def calculatePacketPeakMagnitude(Components):
    firstPass = True
    for ThePacket in Components:
        mseed = simpleMiniseed.unpackMiniseedRecord(ThePacket.data)
        data = mseed.data
        if firstPass:
            datasqrd = []
            npts = len(data)
        if len(data) != npts:
            print("NPTS mismatch, returning negative max")
            return -1, npts
        i = 0
        #print(data[12])
        while i < len(data):
            if firstPass:
                thedata = []
                datasqrd.append(data[i] * data[i])
            else:
                datasqrd[i] = datasqrd[i] + (data[i] * data[i])
            i = i + 1
        firstPass = False
    i = 0
    while i < len(data):
        datasqrd[i] = math.sqrt(datasqrd[i])
        i = i + 1
    return max(datasqrd), npts
def matchingPackets(ThePacket, orientation, starttime):
    mSeed = simpleMiniseed.unpackMiniseedRecord(ThePacket.data)
    if starttime == mSeed.header.starttime:
        if orientation != mSeed.header.channel[2]:
            return True
        else:
            return False
    else:
        return False
示例#3
0
def readOneRecord(f):
    rawBytes = f.read(512)
    if len(rawBytes) < 512:
        if len(rawBytes) == 0:
            # assume done, return None
            return None
        else:
            raise Exception("Not enough bytes {:d}<512".format(len(rawBytes)))
    msr = simpleMiniseed.unpackMiniseedRecord(rawBytes)
    return msr
示例#4
0
 def nextRecord(self):
     rawBytes = self.openFile.read(512)
     msr = None
     if len(rawBytes) < 512:
         print("Done {:d}".format(len(rawBytes)))
         self.openFile.close()
         self.openFile = None
     else:
         msr = simpleMiniseed.unpackMiniseedRecord(rawBytes)
     return msr
def CompareHeaders(dlPacket1, dlPacket2):
    global daliUpload
    #    print(dlPacket1.streamId)
    print(dlPacket2.streamId)
    if dlPacket1.streamId.endswith("MSEED"):
        if dlPacket2.streamId.endswith("MSEED"):
            mseedRecord1 = simpleMiniseed.unpackMiniseedRecord(dlPacket1.data)
            mseedRecord2 = simpleMiniseed.unpackMiniseedRecord(dlPacket2.data)
            if mseedRecord1.header.station == mseedRecord2.header.station:
                print("same station ", mseedRecord1.header.station)
                if mseedRecord1.header.channel != mseedRecord2.header.channel:
                    print("channels: ", mseedRecord1.header.channel,
                          mseedRecord2.header.channel)
                    if mseedRecord1.header.starttime == mseedRecord2.header.starttime:
                        print("same start time ... keep them")
                        return True
                    else:
                        diff = mseedRecord2.header.starttime - mseedRecord1.header.starttime
                        print("Time difference: ", diff)
                        return False
示例#6
0
def readThenWrite():
    with open(filename, 'rb+') as f:
        with open(outfilename, 'wb') as out:
            while True:
                rawBytes = f.read(512)
                if len(rawBytes) < 512:
                    print("Done {:d}".format(len(rawBytes)))
                    break
                msr = simpleMiniseed.unpackMiniseedRecord(rawBytes)
                print("read record: {} {:d} {:d} {:d}".format(
                    msr.codes(), msr.header.recordLength,
                    msr.header.numsamples, msr.header.encoding))
                out.write(msr.pack())
def buildPacketKey(ThePacket, RootChan):
    mSeed = simpleMiniseed.unpackMiniseedRecord(ThePacket.data)
    StartTime = mSeed.header.starttime
    sampleRate = mSeed.header.sampleRate
    net = mSeed.header.network
    sta = mSeed.header.station
    loc = mSeed.header.location
    chan = mSeed.header.channel[0:2]
    Orient = mSeed.header.channel[2]
    #    print("Orientation",Orient)
    #    print("RootChannel",chan)
    if chan != RootChan:
        return "NotRootChan", Orient, StartTime
    key = net + "." + sta + "." + loc + "." + chan
    #    print("KEY: ", key)
    #    print("Start Time: ", StartTime)
    return key, Orient, StartTime, sta
示例#8
0
def checkComponentMax(filename, bitShift=False):
    #print("checkComponent Max {}".format(filename))
    with open(filename, 'rb+') as f:
        while True:
            try:
                rawBytes = f.read(512)
                if len(rawBytes) < 512:
                    #print("Done {:d}".format(len(rawBytes)))
                    break
                msr = simpleMiniseed.unpackMiniseedRecord(rawBytes)
                #print("read record: {} {:d} {:d} {:d}".format(msr.codes(), msr.header.recordLength, msr.header.numsamples, msr.header.encoding))
                for i in range(len(msr.data)):
                    if msr.data[i] > 8000 or msr.data[i] < -8000:
                        print("MAX ALERT for {} at roughly {}, {:d}".format(
                            msr.codes(), msr.starttime(), msr.data[i]))
            except:
                #print("Bad record in file {}".format(filename))
                break
示例#9
0
def recompress(filename, bitShift=False):
    print("recompress {}".format(filename))
    with open(filename, 'rb+') as f:
        while True:
            rawBytes = f.read(512)
            if len(rawBytes) < 512:
                print("Done {:d}".format(len(rawBytes)))
                break
            msr = simpleMiniseed.unpackMiniseedRecord(rawBytes)
            #print("read record: {} {:d} {:d} {:d}".format(msr.codes(), msr.header.recordLength, msr.header.numsamples, msr.header.encoding))
            if not msr.codes() in miniseedBuffers:
                miniseedBuffers[msr.codes()] = \
                    dataBuffer.DataBuffer(msr.header.network,
                        msr.header.station,
                        msr.header.location,
                        msr.header.channel,
                        msr.header.sampleRate,
                        archive=True,
                        encoding=simpleMiniseed.ENC_SHORT)
            if bitShift:
                for i in range(len(msr.data)):
                    msr.data[i] = msr.data[i] >> 2

            miniseedBuffers[msr.codes()].push(msr.starttime(), msr.data)
示例#10
0
async def connection(loop):
    try:
        dali = simpleDali.WebSocketDataLink(uri)
        await dali.parseResponse()
    except Exception:
        dali = simpleDali.SocketDataLink(host, port)
    dali.verbose = True
    serverId = await dali.id(programname, username, processid, architecture)
    print("Resp: {}".format(serverId))
    serverInfo = await dali.info("STATUS")
    print("Info: {} ".format(serverInfo.message))
    print("File directory?")
    channelLoc = input()
    if channelLoc == '0':
        r = await dali.match("XX.XB08.00.HN./MSEED")
    else:
        r = await dali.match(channelLoc)

    begintime = datetime.utcnow() - timedelta(seconds=2)
    r = await dali.positionAfter(begintime)
    if r.type.startswith("ERROR"):
        print(
            "positionAfter() Resonse {}, ringserver might not know about these packets?"
            .format(r))
    else:
        print("positionAfter() Resonse m={}".format(r.message))
    r = await dali.stream()
    sys.stdout.flush()
    while (keepGoing):
        peakPacket = await dali.parseResponse()
        if not peakPacket.type == "PACKET":
            print("parseResponse not a PACKET {} ".format(peakPacket))
        else:
            peakInfo = {}
            devationValues = []
            squares = []
            peakInfo = simpleMiniseed.unpackMiniseedRecord(
                peakPacket.data).data
            channelX = simpleMiniseed.unpackMiniseedHeader(
                peakPacket.data).channel
            timestamp = simpleMiniseed.unpackMiniseedRecord(
                peakPacket.data).starttime()
            average = sum(peakInfo) / len(peakInfo)
            for x in range(len(peakInfo)):
                squares.append(peakInfo[x]**2)
                rms = math.sqrt(sum(squares) / len(peakInfo))
            for x in range(len(peakInfo)):
                devationValues.append((peakInfo[x] - average)**2)
            devation = math.sqrt(sum(devationValues) / (len(peakInfo) - 1))

            print('-========== Channel {} ==========-'.format(channelX))
            print('standard dev = {}'.format(devation))
            print('rms = {}'.format(rms))
            print('max value = {}'.format(max(peakInfo)))
            print('min value = {}'.format(min(peakInfo)))
            print('average = {}'.format(average))
            print('timestamp = {}'.format(timestamp))

            peakInfo = {}
        sys.stdout.flush()
    dali.close()
def doTest():
    global keepGoing
    global daliDownload
    global daliUpload
    loop = asyncio.get_event_loop()
    loop.set_debug(True)

    #    initTask = loop.create_task(initConnections(".*PI04.*HNZ/MSEED"))
    initTask = loop.create_task(initConnections(STREAM_PATTERN))
    loop.run_until_complete(initTask)

    packetDictionary = {}
    packetCount = 0
    Components = []
    match_count = 1
    GotAllThree = False
    #    print("Entering test ... Initializing Components", match_count)

    while (keepGoing):
        #        print("inside keepGoing loop")
        dlPacket = getNextPacket()
        try:
            key, orientation, starttime, station = buildPacketKey(
                dlPacket, "HN")
            if verbose:
                print("Got another packet: ", key, orientation, starttime)
            if not key in packetDictionary:
                #            print("     New Key: ", key,orientation,starttime)
                packetDictionary[key] = []
                packetDictionary[key].append(dlPacket)
            else:
                Components.append(dlPacket)
                for value in packetDictionary[key]:
                    if matchingPackets(value, orientation, starttime):
                        #                print("     A match: ",value,orientation,starttime)

                        Components.append(value)
                        match_count = match_count + 1
    #                    print("B ... putting value in Components",match_count,key)
                    if match_count == 3:
                        for ThePacket in Components:
                            mSeed = simpleMiniseed.unpackMiniseedRecord(
                                ThePacket.data)
                            thedata = mSeed.data
                            StartTime = mSeed.header.starttime
                            sta = mSeed.header.station
                            Orient = mSeed.header.channel[2]
        #                print("Got 3 components at the same time and station ... WooHoo")
                        GotAllThree = True
                        #                    print("Component Length",len(Components))
                        #print(Components)
                        maxMag, npts_packet = calculatePacketPeakMagnitude(
                            Components)
                        #
                        # TEMPORARY FIR FIlter # FIXME:
                        #
                        maxMag = maxMag / COUNTS_PER_G
                        streamid = "{}.{}/MAXACC".format("XX", station)
                        hpdatastart = simpleDali.datetimeToHPTime(starttime)
                        hpdataend = simpleDali.datetimeToHPTime(starttime)
                        jsonMessage = {
                            "station": station,
                            "time": starttime.isoformat(),
                            "accel": maxMag
                        }
                        if verbose:
                            print("Send json max: {}".format(
                                jsonMessage["accel"]))
                        writeJsonToDatalink(streamid, hpdatastart, hpdataend,
                                            jsonMessage)
                        jsonMessage = {}
                        packetDictionary[key].append(dlPacket)
                        for UsedPacket in Components:
                            packetDictionary[key].remove(UsedPacket)
                match_count = 1
                if not GotAllThree:
                    packetDictionary[key].append(dlPacket)
                GotAllThree = False
                Components = []

            packetCount += 1
            if MAX_COUNT > 0 and packetCount > MAX_COUNT:
                keepGoing = False
        except Exception as e:
            print("Got bad miniseed record, skipping..." + e)
    for key, db in dataBuffers.items():
        #            for key, db in dataBuffers.items():
        # just in case some data has not been sent
        db.flush()
        #
    daliDownload.close()
    daliUpload.close()
    loop.close()