def curah_hujan_home(self, tanggal=None, paper=False): if not tanggal: tanggal = datetime.date.today() - datetime.timedelta(days=1) else: try: tanggal = to_date(tanggal) except: tanggal = to_date(tanggal) 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] data = [{ 'pos': a, 'ch': a.get_segmented_rain(tanggal) } for a in agents] sebelum = tanggal - datetime.timedelta(days=1) sesudah = tanggal + datetime.timedelta(days=1) template_render = render.curahhujan.harian_all if paper: template_render = render_plain.curahhujan.harian_paper return template_render(curah_hujan=data, meta={ 'now': tanggal, 'before': sebelum, 'after': sesudah }, wilayah=WILAYAH)
def GET(self): dari = 2015 hingga = datetime.date.today().year - 1 poses = [(a.AgentId, a.cname, a.wilayah) for a in AgentCh.select( OR(AgentCh.q.AgentType == 0, AgentCh.q.AgentType == 1)).orderBy( 'wilayah, cname')] return render.curahhujan.wilayah_rerata(poses)
def get_ch_daily_on_pos(pos, today=datetime.date.today()): pos = [a for a in AgentCh.select(AgentCh.q.AgentType==1) if a.table_name == pos][0] sql = "SELECT id, waktu, manual FROM curahhujan WHERE agent_id=%s AND YEAR(waktu)=%s AND MONTH(waktu)=%s ORDER BY waktu" % (pos.id, today.year, today.month) rs = conn.queryAll(sql) out = [] for r in rs: out.append(dict(id = r[0], waktu=r[1], ch=r[2])) return out
def POST(self, table_name): try: pos = [a for a in AgentCh.select(AgentCh.q.AgentType==1) if a.table_name == table_name][0] except IndexError: return web.notfound() inp = web.input() sql = "SELECT id FROM curahhujan WHERE agent_id=%s AND waktu='%s'" % (pos.id, to_date(inp.waktu)) rs = conn.queryAll(sql) if not rs: ch = CurahHujan(agent=pos, waktu=to_date(inp.waktu), manual=float(inp.hujan)) # publish to MQTT Broker #pub_object(ch) return web.redirect('/adm/ch/' + table_name, absolute=True)
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, table_name): pos = [ a for a in AgentCh.select(AgentCh.q.AgentType == 1) if a.table_name == table_name ][0] webinput = web.input(sampling=str(datetime.date.today() - datetime.timedelta(days=1))) tg = datetime.datetime.strptime(webinput.sampling, '%Y-%m-%d').date() return render.adm.ch.show({ 'pos': pos, 'tg': tg, 'data': get_ch_daily_on_pos(pos.table_name, tg) })
def GET(self, tahun=None, bulan=None): skr = datetime.date.today() data = [] try: tahun = int(tahun) except: tahun = skr.year try: bulan = int(bulan) except: bulan = skr.month (a, z) = calendar.monthrange(tahun, bulan) next_month = datetime.date(tahun, bulan, z) + datetime.timedelta(days=1) prev_month = datetime.date(tahun, bulan, 1) - datetime.timedelta(days=2) tdata = dict([(i, dict(telemetri=None, total=None, manual=None)) for i in range(0, z)]) for a in AgentCh.select( AND( OR(AgentCh.q.AgentType == KLIMATOLOGI, AgentCh.q.AgentType == 0), AgentCh.q.expose == True)).orderBy(["wilayah", "cname"]): sql = "SELECT DAY(waktu), pagi+sore+malam AS telemetri, total, manual \ FROM curahhujan WHERE agent_id=%s AND \ YEAR(waktu)=%s AND MONTH(waktu)=%s" % (a.AgentId, tahun, bulan) rst = AgentCh._connection.queryAll(sql) tdata.update( dict([(r[0], Struct(**dict(telemetri=r[1], total=r[2], manual=r[3]))) for r in rst])) data.append( Struct(**dict( AgentName=a.AgentName, wilayah=a.wilayah, data=tdata))) ctx = dict(curr_month=datetime.date(tahun, bulan, 1), data=data, wilayah=WILAYAH, numdays=z + 1, prev_month=prev_month, next_month=next_month) return render_fluid.curahhujan.human.index(ctx)
def generate_vs(self): poses = AgentCh.select( OR(AgentCh.q.AgentType == 0, AgentCh.q.AgentType == 1)) num_hari = 7 start = datetime.date.today() - datetime.timedelta(days=num_hari) dates = [] i = start while i < datetime.date.today(): dates.append(i) i += datetime.timedelta(days=1) ret = {} for p in poses: i = start pos_data = [] ret[p] = {'pos': p, 'data_hujan': pos_data} while i < datetime.date.today(): pos_data.append(p.get_segmented_rain(i)) i += datetime.timedelta(days=1) with open('templates/curahhujan/vs_generated.html', 'w') as f: f.write(str(render.curahhujan.vs({'poses': ret, 'dates': dates})))
def POST(self, table_name): agent_id = dict([a.split('\t') for a in open('agent_table.txt').readlines()]).get(table_name) pos = Agent.get(agent_id) try: pos = [a for a in AgentCh.select(AgentCh.q.AgentType==1) if a.table_name == table_name][0] except IndexError: return web.notfound() inp = web.input() float_list = 'ch_m,temp_min_m,temp_max_m,humi_m,kec_angin_m,penguapan_m'.split(',') for kol in float_list: if inp.get(kol): inp.update({kol: float(inp.get(kol))}) else: inp.update({kol: None}) inp.update({'penyinaran_m': inp.get('penyinaran_m') and int(inp.get('penyinaran_m')) or None}) inp.update({'agentID': pos.id, 'cuser': session.username, 'cdate': datetime.datetime.now()}) inp.update({'sampling': to_date(inp.get('sampling'))}) if 'csrf_token' in inp: del inp['csrf_token'] rst = KlimatManual.select(AND(KlimatManual.q.agent==pos, func.DATE(KlimatManual.q.sampling)==inp.get('sampling'))) if rst.count(): rst[0].set(**inp) else: km = KlimatManual(**inp) # table curahhujan perlu ditambah/update ch = CurahHujan.select(AND(CurahHujan.q.agent==pos, func.DATE(CurahHujan.q.waktu) == inp.get('sampling'))) if ch.count(): ch[0].set(**{'manual': float(inp.get('ch_m', 0))}) else: ch = CurahHujan(**{'waktu': inp.get('sampling'), 'manual': float(inp.get('ch_m', 0)), 'agent': pos}) return web.redirect('/adm/klimatologi/' + table_name, absolute=True)
def menitKeJamRain(tgl=datetime.date.today(), timpa=False): for a in AgentCh.select( OR(AgentCh.q.AgentType == 1, AgentCh.q.AgentType == 0)): salinKeJamjaman(a.table_name, tgl, timpa)
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)