-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaw_To_Reflectance_EnMAP.py
86 lines (75 loc) · 3.4 KB
/
Raw_To_Reflectance_EnMAP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import requests
import os
import subprocess
import numpy as np
import rasterio
import matplotlib.pyplot as plt
# Constants
ENMAP_PORTAL_URL = "https://enmap.org/data_portal/download"
USERNAME = "your_username" # Replace with your username
PASSWORD = "your_password" # Replace with your password
L0_FILE_URL = "https://enmap.org/data/L0_sample_file_path" # Replace with the L0 data file URL
DOWNLOAD_DIR = "./enmap_data"
L0_FILE_PATH = os.path.join(DOWNLOAD_DIR, "enmap_l0_data.tar.gz")
L2_OUTPUT_DIR = "./enmap_reflectance"
RAW_RADIANCE_FILE = "./enmap_data/raw_radiance.tif" # Replace with the actual radiance file path
REFLECTANCE_FILE = "./enmap_reflectance/reflectance.tif" # Replace with the actual reflectance file path
# Function to download EnMAP data
def download_enmap_data(url, username, password, output_path):
"""Download EnMAP data using HTTP Basic Authentication."""
with requests.get(url, auth=(username, password), stream=True) as response:
if response.status_code == 200:
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Downloaded: {output_path}")
else:
print(f"Failed to download file. HTTP Status: {response.status_code}, {response.text}")
# Function to convert L0 to L2
def convert_l0_to_l2(l0_file_path, output_dir):
"""Convert L0 data to L2 reflectance using EnMAP-Box or a custom tool."""
os.makedirs(output_dir, exist_ok=True)
# Assuming you have a command-line tool for L0 to L2 conversion
# Replace `enmap_converter` with the actual command or script
command = [
"enmap_converter", # Replace with the actual tool
"-i", l0_file_path,
"-o", output_dir,
]
try:
subprocess.run(command, check=True)
print(f"Conversion to L2 completed. Output saved to {output_dir}")
except subprocess.CalledProcessError as e:
print(f"Error during conversion: {e}")
# Function to plot raster data
def plot_hyperspectral_data(file_path, title, band_index=0):
"""Plot a single band of hyperspectral data."""
with rasterio.open(file_path) as src:
data = src.read(band_index + 1) # Read the specific band (1-based index)
plt.figure(figsize=(10, 8))
plt.imshow(data, cmap="viridis")
plt.colorbar(label="Value")
plt.title(f"{title} (Band {band_index + 1})")
plt.xlabel("X Pixel")
plt.ylabel("Y Pixel")
plt.show()
# Main Workflow
if __name__ == "__main__":
# Step 1: Download L0 data
download_enmap_data(L0_FILE_URL, USERNAME, PASSWORD, L0_FILE_PATH)
# Step 2: Convert L0 to L2 data
if os.path.exists(L0_FILE_PATH):
convert_l0_to_l2(L0_FILE_PATH, L2_OUTPUT_DIR)
else:
print("L0 file not found. Skipping conversion.")
# Step 3: Plot raw radiance data
if os.path.exists(RAW_RADIANCE_FILE):
plot_hyperspectral_data(RAW_RADIANCE_FILE, "Raw Radiance Data", band_index=0)
else:
print("Raw radiance file not found.")
# Step 4: Plot reflectance data
if os.path.exists(REFLECTANCE_FILE):
plot_hyperspectral_data(REFLECTANCE_FILE, "Reflectance Data", band_index=0)
else:
print("Reflectance file not found.")