Пример #1
0
def trending_alert_gen(pos_alert, tsm_id, end):
    
    if qdb.does_table_exist('node_alerts') == False:
        #Create a node_alerts table if it doesn't exist yet
        create_node_alerts()
            
    query = "SELECT EXISTS(SELECT * FROM node_alerts"
    query += " WHERE ts = '%s'" %end
    query += " and tsm_id = %s and node_id = %s)" %(tsm_id, pos_alert['node_id'].values[0])
    
    if db.df_read(query, connection='local').values[0][0] == 0:
        node_alert = pos_alert[['disp_alert', 'vel_alert']]
        node_alert['ts'] = end
        node_alert['tsm_id'] = tsm_id
        node_alert['node_id'] = pos_alert['node_id'].values[0]
        data_table = sms.DataTable('node_alerts', node_alert)
        db.df_write(data_table, connection='local')
    
    query = "SELECT * FROM node_alerts WHERE tsm_id = %s and node_id = %s and ts >= '%s'" %(tsm_id, pos_alert['node_id'].values[0], end-timedelta(hours=3))
    node_alert = db.df_read(query, connection='local')
    
    node_alert['node_alert'] = np.where(node_alert['vel_alert'].values >= node_alert['disp_alert'].values,

                             #node alert takes the higher perceive risk between vel alert and disp alert
                             node_alert['vel_alert'].values,                                

                             node_alert['disp_alert'].values)
    
    if len(node_alert[node_alert.node_alert > 0]) > 3:        
        trending_alert = pd.DataFrame({'node_id': [pos_alert['node_id'].values[0]], 'TNL': [max(node_alert['node_alert'].values)]})
    else:
        trending_alert = pd.DataFrame({'node_id': [pos_alert['node_id'].values[0]], 'TNL': [0]})
    
    return trending_alert
Пример #2
0
def main():
    """Writes in rainfall_gauges information on available rain gauges 
     for rainfall alert analysis

    """

    start = datetime.now()
    qdb.print_out(start)

    if qdb.does_table_exist('rainfall_gauges') == False:
        #Create a rainfall_gauges table if it doesn't exist yet
        qdb.create_rainfall_gauges()

    senslope = mem.get('df_dyna_rain_gauges')
    senslope = senslope.loc[senslope.has_rain == 1, :]
    senslope.loc[:, 'data_source'] = 'senslope'

    noah = noah_gauges()

    all_gauges = senslope.append(noah, sort=False)
    all_gauges.loc[:, 'gauge_name'] = all_gauges.loc[:, 'gauge_name'].apply(
        lambda x: str(x))
    all_gauges.loc[:, 'date_activated'] = pd.to_datetime(
        all_gauges.loc[:, 'date_activated'])
    written_gauges = mem.get('df_rain_gauges')
    not_written = set(all_gauges['gauge_name']) \
                     - set(written_gauges['gauge_name'])

    new_gauges = all_gauges.loc[all_gauges.gauge_name.isin(not_written), :]
    new_gauges = new_gauges.loc[new_gauges.date_deactivated.isnull(), :]
    new_gauges = new_gauges.loc[:, [
        'gauge_name', 'data_source', 'longitude', 'latitude', 'date_activated'
    ]]
    if len(new_gauges) != 0:
        data_table = sms.DataTable('rainfall_gauges', new_gauges)
        db.df_write(data_table)

    deactivated = written_gauges.loc[
        ~written_gauges.date_deactivated.isnull(), :]

    deactivated_gauges = all_gauges.loc[(~all_gauges.date_deactivated.isnull()) \
                                  & (~all_gauges.gauge_name.isin(not_written))\
                                  & (~all_gauges.gauge_name.isin(deactivated.gauge_name)), :]
    date_deactivated = pd.to_datetime(
        deactivated_gauges.loc[:, 'date_deactivated'])
    deactivated_gauges.loc[:, 'date_deactivated'] = date_deactivated
    deactivated_gauges = deactivated_gauges.loc[:, [
        'gauge_name', 'data_source', 'longitude', 'latitude', 'date_activated'
    ]]
    if len(deactivated_gauges) != 0:
        data_table = sms.DataTable('rainfall_gauges', deactivated_gauges)
        db.df_write(data_table)

    qdb.print_out('runtime = %s' % (datetime.now() - start))
Пример #3
0
def main(site_code=''):
    """Writes in rainfall_priorities information on nearest rain gauges
    from the project sites for rainfall alert analysis

    """

    start = datetime.now()
    qdb.print_out(start)
    
    coord = mem.get('df_sites')
    if site_code == '':
        try:
            site_code = sys.argv[1].lower()
            site_code = site_code.replace(' ', '').split(',')
        except:
            pass
    else:
        site_code = site_code.replace(' ', '').split(',')
    if site_code != '':
        coord = coord.loc[coord.site_code.isin(site_code), :]
    
    coord = coord.loc[coord.active == 1, ['site_id', 'latitude', 'longitude']]

    rg_coord = mem.get('df_rain_gauges')
    rg_coord = rg_coord[rg_coord.date_deactivated.isnull()]
    site_coord = coord.groupby('site_id', as_index=False)
    nearest_rg = site_coord.apply(get_distance, rg_coord=rg_coord)
    nearest_rg['distance'] = np.round(nearest_rg.distance,2)
    nearest_rg = nearest_rg.reset_index(drop=True)
    
    if qdb.does_table_exist('rainfall_priorities') == False:
        #Create a NOAH table if it doesn't exist yet
        qdb.create_rainfall_priorities()
        
    to_mysql(nearest_rg)
    
    qdb.print_out('runtime = %s' %(datetime.now() - start))
Пример #4
0
def update_single_table(noah_gauges):
    """Updates data of table gauge_name.
    
    Args:
        noah_gauges (dataframe): Rain gauge properties- id, name, data source.
    
    """

    noah_id = noah_gauges['gauge_name'].values[0]
    gauge_name = 'rain_noah_%s' % noah_id

    #check if table gauge_name exists
    if qdb.does_table_exist(gauge_name) == False:
        #Create a NOAH table if it doesn't exist yet
        qdb.print_out("Creating NOAH table '%s'" % gauge_name)
        qdb.create_NOAH_table(gauge_name)
    else:
        qdb.print_out('%s exists' % gauge_name)

    #Find the latest timestamp for noah_id (which is also the start date)
    latest_ts = qdb.get_latest_ts(gauge_name)

    if (latest_ts == '') or (latest_ts == None):
        #assign a starting date if table is currently empty
        latest_ts = datetime.now() - timedelta(3)
    else:
        latest_ts = latest_ts.strftime("%Y-%m-%d %H:%M:%S")

    qdb.print_out("    Start timestamp: %s" % latest_ts)

    #Generate end time
    end_ts = (pd.to_datetime(latest_ts) + timedelta(1)).strftime("%Y-%m-%d")
    qdb.print_out("    End timestamp: %s" % end_ts)

    #Download data for noah_id
    update_table_data(noah_id, gauge_name, latest_ts, end_ts, noah_gauges)
Пример #5
0
def summary_writer(site_id, site_code, gauge_name, rain_id, twoyrmax, halfmax,
                   rainfall, end, write_alert):
    """Summary of cumulative rainfall, threshold, alert and rain gauge used in
    analysis of rainfall.
    
    Args:
        site_id (int): ID per site.
        site_code (str): Three-letter code per site.
        gauge_name (str): Rain gauge used in rainfall analysis.
        rain_id (int): ID of gauge_name.
        twoyrmax (float): Threshold for 3-day cumulative rainfall per site.
        halfmax (float): Threshold for 1-day cumulative rainfall per site.
        rainfall (str): Data to compute cumulative rainfall from.
        end (datetime): End timestamp of alert to be computed.
        write_alert (bool): To write alert in database.

    Returns:
        dataframe: Summary of cumulative rainfall, threshold, alert and 
                   rain gauge used in analysis of rainfall.
    
    """

    one, three = one_three_val_writer(rainfall, end)

    #threshold is reached
    if one >= halfmax or three >= twoyrmax:
        ralert = 1
    #no data
    elif one == None or math.isnan(one):
        ralert = -1
    #rainfall below threshold
    else:
        ralert = 0

    if write_alert or ralert == 1:
        if qdb.does_table_exist('rainfall_alerts') == False:
            #Create a site_alerts table if it doesn't exist yet
            qdb.create_rainfall_alerts()

        columns = ['rain_alert', 'cumulative', 'threshold']
        alerts = pd.DataFrame(columns=columns)

        if ralert == 0:
            if one >= halfmax * 0.75:
                temp_df = pd.Series(['x', one, halfmax], index=columns)
            elif three >= twoyrmax * 0.75:
                temp_df = pd.Series(['x', three, twoyrmax], index=columns)
            else:
                temp_df = pd.Series([False, np.nan, np.nan], index=columns)

            alerts = alerts.append(temp_df, ignore_index=True, sort=False)
        else:
            if one >= halfmax:
                temp_df = pd.Series(['a', one, halfmax], index=columns)
                alerts = alerts.append(temp_df, ignore_index=True, sort=False)
            if three >= twoyrmax:
                temp_df = pd.Series(['b', three, twoyrmax], index=columns)
                alerts = alerts.append(temp_df, ignore_index=True, sort=False)
            if ralert == -1:
                temp_df = pd.Series([False, np.nan, np.nan], index=columns)
                alerts = alerts.append(temp_df, ignore_index=True, sort=False)

        if alerts['rain_alert'][0] != False:
            for index, row in alerts.iterrows():
                rain_alert = row['rain_alert']
                cumulative = row['cumulative']
                threshold = row['threshold']
                if qdb.does_alert_exists(site_id, end,
                                         rain_alert).values[0][0] == 0:
                    df = pd.DataFrame({
                        'ts': [end],
                        'site_id': [site_id],
                        'rain_id': [rain_id],
                        'rain_alert': [rain_alert],
                        'cumulative': [cumulative],
                        'threshold': [threshold]
                    })
                    data_table = sms.DataTable('rainfall_alerts', df)
                    db.df_write(data_table)

    summary = pd.DataFrame({
        'site_id': [site_id],
        'site_code': [site_code],
        '1D cml': [one],
        'half of 2yr max': [round(halfmax, 2)],
        '3D cml': [three],
        '2yr max': [round(twoyrmax, 2)],
        'DataSource': [gauge_name],
        'rain_id': [rain_id],
        'alert': [ralert]
    })

    return summary