def compute_gridded_feat_df(fire_df, gfs_dict_dict, grid_len=4, bb=ak_bb):
    """ Compute a DataFrame for predictive modeling where we break up the space into a grid
    :param fire_df: DataFrame with MODIS fire data
    :param gfs_dict_dict: A dict mapping names of GFS features to dicts representing that feature
    :param cell_size: size, in km, of each grid cell
    :param bb: bounding box for space
    :return: the newly created DataFrame and a mapping from grid cell to lat,lon
    """
    if "x" not in fire_df:
        fire_df = append_xy(fire_df, bb)
    years = fire_df.year.unique()
    df_dict = dict()
    df_dict['dayofyear'] = []
    df_dict['year'] = []
    df_dict['n_det'] = []
    df_dict['grid_i'] = []
    df_dict['grid_j'] = []
    for name in gfs_dict_dict.keys():
        df_dict[name] = []

    year = min(years)
    annual_fires = fire_df[fire_df.year == year]
    dayofyear = np.min(annual_fires.dayofyear)
    max_day = np.max(annual_fires.dayofyear)
    month, day = day2monthday(dayofyear, leapyear=not (year % 4))
    while year <= max(years):
        df_dict['day'].append(day)
        df_dict['month'].append(month)
        df_dict['year'].append(year)
        df_dict['dayofyear'].append(dayofyear)
        today_fires = annual_fires[(annual_fires.day == day)
                                   & (fire_df.month == month)]
        df_dict['n_det'].append(len(today_fires))
        #        else:
        #            n_clusts = 0
        #        df_dict['n_clusters'].append(n_clusts)
        for name, gfs_dict in gfs_dict_dict.iteritems():
            try:
                mean_gfs = np.mean(
                    get_gfs_for_region(day, month, year,
                                       gfs_dict))  # default bb is ak_inland_bb
                df_dict[name].append(mean_gfs)
            except KeyError:
                df_dict[name].append(np.nan)
            except IndexError:
                df_dict[name].append(np.nan)

        dayofyear += 1
        if dayofyear >= max_day:
            year += 1
            annual_fires = fire_df[fire_df.year == year]
            dayofyear = np.min(annual_fires.dayofyear)
            max_day = np.max(annual_fires.dayofyear)
            month, day = day2monthday(dayofyear, leapyear=not (year % 4))
        else:
            month, day = day2monthday(dayofyear, (year % 4) == 0)

    return pd.DataFrame(df_dict)
Exemple #2
0
def make_gif(df, gfs_dict, name="Temp"):
    ak_bb = [55, 71, -165, -138]
    lats = gfs_dict['lats']
    lons = gfs_dict['lons']
    plot_bb_0 = np.where(lats[:, 0] <= ak_bb[0])[0][0]
    plot_bb_1 = np.where(lats[:, 0] <= ak_bb[1])[0][0]
    plot_bb_2 = np.where(lons[0, :] >= (ak_bb[2] % 360))[0][0]
    plot_bb_3 = np.where(lons[0, :] >= (ak_bb[3] % 360))[0][0]

    mp = Basemap(projection="merc",
                 lat_0=55,
                 lon_0=-165,
                 llcrnrlat=55,
                 llcrnrlon=-165,
                 urcrnrlat=71,
                 urcrnrlon=-138,
                 resolution='i')
    start_day = monthday2day(6, 1)
    end_day = monthday2day(9, 1)
    min_temp = gfs_dict['min']
    max_temp = gfs_dict['max']
    prev_lats = []
    prev_lons = []
    for dayy in xrange(start_day, end_day):
        if len(prev_lats):
            mp.plot(np.array(prev_lons), np.array(prev_lats), 'ko')
        monthday = day2monthday(dayy)
        today_fires = df[(df.year == 2013) & (df.month == monthday[0]) &
                         (df.day == monthday[1])]
        if len(today_fires):
            mp_lons, mp_lats = mp(np.array(today_fires.lon),
                                  np.array(today_fires.lat))
            mp.plot(mp_lons, mp_lats, 'ro')
            prev_lats += list(mp_lats)
            prev_lons += list(mp_lons)
        temp_vals = gfs_dict[monthday]
        mp.imshow(temp_vals[plot_bb_0 - 1:plot_bb_1 - 1:-1,
                            plot_bb_2:plot_bb_3],
                  vmin=min_temp,
                  vmax=max_temp)
        plt.title("%s for %d/%d" % (name, monthday[0], monthday[1]))
        mp.drawcoastlines()
        mp.colorbar()
        plt.savefig('gifmaking/day%d.png' % dayy)
        plt.close()

    os.system(
        'convert -delay 100 -loop 0 gifmaking/day*.png gifmaking/%s_loop_2013.gif'
        % name)
def compute_global_feat_df(fire_df, gfs_dict_dict, clust_thresh=10):
    """ Get a DataFrame to make active fire prediction easy
    :param year: Year we want to look at
    :param fire_df: DataFrame of active fires. Should contain fields day, month, x, and y
    :param clusts: Cluster assignments for each detection
    :param gfs_dict_dict: Dict of dicts, each inner dict representing a GFS (weather) layer
    :return: a DataFrame for prediction, with fields fire id, day, day_cent, n_det, n_det_cum, hull_size, hull_size_cum,
                gfs...  where we have as many gfs fields as the len zof gfs_dict_dict
    """
    years = fire_df.year.unique()
    df_dict = dict()
    df_dict['dayofyear'] = []
    df_dict['day'] = []
    df_dict['month'] = []
    df_dict['year'] = []
    df_dict['n_det'] = []
    df_dict['n_clusters'] = []
    for name in gfs_dict_dict.keys():
        df_dict[name] = []

    year = min(years)
    annual_fires = fire_df[fire_df.year == year]
    dayofyear = np.min(annual_fires.dayofyear)
    max_day = np.max(annual_fires.dayofyear)
    month, day = day2monthday(dayofyear, leapyear=not (year % 4))
    while year <= max(years):
        df_dict['day'].append(day)
        df_dict['month'].append(month)
        df_dict['year'].append(year)
        df_dict['dayofyear'].append(dayofyear)
        today_fires = annual_fires[(annual_fires.day == day)
                                   & (fire_df.month == month)]
        df_dict['n_det'].append(len(today_fires))
        if len(today_fires):
            n_clusts, _ = cluster_fires(today_fires,
                                        clust_thresh,
                                        return_df=False)
        else:
            n_clusts = 0
        df_dict['n_clusters'].append(n_clusts)
        for name, gfs_dict in gfs_dict_dict.iteritems():
            try:
                mean_gfs = np.mean(
                    get_gfs_for_region(day, month, year,
                                       gfs_dict))  # default bb is ak_inland_bb
                df_dict[name].append(mean_gfs)
            except KeyError:
                df_dict[name].append(np.nan)
            except IndexError:
                df_dict[name].append(np.nan)

        dayofyear += 1
        if dayofyear >= max_day:
            year += 1
            annual_fires = fire_df[fire_df.year == year]
            dayofyear = np.min(annual_fires.dayofyear)
            max_day = np.max(annual_fires.dayofyear)
            month, day = day2monthday(dayofyear, leapyear=not (year % 4))
        else:
            month, day = day2monthday(dayofyear, (year % 4) == 0)

    return pd.DataFrame(df_dict)
def compute_feat_df(year, fire_df, clusts, gfs_dict_dict):
    """ Get a DataFrame to make active fire prediction easy
    :param year: Year we want to look at
    :param fire_df: DataFrame of active fires. Should contain fields day, month, x, and y
    :param clusts: Cluster assignments for each detection
    :param gfs_dict_dict: Dict of dicts, each inner dict representing a GFS (weather) layer
    :return: a DataFrame for prediction, with fields fire id, day, day_cent, n_det, n_det_cum, hull_size, hull_size_cum,
                gfs...  where we have as many gfs fields as the len zof gfs_dict_dict
    """
    detections = fire_df[fire_df.year == year]
    N = len(detections)
    clust_vals = np.unique(clusts)

    df_dict = dict()
    df_dict['fire_id'] = []
    df_dict['day'] = []
    df_dict['day_cent'] = []
    df_dict['n_det'] = []
    df_dict['n_det_cum'] = []
    #df_dict['hull_size'] = []
    #df_dict['hull_size_cum'] = []
    df_dict['lat'] = []
    df_dict['lon'] = []
    df_dict['x'] = []
    df_dict['y'] = []
    for name in gfs_dict_dict.keys():
        df_dict[name] = []

    for clust in clust_vals:
        clust_dets = detections[clusts == clust]
        days = clust_dets.dayofyear
        min_day = np.min(days)
        max_day = np.max(days)
        center_lat = np.mean(clust_dets.lat)
        center_lon = np.mean(clust_dets.long)
        center_x = np.mean(clust_dets.x)
        center_y = np.mean(clust_dets.y)
        for day in xrange(min_day, max_day + 1):
            # We'll have exactly one entry in our DataFrame for this cluster on this day
            df_dict['lat'].append(center_lat)
            df_dict['lon'].append(center_lon)
            df_dict['x'].append(center_x)
            df_dict['y'].append(center_y)
            day_dets = clust_dets[(clust_dets.dayofyear == day)]
            cum_dets = clust_dets[(clust_dets.dayofyear <= day)]
            df_dict['fire_id'].append(clust)
            df_dict['day'].append(day)
            df_dict['day_cent'].append(day - min_day)
            df_dict['n_det'].append(len(day_dets))
            df_dict['n_det_cum'].append(len(cum_dets))
            #if len(day_dets) > 2:
            #    xys = np.column_stack((day_dets.x, day_dets.y))
            #    df_dict['hull_size'].append(ConvexHull(xys).volume)
            #else:
            #    df_dict['hull_size'].append(0.)
            #if len(cum_dets) > 2:
            #    xys_cum = np.column_stack((cum_dets.x, cum_dets.y))
            #    df_dict['hull_size_cum'].append(ConvexHull(xys_cum).volume)
            #else:
            #    df_dict['hull_size_cum'].append(0.)

            month, dayofmonth = day2monthday(day, leapyear=(year % 4))
            for name, gfs_dict in gfs_dict_dict.iteritems():
                try:
                    gfs_val = get_gfs_val(center_lat, center_lon, dayofmonth,
                                          month, gfs_dict, year)
                    df_dict[name].append(gfs_val)
                except KeyError:
                    df_dict[name].append(np.nan)
                except IndexError:
                    df_dict[name].append(np.nan)

    return pd.DataFrame(df_dict)