예제 #1
0
def getSensorDataSummary(sensorId, locationMessage):
    sensor = SensorDb.getSensorObj(sensorId)
    if sensor is None:
        return {STATUS: NOK, ERROR_MESSAGE: "Sensor Not found"}
    measurementType = sensor.getMeasurementType()
    tzId = locationMessage[TIME_ZONE_KEY]
    acquisitionCount = LocationMessage.getMessageCount(locationMessage)
    util.debugPrint("AquistionCount " + str(acquisitionCount))
    if acquisitionCount == 0:
        return {
            "status": "OK",
            "minOccupancy": 0,
            "tStartReadings": 0,
            "tStartLocalTime": 0,
            "tStartLocalTimeFormattedTimeStamp": "UNKNOWN",
            "tStartDayBoundary": 0,
            "tEndDayBoundary": 0,
            "tEndReadings": 0,
            "tEndLocalTimeFormattedTimeStamp": "UNKNOWN",
            "maxOccupancy": 0,
            "measurementType": measurementType,
            "isStreamingEnabled": sensor.isStreamingEnabled(),
            "sensorStatus": sensor.getSensorStatus(),
            COUNT: 0
        }

    minTime = LocationMessage.getFirstDataMessageTimeStamp(locationMessage)
    maxTime = LocationMessage.getLastDataMessageTimeStamp(locationMessage)

    tStartDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        minTime, tzId)
    (minLocalTime,
     tStartLocalTimeTzName) = timezone.getLocalTime(minTime, tzId)

    tEndDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        maxTime, tzId)

    tstampMin = timezone.formatTimeStampLong(minTime, tzId)
    tstampMax = timezone.formatTimeStampLong(maxTime, tzId)
    retval = {
        "status": "OK",
        "maxOccupancy": 0,
        "minOccupancy": 0,
        "tStartReadings": minTime,
        "tStartLocalTime": minLocalTime,
        "tStartLocalTimeFormattedTimeStamp": tstampMin,
        "tStartDayBoundary": tStartDayBoundary,
        "tEndDayBoundary": tEndDayBoundary,
        "tEndReadings": maxTime,
        "tEndLocalTimeFormattedTimeStamp": tstampMax,
        "measurementType": measurementType,
        "isStreamingEnabled": sensor.isStreamingEnabled(),
        "sensorStatus": sensor.getSensorStatus(),
        COUNT: acquisitionCount
    }

    return retval
예제 #2
0
def getPrevDayBoundary(msg):
    """
    get the previous acquisition day boundary.
    """
    prevMsg = getPrevAcquisition(msg)
    if prevMsg is None:
        locationMessage = getLocationMessage(msg)
        return timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            msg['t'], locationMessage[TIME_ZONE_KEY])
    locationMessage = msgutils.getLocationMessage(prevMsg)
    timeZone = locationMessage[TIME_ZONE_KEY]
    return timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        prevMsg['t'], timeZone)
예제 #3
0
def getDayBoundaryTimeStamp(msg):
    """
    Get the universal time stamp for the day boundary of this message.
    """
    locationMessage = getLocationMessage(msg)
    timeZone = locationMessage[TIME_ZONE_KEY]
    return timezone.getDayBoundaryTimeStampFromUtcTimeStamp(msg['t'], timeZone)
예제 #4
0
def getMinDayBoundaryForAcquistions(msg):
    """
    Get the minimum day boundary for acquistions assocaiated with this msg.
    """
    locationMsg = getLocationMessage(msg)
    timeStamp = locationMsg["firstDataMessageTimeStamp"]
    tzId = msg[TIME_ZONE_KEY]
    return timezone.getDayBoundaryTimeStampFromUtcTimeStamp(timeStamp, tzId)
예제 #5
0
def getNextDayBoundary(msg):
    """
    get the next acquistion day boundary.
    """
    nextMsg = getNextAcquisition(msg)
    if nextMsg is None:
        locationMessage = getLocationMessage(msg)
        return timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            msg['t'], locationMessage[TIME_ZONE_KEY])
    locationMessage = getLocationMessage(nextMsg)
    timeZone = locationMessage[TIME_ZONE_KEY]
    nextDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        nextMsg['t'], timeZone)
    if DebugFlags.debug:
        thisDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            msg['t'], locationMessage[TIME_ZONE_KEY])
        util.debugPrint("getNextDayBoundary: dayBoundary difference " +
                        str((nextDayBoundary - thisDayBoundary) / 60 / 60))
    return nextDayBoundary
예제 #6
0
def getEvents(sensorId, startTime, days):
    captureDb = DbCollections.getCaptureEventDb(sensorId)
    if startTime > 0:
        query = {SENSOR_ID: sensorId, "t": {"$gte": startTime}}
        captureEvent = captureDb.find_one(query)
        if captureEvent is None:
            return {STATUS: OK, "events": []}
        locationMessage = msgutils.getLocationMessage(captureEvent)
        if locationMessage is None:
            return {STATUS: NOK, "ErrorMessage": "Location message not found"}
        tZId = locationMessage[TIME_ZONE_KEY]
        timeStamp = captureEvent['t']
        startTimeDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            timeStamp, tZId)
        endTime = startTimeDayBoundary + days * SECONDS_PER_DAY
        query = {"t": {"$gte": startTimeDayBoundary}, "t": {"$lte": endTime}}
    else:
        query = {}
        captureEvent = captureDb.find_one()
        if captureEvent is None:
            return {STATUS: OK, "events": []}
        locationMessage = msgutils.getLocationMessage(captureEvent)
        if locationMessage is None:
            return {STATUS: NOK, "ErrorMessage": "Location message not found"}
        timeStamp = captureEvent['t']
        tZId = locationMessage[TIME_ZONE_KEY]
        startTime = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            timeStamp, tZId)
        if days > 0:
            endTime = startTime + days * SECONDS_PER_DAY
            query = {"t": {"$gte": startTime}, "t": {"$lte": endTime}}
        else:
            query = {"t": {"$gte": startTime}}

    found = captureDb.find(query)
    retval = []
    if found is not None:
        for value in found:
            del value["_id"]
            retval.append(value)

    return {STATUS: OK, "events": retval}
예제 #7
0
 def testGetDayBoundaryOffset(self):
     global tzId
     t = timezone.getLocalUtcTimeStamp()
     startOfToday = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
         t, tzId)
     self.assertTrue(t > startOfToday)
     delta = t - startOfToday
     now = datetime.datetime.now()
     hourOffset = now.hour
     minOffset = now.minute
     secondOffset = now.second
     newDelta = hourOffset * 60 * 60 + minOffset * 60 + secondOffset
     self.assertTrue(abs(newDelta - delta) < 2)
def getDailyMaxMinMeanStats(sensorId, lat, lon, alt, tstart, ndays, sys2detect,
                            fmin, fmax, subBandMinFreq, subBandMaxFreq):

    locationMessage = DbCollections.getLocationMessages().find_one({
        SENSOR_ID: sensorId,
        LAT: lat,
        LON: lon,
        ALT: alt
    })
    if locationMessage is None:
        return {STATUS: NOK, ERROR_MESSAGE: "Location Information Not Found"}
    locationMessageId = str(locationMessage["_id"])
    tZId = locationMessage[TIME_ZONE_KEY]
    tmin = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(tstart, tZId)
    startMessage = DbCollections.getDataMessages(sensorId).find_one()
    result = {}
    result[STATUS] = OK
    values = {}
    for day in range(0, ndays):
        tstart = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            tmin + day * SECONDS_PER_DAY, tZId)
        tend = tstart + SECONDS_PER_DAY
        queryString = {
            LOCATION_MESSAGE_ID: locationMessageId,
            TIME: {
                '$gte': tstart,
                '$lte': tend
            },
            FREQ_RANGE: msgutils.freqRange(sys2detect, fmin, fmax)
        }
        cur = DbCollections.getDataMessages(sensorId).find(queryString)
        # cur.batch_size(20)
        if startMessage['mType'] == FFT_POWER:
            stats = compute_daily_max_min_mean_stats_for_fft_power(cur)
        else:
            stats = compute_daily_max_min_mean_median_stats_for_swept_freq(
                cur, subBandMinFreq, subBandMaxFreq)
        # gap in readings. continue.
        if stats is None:
            continue
        (cutoff, dailyStat) = stats
        values[day * 24] = dailyStat
    # Now compute the next interval after the last one (if one exists)
    tend = tmin + SECONDS_PER_DAY * ndays
    queryString = {
        LOCATION_MESSAGE_ID: locationMessageId,
        TIME: {
            '$gte': tend
        },
        FREQ_RANGE: msgutils.freqRange(sys2detect, fmin, fmax)
    }
    msg = DbCollections.getDataMessages(sensorId).find_one(queryString)
    if msg is None:
        result["nextTmin"] = tmin
    else:
        nextTmin = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            msg[TIME], tZId)
        result["nextTmin"] = nextTmin
    # Now compute the previous interval before this one.
    prevMessage = msgutils.getPrevAcquisition(startMessage)
    if prevMessage is not None:
        newTmin = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            prevMessage[TIME] - SECONDS_PER_DAY * ndays, tZId)
        queryString = {
            LOCATION_MESSAGE_ID: locationMessageId,
            TIME: {
                '$gte': newTmin
            },
            FREQ_RANGE: msgutils.freqRange(sys2detect, fmin, fmax)
        }
        msg = DbCollections.getDataMessages(sensorId).find_one(queryString)
    else:
        msg = startMessage
    sensor = SensorDb.getSensorObj(sensorId)
    channelCount = sensor.getChannelCount(sys2detect, fmin, fmax)
    result[STATUS] = OK
    result["prevTmin"] = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        msg[TIME], tZId)
    result["tmin"] = tmin
    result["maxFreq"] = fmin
    result["minFreq"] = fmax
    result["cutoff"] = cutoff
    result[CHANNEL_COUNT] = channelCount
    result["startDate"] = timezone.formatTimeStampLong(tmin, tZId)
    result["values"] = values
    util.debugPrint(result)
    return result
def generateSingleDaySpectrogramAndOccupancyForSweptFrequency(
        sensorId, lat, lon, alt, sessionId, startTime, sys2detect, fstart,
        fstop, subBandMinFreq, subBandMaxFreq, cutoff):
    """
    Generate single day spectrogram and occupancy for SweptFrequency

    Parameters:

    - msg: the data message
    - sessionId: login session id.
    - startTime: absolute start time.
    - sys2detect: the system to detect.
    - fstart: start frequency.
    - fstop: stop frequency
    - subBandMinFreq: min freq of subband.
    - subBandMaxFreq: max freq of subband.
    - cutoff: occupancy threshold.

    """
    try:
        chWidth = Config.getScreenConfig()[CHART_WIDTH]
        chHeight = Config.getScreenConfig()[CHART_HEIGHT]

        locationMessage = DbCollections.getLocationMessages().find_one({
            SENSOR_ID:
            sensorId,
            LAT:
            lat,
            LON:
            lon,
            ALT:
            alt
        })
        if locationMessage is None:
            return {STATUS: NOK, ERROR_MESSAGE: "Location message not found"}

        tz = locationMessage[TIME_ZONE_KEY]
        startTimeUtc = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            startTime, tz)
        startMsg = DbCollections.\
            getDataMessages(sensorId).find_one(
                {TIME:{"$gte":startTimeUtc},
                 LOCATION_MESSAGE_ID:str(locationMessage["_id"]),
                 FREQ_RANGE:msgutils.freqRange(sys2detect, fstart, fstop)})
        if startMsg is None:
            util.debugPrint("Not found")
            return {STATUS: NOK, ERROR_MESSAGE: "Data Not Found"}
        if DataMessage.getTime(startMsg) - startTimeUtc > SECONDS_PER_DAY:
            util.debugPrint("Not found - outside day boundary: " +
                            str(startMsg['t'] - startTimeUtc))
            return {
                STATUS: NOK,
                ERROR_MESSAGE: "Not found - outside day boundary."
            }

        msg = startMsg
        sensorId = msg[SENSOR_ID]
        powerValues = msgutils.trimSpectrumToSubBand(msg, subBandMinFreq,
                                                     subBandMaxFreq)
        vectorLength = len(powerValues)
        if cutoff is None:
            cutoff = DataMessage.getThreshold(msg)
        else:
            cutoff = int(cutoff)
        spectrogramFile = sessionId + "/" + sensorId + "." + str(
            startTimeUtc) + "." + str(cutoff) + "." + str(
                subBandMinFreq) + "." + str(subBandMaxFreq)
        spectrogramFilePath = util.getPath(
            STATIC_GENERATED_FILE_LOCATION) + spectrogramFile
        powerVal = np.array(
            [cutoff for i in range(0, MINUTES_PER_DAY * vectorLength)])
        spectrogramData = powerVal.reshape(vectorLength, MINUTES_PER_DAY)
        # artificial power value when sensor is off.
        sensorOffPower = np.transpose(
            np.array([2000 for i in range(0, vectorLength)]))

        prevMessage = msgutils.getPrevAcquisition(msg)

        if prevMessage is None:
            util.debugPrint("prevMessage not found")
            prevMessage = msg
            prevAcquisition = sensorOffPower
        else:
            prevAcquisitionTime = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
                prevMessage['t'], tz)
            util.debugPrint("prevMessage[t] " + str(prevMessage['t']) +
                            " msg[t] " + str(msg['t']) + " prevDayBoundary " +
                            str(prevAcquisitionTime))
            prevAcquisition = np.transpose(
                np.array(
                    msgutils.trimSpectrumToSubBand(prevMessage, subBandMinFreq,
                                                   subBandMaxFreq)))
        occupancy = []
        timeArray = []
        maxpower = -1000
        minpower = 1000
        # Note that we are starting with the first message.
        count = 1
        while True:
            acquisition = msgutils.trimSpectrumToSubBand(
                msg, subBandMinFreq, subBandMaxFreq)
            occupancyCount = float(
                len(filter(lambda x: x >= cutoff, acquisition)))
            occupancyVal = occupancyCount / float(len(acquisition))
            occupancy.append(occupancyVal)
            minpower = np.minimum(minpower, msgutils.getMinPower(msg))
            maxpower = np.maximum(maxpower, msgutils.getMaxPower(msg))
            if prevMessage['t1'] != msg['t1']:
                # GAP detected so fill it with sensorOff
                sindex = get_index(DataMessage.getTime(prevMessage),
                                   startTimeUtc)
                if get_index(DataMessage.getTime(prevMessage),
                             startTimeUtc) < 0:
                    sindex = 0
                for i in range(
                        sindex,
                        get_index(DataMessage.getTime(msg), startTimeUtc)):
                    spectrogramData[:, i] = sensorOffPower
            elif DataMessage.getTime(prevMessage) > startTimeUtc:
                # Prev message is the same tstart and prevMessage is in the range of interest.
                # Sensor was not turned off.
                # fill forward using the prev acquisition.
                for i in range(
                        get_index(DataMessage.getTime(prevMessage),
                                  startTimeUtc),
                        get_index(msg["t"], startTimeUtc)):
                    spectrogramData[:, i] = prevAcquisition
            else:
                # forward fill from prev acquisition to the start time
                # with the previous power value
                for i in range(
                        0, get_index(DataMessage.getTime(msg), startTimeUtc)):
                    spectrogramData[:, i] = prevAcquisition
            colIndex = get_index(DataMessage.getTime(msg), startTimeUtc)
            spectrogramData[:, colIndex] = acquisition
            timeArray.append(
                float(DataMessage.getTime(msg) - startTimeUtc) / float(3600))
            prevMessage = msg
            prevAcquisition = acquisition
            msg = msgutils.getNextAcquisition(msg)
            if msg is None:
                lastMessage = prevMessage
                for i in range(
                        get_index(DataMessage.getTime(prevMessage),
                                  startTimeUtc), MINUTES_PER_DAY):
                    spectrogramData[:, i] = sensorOffPower
                break
            elif DataMessage.getTime(msg) - startTimeUtc >= SECONDS_PER_DAY:
                if msg['t1'] == prevMessage['t1']:
                    for i in range(
                            get_index(DataMessage.getTime(prevMessage),
                                      startTimeUtc), MINUTES_PER_DAY):
                        spectrogramData[:, i] = prevAcquisition
                else:
                    for i in range(
                            get_index(DataMessage.getTime(prevMessage),
                                      startTimeUtc), MINUTES_PER_DAY):
                        spectrogramData[:, i] = sensorOffPower

                lastMessage = prevMessage
                break
            count = count + 1

        # generate the spectrogram as an image.
        if not os.path.exists(spectrogramFilePath + ".png"):
            fig = plt.figure(figsize=(chWidth, chHeight))
            frame1 = plt.gca()
            frame1.axes.get_xaxis().set_visible(False)
            frame1.axes.get_yaxis().set_visible(False)
            cmap = plt.cm.spectral
            cmap.set_under(UNDER_CUTOFF_COLOR)
            cmap.set_over(OVER_CUTOFF_COLOR)
            dirname = util.getPath(STATIC_GENERATED_FILE_LOCATION) + sessionId
            if maxpower < cutoff:
                maxpower = cutoff
                minpower = cutoff
            if not os.path.exists(dirname):
                os.makedirs(dirname)
            fig = plt.imshow(spectrogramData,
                             interpolation='none',
                             origin='lower',
                             aspect='auto',
                             vmin=cutoff,
                             vmax=maxpower,
                             cmap=cmap)
            util.debugPrint("Generated fig")
            plt.savefig(spectrogramFilePath + '.png',
                        bbox_inches='tight',
                        pad_inches=0,
                        dpi=100)
            plt.clf()
            plt.close()
        else:
            util.debugPrint("File exists - not generating image")

        util.debugPrint("FileName: " + spectrogramFilePath + ".png")

        util.debugPrint("Reading " + spectrogramFilePath + ".png")
        # get the size of the generated png.
        reader = png.Reader(filename=spectrogramFilePath + ".png")
        (width, height, pixels, metadata) = reader.read()

        util.debugPrint("width = " + str(width) + " height = " + str(height))

        # generate the colorbar as a separate image.
        if not os.path.exists(spectrogramFilePath + ".cbar.png"):
            norm = mpl.colors.Normalize(vmin=cutoff, vmax=maxpower)
            fig = plt.figure(figsize=(chWidth * 0.3, chHeight * 1.2))
            ax1 = fig.add_axes([0.0, 0, 0.1, 1])
            mpl.colorbar.ColorbarBase(ax1,
                                      cmap=cmap,
                                      norm=norm,
                                      orientation='vertical')
            plt.savefig(spectrogramFilePath + '.cbar.png',
                        bbox_inches='tight',
                        pad_inches=0,
                        dpi=50)
            plt.clf()
            plt.close()
        else:
            util.debugPrint(spectrogramFilePath + ".cbar.png" +
                            " exists -- not generating")

        localTime, tzName = timezone.getLocalTime(startTimeUtc, tz)

        # step back for 24 hours.
        prevAcquisitionTime = msgutils.getPrevDayBoundary(startMsg)
        nextAcquisitionTime = msgutils.getNextDayBoundary(lastMessage)
        meanOccupancy = np.mean(occupancy)
        maxOccupancy = np.max(occupancy)
        minOccupancy = np.min(occupancy)
        medianOccupancy = np.median(occupancy)

        result = {
            "spectrogram":
            Config.getGeneratedDataPath() + "/" + spectrogramFile + ".png",
            "cbar": Config.getGeneratedDataPath() + "/" + spectrogramFile +
            ".cbar.png",
            "maxPower": maxpower,
            "maxOccupancy": maxOccupancy,
            "minOccupancy": minOccupancy,
            "meanOccupancy": meanOccupancy,
            "medianOccupancy": medianOccupancy,
            "cutoff": cutoff,
            "aquisitionCount": count,
            "minPower": minpower,
            "tStartTimeUtc": startTimeUtc,
            "timeDelta": HOURS_PER_DAY,
            "prevAcquisition": prevAcquisitionTime,
            "nextAcquisition": nextAcquisitionTime,
            "formattedDate": timezone.formatTimeStampLong(startTimeUtc, tz),
            "image_width": float(width),
            "image_height": float(height)
        }

        result["timeArray"] = timeArray
        result["occupancyArray"] = occupancy
        if "ENBW" in lastMessage["mPar"]:
            enbw = lastMessage["mPar"]["ENBW"]
            result["ENBW"] = enbw

        if "RBW" in lastMessage["mPar"]:
            rbw = lastMessage["mPar"]["RBW"]
            result["RBW"] = rbw
        result[STATUS] = OK
        util.debugPrint(result)
        return result
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
        raise
예제 #10
0
def generatePowerVsTimeForSweptFrequency(sensorId, startTime, freqHz,
                                         sessionId):
    """
    generate a power vs. time plot for swept frequency readings.
    The plot is generated for a period of one day.
    """
    chWidth = Config.getScreenConfig()[CHART_WIDTH]
    chHeight = Config.getScreenConfig()[CHART_HEIGHT]

    dataMessages = DbCollections.getDataMessages(sensorId)
    if dataMessages is None:
        return {
            STATUS: NOK,
            ERROR_MESSAGE: "Data Message Collection not found"
        }
    msg = dataMessages.find_one({
        SENSOR_ID: sensorId,
        "t": {
            "$gt": int(startTime)
        }
    })
    (maxFreq, minFreq) = msgutils.getMaxMinFreq(msg)
    locationMessage = msgutils.getLocationMessage(msg)
    timeZone = locationMessage[TIME_ZONE_KEY]
    if freqHz > maxFreq:
        freqHz = maxFreq
    if freqHz < minFreq:
        freqHz = minFreq
    n = int(msg["mPar"]["n"])
    freqIndex = int(
        float(freqHz - minFreq) / float(maxFreq - minFreq) * float(n))
    powerArray = []
    timeArray = []
    startTime = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        msg['t'], timeZone)
    while True:
        data = msgutils.getData(msg)
        powerArray.append(data[freqIndex])
        timeArray.append(float(msg['t'] - startTime) / float(3600))
        nextMsg = msgutils.getNextAcquisition(msg)
        if nextMsg is None:
            break
        elif nextMsg['t'] - startTime > SECONDS_PER_DAY:
            break
        else:
            msg = nextMsg

    plt.figure(figsize=(chWidth, chHeight))
    plt.xlim([0, 23])
    freqMHz = float(freqHz) / 1E6
    title = "Power vs. Time at " + str(freqMHz) + " MHz"
    plt.title(title)
    xlabel = "Time (H) from start of day"
    plt.xlabel(xlabel)
    ylabel = "Signal Power (dBm)"
    plt.ylabel(ylabel)
    plt.xlim([0, 23])
    plt.scatter(timeArray, powerArray)
    spectrumFile = sessionId + "/" + msg[SENSOR_ID] + "." + str(
        startTime) + "." + str(freqMHz) + ".power.png"
    spectrumFilePath = util.getPath(
        STATIC_GENERATED_FILE_LOCATION) + spectrumFile
    plt.savefig(spectrumFilePath, pad_inches=0, dpi=100)
    plt.clf()
    plt.close()
    retval = {
        STATUS: OK,
        "powervstime": Config.getGeneratedDataPath() + "/" + spectrumFile,
        "timeArray": timeArray,
        "powerValues": powerArray,
        "title": title,
        "xlabel": xlabel,
        "ylabel": ylabel
    }
    return retval
예제 #11
0
def getBandDataSummary(sensorId,
                       locationMessage,
                       sys2detect,
                       minFreq,
                       maxFreq,
                       mintime,
                       dayCount=None):
    sensor = SensorDb.getSensorObj(sensorId)

    if sensor is None:
        return {STATUS: NOK, ERROR_MESSAGE: "Sensor Not found"}
    measurementType = sensor.getMeasurementType()

    tzId = locationMessage[TIME_ZONE_KEY]
    locationMessageId = str(locationMessage["_id"])

    freqRange = msgutils.freqRange(sys2detect, minFreq, maxFreq)
    count = LocationMessage.getBandCount(locationMessage, freqRange)
    if count == 0:
        return {
            FREQ_RANGE: freqRange,
            COUNT: 0,
            "minFreq": minFreq,
            "maxFreq": maxFreq,
            SYSTEM_TO_DETECT: sys2detect
        }
    else:
        minOccupancy = LocationMessage.getMinBandOccupancy(
            locationMessage, freqRange)
        maxOccupancy = LocationMessage.getMaxBandOccupancy(
            locationMessage, freqRange)
        count = LocationMessage.getBandCount(locationMessage, freqRange)
        meanOccupancy = LocationMessage.getMeanOccupancy(
            locationMessage, freqRange)
        minTime = LocationMessage.getFirstMessageTimeStampForBand(
            locationMessage, freqRange)
        maxTime = LocationMessage.getLastMessageTimeStampForBand(
            locationMessage, freqRange)

        maxTimes = timezone.getLocalTime(maxTime, tzId)
        (tEndReadingsLocalTime, tEndReadingsLocalTimeTzName) = maxTimes

        tEndDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            maxTime, tzId)
        tStartDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            minTime, tzId)

        tstampMin = timezone.formatTimeStampLong(minTime, tzId)
        tstampMax = timezone.formatTimeStampLong(maxTime, tzId)
        retval = {
            "tStartDayBoundary": tStartDayBoundary,
            "tEndDayBoundary": tEndDayBoundary,
            "tStartReadings": minTime,
            "tStartLocalTime": minTime,
            "tStartLocalTimeFormattedTimeStamp": tstampMin,
            "tEndReadings": maxTime,
            "tEndReadingsLocalTime": maxTime,
            "tEndLocalTimeFormattedTimeStamp": tstampMax,
            "tEndDayBoundary": tEndDayBoundary,
            "maxOccupancy": maxOccupancy,
            "meanOccupancy": meanOccupancy,
            "minOccupancy": minOccupancy,
            "maxFreq": maxFreq,
            "minFreq": minFreq,
            SYSTEM_TO_DETECT: sys2detect,
            FREQ_RANGE: freqRange,
            "measurementType": measurementType,
            "active": sensor.isBandActive(sys2detect, minFreq, maxFreq),
            COUNT: count
        }
        return retval
예제 #12
0
def getDataSummaryForAllBands(sensorId,
                              locationMessage,
                              tmin=None,
                              dayCount=None):
    """
    get the summary of the data corresponding to the location message.
    """
    # tmin and tmax are the min and the max of the time range of interest.
    locationMessageId = str(locationMessage["_id"])

    tzId = locationMessage[TIME_ZONE_KEY]
    sensor = SensorDb.getSensor(sensorId)
    if sensor is None:
        return {STATUS: NOK, ERROR_MESSAGE: "Sensor Not found in SensorDb"}
    bands = sensor[THRESHOLDS]
    if len(bands.keys()) == 0:
        return {STATUS: NOK, ERROR_MESSAGE: "Sensor has no bands"}
    measurementType = sensor[MEASUREMENT_TYPE]
    bandStatistics = []
    query = {SENSOR_ID: sensorId, "locationMessageId": locationMessageId}
    msg = DbCollections.getDataMessages(sensorId).find_one(query)
    if msg is None:
        for key in bands.keys():
            band = bands[key]
            minFreq = band[MIN_FREQ_HZ]
            maxFreq = band[MAX_FREQ_HZ]
            sys2detect = band[SYSTEM_TO_DETECT]
            isActive = band[ACTIVE]
            bandInfo = {
                "tStartDayBoundary": 0,
                "tEndDayBoundary": 0,
                "tStartReadings": 0,
                "tStartLocalTime": 0,
                "tStartLocalTimeFormattedTimeStamp": UNKNOWN,
                "tEndReadings": 0,
                "tEndReadingsLocalTime": 0,
                "tEndLocalTimeFormattedTimeStamp": UNKNOWN,
                "tEndDayBoundary": 0,
                "maxOccupancy": 0,
                "meanOccupancy": 0,
                "minOccupancy": 0,
                "maxFreq": maxFreq,
                "minFreq": minFreq,
                SYSTEM_TO_DETECT: sys2detect,
                "measurementType": measurementType,
                "active": isActive,
                COUNT: 0
            }

            bandStatistics.append(bandInfo)
        return {STATUS: "OK", "bands": bandStatistics}

    if tmin is None and dayCount is None:
        query = {SENSOR_ID: sensorId, "locationMessageId": locationMessageId}
        tmin = msgutils.getDayBoundaryTimeStamp(msg)
        mintime = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            int(tmin), tzId)
    elif tmin is not None and dayCount is None:
        mintime = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            int(tmin), tzId)
    else:
        mintime = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            int(tmin), tzId)

    for key in bands.keys():
        band = bands[key]
        minFreq = band[MIN_FREQ_HZ]
        maxFreq = band[MAX_FREQ_HZ]
        sys2detect = band[SYSTEM_TO_DETECT]
        bandSummary = getBandDataSummary(sensorId,
                                         locationMessage,
                                         sys2detect,
                                         minFreq,
                                         maxFreq,
                                         mintime,
                                         dayCount=dayCount)
        bandStatistics.append(bandSummary)

    return {STATUS: "OK", "bands": bandStatistics}
예제 #13
0
def getOneDayStats(sensorId, lat, lon, alt, startTime, sys2detect, minFreq,
                   maxFreq):
    """
    Generate and return a JSON structure with the one day statistics.

    startTime is the start time in UTC
    sys2detect is the system to detect.
    minFreq is the minimum frequency of the frequency band of interest.
    maxFreq is the maximum frequency of the frequency band of interest.

    """
    locationMessage = DbCollections.getLocationMessages().find_one({
        SENSOR_ID: sensorId,
        LAT: lat,
        LON: lon,
        ALT: alt
    })
    if locationMessage is None:
        return {STATUS: NOK, ERROR_MESSAGE: "No location information"}
    freqRange = msgutils.freqRange(sys2detect, minFreq, maxFreq)
    mintime = int(startTime)
    maxtime = mintime + SECONDS_PER_DAY
    tzId = locationMessage[TIME_ZONE_KEY]
    mintime = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(startTime, tzId)
    maxtime = mintime + SECONDS_PER_DAY
    query = {
        SENSOR_ID: sensorId,
        LOCATION_MESSAGE_ID: str(locationMessage["_id"]),
        "t": {
            "$lte": maxtime,
            "$gte": mintime
        },
        FREQ_RANGE: freqRange
    }
    cur = DbCollections.getDataMessages(sensorId).find(query)
    if cur is None or cur.count() == 0:
        return {STATUS: NOK, ERROR_MESSAGE: "Data messages not found"}
    res = {}
    values = {}
    res["formattedDate"] = timezone.formatTimeStampLong(
        mintime, locationMessage[TIME_ZONE_KEY])
    acquisitionCount = cur.count()
    prevMsg = None
    for msg in cur:
        if prevMsg is None:
            prevMsg = msgutils.getPrevAcquisition(msg)
        channelCount = msg["mPar"]["n"]
        measurementsPerAcquisition = msg["nM"]
        cutoff = msg["cutoff"]
        values[int(msg["t"] - mintime)] = {
            "t": msg["t"],
            "maxPower": msg["maxPower"],
            "minPower": msg["minPower"],
            "maxOccupancy": msg["maxOccupancy"],
            "minOccupancy": msg["minOccupancy"],
            "meanOccupancy": msg["meanOccupancy"],
            "medianOccupancy": msg["medianOccupancy"]
        }
    query = {SENSOR_ID: sensorId, "t": {"$gt": maxtime}, FREQ_RANGE: freqRange}
    msg = DbCollections.getDataMessages(sensorId).find_one(query)
    if msg is not None:
        nextDay = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            msg["t"], tzId)
    else:
        nextDay = mintime
    if prevMsg is not None:
        prevDayBoundary = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            prevMsg["t"], tzId)
        query = {
            SENSOR_ID: sensorId,
            "t": {
                "$gte": prevDayBoundary
            },
            FREQ_RANGE: freqRange
        }
        msg = DbCollections.getDataMessages(sensorId).find_one(query)
        prevDay = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            msg["t"], tzId)
    else:
        prevDay = mintime
    res["nextIntervalStart"] = nextDay
    res["prevIntervalStart"] = prevDay
    res["currentIntervalStart"] = mintime
    res[CHANNEL_COUNT] = channelCount
    res["measurementsPerAcquisition"] = measurementsPerAcquisition
    res[ACQUISITION_COUNT] = acquisitionCount
    res["cutoff"] = cutoff
    res["values"] = values
    res[STATUS] = OK
    return res
예제 #14
0
def getHourlyMaxMinMeanStats(sensorId, startTime, sys2detect, fmin, fmax,
                             subBandMinFreq, subBandMaxFreq, sessionId):

    sensor = SensorDb.getSensor(sensorId)
    if sensor is None:
        return {STATUS: NOK, ERROR_MESSAGE: "Sensor Not Found"}

    tstart = int(startTime)
    fmin = int(subBandMinFreq)
    fmax = int(subBandMaxFreq)
    freqRange = msgutils.freqRange(sys2detect, fmin, fmax)

    queryString = {
        SENSOR_ID: sensorId,
        TIME: {
            '$gte': tstart
        },
        FREQ_RANGE: freqRange
    }
    util.debugPrint(queryString)

    startMessage = DbCollections.getDataMessages(sensorId).find_one(
        queryString)
    if startMessage is None:
        errorStr = "Start Message Not Found"
        util.debugPrint(errorStr)
        response = {STATUS: NOK, ERROR_MESSAGE: "No data found"}
        return response

    locationMessageId = DataMessage.getLocationMessageId(startMessage)

    retval = {STATUS: OK}
    values = {}
    locationMessage = DbCollections.getLocationMessages().find_one(
        {"_id": locationMessageId})

    tZId = LocationMessage.getTimeZone(locationMessage)

    tmin = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        tstart, LocationMessage.getTimeZone(locationMessage))

    for hour in range(0, 23):
        dataMessages = DbCollections.getDataMessages(sensorId).find({
            "t": {
                "$gte": tmin + hour * SECONDS_PER_HOUR
            },
            "t": {
                "$lte": (hour + 1) * SECONDS_PER_HOUR
            },
            FREQ_RANGE:
            freqRange
        })
        if dataMessages is not None:
            stats = compute_stats_for_fft_power(dataMessages)
            (nChannels, maxFreq, minFreq, cutoff, result) = stats
            values[hour] = result

    retval["values"] = values

    # Now compute the next interval after the last one (if one exists)
    tend = tmin + SECONDS_PER_DAY
    queryString = {
        SENSOR_ID: sensorId,
        TIME: {
            '$gte': tend
        },
        FREQ_RANGE: freqRange
    }
    msg = DbCollections.getDataMessages(sensorId).find_one(queryString)
    if msg is None:
        result["nextTmin"] = tmin
    else:
        nextTmin = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            msg[TIME], tZId)
        result["nextTmin"] = nextTmin
    # Now compute the previous interval before this one.
    prevMessage = msgutils.getPrevAcquisition(startMessage)
    if prevMessage is not None:
        newTmin = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
            prevMessage[TIME] - SECONDS_PER_DAY, tZId)
        queryString = {
            SENSOR_ID: sensorId,
            TIME: {
                '$gte': newTmin
            },
            FREQ_RANGE: msgutils.freqRange(sys2detect, fmin, fmax)
        }
        msg = DbCollections.getDataMessages(sensorId).find_one(queryString)
    else:
        msg = startMessage
    result[STATUS] = OK
    result["prevTmin"] = timezone.getDayBoundaryTimeStampFromUtcTimeStamp(
        msg[TIME], tZId)
    result["tmin"] = tmin
    result["maxFreq"] = maxFreq
    result["minFreq"] = minFreq
    result["cutoff"] = cutoff
    result[CHANNEL_COUNT] = nChannels
    result["startDate"] = timezone.formatTimeStampLong(tmin, tZId)
    result["values"] = values
    return result