def render(self, request): # {{{ Output based on URI query args tvals = { "dbname": config.dbname, "application_title": config.application_title, "jquery_includes": self._jquery_includes(), "dir": '', "key": '', "my_list": '' } args = request.uri.split("/")[1:] template_queryparser = config.queryparser_html_template my_list = '<ul class="ui-helper-reset ui-helper-clearfix">' if args: metadatatype = args[0] tvals['dir'] = args[0] if metadatatype == 'stations': if len(args) > 1: tvals['key'] = args[1] args_list = args[1].split('+') mydata = eventdata.event_list(args_list) if not mydata: request.setResponseCode(404) return "Station code %s did not record any events (404 error)" % (args[1]) else: mydata = eventdata.available_stations() elif metadatatype == 'events': if len(args) > 1: mydata = eventdata.event_list(None,args[1]) metadatatype = 'wfs' tvals['key'] = strftime("%Y-%m-%d %H:%M:%S",gmtime(float(args[1]))) if not mydata: request.setResponseCode(404) return "Origin time %s does not have any stations recording arrivals (404 error)" % (args[1]) else: mydata = eventdata.event_list() else: request.setResponseCode(404) return "Data type %s not found (404 error)" % (args[1]) if isinstance(mydata,dict): for my_key,my_vals in mydata.iteritems(): my_vals.sort() for val in my_vals: if metadatatype == 'stations': val_readable = strftime("%Y-%m-%d %H:%M:%S",gmtime(float(val))) my_list += "<li class='ui-state-active ui-corner-all'><a href='/wfs/%s/%s'>%s</a></li>\n" % (my_key,val,val_readable) else: val_readable = val my_list += "<li class='ui-state-active ui-corner-all'><a href='/wfs/%s/%s'>%s</a></li>\n" % (val,my_key,val_readable) else: mydata.sort() for mys in mydata: if metadatatype == 'events': mys_readable = strftime("%Y-%m-%d %H:%M:%S",gmtime(float(mys))) else: mys_readable = mys my_list += "<li class='ui-state-active ui-corner-all'><a href='/%s/%s'>%s</a></li>\n" % (metadatatype,mys,mys_readable) tvals['my_list'] = my_list else: request.setResponseCode(404) return "Resource not found (404 error)" # }}} Output based on URI query args html_stations = Template(open(template_queryparser).read()).substitute(tvals) request.write( html_stations ) return ""
def render_GET(self, request): #{{{ type, net, sta, orid, chan, orid_time, time_window, time_start, time_end, availability, canvas_size, amount, filter, phases = self._extract_request(request) """ Handle different type of data request such as metadata (stations, events) and waveform data """ request.setHeader("content-type", "application/json") response_data = {} if config.verbose: log.msg("Type of query:%s " % type) if type == 'wf': """ TEST: http://localhost:8008/data?type=wf&sta=113A&orid=66554 http://localhost:8008/data?type=wf&sta=113A&orid=66554&ts=1230900154&te=1230900254&chan=BHZ http://localhost:8008/data?type=wf&sta=113A&ts=1230900154&te=1230900254 #DEBUG TOOL: #This line will output all vars as a json object: return json.dumps({"net": net, "sta": sta, "chan":chan, "orid":orid, "orid_time":orid_time, "time_window":time_window, "time_start":time_start, "time_end":time_end, "availability":availability, "canvas_size":canvas_size, "amount":amount, "filter":filter }) """ function = "Function: get_segment(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" % (str(sta), str(chan), canvas_size, orid, orid_time, time_window, time_start, time_end, amount, filter, phases) if config.debug: log.msg(function) # try: return json.dumps(eventdata.get_segment(sta, chan, canvas_size, orid, orid_time, time_window, time_start, time_end, amount, filter, phases)) # except: # request.setHeader("response-code", 500) # log.msg("\n") # log.msg("Problems on... " + function) # log.msg("\n") # return function elif type == 'coverage': """ You can test this with: http://localhost:8008/data?type=coverage - list coverage tuples of (time,end_time) for all stations and default channels or http://localhost:8008/data?type=coverage&sta=X18A&chan=BHZ - list coverage tuples of (time,end_time) for station X18A chan BHZ or http://localhost:8008/data?type=coverage&te=1230940700 - list coverage tuples of (time,end_time) until time_end or http://localhost:8008/data?type=coverage&chan=BHZ&ts=1230768001&te=1230940700 - list coverage tuples of (time,end_time) between start and end times for all BHZ chans or http://localhost:8008/data?type=coverage&sta=X18A&chan=BHZ&ts=1230768001&te=1230940700 Multiple stations/channels query... http://localhost:8008/data?type=events&sta=113A&sta=123A&chan=BHZ&chan=BHE&chan=BHN """ if config.verbose: log.msg("Query coverage. STA:%s CHAN:%s START:%s END:%s" % (str(sta),str(chan),str(time_start),str(time_end)) ) response_data = {'type':'coverage'} response_data.update({'format':'bars'}) response_data.update(eventdata.coverage(sta,chan,time_start,time_end)) return json.dumps(response_data) elif type == 'events': """ You can test this with: http://localhost:8008/data?type=events - list of events or http://localhost:8008/data?type=events&sta=127A - dict. of events recorded by station 127A or http://localhost:8008/data?type=events&orid=66484 - list of stations that recorded event 66484 or http://localhost:8008/data?type=events&sta=127A&orid=66484 - returns a floating point that is the arrival time UPDATE: Multiple stations query... http://localhost:8008/data?type=events&sta=113A&sta=123A """ if config.verbose: log.msg("Query events. STA:%s ORID:%s" % (str(sta),orid) ) return json.dumps(eventdata.event_list(sta,orid)) elif type == 'stations': """ You can test this with: http://localhost:8008/data?type=stations http://localhost:8008/data?type=stations&sta=Y12C """ return json.dumps(eventdata.available_stations(sta)) elif type == 'filters': """ You can test this with: http://localhost:8008/data?type=filters """ return json.dumps(config.filters, sort_keys=True) else: request.setHeader("response-code", 500) log.msg("ERROR in query. Unknown type:%s" % type) return "Unknown query type:(%s)" % type return 0