def GET(self, pos_id): try: pos = AgentCh.get(int(pos_id)) except SQLObjectNotFound: return web.notfound() dari = datetime.date.today().year hingga = datetime.date.today().year - 1 sql = "SELECT waktu, MAX(manual) \ FROM curahhujan \ WHERE agent_id=%s \ GROUP BY YEAR(waktu), \ MONTH(waktu) \ ORDER BY waktu ASC" % (pos_id, ) data = {} tahun = 0 for d in AgentCh._connection.queryAll(sql): if d[0]: if tahun != d[0].year: _year_data = [] data[d[0].year] = _year_data tahun = d[0].year _year_data.append((d[0].month, d[1] or 0)) data = [(k, v) for k, v in data.items()] data.sort() return render.curahhujan.tertinggi({'pos': pos, 'data': data})
def salinKeJamjaman(tablename, tgl=datetime.date.today(), timpa=False): '''Membaca data Rain dari <table>(5 menitan) menjadi satu jam, simpan ke table 'periodikjam' ''' aid = dict([a.split('\t') for a in open('agent_table.txt').readlines()])[tablename] agent = AgentCh.get(aid) sql = "SELECT SamplingDate, HOUR(SamplingTime), \ SUM(Rain * %(tipping_factor)s) FROM %(tablename)s \ WHERE SamplingDate='%(tgl)s' \ GROUP BY HOUR(SamplingTime) \ ORDER BY SamplingTime" for c in conn.queryAll(sql % dict( tipping_factor=agent.TippingFactor, tablename=tablename, tgl=tgl)): sampling = datetime.datetime.fromtimestamp( int(c[0].strftime('%s')) + int(c[1]) * 3600) row = dict(agent=agent, sampling=sampling, rain=c[2]) pj = PeriodikJam.select( AND(PeriodikJam.q.sampling == sampling, PeriodikJam.q.agent == agent)) if not pj.count(): PeriodikJam(**row) if timpa: pj[0].rain = c[2]
def GET(self, pid): try: pos = AgentCh.get(pid) except SQLObjectNotFound: return web.notfound() HIDE_THIS = [a.strip() for a in open('HIDE_ARR.txt').read().split(',')] agents = AgentCh.select( AND( OR(AgentCh.q.AgentType == KLIMATOLOGI, AgentCh.q.AgentType == 0.0), AgentCh.q.expose == True)).orderBy(( 'wilayah', 'urutan', )) agents = [a for a in agents if a.table_name not in HIDE_THIS] now = datetime.datetime.now() try: tanggal = to_date(web.input().get('d')) except: tanggal = datetime.date.today() if now.hour < 9: tanggal = tanggal - datetime.timedelta(days=1) ch_per_jam = pos.get_aday_ch(tanggal) sebelum = tanggal - datetime.timedelta(days=1) if tanggal == datetime.date.today(): sesudah = None else: sesudah = tanggal + datetime.timedelta(days=1) start = datetime.datetime.combine(tanggal, now.time().replace(hour=7, minute=0)) labels = [(start + datetime.timedelta(hours=h)).hour for h in range(24)] ch_set = [] ch_ = dict([(c[0].hour, c[1]) for c in ch_per_jam]) for l in labels: ch_set.append((l, float(ch_.get(l, 0) or '0'))) return render.curahhujan.jamjaman({ 'pos': pos, 'tanggal': tanggal, 'poses': agents, 'wilayah': WILAYAH, 'sebelum': sebelum, 'sesudah': sesudah, 'data': { 'categories': labels, 'series': ch_set } })
def GET(self, pos_id): try: pos = AgentCh.get(int(pos_id)) except SQLObjectNotFound: return web.notfound() web_input = web.input() periode = web_input.get('p', 10) year = int(web_input.get('year', datetime.date.today().year)) sql = "SELECT waktu, manual FROM curahhujan \ WHERE agent_id=%s AND YEAR(waktu)=%s ORDER BY waktu" % (pos_id, year) setahun = [] # 10 harian bulan = 0 bln_ = [0, 0, 0] for r in conn.queryAll(sql): if r[0].month != bulan: if bulan: setahun.append((bulan, bln_)) bln_ = [0, 0, 0] bulan = r[0].month if r[0].day < 10: bln_[0] += r[1] elif r[0].day < 21: bln_[1] += r[1] else: bln_[2] += r[1] setahun.append((bulan, bln_)) setahun = dict(setahun) series = [setahun.get(b, [0, 0, 0]) for b in range(1, 13)] s = [] d = [] t = [] for data in series: s.append(data[0]) d.append(data[1]) t.append(data[2]) data = { 'periode': periode, 'year': year, 'pos': pos, 'series': (s, d, t) } template = (periode == 10) and render.curahhujan.daily10 \ or render.curahhujan.daily15 return template(data)
def GET(self, pid): try: pos = AgentCh.get(pid) except SQLObjectNotFound: return web.notfound() now = datetime.datetime.now() try: tanggal = to_date(web.input().get('d')) except: tanggal = datetime.date.today() if now.hour < 9: tanggal = tanggal - datetime.timedelta(days=1) ch_per_jam = pos.get_aday_ch(tanggal) sebelum = tanggal - datetime.timedelta(days=1) if tanggal == datetime.date.today(): sesudah = None else: sesudah = tanggal + datetime.timedelta(days=1) start = datetime.datetime.combine(tanggal, now.time().replace(hour=7, minute=0)) labels = [(start + datetime.timedelta(hours=h)).hour for h in range(24)] ch_set = [] ch_ = dict([(c[0].hour, c[1]) for c in ch_per_jam]) for l in labels: ch_set.append((l, float(ch_.get(l, 0) or '0'))) return render.curahhujan.jamjaman({ 'pos': pos, 'tanggal': tanggal, 'sebelum': sebelum, 'sesudah': sesudah, 'data': { 'categories': labels, 'series': ch_set } })
def curah_hujan_pos(self, pos_id, tahun=datetime.date.today().year, bulan=''): ''' Jika bulan valid, otomatis tahun juga valid, tampilkan curah hujan setiap hari pada bulan terpilih sumbu mendatar berisi tanggal Jika tahun valid, bulan kosong, tampilkan curah hujan 3 tahun lalu, dari tahun terpilih, sumbu mendatar berisi bulan-bulan ''' try: agent = AgentCh.get(pos_id) except SQLObjectNotFound: return web.notfound() if agent.AgentType not in (1.0, 0.0): return web.notfound() ch = agent.get_ch(tahun, bulan) data = [] for a in ch: try: data.append((a[0], a[2] or 0)) except: data.append((a[0], 0)) series = {} to_render = render.curahhujan.bulanan if bulan: # hujan per hari pada 'bulan' series = [ 0 for r in range(calendar.monthrange(tahun, int(bulan))[1]) ] sql = "SELECT waktu, manual, pagi, sore, malam, tmalam FROM curahhujan \ WHERE agent_id=%s AND YEAR(waktu)=%s AND MONTH(waktu)=%s" % ( pos_id, tahun, bulan) for d in conn.queryAll(sql): series[d[0].day - 1] = d[1] data = Struct( **{ 'series': series, 'categories': [s + 1 for s in range(len(series))], 'bulan': datetime.date(tahun, int(bulan), 1) }) to_render = render.curahhujan.harian elif data: print data # hujan per bulan pada 'tahun' th = data[0][0].year series[th] = [0 for r in range(0, 12)] for d in data: if d[0].year != th: th = d[0].year series[th] = [0 for r in range(0, 12)] series[th][d[0].month - 1] = d[1] else: series[th][d[0].month - 1] = d[1] data = [ Struct(**{ 'tahun': k, 'series': v }) for k, v in sorted(series.items()) ] ctx = {'pos': agent, 'data': data} return to_render(ctx)
def curah_hujan_pos(self, pos_id, tahun=datetime.date.today().year, bulan=''): ''' Jika bulan valid, otomatis tahun juga valid, tampilkan curah hujan setiap hari pada bulan terpilih sumbu mendatar berisi tanggal Jika tahun valid, bulan kosong, tampilkan curah hujan 3 tahun lalu, dari tahun terpilih, sumbu mendatar berisi bulan-bulan ''' try: agent = AgentCh.get(pos_id) except SQLObjectNotFound: return web.notfound() if agent.AgentType not in (1.0, 0.0): return web.notfound() HIDE_THIS = [a.strip() for a in open('HIDE_ARR.txt').read().split(',')] agents = AgentCh.select( AND( OR(AgentCh.q.AgentType == KLIMATOLOGI, AgentCh.q.AgentType == 0.0), AgentCh.q.expose == True)).orderBy(( 'wilayah', 'urutan', )) agents = [a for a in agents if a.table_name not in HIDE_THIS] ch = agent.get_ch(tahun, bulan) data = [] for a in ch: try: data.append((a[0], a[2] or 0)) except: data.append((a[0], 0)) series = {} to_render = render.curahhujan.bulanan if bulan: # hujan per hari pada 'bulan' series = [ 0 for r in range(calendar.monthrange(tahun, int(bulan))[1]) ] sql = "SELECT waktu, manual, pagi, sore, malam, tmalam FROM curahhujan \ WHERE agent_id=%s AND YEAR(waktu)=%s AND MONTH(waktu)=%s" % ( pos_id, tahun, bulan) for d in conn.queryAll(sql): series[d[0].day - 1] = d[1] data = Struct( **{ 'series': series, 'categories': [s + 1 for s in range(len(series))], 'bulan': datetime.date(tahun, int(bulan), 1) }) to_render = render.curahhujan.harian elif data: # hujan per bulan pada 'tahun' th = data[0][0].year series[th] = [0 for r in range(0, 12)] for d in data: if d[0].year != th: th = d[0].year series[th] = [0 for r in range(0, 12)] series[th][d[0].month - 1] = d[1] else: series[th][d[0].month - 1] = d[1] data = [ Struct(**{ 'tahun': k, 'series': v }) for k, v in sorted(series.items()) ] ctx = {'pos': agent, 'data': data, 'poses': agents, 'wilayah': WILAYAH} return to_render(ctx)