Exemplo n.º 1
0
def main():
    with open('GSMAlert.txt', 'w') as w:
        w.write('')
        
    window,config = rtw.getwindow()
    
    props = q.GetRainProps('rain_props')
    PublicAlert = pd.DataFrame({'timestamp': [window.end]*len(props), 'site': props['name'].values, 'source': ['public']*len(props), 'alert': [np.nan]*len(props), 'updateTS': [window.end]*len(props), 'palert_source': [np.nan]*len(props), 'internal_alert': [np.nan]*len(props), 'validity': [np.nan]*len(props), 'sensor_alert': [[]]*len(props), 'rain_alert': [np.nan]*len(props), 'ground_alert': [np.nan]*len(props), 'retriggerTS': [[]]*len(props), 'tech_info': [{}]*len(props)})
    PublicAlert = PublicAlert[['timestamp', 'site', 'source', 'alert', 'updateTS', 'palert_source', 'internal_alert', 'validity', 'sensor_alert', 'rain_alert', 'ground_alert', 'retriggerTS', 'tech_info']]

    Site_Public_Alert = PublicAlert.groupby('site')
    PublicAlert = Site_Public_Alert.apply(SitePublicAlert, window=window)
    PublicAlert = PublicAlert[['timestamp', 'site', 'alert', 'internal_alert', 'palert_source', 'validity', 'sensor_alert', 'rain_alert', 'ground_alert', 'retriggerTS', 'tech_info']]
    PublicAlert = PublicAlert.rename(columns = {'palert_source': 'source'})
    PublicAlert = PublicAlert.sort_values(['alert', 'site'], ascending = [False, True])
    
    PublicAlert.to_csv('PublicAlert.txt', header=True, index=None, sep='\t', mode='w')
    
    PublicAlert['timestamp'] = PublicAlert['timestamp'].apply(lambda x: str(x))
    PublicAlert['validity'] = PublicAlert['validity'].apply(lambda x: str(x))
    public_json = PublicAlert.to_json(orient="records")
    
    invdf = pd.read_csv('InvalidAlert.txt', sep = ':')
    invdf['timestamp'] = invdf['timestamp'].apply(lambda x: str(x))
    inv_json = invdf.to_json(orient="records")

    df_json = dict({'alerts': public_json, 'invalids': inv_json})
    
    df_json = '[' + str(df_json).replace("\\\'", '').replace('\'', '').replace('alerts:', '"alerts":').replace('invalids:', '"invalids":') + ']'

    with open('PublicAlert.json', 'w') as w:
        w.write(df_json)
                
    return PublicAlert
Exemplo n.º 2
0
def SiteCoord():
    RGdf = q.GetRainProps('rain_props')
    RGdf = RGdf.loc[RGdf.name != 'msl']

    RG = list(RGdf.rain_arq.dropna().apply(lambda x: x[:len(x) - 1]))
    RG = '|'.join(RG)
    query = "SELECT * FROM senslopedb.site_column where name REGEXP '%s'" % RG
    RGCoord = q.GetDBDataFrame(query)
    RGCoord['name'] = RGCoord.name.apply(lambda x: x + 'w')

    RG = list(RGdf.rain_senslope.dropna().apply(lambda x: x[:len(x) - 1]))
    RG = '|'.join(RG)
    query = "SELECT * FROM senslopedb.site_column where name REGEXP '%s'" % RG
    df = q.GetDBDataFrame(query)
    df['name'] = df.name.apply(lambda x: x[0:3] + 'w')
    RGCoord = RGCoord.append(df)

    RGCoord = RGCoord.drop_duplicates(
        ['sitio', 'barangay', 'municipality', 'province'])
    RGCoord = RGCoord[['name', 'lat', 'lon', 'barangay', 'province']]
    RGCoord = RGCoord.rename(columns={
        'name': 'dev_id',
        'barangay': 'location'
    })
    RGCoord['type'] = 'SenslopeRG'
    RGCoord = RGCoord.sort('dev_id')
    return RGCoord
Exemplo n.º 3
0
def main():
    RGdf = q.GetRainProps('rain_props')[[
        'r_id', 'name', 'max_rain_2year', 'rain_senslope', 'rain_arq'
    ]]
    siteRGdf = RGdf.groupby('name')
    RG = siteRGdf.apply(NearRGdf)
    return RG
Exemplo n.º 4
0
def main(site='', Print=True, end=datetime.now()):

    output_path = os.path.abspath(
        os.path.join(os.path.dirname(__file__), '../..'))

    s = cfg.config()

    #creates directory if it doesn't exist
    if s.io.PrintPlot or s.io.PrintSummaryAlert:
        if not os.path.exists(output_path + s.io.RainfallPlotsPath):
            os.makedirs(output_path + s.io.RainfallPlotsPath)

    #1. setting monitoring window
    end, start, offsetstart = get_rt_window(s.io.rt_window_length,
                                            s.io.roll_window_length,
                                            end=end)
    tsn = end.strftime("%Y-%m-%d_%H-%M-%S")

    #rainprops containing noah id and threshold
    rainprops = q.GetRainProps('rain_props')
    if site == '':
        pass
    else:
        rainprops = rainprops[rainprops.name == site]
    siterainprops = rainprops.groupby('name')

    summary = siterainprops.apply(RA.main, end=end, s=s)
    summary = summary.reset_index(drop=True).set_index('site')[[
        '1D cml', 'half of 2yr max', '3D cml', '2yr max', 'DataSource',
        'alert', 'advisory'
    ]]
    summary[['1D cml', 'half of 2yr max', '3D cml', '2yr max']] = np.round(
        summary[['1D cml', 'half of 2yr max', '3D cml', '2yr max']], 1)
    summary_json = summary.reset_index()
    summary_json['ts'] = str(end)
    summary_json = summary_json.to_json(orient="records")

    if Print == True:
        if s.io.PrintSummaryAlert:
            summary.to_csv(output_path + s.io.RainfallPlotsPath +
                           'SummaryOfRainfallAlertGenerationFor' + tsn +
                           s.io.CSVFormat,
                           sep=',',
                           mode='w')

            with open(
                    output_path + s.io.RainfallPlotsPath + 'summary' + tsn +
                    '.json', 'w') as w:
                w.write(summary_json)

        if s.io.PrintPlot:
            siterainprops.apply(RP.main,
                                offsetstart=offsetstart,
                                start=start,
                                end=end,
                                tsn=tsn,
                                s=s,
                                output_path=output_path)

    return summary_json
Exemplo n.º 5
0
def main():
    end = get_end_ts()

    rainprops = q.GetRainProps('rain_props')
    siterainprops = rainprops.groupby('name', as_index=False)

    popup_notif = siterainprops.apply(notif, end=end)
    popup_notif = popup_notif.dropna()

    if len(popup_notif) != 0:
        text = 'Check the following rainfall data:\n'
        text += popup_notif[['site', 'rain_gauge']].to_csv(index=False,
                                                           header=False,
                                                           sep=':')
        text = text.replace(':', ': ')
        popupmsg(text)
Exemplo n.º 6
0
def main():
    
    while True:
        site = raw_input('site name: ').lower()
        df = q.GetRainProps('rain_props')
        df['name'] = df['name'].apply(lambda x: x.lower())
        if site in df.name.values:
            break
        else:
            print 'site name is not in the list'
            print '#'*45
            print '##', ','.join(df.name.values[0:10]), '##'
            print '##', ','.join(df.name.values[10:20]), '##'
            print '##', ','.join(df.name.values[20:30]), '##'
            print '##', ','.join(df.name.values[30:40]), '##'
            print '##', ','.join(df.name.values[40:50]), '##'
            print '#'*45
            continue
    
    WriteToDB(site)
def main(end='', site=''):
    # data timestamp
    if end == '':
        while True:
            try:
                end = pd.to_datetime(
                    raw_input(
                        'timestamp format YYYY-MM-DD HH:MM (e.g. 2017-01-13 19:30): '
                    ))
                break
            except:
                print 'invalid timestamp format'
                pass
    rain_props = q.GetRainProps('rain_props')
    # rain gauge properties
    if site == '':
        while True:
            site = raw_input('site (e.g. agb): ').lower()
            site_rain_props = rain_props[rain_props.name == site]
            if len(site_rain_props) != 0:
                break
            print 'site not in the list'
    else:
        site_rain_props = rain_props[rain_props.name == site]
    # 3-day threshold
    twoyrmax = site_rain_props['max_rain_2year'].values[0]
    # 1-day threshold
    halfmax = twoyrmax / 2.
    gauge_ids = [site_rain_props['RG1'].values[0]] + [
        site_rain_props['RG2'].values[0]
    ] + [site_rain_props['RG3'].values[0]]
    try:
        if site_rain_props['rain_senslope'].values[0] != None:
            gauge_ids = [site_rain_props['rain_senslope'].values[0]
                         ] + gauge_ids
    except:
        pass
    try:
        if site_rain_props['rain_arq'].values[0] != None:
            gauge_ids = [site_rain_props['rain_arq'].values[0]] + gauge_ids
    except:
        pass
    # rain gauge id
    while True:
        gauge = raw_input('rain gauge id (e.g. agbtaw or 557): ').lower()
        try:
            gauge = 'rain_noah_' + str(int(gauge))
        except:
            pass
        if gauge in gauge_ids:
            break
        print '\n\n'
        print 'rain gauge id not in the list'
        rain_lst = '##  ' + ','.join(gauge_ids) + '  ##'
        print '#' * len(rain_lst)
        print rain_lst
        print '#' * len(rain_lst)
        print '\n\n'

    # rainfall data for the past 3 days
    start = end - timedelta(3)
    offsetstart = start - timedelta(hours=0.5)
    rainfall = A.GetResampledData(gauge, offsetstart, start, end)
    rainfall = rainfall[(rainfall.index >= start) & (rainfall.index <= end)]

    try:
        one, three = A.onethree_val_writer(rainfall)
        return end, site, one, halfmax, three, twoyrmax, gauge_ids, gauge
    except:
        one, three = '', ''
        return end, site, one, halfmax, three, twoyrmax, gauge_ids, gauge
Exemplo n.º 8
0
################################     MAIN     ################################

#1. setting monitoring window
roll_window_numpts, end, start, offsetstart, monwin = set_monitoring_window(
    roll_window_length, data_dt, rt_window_length, num_roll_window_ops)

index = pd.date_range(end - timedelta(rt_window_length),
                      periods=rt_window_length + 1,
                      freq='D')
columns = ['maxhalf', 'max']
base = pd.DataFrame(index=index, columns=columns)

tsn = end.strftime("%Y-%m-%d_%H-%M-%S")

#rainprops containing noah id and threshold
rainprops = q.GetRainProps()
rainprops['rain_arq'] = rainprops['rain_arq'].fillna(
    rainprops['rain_senslope'])
rainprops = rainprops[[
    'name', 'rain_arq', 'max_rain_2year', 'rain_noah', 'rain_noah2',
    'rain_noah3'
]]
rainprops['rain_arq'] = rainprops['rain_arq'].fillna(rainprops['name'])
rainprops['site'] = rainprops['rain_arq']
rainprops = rainprops[[
    'site', 'max_rain_2year', 'rain_noah', 'rain_noah2', 'rain_noah3'
]]
rainprops = rainprops.drop_duplicates(['site'], take_last=True)
rainprops = rainprops.reset_index(drop=True)

#empty dataframe