def get_data(self,query):
        #
        # Return points or bins of data for query
        #

        response_data = ""

        self.logger.debug("QueryParser(): get_data(): Build COMMAND")

        self.logger.debug("QueryParser(): get_data(): Get data for uri:%s.%s" % (
            query['sta'],query['chan']))

        if not query['sta']:
            response_data = "Not valid station value"
            self.logger.error(response_data)
            return { "ERROR": response_data }

        if not query['chan']:
            response_data = "Not valid channel value "
            self.logger.error(response_data)
            return { "ERROR": response_data }

        start = isNumber(query['start'])
        end   = isNumber(query['end'])

        if not start:
            temp_dic = self.stations(query['sta'][0])
            if temp_dic:
                start = temp_dic[query['chan'][0]]['end'] - \
                            self.config.default_time_window

        #if not start: start = stock.now()

        if not end: end = start + self.config.default_time_window

        tempdb = self.dbcentral(start)
        if not tempdb:
            response_data = "Not valid database for this time [%s]" % start
            self.logger.error(response_data)
            return { "ERROR": response_data }

        regex = "-s 'sta=~/%s/ && chan=~/%s/' " % (query['sta'],query['chan'])

        if query['filter'] != 'None':
            filter = "-f '%s'" % query['filter']
        else:
            filter = ""

        if query['coverage']:
            coverage = "-b "
        else:
            coverage = ""

        if query['realtime']:
            realtime = "-r "
        else:
            realtime = ""

        if query['precision']:
            precision = "-q %s" % query['precision']
        else:
            precision = ""

        if query['median']:
            median = "-a "
        else:
            median = ""

        if query['calibrate']:
            calibrate = "-c "
        else:
            calibrate = ""

        if query['period']:
            period = "-t %s" % query['period']
        else:
            period = ""

        if query['page']:
            page = "-p %s" % query['page']
        else:
            page = ""

        run = "dbwfserver_extract %s %s %s %s %s %s %s %s %s -n %s -m %s %s %s %s 2>&1" % (
            regex, coverage, filter, page, calibrate, precision, realtime,
            median, period, self.config.max_traces, self.config.max_points,
            tempdb, start, end
        )

        self.logger.info("QueryParser(): get_data(): Extraction command: [%s]" % run)

        # Method 1
        #master, slave = pty.openpty()
        #pipe = Popen(run, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True, shell=True)
        #stdout = os.fdopen(master)
        #return stdout.readline()

        # Method 2
        return os.popen(run).read().replace('\n', '')
Exemple #2
0
    def get_data(self, query):
        #
        # Return points or bins of data for query
        #

        response_data = ""

        self.logger.debug("QueryParser(): get_data(): Build COMMAND")

        self.logger.debug("QueryParser(): get_data(): Get data for uri:%s.%s" %
                          (query['sta'], query['chan']))

        if not query['sta']:
            response_data = "Not valid station value"
            self.logger.error(response_data)
            return {"ERROR": response_data}

        if not query['chan']:
            response_data = "Not valid channel value "
            self.logger.error(response_data)
            return {"ERROR": response_data}

        start = isNumber(query['start'])
        end = isNumber(query['end'])

        if not start:
            temp_dic = self.stations(query['sta'][0])
            if temp_dic:
                start = temp_dic[query['chan'][0]]['end'] - \
                            self.config.default_time_window

        #if not start: start = stock.now()

        if not end: end = start + self.config.default_time_window

        tempdb = self.dbcentral(start)
        if not tempdb:
            response_data = "Not valid database for this time [%s]" % start
            self.logger.error(response_data)
            return {"ERROR": response_data}

        regex = "-s 'sta=~/%s/ && chan=~/%s/' " % (query['sta'], query['chan'])

        if query['filter'] != 'None':
            filter = "-f '%s'" % query['filter']
        else:
            filter = ""

        if query['coverage']:
            coverage = "-b "
        else:
            coverage = ""

        if query['realtime']:
            realtime = "-r "
        else:
            realtime = ""

        if query['precision']:
            precision = "-q %s" % query['precision']
        else:
            precision = ""

        if query['median']:
            median = "-a "
        else:
            median = ""

        if query['calibrate']:
            calibrate = "-c "
        else:
            calibrate = ""

        if query['period']:
            period = "-t %s" % query['period']
        else:
            period = ""

        if query['page']:
            page = "-p %s" % query['page']
        else:
            page = ""

        run = "dbwfserver_extract %s %s %s %s %s %s %s %s %s -n %s -m %s %s %s %s 2>&1" % (
            regex, coverage, filter, page, calibrate, precision, realtime,
            median, period, self.config.max_traces, self.config.max_points,
            tempdb, start, end)

        self.logger.info(
            "QueryParser(): get_data(): Extraction command: [%s]" % run)

        # Method 1
        #master, slave = pty.openpty()
        #pipe = Popen(run, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True, shell=True)
        #stdout = os.fdopen(master)
        #return stdout.readline()

        # Method 2
        return os.popen(run).read().replace('\n', '')
    def _parse_request(self,args):

        """
        Strict format for uri:
            localhost/wf/sta/chan/time/time/page

            Data-only calls:
            localhost/data/wf
            localhost/data/times
            localhost/data/events
            localhost/data/filters
            localhost/data/stations
            localhost/data/coverage
            localhost/data/channels
            localhost/data/wf/sta/chan/time/time/page
        """
        self.logger.debug("QueryParser(): _parse_request(): URI: %s" % str(args) )

        uri = {}
        time_window = float(self.config.default_time_window)

        uri.update( {
            "sta":[],
            "chan":[],
            "end":0,
            "data":False,
            "start":0,
            "page":1,
            "coverage":0,
            "time_window":False
        } )

        if 'data' in args:
            self.logger.info("QueryParser() _parse_request(): data query!")
            uri['data'] = True
            args.remove('data')

        # localhost/sta
        if len(args) > 1:
            uri['sta'] = args[1]

        # localhost/sta/chan
        if len(args) > 2:
            uri['chan'] = args[2]

        # localhost/sta/chan/time
        if len(args) > 3:
            uri['start'] = args[3]

        # localhost/sta/chan/time/time
        if len(args) > 4:
            uri['end'] = args[4]

        # localhost/sta/chan/time/time/page
        if len(args) > 5:
            uri['page'] = args[5]

        #
        # Fix start
        #
        if uri['start']:
            if isNumber(uri['start']):
                uri['start'] = isNumber(uri['start'])
            elif uri['start'] == 'hour':
                uri['start'] = 0
                time_window = 3600
            elif uri['start'] == 'day':
                uri['start'] = 0
                time_window = 86400
            elif uri['start'] == 'week':
                uri['start'] = 0
                time_window = 604800
            elif uri['start'] == 'month':
                uri['start'] = 0
                time_window = 2629743
            else:
                uri['start'] = 0

        #
        # Fix end
        #
        if uri['end']:
            if isNumber(uri['end']):
                uri['end'] = isNumber(uri['end'])
            elif uri['end'] == 'hour':
                uri['end'] = 0
                time_window = 3600
            elif uri['end'] == 'day':
                uri['end'] = 0
                time_window = 86400
            elif uri['end'] == 'week':
                uri['end'] = 0
                time_window = 604800
            elif uri['end'] == 'month':
                uri['end'] = 0
                time_window = 2629743
            else:
                uri['end'] = 0

        #
        # Build missing times if needed
        #
        if uri['sta'] and uri['chan']:
            if not uri['start'] :
                uri['end'] = self.stations.max_time()
                uri['start'] = uri['end'] - time_window

                uri['end']   = isNumber(uri['end'])
                uri['start'] = isNumber(uri['start'])

            if not uri['end']:
                uri['end'] = uri['start'] + time_window

        self.logger.debug("QueryParser(): _parse_request(): [sta:%s chan:%s start:%s end:%s]" % (uri['sta'], uri['chan'], uri['start'], uri['end']) )

        return uri
Exemple #4
0
    def _parse_request(self, args):
        """
        Strict format for uri:
            localhost/wf/sta/chan/time/time/page

            Data-only calls:
            localhost/data/wf
            localhost/data/times
            localhost/data/events
            localhost/data/filters
            localhost/data/stations
            localhost/data/coverage
            localhost/data/channels
            localhost/data/wf/sta/chan/time/time/page
        """
        self.logger.debug("QueryParser(): _parse_request(): URI: %s" %
                          str(args))

        uri = {}
        time_window = float(self.config.default_time_window)

        uri.update({
            "sta": [],
            "chan": [],
            "end": 0,
            "data": False,
            "start": 0,
            "page": 1,
            "coverage": 0,
            "time_window": False
        })

        if 'data' in args:
            self.logger.info("QueryParser() _parse_request(): data query!")
            uri['data'] = True
            args.remove('data')

        # localhost/sta
        if len(args) > 1:
            uri['sta'] = args[1]

        # localhost/sta/chan
        if len(args) > 2:
            uri['chan'] = args[2]

        # localhost/sta/chan/time
        if len(args) > 3:
            uri['start'] = args[3]

        # localhost/sta/chan/time/time
        if len(args) > 4:
            uri['end'] = args[4]

        # localhost/sta/chan/time/time/page
        if len(args) > 5:
            uri['page'] = args[5]

        #
        # Fix start
        #
        if uri['start']:
            if isNumber(uri['start']):
                uri['start'] = isNumber(uri['start'])
            elif uri['start'] == 'hour':
                uri['start'] = 0
                time_window = 3600
            elif uri['start'] == 'day':
                uri['start'] = 0
                time_window = 86400
            elif uri['start'] == 'week':
                uri['start'] = 0
                time_window = 604800
            elif uri['start'] == 'month':
                uri['start'] = 0
                time_window = 2629743
            else:
                uri['start'] = 0

        #
        # Fix end
        #
        if uri['end']:
            if isNumber(uri['end']):
                uri['end'] = isNumber(uri['end'])
            elif uri['end'] == 'hour':
                uri['end'] = 0
                time_window = 3600
            elif uri['end'] == 'day':
                uri['end'] = 0
                time_window = 86400
            elif uri['end'] == 'week':
                uri['end'] = 0
                time_window = 604800
            elif uri['end'] == 'month':
                uri['end'] = 0
                time_window = 2629743
            else:
                uri['end'] = 0

        #
        # Build missing times if needed
        #
        if uri['sta'] and uri['chan']:
            if not uri['start']:
                uri['end'] = self.stations.max_time()
                uri['start'] = uri['end'] - time_window

                uri['end'] = isNumber(uri['end'])
                uri['start'] = isNumber(uri['start'])

            if not uri['end']:
                uri['end'] = uri['start'] + time_window

        self.logger.debug(
            "QueryParser(): _parse_request(): [sta:%s chan:%s start:%s end:%s]"
            % (uri['sta'], uri['chan'], uri['start'], uri['end']))

        return uri