<jupyter_start><jupyter_text>GLADs Intersecting AOIs<jupyter_code>#Import Global Metadata etc

%run '0.Importable_Globals.ipynb'
from datetime import date
from datetime import timedelta
import datetime<jupyter_output><empty_output><jupyter_text>Standard Rank ApproachAllows users to rank regions withing an iso, or adm1, by the amount of GLAD alerts that have occured in a given time frame.e.g. https://production-api.globalforestwatch.org/query/b6c75127-85c1-42fa-81a4-088d313797bc?sql=SELECT%20*%20FROM%20data%20%20WHERE%20polyname%20=%20%27wdpa%27%20LIMIT%2010See: https://basecamp.com/3063126/projects/10727890/todos/344440348and: https://basecamp.com/3063126/projects/10727890/todos/344440348comment_605072039polynames: - 'gadm28'- 'ifl_2013'- 'wdpa'- 'kba'- 'aze'- TCLs (tiger conservation landscapes) not sure...Rank by:- area of GLADs within region (ha)- area of GLADs as a persentage of total extent<jupyter_code>adm0 = 'BRA'
adm1 = None
polyname = 'gadm28'
threshold = 30     #hard code
extent_year = 2010 #hard coded since all glads data is post-2010
window = 12        #allow 4,12,26,52

sortBy = 'area_perc'

tags = ["forest_change", "Biodiversity"] #replaces current tree cover loss widget in summary

selectable_polynames = ['gadm28', 'kba', 'ifl_2013', 'wdpa', 'aze', 'tcl']
areaId_to_name = None
if adm1:
    tmp = get_admin2_json(iso=adm0, adm1=adm1)
    areaId_to_name ={}
    for row in tmp:
        areaId_to_name[row.get('adm2')] = row.get('name')
if iso and not adm1:
    tmp = get_admin1_json(iso=adm0)
    areaId_to_name={}
    for row in tmp:
        areaId_to_name[row.get('adm1')] = row.get('name') 
def glads_queries_by_poly(iso, adm1=None, polyname='gadm28'):
    #POLYNAME = gadm28, wdpa...
    # Return count for now (in case we change our mind). But do not include on prod
        url = f'https://production-api.globalforestwatch.org/query/b6c75127-85c1-42fa-81a4-088d313797bc?sql='
        if adm1:
            print(f'Requesting adm2 alerts')
            sql = f"""  SELECT iso, adm1, adm2, alert_count as count, alert_date as date, area_ha, polyname 
                        FROM data 
                        WHERE iso = '{iso}'
                        AND adm1 = {adm1}
                        AND polyname = '{polyname}'"""  
        elif iso:
            print(f'Requesting adm1 alerts')
            sql = f"""  SELECT iso, adm1, adm2, alert_count as count, alert_date as date, area_ha, polyname
                        FROM data 
                        WHERE iso = '{iso}'
                        AND polyname = '{polyname}'"""
        
        r = requests.get(url+sql)
        print(r.url)

        return r.json().get('data',None)#.get('attributes', None).get('value', None)

# Returns json object containing admin-codes, total area and extent (both in ha)
# If adm1 is not specified, it returns the total values for each adm1 region
# Else, returns the adm2 values within that adm1 region
# You may also specify a polyname (intersecting area) e.g. 'extent and % of plantations only'
# By default polyname is 'gadm28' (all forest extent)

def multiregion_extent_queries(adm0, adm1=None, year='area_extent_2000', p_name='gadm28', threshold=30):
    if adm0 and not adm1:
        print('Request for adm1 areas')
        sql = (f"SELECT adm1 as region, sum({year}) as extent, sum(area_gadm28) as total "
               f"FROM {ds} "
               f"WHERE iso = '{adm0}' "
                f"AND thresh = {threshold} "
               f"AND polyname = '{p_name}' "
               f"GROUP BY adm1 "
               f"ORDER BY adm1")
    elif adm0 and adm1:
        print('Request for adm2 areas')
        sql = (f"SELECT adm2 as region, {year} as extent, area_gadm28 as total "
                f"FROM {ds} "
                f"WHERE iso = '{adm0}' "
                f"AND thresh = {threshold} "
                f"AND polyname = '{p_name}' "
                f"AND adm1 = '{adm1}' ")
    return sql
def sumByAdm(data, adm1=None, window=4, sortBy='area_ha'):
    """Filters away dates outside of the past {window} weeks and then groups by adm"""
    if adm1:
        admin = 'adm2'
    else:
        admin = 'adm1'
        
    today = date.today()

    start = today.strftime('%Y/%m/%d')
    end = (today - timedelta(weeks=window-1)).strftime('%Y/%m/%d')
    
    tmp = []
    for d in data:
        if d.get(admin) not in tmp:
            tmp.append(d.get(admin))

    adm_list = []
    for adm in tmp:   
        adm_filter = list(filter(lambda x: (x.get(admin) == adm and x.get('date') >= end), data))
        total_area = total_count = 0
        ext = list(filter(lambda x: x.get('region') == adm, extent_data))[0].get('extent')
        for a in adm_filter:
            total_count += a.get('count')
            total_area += a.get('area_ha')
        
        adm_list.append({'adm': areaId_to_name[adm], 
                         'area_ha': total_area, 
                         'area_perc': 100 * total_area / ext
#                          'count': total_count,
#                          'count_per_ha': 100 * total_count / ext
                        })

    ranked_list = sorted(adm_list, key=lambda k: k[sortBy], reverse=True)
    
    return ranked_list
# Example sql and returned data


url = f"https://production-api.globalforestwatch.org/v1/query/{ds}"
sql = multiregion_extent_queries(adm0, adm1, extent_year_dict[extent_year], polyname, threshold)


properties = {"sql": sql}
r = requests.get(url, params = properties)
print(r.url)
print(f'Status: {r.status_code}')



extent_data = r.json()['data']
glad_data = glads_queries_by_poly(iso=adm0, adm1=adm1, polyname=polyname)

sorted(glad_data, key=lambda k: k['date'], reverse=True)[0:5]
#Append ranking number using a list comprehension
ranked_data = [{'adm': x.get('adm'),
                'area_ha': x.get('area_ha'),
                'area_perc': x.get('area_perc'),
#                 'count': x.get('count'),
#                 'count_per_ha': x.get('count_per_ha'),
                'rank': i+1} for i,x in enumerate(sumByAdm(data=glad_data, adm1=adm1, window=window, sortBy=sortBy))]
ranked_data[0:5]<jupyter_output><empty_output><jupyter_text>Dynamic Sentence<jupyter_code>phrase_dict = {
    'gadm28': 'in',
    'wdpa': 'in protected areas within',
    'aze': 'in Alliance for Zero Extinction sites within',
    'ifl_2013': 'in intact forests within',
    'tcl': 'in tiger conservation landscapes within',
    'kba': 'in key biodiversity areas within'
}
    
if window == 4:
    timeframe = 'month'
elif window == 12:
    timeframe = '3 months'
elif window == 26:
    timeframe = '6 months'
elif window == 52:
    timeframe = 'year'

print(f"In the last {timeframe} ",end="")
print(f"{ranked_data[0].get('area_ha')} ha of GLAD alerts were detected {phrase_dict[polyname]} ",end="")
print(f"{ranked_data[0].get('adm')}, ",end="")
print(f"equivalent to a {ranked_data[0].get('area_perc')}% loss ",end="")
print(f"relative to 2010 tree cover extent.",end="")<jupyter_output>In the last 3 months 74583.33857296083 ha of GLAD alerts were detected in Maranhão, equivalent to a 0.46339521267453937% loss relative to 2010 tree cover extent.<jupyter_text>Experimental - DO NOT IMPLEMENT!Could be interesting to plot counts vs time for the last X weeks for the top 5 admin regions experiencing the largest number of glad alerts.<jupyter_code>def glads_queries_by_poly(iso, adm1=None, polyname='gadm28'):
    #POLYNAME = gadm28, wdpa...
        url = f'https://production-api.globalforestwatch.org/query/b6c75127-85c1-42fa-81a4-088d313797bc?sql='
        if adm1:
            print(f'Requesting adm2 alerts')
            sql = f"""  SELECT iso, adm1, adm2, alert_count as count, alert_date as date, area_ha, polyname 
                        FROM data 
                        WHERE iso = '{iso}'
                        AND adm1 = {adm1}
                        AND polyname = '{polyname}'"""  
        elif iso:
            print(f'Requesting adm1 alerts')
            sql = f"""  SELECT iso, adm1, adm2, alert_count as count, alert_date as date, area_ha, polyname
                        FROM data 
                        WHERE iso = '{iso}'
                        AND polyname = '{polyname}'"""
        
        r = requests.get(url+sql)
        print(r.url)

        return r.json().get('data',None)#.get('attributes', None).get('value', None)

def cleanData(test_data):
    
    for d in test_data:
        year = int(d.get('date')[0:4])
        week = int(d.get('date')[5:7])
        day = int(d.get('date')[8:10])

        date = datetime.date(year,week,day)

        d['j_year'] = date.isocalendar()[0]
        d['j_week'] = date.isocalendar()[1]
    return test_data
adm0 = 'BRA'
adm1 = None
polyname = 'gadm28'

tags = ["forest_change", "Biodiversity"] #replaces current tree cover loss widget in summary

selectable_polynames = ['gadm28', 'kba', 'ifl_2013', 'wdpa', 'aze', 'tcl']
def createData(test_data, adm1=None):

    window = 58
    today = date.today()
    end = (today - timedelta(weeks=window)).strftime('%Y/%m/%d')

    if adm1:
        print('adm2')
        admin = 'adm2' 
    else:
        print('adm1')
        admin = 'adm1'

    tmp = []
    for d in test_data:
        if d.get(admin) not in tmp:
            tmp.append(d.get(admin))

    adm_list = []
    for adm in tmp:   
        adm_filter = list(filter(lambda x: x.get(admin) == adm, test_data))
        adm_filter = cleanData(list(filter(lambda x: x.get('date') >= end, adm_filter)))

        tmp = []
        for w in range(0,window):

            tmp_week = (today - timedelta(weeks=w))
            year = tmp_week.isocalendar()[0]
            week = tmp_week.isocalendar()[1]

            tmp_filter = list(filter(lambda x: (x.get('j_week') == week and x.get('j_year') == year), adm_filter))

            ta = tc = 0
            if (len(tmp_filter) != 0):
                for t in tmp_filter:
                    ta += t.get('area_ha')
                    tc += t.get('count')

            tmp.append({'area_ha': ta, 'count': tc, 'j_week': week, 'j_year': year})

        adm_filter = sorted(tmp, key=lambda k: (k['j_year'], k['j_week']), reverse=True)

        adm_list.append({'adm': adm, 'data': adm_filter})
        
    rank_data = []
    for d in adm_list:
        tc = 0
        ta = 0
        for el in d.get('data'):
            ta += el.get('area_ha')
            tc += el.get('count')

        rank_data.append({'adm': d.get('adm'),'data': d.get('data'), 'total_count': tc, 'area_ha': ta})

    return sorted(rank_data, key=lambda k: k['area_ha'], reverse=True)
def convolution(data):
    """Takes a list of means data and applies a runnung-mean over it with a window of 12 weeks
    to return a smoothed set of values. Incrementally run through the list of means and
    calculates the mean of 12 values at a time."""
    
    window = 6
    N = len(data)
    
    smoothed = []
    
    for i in range(0, int(N-window)):
        tmp = np.mean(data[i:i+window])
        smoothed.append(tmp)
        
    return smoothed
def getGladViz(test_data, metric='count'):
    """Displays the top 5 adm regions and plots their metric ('counts' or 'area_ha')
       against the number of weeks. Each of the adm regions counts are stacked on top of 
       eachother to show a cumulative metric (in shades of red). The total number 
       of counts for teh region is also displayed in grey as a dotted line.
       Finally, to avoid noise, the counts are smoothed with a window of 6 weeks."""
    
    x_val = []

    y1 = []
    y2 = []
    y3 = []
    y4 = []
    y5 = []
    yt = []

    for i in range(0, 58):
        tmp = 0
        for el in test_data:
            tmp += el.get('data')[i].get(metric)
        
        yt.append(tmp)
        y5.append(test_data[4].get('data')[i].get(metric))
        y4.append(test_data[3].get('data')[i].get(metric)+y5[i])
        y3.append(test_data[2].get('data')[i].get(metric)+y4[i])
        y2.append(test_data[1].get('data')[i].get(metric)+y3[i])
        y1.append(test_data[0].get('data')[i].get(metric)+y2[i])
        x_val.append(-i)


    y1 = convolution(y1)
    y2 = convolution(y2)
    y3 = convolution(y3)
    y4 = convolution(y4)
    y5 = convolution(y5)
    yt = convolution(yt)
    x_val = x_val[3:-3]

    plt.figure(figsize=(20,10))

    plt.plot(x_val, yt, 'r--', color='#aaaaaa', linewidth=2.5)
    plt.plot(x_val, y1, 'r-', color='#5c0909', linewidth=1.5)  # week in question=RED
    plt.plot(x_val, y2, 'r-', color='#a70000', linewidth=1.5)  # mean=BLACK
    plt.plot(x_val, y3, 'r-',color='#ff0000', linewidth=1.5)  # mean=BLACK
    plt.plot(x_val, y4, 'r-',color='#ff5252', linewidth=1.5)  # mean=BLACK
    plt.plot(x_val, y5, 'r-',color='#ff7b7b', linewidth=1.5)  # mean=BLACK
    
    plt.fill_between(x_val, y1, y2, color='#5c0909', alpha=0.35)
    plt.fill_between(x_val, y2, y3, color='#a70000', alpha=0.35)
    plt.fill_between(x_val, y3, y4, color='#ff0000', alpha=0.35)
    plt.fill_between(x_val, y4, y5, color='#ff5252', alpha=0.35)
    plt.fill_between(x_val, y5, 0, color='#ff7b7b', alpha=0.35)


    ax = plt.gca()

    ax.set_xlabel('weeks')
    ax.set_ylabel(f"Thousands of {metric}s")

    plt.show()    
adm0 = 'MYS'
adm1 = None
polyname = 'gadm28'

getGladViz(createData(glads_queries_by_poly(iso=adm0, adm1=adm1, polyname=polyname), adm1))

adm0 = 'IDN'
adm1 = None
polyname = 'gadm28'
getGladViz(createData(glads_queries_by_poly(iso=adm0, adm1=adm1, polyname=polyname), adm1))

adm0 = 'BRA'
adm1 = None
polyname = 'wdpa'
getGladViz(createData(glads_queries_by_poly(iso=adm0, adm1=adm1, polyname=polyname), adm1))

def getGladVizB(test_data, metric='count'):
    
    x_val = []

    y1 = []
    y2 = []
    y3 = []
    y4 = []
    y5 = []
    yt = []

    for i in range(0, 58):
        tmp = 0
        for el in test_data:
            tmp += el.get('data')[i].get(metric)
        
        yt.append(tmp)
        y5.append(test_data[4].get('data')[i].get(metric))
        y4.append(test_data[3].get('data')[i].get(metric)+y5[i])
        y3.append(test_data[2].get('data')[i].get(metric)+y4[i])
        y2.append(test_data[1].get('data')[i].get(metric)+y3[i])
        y1.append(test_data[0].get('data')[i].get(metric)+y2[i])
        x_val.append(-i)


    y1 = convolution(y1)
    y2 = convolution(y2)
    y3 = convolution(y3)
    y4 = convolution(y4)
    y5 = convolution(y5)
    yt = convolution(yt)
    x_val = x_val[3:-3]

    plt.figure(figsize=(20,10))

    plt.plot(x_val, yt, 'r--', color='#aaaaaa', linewidth=2.5)
    plt.bar(x_val, y1, color='#5c0909', bottom=0)  # week in question=RED
    plt.bar(x_val, y2, color='#a70000', bottom=0)  # mean=BLACK
    plt.bar(x_val, y3, color='#ff0000', bottom=0)  # mean=BLACK
    plt.bar(x_val, y4, color='#ff5252', bottom=0)  # mean=BLACK
    plt.bar(x_val, y5, color='#ff7b7b', bottom=0)  # mean=BLACK
    

    ax = plt.gca()

    ax.set_xlabel('weeks')
    ax.set_ylabel(f"Thousands of {metric}s")

    plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup