ronnyhdez / blog

A site for my blog posts
https://blog.ronnyale.com/
Creative Commons Zero v1.0 Universal
0 stars 0 forks source link

Blog about checking a nested dictionary that comes from a GEE process #61

Open ronnyhdez opened 4 months ago

ronnyhdez commented 4 months ago

I was unfamiliar with the object that resulted from the leaf tool box sampler. Here are some of the steps I used to get a feeling of it's structure and then loop to create a dataframe:

site = ["projects/ee-ronnyale/assets/random_sample_10_filtered_polygons"]
# site = ["projects/ee-ronnyale/assets/random_sample_1000_filtered_polygons"]

import time

# Landsat 8
start_time = time.time()
sitesDictionaryL08SR = LEAF.sampleSites(
    site,
    imageCollectionName="LANDSAT/LC08/C02/T1_L2",
    algorithm=SL2PV0,
    variableName="Surface_Reflectance",
    maxCloudcover=90,
    outputScaleSize=30,
    inputScaleSize=30,
    bufferSpatialSize=0,
    bufferTemporalSize=["2021-04-01", "2022-10-01"],
    subsamplingFraction=0.99,
)
end_time = time.time()
execution_time = end_time - start_time
print(f"Execution time sitesDictionaryL08SR: {execution_time} seconds")

# Check dictionay characteristics
# print(type(sitesDictionaryL08SR))
# print(len(sitesDictionaryL08SR))

print(sitesDictionaryL08SR.keys())
print(sitesDictionaryL08SR['projects/ee-ronnyale/assets/random_sample_10_filtered_polygons'])

import json
print(json.dumps(sitesDictionaryL08SR, indent = 4))
print(sitesDictionaryL08SR.head())

import pprint
pp = pprint.PrettyPrinter(indent = 4)
pp.pprint(sitesDictionaryL08SR)

keys = sitesDictionaryL08SR.keys()
print(keys)

# I have nested dictionaries
outer_key = 'projects/ee-ronnyale/assets/random_sample_10_filtered_polygons'
first_item = sitesDictionaryL08SR[outer_key][0]
print(first_item)

first_item_keys = first_item.keys()
print(first_item_keys)

# Now, each element contains 
feature_item = sitesDictionaryL08SR[outer_key][0]['feature']
print(feature_item.keys())
feature_item.head()

# Now, each element contains 
feature_item = sitesDictionaryL08SR[outer_key][0]['leaftoolbox.SL2PV0']
print(feature_item.keys())

feature_item.head()

# Obtain well site id
site_id = sitesDictionaryL08SR[outer_key][2]['feature']['wllst__']
print(site_id)

items = sitesDictionaryL08SR.items()
print(items)

first_item = sitesDictionaryL08SR[outer_key]
print(first_item[4]['feature']['wllst__'])
print(range(len(first_item)))

results = []
for item in range(len(first_item)):
    df = first_item[item]['leaftoolbox.SL2PV0']
    df['site'] = first_item[item]['feature']['wllst__']
    results.append(df)
    print(first_item[item]['feature']['wllst__'])

# Combine all data frames
combined_df = pd.concat(results, ignore_index=True)
# combined_df.to_csv('test.csv', index = False)