Skip to content

Commit

Permalink
Reformatting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
lunasilvestre committed Oct 6, 2023
1 parent 6378467 commit e8beaff
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 75 deletions.
182 changes: 113 additions & 69 deletions validation/example_1_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,60 @@
import io
from googleapiclient.http import MediaIoBaseDownload

service_account_file = '.gee-sa-priv-key.json'
service_account_file = ".gee-sa-priv-key.json"
folder_name = "python_lst_geotiffs"
local_download_path = "./downloads"

SCOPES = [
'https://www.googleapis.com/auth/earthengine',
'https://www.googleapis.com/auth/devstorage.full_control',
'https://www.googleapis.com/auth/drive'
"https://www.googleapis.com/auth/earthengine",
"https://www.googleapis.com/auth/devstorage.full_control",
"https://www.googleapis.com/auth/drive",
]


def initialize_services(service_account_file):
"""Initialize Earth Engine and Google Drive services."""
credentials = service_account.Credentials.from_service_account_file(
service_account_file, scopes=SCOPES)
service_account_file, scopes=SCOPES
)
ee.Initialize(credentials)
return build('drive', 'v3', credentials=credentials)
return build("drive", "v3", credentials=credentials)


def clean_folder(service, folder_name):
"""Delete all files in the specified Google Drive folder."""

# Search for the folder by its name
folder_results = service.files().list(
q=f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder'",
fields="files(id)"
).execute()
folder_results = (
service.files()
.list(
q=f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder'",
fields="files(id)",
)
.execute()
)

folders = folder_results.get('files', [])
folders = folder_results.get("files", [])

if not folders:
print(f"Folder {folder_name} not found.")
return

folder_id = folders[0]['id']
folder_id = folders[0]["id"]

# List all files in the folder
file_results = service.files().list(
q=f"'{folder_id}' in parents",
fields="files(id, name)"
).execute()
file_results = (
service.files()
.list(q=f"'{folder_id}' in parents", fields="files(id, name)")
.execute()
)

files = file_results.get('files', [])
files = file_results.get("files", [])

# Delete each file
for file in files:
try:
service.files().delete(fileId=file['id']).execute()
service.files().delete(fileId=file["id"]).execute()
print(f"Deleted {file['name']} from {folder_name}")
except Exception as e:
print(f"Error deleting {file['name']} from {folder_name}: {e}")
Expand All @@ -63,33 +69,38 @@ def download_files(service, folder_name, download_path):
"""Download all files from the specified Google Drive folder to a local path."""

# Search for the folder by its name
folder_results = service.files().list(
q=f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder'",
fields="files(id)"
).execute()
folder_results = (
service.files()
.list(
q=f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder'",
fields="files(id)",
)
.execute()
)

folders = folder_results.get('files', [])
folders = folder_results.get("files", [])

if not folders:
print(f"Folder {folder_name} not found.")
return

folder_id = folders[0]['id']
folder_id = folders[0]["id"]

# List all files in the folder
file_results = service.files().list(
q=f"'{folder_id}' in parents",
fields="files(id, name, mimeType)"
).execute()
file_results = (
service.files()
.list(q=f"'{folder_id}' in parents", fields="files(id, name, mimeType)")
.execute()
)

files = file_results.get('files', [])
files = file_results.get("files", [])

# Download each file
for file in files:
request = service.files().get_media(fileId=file['id'])
file_name = file['name']
request = service.files().get_media(fileId=file["id"])
file_name = file["name"]

fh = io.FileIO(f"{download_path}/{file_name}", 'wb')
fh = io.FileIO(f"{download_path}/{file_name}", "wb")
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
Expand All @@ -116,67 +127,100 @@ def process_data(drive_service, folder_name):
use_ndvi = True

landsat_coll = fetch_landsat_collection(
satellite, date_start, date_end, geometry, use_ndvi)
satellite, date_start, date_end, geometry, use_ndvi
)

ex_image = landsat_coll.first()

cmap1 = ["blue", "cyan", "green", "yellow", "red"]
cmap2 = ["F2F2F2", "EFC2B3", "ECB176", "E9BD3A", "E6E600", "63C600", "00A600"]

visualizations = [
{'bands': ['TPW'], 'min': 0.0, 'max': 60.0, 'palette': cmap1,
'description': 'TCWV'},
{'bands': ['TPWpos'], 'min': 0.0, 'max': 9.0, 'palette': cmap1,
'description': 'TCWVpos'},
{'bands': ['FVC'], 'min': 0.0, 'max': 1.0, 'palette': cmap2,
'description': 'FVC'},
{'bands': ['EM'], 'min': 0.9, 'max': 1.0, 'palette': cmap1,
'description': 'Emissivity'},
{'bands': ['B10'], 'min': 290, 'max': 320, 'palette': cmap1,
'description': 'TIR_BT'},
{'bands': ['LST'], 'min': 290, 'max': 320, 'palette': cmap1,
'description': 'LST'},
{'bands': ['LST'], 'description': 'LST_Celsius_Raw'},
{'bands': ['SR_B4', 'SR_B3', 'SR_B2'], 'min': 0, 'max': 0.3,
'description': 'RGB'},
{
"bands": ["TPW"],
"min": 0.0,
"max": 60.0,
"palette": cmap1,
"description": "TCWV",
},
{
"bands": ["TPWpos"],
"min": 0.0,
"max": 9.0,
"palette": cmap1,
"description": "TCWVpos",
},
{
"bands": ["FVC"],
"min": 0.0,
"max": 1.0,
"palette": cmap2,
"description": "FVC",
},
{
"bands": ["EM"],
"min": 0.9,
"max": 1.0,
"palette": cmap1,
"description": "Emissivity",
},
{
"bands": ["B10"],
"min": 290,
"max": 320,
"palette": cmap1,
"description": "TIR_BT",
},
{
"bands": ["LST"],
"min": 290,
"max": 320,
"palette": cmap1,
"description": "LST",
},
{"bands": ["LST"], "description": "LST_Celsius_Raw"},
{
"bands": ["SR_B4", "SR_B3", "SR_B2"],
"min": 0,
"max": 0.3,
"description": "RGB",
},
]

for vis in visualizations:
if 'RGB' in vis['description']:
if "RGB" in vis["description"]:
# Special case for RGB. We don't need to apply a palette.
export_image = ex_image.select(vis['bands'])
elif vis['description'] == 'LST_Celsius_Raw':
export_image = ex_image.select(vis["bands"])
elif vis["description"] == "LST_Celsius_Raw":
# Special case for raw LST in Celsius.
export_image = kelvin_to_celsius(ex_image.select(vis['bands']))
export_image = kelvin_to_celsius(ex_image.select(vis["bands"]))
else:
export_image = ex_image.visualize(
bands=vis['bands'],
min=vis['min'],
max=vis['max'],
palette=vis['palette']
bands=vis["bands"],
min=vis["min"],
max=vis["max"],
palette=vis["palette"],
)

export_params = {
'image': export_image,
'description': vis['description'],
'scale': 30,
'region': geometry.getInfo()['coordinates'],
'fileFormat': 'GeoTIFF',
'folder': folder_name,
'crs': 'EPSG:4326',
'formatOptions': {
'cloudOptimized': True
}
"image": export_image,
"description": vis["description"],
"scale": 30,
"region": geometry.getInfo()["coordinates"],
"fileFormat": "GeoTIFF",
"folder": folder_name,
"crs": "EPSG:4326",
"formatOptions": {"cloudOptimized": True},
}

task = ee.batch.Export.image.toDrive(**export_params)
task.start()

while task.status()['state'] in ['READY', 'RUNNING']:
print('Task status:', task.status())
while task.status()["state"] in ["READY", "RUNNING"]:
print("Task status:", task.status())
time.sleep(30)

print('Task completed with state:', task.status()['state'])
print("Task completed with state:", task.status()["state"])
except Exception as e:
print(f"Error processing and exporting data: {e}")

Expand Down
13 changes: 7 additions & 6 deletions validation/geotiffs_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

def compare_images(img_path1, img_path2):
with rst.open(img_path1) as src1, rst.open(img_path2) as src2:

# Check and report size difference
h1, w1 = src1.shape
h2, w2 = src2.shape
Expand All @@ -27,9 +26,11 @@ def compare_images(img_path1, img_path2):
start_row_2 = h2 - target_h

data1 = src1.read(
1, window=rst.windows.Window(0, start_row_1, target_w, target_h))
1, window=rst.windows.Window(0, start_row_1, target_w, target_h)
)
data2 = src2.read(
1, window=rst.windows.Window(0, start_row_2, target_w, target_h))
1, window=rst.windows.Window(0, start_row_2, target_w, target_h)
)

# Calculate the difference
difference = data1 - data2
Expand Down Expand Up @@ -66,8 +67,8 @@ def compare_images(img_path1, img_path2):
# img2_path = "./nodejs_downloads/LST.tif"
# compare_images(img1_path, img2_path)

dir1 = './nodejs_downloads'
dir2 = './python_downloads'
dir1 = "./nodejs_downloads"
dir2 = "./python_downloads"

for filename in os.listdir(dir1):
if filename.endswith(".tif"):
Expand All @@ -77,6 +78,6 @@ def compare_images(img_path1, img_path2):
if os.path.exists(img2_path):
print(f"Comparing {filename}...")
compare_images(img1_path, img2_path)
print('-' * 40)
print("-" * 40)
else:
print(f"Warning: {filename} not found in {dir2}. Skipping comparison.")

0 comments on commit e8beaff

Please sign in to comment.