예제 #1
0
 def isBandActive(self, sys2detect, minFreq, maxFreq):
     thresholds = self.sensor[SENSOR_THRESHOLDS]
     bandName = msgutils.freqRange(sys2detect, minFreq, maxFreq)
     if bandName not in self.sensor[SENSOR_THRESHOLDS]:
         return False
     else:
         return self.sensor[SENSOR_THRESHOLDS][bandName]["active"]
def checkForDataAvailability(sensorId, startTime, days, sys2detect, minFreq,
                             maxFreq):
    endTime = int(startTime) + int(days) * SECONDS_PER_DAY
    freqRange = msgutils.freqRange(sys2detect, int(minFreq), int(maxFreq))
    query = {SENSOR_ID: sensorId,
             "t": {"$lte": int(endTime)},
             "t": {"$gte": int(startTime)},
             FREQ_RANGE: freqRange}
    firstMessage = DbCollections.getDataMessages(sensorId).find_one(query)
    if firstMessage is None:
        util.debugPrint("checkForDataAvailability: returning false")
        return False
    else:
        util.debugPrint("checkForDataAvailability: returning true")
        return True
예제 #3
0
def getOccupancies(sensorId, sys2detect, minFreq, maxFreq, startTime, seconds,
                   sessionId):
    freqRange = msgutils.freqRange(sys2detect, minFreq, maxFreq)
    dataMessages = DbCollections.getDataMessages(sensorId)
    dataMessage = dataMessages.find_one({})
    if dataMessages is None:
        return {STATUS: "NOK", STATUS_MESSAGE: "No Data Found"}
    endTime = startTime + seconds
    query = {
        SENSOR_ID: sensorId,
        FREQ_RANGE: freqRange,
        "$and": [{
            TIME: {
                "$gte": startTime
            }
        }, {
            TIME: {
                "$lte": endTime
            }
        }]
    }
    # print query
    cur = dataMessages.find(query)
    if cur is None or cur.count() == 0:
        return {STATUS: "NOK", STATUS_MESSAGE: "No Data Found"}
    occupancyFileName = sessionId + "/" + sensorId + ":" + freqRange + ".occupancy." + str(
        startTime) + "-" + str(seconds) + ".txt"
    if not os.path.exists(
            util.getPath(STATIC_GENERATED_FILE_LOCATION) + sessionId):
        os.mkdir(util.getPath(STATIC_GENERATED_FILE_LOCATION) + sessionId)
    occupancyFileUrl = Config.getGeneratedDataPath() + "/" + occupancyFileName
    occupancyFilePath = util.getPath(
        STATIC_GENERATED_FILE_LOCATION) + occupancyFileName
    occupancyFile = open(occupancyFilePath, "w")
    timeFileName = sessionId + "/" + sensorId + ":" + freqRange + ".occupancy.time." + str(
        startTime) + "-" + str(seconds) + ".txt"
    if not os.path.exists(
            util.getPath(STATIC_GENERATED_FILE_LOCATION) + sessionId):
        os.mkdir(util.getPath(STATIC_GENERATED_FILE_LOCATION) + sessionId)
    timeFileUrl = Config.getGeneratedDataPath() + "/" + timeFileName
    timeFilePath = util.getPath(STATIC_GENERATED_FILE_LOCATION) + timeFileName
    timeFile = open(timeFilePath, "w")
    tm = None
    timeSinceStart = 0
    try:
        for dataMessage in cur:
            del dataMessage["_id"]
            # print dumps(dataMessage,indent = 4)
            nM = DataMessage.getNumberOfMeasurements(dataMessage)
            td = DataMessage.getMeasurementDuration(dataMessage)
            tm = DataMessage.getTimePerMeasurement(dataMessage)
            occupancyStartTime = dataMessage[TIME]
            occupancyEndTime = occupancyStartTime + nM * tm
            occupancyData = msgutils.getOccupancyData(dataMessage)
            secondsPerEntry = float(td) / float(nM)

            if startTime <= occupancyStartTime and endTime >= occupancyEndTime:
                sindex = 0
                findex = nM
            elif startTime > occupancyStartTime and endTime < occupancyEndTime:
                sindex = int(
                    (startTime - occupancyStartTime) / secondsPerEntry)
                findex = int(nM -
                             (occupancyEndTime - endTime) / secondsPerEntry)
            elif startTime >= occupancyStartTime:
                # print "Case 3 ", startTime, occupancyStartTime
                sindex = int(
                    (startTime - occupancyStartTime) / secondsPerEntry)
                findex = nM
            elif endTime <= occupancyEndTime:
                sindex = 0
                findex = int(nM -
                             (occupancyEndTime - endTime) / secondsPerEntry)
            timeSinceStart = timeSinceStart + sindex * tm
            print "sindex/findex", sindex, findex
            for i in range(sindex, findex):
                occupancy = str(int(occupancyData[i]))
                occupancyFile.write(occupancy + "\n")
            for i in range(sindex, findex):
                timeFile.write(str(timeSinceStart) + "\n")
                timeSinceStart = timeSinceStart + tm
        occupancyFile.close()
        timeFile.close()
        return {
            STATUS: "OK",
            OCCUPANCY_FILE_URL: occupancyFileUrl,
            TIME_FILE_URL: timeFileUrl
        }

    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
    finally:
        timeFile.close()
        occupancyFile.close()
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
예제 #6
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
예제 #7
0
def getAcquistionCount(sensorId, lat, lon, alt, sys2detect, minfreq, maxfreq,
                       tAcquistionStart, dayCount):
    """
    Get the acquisition count of a sensor given its location and band of interest.
    """
    locationMessage = DbCollections.getLocationMessages().find_one({
        SENSOR_ID: sensorId,
        LAT: lat,
        LON: lon,
        ALT: alt
    })
    if locationMessage is None:
        retval = {COUNT: 0}
        retval[STATUS] = "OK"
        return retval

    freqRange = msgutils.freqRange(sys2detect, minfreq, maxfreq)
    query = {
        SENSOR_ID: sensorId,
        LOCATION_MESSAGE_ID: str(locationMessage["_id"]),
        "t": {
            "$gte": tAcquistionStart
        },
        FREQ_RANGE: freqRange
    }
    msg = DbCollections.getDataMessages(sensorId).find_one(query)
    if msg is None:
        retval = {COUNT: 0}
        retval[STATUS] = "OK"
        return retval

    startTime = msgutils.getDayBoundaryTimeStamp(msg)

    if dayCount > 0:
        endTime = startTime + SECONDS_PER_DAY * dayCount
        query = {
            SENSOR_ID: sensorId,
            LOCATION_MESSAGE_ID: str(locationMessage["_id"]),
            TIME: {
                "$gte": startTime
            },
            TIME: {
                "$lte": endTime
            },
            FREQ_RANGE: freqRange
        }
    else:
        query = {
            SENSOR_ID: sensorId,
            LOCATION_MESSAGE_ID: str(locationMessage["_id"]),
            TIME: {
                "$gte": startTime
            },
            FREQ_RANGE: freqRange
        }

    cur = DbCollections.getDataMessages(sensorId).find(query)
    count = cur.count()

    cur.sort(TIME, pymongo.DESCENDING).limit(2)
    lastMessage = cur.next()
    endTime = msgutils.getDayBoundaryTimeStamp(lastMessage)

    retval = {COUNT: count}
    retval["tStartReadings"] = msg[TIME]
    retval["tEndReadings"] = lastMessage[TIME]
    retval["tStartDayBoundary"] = startTime
    retval["tEndDayBoundary"] = endTime
    query = {
        SENSOR_ID: sensorId,
        TIME: {
            "$gte": startTime
        },
        FREQ_RANGE: freqRange
    }
    cur = DbCollections.getDataMessages(sensorId).find(query)
    cur.sort(TIME, pymongo.DESCENDING).limit(2)
    lastMessage = cur.next()
    endTime = msgutils.getDayBoundaryTimeStamp(lastMessage)
    retval["tEndReadingsDayBoundary"] = endTime

    retval[STATUS] = "OK"
    return retval
def generateZipFile(sensorId, startTime, days, sys2detect, minFreq, maxFreq,
                    dumpFileNamePrefix, sessionId):
    util.debugPrint("generateZipFile: " + sensorId + "/" + str(days) + "/" +
                    str(minFreq) + "/" + str(maxFreq) + "/" + sessionId)
    dumpFileName = sessionId + "/" + dumpFileNamePrefix + ".txt"
    zipFileName = sessionId + "/" + dumpFileNamePrefix + ".zip"
    dirname = util.getPath(STATIC_GENERATED_FILE_LOCATION + sessionId)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    dumpFilePath = util.getPath(STATIC_GENERATED_FILE_LOCATION) + dumpFileName
    zipFilePath = util.getPath(STATIC_GENERATED_FILE_LOCATION) + zipFileName
    if os.path.exists(dumpFilePath):
        os.remove(dumpFilePath)
    if os.path.exists(zipFilePath):
        os.remove(zipFilePath)
    endTime = int(startTime) + int(days) * SECONDS_PER_DAY
    freqRange = msgutils.freqRange(sys2detect, int(minFreq), int(maxFreq))
    query = {SENSOR_ID: sensorId,
             "t": {"$lte": int(endTime)},
             "t": {"$gte": int(startTime)},
             FREQ_RANGE: freqRange}
    firstMessage = DbCollections.getDataMessages(sensorId).find_one(query)

    if firstMessage is None:
        util.debugPrint("No data found")
        return

    locationMessage = msgutils.getLocationMessage(firstMessage)

    if locationMessage is None:
        util.debugPrint("generateZipFileForDownload: No location info found")
        return

    systemMessage = DbCollections.getSystemMessages().find_one({SENSOR_ID: sensorId})
    if systemMessage is None:
        util.debugPrint("generateZipFileForDownload: No system info found")
        return

    dumpFile = open(dumpFilePath, "a")
    zipFile = zipfile.ZipFile(zipFilePath, mode="w")
    try:
        # Write out the system message.
        data = msgutils.getCalData(systemMessage)
        systemMessage[DATA_TYPE] = ASCII
        if CAL in systemMessage and DATA_KEY in systemMessage[CAL]:
            del systemMessage[CAL][DATA_KEY]
        del systemMessage["_id"]
        systemMessageString = json.dumps(systemMessage,
                                         sort_keys=False,
                                         indent=4)
        length = len(systemMessageString)
        dumpFile.write(str(length))
        dumpFile.write("\n")
        dumpFile.write(systemMessageString)
        if data is not None:
            dataString = str(data)
            dumpFile.write(dataString)

        # Write out the location message.
        del locationMessage["_id"]
        locationMessageString = json.dumps(locationMessage,
                                           sort_keys=False,
                                           indent=4)
        locationMessageLength = len(locationMessageString)
        dumpFile.write(str(locationMessageLength))
        dumpFile.write("\n")
        dumpFile.write(locationMessageString)

        # Write out the data messages one at a time
        c = DbCollections.getDataMessages(sensorId).find(query)
        for dataMessage in c:
            data = msgutils.getData(dataMessage)
            # delete fields we don't want to export
            del dataMessage["_id"]
            del dataMessage["locationMessageId"]
            del dataMessage[DATA_KEY]
            del dataMessage["cutoff"]
            dataMessage["Compression"] = "None"
            dataMessageString = json.dumps(dataMessage,
                                           sort_keys=False,
                                           indent=4)
            length = len(dataMessageString)
            dumpFile.write(str(length))
            dumpFile.write("\n")
            dumpFile.write(dataMessageString)
            if dataMessage[DATA_TYPE] == ASCII:
                dumpFile.write(str(data))
            elif dataMessage[DATA_TYPE] == BINARY_INT8:
                for dataByte in data:
                    dumpFile.write(struct.pack('b', dataByte))
            elif dataMessage[DATA_TYPE] == BINARY_INT16:
                for dataWord in data:
                    dumpFile.write(struct.pack('i', dataWord))
            elif dataMessage[DATA_TYPE] == BINARY_FLOAT32:
                for dataWord in data:
                    dumpFile.write(struct.pack('f', dataWord))
        zipFile.write(dumpFilePath,
                      arcname=dumpFileNamePrefix + ".txt",
                      compress_type=zipfile.ZIP_DEFLATED)
        zipFile.close()
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print sys.exc_info()
        traceback.print_exc()
        util.logStackTrace(sys.exc_info())
    finally:
        dumpFile.close()
        os.remove(dumpFilePath)
        zipFile.close()
예제 #9
0
 def getChannelCount(self, sys2detect, minFreq, maxFreq):
     thresholds = self.sensor[SENSOR_THRESHOLDS]
     bandId = msgutils.freqRange(sys2detect, minFreq, maxFreq)
     return thresholds[bandId]["channelCount"]
예제 #10
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
예제 #11
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